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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 = "" |
| 56 | importPackages = "*" |
| 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 | 472d839 | 2018-05-23 17:06:51 -0700 | [diff] [blame] | 67 | if len(dep.java.outputs.jars) == 0: |
| 68 | continue |
| 69 | |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 70 | file = dep.java.outputs.jars[0].class_jar |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 71 | |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 72 | if cp: |
| 73 | cp += ":" |
| 74 | cp += file.path |
| 75 | inputDependencies = inputDependencies + [file] |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 76 | |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 77 | # extract the class files for use by bnd |
Ray Milkey | 3275ae8 | 2018-05-29 15:35:36 -0700 | [diff] [blame] | 78 | classes = ctx.actions.declare_file("classes" + ctx.label.name.replace("/", "-")) |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 79 | classesPath = classes.path |
| 80 | jarCommand = "mkdir -p %s && cp %s %s && cd %s && jar xf *.jar" % (classesPath, jar, classesPath, classesPath) |
| 81 | ctx.actions.run_shell( |
| 82 | inputs = inputDependencies, |
| 83 | outputs = [classes], |
| 84 | command = jarCommand, |
| 85 | progress_message = "Expanding jar file: %s" % jar, |
| 86 | ) |
| 87 | inputDependencies += [classes] |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 88 | |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 89 | # call bnd to make the OSGI jar file |
| 90 | arguments = [ |
| 91 | jar, |
| 92 | output, |
| 93 | cp, |
| 94 | name, |
| 95 | group, |
| 96 | version, |
| 97 | license, |
| 98 | importPackages, |
| 99 | exportPackages, |
| 100 | includeResources, |
| 101 | webContext, |
| 102 | dynamicimportPackages, |
| 103 | classesPath, |
| 104 | ] |
| 105 | ctx.actions.run( |
| 106 | inputs = inputDependencies, |
| 107 | outputs = [ctx.outputs.osgi_jar], |
| 108 | arguments = arguments, |
| 109 | progress_message = "Running bnd wrapper on: %s" % ctx.attr.name, |
| 110 | executable = ctx.executable._bnd_exe, |
| 111 | ) |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 112 | |
| 113 | bnd = rule( |
| 114 | attrs = { |
| 115 | "deps": attr.label_list(), |
| 116 | "version": attr.string(), |
| 117 | "package_name_root": attr.string(), |
Ray Milkey | 3275ae8 | 2018-05-29 15:35:36 -0700 | [diff] [blame] | 118 | "source": attr.label(), |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 119 | "_bnd_exe": attr.label( |
| 120 | executable = True, |
| 121 | cfg = "host", |
| 122 | allow_files = True, |
| 123 | default = Label("//utils/osgiwrap:osgi-jar"), |
| 124 | ), |
| 125 | }, |
| 126 | fragments = ["java"], |
| 127 | outputs = { |
| 128 | "osgi_jar": "lib%{name}.jar", |
| 129 | }, |
| 130 | implementation = _bnd_impl, |
| 131 | ) |
| 132 | |
| 133 | def _fwd_bnd(name, source, deps, version, package_name_root, visibility): |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 134 | bnd(name = name, source = source, deps = deps, version = version, package_name_root = package_name_root, visibility = visibility) |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 135 | |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 136 | def wrapped_osgi_library(name, jar, deps, version = ONOS_VERSION, package_name_root = "org.onosproject", visibility = ["//visibility:private"]): |
| 137 | _fwd_bnd(name, jar, deps, version, package_name_root, visibility) |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 138 | |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 139 | def osgi_jar_with_tests( |
| 140 | name = None, |
| 141 | deps = None, |
| 142 | test_deps = None, |
| 143 | package_name_root = "org.onosproject", |
| 144 | srcs = None, |
| 145 | resources_root = None, |
| 146 | resources = None, |
| 147 | test_srcs = None, |
| 148 | exclude_tests = None, |
| 149 | test_resources = None, |
| 150 | visibility = ["//visibility:public"], |
| 151 | version = ONOS_VERSION): |
| 152 | if name == None: |
| 153 | name = "onos-" + native.package_name().replace("/", "-") |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 154 | if srcs == None: |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 155 | srcs = all_java_sources() |
| 156 | if resources == None: |
| 157 | resources = all_resources(resources_root) |
| 158 | if test_srcs == None: |
| 159 | test_srcs = all_java_test_sources() |
| 160 | if test_resources == None: |
| 161 | test_resources = all_test_resources() |
| 162 | if exclude_tests == None: |
| 163 | exclude_tests = [] |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 164 | if deps == None: |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 165 | deps = COMPILE |
| 166 | if test_deps == None: |
| 167 | test_deps = TEST |
| 168 | tests_name = name + "-tests" |
| 169 | tests_jar_deps = list(depset(deps + test_deps)) + [name] |
| 170 | all_test_deps = tests_jar_deps + [tests_name] |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 171 | |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 172 | native.java_library(name = name, srcs = srcs, resources = resources, deps = deps, visibility = visibility) |
| 173 | _fwd_bnd(name + "-osgi", name, deps, version, package_name_root, visibility) |
| 174 | if test_srcs != []: |
| 175 | native.java_library( |
| 176 | name = tests_name, |
| 177 | srcs = test_srcs, |
| 178 | resources = test_resources, |
| 179 | deps = tests_jar_deps, |
| 180 | visibility = visibility, |
| 181 | ) |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 182 | |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 183 | generate_test_rules( |
| 184 | name = name + "-tests-gen", |
| 185 | test_files = test_srcs, |
| 186 | exclude_tests = exclude_tests, |
| 187 | deps = all_test_deps, |
| 188 | ) |
| 189 | |
| 190 | def osgi_jar( |
| 191 | name = None, |
| 192 | deps = None, |
| 193 | package_name_root = "org.onosproject", |
| 194 | srcs = None, |
| 195 | resources_root = None, |
| 196 | resources = None, |
| 197 | visibility = ["//visibility:public"], |
| 198 | version = ONOS_VERSION): |
| 199 | if srcs == None: |
| 200 | srcs = all_java_sources() |
| 201 | if deps == None: |
| 202 | deps = COMPILE |
| 203 | |
| 204 | osgi_jar_with_tests( |
| 205 | name = name, |
| 206 | deps = deps, |
| 207 | test_deps = [], |
| 208 | package_name_root = package_name_root, |
| 209 | srcs = srcs, |
| 210 | resources = resources, |
| 211 | resources_root = resources_root, |
| 212 | test_srcs = [], |
| 213 | exclude_tests = [], |
| 214 | test_resources = [], |
| 215 | visibility = visibility, |
| 216 | version = version, |
| 217 | ) |