Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(474)

Unified Diff: tools/win/link_limiter/build_link_limiter.py

Issue 10826067: Implement a tool called link_limiter to impose a global restriction on how many (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Refactor to take shimmed exe and pipe name from argv[0] (sneaky, hunh?) Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: tools/win/link_limiter/build_link_limiter.py
diff --git a/tools/win/link_limiter/build_link_limiter.py b/tools/win/link_limiter/build_link_limiter.py
new file mode 100644
index 0000000000000000000000000000000000000000..7a9bd4f40d03ea98db54fbd7c119083f9dd683c5
--- /dev/null
+++ b/tools/win/link_limiter/build_link_limiter.py
@@ -0,0 +1,99 @@
+#!/usr/bin/env python
+# Copyright (c) 2012 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+import glob
+import os
+import shutil
+import subprocess
+import sys
+import tempfile
+
+BUILD_DIR = "build"
cmp 2012/08/01 22:23:53 nit: single quotes
+
+
+def run_with_vsvars(cmd, tmpdir=None):
+ fd, filename = tempfile.mkstemp('.bat', text=True)
+ with os.fdopen(fd, "w") as f:
cmp 2012/08/01 22:23:53 nit: single quotes
+ print >> f, '@echo off'
+ print >> f, r'call "%VS100COMNTOOLS%\vsvars32.bat"'
+ if tmpdir:
+ print >> f, r'cd %s' % tmpdir
+ print >> f, cmd
+ try:
+ p = subprocess.Popen([filename], shell=True, stdout=subprocess.PIPE,
+ universal_newlines=True)
+ out, _ = p.communicate()
+ return p.returncode, out
+ finally:
+ os.unlink(filename)
+
+
+def get_vc_dir():
+ _, out = run_with_vsvars('echo VCINSTALLDIR=%VCINSTALLDIR%')
+ for line in out.splitlines(): # pylint: disable-msg=E1103
+ if line.startswith('VCINSTALLDIR='):
+ return line[len('VCINSTALLDIR='):]
+ return None
+
+
+def build(infile):
+ if not os.path.exists(BUILD_DIR):
+ os.makedirs(BUILD_DIR)
+ outfile = 'limiter.exe'
+ outpath = os.path.join(BUILD_DIR, outfile)
+ cpptime = os.path.getmtime(infile)
+ 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
+ print 'Building %s...' % outfile
+ rc, out = run_with_vsvars(
+ 'cl /nologo /Ox /Zi /W4 /WX /D_UNICODE /DUNICODE'
+ ' /D_CRT_SECURE_NO_WARNINGS /EHsc'
+ ' %s /link /out:%s' % (os.path.join("..", infile), outfile), BUILD_DIR)
+ if rc:
+ print out
+ print 'Failed to build %s' % outfile
+ sys.exit(1)
+ else:
+ print "%s already built" % outfile
+ return outpath
+
+
+def main():
+ if sys.argv[-1] == 'clean':
+ if os.path.exists(BUILD_DIR):
nsylvain 2012/08/01 18:53:46 Slightly afraid someone would do : cd src\ && pyt
+ shutil.rmtree(BUILD_DIR)
+ for exe in glob.glob('*.exe'):
+ os.unlink(exe)
+ return 0
+
+ vcdir = os.environ.get('VCINSTALLDIR')
+ if not vcdir:
+ vcdir = get_vc_dir()
+ if not vcdir:
+ print "Couldn't get VCINSTALLDIR. Run vsvars32.bat?"
+ return 1
+ os.environ['PATH'] += (';' + os.path.join(vcdir, 'bin') +
+ ';' + os.path.join(vcdir, r'..\Common7\IDE'))
+
+ # Switch to our own dir.
+ 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
+
+ # Verify that we can find link.exe.
+ link = os.path.join(vcdir, 'bin', 'link.exe')
+ if not os.path.exists(link):
+ print 'link.exe not found at %s' % link
+ return 1
+
+ exe_name = build('limiter.cpp')
+ 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.
+ 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
+ shutil.copyfile(exe_name, newpath)
+ print '%s shim built. Use with msbuild like: "/p:LinkToolExe=%s"' \
+ % (shim_exe, os.path.abspath(newpath))
+
+ return 0
+
+
+if __name__ == '__main__':
+ sys.exit(main())

Powered by Google App Engine
This is Rietveld 408576698