OLD | NEW |
| (Empty) |
1 #!/usr/bin/env python | |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
3 # Use of this source code is governed by a BSD-style license that can be | |
4 # found in the LICENSE file. | |
5 | |
6 import os | |
7 import shutil | |
8 import subprocess | |
9 import sys | |
10 import tempfile | |
11 | |
12 if sys.platform == 'win32': | |
13 import _winreg | |
14 elif sys.platform == 'cygwin': | |
15 try: | |
16 import cygwinreg as _winreg | |
17 except ImportError: | |
18 print "Please install cygwinreg so I can access the registry." | |
19 print "Install setuptools and run 'easy_install cygwinreg'." | |
20 | |
21 | |
22 VSVARS_PATH = ('C:\\Program Files (x86)\\Microsoft Visual Studio 9.0\\' | |
23 'Common7\\Tools\\vsvars32.bat') | |
24 | |
25 | |
26 def run_with_vsvars(cmd): | |
27 (fd, filename) = tempfile.mkstemp('.bat', text=True) | |
28 f = os.fdopen(fd, "w") | |
29 f.write('@echo off\n') | |
30 f.write('call "%s"\n' % VSVARS_PATH) | |
31 f.write(cmd + '\n') | |
32 f.close() | |
33 try: | |
34 p = subprocess.Popen([filename], shell=True, stdout=subprocess.PIPE, | |
35 universal_newlines=True) | |
36 out, err = p.communicate() | |
37 return p.returncode, out | |
38 finally: | |
39 os.unlink(filename) | |
40 | |
41 | |
42 def get_vc_dir(): | |
43 rc, out = run_with_vsvars('echo VCINSTALLDIR=%VCINSTALLDIR%') | |
44 for line in out.splitlines(): | |
45 if line.startswith('VCINSTALLDIR='): | |
46 return line[len('VCINSTALLDIR='):] | |
47 return None | |
48 | |
49 | |
50 def main(): | |
51 vcdir = os.environ.get('VCINSTALLDIR') | |
52 if not vcdir: | |
53 vcdir = get_vc_dir() | |
54 if not vcdir: | |
55 print 'Couldn\'t get VCINSTALLDIR. Run vsvars32.bat?' | |
56 return 1 | |
57 os.environ['PATH'] += (';' + os.path.join(vcdir, 'bin') + | |
58 ';' + os.path.join(vcdir, '..\\Common7\\IDE')) | |
59 | |
60 # Switch to our own dir. | |
61 os.chdir(os.path.dirname(os.path.abspath(__file__))) | |
62 | |
63 # Verify that we can find link.exe. | |
64 link = os.path.join(vcdir, 'bin', 'link.exe') | |
65 link_backup = os.path.join(vcdir, 'bin', 'link.exe.supalink_orig.exe') | |
66 if not os.path.exists(link): | |
67 print 'link.exe not found at %s' % link | |
68 return 1 | |
69 | |
70 # Don't re-backup link.exe, so only copy link.exe to backup if it's | |
71 # not there already. | |
72 if not os.path.exists(link_backup): | |
73 try: | |
74 print 'Saving original link.exe...' | |
75 shutil.copyfile(link, link_backup) | |
76 except IOError: | |
77 print (('Wasn\'t able to back up %s to %s. ' | |
78 'Not running with Administrator privileges?') | |
79 % (link, link_backup)) | |
80 return 1 | |
81 | |
82 # Build supalink.exe but only if it's out of date. | |
83 cpptime = os.path.getmtime('supalink.cpp') | |
84 if (not os.path.exists('supalink.exe') or | |
85 cpptime > os.path.getmtime('supalink.exe')): | |
86 print 'Building supalink.exe...' | |
87 rc, out = run_with_vsvars('cl /nologo /Ox /Zi /W4 /WX /D_UNICODE /DUNICODE' | |
88 ' /D_CRT_SECURE_NO_WARNINGS /EHsc supalink.cpp' | |
89 ' /link /out:supalink.exe') | |
90 if rc: | |
91 print out | |
92 print 'Failed to build supalink.exe' | |
93 return 1 | |
94 | |
95 # Copy supalink into place if it's been updated since last time we ran. | |
96 exetime = os.path.getmtime('supalink.exe') | |
97 if exetime > os.path.getmtime(link): | |
98 print 'Copying supalink.exe over link.exe...' | |
99 try: | |
100 shutil.copyfile('supalink.exe', link) | |
101 except IOError: | |
102 print ('Wasn\'t able to copy supalink.exe over %s. ' | |
103 'Not running with Administrator privileges?' % link) | |
104 return 1 | |
105 | |
106 _winreg.SetValue(_winreg.HKEY_CURRENT_USER, | |
107 'Software\\Chromium\\supalink_installed', | |
108 _winreg.REG_SZ, | |
109 link_backup) | |
110 | |
111 print 'Linker shim installed. Regenerate via gyp: "gclient runhooks".' | |
112 return 0 | |
113 | |
114 | |
115 if __name__ == '__main__': | |
116 sys.exit(main()) | |
OLD | NEW |