blob: 613bf7b34c1876d67515d9e8186159d5e60c2512 [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):
26 output = ctx.outputs.feature_xml
27
Ray Milkey32d99ba2018-06-06 14:15:00 -070028 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 Vachuskaaab45d12018-06-05 16:39:46 -070038
39 inputs = []
40 for dep in ctx.attr.included_bundles:
Ray Milkey32d99ba2018-06-06 14:15:00 -070041 args += ["-b", maven_coordinates(dep.label)]
Thomas Vachuskaaab45d12018-06-05 16:39:46 -070042
Ray Milkey25b785a2018-06-12 09:59:14 -070043 for f in ctx.attr.included_bundles:
44 if java_common.provider in f:
45 inputs += [f.files.to_list()[0]]
46
47 for f in ctx.attr.excluded_bundles:
48 args += ["-e", maven_coordinates(dep.label)]
49 if java_common.provider in f:
50 inputs += [f.files.to_list()[0]]
Thomas Vachuskaaab45d12018-06-05 16:39:46 -070051
52 for f in ctx.attr.required_features:
Ray Milkey32d99ba2018-06-06 14:15:00 -070053 args += ["-f", f]
Thomas Vachuskaaab45d12018-06-05 16:39:46 -070054
Ray Milkey32d99ba2018-06-06 14:15:00 -070055 args += ["-F" if ctx.attr.generate_file else "-E"]
Thomas Vachuskaaab45d12018-06-05 16:39:46 -070056
57 ctx.actions.run(
58 inputs = inputs,
Ray Milkey32d99ba2018-06-06 14:15:00 -070059 outputs = [output],
Thomas Vachuskaaab45d12018-06-05 16:39:46 -070060 arguments = args,
61 progress_message = "Generating feature %s" % ctx.attr.name,
Ray Milkey32d99ba2018-06-06 14:15:00 -070062 executable = ctx.executable._writer,
Thomas Vachuskaaab45d12018-06-05 16:39:46 -070063 )
64
65osgi_feature = rule(
66 attrs = {
67 "description": attr.string(),
68 "version": attr.string(default = ONOS_VERSION),
Ray Milkey32d99ba2018-06-06 14:15:00 -070069 "required_features": attr.string_list(default = ["onos-api"]),
Thomas Vachuskaaab45d12018-06-05 16:39:46 -070070 "included_bundles": attr.label_list(),
71 "excluded_bundles": attr.label_list(default = []),
72 "generate_file": attr.bool(default = False),
Ray Milkey32d99ba2018-06-06 14:15:00 -070073 "_writer": attr.label(
74 executable = True,
75 cfg = "host",
76 allow_files = True,
77 default = Label("//tools/build/bazel:onos_app_writer"),
78 ),
Thomas Vachuskaaab45d12018-06-05 16:39:46 -070079 },
80 outputs = {
81 "feature_xml": "feature-%{name}.xml",
82 },
83 implementation = _osgi_feature_impl,
84)
85
86# OSGi feature XML header & footer constants
87FEATURES_HEADER = '''\
88<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
89<features xmlns="http://karaf.apache.org/xmlns/features/v1.2.0"
90 name="onos-%s">
91 <repository>mvn:org.apache.karaf.features/standard/3.0.8/xml/features</repository>
92''' % ONOS_VERSION
93
Ray Milkey32d99ba2018-06-06 14:15:00 -070094FEATURES_FOOTER = "</features>"
Thomas Vachuskaaab45d12018-06-05 16:39:46 -070095
96# Implementation of a rule to produce an OSGi feature repo XML file
97def _osgi_feature_repo_impl(ctx):
98 output = ctx.outputs.feature_repo_xml
99
100 cmd = "(echo '%s';" % FEATURES_HEADER
101 inputs = []
102 for dep in ctx.attr.exported_features:
103 for f in dep.files.to_list():
Ray Milkey32d99ba2018-06-06 14:15:00 -0700104 inputs += [f]
Thomas Vachuskaaab45d12018-06-05 16:39:46 -0700105 cmd += "cat %s;" % f.path
106 cmd += "echo '%s') > %s;" % (FEATURES_FOOTER, output.path)
107
108 ctx.actions.run_shell(
109 inputs = inputs,
Ray Milkey32d99ba2018-06-06 14:15:00 -0700110 outputs = [output],
Thomas Vachuskaaab45d12018-06-05 16:39:46 -0700111 progress_message = "Generating feature repo %s" % ctx.attr.name,
112 command = cmd,
113 )
114
115osgi_feature_repo = rule(
116 attrs = {
117 "description": attr.string(),
118 "version": attr.string(default = ONOS_VERSION),
119 "exported_features": attr.label_list(),
120 },
121 outputs = {
122 "feature_repo_xml": "feature-repo-%{name}.xml",
123 },
124 implementation = _osgi_feature_repo_impl,
125)