-
Notifications
You must be signed in to change notification settings - Fork 14
/
def.bzl
157 lines (137 loc) · 5.51 KB
/
def.bzl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# Copyright 2018 The Bazel Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Installer Rules
Skylark rules for installing files using Bazel.
"""
load("@bazel_skylib//lib:paths.bzl", "paths")
load("@bazel_skylib//lib:shell.bzl", "shell")
_INSTALLER_GEN_SUFFIX = "_gen"
_TEMPLATE_TARGET = "@com_github_google_rules_install//installer:installer_template"
def _install_files_depset(default_info):
direct = []
transitive = []
if default_info.files:
transitive.append(default_info.files)
if default_info.default_runfiles:
transitive.append(default_info.default_runfiles.files)
if default_info.files_to_run and default_info.files_to_run.executable:
direct = [default_info.files_to_run.executable]
if direct or transitive:
return depset(direct = direct, transitive = transitive)
return None
def _gen_install_binary_impl(ctx):
transitive_runfiles = []
sources = []
targets = []
for input in ctx.attr.data:
input_files = _install_files_depset(input[DefaultInfo])
if not input_files:
continue
transitive_runfiles.append(input_files)
workspace = (input.label.workspace_name or ctx.workspace_name)
external_prefix = "external/" + workspace + "/"
for file in input_files.to_list():
file_path = file.path
root_path = file.root.path + "/"
if file_path.startswith(root_path):
file_path = file_path[len(root_path):]
if file_path.startswith(external_prefix):
file_path = file_path[len(external_prefix):]
sources.append(workspace + "/" + file_path)
target = paths.join(ctx.attr.target_subdir, paths.basename(file.short_path))
targets.append(target)
ctx.actions.expand_template(
output = ctx.outputs.out,
template = ctx.file._template,
substitutions = {
"@@SOURCE_FILES@@": shell.array_literal(sources),
"@@TARGET_NAMES@@": shell.array_literal(targets),
"@@EXECUTABLE@@": shell.quote(repr(ctx.attr.executable)),
"@@INSTALLER_LABEL@@": shell.quote("@{}//{}:{}".format(
ctx.workspace_name,
ctx.label.package,
# Strip leading '_' and suffix form the name.
ctx.label.name[1:-len(_INSTALLER_GEN_SUFFIX)],
)),
"@@GENERATED_WARNING@@": (
"# DO NOT EDIT THIS FILE.\n" +
"# It was generated by {} in @{}.\n" +
"#"
).format(ctx.label, ctx.workspace_name),
},
is_executable = True,
)
return [DefaultInfo(
executable = ctx.outputs.out,
runfiles = ctx.runfiles(
files = [ctx.outputs.out],
transitive_files = depset(transitive = transitive_runfiles),
),
)]
def _compilation_mode_transition_impl(settings, attr):
mode = attr.compilation_mode or settings["//command_line_option:compilation_mode"]
return {"//command_line_option:compilation_mode": mode}
_compilation_mode_transition = transition(
implementation = _compilation_mode_transition_impl,
inputs = ["//command_line_option:compilation_mode"],
outputs = ["//command_line_option:compilation_mode"],
)
_gen_installer = rule(
implementation = _gen_install_binary_impl,
attrs = {
"compilation_mode": attr.string(),
"data": attr.label_list(
mandatory = True,
allow_files = True,
cfg = _compilation_mode_transition,
),
"executable": attr.bool(default = True),
"target_subdir": attr.string(default = ""),
"_template": attr.label(
allow_single_file = True,
default = Label(_TEMPLATE_TARGET),
),
"_whitelist_function_transition": attr.label(default = "@bazel_tools//tools/whitelists/function_transition_whitelist"),
},
executable = True,
outputs = {
"out": "%{name}.sh",
},
)
def installer(name, data, compilation_mode = "opt", executable = True, target_subdir = ""):
"""Creates an installer
This rule creates an installer for targets in data. Running the installer
copies built targets to a given prefix. The prefix has to be passed as an
argument to the installer.
Args:
name: A unique name of this rule.
compilation_mode: If not empty, sets compilation_mode of targets in data.
data: Targets to be installed. File names will not be changed.
executable: If True, the copied files will be set as executable.
target_subdir: Optional subdir under the prefix where the files will be
placed.
"""
installer_name = "_{}{}".format(name, _INSTALLER_GEN_SUFFIX)
_gen_installer(
name = installer_name,
compilation_mode = compilation_mode,
data = data,
executable = executable,
target_subdir = target_subdir,
)
native.sh_binary(
name = name,
srcs = [":" + installer_name],
deps = ["@bazel_tools//tools/bash/runfiles"],
)