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") |
| 20 | |
Ray Milkey | 32ea35c | 2018-06-06 15:28:07 -0700 | [diff] [blame] | 21 | def _all_java_sources(): |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 22 | return native.glob(["src/main/java/**/*.java"]) |
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_test_sources(): |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 25 | return native.glob(["src/test/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_test_resources(): |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 28 | return native.glob(["src/test/resources/**"]) |
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_resources(resources_root): |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 31 | if resources_root == None: |
| 32 | return native.glob(["src/main/resources/**"]) |
| 33 | else: |
| 34 | return native.glob([resources_root + "**"]) |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 35 | |
Ray Milkey | 25747d8 | 2018-06-13 14:12:51 -0700 | [diff] [blame] | 36 | def _webapp(): |
| 37 | return native.glob(["src/main/webapp/**"]) |
| 38 | |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 39 | # Implementation of the rule to call bnd to make an OSGI jar file |
| 40 | def _bnd_impl(ctx): |
Ray Milkey | 3275ae8 | 2018-05-29 15:35:36 -0700 | [diff] [blame] | 41 | if (len(ctx.files.source) == 1): |
| 42 | input_file = ctx.files.source[0] |
| 43 | else: |
| 44 | # this is a list of inputs. The one we want is the last one |
| 45 | # in the list that isn't a source jar |
| 46 | for file in reversed(ctx.files.source): |
| 47 | if ("-src" in file.path): |
| 48 | continue |
| 49 | else: |
| 50 | input_file = file |
| 51 | break |
| 52 | |
| 53 | jar = input_file.path |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 54 | output = ctx.outputs.osgi_jar.path |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 55 | name = ctx.attr.source.label.name |
| 56 | group = ctx.attr.package_name_root |
| 57 | version = ctx.attr.version |
| 58 | license = "" |
Ray Milkey | 12ae6ca | 2018-06-11 15:34:30 -0700 | [diff] [blame] | 59 | import_packages = ctx.attr.import_packages |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 60 | exportPackages = "*" |
| 61 | includeResources = "" |
Ray Milkey | 25747d8 | 2018-06-13 14:12:51 -0700 | [diff] [blame] | 62 | web_context = ctx.attr.web_context |
| 63 | if web_context == None or web_context == "": |
| 64 | web_context = "NONE" |
| 65 | web_xml = ctx.attr.web_xml |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 66 | dynamicimportPackages = "" |
Ray Milkey | 3275ae8 | 2018-05-29 15:35:36 -0700 | [diff] [blame] | 67 | cp = "" |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 68 | |
Ray Milkey | 3275ae8 | 2018-05-29 15:35:36 -0700 | [diff] [blame] | 69 | inputDependencies = [input_file] |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 70 | |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 71 | # determine the dependencies and build the class path |
| 72 | for dep in ctx.attr.deps: |
Ray Milkey | 25b785a | 2018-06-12 09:59:14 -0700 | [diff] [blame] | 73 | if java_common.provider in dep: |
| 74 | file = dep.files.to_list()[0] |
Ray Milkey | 472d839 | 2018-05-23 17:06:51 -0700 | [diff] [blame] | 75 | |
Ray Milkey | 25b785a | 2018-06-12 09:59:14 -0700 | [diff] [blame] | 76 | if cp: |
| 77 | cp += ":" |
| 78 | cp += file.path |
| 79 | inputDependencies = inputDependencies + [file] |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 80 | |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 81 | # extract the class files for use by bnd |
Ray Milkey | 3275ae8 | 2018-05-29 15:35:36 -0700 | [diff] [blame] | 82 | classes = ctx.actions.declare_file("classes" + ctx.label.name.replace("/", "-")) |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 83 | classesPath = classes.path |
| 84 | jarCommand = "mkdir -p %s && cp %s %s && cd %s && jar xf *.jar" % (classesPath, jar, classesPath, classesPath) |
| 85 | ctx.actions.run_shell( |
| 86 | inputs = inputDependencies, |
| 87 | outputs = [classes], |
| 88 | command = jarCommand, |
| 89 | progress_message = "Expanding jar file: %s" % jar, |
| 90 | ) |
| 91 | inputDependencies += [classes] |
Ray Milkey | 25747d8 | 2018-06-13 14:12:51 -0700 | [diff] [blame] | 92 | web_xml_root_path = "" |
| 93 | if len(web_xml) != 0: |
| 94 | web_xml_root = web_xml[0].files.to_list()[0] |
| 95 | inputDependencies += [web_xml_root] |
| 96 | 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] | 97 | |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 98 | # call bnd to make the OSGI jar file |
| 99 | arguments = [ |
| 100 | jar, |
| 101 | output, |
| 102 | cp, |
| 103 | name, |
| 104 | group, |
| 105 | version, |
| 106 | license, |
Ray Milkey | 12ae6ca | 2018-06-11 15:34:30 -0700 | [diff] [blame] | 107 | import_packages, |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 108 | exportPackages, |
| 109 | includeResources, |
Ray Milkey | 25747d8 | 2018-06-13 14:12:51 -0700 | [diff] [blame] | 110 | web_context, |
| 111 | web_xml_root_path, |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 112 | dynamicimportPackages, |
| 113 | classesPath, |
| 114 | ] |
Ray Milkey | 12ae6ca | 2018-06-11 15:34:30 -0700 | [diff] [blame] | 115 | |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 116 | ctx.actions.run( |
| 117 | inputs = inputDependencies, |
| 118 | outputs = [ctx.outputs.osgi_jar], |
| 119 | arguments = arguments, |
| 120 | progress_message = "Running bnd wrapper on: %s" % ctx.attr.name, |
| 121 | executable = ctx.executable._bnd_exe, |
| 122 | ) |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 123 | |
Ray Milkey | 25b785a | 2018-06-12 09:59:14 -0700 | [diff] [blame] | 124 | deps = [] |
| 125 | if java_common.provider in ctx.attr.source: |
| 126 | deps.append(ctx.attr.source[java_common.provider]) |
| 127 | deps_provider = java_common.merge(deps) |
| 128 | return struct( |
Ray Milkey | 15053f0 | 2018-06-13 10:00:45 -0700 | [diff] [blame] | 129 | providers = [deps_provider], |
Ray Milkey | 25b785a | 2018-06-12 09:59:14 -0700 | [diff] [blame] | 130 | ) |
| 131 | |
Ray Milkey | 32ea35c | 2018-06-06 15:28:07 -0700 | [diff] [blame] | 132 | _bnd = rule( |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 133 | attrs = { |
| 134 | "deps": attr.label_list(), |
| 135 | "version": attr.string(), |
| 136 | "package_name_root": attr.string(), |
Ray Milkey | 3275ae8 | 2018-05-29 15:35:36 -0700 | [diff] [blame] | 137 | "source": attr.label(), |
Ray Milkey | 12ae6ca | 2018-06-11 15:34:30 -0700 | [diff] [blame] | 138 | "import_packages": attr.string(), |
Ray Milkey | 25747d8 | 2018-06-13 14:12:51 -0700 | [diff] [blame] | 139 | "web_context": attr.string(), |
| 140 | "web_xml": attr.label_list(allow_files = True), |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 141 | "_bnd_exe": attr.label( |
| 142 | executable = True, |
| 143 | cfg = "host", |
| 144 | allow_files = True, |
| 145 | default = Label("//utils/osgiwrap:osgi-jar"), |
| 146 | ), |
| 147 | }, |
| 148 | fragments = ["java"], |
| 149 | outputs = { |
| 150 | "osgi_jar": "lib%{name}.jar", |
| 151 | }, |
| 152 | implementation = _bnd_impl, |
| 153 | ) |
| 154 | |
Ray Milkey | 25747d8 | 2018-06-13 14:12:51 -0700 | [diff] [blame] | 155 | def wrapped_osgi_jar( |
| 156 | name, |
| 157 | jar, |
| 158 | deps, |
| 159 | version = ONOS_VERSION, |
| 160 | package_name_root = "org.onosproject", |
| 161 | import_packages = "*", |
| 162 | web_context = None, |
| 163 | web_xml = None, |
| 164 | visibility = ["//visibility:private"]): |
| 165 | _bnd( |
| 166 | name = name, |
| 167 | source = jar, |
| 168 | deps = deps, |
| 169 | version = version, |
| 170 | package_name_root = package_name_root, |
| 171 | visibility = visibility, |
| 172 | import_packages = import_packages, |
| 173 | web_xml = web_xml, |
| 174 | ) |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 175 | |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 176 | def osgi_jar_with_tests( |
| 177 | name = None, |
| 178 | deps = None, |
| 179 | test_deps = None, |
| 180 | package_name_root = "org.onosproject", |
| 181 | srcs = None, |
| 182 | resources_root = None, |
| 183 | resources = None, |
| 184 | test_srcs = None, |
| 185 | exclude_tests = None, |
| 186 | test_resources = None, |
| 187 | visibility = ["//visibility:public"], |
Ray Milkey | 12ae6ca | 2018-06-11 15:34:30 -0700 | [diff] [blame] | 188 | version = ONOS_VERSION, |
Ray Milkey | 25747d8 | 2018-06-13 14:12:51 -0700 | [diff] [blame] | 189 | web_context = None, |
| 190 | api_title = "", |
| 191 | api_version = "", |
| 192 | api_description = "", |
| 193 | api_package = "", |
Ray Milkey | 15053f0 | 2018-06-13 10:00:45 -0700 | [diff] [blame] | 194 | import_packages = None): |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 195 | if name == None: |
| 196 | name = "onos-" + native.package_name().replace("/", "-") |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 197 | if srcs == None: |
Ray Milkey | 32ea35c | 2018-06-06 15:28:07 -0700 | [diff] [blame] | 198 | srcs = _all_java_sources() |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 199 | if resources == None: |
Ray Milkey | 32ea35c | 2018-06-06 15:28:07 -0700 | [diff] [blame] | 200 | resources = _all_resources(resources_root) |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 201 | if test_srcs == None: |
Ray Milkey | 32ea35c | 2018-06-06 15:28:07 -0700 | [diff] [blame] | 202 | test_srcs = _all_java_test_sources() |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 203 | if test_resources == None: |
Ray Milkey | 32ea35c | 2018-06-06 15:28:07 -0700 | [diff] [blame] | 204 | test_resources = _all_test_resources() |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 205 | if exclude_tests == None: |
| 206 | exclude_tests = [] |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 207 | if deps == None: |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 208 | deps = COMPILE |
| 209 | if test_deps == None: |
| 210 | test_deps = TEST |
Ray Milkey | 12ae6ca | 2018-06-11 15:34:30 -0700 | [diff] [blame] | 211 | if import_packages == None: |
| 212 | import_packages = "*" |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 213 | tests_name = name + "-tests" |
| 214 | tests_jar_deps = list(depset(deps + test_deps)) + [name] |
| 215 | all_test_deps = tests_jar_deps + [tests_name] |
Ray Milkey | 25747d8 | 2018-06-13 14:12:51 -0700 | [diff] [blame] | 216 | web_xml = _webapp() |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 217 | |
Ray Milkey | 25747d8 | 2018-06-13 14:12:51 -0700 | [diff] [blame] | 218 | # compile the Java code |
Ray Milkey | 25b785a | 2018-06-12 09:59:14 -0700 | [diff] [blame] | 219 | native.java_library(name = name + "-native", srcs = srcs, resources = resources, deps = deps, visibility = visibility) |
Ray Milkey | 25747d8 | 2018-06-13 14:12:51 -0700 | [diff] [blame] | 220 | |
Ray Milkey | 32ea35c | 2018-06-06 15:28:07 -0700 | [diff] [blame] | 221 | _bnd( |
Ray Milkey | 25b785a | 2018-06-12 09:59:14 -0700 | [diff] [blame] | 222 | name = name, |
| 223 | source = name + "-native", |
Ray Milkey | 32ea35c | 2018-06-06 15:28:07 -0700 | [diff] [blame] | 224 | deps = deps, |
| 225 | version = version, |
| 226 | package_name_root = package_name_root, |
| 227 | visibility = visibility, |
Ray Milkey | 12ae6ca | 2018-06-11 15:34:30 -0700 | [diff] [blame] | 228 | import_packages = import_packages, |
Ray Milkey | 25747d8 | 2018-06-13 14:12:51 -0700 | [diff] [blame] | 229 | web_context = web_context, |
| 230 | web_xml = web_xml, |
Ray Milkey | 32ea35c | 2018-06-06 15:28:07 -0700 | [diff] [blame] | 231 | ) |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 232 | if test_srcs != []: |
| 233 | native.java_library( |
| 234 | name = tests_name, |
| 235 | srcs = test_srcs, |
| 236 | resources = test_resources, |
| 237 | deps = tests_jar_deps, |
| 238 | visibility = visibility, |
| 239 | ) |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 240 | |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 241 | generate_test_rules( |
| 242 | name = name + "-tests-gen", |
| 243 | test_files = test_srcs, |
| 244 | exclude_tests = exclude_tests, |
| 245 | deps = all_test_deps, |
| 246 | ) |
| 247 | |
| 248 | def osgi_jar( |
| 249 | name = None, |
| 250 | deps = None, |
Ray Milkey | 12ae6ca | 2018-06-11 15:34:30 -0700 | [diff] [blame] | 251 | import_packages = None, |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 252 | package_name_root = "org.onosproject", |
| 253 | srcs = None, |
| 254 | resources_root = None, |
| 255 | resources = None, |
| 256 | visibility = ["//visibility:public"], |
Ray Milkey | 12ae6ca | 2018-06-11 15:34:30 -0700 | [diff] [blame] | 257 | version = ONOS_VERSION, |
| 258 | # TODO - implement these for swagger and web.xml |
Ray Milkey | 25747d8 | 2018-06-13 14:12:51 -0700 | [diff] [blame] | 259 | web_context = None, |
Ray Milkey | 12ae6ca | 2018-06-11 15:34:30 -0700 | [diff] [blame] | 260 | api_title = "", |
| 261 | api_version = "", |
| 262 | api_description = "", |
Ray Milkey | 15053f0 | 2018-06-13 10:00:45 -0700 | [diff] [blame] | 263 | api_package = ""): |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 264 | if srcs == None: |
Ray Milkey | 32ea35c | 2018-06-06 15:28:07 -0700 | [diff] [blame] | 265 | srcs = _all_java_sources() |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 266 | if deps == None: |
| 267 | deps = COMPILE |
| 268 | |
| 269 | osgi_jar_with_tests( |
| 270 | name = name, |
| 271 | deps = deps, |
| 272 | test_deps = [], |
| 273 | package_name_root = package_name_root, |
| 274 | srcs = srcs, |
| 275 | resources = resources, |
| 276 | resources_root = resources_root, |
| 277 | test_srcs = [], |
| 278 | exclude_tests = [], |
| 279 | test_resources = [], |
| 280 | visibility = visibility, |
| 281 | version = version, |
Ray Milkey | 12ae6ca | 2018-06-11 15:34:30 -0700 | [diff] [blame] | 282 | import_packages = import_packages, |
Ray Milkey | 25747d8 | 2018-06-13 14:12:51 -0700 | [diff] [blame] | 283 | web_context = web_context, |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 284 | ) |