Bumping manifests a=b2g-bump
[gecko.git] / build / unix / rewrite_asan_dylib.py
blob869c46e29573e133c422479da74787d0eff0ac8e
1 # This Source Code Form is subject to the terms of the Mozilla Public
2 # License, v. 2.0. If a copy of the MPL was not distributed with this
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 import sys
6 import os
7 import subprocess
8 import shutil
10 '''
11 Scans the given directories for binaries referencing the AddressSanitizer
12 runtime library, copies it to the main directory and rewrites binaries to not
13 reference it with absolute paths but with @executable_path instead.
14 '''
16 # This is the dylib we're looking for
17 DYLIB_NAME='libclang_rt.asan_osx_dynamic.dylib'
19 def scan_directory(path):
20 dylibCopied = False
22 for root, subdirs, files in os.walk(path):
23 for filename in files:
24 filename = os.path.join(root, filename)
26 # Skip all files that aren't either dylibs or executable
27 if not (filename.endswith('.dylib') or os.access(filename, os.X_OK)):
28 continue
30 try:
31 otoolOut = subprocess.check_output(['otool', '-L', filename])
32 except:
33 # Errors are expected on non-mach executables, ignore them and continue
34 continue
36 for line in otoolOut.splitlines():
37 if line.find(DYLIB_NAME) != -1:
38 absDylibPath = line.split()[0]
40 # Don't try to rewrite binaries twice
41 if absDylibPath.find('@executable_path/') == 0:
42 continue
44 if not dylibCopied:
45 # Copy the runtime once to the main directory, which is passed
46 # as the argument to this function.
47 shutil.copy(absDylibPath, path)
49 # Now rewrite the library itself
50 subprocess.check_call(['install_name_tool', '-id', '@executable_path/' + DYLIB_NAME, os.path.join(path, DYLIB_NAME)])
51 dylibCopied = True
53 # Now use install_name_tool to rewrite the path in our binary
54 subprocess.check_call(['install_name_tool', '-change', absDylibPath, '@executable_path/' + DYLIB_NAME, filename])
55 break
57 if __name__ == '__main__':
58 for d in sys.argv[1:]:
59 scan_directory(d)