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