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