blob: 24e3ddf2b5d8b9876bebaf6159209a6278773565 [file] [log] [blame]
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00001# Copyright 2015 The Bazel Authors. All rights reserved.
Damien Martin-Guillerezab13f682015-07-28 08:19:32 +00002#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14"""Self-extracting binary.
15
16Generate a binary suitable for self-extraction:
17
18self_extract_binary(
19 name = "install.sh",
20 launcher = "launcher.sh",
21 resources = ["path1/file1", "path2/file2"],
22 flatten_ressources = ["path3/file3"],
23)
24
25will generate a file 'install.sh' with a header (launcher.sh)
26and a ZIP footer with the following entries:
27 path1/
28 path1/file1
29 path2/
30 path2/file2
31 file3
32
33"""
34
35def _self_extract_binary(ctx):
vladmos20a042f2018-06-01 04:51:21 -070036 """Implementation for the self_extract_binary rule."""
37
38 # This is a bit complex for stripping out timestamps
39 zip_artifact = ctx.new_file(ctx.label.name + ".zip")
40 touch_empty_files = [
41 "mkdir -p $(dirname ${tmpdir}/%s); touch ${tmpdir}/%s" % (f, f)
42 for f in ctx.attr.empty_files
43 ]
44 cp_resources = [
45 ("mkdir -p $(dirname ${tmpdir}/%s)\n" % r.short_path +
46 "cp %s ${tmpdir}/%s" % (r.path, r.short_path))
47 for r in ctx.files.resources
48 ]
49 cp_flatten_resources = [
50 "cp %s ${tmpdir}/%s" % (r.path, r.basename)
51 for r in ctx.files.flatten_resources
52 ]
53 ctx.action(
54 inputs = ctx.files.resources + ctx.files.flatten_resources,
55 outputs = [zip_artifact],
56 command = "\n".join([
57 "tmpdir=$(mktemp -d ${TMPDIR:-/tmp}/tmp.XXXXXXXX)",
58 "trap \"rm -fr ${tmpdir}\" EXIT",
59 ] + touch_empty_files + cp_resources + cp_flatten_resources + [
60 "find ${tmpdir} -exec touch -t 198001010000.00 '{}' ';'",
61 "(d=${PWD}; cd ${tmpdir}; zip -rq ${d}/%s *)" % zip_artifact.path,
62 ]),
63 mnemonic = "ZipBin",
64 )
65 ctx.action(
66 inputs = [ctx.file.launcher, zip_artifact],
67 outputs = [ctx.outputs.executable],
68 command = "\n".join([
69 "cat %s %s > %s" % (
70 ctx.file.launcher.path,
71 zip_artifact.path,
72 ctx.outputs.executable.path,
73 ),
74 "zip -qA %s" % ctx.outputs.executable.path,
75 ]),
76 mnemonic = "BuildSelfExtractable",
77 )
Damien Martin-Guillerezab13f682015-07-28 08:19:32 +000078
79self_extract_binary = rule(
80 _self_extract_binary,
Damien Martin-Guillerezab13f682015-07-28 08:19:32 +000081 attrs = {
82 "launcher": attr.label(
philwo6f7066e2017-04-28 17:51:22 +020083 mandatory = True,
84 allow_files = True,
85 single_file = True,
86 ),
87 "empty_files": attr.string_list(default = []),
Damien Martin-Guillerezab13f682015-07-28 08:19:32 +000088 "resources": attr.label_list(
philwo6f7066e2017-04-28 17:51:22 +020089 default = [],
90 allow_files = True,
91 ),
Damien Martin-Guillerezab13f682015-07-28 08:19:32 +000092 "flatten_resources": attr.label_list(
philwo6f7066e2017-04-28 17:51:22 +020093 default = [],
94 allow_files = True,
95 ),
96 },
97 executable = True,
98)