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