1 # Copyright (c) 2013 The Chromium Authors. All rights reserved. 2 # Use of this source code is governed by a BSD-style license that can be 3 # found in the LICENSE file. 4 5 import _winreg 6 import os 7 import shutil 8 import subprocess 9 import sys 10 11 12 BASE_DIR = os.path.dirname(os.path.abspath(__file__)) 13 14 15 def IsExe(fpath): 16 return os.path.isfile(fpath) and os.access(fpath, os.X_OK) 17 18 19 def FindInPath(program): 20 fpath, _ = os.path.split(program) 21 if fpath: 22 if IsExe(program): 23 return program 24 else: 25 for path in os.environ['PATH'].split(os.pathsep): 26 path = path.strip('"') 27 exe_file = os.path.join(path, program) 28 if not path or not os.path.isabs(path): 29 continue 30 if IsExe(exe_file): 31 return exe_file 32 return None 33 34 35 def EscapeForCommandLineAndCString(path): 36 """Quoted sufficiently to be passed on the compile command line as a define 37 to be turned into a string in the target C program.""" 38 path = '"' + path + '"' 39 return path.replace('\\', '\\\\').replace('"', '\\"') 40 41 42 def main(): 43 # Switch to our own dir. 44 os.chdir(BASE_DIR) 45 46 link = FindInPath('link.exe') 47 mt = FindInPath('mt.exe') 48 if not link or not mt: 49 print("Couldn't find link.exe or mt.exe in PATH. " 50 "Must run from Administrator Visual Studio Command Prompt.") 51 return 1 52 53 link_backup = os.path.join(os.path.split(link)[0], 'link.exe.split_link.exe') 54 55 # Don't re-backup link.exe, so only copy link.exe to backup if it's 56 # not there already. 57 if not os.path.exists(link_backup): 58 try: 59 print 'Saving original link.exe...' 60 shutil.copyfile(link, link_backup) 61 except IOError: 62 print(("Wasn't able to back up %s to %s. " 63 "Not running with Administrator privileges?") 64 % (link, link_backup)) 65 return 1 66 67 # Build our linker shim. 68 print 'Building split_link.exe...' 69 split_link_py = os.path.abspath('split_link.py') 70 script_path = EscapeForCommandLineAndCString(split_link_py) 71 python = EscapeForCommandLineAndCString(sys.executable) 72 subprocess.check_call('cl.exe /nologo /Ox /Zi /W4 /WX /D_UNICODE /DUNICODE' 73 ' /D_CRT_SECURE_NO_WARNINGS /EHsc split_link.cc' 74 ' /DPYTHON_PATH="%s"' 75 ' /DSPLIT_LINK_SCRIPT_PATH="%s"' 76 ' /link shell32.lib shlwapi.lib /out:split_link.exe' % ( 77 python, script_path)) 78 79 # Copy shim into place. 80 print 'Copying split_link.exe over link.exe...' 81 try: 82 shutil.copyfile('split_link.exe', link) 83 _winreg.SetValue(_winreg.HKEY_CURRENT_USER, 84 'Software\\Chromium\\split_link_installed', 85 _winreg.REG_SZ, 86 link_backup) 87 _winreg.SetValue(_winreg.HKEY_CURRENT_USER, 88 'Software\\Chromium\\split_link_mt_path', 89 _winreg.REG_SZ, 90 mt) 91 except IOError: 92 print("Wasn't able to copy split_link.exe over %s. " 93 "Not running with Administrator privileges?" % link) 94 return 1 95 96 return 0 97 98 99 if __name__ == '__main__': 100 sys.exit(main()) 101