1#!/usr/bin/env python
2#===----------------------------------------------------------------------===##
3#
4# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5# See https://llvm.org/LICENSE.txt for license information.
6# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7#
8#===----------------------------------------------------------------------===##
9
10"""
11Generate a linker script that links libc++ to the proper ABI library.
12An example script for c++abi would look like "INPUT(libc++.so.1 -lc++abi)".
13"""
14
15import argparse
16import os
17import sys
18
19
20def main():
21    parser = argparse.ArgumentParser(description=__doc__)
22    parser.add_argument("--input", help="Path to libc++ library", required=True)
23    parser.add_argument("--output", help="Path to libc++ linker script",
24                        required=True)
25    parser.add_argument("libraries", nargs="+",
26                        help="List of libraries libc++ depends on")
27    args = parser.parse_args()
28
29    # Use the relative path for the libc++ library.
30    libcxx = os.path.relpath(args.input, os.path.dirname(args.output))
31
32    # Prepare the list of public libraries to link.
33    public_libs = ['-l%s' % l for l in args.libraries]
34
35    # Generate the linker script contents.
36    contents = "INPUT(%s)" % ' '.join([libcxx] + public_libs)
37
38    # Remove the existing libc++ symlink if it exists.
39    if os.path.islink(args.output):
40        os.unlink(args.output)
41
42    # Replace it with the linker script.
43    with open(args.output, 'w') as f:
44        f.write(contents + "\n")
45
46    return 0
47
48
49if __name__ == '__main__':
50    sys.exit(main())
51