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