Skip to content

Commit e9a2d1c

Browse files
committedApr 22, 2019
[libcxx] Update gen_link_script.py to support different input and output
This enables the use of this script from other build systems like GN which don't support post-build actions as well as for static archives. Differential Revision: https://reviews.llvm.org/D60309 llvm-svn: 358915
1 parent 2eea99a commit e9a2d1c

File tree

2 files changed

+34
-62
lines changed

2 files changed

+34
-62
lines changed
 

‎libcxx/lib/CMakeLists.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,8 @@ if (LIBCXX_ENABLE_SHARED)
230230
COMMAND
231231
${PYTHON_EXECUTABLE} ${LIBCXX_SOURCE_DIR}/utils/gen_link_script.py
232232
ARGS
233-
"$<TARGET_LINKER_FILE:cxx_shared>"
233+
--input "$<TARGET_SONAME_FILE:cxx_shared>"
234+
--output "$<TARGET_LINKER_FILE:cxx_shared>"
234235
${LIBCXX_INTERFACE_LIBRARY_NAMES}
235236
WORKING_DIRECTORY ${LIBCXX_BUILD_DIR}
236237
)

‎libcxx/utils/gen_link_script.py

+32-61
Original file line numberDiff line numberDiff line change
@@ -7,77 +7,48 @@
77
#
88
#===----------------------------------------------------------------------===##
99

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
1016
import os
1117
import sys
1218

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
5619

5720
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()
5931

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]
6334

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))
6638

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)
6945

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")
7449

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
8051

8152

8253
if __name__ == '__main__':
83-
main()
54+
sys.exit(main())

0 commit comments

Comments
 (0)
Please sign in to comment.