blob: 7c29e7899ffb43d30f533269ead6a2b76a9d13da [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 for f in dep.java.outputs.jars:
Ray Milkey32d99ba2018-06-06 14:15:00 -070043 inputs += [f.class_jar]
Thomas Vachuskaaab45d12018-06-05 16:39:46 -070044
45 for dep in ctx.attr.excluded_bundles:
Ray Milkey32d99ba2018-06-06 14:15:00 -070046 args += ["-e", maven_coordinates(dep.label)]
Thomas Vachuskaaab45d12018-06-05 16:39:46 -070047 for f in dep.java.outputs.jars:
Ray Milkey32d99ba2018-06-06 14:15:00 -070048 inputs += [f.class_jar]
Thomas Vachuskaaab45d12018-06-05 16:39:46 -070049
50 for f in ctx.attr.required_features:
Ray Milkey32d99ba2018-06-06 14:15:00 -070051 args += ["-f", f]
Thomas Vachuskaaab45d12018-06-05 16:39:46 -070052
Ray Milkey32d99ba2018-06-06 14:15:00 -070053 args += ["-F" if ctx.attr.generate_file else "-E"]
Thomas Vachuskaaab45d12018-06-05 16:39:46 -070054
55 ctx.actions.run(
56 inputs = inputs,
Ray Milkey32d99ba2018-06-06 14:15:00 -070057 outputs = [output],
Thomas Vachuskaaab45d12018-06-05 16:39:46 -070058 arguments = args,
59 progress_message = "Generating feature %s" % ctx.attr.name,
Ray Milkey32d99ba2018-06-06 14:15:00 -070060 executable = ctx.executable._writer,
Thomas Vachuskaaab45d12018-06-05 16:39:46 -070061 )
62
63osgi_feature = rule(
64 attrs = {
65 "description": attr.string(),
66 "version": attr.string(default = ONOS_VERSION),
Ray Milkey32d99ba2018-06-06 14:15:00 -070067 "required_features": attr.string_list(default = ["onos-api"]),
Thomas Vachuskaaab45d12018-06-05 16:39:46 -070068 "included_bundles": attr.label_list(),
69 "excluded_bundles": attr.label_list(default = []),
70 "generate_file": attr.bool(default = False),
Ray Milkey32d99ba2018-06-06 14:15:00 -070071 "_writer": attr.label(
72 executable = True,
73 cfg = "host",
74 allow_files = True,
75 default = Label("//tools/build/bazel:onos_app_writer"),
76 ),
Thomas Vachuskaaab45d12018-06-05 16:39:46 -070077 },
78 outputs = {
79 "feature_xml": "feature-%{name}.xml",
80 },
81 implementation = _osgi_feature_impl,
82)
83
84# OSGi feature XML header & footer constants
85FEATURES_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 Milkey32d99ba2018-06-06 14:15:00 -070092FEATURES_FOOTER = "</features>"
Thomas Vachuskaaab45d12018-06-05 16:39:46 -070093
94# Implementation of a rule to produce an OSGi feature repo XML file
95def _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 Milkey32d99ba2018-06-06 14:15:00 -0700102 inputs += [f]
Thomas Vachuskaaab45d12018-06-05 16:39:46 -0700103 cmd += "cat %s;" % f.path
104 cmd += "echo '%s') > %s;" % (FEATURES_FOOTER, output.path)
105
106 ctx.actions.run_shell(
107 inputs = inputs,
Ray Milkey32d99ba2018-06-06 14:15:00 -0700108 outputs = [output],
Thomas Vachuskaaab45d12018-06-05 16:39:46 -0700109 progress_message = "Generating feature repo %s" % ctx.attr.name,
110 command = cmd,
111 )
112
113osgi_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)