Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | # Copyright 2015 The Bazel Authors. All rights reserved. |
Damien Martin-Guillerez | ab13f68 | 2015-07-28 08:19:32 +0000 | [diff] [blame] | 2 | # |
| 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 | |
| 16 | Generate a binary suitable for self-extraction: |
| 17 | |
| 18 | self_extract_binary( |
| 19 | name = "install.sh", |
| 20 | launcher = "launcher.sh", |
| 21 | resources = ["path1/file1", "path2/file2"], |
| 22 | flatten_ressources = ["path3/file3"], |
| 23 | ) |
| 24 | |
| 25 | will generate a file 'install.sh' with a header (launcher.sh) |
| 26 | and a ZIP footer with the following entries: |
| 27 | path1/ |
| 28 | path1/file1 |
| 29 | path2/ |
| 30 | path2/file2 |
| 31 | file3 |
| 32 | |
| 33 | """ |
| 34 | |
| 35 | def _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-Guillerez | ea9a436 | 2015-08-31 12:22:29 +0000 | [diff] [blame] | 39 | 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-Guillerez | ab13f68 | 2015-07-28 08:19:32 +0000 | [diff] [blame] | 43 | 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-Guillerez | ea9a436 | 2015-08-31 12:22:29 +0000 | [diff] [blame] | 58 | ] + touch_empty_files + cp_resources + cp_flatten_resources + [ |
Damien Martin-Guillerez | ab13f68 | 2015-07-28 08:19:32 +0000 | [diff] [blame] | 59 | "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 | |
| 76 | self_extract_binary = rule( |
| 77 | _self_extract_binary, |
| 78 | executable = True, |
| 79 | attrs = { |
| 80 | "launcher": attr.label( |
| 81 | mandatory=True, |
| 82 | allow_files=True, |
| 83 | single_file=True), |
Damien Martin-Guillerez | ea9a436 | 2015-08-31 12:22:29 +0000 | [diff] [blame] | 84 | "empty_files": attr.string_list(default=[]), |
Damien Martin-Guillerez | ab13f68 | 2015-07-28 08:19:32 +0000 | [diff] [blame] | 85 | "resources": attr.label_list( |
| 86 | default=[], |
| 87 | allow_files=True), |
| 88 | "flatten_resources": attr.label_list( |
| 89 | default=[], |
| 90 | allow_files=True), |
| 91 | }, |
| 92 | ) |