xref: /llvm-project/llvm/utils/gn/secondary/libcxx/utils/gen_link_script.py (revision b71edfaa4ec3c998aadb35255ce2f60bba2940b0)
10ff3cc20SNico Weber#!/usr/bin/env python3
28f7a3204SNico Weber# ===----------------------------------------------------------------------===##
38f7a3204SNico Weber#
48f7a3204SNico Weber# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
58f7a3204SNico Weber# See https://llvm.org/LICENSE.txt for license information.
68f7a3204SNico Weber# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
78f7a3204SNico Weber#
88f7a3204SNico Weber# ===----------------------------------------------------------------------===##
98f7a3204SNico Weber
108f7a3204SNico Weber"""
118f7a3204SNico WeberGenerate a linker script that links libc++ to the proper ABI library.
128f7a3204SNico WeberAn example script for c++abi would look like "INPUT(libc++.so.1 -lc++abi)".
138f7a3204SNico Weber"""
148f7a3204SNico Weber
158f7a3204SNico Weberimport argparse
168f7a3204SNico Weberimport os
178f7a3204SNico Weberimport sys
188f7a3204SNico Weber
198f7a3204SNico Weber
208f7a3204SNico Weberdef main():
218f7a3204SNico Weber    parser = argparse.ArgumentParser(description=__doc__)
228f7a3204SNico Weber    parser.add_argument("--input", help="Path to libc++ library", required=True)
23*b71edfaaSTobias Hieta    parser.add_argument("--output", help="Path to libc++ linker script", required=True)
24*b71edfaaSTobias Hieta    parser.add_argument(
25*b71edfaaSTobias Hieta        "libraries", nargs="+", help="List of libraries libc++ depends on"
26*b71edfaaSTobias Hieta    )
278f7a3204SNico Weber    args = parser.parse_args()
288f7a3204SNico Weber
298f7a3204SNico Weber    # Use the relative path for the libc++ library.
308f7a3204SNico Weber    libcxx = os.path.relpath(args.input, os.path.dirname(args.output))
318f7a3204SNico Weber
328f7a3204SNico Weber    # Prepare the list of public libraries to link.
33*b71edfaaSTobias Hieta    public_libs = ["-l%s" % l for l in args.libraries]
348f7a3204SNico Weber
358f7a3204SNico Weber    # Generate the linker script contents.
36*b71edfaaSTobias Hieta    contents = "INPUT(%s)" % " ".join([libcxx] + public_libs)
378f7a3204SNico Weber
388f7a3204SNico Weber    # Remove the existing libc++ symlink if it exists.
398f7a3204SNico Weber    if os.path.islink(args.output):
408f7a3204SNico Weber        os.unlink(args.output)
418f7a3204SNico Weber
428f7a3204SNico Weber    # Replace it with the linker script.
43*b71edfaaSTobias Hieta    with open(args.output, "w") as f:
448f7a3204SNico Weber        f.write(contents + "\n")
458f7a3204SNico Weber
468f7a3204SNico Weber    return 0
478f7a3204SNico Weber
488f7a3204SNico Weber
49*b71edfaaSTobias Hietaif __name__ == "__main__":
508f7a3204SNico Weber    sys.exit(main())
51