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