Ray Milkey | 3275ae8 | 2018-05-29 15:35:36 -0700 | [diff] [blame] | 1 | """ |
| 2 | Copyright 2018-present Open Networking Foundation |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | """ |
| 16 | |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 17 | load("//tools/build/bazel:generate_workspace.bzl", "COMPILE", "TEST") |
| 18 | load("//tools/build/bazel:variables.bzl", "ONOS_VERSION") |
| 19 | load("//tools/build/bazel:generate_test_rules.bzl", "generate_test_rules") |
Ray Milkey | b7949e7 | 2018-06-19 18:31:02 -0700 | [diff] [blame] | 20 | load("//tools/build/bazel:checkstyle.bzl", "checkstyle_test") |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 21 | |
Ray Milkey | 32ea35c | 2018-06-06 15:28:07 -0700 | [diff] [blame] | 22 | def _all_java_sources(): |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 23 | return native.glob(["src/main/java/**/*.java"]) |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 24 | |
Ray Milkey | 32ea35c | 2018-06-06 15:28:07 -0700 | [diff] [blame] | 25 | def _all_java_test_sources(): |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 26 | return native.glob(["src/test/java/**/*.java"]) |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 27 | |
Ray Milkey | 32ea35c | 2018-06-06 15:28:07 -0700 | [diff] [blame] | 28 | def _all_test_resources(): |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 29 | return native.glob(["src/test/resources/**"]) |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 30 | |
Ray Milkey | 32ea35c | 2018-06-06 15:28:07 -0700 | [diff] [blame] | 31 | def _all_resources(resources_root): |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 32 | if resources_root == None: |
| 33 | return native.glob(["src/main/resources/**"]) |
| 34 | else: |
| 35 | return native.glob([resources_root + "**"]) |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 36 | |
Ray Milkey | 25747d8 | 2018-06-13 14:12:51 -0700 | [diff] [blame] | 37 | def _webapp(): |
Ray Milkey | 6b3775a | 2018-06-28 11:18:44 -0700 | [diff] [blame] | 38 | return native.glob(["src/main/webapp/WEB-INF/web.xml"]) |
| 39 | |
| 40 | def _include_resources_to_string(include_resources): |
| 41 | result = "" |
| 42 | for (path, filename) in include_resources.items(): |
| 43 | result += (path + "=" + filename) |
| 44 | return result |
Ray Milkey | 25747d8 | 2018-06-13 14:12:51 -0700 | [diff] [blame] | 45 | |
Ray Milkey | 275af2c | 2018-06-15 13:05:08 -0700 | [diff] [blame] | 46 | """ |
| 47 | Implementation of the rule to call bnd to make an OSGI jar file |
| 48 | """ |
Ray Milkey | 6b3775a | 2018-06-28 11:18:44 -0700 | [diff] [blame] | 49 | |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 50 | def _bnd_impl(ctx): |
Ray Milkey | 3275ae8 | 2018-05-29 15:35:36 -0700 | [diff] [blame] | 51 | if (len(ctx.files.source) == 1): |
| 52 | input_file = ctx.files.source[0] |
| 53 | else: |
| 54 | # this is a list of inputs. The one we want is the last one |
| 55 | # in the list that isn't a source jar |
| 56 | for file in reversed(ctx.files.source): |
| 57 | if ("-src" in file.path): |
| 58 | continue |
| 59 | else: |
| 60 | input_file = file |
| 61 | break |
| 62 | |
| 63 | jar = input_file.path |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 64 | output = ctx.outputs.osgi_jar.path |
Thomas Vachuska | 8e022a9 | 2018-07-10 14:47:38 -0700 | [diff] [blame] | 65 | name = ctx.attr.name |
Ray Milkey | 275af2c | 2018-06-15 13:05:08 -0700 | [diff] [blame] | 66 | group = ctx.attr.group |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 67 | version = ctx.attr.version |
| 68 | license = "" |
Ray Milkey | 12ae6ca | 2018-06-11 15:34:30 -0700 | [diff] [blame] | 69 | import_packages = ctx.attr.import_packages |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 70 | exportPackages = "*" |
Ray Milkey | 6b3775a | 2018-06-28 11:18:44 -0700 | [diff] [blame] | 71 | include_resources = ctx.attr.include_resources |
Ray Milkey | 25747d8 | 2018-06-13 14:12:51 -0700 | [diff] [blame] | 72 | web_context = ctx.attr.web_context |
| 73 | if web_context == None or web_context == "": |
| 74 | web_context = "NONE" |
| 75 | web_xml = ctx.attr.web_xml |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 76 | dynamicimportPackages = "" |
Ray Milkey | 3275ae8 | 2018-05-29 15:35:36 -0700 | [diff] [blame] | 77 | cp = "" |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 78 | |
Ray Milkey | 3275ae8 | 2018-05-29 15:35:36 -0700 | [diff] [blame] | 79 | inputDependencies = [input_file] |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 80 | |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 81 | # determine the dependencies and build the class path |
| 82 | for dep in ctx.attr.deps: |
Ray Milkey | 25b785a | 2018-06-12 09:59:14 -0700 | [diff] [blame] | 83 | if java_common.provider in dep: |
| 84 | file = dep.files.to_list()[0] |
Ray Milkey | 472d839 | 2018-05-23 17:06:51 -0700 | [diff] [blame] | 85 | |
Ray Milkey | 25b785a | 2018-06-12 09:59:14 -0700 | [diff] [blame] | 86 | if cp: |
| 87 | cp += ":" |
| 88 | cp += file.path |
| 89 | inputDependencies = inputDependencies + [file] |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 90 | |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 91 | # extract the class files for use by bnd |
Ray Milkey | 3275ae8 | 2018-05-29 15:35:36 -0700 | [diff] [blame] | 92 | classes = ctx.actions.declare_file("classes" + ctx.label.name.replace("/", "-")) |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 93 | classesPath = classes.path |
| 94 | jarCommand = "mkdir -p %s && cp %s %s && cd %s && jar xf *.jar" % (classesPath, jar, classesPath, classesPath) |
| 95 | ctx.actions.run_shell( |
| 96 | inputs = inputDependencies, |
| 97 | outputs = [classes], |
| 98 | command = jarCommand, |
| 99 | progress_message = "Expanding jar file: %s" % jar, |
| 100 | ) |
| 101 | inputDependencies += [classes] |
Ray Milkey | 25747d8 | 2018-06-13 14:12:51 -0700 | [diff] [blame] | 102 | web_xml_root_path = "" |
| 103 | if len(web_xml) != 0: |
| 104 | web_xml_root = web_xml[0].files.to_list()[0] |
| 105 | inputDependencies += [web_xml_root] |
| 106 | web_xml_root_path = web_xml_root.path.replace("WEB-INF/web.xml", "") |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 107 | |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 108 | # call bnd to make the OSGI jar file |
| 109 | arguments = [ |
| 110 | jar, |
| 111 | output, |
| 112 | cp, |
| 113 | name, |
| 114 | group, |
| 115 | version, |
| 116 | license, |
Ray Milkey | 12ae6ca | 2018-06-11 15:34:30 -0700 | [diff] [blame] | 117 | import_packages, |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 118 | exportPackages, |
Ray Milkey | 6b3775a | 2018-06-28 11:18:44 -0700 | [diff] [blame] | 119 | include_resources, |
Ray Milkey | 25747d8 | 2018-06-13 14:12:51 -0700 | [diff] [blame] | 120 | web_context, |
| 121 | web_xml_root_path, |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 122 | dynamicimportPackages, |
| 123 | classesPath, |
| 124 | ] |
Ray Milkey | 12ae6ca | 2018-06-11 15:34:30 -0700 | [diff] [blame] | 125 | |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 126 | ctx.actions.run( |
| 127 | inputs = inputDependencies, |
| 128 | outputs = [ctx.outputs.osgi_jar], |
| 129 | arguments = arguments, |
| 130 | progress_message = "Running bnd wrapper on: %s" % ctx.attr.name, |
| 131 | executable = ctx.executable._bnd_exe, |
| 132 | ) |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 133 | |
Ray Milkey | 25b785a | 2018-06-12 09:59:14 -0700 | [diff] [blame] | 134 | deps = [] |
| 135 | if java_common.provider in ctx.attr.source: |
| 136 | deps.append(ctx.attr.source[java_common.provider]) |
| 137 | deps_provider = java_common.merge(deps) |
| 138 | return struct( |
Ray Milkey | 15053f0 | 2018-06-13 10:00:45 -0700 | [diff] [blame] | 139 | providers = [deps_provider], |
Ray Milkey | 25b785a | 2018-06-12 09:59:14 -0700 | [diff] [blame] | 140 | ) |
| 141 | |
Ray Milkey | 275af2c | 2018-06-15 13:05:08 -0700 | [diff] [blame] | 142 | """ |
| 143 | Rule definition for calling bnd to make an OSGi jar file. |
| 144 | """ |
Ray Milkey | 32ea35c | 2018-06-06 15:28:07 -0700 | [diff] [blame] | 145 | _bnd = rule( |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 146 | attrs = { |
| 147 | "deps": attr.label_list(), |
| 148 | "version": attr.string(), |
Ray Milkey | 275af2c | 2018-06-15 13:05:08 -0700 | [diff] [blame] | 149 | "group": attr.string(), |
Ray Milkey | 3275ae8 | 2018-05-29 15:35:36 -0700 | [diff] [blame] | 150 | "source": attr.label(), |
Ray Milkey | 12ae6ca | 2018-06-11 15:34:30 -0700 | [diff] [blame] | 151 | "import_packages": attr.string(), |
Ray Milkey | 25747d8 | 2018-06-13 14:12:51 -0700 | [diff] [blame] | 152 | "web_context": attr.string(), |
| 153 | "web_xml": attr.label_list(allow_files = True), |
Ray Milkey | 6b3775a | 2018-06-28 11:18:44 -0700 | [diff] [blame] | 154 | "include_resources": attr.string(), |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 155 | "_bnd_exe": attr.label( |
| 156 | executable = True, |
| 157 | cfg = "host", |
| 158 | allow_files = True, |
| 159 | default = Label("//utils/osgiwrap:osgi-jar"), |
| 160 | ), |
| 161 | }, |
| 162 | fragments = ["java"], |
| 163 | outputs = { |
| 164 | "osgi_jar": "lib%{name}.jar", |
| 165 | }, |
| 166 | implementation = _bnd_impl, |
| 167 | ) |
| 168 | |
Ray Milkey | 275af2c | 2018-06-15 13:05:08 -0700 | [diff] [blame] | 169 | """ |
| 170 | Implementation of the rule to call swagger generator to create the registrator java class source |
| 171 | """ |
Ray Milkey | 6b3775a | 2018-06-28 11:18:44 -0700 | [diff] [blame] | 172 | |
Ray Milkey | 275af2c | 2018-06-15 13:05:08 -0700 | [diff] [blame] | 173 | def _swagger_java_impl(ctx): |
| 174 | api_title = ctx.attr.api_title |
| 175 | api_version = ctx.attr.api_version |
| 176 | api_description = ctx.attr.api_description |
| 177 | api_package = ctx.attr.api_package |
| 178 | web_context = ctx.attr.web_context |
| 179 | |
| 180 | output_java = ctx.outputs.swagger_java.path |
Ray Milkey | 6b3775a | 2018-06-28 11:18:44 -0700 | [diff] [blame] | 181 | output_dir = output_java[:output_java.find("generated-sources")] |
Ray Milkey | 275af2c | 2018-06-15 13:05:08 -0700 | [diff] [blame] | 182 | |
| 183 | package_name = ctx.attr.package_name |
| 184 | |
| 185 | srcs_arg = "" |
| 186 | resources_arg = "" |
| 187 | input_dependencies = [] |
| 188 | |
| 189 | for file in ctx.files.srcs: |
| 190 | srcs_arg += file.path + "," |
| 191 | input_dependencies.append(file) |
| 192 | |
| 193 | for resource in resources_arg: |
| 194 | resources_arg += resource.path + "," |
| 195 | |
| 196 | # call swagger generator to make the swagger JSON and java files |
| 197 | arguments = [ |
| 198 | srcs_arg, |
| 199 | resources_arg, |
| 200 | "", |
| 201 | package_name + "/src/main/resources", |
| 202 | output_dir, |
| 203 | output_dir, |
| 204 | web_context, |
| 205 | api_title, |
| 206 | api_version, |
| 207 | api_package, |
| 208 | api_description, |
| 209 | ] |
| 210 | |
| 211 | ctx.actions.run( |
| 212 | inputs = ctx.files.srcs, |
| 213 | outputs = [ctx.outputs.swagger_java], |
| 214 | arguments = arguments, |
| 215 | progress_message = "Running swagger generator on: %s" % ctx.attr.name, |
| 216 | executable = ctx.executable._swagger_generator_exe, |
| 217 | ) |
| 218 | |
| 219 | """ |
| 220 | Implementation of the rule to call swagger generator for swagger.json file |
| 221 | """ |
Ray Milkey | 6b3775a | 2018-06-28 11:18:44 -0700 | [diff] [blame] | 222 | |
Ray Milkey | 275af2c | 2018-06-15 13:05:08 -0700 | [diff] [blame] | 223 | def _swagger_json_impl(ctx): |
| 224 | api_title = ctx.attr.api_title |
| 225 | api_version = ctx.attr.api_version |
| 226 | api_description = ctx.attr.api_description |
| 227 | api_package = ctx.attr.api_package |
| 228 | web_context = ctx.attr.web_context |
| 229 | |
| 230 | output_json = ctx.outputs.swagger_json.path |
| 231 | output_dir = output_json[:output_json.find("swagger.json")] |
| 232 | |
| 233 | package_name = ctx.attr.package_name |
| 234 | |
| 235 | srcs_arg = "" |
| 236 | resources_arg = "" |
| 237 | input_dependencies = [] |
| 238 | |
| 239 | for file in ctx.files.srcs: |
| 240 | srcs_arg += file.path + "," |
| 241 | input_dependencies.append(file) |
| 242 | |
| 243 | for resource in resources_arg: |
| 244 | resources_arg += resource.path + "," |
| 245 | |
| 246 | # call swagger generator to make the swagger JSON and java files |
| 247 | arguments = [ |
| 248 | srcs_arg, |
| 249 | resources_arg, |
| 250 | "", |
| 251 | package_name + "/src/main/resources", |
| 252 | output_dir, |
| 253 | output_dir, |
| 254 | web_context, |
| 255 | api_title, |
| 256 | api_version, |
| 257 | api_package, |
| 258 | api_description, |
| 259 | ] |
| 260 | |
| 261 | ctx.actions.run( |
| 262 | inputs = ctx.files.srcs, |
| 263 | outputs = [ctx.outputs.swagger_json], |
| 264 | arguments = arguments, |
| 265 | progress_message = "Running swagger generator on: %s" % ctx.attr.name, |
| 266 | executable = ctx.executable._swagger_generator_exe, |
| 267 | ) |
| 268 | |
| 269 | """ |
| 270 | Rule definition to call swagger generator to create the registrator java class source |
| 271 | """ |
| 272 | _swagger_java = rule( |
| 273 | attrs = { |
| 274 | "srcs": attr.label_list(allow_files = True), |
| 275 | "package_name": attr.string(), |
| 276 | "api_title": attr.string(), |
| 277 | "api_version": attr.string(), |
| 278 | "api_description": attr.string(), |
| 279 | "api_package": attr.string(), |
| 280 | "web_context": attr.string(), |
| 281 | "_swagger_generator_exe": attr.label( |
| 282 | executable = True, |
| 283 | cfg = "host", |
| 284 | allow_files = True, |
| 285 | default = Label("//tools/build/buck-plugin:swagger_generator"), |
| 286 | ), |
| 287 | "swagger_java": attr.output(), |
| 288 | }, |
| 289 | fragments = ["java"], |
| 290 | implementation = _swagger_java_impl, |
| 291 | ) |
| 292 | |
| 293 | """ |
| 294 | Rule definition to call swagger generator to create the swagger JSON |
| 295 | """ |
| 296 | _swagger_json = rule( |
| 297 | attrs = { |
| 298 | "srcs": attr.label_list(allow_files = True), |
| 299 | "package_name": attr.string(), |
| 300 | "api_title": attr.string(), |
| 301 | "api_version": attr.string(), |
| 302 | "api_description": attr.string(), |
| 303 | "api_package": attr.string(), |
| 304 | "web_context": attr.string(), |
| 305 | "_swagger_generator_exe": attr.label( |
| 306 | executable = True, |
| 307 | cfg = "host", |
| 308 | allow_files = True, |
| 309 | default = Label("//tools/build/buck-plugin:swagger_generator"), |
| 310 | ), |
| 311 | "swagger_json": attr.output(), |
| 312 | }, |
| 313 | fragments = ["java"], |
| 314 | implementation = _swagger_json_impl, |
| 315 | ) |
| 316 | |
| 317 | """ |
| 318 | Converts a jar file to an OSGI compatible jar file. |
| 319 | |
| 320 | Args: |
| 321 | name: name of the rule to create the OSGI jar file - required |
| 322 | jar: jar file to convert - required target |
| 323 | deps: dependencies needed by the jar file - required list of targets |
| 324 | version: Version of the generated jar file. Optional, defaults to the current ONOS version |
| 325 | group: Maven group ID for the resulting jar file. Optional, defaults to 'org.onosproject' |
| 326 | import_packages: OSGI import list. Optional, comma separated list, defaults to "*" |
| 327 | visibility: Visibility of the produced jar file to other BUILDs. Optional, defaults to private |
| 328 | """ |
Ray Milkey | 6b3775a | 2018-06-28 11:18:44 -0700 | [diff] [blame] | 329 | |
Ray Milkey | 25747d8 | 2018-06-13 14:12:51 -0700 | [diff] [blame] | 330 | def wrapped_osgi_jar( |
| 331 | name, |
| 332 | jar, |
| 333 | deps, |
| 334 | version = ONOS_VERSION, |
Ray Milkey | 275af2c | 2018-06-15 13:05:08 -0700 | [diff] [blame] | 335 | group = "org.onosproject", |
Ray Milkey | 25747d8 | 2018-06-13 14:12:51 -0700 | [diff] [blame] | 336 | import_packages = "*", |
Ray Milkey | 25747d8 | 2018-06-13 14:12:51 -0700 | [diff] [blame] | 337 | visibility = ["//visibility:private"]): |
| 338 | _bnd( |
| 339 | name = name, |
| 340 | source = jar, |
| 341 | deps = deps, |
| 342 | version = version, |
Ray Milkey | 275af2c | 2018-06-15 13:05:08 -0700 | [diff] [blame] | 343 | group = group, |
Ray Milkey | 25747d8 | 2018-06-13 14:12:51 -0700 | [diff] [blame] | 344 | visibility = visibility, |
| 345 | import_packages = import_packages, |
Ray Milkey | 275af2c | 2018-06-15 13:05:08 -0700 | [diff] [blame] | 346 | web_xml = None, |
Ray Milkey | 25747d8 | 2018-06-13 14:12:51 -0700 | [diff] [blame] | 347 | ) |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 348 | |
Ray Milkey | 275af2c | 2018-06-15 13:05:08 -0700 | [diff] [blame] | 349 | """ |
| 350 | Creates an OSGI jar and test jar file from a set of source and test files. |
| 351 | See osgi_jar() for a description of shared parameters. |
| 352 | Args: |
| 353 | test_srcs: Test source file(s) to compile. Optional list of targets, defaults to src/test/java/**/*.java |
| 354 | test_deps: Dependencies for the test jar. Optional list of targets, defaults to a common set of dependencies |
| 355 | test_resources: Resources to include in the test jar. Optional list of targets, defaults to src/test/resources/** |
| 356 | exclude_tests: Tests that should not be run. Useful for excluding things like test files without any @Test methods. |
| 357 | Optional ist of targets, defaults to [] |
| 358 | """ |
Ray Milkey | 6b3775a | 2018-06-28 11:18:44 -0700 | [diff] [blame] | 359 | |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 360 | def osgi_jar_with_tests( |
| 361 | name = None, |
| 362 | deps = None, |
| 363 | test_deps = None, |
Ray Milkey | 275af2c | 2018-06-15 13:05:08 -0700 | [diff] [blame] | 364 | group = "org.onosproject", |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 365 | srcs = None, |
| 366 | resources_root = None, |
| 367 | resources = None, |
Thomas Vachuska | f8c8cb9 | 2018-07-11 17:12:43 -0700 | [diff] [blame] | 368 | resource_jars = [], |
Ray Milkey | 6b3775a | 2018-06-28 11:18:44 -0700 | [diff] [blame] | 369 | include_resources = {}, |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 370 | test_srcs = None, |
| 371 | exclude_tests = None, |
| 372 | test_resources = None, |
| 373 | visibility = ["//visibility:public"], |
Ray Milkey | 12ae6ca | 2018-06-11 15:34:30 -0700 | [diff] [blame] | 374 | version = ONOS_VERSION, |
Thomas Vachuska | 5b9ff6a | 2018-07-13 11:00:50 -0700 | [diff] [blame] | 375 | suppress_errorprone = False, |
Ray Milkey | 134d292 | 2018-07-15 15:24:01 -0700 | [diff] [blame] | 376 | suppress_checkstyle = False, |
Ray Milkey | 25747d8 | 2018-06-13 14:12:51 -0700 | [diff] [blame] | 377 | web_context = None, |
| 378 | api_title = "", |
| 379 | api_version = "", |
| 380 | api_description = "", |
| 381 | api_package = "", |
Ray Milkey | 15053f0 | 2018-06-13 10:00:45 -0700 | [diff] [blame] | 382 | import_packages = None): |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 383 | if name == None: |
| 384 | name = "onos-" + native.package_name().replace("/", "-") |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 385 | if srcs == None: |
Ray Milkey | 32ea35c | 2018-06-06 15:28:07 -0700 | [diff] [blame] | 386 | srcs = _all_java_sources() |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 387 | if resources == None: |
Ray Milkey | 32ea35c | 2018-06-06 15:28:07 -0700 | [diff] [blame] | 388 | resources = _all_resources(resources_root) |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 389 | if test_srcs == None: |
Ray Milkey | 32ea35c | 2018-06-06 15:28:07 -0700 | [diff] [blame] | 390 | test_srcs = _all_java_test_sources() |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 391 | if test_resources == None: |
Ray Milkey | 32ea35c | 2018-06-06 15:28:07 -0700 | [diff] [blame] | 392 | test_resources = _all_test_resources() |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 393 | if exclude_tests == None: |
| 394 | exclude_tests = [] |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 395 | if deps == None: |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 396 | deps = COMPILE |
| 397 | if test_deps == None: |
| 398 | test_deps = TEST |
Ray Milkey | 12ae6ca | 2018-06-11 15:34:30 -0700 | [diff] [blame] | 399 | if import_packages == None: |
| 400 | import_packages = "*" |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 401 | tests_name = name + "-tests" |
| 402 | tests_jar_deps = list(depset(deps + test_deps)) + [name] |
| 403 | all_test_deps = tests_jar_deps + [tests_name] |
Ray Milkey | 25747d8 | 2018-06-13 14:12:51 -0700 | [diff] [blame] | 404 | web_xml = _webapp() |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 405 | |
Ray Milkey | 275af2c | 2018-06-15 13:05:08 -0700 | [diff] [blame] | 406 | native_srcs = srcs |
| 407 | native_resources = resources |
Ray Milkey | 6b3775a | 2018-06-28 11:18:44 -0700 | [diff] [blame] | 408 | if web_context != None and api_title != "" and len(resources) != 0: |
Ray Milkey | 275af2c | 2018-06-15 13:05:08 -0700 | [diff] [blame] | 409 | # generate Swagger files if needed |
| 410 | _swagger_java( |
| 411 | name = name + "_swagger_java", |
| 412 | srcs = srcs + resources, |
| 413 | package_name = native.package_name(), |
| 414 | api_title = api_title, |
| 415 | api_version = api_version, |
| 416 | api_description = api_description, |
| 417 | web_context = web_context, |
| 418 | api_package = api_package, |
| 419 | swagger_java = ("src/main/resources/apidoc/generated-sources/" + |
Ray Milkey | 6b3775a | 2018-06-28 11:18:44 -0700 | [diff] [blame] | 420 | api_package.replace(".", "/") + |
| 421 | "/ApiDocRegistrator.java").replace("//", "/"), |
Ray Milkey | 275af2c | 2018-06-15 13:05:08 -0700 | [diff] [blame] | 422 | ) |
| 423 | _swagger_json( |
| 424 | name = name + "_swagger_json", |
| 425 | srcs = srcs + resources, |
| 426 | package_name = native.package_name(), |
| 427 | api_title = api_title, |
| 428 | api_version = api_version, |
| 429 | api_description = api_description, |
| 430 | web_context = web_context, |
| 431 | api_package = api_package, |
| 432 | swagger_json = "src/main/resources/apidoc/swagger.json", |
| 433 | ) |
| 434 | native_resources = [] |
| 435 | for r in resources: |
Ray Milkey | 6b3775a | 2018-06-28 11:18:44 -0700 | [diff] [blame] | 436 | if not "definitions" in r: |
Ray Milkey | 275af2c | 2018-06-15 13:05:08 -0700 | [diff] [blame] | 437 | native_resources.append(r) |
Ray Milkey | 6b3775a | 2018-06-28 11:18:44 -0700 | [diff] [blame] | 438 | native_srcs = srcs + [name + "_swagger_java"] |
| 439 | native_resources.append(name + "_swagger_json") |
Ray Milkey | 275af2c | 2018-06-15 13:05:08 -0700 | [diff] [blame] | 440 | |
Thomas Vachuska | 5b9ff6a | 2018-07-13 11:00:50 -0700 | [diff] [blame] | 441 | javacopts = [ "-XepDisableAllChecks" ] if suppress_errorprone else [] |
| 442 | |
Ray Milkey | 25747d8 | 2018-06-13 14:12:51 -0700 | [diff] [blame] | 443 | # compile the Java code |
Thomas Vachuska | f8c8cb9 | 2018-07-11 17:12:43 -0700 | [diff] [blame] | 444 | if len(resource_jars) > 0: |
| 445 | native.java_library(name = name + "-native", srcs = native_srcs, resource_jars = resource_jars, |
Thomas Vachuska | 5b9ff6a | 2018-07-13 11:00:50 -0700 | [diff] [blame] | 446 | deps = deps, visibility = visibility, javacopts = javacopts) |
Thomas Vachuska | f8c8cb9 | 2018-07-11 17:12:43 -0700 | [diff] [blame] | 447 | else: |
| 448 | native.java_library(name = name + "-native", srcs = native_srcs, resources = native_resources, |
Thomas Vachuska | 5b9ff6a | 2018-07-13 11:00:50 -0700 | [diff] [blame] | 449 | deps = deps, visibility = visibility, javacopts = javacopts) |
Ray Milkey | 25747d8 | 2018-06-13 14:12:51 -0700 | [diff] [blame] | 450 | |
Ray Milkey | 32ea35c | 2018-06-06 15:28:07 -0700 | [diff] [blame] | 451 | _bnd( |
Ray Milkey | 25b785a | 2018-06-12 09:59:14 -0700 | [diff] [blame] | 452 | name = name, |
| 453 | source = name + "-native", |
Ray Milkey | 32ea35c | 2018-06-06 15:28:07 -0700 | [diff] [blame] | 454 | deps = deps, |
| 455 | version = version, |
Ray Milkey | 275af2c | 2018-06-15 13:05:08 -0700 | [diff] [blame] | 456 | group = group, |
Ray Milkey | 32ea35c | 2018-06-06 15:28:07 -0700 | [diff] [blame] | 457 | visibility = visibility, |
Ray Milkey | 12ae6ca | 2018-06-11 15:34:30 -0700 | [diff] [blame] | 458 | import_packages = import_packages, |
Ray Milkey | 25747d8 | 2018-06-13 14:12:51 -0700 | [diff] [blame] | 459 | web_context = web_context, |
| 460 | web_xml = web_xml, |
Ray Milkey | 6b3775a | 2018-06-28 11:18:44 -0700 | [diff] [blame] | 461 | include_resources = _include_resources_to_string(include_resources), |
Ray Milkey | 32ea35c | 2018-06-06 15:28:07 -0700 | [diff] [blame] | 462 | ) |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 463 | if test_srcs != []: |
| 464 | native.java_library( |
| 465 | name = tests_name, |
| 466 | srcs = test_srcs, |
| 467 | resources = test_resources, |
| 468 | deps = tests_jar_deps, |
| 469 | visibility = visibility, |
| 470 | ) |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 471 | |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 472 | generate_test_rules( |
| 473 | name = name + "-tests-gen", |
| 474 | test_files = test_srcs, |
| 475 | exclude_tests = exclude_tests, |
| 476 | deps = all_test_deps, |
| 477 | ) |
| 478 | |
Ray Milkey | 134d292 | 2018-07-15 15:24:01 -0700 | [diff] [blame] | 479 | if not suppress_checkstyle: |
| 480 | checkstyle_test( |
| 481 | name = name + "_checkstyle_test", |
| 482 | srcs = srcs, |
| 483 | ) |
Ray Milkey | b7949e7 | 2018-06-19 18:31:02 -0700 | [diff] [blame] | 484 | |
Ray Milkey | 275af2c | 2018-06-15 13:05:08 -0700 | [diff] [blame] | 485 | """ |
| 486 | Creates an OSGI jar file from a set of source files. |
| 487 | |
| 488 | Args: |
| 489 | name: Name of the rule to generate. Optional, defaults to a name based on the location in the source tree. |
| 490 | For example apps/mcast/app becomes onos-apps-mcast-app |
| 491 | deps: Dependencies of the generated jar file. Expressed as a list of targets |
| 492 | import_packages: OSGI import list. Optional, comma separated list, defaults to "*" |
| 493 | group: Maven group ID for the resulting jar file. Optional, defaults to 'org.onosproject' |
| 494 | srcs: Source file(s) to compile. Optional list of targets, defaults to src/main/java/**/*.java |
| 495 | resources_root: Relative path to the root of the tree of resources for this jar. Optional, defaults to src/main/resources |
| 496 | resources: Resources to include in the jar file. Optional list of targets, defaults to all files beneath resources_root |
| 497 | visibility: Visibility of the produced jar file to other BUILDs. Optional, defaults to public |
| 498 | version: Version of the generated jar file. Optional, defaults to the current ONOS version |
Ray Milkey | 134d292 | 2018-07-15 15:24:01 -0700 | [diff] [blame] | 499 | suppress_errorprone: If true, don't run ErrorProne tests. Default is false |
| 500 | suppress_checkstyle: If true, don't run checkstyle tests. Default is false |
Ray Milkey | 275af2c | 2018-06-15 13:05:08 -0700 | [diff] [blame] | 501 | web_context: Web context for a WAB file if needed. Only needed if the jar file provides a REST API. Optional string |
| 502 | api_title: Swagger API title. Optional string, only used if the jar file provides a REST API and has swagger annotations |
| 503 | api_version: Swagger API version. Optional string, only used if the jar file provides a REST API and has swagger annotations |
| 504 | api_description: Swagger API description. Optional string, only used if the jar file provides a REST API and has swagger annotations |
| 505 | api_package: Swagger API package name. Optional string, only used if the jar file provides a REST API and has swagger annotations |
| 506 | """ |
Ray Milkey | 6b3775a | 2018-06-28 11:18:44 -0700 | [diff] [blame] | 507 | |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 508 | def osgi_jar( |
| 509 | name = None, |
| 510 | deps = None, |
Ray Milkey | 12ae6ca | 2018-06-11 15:34:30 -0700 | [diff] [blame] | 511 | import_packages = None, |
Ray Milkey | 275af2c | 2018-06-15 13:05:08 -0700 | [diff] [blame] | 512 | group = "org.onosproject", |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 513 | srcs = None, |
| 514 | resources_root = None, |
| 515 | resources = None, |
Thomas Vachuska | f8c8cb9 | 2018-07-11 17:12:43 -0700 | [diff] [blame] | 516 | resource_jars = [], |
Ray Milkey | 6b3775a | 2018-06-28 11:18:44 -0700 | [diff] [blame] | 517 | include_resources = {}, |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 518 | visibility = ["//visibility:public"], |
Ray Milkey | 12ae6ca | 2018-06-11 15:34:30 -0700 | [diff] [blame] | 519 | version = ONOS_VERSION, |
Thomas Vachuska | 5b9ff6a | 2018-07-13 11:00:50 -0700 | [diff] [blame] | 520 | suppress_errorprone = False, |
Ray Milkey | 134d292 | 2018-07-15 15:24:01 -0700 | [diff] [blame] | 521 | suppress_checkstyle = False, |
Ray Milkey | 25747d8 | 2018-06-13 14:12:51 -0700 | [diff] [blame] | 522 | web_context = None, |
Ray Milkey | 12ae6ca | 2018-06-11 15:34:30 -0700 | [diff] [blame] | 523 | api_title = "", |
| 524 | api_version = "", |
| 525 | api_description = "", |
Ray Milkey | 15053f0 | 2018-06-13 10:00:45 -0700 | [diff] [blame] | 526 | api_package = ""): |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 527 | if srcs == None: |
Ray Milkey | 32ea35c | 2018-06-06 15:28:07 -0700 | [diff] [blame] | 528 | srcs = _all_java_sources() |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 529 | if deps == None: |
| 530 | deps = COMPILE |
| 531 | |
| 532 | osgi_jar_with_tests( |
| 533 | name = name, |
| 534 | deps = deps, |
| 535 | test_deps = [], |
Ray Milkey | 275af2c | 2018-06-15 13:05:08 -0700 | [diff] [blame] | 536 | group = group, |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 537 | srcs = srcs, |
| 538 | resources = resources, |
| 539 | resources_root = resources_root, |
Thomas Vachuska | f8c8cb9 | 2018-07-11 17:12:43 -0700 | [diff] [blame] | 540 | resource_jars = resource_jars, |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 541 | test_srcs = [], |
| 542 | exclude_tests = [], |
| 543 | test_resources = [], |
| 544 | visibility = visibility, |
Thomas Vachuska | 5b9ff6a | 2018-07-13 11:00:50 -0700 | [diff] [blame] | 545 | suppress_errorprone = suppress_errorprone, |
Ray Milkey | 134d292 | 2018-07-15 15:24:01 -0700 | [diff] [blame] | 546 | suppress_checkstyle = suppress_checkstyle, |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 547 | version = version, |
Ray Milkey | 12ae6ca | 2018-06-11 15:34:30 -0700 | [diff] [blame] | 548 | import_packages = import_packages, |
Ray Milkey | 275af2c | 2018-06-15 13:05:08 -0700 | [diff] [blame] | 549 | api_title = api_title, |
| 550 | api_version = api_version, |
| 551 | api_description = api_description, |
| 552 | api_package = api_package, |
Ray Milkey | 25747d8 | 2018-06-13 14:12:51 -0700 | [diff] [blame] | 553 | web_context = web_context, |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 554 | ) |