blob: 9683789580e44f30989e943db2e596507d421170 [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 = [
27 "-O", ctx.outputs.feature_xml.path,
28 "-n", ctx.attr.name,
29 "-v", ctx.attr.version,
30 "-t", ctx.attr.description,
Ray Milkey32d99ba2018-06-06 14:15:00 -070031 ]
Thomas Vachuskae8f06892018-06-12 15:54:49 -070032 bundleArgs = [ctx.outputs.bundle_zip.path]
Thomas Vachuskaaab45d12018-06-05 16:39:46 -070033 inputs = []
Thomas Vachuskae8f06892018-06-12 15:54:49 -070034
Thomas Vachuskaaab45d12018-06-05 16:39:46 -070035 for dep in ctx.attr.included_bundles:
Thomas Vachuskae8f06892018-06-12 15:54:49 -070036 coord = maven_coordinates(dep.label)
37 xmlArgs += ["-b", coord]
38 if java_common.provider in dep:
39 inputs += [dep.files.to_list()[0]]
40 bundleArgs += [dep.files.to_list()[0].path, coord]
Thomas Vachuskaaab45d12018-06-05 16:39:46 -070041
Thomas Vachuskae8f06892018-06-12 15:54:49 -070042 for f in ctx.attr.excluded_bundles:
43 xmlArgs += ["-e", maven_coordinates(dep.label)]
44 if java_common.provider in f:
45 inputs += [f.files.to_list()[0]]
Thomas Vachuskaaab45d12018-06-05 16:39:46 -070046
47 for f in ctx.attr.required_features:
Thomas Vachuskae8f06892018-06-12 15:54:49 -070048 xmlArgs += ["-f", f]
Thomas Vachuskaaab45d12018-06-05 16:39:46 -070049
Thomas Vachuskae8f06892018-06-12 15:54:49 -070050 xmlArgs += ["-F" if ctx.attr.generate_file else "-E"]
Thomas Vachuskaaab45d12018-06-05 16:39:46 -070051
52 ctx.actions.run(
53 inputs = inputs,
Thomas Vachuskae8f06892018-06-12 15:54:49 -070054 outputs = [ctx.outputs.feature_xml],
55 arguments = xmlArgs,
56 progress_message = "Generating feature %s XML" % ctx.attr.name,
Ray Milkey32d99ba2018-06-06 14:15:00 -070057 executable = ctx.executable._writer,
Thomas Vachuskaaab45d12018-06-05 16:39:46 -070058 )
59
Thomas Vachuskae8f06892018-06-12 15:54:49 -070060 ctx.actions.run(
61 inputs = inputs,
62 outputs = [ctx.outputs.bundle_zip],
63 arguments = bundleArgs,
64 progress_message = "Generating feature %s bundle" % ctx.attr.name,
65 executable = ctx.executable._bundler,
66 )
67
Thomas Vachuskaaab45d12018-06-05 16:39:46 -070068osgi_feature = rule(
69 attrs = {
70 "description": attr.string(),
71 "version": attr.string(default = ONOS_VERSION),
Ray Milkey32d99ba2018-06-06 14:15:00 -070072 "required_features": attr.string_list(default = ["onos-api"]),
Thomas Vachuskaaab45d12018-06-05 16:39:46 -070073 "included_bundles": attr.label_list(),
74 "excluded_bundles": attr.label_list(default = []),
75 "generate_file": attr.bool(default = False),
Ray Milkey32d99ba2018-06-06 14:15:00 -070076 "_writer": attr.label(
77 executable = True,
78 cfg = "host",
79 allow_files = True,
80 default = Label("//tools/build/bazel:onos_app_writer"),
81 ),
Thomas Vachuskae8f06892018-06-12 15:54:49 -070082 "_bundler": attr.label(
83 executable = True,
84 cfg = "host",
85 allow_files = True,
86 default = Label("//tools/build/bazel:onos_feature"),
87 ),
Thomas Vachuskaaab45d12018-06-05 16:39:46 -070088 },
89 outputs = {
90 "feature_xml": "feature-%{name}.xml",
Thomas Vachuskae8f06892018-06-12 15:54:49 -070091 "bundle_zip": "feature-%{name}.zip",
Thomas Vachuskaaab45d12018-06-05 16:39:46 -070092 },
93 implementation = _osgi_feature_impl,
94)
95
96# OSGi feature XML header & footer constants
97FEATURES_HEADER = '''\
98<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
99<features xmlns="http://karaf.apache.org/xmlns/features/v1.2.0"
100 name="onos-%s">
101 <repository>mvn:org.apache.karaf.features/standard/3.0.8/xml/features</repository>
102''' % ONOS_VERSION
103
Ray Milkey32d99ba2018-06-06 14:15:00 -0700104FEATURES_FOOTER = "</features>"
Thomas Vachuskaaab45d12018-06-05 16:39:46 -0700105
106# Implementation of a rule to produce an OSGi feature repo XML file
107def _osgi_feature_repo_impl(ctx):
108 output = ctx.outputs.feature_repo_xml
109
110 cmd = "(echo '%s';" % FEATURES_HEADER
111 inputs = []
112 for dep in ctx.attr.exported_features:
113 for f in dep.files.to_list():
Ray Milkey32d99ba2018-06-06 14:15:00 -0700114 inputs += [f]
Thomas Vachuskaaab45d12018-06-05 16:39:46 -0700115 cmd += "cat %s;" % f.path
116 cmd += "echo '%s') > %s;" % (FEATURES_FOOTER, output.path)
117
118 ctx.actions.run_shell(
119 inputs = inputs,
Ray Milkey32d99ba2018-06-06 14:15:00 -0700120 outputs = [output],
Thomas Vachuskaaab45d12018-06-05 16:39:46 -0700121 progress_message = "Generating feature repo %s" % ctx.attr.name,
122 command = cmd,
123 )
124
125osgi_feature_repo = rule(
126 attrs = {
127 "description": attr.string(),
128 "version": attr.string(default = ONOS_VERSION),
129 "exported_features": attr.label_list(),
130 },
131 outputs = {
132 "feature_repo_xml": "feature-repo-%{name}.xml",
133 },
134 implementation = _osgi_feature_repo_impl,
135)