blob: e83df6822da19451ed2041d2a1e1872d1b5e4e20 [file] [log] [blame]
Thomas Vachuskaaab45d12018-06-05 16:39:46 -07001"""
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
17load("//tools/build/bazel:generate_workspace.bzl", "COMPILE", "TEST", "maven_coordinates")
Ray Milkey32d99ba2018-06-06 14:15:00 -070018load("//tools/build/bazel:variables.bzl", "ONOS_GROUP_ID", "ONOS_VERSION")
Thomas Vachuskaaab45d12018-06-05 16:39:46 -070019
20def dump(obj):
Ray Milkey32d99ba2018-06-06 14:15:00 -070021 for attr in dir(obj):
22 print("obj.%s = %r" % (attr, getattr(obj, attr)))
Thomas Vachuskaaab45d12018-06-05 16:39:46 -070023
24# Implementation of a rule to produce an OSGi feature XML snippet
25def _osgi_feature_impl(ctx):
Thomas Vachuskae8f06892018-06-12 15:54:49 -070026 xmlArgs = [
Ray Milkey15053f02018-06-13 10:00:45 -070027 "-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 Milkey32d99ba2018-06-06 14:15:00 -070035 ]
Thomas Vachuska8e022a92018-07-10 14:47:38 -070036 bundleArgs = [ctx.outputs.feature_zip.path]
Thomas Vachuskaaab45d12018-06-05 16:39:46 -070037 inputs = []
Thomas Vachuskae8f06892018-06-12 15:54:49 -070038
Thomas Vachuskaaab45d12018-06-05 16:39:46 -070039 for dep in ctx.attr.included_bundles:
Thomas Vachuskae8f06892018-06-12 15:54:49 -070040 coord = maven_coordinates(dep.label)
41 xmlArgs += ["-b", coord]
Ray Milkey6b3775a2018-06-28 11:18:44 -070042
43 inputs += [dep.files.to_list()[0]]
44 bundleArgs += [dep.files.to_list()[0].path, coord]
Thomas Vachuskaaab45d12018-06-05 16:39:46 -070045
Thomas Vachuskae8f06892018-06-12 15:54:49 -070046 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 Vachuskaaab45d12018-06-05 16:39:46 -070050
51 for f in ctx.attr.required_features:
Thomas Vachuskae8f06892018-06-12 15:54:49 -070052 xmlArgs += ["-f", f]
Thomas Vachuskaaab45d12018-06-05 16:39:46 -070053
Thomas Vachuskae8f06892018-06-12 15:54:49 -070054 xmlArgs += ["-F" if ctx.attr.generate_file else "-E"]
Thomas Vachuskaaab45d12018-06-05 16:39:46 -070055
56 ctx.actions.run(
57 inputs = inputs,
Thomas Vachuskae8f06892018-06-12 15:54:49 -070058 outputs = [ctx.outputs.feature_xml],
59 arguments = xmlArgs,
60 progress_message = "Generating feature %s XML" % ctx.attr.name,
Thomas Vachuska8e022a92018-07-10 14:47:38 -070061 executable = ctx.executable._feature_writer,
Thomas Vachuskaaab45d12018-06-05 16:39:46 -070062 )
63
Thomas Vachuskae8f06892018-06-12 15:54:49 -070064 ctx.actions.run(
65 inputs = inputs,
Thomas Vachuska8e022a92018-07-10 14:47:38 -070066 outputs = [ctx.outputs.feature_zip],
Thomas Vachuskae8f06892018-06-12 15:54:49 -070067 arguments = bundleArgs,
68 progress_message = "Generating feature %s bundle" % ctx.attr.name,
Thomas Vachuska8e022a92018-07-10 14:47:38 -070069 executable = ctx.executable._feature_bundler,
Thomas Vachuskae8f06892018-06-12 15:54:49 -070070 )
71
Thomas Vachuskaaab45d12018-06-05 16:39:46 -070072osgi_feature = rule(
73 attrs = {
74 "description": attr.string(),
75 "version": attr.string(default = ONOS_VERSION),
Ray Milkey32d99ba2018-06-06 14:15:00 -070076 "required_features": attr.string_list(default = ["onos-api"]),
Thomas Vachuskaaab45d12018-06-05 16:39:46 -070077 "included_bundles": attr.label_list(),
78 "excluded_bundles": attr.label_list(default = []),
79 "generate_file": attr.bool(default = False),
Thomas Vachuska8e022a92018-07-10 14:47:38 -070080 "_feature_writer": attr.label(
Ray Milkey32d99ba2018-06-06 14:15:00 -070081 executable = True,
82 cfg = "host",
83 allow_files = True,
Thomas Vachuska8e022a92018-07-10 14:47:38 -070084 default = Label("//tools/build/bazel:onos_app_tools"),
Ray Milkey32d99ba2018-06-06 14:15:00 -070085 ),
Thomas Vachuska8e022a92018-07-10 14:47:38 -070086 "_feature_bundler": attr.label(
Thomas Vachuskae8f06892018-06-12 15:54:49 -070087 executable = True,
88 cfg = "host",
89 allow_files = True,
Thomas Vachuska8e022a92018-07-10 14:47:38 -070090 default = Label("//tools/build/bazel:osgi_feature_bundler"),
Thomas Vachuskae8f06892018-06-12 15:54:49 -070091 ),
Thomas Vachuskaaab45d12018-06-05 16:39:46 -070092 },
93 outputs = {
94 "feature_xml": "feature-%{name}.xml",
Thomas Vachuska8e022a92018-07-10 14:47:38 -070095 "feature_zip": "feature-%{name}.zip",
Thomas Vachuskaaab45d12018-06-05 16:39:46 -070096 },
97 implementation = _osgi_feature_impl,
98)
99
100# OSGi feature XML header & footer constants
101FEATURES_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">
Ray Milkey70bd55d2022-01-05 11:33:10 -0800105 <repository>mvn:org.apache.karaf.features/standard/4.2.14/xml/features</repository>
Thomas Vachuskaaab45d12018-06-05 16:39:46 -0700106''' % ONOS_VERSION
107
Ray Milkey32d99ba2018-06-06 14:15:00 -0700108FEATURES_FOOTER = "</features>"
Thomas Vachuskaaab45d12018-06-05 16:39:46 -0700109
110# Implementation of a rule to produce an OSGi feature repo XML file
111def _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 Milkey32d99ba2018-06-06 14:15:00 -0700118 inputs += [f]
Thomas Vachuska510419f2018-06-28 17:05:09 -0700119 cmd += "cat %s;" % f.path if f.path.endswith(".xml") else ""
Thomas Vachuskaaab45d12018-06-05 16:39:46 -0700120 cmd += "echo '%s') > %s;" % (FEATURES_FOOTER, output.path)
121
122 ctx.actions.run_shell(
123 inputs = inputs,
Ray Milkey32d99ba2018-06-06 14:15:00 -0700124 outputs = [output],
Thomas Vachuskaaab45d12018-06-05 16:39:46 -0700125 progress_message = "Generating feature repo %s" % ctx.attr.name,
126 command = cmd,
127 )
128
129osgi_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)