Yong Hong | 97ca3b9 | 2021-03-22 21:28:18 +0800 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # Copyright 2021 The Chromium OS Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | """Bundle the given ELF executable binary and its dependencies together. |
| 6 | |
| 7 | This script provides a simple implementation to build a self-extractable |
| 8 | binary that can install the executable binary and its dependent dynamic-linked |
| 9 | libraries to the specific path. |
| 10 | """ |
| 11 | |
| 12 | import argparse |
| 13 | import subprocess |
| 14 | import sys |
| 15 | import tempfile |
| 16 | from typing import List |
| 17 | |
| 18 | # Hard-coded the absolute path to `lddtree` because the dirname of it might |
| 19 | # not be listed in the environment variable `PATH` when this script is invoked. |
| 20 | _LDDTREE_BIN_PATH = '/mnt/host/source/chromite/bin/lddtree' |
| 21 | |
| 22 | |
| 23 | def main(argv: List[str]) -> int: |
| 24 | ap = argparse.ArgumentParser(description=__doc__) |
| 25 | ap.add_argument( |
| 26 | '--root-dir', |
| 27 | required=True, |
| 28 | help='Path to the root directory containing build dependencies.') |
| 29 | ap.add_argument('--target-path', |
| 30 | required=True, |
| 31 | help='Path to the ELF executable binary to bundle.') |
| 32 | ap.add_argument('--bundle-description', |
| 33 | help='The description for the bundled file.') |
| 34 | ap.add_argument('--output-path', |
| 35 | required=True, |
| 36 | help='Path to the output bundled file.') |
| 37 | opts = ap.parse_args(argv) |
| 38 | |
| 39 | with tempfile.TemporaryDirectory() as archive_dir: |
| 40 | subprocess.check_call([ |
| 41 | _LDDTREE_BIN_PATH, '--copy-to-tree', archive_dir, '--root', |
| 42 | opts.root_dir, '--no-auto-root', '--generate-wrappers', '--bindir', '/', |
| 43 | '--verbose', opts.target_path |
| 44 | ]) |
| 45 | |
| 46 | # Build the bundled file. |
| 47 | subprocess.check_call([ |
| 48 | 'makeself', '--license', '/dev/null', '--nox11', '--current', '--bzip2', |
| 49 | archive_dir, opts.output_path, opts.bundle_description |
| 50 | ]) |
| 51 | |
| 52 | return 0 |
| 53 | |
| 54 | |
| 55 | if __name__ == '__main__': |
| 56 | sys.exit(main(sys.argv[1:])) |