blob: 9f1f0b771fc1f4af5eb05c0a2b4eccf00ed4f58d [file] [log] [blame]
Eric Fiselier8cbf0202015-10-14 19:54:03 +00001#!/usr/bin/env python
Eric Fiseliera9e91f32016-01-19 21:58:49 +00002#===----------------------------------------------------------------------===##
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 Fiselier8cbf0202015-10-14 19:54:03 +000011import os
12import sys
13
14def print_and_exit(msg):
15 sys.stderr.write(msg + '\n')
16 sys.exit(1)
17
18def usage_and_exit():
19 print_and_exit("Usage: ./gen_link_script.py [--help] [--dryrun] <path/to/libcxx.so> <abi_libname>")
20
21def 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
31Arguments
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
37Exit Status:
38 0 if OK,
39 1 if the action failed.
40"""
41 print_and_exit(help_msg)
42
43def 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
59def 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
87if __name__ == '__main__':
88 main()