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