gen_link_script.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. Generate a linker script that links libc++ to the proper ABI library.
  11. An example script for c++abi would look like "INPUT(libc++.so.1 -lc++abi)".
  12. """
  13. import argparse
  14. import os
  15. import sys
  16. def main():
  17. parser = argparse.ArgumentParser(description=__doc__)
  18. parser.add_argument("--dryrun", help="Don't write any output",
  19. action="store_true", default=False)
  20. parser.add_argument("--rename", action="store_true", default=False,
  21. help="Rename the output as input so we can replace it")
  22. parser.add_argument("--input", help="Path to libc++ library", required=True)
  23. parser.add_argument("--output", help="Path to libc++ linker script", required=True)
  24. parser.add_argument("libraries", nargs="+",
  25. help="List of libraries libc++ depends on")
  26. args = parser.parse_args()
  27. # Use the relative path for the libc++ library.
  28. libcxx = os.path.relpath(args.input, os.path.dirname(args.output))
  29. # Prepare the list of public libraries to link.
  30. public_libs = ['-l%s' % l for l in args.libraries]
  31. # Generate the linker script contents.
  32. contents = "INPUT(%s)" % ' '.join([libcxx] + public_libs)
  33. print("GENERATING SCRIPT: '%s' as file %s" % (contents, args.output))
  34. if args.dryrun:
  35. return 0
  36. # Remove the existing libc++ symlink if it exists.
  37. if os.path.islink(args.output):
  38. os.unlink(args.output)
  39. # Replace it with the linker script.
  40. with open(args.output, 'w') as f:
  41. f.write(contents + "\n")
  42. return 0
  43. if __name__ == '__main__':
  44. sys.exit(main())