-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpkg.bzl
100 lines (81 loc) · 3.05 KB
/
pkg.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
load("@rules_pkg//pkg:providers.bzl", "PackageFilegroupInfo", "PackageFilesInfo", "PackageSymlinkInfo")
def _runfile_path(workspace_name, file):
path = file.short_path
return path[len("../"):] if path.startswith("../") else "%s/%s" % (workspace_name, path)
def _runfiles_pkg_files(workspace_name, runfiles):
files = {}
for file in runfiles.files.to_list():
files[_runfile_path(workspace_name, file)] = file
for file in runfiles.symlinks.to_list():
files[file.path] = "%s/%s" % (workspace_name, files.target_file)
for file in runfiles.root_symlinks.to_list():
files[file.path] = files.target_file
return PackageFilesInfo(
dest_src_map = files,
attributes = {"mode": "0755"},
)
def _pkg_runfiles_impl(ctx):
runfiles = ctx.attr.runfiles[DefaultInfo]
label = ctx.label
workspace_name = ctx.workspace_name
runfiles_files = _runfiles_pkg_files(workspace_name, runfiles.default_runfiles)
pkg_filegroup_info = PackageFilegroupInfo(
pkg_dirs = [],
pkg_files = [(runfiles_files, label)],
pkg_symlinks = [],
)
default_info = DefaultInfo(files = depset(runfiles_files.dest_src_map.values()))
return [default_info, pkg_filegroup_info]
pkg_runfiles = rule(
implementation = _pkg_runfiles_impl,
attrs = {
"runfiles": attr.label(
doc = "Runfiles.",
mandatory = True,
),
},
provides = [PackageFilegroupInfo],
)
def _pkg_executable_impl(ctx):
bin = ctx.attr.bin[DefaultInfo]
bin_executable = ctx.executable.bin
bin_executable_ext = bin_executable.extension
if bin_executable_ext:
bin_executable_ext = "." + bin_executable_ext
path = ctx.attr.path + bin_executable_ext
label = ctx.label
workspace_name = ctx.workspace_name
runfiles_files = _runfiles_pkg_files(workspace_name, bin.default_runfiles)
dest_src_map = {"%s.runfiles/%s" % (path, p): file for p, file in runfiles_files.dest_src_map.items()}
runfiles_files = PackageFilesInfo(
dest_src_map = dest_src_map,
attributes = runfiles_files.attributes,
)
executable_symlink = PackageSymlinkInfo(
attributes = {"mode": "0755"},
destination = path,
target = "%s.runfiles/%s" % (path, _runfile_path(workspace_name, bin_executable)),
)
pkg_filegroup_info = PackageFilegroupInfo(
pkg_dirs = [],
pkg_files = [(runfiles_files, label)],
pkg_symlinks = [(executable_symlink, label)],
)
default_info = DefaultInfo(files = depset(runfiles_files.dest_src_map.values()))
return [default_info, pkg_filegroup_info]
pkg_executable = rule(
implementation = _pkg_executable_impl,
attrs = {
"bin": attr.label(
doc = "Executable.",
executable = True,
cfg = "target",
mandatory = True,
),
"path": attr.string(
doc = "Packaged path of executable. (Runfiles tree will be at <path>.runfiles.)",
mandatory = True,
),
},
provides = [PackageFilegroupInfo],
)