Eric Fiselier | 8cbf020 | 2015-10-14 19:54:03 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
Eric Fiselier | a9e91f3 | 2016-01-19 21:58:49 +0000 | [diff] [blame] | 2 | #===----------------------------------------------------------------------===## |
| 3 | # |
| 4 | # The LLVM Compiler Infrastructure |
| 5 | # |
| 6 | # This file is dual licensed under the MIT and the University of Illinois Open |
| 7 | # Source Licenses. See LICENSE.TXT for details. |
| 8 | # |
| 9 | #===----------------------------------------------------------------------===## |
| 10 | |
Eric Fiselier | 8cbf020 | 2015-10-14 19:54:03 +0000 | [diff] [blame] | 11 | import os |
| 12 | import sys |
| 13 | |
| 14 | def print_and_exit(msg): |
| 15 | sys.stderr.write(msg + '\n') |
| 16 | sys.exit(1) |
| 17 | |
| 18 | def usage_and_exit(): |
| 19 | print_and_exit("Usage: ./gen_link_script.py [--help] [--dryrun] <path/to/libcxx.so> <abi_libname>") |
| 20 | |
| 21 | def help_and_exit(): |
| 22 | help_msg = \ |
| 23 | """Usage |
| 24 | |
| 25 | gen_link_script.py [--help] [--dryrun] <path/to/libcxx.so> <abi_libname> |
| 26 | |
| 27 | Generate a linker script that links libc++ to the proper ABI library. |
| 28 | The script replaces the specified libc++ symlink. |
| 29 | An example script for c++abi would look like "INPUT(libc++.so.1 -lc++abi)". |
| 30 | |
| 31 | Arguments |
| 32 | <path/to/libcxx.so> - The top level symlink to the versioned libc++ shared |
| 33 | library. This file is replaced with a linker script. |
| 34 | <abi_libname> - The name of the ABI library to use in the linker script. |
| 35 | The name must be one of [c++abi, stdc++, supc++, cxxrt]. |
| 36 | |
| 37 | Exit Status: |
| 38 | 0 if OK, |
| 39 | 1 if the action failed. |
| 40 | """ |
| 41 | print_and_exit(help_msg) |
| 42 | |
| 43 | def parse_args(): |
| 44 | args = list(sys.argv) |
| 45 | del args[0] |
| 46 | if len(args) == 0: |
| 47 | usage_and_exit() |
| 48 | if args[0] == '--help': |
| 49 | help_and_exit() |
| 50 | dryrun = '--dryrun' == args[0] |
| 51 | if dryrun: |
| 52 | del args[0] |
| 53 | if len(args) != 2: |
| 54 | usage_and_exit() |
| 55 | symlink_file = args[0] |
| 56 | abi_libname = args[1] |
| 57 | return dryrun, symlink_file, abi_libname |
| 58 | |
| 59 | def main(): |
| 60 | dryrun, symlink_file, abi_libname = parse_args() |
| 61 | |
| 62 | # Check that the given libc++.so file is a valid symlink. |
| 63 | if not os.path.islink(symlink_file): |
| 64 | print_and_exit("symlink file %s is not a symlink" % symlink_file) |
| 65 | |
| 66 | # Read the symlink so we know what libc++ to link to in the linker script. |
| 67 | linked_libcxx = os.readlink(symlink_file) |
| 68 | |
| 69 | # Check that the abi_libname is one of the supported values. |
| 70 | supported_abi_list = ['c++abi', 'stdc++', 'supc++', 'cxxrt'] |
| 71 | if abi_libname not in supported_abi_list: |
| 72 | print_and_exit("abi name '%s' is not supported: Use one of %r" % |
| 73 | (abi_libname, supported_abi_list)) |
| 74 | |
| 75 | # Generate the linker script contents and print the script and destination |
| 76 | # information. |
| 77 | contents = "INPUT(%s -l%s)" % (linked_libcxx, abi_libname) |
| 78 | print("GENERATING SCRIPT: '%s' as file %s" % (contents, symlink_file)) |
| 79 | |
| 80 | # Remove the existing libc++ symlink and replace it with the script. |
| 81 | if not dryrun: |
| 82 | os.unlink(symlink_file) |
| 83 | with open(symlink_file, 'w') as f: |
| 84 | f.write(contents + "\n") |
| 85 | |
| 86 | |
| 87 | if __name__ == '__main__': |
| 88 | main() |