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): |
Thomas Vachuska | e8f0689 | 2018-06-12 15:54:49 -0700 | [diff] [blame] | 26 | xmlArgs = [ |
Ray Milkey | 15053f0 | 2018-06-13 10:00:45 -0700 | [diff] [blame] | 27 | "-O", |
| 28 | ctx.outputs.feature_xml.path, |
| 29 | "-n", |
| 30 | ctx.attr.name, |
| 31 | "-v", |
| 32 | ctx.attr.version, |
| 33 | "-t", |
| 34 | ctx.attr.description, |
Ray Milkey | 32d99ba | 2018-06-06 14:15:00 -0700 | [diff] [blame] | 35 | ] |
Thomas Vachuska | 8e022a9 | 2018-07-10 14:47:38 -0700 | [diff] [blame] | 36 | bundleArgs = [ctx.outputs.feature_zip.path] |
Thomas Vachuska | aab45d1 | 2018-06-05 16:39:46 -0700 | [diff] [blame] | 37 | inputs = [] |
Thomas Vachuska | e8f0689 | 2018-06-12 15:54:49 -0700 | [diff] [blame] | 38 | |
Thomas Vachuska | aab45d1 | 2018-06-05 16:39:46 -0700 | [diff] [blame] | 39 | for dep in ctx.attr.included_bundles: |
Thomas Vachuska | e8f0689 | 2018-06-12 15:54:49 -0700 | [diff] [blame] | 40 | coord = maven_coordinates(dep.label) |
| 41 | xmlArgs += ["-b", coord] |
Ray Milkey | 6b3775a | 2018-06-28 11:18:44 -0700 | [diff] [blame] | 42 | |
| 43 | inputs += [dep.files.to_list()[0]] |
| 44 | bundleArgs += [dep.files.to_list()[0].path, coord] |
Thomas Vachuska | aab45d1 | 2018-06-05 16:39:46 -0700 | [diff] [blame] | 45 | |
Thomas Vachuska | e8f0689 | 2018-06-12 15:54:49 -0700 | [diff] [blame] | 46 | for f in ctx.attr.excluded_bundles: |
| 47 | xmlArgs += ["-e", maven_coordinates(dep.label)] |
| 48 | if java_common.provider in f: |
| 49 | inputs += [f.files.to_list()[0]] |
Thomas Vachuska | aab45d1 | 2018-06-05 16:39:46 -0700 | [diff] [blame] | 50 | |
| 51 | for f in ctx.attr.required_features: |
Thomas Vachuska | e8f0689 | 2018-06-12 15:54:49 -0700 | [diff] [blame] | 52 | xmlArgs += ["-f", f] |
Thomas Vachuska | aab45d1 | 2018-06-05 16:39:46 -0700 | [diff] [blame] | 53 | |
Thomas Vachuska | e8f0689 | 2018-06-12 15:54:49 -0700 | [diff] [blame] | 54 | xmlArgs += ["-F" if ctx.attr.generate_file else "-E"] |
Thomas Vachuska | aab45d1 | 2018-06-05 16:39:46 -0700 | [diff] [blame] | 55 | |
| 56 | ctx.actions.run( |
| 57 | inputs = inputs, |
Thomas Vachuska | e8f0689 | 2018-06-12 15:54:49 -0700 | [diff] [blame] | 58 | outputs = [ctx.outputs.feature_xml], |
| 59 | arguments = xmlArgs, |
| 60 | progress_message = "Generating feature %s XML" % ctx.attr.name, |
Thomas Vachuska | 8e022a9 | 2018-07-10 14:47:38 -0700 | [diff] [blame] | 61 | executable = ctx.executable._feature_writer, |
Thomas Vachuska | aab45d1 | 2018-06-05 16:39:46 -0700 | [diff] [blame] | 62 | ) |
| 63 | |
Thomas Vachuska | e8f0689 | 2018-06-12 15:54:49 -0700 | [diff] [blame] | 64 | ctx.actions.run( |
| 65 | inputs = inputs, |
Thomas Vachuska | 8e022a9 | 2018-07-10 14:47:38 -0700 | [diff] [blame] | 66 | outputs = [ctx.outputs.feature_zip], |
Thomas Vachuska | e8f0689 | 2018-06-12 15:54:49 -0700 | [diff] [blame] | 67 | arguments = bundleArgs, |
| 68 | progress_message = "Generating feature %s bundle" % ctx.attr.name, |
Thomas Vachuska | 8e022a9 | 2018-07-10 14:47:38 -0700 | [diff] [blame] | 69 | executable = ctx.executable._feature_bundler, |
Thomas Vachuska | e8f0689 | 2018-06-12 15:54:49 -0700 | [diff] [blame] | 70 | ) |
| 71 | |
Thomas Vachuska | aab45d1 | 2018-06-05 16:39:46 -0700 | [diff] [blame] | 72 | osgi_feature = rule( |
| 73 | attrs = { |
| 74 | "description": attr.string(), |
| 75 | "version": attr.string(default = ONOS_VERSION), |
Ray Milkey | 32d99ba | 2018-06-06 14:15:00 -0700 | [diff] [blame] | 76 | "required_features": attr.string_list(default = ["onos-api"]), |
Thomas Vachuska | aab45d1 | 2018-06-05 16:39:46 -0700 | [diff] [blame] | 77 | "included_bundles": attr.label_list(), |
| 78 | "excluded_bundles": attr.label_list(default = []), |
| 79 | "generate_file": attr.bool(default = False), |
Thomas Vachuska | 8e022a9 | 2018-07-10 14:47:38 -0700 | [diff] [blame] | 80 | "_feature_writer": attr.label( |
Ray Milkey | 32d99ba | 2018-06-06 14:15:00 -0700 | [diff] [blame] | 81 | executable = True, |
| 82 | cfg = "host", |
| 83 | allow_files = True, |
Thomas Vachuska | 8e022a9 | 2018-07-10 14:47:38 -0700 | [diff] [blame] | 84 | default = Label("//tools/build/bazel:onos_app_tools"), |
Ray Milkey | 32d99ba | 2018-06-06 14:15:00 -0700 | [diff] [blame] | 85 | ), |
Thomas Vachuska | 8e022a9 | 2018-07-10 14:47:38 -0700 | [diff] [blame] | 86 | "_feature_bundler": attr.label( |
Thomas Vachuska | e8f0689 | 2018-06-12 15:54:49 -0700 | [diff] [blame] | 87 | executable = True, |
| 88 | cfg = "host", |
| 89 | allow_files = True, |
Thomas Vachuska | 8e022a9 | 2018-07-10 14:47:38 -0700 | [diff] [blame] | 90 | default = Label("//tools/build/bazel:osgi_feature_bundler"), |
Thomas Vachuska | e8f0689 | 2018-06-12 15:54:49 -0700 | [diff] [blame] | 91 | ), |
Thomas Vachuska | aab45d1 | 2018-06-05 16:39:46 -0700 | [diff] [blame] | 92 | }, |
| 93 | outputs = { |
| 94 | "feature_xml": "feature-%{name}.xml", |
Thomas Vachuska | 8e022a9 | 2018-07-10 14:47:38 -0700 | [diff] [blame] | 95 | "feature_zip": "feature-%{name}.zip", |
Thomas Vachuska | aab45d1 | 2018-06-05 16:39:46 -0700 | [diff] [blame] | 96 | }, |
| 97 | implementation = _osgi_feature_impl, |
| 98 | ) |
| 99 | |
| 100 | # OSGi feature XML header & footer constants |
| 101 | FEATURES_HEADER = '''\ |
| 102 | <?xml version="1.0" encoding="UTF-8" standalone="yes"?> |
| 103 | <features xmlns="http://karaf.apache.org/xmlns/features/v1.2.0" |
| 104 | name="onos-%s"> |
Thomas Vachuska | 3770b08 | 2019-01-02 16:38:37 -0800 | [diff] [blame] | 105 | <repository>mvn:org.apache.karaf.features/standard/4.2.2/xml/features</repository> |
Thomas Vachuska | aab45d1 | 2018-06-05 16:39:46 -0700 | [diff] [blame] | 106 | ''' % ONOS_VERSION |
| 107 | |
Ray Milkey | 32d99ba | 2018-06-06 14:15:00 -0700 | [diff] [blame] | 108 | FEATURES_FOOTER = "</features>" |
Thomas Vachuska | aab45d1 | 2018-06-05 16:39:46 -0700 | [diff] [blame] | 109 | |
| 110 | # Implementation of a rule to produce an OSGi feature repo XML file |
| 111 | def _osgi_feature_repo_impl(ctx): |
| 112 | output = ctx.outputs.feature_repo_xml |
| 113 | |
| 114 | cmd = "(echo '%s';" % FEATURES_HEADER |
| 115 | inputs = [] |
| 116 | for dep in ctx.attr.exported_features: |
| 117 | for f in dep.files.to_list(): |
Ray Milkey | 32d99ba | 2018-06-06 14:15:00 -0700 | [diff] [blame] | 118 | inputs += [f] |
Thomas Vachuska | 510419f | 2018-06-28 17:05:09 -0700 | [diff] [blame] | 119 | cmd += "cat %s;" % f.path if f.path.endswith(".xml") else "" |
Thomas Vachuska | aab45d1 | 2018-06-05 16:39:46 -0700 | [diff] [blame] | 120 | cmd += "echo '%s') > %s;" % (FEATURES_FOOTER, output.path) |
| 121 | |
| 122 | ctx.actions.run_shell( |
| 123 | inputs = inputs, |
Ray Milkey | 32d99ba | 2018-06-06 14:15:00 -0700 | [diff] [blame] | 124 | outputs = [output], |
Thomas Vachuska | aab45d1 | 2018-06-05 16:39:46 -0700 | [diff] [blame] | 125 | progress_message = "Generating feature repo %s" % ctx.attr.name, |
| 126 | command = cmd, |
| 127 | ) |
| 128 | |
| 129 | osgi_feature_repo = rule( |
| 130 | attrs = { |
| 131 | "description": attr.string(), |
| 132 | "version": attr.string(default = ONOS_VERSION), |
| 133 | "exported_features": attr.label_list(), |
| 134 | }, |
| 135 | outputs = { |
| 136 | "feature_repo_xml": "feature-repo-%{name}.xml", |
| 137 | }, |
| 138 | implementation = _osgi_feature_repo_impl, |
| 139 | ) |