gen_link_script.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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",
  24. required=True)
  25. parser.add_argument("libraries", nargs="+",
  26. help="List of libraries libc++ depends on")
  27. args = parser.parse_args()
  28. # Use the relative path for the libc++ library.
  29. libcxx = os.path.relpath(args.input, os.path.dirname(args.output))
  30. # Prepare the list of public libraries to link.
  31. public_libs = ['-l%s' % l for l in args.libraries]
  32. # Generate the linker script contents.
  33. contents = "INPUT(%s)" % ' '.join([libcxx] + public_libs)
  34. if args.dryrun:
  35. print("GENERATING SCRIPT: '%s' as file %s" % (contents, args.output))
  36. return 0
  37. # Remove the existing libc++ symlink if it exists.
  38. if os.path.islink(args.output):
  39. os.unlink(args.output)
  40. # Replace it with the linker script.
  41. with open(args.output, 'w') as f:
  42. f.write(contents + "\n")
  43. return 0
  44. if __name__ == '__main__':
  45. sys.exit(main())