Advertisement
Guest User

Untitled

a guest
Mar 29th, 2020
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. #!/usr/bin/env python2
  2. # Copyright 2016 The Emscripten Authors. All rights reserved.
  3. # Emscripten is available under two separate licenses, the MIT license and the
  4. # University of Illinois/NCSA Open Source License. Both these licenses can be
  5. # found in the LICENSE file.
  6.  
  7. """Archive helper script
  8.  
  9. This script acts as a frontend replacement for `ar`. See emcc.
  10. This is needed because, unlike a traditional linker, emscripten can't handle
  11. archive with duplicate member names. This is because emscripten extracts
  12. archive to a temporary location and duplicate filenames will clobber each
  13. other in this case.
  14. """
  15.  
  16. # TODO(sbc): Implement `ar x` within emscripten, in python, to avoid this issue
  17. # and delete this file.
  18.  
  19. from __future__ import print_function
  20. import hashlib
  21. import os
  22. import shutil
  23. import sys
  24. import tempfile
  25.  
  26. from tools.toolchain_profiler import ToolchainProfiler
  27. from tools import shared
  28. from tools.response_file import substitute_response_files, create_response_file
  29.  
  30. if __name__ == '__main__':
  31. ToolchainProfiler.record_process_start()
  32.  
  33.  
  34. #
  35. # Main run() function
  36. #
  37. def run():
  38. if shared.Settings.WASM_BACKEND:
  39. # The wasm backend does suffer from the same probllem as fastcomp so doesn't
  40. # need the filename hashing.
  41. cmd = [shared.LLVM_AR] + sys.argv[1:]
  42. return shared.run_process(cmd, stdin=sys.stdin, check=False).returncode
  43.  
  44. try:
  45. args = substitute_response_files(sys.argv)
  46. except IOError as e:
  47. shared.exit_with_error(e)
  48. newargs = [shared.LLVM_AR] + args[1:]
  49.  
  50. tmpdir = None
  51. response_filename = None
  52.  
  53. # The 3 argmuent form of ar doesn't involve other files. For example
  54. # 'ar x libfoo.a'.
  55. if len(newargs) > 3:
  56. tmpdir = tempfile.mkdtemp(prefix='emar-')
  57. cmd = newargs[1]
  58. if 'r' in cmd or 'q' in cmd:
  59. # We are adding files to the archive.
  60. # Normally the output file is then arg 2, except in the case were the
  61. # a or b modifiers are used in which case its arg 3.
  62. if 'a' in cmd or 'b' in cmd:
  63. out_arg_index = 3
  64. else:
  65. out_arg_index = 2
  66.  
  67. # Add a hash to colliding basename, to make them unique.
  68. for j in range(out_arg_index + 1, len(newargs)):
  69. orig_name = newargs[j]
  70. full_name = os.path.abspath(orig_name)
  71. basename = os.path.basename(full_name)
  72.  
  73. h = hashlib.md5(full_name.encode('utf-8')).hexdigest()[:8]
  74. parts = basename.split('.')
  75. parts[0] += '_' + h
  76. newname = '.'.join(parts)
  77. full_newname = os.path.join(tmpdir, newname)
  78. shutil.copyfile(orig_name, full_newname)
  79. newargs[j] = full_newname
  80.  
  81. if shared.DEBUG:
  82. print('emar:', sys.argv, ' ==> ', newargs, file=sys.stderr)
  83. response_filename = create_response_file(newargs[3:], shared.get_emscripten_temp_dir())
  84. newargs = newargs[:3] + ['@' + response_filename]
  85.  
  86. if shared.DEBUG:
  87. print('emar:', sys.argv, ' ==> ', newargs, file=sys.stderr)
  88.  
  89. rtn = shared.run_process(newargs, stdin=sys.stdin, check=False).returncode
  90. if tmpdir:
  91. shutil.rmtree(tmpdir)
  92. shared.try_delete(response_filename)
  93. return rtn
  94.  
  95.  
  96. if __name__ == '__main__':
  97. sys.exit(run())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement