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 glob | |
7 import os | |
8 import shutil | |
9 import subprocess | |
10 import sys | |
11 import tempfile | |
12 | |
13 BUILD_DIR = "build" | |
cmp
2012/08/01 22:23:53
nit: single quotes
| |
14 | |
15 | |
16 def run_with_vsvars(cmd, tmpdir=None): | |
17 fd, filename = tempfile.mkstemp('.bat', text=True) | |
18 with os.fdopen(fd, "w") as f: | |
cmp
2012/08/01 22:23:53
nit: single quotes
| |
19 print >> f, '@echo off' | |
20 print >> f, r'call "%VS100COMNTOOLS%\vsvars32.bat"' | |
21 if tmpdir: | |
22 print >> f, r'cd %s' % tmpdir | |
23 print >> f, cmd | |
24 try: | |
25 p = subprocess.Popen([filename], shell=True, stdout=subprocess.PIPE, | |
26 universal_newlines=True) | |
27 out, _ = p.communicate() | |
28 return p.returncode, out | |
29 finally: | |
30 os.unlink(filename) | |
31 | |
32 | |
33 def get_vc_dir(): | |
34 _, out = run_with_vsvars('echo VCINSTALLDIR=%VCINSTALLDIR%') | |
35 for line in out.splitlines(): # pylint: disable-msg=E1103 | |
36 if line.startswith('VCINSTALLDIR='): | |
37 return line[len('VCINSTALLDIR='):] | |
38 return None | |
39 | |
40 | |
41 def build(infile): | |
42 if not os.path.exists(BUILD_DIR): | |
43 os.makedirs(BUILD_DIR) | |
44 outfile = 'limiter.exe' | |
45 outpath = os.path.join(BUILD_DIR, outfile) | |
46 cpptime = os.path.getmtime(infile) | |
47 if not os.path.exists(outpath) or cpptime > os.path.getmtime(outpath): | |
nsylvain
2012/08/01 18:53:46
do you really need the optimization for the time?
iannucci
2012/08/01 19:09:53
This was a holdover from the supalink code. I can
| |
48 print 'Building %s...' % outfile | |
49 rc, out = run_with_vsvars( | |
50 'cl /nologo /Ox /Zi /W4 /WX /D_UNICODE /DUNICODE' | |
51 ' /D_CRT_SECURE_NO_WARNINGS /EHsc' | |
52 ' %s /link /out:%s' % (os.path.join("..", infile), outfile), BUILD_DIR) | |
53 if rc: | |
54 print out | |
55 print 'Failed to build %s' % outfile | |
56 sys.exit(1) | |
57 else: | |
58 print "%s already built" % outfile | |
59 return outpath | |
60 | |
61 | |
62 def main(): | |
63 if sys.argv[-1] == 'clean': | |
64 if os.path.exists(BUILD_DIR): | |
nsylvain
2012/08/01 18:53:46
Slightly afraid someone would do : cd src\ && pyt
| |
65 shutil.rmtree(BUILD_DIR) | |
66 for exe in glob.glob('*.exe'): | |
67 os.unlink(exe) | |
68 return 0 | |
69 | |
70 vcdir = os.environ.get('VCINSTALLDIR') | |
71 if not vcdir: | |
72 vcdir = get_vc_dir() | |
73 if not vcdir: | |
74 print "Couldn't get VCINSTALLDIR. Run vsvars32.bat?" | |
75 return 1 | |
76 os.environ['PATH'] += (';' + os.path.join(vcdir, 'bin') + | |
77 ';' + os.path.join(vcdir, r'..\Common7\IDE')) | |
78 | |
79 # Switch to our own dir. | |
80 os.chdir(os.path.dirname(os.path.abspath(__file__))) | |
nsylvain
2012/08/01 18:53:46
or maybe you can just run this before checking for
iannucci
2012/08/01 19:09:53
yeah good call. I'll make the script normalize its
| |
81 | |
82 # Verify that we can find link.exe. | |
83 link = os.path.join(vcdir, 'bin', 'link.exe') | |
84 if not os.path.exists(link): | |
85 print 'link.exe not found at %s' % link | |
86 return 1 | |
87 | |
88 exe_name = build('limiter.cpp') | |
89 for shim_exe in "lib.exe link.exe".split(): | |
nsylvain
2012/08/01 18:53:46
why the .split? why not for shim_exe in('lib.exe',
iannucci
2012/08/01 19:09:53
I tend to lose split b/c less punctuation. I'll tu
iannucci
2012/08/01 19:16:22
Er... 'use' split.
| |
90 newpath = "%s__LIMITER.exe" % shim_exe | |
nsylvain
2012/08/01 18:53:46
single quotes
iannucci
2012/08/01 19:09:53
Why single quotes (curious)? Python makes no disti
cmp
2012/08/01 22:23:53
Nothing in the style guide about single versus dou
| |
91 shutil.copyfile(exe_name, newpath) | |
92 print '%s shim built. Use with msbuild like: "/p:LinkToolExe=%s"' \ | |
93 % (shim_exe, os.path.abspath(newpath)) | |
94 | |
95 return 0 | |
96 | |
97 | |
98 if __name__ == '__main__': | |
99 sys.exit(main()) | |
OLD | NEW |