blob: 44b726ddfe90717a23c80492efd86b82621a0fbf [file] [log] [blame]
Yong Hong97ca3b92021-03-22 21:28:18 +08001#!/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
7This script provides a simple implementation to build a self-extractable
8binary that can install the executable binary and its dependent dynamic-linked
9libraries to the specific path.
10"""
11
12import argparse
13import subprocess
14import sys
15import tempfile
16from 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
23def 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
55if __name__ == '__main__':
56 sys.exit(main(sys.argv[1:]))