|
7 | 7 | #
|
8 | 8 | #===----------------------------------------------------------------------===##
|
9 | 9 |
|
| 10 | +""" |
| 11 | +Generate a linker script that links libc++ to the proper ABI library. |
| 12 | +An example script for c++abi would look like "INPUT(libc++.so.1 -lc++abi)". |
| 13 | +""" |
| 14 | + |
| 15 | +import argparse |
10 | 16 | import os
|
11 | 17 | import sys
|
12 | 18 |
|
13 |
| -def print_and_exit(msg): |
14 |
| - sys.stderr.write(msg + '\n') |
15 |
| - sys.exit(1) |
16 |
| - |
17 |
| -def usage_and_exit(): |
18 |
| - print_and_exit("Usage: ./gen_link_script.py [--help] [--dryrun] <path/to/libcxx.so> <public_libs>...") |
19 |
| - |
20 |
| -def help_and_exit(): |
21 |
| - help_msg = \ |
22 |
| -"""Usage |
23 |
| -
|
24 |
| - gen_link_script.py [--help] [--dryrun] <path/to/libcxx.so> <public_libs>... |
25 |
| -
|
26 |
| - Generate a linker script that links libc++ to the proper ABI library. |
27 |
| - The script replaces the specified libc++ symlink. |
28 |
| - An example script for c++abi would look like "INPUT(libc++.so.1 -lc++abi)". |
29 |
| -
|
30 |
| -Arguments |
31 |
| - <path/to/libcxx.so> - The top level symlink to the versioned libc++ shared |
32 |
| - library. This file is replaced with a linker script. |
33 |
| - <public_libs> - List of library names to include in linker script. |
34 |
| -
|
35 |
| -Exit Status: |
36 |
| - 0 if OK, |
37 |
| - 1 if the action failed. |
38 |
| -""" |
39 |
| - print_and_exit(help_msg) |
40 |
| - |
41 |
| -def parse_args(): |
42 |
| - args = list(sys.argv) |
43 |
| - del args[0] |
44 |
| - if len(args) == 0: |
45 |
| - usage_and_exit() |
46 |
| - if args[0] == '--help': |
47 |
| - help_and_exit() |
48 |
| - dryrun = '--dryrun' == args[0] |
49 |
| - if dryrun: |
50 |
| - del args[0] |
51 |
| - if len(args) < 2: |
52 |
| - usage_and_exit() |
53 |
| - symlink_file = args[0] |
54 |
| - public_libs = args[1:] |
55 |
| - return dryrun, symlink_file, public_libs |
56 | 19 |
|
57 | 20 | def main():
|
58 |
| - dryrun, symlink_file, public_libs = parse_args() |
| 21 | + parser = argparse.ArgumentParser(description=__doc__) |
| 22 | + parser.add_argument("--dryrun", help="Don't write any output", |
| 23 | + action="store_true", default=False) |
| 24 | + parser.add_argument("--rename", action="store_true", default=False, |
| 25 | + help="Rename the output as input so we can replace it") |
| 26 | + parser.add_argument("--input", help="Path to libc++ library", required=True) |
| 27 | + parser.add_argument("--output", help="Path to libc++ linker script", required=True) |
| 28 | + parser.add_argument("libraries", nargs="+", |
| 29 | + help="List of libraries libc++ depends on") |
| 30 | + args = parser.parse_args() |
59 | 31 |
|
60 |
| - # Check that the given libc++.so file is a valid symlink. |
61 |
| - if not os.path.islink(symlink_file): |
62 |
| - print_and_exit("symlink file %s is not a symlink" % symlink_file) |
| 32 | + # Prepare the list of public libraries to link. |
| 33 | + public_libs = ['-l%s' % l for l in args.libraries] |
63 | 34 |
|
64 |
| - # Read the symlink so we know what libc++ to link to in the linker script. |
65 |
| - linked_libcxx = os.readlink(symlink_file) |
| 35 | + # Generate the linker script contents. |
| 36 | + contents = "INPUT(%s)" % ' '.join([args.input] + public_libs) |
| 37 | + print("GENERATING SCRIPT: '%s' as file %s" % (contents, args.output)) |
66 | 38 |
|
67 |
| - # Prepare the list of public libraries to link. |
68 |
| - public_libs = ['-l%s' % l for l in public_libs] |
| 39 | + if args.dryrun: |
| 40 | + return 0 |
| 41 | + |
| 42 | + # Remove the existing libc++ symlink if it exists. |
| 43 | + if os.path.islink(args.output): |
| 44 | + os.unlink(args.output) |
69 | 45 |
|
70 |
| - # Generate the linker script contents and print the script and destination |
71 |
| - # information. |
72 |
| - contents = "INPUT(%s %s)" % (linked_libcxx, ' '.join(public_libs)) |
73 |
| - print("GENERATING SCRIPT: '%s' as file %s" % (contents, symlink_file)) |
| 46 | + # Replace it with the linker script. |
| 47 | + with open(args.output, 'w') as f: |
| 48 | + f.write(contents + "\n") |
74 | 49 |
|
75 |
| - # Remove the existing libc++ symlink and replace it with the script. |
76 |
| - if not dryrun: |
77 |
| - os.unlink(symlink_file) |
78 |
| - with open(symlink_file, 'w') as f: |
79 |
| - f.write(contents + "\n") |
| 50 | + return 0 |
80 | 51 |
|
81 | 52 |
|
82 | 53 | if __name__ == '__main__':
|
83 |
| - main() |
| 54 | + sys.exit(main()) |
0 commit comments