-
Notifications
You must be signed in to change notification settings - Fork 18
/
library.bzl
152 lines (139 loc) · 5.15 KB
/
library.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
# 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
#
# http://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.
load(
"@rules_r//R/internal:common.bzl",
_layer_library_deps = "layer_library_deps",
_library_deps = "library_deps",
_runfiles = "runfiles",
)
load("@rules_r//R:providers.bzl", "RLibrary", "RPackage")
def _library_impl(ctx):
info = ctx.toolchains["@rules_r//R:toolchain_type"].RInfo
library_deps = _library_deps(ctx.attr.pkgs)
ctx.actions.expand_template(
template = ctx.file._library_sh_tpl,
output = ctx.outputs.executable,
substitutions = {
"{library_path}": ctx.attr.library_path,
"{lib_dirs}": "\n".join([d.short_path for d in library_deps.lib_dirs]),
"{Rscript}": " ".join(info.rscript),
},
is_executable = True,
)
layered_lib_files = _layer_library_deps(ctx, library_deps)
files_tools = library_deps.transitive_tools.to_list()
container_file_map = dict(layered_lib_files)
container_file_map.update({"tools": files_tools})
runfiles = ctx.runfiles(files = library_deps.lib_dirs + [info.state])
runfiles = runfiles.merge(_runfiles(ctx, ctx.attr.pkgs))
return [
DefaultInfo(
files = depset([ctx.outputs.executable]),
runfiles = runfiles,
),
RLibrary(
container_file_map = container_file_map,
pkgs = ctx.attr.pkgs,
),
OutputGroupInfo(
external = layered_lib_files["external"],
internal = layered_lib_files["internal"],
tools = files_tools,
),
]
r_library = rule(
attrs = {
"pkgs": attr.label_list(
providers = [RPackage],
doc = "Package (and dependencies) to install",
),
"library_path": attr.string(
default = "",
doc = ("If different from system default, default library " +
"location for installation. For runtime overrides, " +
"use bazel run [target] -- -l [path]"),
),
"_library_sh_tpl": attr.label(
allow_single_file = True,
default = "@rules_r//R/scripts:library.sh.tpl",
),
},
doc = ("Rule to install the given package and all dependencies to " +
"a user provided or system default R library site."),
executable = True,
toolchains = ["@rules_r//R:toolchain_type"],
implementation = _library_impl,
)
def _r_library_tar_impl(ctx):
provider = ctx.attr.library[RLibrary]
file_inputs = []
args = ctx.actions.args()
args.add("--output=" + ctx.outputs.out.path)
args.use_param_file("--flagfile=%s")
for layer in ctx.attr.layers:
file_inputs += provider.container_file_map[layer]
if layer == "tools":
path_prefix = ctx.attr.tools_install_path
for f in provider.container_file_map[layer]:
args.add("--file=%s=%s" % (f.path, path_prefix + "/" + f.basename))
else:
path_prefix = ctx.attr.library_path
for f in provider.container_file_map[layer]:
args.add("--file=%s=%s" % (f.path, path_prefix))
if ctx.attr.compression:
args.add("--compression=%s" % ctx.attr.compression)
ctx.actions.run(
outputs = [ctx.outputs.out],
inputs = file_inputs,
executable = ctx.executable._build_tar,
arguments = [args],
mnemonic = "RLibraryTar",
)
return [DefaultInfo(runfiles = ctx.runfiles([ctx.outputs.out]))]
r_library_tar = rule(
attrs = {
"library": attr.label(
providers = [RLibrary],
doc = "The r_library target that this tar will capture.",
),
"library_path": attr.string(
doc = ("Subdirectory within the container where all the " +
"packages are installed"),
),
"tools_install_path": attr.string(
doc = ("Subdirectory within the container where all the " +
"tools are installed"),
),
"layers": attr.string_list(
default = [
"external",
"internal",
"tools",
],
doc = "Library layers to include in the tar.",
),
"compression": attr.string(default = "gz"),
"_build_tar": attr.label(
default = Label("@rules_pkg//pkg/private/tar:build_tar"),
cfg = "host",
executable = True,
allow_files = True,
),
},
doc = "Rule to create a tar archive of the files in this library.",
outputs = {
"out": "%{name}.tar" + ".%{compression}" if "%{compression}" else "",
},
implementation = _r_library_tar_impl,
)