Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 1 | load("//tools/build/bazel:generate_workspace.bzl", "COMPILE", "TEST") |
| 2 | load("//tools/build/bazel:variables.bzl", "ONOS_VERSION") |
| 3 | load("//tools/build/bazel:generate_test_rules.bzl", "generate_test_rules") |
| 4 | |
| 5 | def all_java_sources(): |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 6 | return native.glob(["src/main/java/**/*.java"]) |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 7 | |
| 8 | def all_java_test_sources(): |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 9 | return native.glob(["src/test/java/**/*.java"]) |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 10 | |
| 11 | def all_test_resources(): |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 12 | return native.glob(["src/test/resources/**"]) |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 13 | |
| 14 | def all_resources(resources_root): |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 15 | if resources_root == None: |
| 16 | return native.glob(["src/main/resources/**"]) |
| 17 | else: |
| 18 | return native.glob([resources_root + "**"]) |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 19 | |
| 20 | # Implementation of the rule to call bnd to make an OSGI jar file |
| 21 | def _bnd_impl(ctx): |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 22 | jar = ctx.file.source.path |
| 23 | output = ctx.outputs.osgi_jar.path |
| 24 | cp = "" |
| 25 | name = ctx.attr.source.label.name |
| 26 | group = ctx.attr.package_name_root |
| 27 | version = ctx.attr.version |
| 28 | license = "" |
| 29 | importPackages = "*" |
| 30 | exportPackages = "*" |
| 31 | includeResources = "" |
| 32 | webContext = "NONE" |
| 33 | dynamicimportPackages = "" |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 34 | |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 35 | inputDependencies = [ctx.file.source] |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 36 | |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 37 | # determine the dependencies and build the class path |
| 38 | for dep in ctx.attr.deps: |
Ray Milkey | 472d839 | 2018-05-23 17:06:51 -0700 | [diff] [blame] | 39 | if len(dep.java.outputs.jars) == 0: |
| 40 | continue |
| 41 | |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 42 | file = dep.java.outputs.jars[0].class_jar |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 43 | |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 44 | if cp: |
| 45 | cp += ":" |
| 46 | cp += file.path |
| 47 | inputDependencies = inputDependencies + [file] |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 48 | |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 49 | # extract the class files for use by bnd |
| 50 | classes = ctx.actions.declare_file("classes") |
| 51 | classesPath = classes.path |
| 52 | jarCommand = "mkdir -p %s && cp %s %s && cd %s && jar xf *.jar" % (classesPath, jar, classesPath, classesPath) |
| 53 | ctx.actions.run_shell( |
| 54 | inputs = inputDependencies, |
| 55 | outputs = [classes], |
| 56 | command = jarCommand, |
| 57 | progress_message = "Expanding jar file: %s" % jar, |
| 58 | ) |
| 59 | inputDependencies += [classes] |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 60 | |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 61 | # call bnd to make the OSGI jar file |
| 62 | arguments = [ |
| 63 | jar, |
| 64 | output, |
| 65 | cp, |
| 66 | name, |
| 67 | group, |
| 68 | version, |
| 69 | license, |
| 70 | importPackages, |
| 71 | exportPackages, |
| 72 | includeResources, |
| 73 | webContext, |
| 74 | dynamicimportPackages, |
| 75 | classesPath, |
| 76 | ] |
| 77 | ctx.actions.run( |
| 78 | inputs = inputDependencies, |
| 79 | outputs = [ctx.outputs.osgi_jar], |
| 80 | arguments = arguments, |
| 81 | progress_message = "Running bnd wrapper on: %s" % ctx.attr.name, |
| 82 | executable = ctx.executable._bnd_exe, |
| 83 | ) |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 84 | |
| 85 | bnd = rule( |
| 86 | attrs = { |
| 87 | "deps": attr.label_list(), |
| 88 | "version": attr.string(), |
| 89 | "package_name_root": attr.string(), |
| 90 | "source": attr.label(allow_single_file = True), |
| 91 | "_bnd_exe": attr.label( |
| 92 | executable = True, |
| 93 | cfg = "host", |
| 94 | allow_files = True, |
| 95 | default = Label("//utils/osgiwrap:osgi-jar"), |
| 96 | ), |
| 97 | }, |
| 98 | fragments = ["java"], |
| 99 | outputs = { |
| 100 | "osgi_jar": "lib%{name}.jar", |
| 101 | }, |
| 102 | implementation = _bnd_impl, |
| 103 | ) |
| 104 | |
| 105 | def _fwd_bnd(name, source, deps, version, package_name_root, visibility): |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 106 | 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] | 107 | |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 108 | def wrapped_osgi_library(name, jar, deps, version = ONOS_VERSION, package_name_root = "org.onosproject", visibility = ["//visibility:private"]): |
| 109 | _fwd_bnd(name, jar, deps, version, package_name_root, visibility) |
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 | def osgi_jar_with_tests( |
| 112 | name = None, |
| 113 | deps = None, |
| 114 | test_deps = None, |
| 115 | package_name_root = "org.onosproject", |
| 116 | srcs = None, |
| 117 | resources_root = None, |
| 118 | resources = None, |
| 119 | test_srcs = None, |
| 120 | exclude_tests = None, |
| 121 | test_resources = None, |
| 122 | visibility = ["//visibility:public"], |
| 123 | version = ONOS_VERSION): |
| 124 | if name == None: |
| 125 | name = "onos-" + native.package_name().replace("/", "-") |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 126 | if srcs == None: |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 127 | srcs = all_java_sources() |
| 128 | if resources == None: |
| 129 | resources = all_resources(resources_root) |
| 130 | if test_srcs == None: |
| 131 | test_srcs = all_java_test_sources() |
| 132 | if test_resources == None: |
| 133 | test_resources = all_test_resources() |
| 134 | if exclude_tests == None: |
| 135 | exclude_tests = [] |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 136 | if deps == None: |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 137 | deps = COMPILE |
| 138 | if test_deps == None: |
| 139 | test_deps = TEST |
| 140 | tests_name = name + "-tests" |
| 141 | tests_jar_deps = list(depset(deps + test_deps)) + [name] |
| 142 | all_test_deps = tests_jar_deps + [tests_name] |
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 | native.java_library(name = name, srcs = srcs, resources = resources, deps = deps, visibility = visibility) |
| 145 | _fwd_bnd(name + "-osgi", name, deps, version, package_name_root, visibility) |
| 146 | if test_srcs != []: |
| 147 | native.java_library( |
| 148 | name = tests_name, |
| 149 | srcs = test_srcs, |
| 150 | resources = test_resources, |
| 151 | deps = tests_jar_deps, |
| 152 | visibility = visibility, |
| 153 | ) |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 154 | |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 155 | generate_test_rules( |
| 156 | name = name + "-tests-gen", |
| 157 | test_files = test_srcs, |
| 158 | exclude_tests = exclude_tests, |
| 159 | deps = all_test_deps, |
| 160 | ) |
| 161 | |
| 162 | def osgi_jar( |
| 163 | name = None, |
| 164 | deps = None, |
| 165 | package_name_root = "org.onosproject", |
| 166 | srcs = None, |
| 167 | resources_root = None, |
| 168 | resources = None, |
| 169 | visibility = ["//visibility:public"], |
| 170 | version = ONOS_VERSION): |
| 171 | if srcs == None: |
| 172 | srcs = all_java_sources() |
| 173 | if deps == None: |
| 174 | deps = COMPILE |
| 175 | |
| 176 | osgi_jar_with_tests( |
| 177 | name = name, |
| 178 | deps = deps, |
| 179 | test_deps = [], |
| 180 | package_name_root = package_name_root, |
| 181 | srcs = srcs, |
| 182 | resources = resources, |
| 183 | resources_root = resources_root, |
| 184 | test_srcs = [], |
| 185 | exclude_tests = [], |
| 186 | test_resources = [], |
| 187 | visibility = visibility, |
| 188 | version = version, |
| 189 | ) |