gen_link_script.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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> <abi_libname>")
  17. def help_and_exit():
  18. help_msg = \
  19. """Usage
  20. gen_link_script.py [--help] [--dryrun] <path/to/libcxx.so> <abi_libname>
  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. <abi_libname> - The name of the ABI library to use in the linker script.
  28. The name must be one of [c++abi, stdc++, supc++, cxxrt].
  29. Exit Status:
  30. 0 if OK,
  31. 1 if the action failed.
  32. """
  33. print_and_exit(help_msg)
  34. def parse_args():
  35. args = list(sys.argv)
  36. del args[0]
  37. if len(args) == 0:
  38. usage_and_exit()
  39. if args[0] == '--help':
  40. help_and_exit()
  41. dryrun = '--dryrun' == args[0]
  42. if dryrun:
  43. del args[0]
  44. if len(args) != 2:
  45. usage_and_exit()
  46. symlink_file = args[0]
  47. abi_libname = args[1]
  48. return dryrun, symlink_file, abi_libname
  49. def main():
  50. dryrun, symlink_file, abi_libname = parse_args()
  51. # Check that the given libc++.so file is a valid symlink.
  52. if not os.path.islink(symlink_file):
  53. print_and_exit("symlink file %s is not a symlink" % symlink_file)
  54. # Read the symlink so we know what libc++ to link to in the linker script.
  55. linked_libcxx = os.readlink(symlink_file)
  56. # Check that the abi_libname is one of the supported values.
  57. supported_abi_list = ['c++abi', 'stdc++', 'supc++', 'cxxrt']
  58. if abi_libname not in supported_abi_list:
  59. print_and_exit("abi name '%s' is not supported: Use one of %r" %
  60. (abi_libname, supported_abi_list))
  61. # Generate the linker script contents and print the script and destination
  62. # information.
  63. contents = "INPUT(%s -l%s)" % (linked_libcxx, abi_libname)
  64. print("GENERATING SCRIPT: '%s' as file %s" % (contents, symlink_file))
  65. # Remove the existing libc++ symlink and replace it with the script.
  66. if not dryrun:
  67. os.unlink(symlink_file)
  68. with open(symlink_file, 'w') as f:
  69. f.write(contents + "\n")
  70. if __name__ == '__main__':
  71. main()