Thomas Vachuska | aab45d1 | 2018-06-05 16:39:46 -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 | |
| 17 | load("//tools/build/bazel:generate_workspace.bzl", "COMPILE", "TEST", "maven_coordinates") |
Ray Milkey | 32d99ba | 2018-06-06 14:15:00 -0700 | [diff] [blame] | 18 | load("//tools/build/bazel:variables.bzl", "ONOS_GROUP_ID", "ONOS_VERSION") |
Thomas Vachuska | aab45d1 | 2018-06-05 16:39:46 -0700 | [diff] [blame] | 19 | |
| 20 | def dump(obj): |
Ray Milkey | 32d99ba | 2018-06-06 14:15:00 -0700 | [diff] [blame] | 21 | for attr in dir(obj): |
| 22 | print("obj.%s = %r" % (attr, getattr(obj, attr))) |
Thomas Vachuska | aab45d1 | 2018-06-05 16:39:46 -0700 | [diff] [blame] | 23 | |
| 24 | # Implementation of a rule to produce an OSGi feature XML snippet |
| 25 | def _osgi_feature_impl(ctx): |
| 26 | output = ctx.outputs.feature_xml |
| 27 | |
Ray Milkey | 32d99ba | 2018-06-06 14:15:00 -0700 | [diff] [blame] | 28 | args = [ |
| 29 | "-O", |
| 30 | output.path, |
| 31 | "-n", |
| 32 | ctx.attr.name, |
| 33 | "-v", |
| 34 | ctx.attr.version, |
| 35 | "-t", |
| 36 | ctx.attr.description, |
| 37 | ] |
Thomas Vachuska | aab45d1 | 2018-06-05 16:39:46 -0700 | [diff] [blame] | 38 | |
| 39 | inputs = [] |
| 40 | for dep in ctx.attr.included_bundles: |
Ray Milkey | 32d99ba | 2018-06-06 14:15:00 -0700 | [diff] [blame] | 41 | args += ["-b", maven_coordinates(dep.label)] |
Thomas Vachuska | aab45d1 | 2018-06-05 16:39:46 -0700 | [diff] [blame] | 42 | for f in dep.java.outputs.jars: |
Ray Milkey | 32d99ba | 2018-06-06 14:15:00 -0700 | [diff] [blame] | 43 | inputs += [f.class_jar] |
Thomas Vachuska | aab45d1 | 2018-06-05 16:39:46 -0700 | [diff] [blame] | 44 | |
| 45 | for dep in ctx.attr.excluded_bundles: |
Ray Milkey | 32d99ba | 2018-06-06 14:15:00 -0700 | [diff] [blame] | 46 | args += ["-e", maven_coordinates(dep.label)] |
Thomas Vachuska | aab45d1 | 2018-06-05 16:39:46 -0700 | [diff] [blame] | 47 | for f in dep.java.outputs.jars: |
Ray Milkey | 32d99ba | 2018-06-06 14:15:00 -0700 | [diff] [blame] | 48 | inputs += [f.class_jar] |
Thomas Vachuska | aab45d1 | 2018-06-05 16:39:46 -0700 | [diff] [blame] | 49 | |
| 50 | for f in ctx.attr.required_features: |
Ray Milkey | 32d99ba | 2018-06-06 14:15:00 -0700 | [diff] [blame] | 51 | args += ["-f", f] |
Thomas Vachuska | aab45d1 | 2018-06-05 16:39:46 -0700 | [diff] [blame] | 52 | |
Ray Milkey | 32d99ba | 2018-06-06 14:15:00 -0700 | [diff] [blame] | 53 | args += ["-F" if ctx.attr.generate_file else "-E"] |
Thomas Vachuska | aab45d1 | 2018-06-05 16:39:46 -0700 | [diff] [blame] | 54 | |
| 55 | ctx.actions.run( |
| 56 | inputs = inputs, |
Ray Milkey | 32d99ba | 2018-06-06 14:15:00 -0700 | [diff] [blame] | 57 | outputs = [output], |
Thomas Vachuska | aab45d1 | 2018-06-05 16:39:46 -0700 | [diff] [blame] | 58 | arguments = args, |
| 59 | progress_message = "Generating feature %s" % ctx.attr.name, |
Ray Milkey | 32d99ba | 2018-06-06 14:15:00 -0700 | [diff] [blame] | 60 | executable = ctx.executable._writer, |
Thomas Vachuska | aab45d1 | 2018-06-05 16:39:46 -0700 | [diff] [blame] | 61 | ) |
| 62 | |
| 63 | osgi_feature = rule( |
| 64 | attrs = { |
| 65 | "description": attr.string(), |
| 66 | "version": attr.string(default = ONOS_VERSION), |
Ray Milkey | 32d99ba | 2018-06-06 14:15:00 -0700 | [diff] [blame] | 67 | "required_features": attr.string_list(default = ["onos-api"]), |
Thomas Vachuska | aab45d1 | 2018-06-05 16:39:46 -0700 | [diff] [blame] | 68 | "included_bundles": attr.label_list(), |
| 69 | "excluded_bundles": attr.label_list(default = []), |
| 70 | "generate_file": attr.bool(default = False), |
Ray Milkey | 32d99ba | 2018-06-06 14:15:00 -0700 | [diff] [blame] | 71 | "_writer": attr.label( |
| 72 | executable = True, |
| 73 | cfg = "host", |
| 74 | allow_files = True, |
| 75 | default = Label("//tools/build/bazel:onos_app_writer"), |
| 76 | ), |
Thomas Vachuska | aab45d1 | 2018-06-05 16:39:46 -0700 | [diff] [blame] | 77 | }, |
| 78 | outputs = { |
| 79 | "feature_xml": "feature-%{name}.xml", |
| 80 | }, |
| 81 | implementation = _osgi_feature_impl, |
| 82 | ) |
| 83 | |
| 84 | # OSGi feature XML header & footer constants |
| 85 | FEATURES_HEADER = '''\ |
| 86 | <?xml version="1.0" encoding="UTF-8" standalone="yes"?> |
| 87 | <features xmlns="http://karaf.apache.org/xmlns/features/v1.2.0" |
| 88 | name="onos-%s"> |
| 89 | <repository>mvn:org.apache.karaf.features/standard/3.0.8/xml/features</repository> |
| 90 | ''' % ONOS_VERSION |
| 91 | |
Ray Milkey | 32d99ba | 2018-06-06 14:15:00 -0700 | [diff] [blame] | 92 | FEATURES_FOOTER = "</features>" |
Thomas Vachuska | aab45d1 | 2018-06-05 16:39:46 -0700 | [diff] [blame] | 93 | |
| 94 | # Implementation of a rule to produce an OSGi feature repo XML file |
| 95 | def _osgi_feature_repo_impl(ctx): |
| 96 | output = ctx.outputs.feature_repo_xml |
| 97 | |
| 98 | cmd = "(echo '%s';" % FEATURES_HEADER |
| 99 | inputs = [] |
| 100 | for dep in ctx.attr.exported_features: |
| 101 | for f in dep.files.to_list(): |
Ray Milkey | 32d99ba | 2018-06-06 14:15:00 -0700 | [diff] [blame] | 102 | inputs += [f] |
Thomas Vachuska | aab45d1 | 2018-06-05 16:39:46 -0700 | [diff] [blame] | 103 | cmd += "cat %s;" % f.path |
| 104 | cmd += "echo '%s') > %s;" % (FEATURES_FOOTER, output.path) |
| 105 | |
| 106 | ctx.actions.run_shell( |
| 107 | inputs = inputs, |
Ray Milkey | 32d99ba | 2018-06-06 14:15:00 -0700 | [diff] [blame] | 108 | outputs = [output], |
Thomas Vachuska | aab45d1 | 2018-06-05 16:39:46 -0700 | [diff] [blame] | 109 | progress_message = "Generating feature repo %s" % ctx.attr.name, |
| 110 | command = cmd, |
| 111 | ) |
| 112 | |
| 113 | osgi_feature_repo = rule( |
| 114 | attrs = { |
| 115 | "description": attr.string(), |
| 116 | "version": attr.string(default = ONOS_VERSION), |
| 117 | "exported_features": attr.label_list(), |
| 118 | }, |
| 119 | outputs = { |
| 120 | "feature_repo_xml": "feature-repo-%{name}.xml", |
| 121 | }, |
| 122 | implementation = _osgi_feature_repo_impl, |
| 123 | ) |