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