-
Notifications
You must be signed in to change notification settings - Fork 1
/
defs.bzl
150 lines (120 loc) · 4.3 KB
/
defs.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
def _write_rn_listfile(ctx = None, listFile = None):
listFileLines = []
for srcFile in ctx.files.srcs:
if srcFile.short_path != srcFile.path:
listFileLines.append(srcFile.path)
listFileLines.append(srcFile.short_path)
ctx.actions.write(
output = listFile,
content = "\n".join(listFileLines),
is_executable = False,
)
return len(listFileLines) > 0
def _write_strip_prefix_listfile(ctx = None, listFile = None):
listFileLines = []
stripPrefix = ctx.attr.strip_prefix
for srcFile in ctx.files.srcs:
if srcFile.short_path.startswith(stripPrefix):
newShortPath = srcFile.short_path[len(stripPrefix):]
listFileLines.append(srcFile.short_path)
listFileLines.append(newShortPath)
ctx.actions.write(
output = listFile,
content = "\n".join(listFileLines),
is_executable = False,
)
return len(listFileLines) > 0
def _write_remap_listfile(ctx = None, listFile = None):
listFileLines = []
for key in ctx.attr.remap_paths:
listFileLines.append(key)
listFileLines.append(ctx.attr.remap_paths[key])
ctx.actions.write(
output = listFile,
content = "\n".join(listFileLines),
is_executable = False,
)
return len(listFileLines) > 0
def _pkg_7z_impl(ctx):
name = ctx.attr.name
archive_name = name + '.' + ctx.attr.extension
archive_file = ctx.actions.declare_file(archive_name)
outs = depset([archive_file])
srcsListFile = ctx.actions.declare_file(name + "__srcs.listfile")
is_p7zip = not ctx.executable._7zip.path.endswith(".exe")
args = ["a", "-y"]
srcPaths = []
if is_p7zip:
args.append("-l")
args.append(archive_file.path)
args.append("@" + srcsListFile.path)
for src in ctx.files.srcs:
srcPaths.append(src.path)
ctx.actions.write(
output = srcsListFile,
content = "\n".join(srcPaths),
is_executable = False,
)
exec_7za = ctx.executable._7zip.path + " "
exec_7za_rn = exec_7za + "rn " + archive_file.path + " "
command = exec_7za + " ".join(args) + " > nul"
listFileInputs = [srcsListFile]
if not ctx.attr.full_paths:
rnSrcsListFile = ctx.actions.declare_file(name + "__rn_srcs.listfile")
listFileInputs.append(rnSrcsListFile)
filledRnListFile = _write_rn_listfile(
ctx = ctx,
listFile = rnSrcsListFile
)
if filledRnListFile:
command += " && " + exec_7za_rn + "@" + rnSrcsListFile.path + "> nul"
if len(ctx.attr.strip_prefix) > 0:
strippedSrcsListFile = ctx.actions.declare_file(name + "__stripped_srcs.listfile")
listFileInputs.append(strippedSrcsListFile)
filledStrippedListFile = _write_strip_prefix_listfile(
ctx = ctx,
listFile = strippedSrcsListFile,
)
if filledStrippedListFile:
command += " && " + exec_7za_rn + "@" + strippedSrcsListFile.path + " > nul"
if len(ctx.attr.remap_paths.keys()) > 0:
remapSrcsListFile = ctx.actions.declare_file(name + "__remap_srcs.listfile")
listFileInputs.append(remapSrcsListFile)
filledRemapListFile = _write_remap_listfile(
ctx = ctx,
listFile = remapSrcsListFile,
)
if filledRemapListFile:
command += " && " + exec_7za_rn + "@" + remapSrcsListFile.path + " > nul"
ctx.actions.run_shell(
outputs = [archive_file],
command = command,
tools = [ctx.executable._7zip],
inputs = ctx.files.srcs + listFileInputs,
arguments = [],
)
return [
DefaultInfo(files = outs),
OutputGroupInfo(listfiles = depset(listFileInputs)),
]
pkg_7z = rule(
implementation = _pkg_7z_impl,
attrs = {
"srcs": attr.label_list(
allow_files = True,
),
"extension": attr.string(
default = '7z',
values = ['7z', 'zip'],
),
"strip_prefix": attr.string(),
"remap_paths": attr.string_dict(default = {}),
"full_paths": attr.bool(default = False),
"_7zip": attr.label(
default = Label("@7zip//:7za"),
executable = True,
allow_single_file = True,
cfg = "host",
),
},
)