blob: 75f19279c33fdf0311d916b14b68708d702cec15 [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):
36 """Implementation for the self_extract_binary rule."""
37 # This is a bit complex for stripping out timestamps
38 zip_artifact = ctx.new_file(ctx.label.name + ".zip")
Damien Martin-Guillerezea9a4362015-08-31 12:22:29 +000039 touch_empty_files = [
40 "mkdir -p $(dirname ${tmpdir}/%s); touch ${tmpdir}/%s" % (f, f)
41 for f in ctx.attr.empty_files
42 ]
Damien Martin-Guillerezab13f682015-07-28 08:19:32 +000043 cp_resources = [
44 ("mkdir -p $(dirname ${tmpdir}/%s)\n" % r.short_path +
45 "cp %s ${tmpdir}/%s" % (r.path, r.short_path))
46 for r in ctx.files.resources
47 ]
48 cp_flatten_resources = [
49 "cp %s ${tmpdir}/%s" % (r.path, r.basename)
50 for r in ctx.files.flatten_resources
51 ]
52 ctx.action(
53 inputs = ctx.files.resources + ctx.files.flatten_resources,
54 outputs = [zip_artifact],
55 command = "\n".join([
56 "tmpdir=$(mktemp -d ${TMPDIR:-/tmp}/tmp.XXXXXXXX)",
57 "trap \"rm -fr ${tmpdir}\" EXIT"
Damien Martin-Guillerezea9a4362015-08-31 12:22:29 +000058 ] + touch_empty_files + cp_resources + cp_flatten_resources + [
Damien Martin-Guillerezab13f682015-07-28 08:19:32 +000059 "find ${tmpdir} -exec touch -t 198001010000.00 '{}' ';'",
60 "(d=${PWD}; cd ${tmpdir}; zip -rq ${d}/%s *)" % zip_artifact.path,
61 ]),
62 mnemonic = "ZipBin",
63 )
64 ctx.action(
65 inputs = [ctx.file.launcher, zip_artifact],
66 outputs = [ctx.outputs.executable],
67 command = "\n".join([
68 "cat %s %s > %s" % (ctx.file.launcher.path,
69 zip_artifact.path,
70 ctx.outputs.executable.path),
71 "zip -qA %s" % ctx.outputs.executable.path
72 ]),
73 mnemonic = "BuildSelfExtractable",
74 )
75
76self_extract_binary = rule(
77 _self_extract_binary,
Damien Martin-Guillerezab13f682015-07-28 08:19:32 +000078 attrs = {
79 "launcher": attr.label(
philwo6f7066e2017-04-28 17:51:22 +020080 mandatory = True,
81 allow_files = True,
82 single_file = True,
83 ),
84 "empty_files": attr.string_list(default = []),
Damien Martin-Guillerezab13f682015-07-28 08:19:32 +000085 "resources": attr.label_list(
philwo6f7066e2017-04-28 17:51:22 +020086 default = [],
87 allow_files = True,
88 ),
Damien Martin-Guillerezab13f682015-07-28 08:19:32 +000089 "flatten_resources": attr.label_list(
philwo6f7066e2017-04-28 17:51:22 +020090 default = [],
91 allow_files = True,
92 ),
93 },
94 executable = True,
95)