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") |
Thomas Vachuska | ac9e524 | 2018-07-19 16:15:39 -0700 | [diff] [blame] | 21 | load("//tools/build/bazel:pom_file.bzl", "pom_file") |
Thomas Vachuska | 50ac098 | 2018-07-19 10:17:37 -0700 | [diff] [blame] | 22 | load("//tools/build/bazel:java_sources.bzl", "java_sources") |
Thomas Vachuska | 0f7d7a4 | 2018-07-18 15:23:40 -0700 | [diff] [blame] | 23 | load("//tools/build/bazel:javadoc.bzl", "javadoc") |
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_sources(): |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 26 | return native.glob(["src/main/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_java_test_sources(): |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 29 | return native.glob(["src/test/java/**/*.java"]) |
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_test_resources(): |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 32 | return native.glob(["src/test/resources/**"]) |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 33 | |
Ray Milkey | 32ea35c | 2018-06-06 15:28:07 -0700 | [diff] [blame] | 34 | def _all_resources(resources_root): |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 35 | if resources_root == None: |
| 36 | return native.glob(["src/main/resources/**"]) |
| 37 | else: |
| 38 | return native.glob([resources_root + "**"]) |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 39 | |
Ray Milkey | 25747d8 | 2018-06-13 14:12:51 -0700 | [diff] [blame] | 40 | def _webapp(): |
Ray Milkey | 6b3775a | 2018-06-28 11:18:44 -0700 | [diff] [blame] | 41 | return native.glob(["src/main/webapp/WEB-INF/web.xml"]) |
| 42 | |
| 43 | def _include_resources_to_string(include_resources): |
| 44 | result = "" |
| 45 | for (path, filename) in include_resources.items(): |
| 46 | result += (path + "=" + filename) |
| 47 | return result |
Ray Milkey | 25747d8 | 2018-06-13 14:12:51 -0700 | [diff] [blame] | 48 | |
Ray Milkey | 275af2c | 2018-06-15 13:05:08 -0700 | [diff] [blame] | 49 | """ |
| 50 | Implementation of the rule to call bnd to make an OSGI jar file |
| 51 | """ |
Ray Milkey | 6b3775a | 2018-06-28 11:18:44 -0700 | [diff] [blame] | 52 | |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 53 | def _bnd_impl(ctx): |
Ray Milkey | 3275ae8 | 2018-05-29 15:35:36 -0700 | [diff] [blame] | 54 | if (len(ctx.files.source) == 1): |
| 55 | input_file = ctx.files.source[0] |
| 56 | else: |
| 57 | # this is a list of inputs. The one we want is the last one |
| 58 | # in the list that isn't a source jar |
| 59 | for file in reversed(ctx.files.source): |
| 60 | if ("-src" in file.path): |
| 61 | continue |
| 62 | else: |
| 63 | input_file = file |
| 64 | break |
| 65 | |
| 66 | jar = input_file.path |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 67 | output = ctx.outputs.osgi_jar.path |
Thomas Vachuska | 8e022a9 | 2018-07-10 14:47:38 -0700 | [diff] [blame] | 68 | name = ctx.attr.name |
Ray Milkey | 275af2c | 2018-06-15 13:05:08 -0700 | [diff] [blame] | 69 | group = ctx.attr.group |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 70 | version = ctx.attr.version |
| 71 | license = "" |
Ray Milkey | 12ae6ca | 2018-06-11 15:34:30 -0700 | [diff] [blame] | 72 | import_packages = ctx.attr.import_packages |
Jian Li | 4ad8687 | 2018-08-05 03:34:35 +0900 | [diff] [blame] | 73 | bundle_classpath = ctx.attr.bundle_classpath |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 74 | exportPackages = "*" |
Ray Milkey | 6b3775a | 2018-06-28 11:18:44 -0700 | [diff] [blame] | 75 | include_resources = ctx.attr.include_resources |
Ray Milkey | 25747d8 | 2018-06-13 14:12:51 -0700 | [diff] [blame] | 76 | web_context = ctx.attr.web_context |
| 77 | if web_context == None or web_context == "": |
| 78 | web_context = "NONE" |
| 79 | web_xml = ctx.attr.web_xml |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 80 | dynamicimportPackages = "" |
Ray Milkey | 3275ae8 | 2018-05-29 15:35:36 -0700 | [diff] [blame] | 81 | cp = "" |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 82 | |
Ray Milkey | 3275ae8 | 2018-05-29 15:35:36 -0700 | [diff] [blame] | 83 | inputDependencies = [input_file] |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 84 | |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 85 | # determine the dependencies and build the class path |
| 86 | for dep in ctx.attr.deps: |
Ray Milkey | 25b785a | 2018-06-12 09:59:14 -0700 | [diff] [blame] | 87 | if java_common.provider in dep: |
| 88 | file = dep.files.to_list()[0] |
Ray Milkey | 25b785a | 2018-06-12 09:59:14 -0700 | [diff] [blame] | 89 | if cp: |
| 90 | cp += ":" |
| 91 | cp += file.path |
| 92 | inputDependencies = inputDependencies + [file] |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 93 | |
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, |
Ray Milkey | 6b3775a | 2018-06-28 11:18:44 -0700 | [diff] [blame] | 111 | include_resources, |
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, |
Thomas Vachuska | e443694 | 2018-08-07 19:27:10 -0700 | [diff] [blame] | 115 | "classes", |
Jian Li | 4ad8687 | 2018-08-05 03:34:35 +0900 | [diff] [blame] | 116 | bundle_classpath, |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 117 | ] |
Ray Milkey | 12ae6ca | 2018-06-11 15:34:30 -0700 | [diff] [blame] | 118 | |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 119 | ctx.actions.run( |
| 120 | inputs = inputDependencies, |
| 121 | outputs = [ctx.outputs.osgi_jar], |
| 122 | arguments = arguments, |
| 123 | progress_message = "Running bnd wrapper on: %s" % ctx.attr.name, |
| 124 | executable = ctx.executable._bnd_exe, |
| 125 | ) |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 126 | |
Ray Milkey | 25b785a | 2018-06-12 09:59:14 -0700 | [diff] [blame] | 127 | deps = [] |
| 128 | if java_common.provider in ctx.attr.source: |
| 129 | deps.append(ctx.attr.source[java_common.provider]) |
| 130 | deps_provider = java_common.merge(deps) |
| 131 | return struct( |
Ray Milkey | 15053f0 | 2018-06-13 10:00:45 -0700 | [diff] [blame] | 132 | providers = [deps_provider], |
Ray Milkey | 25b785a | 2018-06-12 09:59:14 -0700 | [diff] [blame] | 133 | ) |
| 134 | |
Ray Milkey | 275af2c | 2018-06-15 13:05:08 -0700 | [diff] [blame] | 135 | """ |
| 136 | Rule definition for calling bnd to make an OSGi jar file. |
| 137 | """ |
Ray Milkey | 32ea35c | 2018-06-06 15:28:07 -0700 | [diff] [blame] | 138 | _bnd = rule( |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 139 | attrs = { |
| 140 | "deps": attr.label_list(), |
| 141 | "version": attr.string(), |
Ray Milkey | 275af2c | 2018-06-15 13:05:08 -0700 | [diff] [blame] | 142 | "group": attr.string(), |
Ray Milkey | 3275ae8 | 2018-05-29 15:35:36 -0700 | [diff] [blame] | 143 | "source": attr.label(), |
Ray Milkey | 12ae6ca | 2018-06-11 15:34:30 -0700 | [diff] [blame] | 144 | "import_packages": attr.string(), |
Jian Li | 4ad8687 | 2018-08-05 03:34:35 +0900 | [diff] [blame] | 145 | "bundle_classpath": attr.string(), |
Ray Milkey | 25747d8 | 2018-06-13 14:12:51 -0700 | [diff] [blame] | 146 | "web_context": attr.string(), |
| 147 | "web_xml": attr.label_list(allow_files = True), |
Ray Milkey | 6b3775a | 2018-06-28 11:18:44 -0700 | [diff] [blame] | 148 | "include_resources": attr.string(), |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 149 | "_bnd_exe": attr.label( |
| 150 | executable = True, |
| 151 | cfg = "host", |
| 152 | allow_files = True, |
| 153 | default = Label("//utils/osgiwrap:osgi-jar"), |
| 154 | ), |
| 155 | }, |
| 156 | fragments = ["java"], |
| 157 | outputs = { |
| 158 | "osgi_jar": "lib%{name}.jar", |
| 159 | }, |
| 160 | implementation = _bnd_impl, |
| 161 | ) |
| 162 | |
Ray Milkey | 275af2c | 2018-06-15 13:05:08 -0700 | [diff] [blame] | 163 | """ |
| 164 | Implementation of the rule to call swagger generator to create the registrator java class source |
| 165 | """ |
Ray Milkey | 6b3775a | 2018-06-28 11:18:44 -0700 | [diff] [blame] | 166 | |
Ray Milkey | 275af2c | 2018-06-15 13:05:08 -0700 | [diff] [blame] | 167 | def _swagger_java_impl(ctx): |
| 168 | api_title = ctx.attr.api_title |
| 169 | api_version = ctx.attr.api_version |
| 170 | api_description = ctx.attr.api_description |
| 171 | api_package = ctx.attr.api_package |
| 172 | web_context = ctx.attr.web_context |
| 173 | |
| 174 | output_java = ctx.outputs.swagger_java.path |
Ray Milkey | 6b3775a | 2018-06-28 11:18:44 -0700 | [diff] [blame] | 175 | output_dir = output_java[:output_java.find("generated-sources")] |
Ray Milkey | 275af2c | 2018-06-15 13:05:08 -0700 | [diff] [blame] | 176 | |
| 177 | package_name = ctx.attr.package_name |
| 178 | |
| 179 | srcs_arg = "" |
| 180 | resources_arg = "" |
| 181 | input_dependencies = [] |
| 182 | |
| 183 | for file in ctx.files.srcs: |
| 184 | srcs_arg += file.path + "," |
| 185 | input_dependencies.append(file) |
| 186 | |
| 187 | for resource in resources_arg: |
| 188 | resources_arg += resource.path + "," |
| 189 | |
| 190 | # call swagger generator to make the swagger JSON and java files |
| 191 | arguments = [ |
| 192 | srcs_arg, |
| 193 | resources_arg, |
| 194 | "", |
| 195 | package_name + "/src/main/resources", |
| 196 | output_dir, |
| 197 | output_dir, |
| 198 | web_context, |
| 199 | api_title, |
| 200 | api_version, |
| 201 | api_package, |
| 202 | api_description, |
| 203 | ] |
| 204 | |
| 205 | ctx.actions.run( |
| 206 | inputs = ctx.files.srcs, |
| 207 | outputs = [ctx.outputs.swagger_java], |
| 208 | arguments = arguments, |
| 209 | progress_message = "Running swagger generator on: %s" % ctx.attr.name, |
| 210 | executable = ctx.executable._swagger_generator_exe, |
| 211 | ) |
| 212 | |
| 213 | """ |
| 214 | Implementation of the rule to call swagger generator for swagger.json file |
| 215 | """ |
Ray Milkey | 6b3775a | 2018-06-28 11:18:44 -0700 | [diff] [blame] | 216 | |
Ray Milkey | 275af2c | 2018-06-15 13:05:08 -0700 | [diff] [blame] | 217 | def _swagger_json_impl(ctx): |
| 218 | api_title = ctx.attr.api_title |
| 219 | api_version = ctx.attr.api_version |
| 220 | api_description = ctx.attr.api_description |
| 221 | api_package = ctx.attr.api_package |
| 222 | web_context = ctx.attr.web_context |
| 223 | |
| 224 | output_json = ctx.outputs.swagger_json.path |
| 225 | output_dir = output_json[:output_json.find("swagger.json")] |
| 226 | |
| 227 | package_name = ctx.attr.package_name |
| 228 | |
| 229 | srcs_arg = "" |
| 230 | resources_arg = "" |
| 231 | input_dependencies = [] |
| 232 | |
| 233 | for file in ctx.files.srcs: |
| 234 | srcs_arg += file.path + "," |
| 235 | input_dependencies.append(file) |
| 236 | |
| 237 | for resource in resources_arg: |
| 238 | resources_arg += resource.path + "," |
| 239 | |
| 240 | # call swagger generator to make the swagger JSON and java files |
| 241 | arguments = [ |
| 242 | srcs_arg, |
| 243 | resources_arg, |
| 244 | "", |
| 245 | package_name + "/src/main/resources", |
| 246 | output_dir, |
| 247 | output_dir, |
| 248 | web_context, |
| 249 | api_title, |
| 250 | api_version, |
| 251 | api_package, |
| 252 | api_description, |
| 253 | ] |
| 254 | |
| 255 | ctx.actions.run( |
| 256 | inputs = ctx.files.srcs, |
| 257 | outputs = [ctx.outputs.swagger_json], |
| 258 | arguments = arguments, |
| 259 | progress_message = "Running swagger generator on: %s" % ctx.attr.name, |
| 260 | executable = ctx.executable._swagger_generator_exe, |
| 261 | ) |
| 262 | |
| 263 | """ |
| 264 | Rule definition to call swagger generator to create the registrator java class source |
| 265 | """ |
| 266 | _swagger_java = rule( |
| 267 | attrs = { |
| 268 | "srcs": attr.label_list(allow_files = True), |
| 269 | "package_name": attr.string(), |
| 270 | "api_title": attr.string(), |
| 271 | "api_version": attr.string(), |
| 272 | "api_description": attr.string(), |
| 273 | "api_package": attr.string(), |
| 274 | "web_context": attr.string(), |
| 275 | "_swagger_generator_exe": attr.label( |
| 276 | executable = True, |
| 277 | cfg = "host", |
| 278 | allow_files = True, |
| 279 | default = Label("//tools/build/buck-plugin:swagger_generator"), |
| 280 | ), |
| 281 | "swagger_java": attr.output(), |
| 282 | }, |
| 283 | fragments = ["java"], |
| 284 | implementation = _swagger_java_impl, |
| 285 | ) |
| 286 | |
| 287 | """ |
| 288 | Rule definition to call swagger generator to create the swagger JSON |
| 289 | """ |
| 290 | _swagger_json = rule( |
| 291 | attrs = { |
| 292 | "srcs": attr.label_list(allow_files = True), |
| 293 | "package_name": attr.string(), |
| 294 | "api_title": attr.string(), |
| 295 | "api_version": attr.string(), |
| 296 | "api_description": attr.string(), |
| 297 | "api_package": attr.string(), |
| 298 | "web_context": attr.string(), |
| 299 | "_swagger_generator_exe": attr.label( |
| 300 | executable = True, |
| 301 | cfg = "host", |
| 302 | allow_files = True, |
| 303 | default = Label("//tools/build/buck-plugin:swagger_generator"), |
| 304 | ), |
| 305 | "swagger_json": attr.output(), |
| 306 | }, |
| 307 | fragments = ["java"], |
| 308 | implementation = _swagger_json_impl, |
| 309 | ) |
| 310 | |
| 311 | """ |
| 312 | Converts a jar file to an OSGI compatible jar file. |
| 313 | |
| 314 | Args: |
| 315 | name: name of the rule to create the OSGI jar file - required |
| 316 | jar: jar file to convert - required target |
| 317 | deps: dependencies needed by the jar file - required list of targets |
| 318 | version: Version of the generated jar file. Optional, defaults to the current ONOS version |
| 319 | group: Maven group ID for the resulting jar file. Optional, defaults to 'org.onosproject' |
| 320 | import_packages: OSGI import list. Optional, comma separated list, defaults to "*" |
| 321 | visibility: Visibility of the produced jar file to other BUILDs. Optional, defaults to private |
| 322 | """ |
Ray Milkey | 6b3775a | 2018-06-28 11:18:44 -0700 | [diff] [blame] | 323 | |
Ray Milkey | 25747d8 | 2018-06-13 14:12:51 -0700 | [diff] [blame] | 324 | def wrapped_osgi_jar( |
| 325 | name, |
| 326 | jar, |
| 327 | deps, |
| 328 | version = ONOS_VERSION, |
Ray Milkey | 275af2c | 2018-06-15 13:05:08 -0700 | [diff] [blame] | 329 | group = "org.onosproject", |
Ray Milkey | 25747d8 | 2018-06-13 14:12:51 -0700 | [diff] [blame] | 330 | import_packages = "*", |
Ray Milkey | 25747d8 | 2018-06-13 14:12:51 -0700 | [diff] [blame] | 331 | visibility = ["//visibility:private"]): |
| 332 | _bnd( |
| 333 | name = name, |
| 334 | source = jar, |
| 335 | deps = deps, |
| 336 | version = version, |
Ray Milkey | 275af2c | 2018-06-15 13:05:08 -0700 | [diff] [blame] | 337 | group = group, |
Ray Milkey | 25747d8 | 2018-06-13 14:12:51 -0700 | [diff] [blame] | 338 | visibility = visibility, |
| 339 | import_packages = import_packages, |
Ray Milkey | 275af2c | 2018-06-15 13:05:08 -0700 | [diff] [blame] | 340 | web_xml = None, |
Ray Milkey | 25747d8 | 2018-06-13 14:12:51 -0700 | [diff] [blame] | 341 | ) |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 342 | |
Ray Milkey | 275af2c | 2018-06-15 13:05:08 -0700 | [diff] [blame] | 343 | """ |
| 344 | Creates an OSGI jar and test jar file from a set of source and test files. |
| 345 | See osgi_jar() for a description of shared parameters. |
| 346 | Args: |
| 347 | test_srcs: Test source file(s) to compile. Optional list of targets, defaults to src/test/java/**/*.java |
| 348 | test_deps: Dependencies for the test jar. Optional list of targets, defaults to a common set of dependencies |
| 349 | test_resources: Resources to include in the test jar. Optional list of targets, defaults to src/test/resources/** |
| 350 | exclude_tests: Tests that should not be run. Useful for excluding things like test files without any @Test methods. |
| 351 | Optional ist of targets, defaults to [] |
| 352 | """ |
Ray Milkey | 6b3775a | 2018-06-28 11:18:44 -0700 | [diff] [blame] | 353 | |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 354 | def osgi_jar_with_tests( |
| 355 | name = None, |
| 356 | deps = None, |
| 357 | test_deps = None, |
Ray Milkey | 275af2c | 2018-06-15 13:05:08 -0700 | [diff] [blame] | 358 | group = "org.onosproject", |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 359 | srcs = None, |
| 360 | resources_root = None, |
| 361 | resources = None, |
Thomas Vachuska | f8c8cb9 | 2018-07-11 17:12:43 -0700 | [diff] [blame] | 362 | resource_jars = [], |
Ray Milkey | 6b3775a | 2018-06-28 11:18:44 -0700 | [diff] [blame] | 363 | include_resources = {}, |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 364 | test_srcs = None, |
| 365 | exclude_tests = None, |
Thomas Vachuska | a79bf6e | 2018-08-07 11:24:40 -0700 | [diff] [blame] | 366 | medium_tests = [], |
| 367 | large_tests = [], |
| 368 | enormous_tests = [], |
| 369 | flaky_tests = [], |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 370 | test_resources = None, |
| 371 | visibility = ["//visibility:public"], |
Ray Milkey | 12ae6ca | 2018-06-11 15:34:30 -0700 | [diff] [blame] | 372 | version = ONOS_VERSION, |
Thomas Vachuska | 5b9ff6a | 2018-07-13 11:00:50 -0700 | [diff] [blame] | 373 | suppress_errorprone = False, |
Ray Milkey | 134d292 | 2018-07-15 15:24:01 -0700 | [diff] [blame] | 374 | suppress_checkstyle = False, |
Thomas Vachuska | 0f7d7a4 | 2018-07-18 15:23:40 -0700 | [diff] [blame] | 375 | suppress_javadocs = False, |
Ray Milkey | 25747d8 | 2018-06-13 14:12:51 -0700 | [diff] [blame] | 376 | web_context = None, |
| 377 | api_title = "", |
| 378 | api_version = "", |
| 379 | api_description = "", |
| 380 | api_package = "", |
Jian Li | 4ad8687 | 2018-08-05 03:34:35 +0900 | [diff] [blame] | 381 | import_packages = None, |
| 382 | bundle_classpath = ""): |
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, |
Jian Li | 4ad8687 | 2018-08-05 03:34:35 +0900 | [diff] [blame] | 459 | bundle_classpath = bundle_classpath, |
Ray Milkey | 25747d8 | 2018-06-13 14:12:51 -0700 | [diff] [blame] | 460 | web_context = web_context, |
| 461 | web_xml = web_xml, |
Ray Milkey | 6b3775a | 2018-06-28 11:18:44 -0700 | [diff] [blame] | 462 | include_resources = _include_resources_to_string(include_resources), |
Ray Milkey | 32ea35c | 2018-06-06 15:28:07 -0700 | [diff] [blame] | 463 | ) |
Thomas Vachuska | 0f7d7a4 | 2018-07-18 15:23:40 -0700 | [diff] [blame] | 464 | |
Thomas Vachuska | ac9e524 | 2018-07-19 16:15:39 -0700 | [diff] [blame] | 465 | # rule for generating pom file for publishing |
| 466 | pom_file(name = name + "-pom", artifact = name, deps = deps, visibility = visibility) |
| 467 | |
| 468 | # rule for building source jar |
Thomas Vachuska | 50ac098 | 2018-07-19 10:17:37 -0700 | [diff] [blame] | 469 | if not suppress_javadocs: |
| 470 | java_sources(name = name + "-sources", srcs = srcs, visibility = visibility) |
| 471 | |
Thomas Vachuska | 0f7d7a4 | 2018-07-18 15:23:40 -0700 | [diff] [blame] | 472 | # rule for building javadocs |
| 473 | if not suppress_javadocs: |
Ray Milkey | 7f46b1f | 2018-07-24 19:01:58 -0700 | [diff] [blame] | 474 | javadoc(name = name + "-javadoc", deps = deps, srcs = srcs, visibility = visibility) |
Thomas Vachuska | 0f7d7a4 | 2018-07-18 15:23:40 -0700 | [diff] [blame] | 475 | |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 476 | if test_srcs != []: |
| 477 | native.java_library( |
| 478 | name = tests_name, |
| 479 | srcs = test_srcs, |
| 480 | resources = test_resources, |
| 481 | deps = tests_jar_deps, |
| 482 | visibility = visibility, |
| 483 | ) |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 484 | |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 485 | generate_test_rules( |
| 486 | name = name + "-tests-gen", |
| 487 | test_files = test_srcs, |
| 488 | exclude_tests = exclude_tests, |
Thomas Vachuska | a79bf6e | 2018-08-07 11:24:40 -0700 | [diff] [blame] | 489 | medium_tests = medium_tests, |
| 490 | large_tests = large_tests, |
| 491 | enormous_tests = enormous_tests, |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 492 | deps = all_test_deps, |
| 493 | ) |
| 494 | |
Ray Milkey | 134d292 | 2018-07-15 15:24:01 -0700 | [diff] [blame] | 495 | if not suppress_checkstyle: |
| 496 | checkstyle_test( |
| 497 | name = name + "_checkstyle_test", |
| 498 | srcs = srcs, |
| 499 | ) |
Ray Milkey | b7949e7 | 2018-06-19 18:31:02 -0700 | [diff] [blame] | 500 | |
Ray Milkey | 275af2c | 2018-06-15 13:05:08 -0700 | [diff] [blame] | 501 | """ |
| 502 | Creates an OSGI jar file from a set of source files. |
| 503 | |
| 504 | Args: |
| 505 | name: Name of the rule to generate. Optional, defaults to a name based on the location in the source tree. |
| 506 | For example apps/mcast/app becomes onos-apps-mcast-app |
| 507 | deps: Dependencies of the generated jar file. Expressed as a list of targets |
| 508 | import_packages: OSGI import list. Optional, comma separated list, defaults to "*" |
Jian Li | 4ad8687 | 2018-08-05 03:34:35 +0900 | [diff] [blame] | 509 | bundle_classpath: intended for including dependencies in our bundle, so that our bundle can be deployed standalone |
Ray Milkey | 275af2c | 2018-06-15 13:05:08 -0700 | [diff] [blame] | 510 | group: Maven group ID for the resulting jar file. Optional, defaults to 'org.onosproject' |
| 511 | srcs: Source file(s) to compile. Optional list of targets, defaults to src/main/java/**/*.java |
| 512 | resources_root: Relative path to the root of the tree of resources for this jar. Optional, defaults to src/main/resources |
| 513 | resources: Resources to include in the jar file. Optional list of targets, defaults to all files beneath resources_root |
| 514 | visibility: Visibility of the produced jar file to other BUILDs. Optional, defaults to public |
| 515 | 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] | 516 | suppress_errorprone: If true, don't run ErrorProne tests. Default is false |
| 517 | suppress_checkstyle: If true, don't run checkstyle tests. Default is false |
Ray Milkey | 275af2c | 2018-06-15 13:05:08 -0700 | [diff] [blame] | 518 | web_context: Web context for a WAB file if needed. Only needed if the jar file provides a REST API. Optional string |
| 519 | api_title: Swagger API title. Optional string, only used if the jar file provides a REST API and has swagger annotations |
| 520 | api_version: Swagger API version. Optional string, only used if the jar file provides a REST API and has swagger annotations |
| 521 | api_description: Swagger API description. Optional string, only used if the jar file provides a REST API and has swagger annotations |
| 522 | api_package: Swagger API package name. Optional string, only used if the jar file provides a REST API and has swagger annotations |
| 523 | """ |
Ray Milkey | 6b3775a | 2018-06-28 11:18:44 -0700 | [diff] [blame] | 524 | |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 525 | def osgi_jar( |
| 526 | name = None, |
| 527 | deps = None, |
Ray Milkey | 12ae6ca | 2018-06-11 15:34:30 -0700 | [diff] [blame] | 528 | import_packages = None, |
Ray Milkey | 275af2c | 2018-06-15 13:05:08 -0700 | [diff] [blame] | 529 | group = "org.onosproject", |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 530 | srcs = None, |
| 531 | resources_root = None, |
| 532 | resources = None, |
Thomas Vachuska | f8c8cb9 | 2018-07-11 17:12:43 -0700 | [diff] [blame] | 533 | resource_jars = [], |
Ray Milkey | 6b3775a | 2018-06-28 11:18:44 -0700 | [diff] [blame] | 534 | include_resources = {}, |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 535 | visibility = ["//visibility:public"], |
Ray Milkey | 12ae6ca | 2018-06-11 15:34:30 -0700 | [diff] [blame] | 536 | version = ONOS_VERSION, |
Thomas Vachuska | 5b9ff6a | 2018-07-13 11:00:50 -0700 | [diff] [blame] | 537 | suppress_errorprone = False, |
Ray Milkey | 134d292 | 2018-07-15 15:24:01 -0700 | [diff] [blame] | 538 | suppress_checkstyle = False, |
Thomas Vachuska | 0f7d7a4 | 2018-07-18 15:23:40 -0700 | [diff] [blame] | 539 | suppress_javadocs = False, |
Ray Milkey | 25747d8 | 2018-06-13 14:12:51 -0700 | [diff] [blame] | 540 | web_context = None, |
Ray Milkey | 12ae6ca | 2018-06-11 15:34:30 -0700 | [diff] [blame] | 541 | api_title = "", |
| 542 | api_version = "", |
| 543 | api_description = "", |
Jian Li | 4ad8687 | 2018-08-05 03:34:35 +0900 | [diff] [blame] | 544 | api_package = "", |
| 545 | bundle_classpath = ""): |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 546 | if srcs == None: |
Ray Milkey | 32ea35c | 2018-06-06 15:28:07 -0700 | [diff] [blame] | 547 | srcs = _all_java_sources() |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 548 | if deps == None: |
| 549 | deps = COMPILE |
| 550 | |
| 551 | osgi_jar_with_tests( |
| 552 | name = name, |
| 553 | deps = deps, |
| 554 | test_deps = [], |
Ray Milkey | 275af2c | 2018-06-15 13:05:08 -0700 | [diff] [blame] | 555 | group = group, |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 556 | srcs = srcs, |
| 557 | resources = resources, |
| 558 | resources_root = resources_root, |
Thomas Vachuska | f8c8cb9 | 2018-07-11 17:12:43 -0700 | [diff] [blame] | 559 | resource_jars = resource_jars, |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 560 | test_srcs = [], |
| 561 | exclude_tests = [], |
| 562 | test_resources = [], |
| 563 | visibility = visibility, |
Thomas Vachuska | 5b9ff6a | 2018-07-13 11:00:50 -0700 | [diff] [blame] | 564 | suppress_errorprone = suppress_errorprone, |
Ray Milkey | 134d292 | 2018-07-15 15:24:01 -0700 | [diff] [blame] | 565 | suppress_checkstyle = suppress_checkstyle, |
Thomas Vachuska | 0f7d7a4 | 2018-07-18 15:23:40 -0700 | [diff] [blame] | 566 | suppress_javadocs = suppress_javadocs, |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 567 | version = version, |
Ray Milkey | 12ae6ca | 2018-06-11 15:34:30 -0700 | [diff] [blame] | 568 | import_packages = import_packages, |
Ray Milkey | 275af2c | 2018-06-15 13:05:08 -0700 | [diff] [blame] | 569 | api_title = api_title, |
| 570 | api_version = api_version, |
| 571 | api_description = api_description, |
| 572 | api_package = api_package, |
Ray Milkey | 25747d8 | 2018-06-13 14:12:51 -0700 | [diff] [blame] | 573 | web_context = web_context, |
Jian Li | 4ad8687 | 2018-08-05 03:34:35 +0900 | [diff] [blame] | 574 | bundle_classpath = bundle_classpath, |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 575 | ) |