gen_link_script.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/usr/bin/env python
  2. #===----------------------------------------------------------------------===##
  3. #
  4. # The LLVM Compiler Infrastructure
  5. #
  6. # This file is dual licensed under the MIT and the University of Illinois Open
  7. # Source Licenses. See LICENSE.TXT for details.
  8. #
  9. #===----------------------------------------------------------------------===##
  10. import os
  11. import sys
  12. def print_and_exit(msg):
  13. sys.stderr.write(msg + '\n')
  14. sys.exit(1)
  15. def usage_and_exit():
  16. print_and_exit("Usage: ./gen_link_script.py [--help] [--dryrun] <path/to/libcxx.so> <public_libs>...")
  17. def help_and_exit():
  18. help_msg = \
  19. """Usage
  20. gen_link_script.py [--help] [--dryrun] <path/to/libcxx.so> <public_libs>...
  21. Generate a linker script that links libc++ to the proper ABI library.
  22. The script replaces the specified libc++ symlink.
  23. An example script for c++abi would look like "INPUT(libc++.so.1 -lc++abi)".
  24. Arguments
  25. <path/to/libcxx.so> - The top level symlink to the versioned libc++ shared
  26. library. This file is replaced with a linker script.
  27. <public_libs> - List of library names to include in linker script.
  28. Exit Status:
  29. 0 if OK,
  30. 1 if the action failed.
  31. """
  32. print_and_exit(help_msg)
  33. def parse_args():
  34. args = list(sys.argv)
  35. del args[0]
  36. if len(args) == 0:
  37. usage_and_exit()
  38. if args[0] == '--help':
  39. help_and_exit()
  40. dryrun = '--dryrun' == args[0]
  41. if dryrun:
  42. del args[0]
  43. if len(args) < 2:
  44. usage_and_exit()
  45. symlink_file = args[0]
  46. public_libs = args[1:]
  47. return dryrun, symlink_file, public_libs
  48. def main():
  49. dryrun, symlink_file, public_libs = parse_args()
  50. # Check that the given libc++.so file is a valid symlink.
  51. if not os.path.islink(symlink_file):
  52. print_and_exit("symlink file %s is not a symlink" % symlink_file)
  53. # Read the symlink so we know what libc++ to link to in the linker script.
  54. linked_libcxx = os.readlink(symlink_file)
  55. # Prepare the list of public libraries to link.
  56. public_libs = ['-l%s' % l for l in public_libs]
  57. # Generate the linker script contents and print the script and destination
  58. # information.
  59. contents = "INPUT(%s %s)" % (linked_libcxx, ' '.join(public_libs))
  60. print("GENERATING SCRIPT: '%s' as file %s" % (contents, symlink_file))
  61. # Remove the existing libc++ symlink and replace it with the script.
  62. if not dryrun:
  63. os.unlink(symlink_file)
  64. with open(symlink_file, 'w') as f:
  65. f.write(contents + "\n")
  66. if __name__ == '__main__':
  67. main()