| OLD | NEW |
| (Empty) |
| 1 # Copyright 2014 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 | |
| 6 def _FixDistutilsMsvcCompiler(): | |
| 7 # To avoid runtime mismatch, distutils should use the compiler which was used | |
| 8 # to build python. But our module does not use the runtime much, so it should | |
| 9 # be fine to build within a different environment. | |
| 10 # See also: http://bugs.python.org/issue7511 | |
| 11 from distutils import msvc9compiler | |
| 12 for version in [msvc9compiler.get_build_version(), 9.0, 10.0, 11.0, 12.0]: | |
| 13 msvc9compiler.VERSION = version | |
| 14 try: | |
| 15 msvc9compiler.MSVCCompiler().initialize() | |
| 16 return | |
| 17 except Exception: | |
| 18 pass | |
| 19 raise Exception('Could not initialize MSVC compiler for distutils.') | |
| 20 | |
| 21 | |
| 22 def BuildExtension(sources, output_dir, extension_name): | |
| 23 from distutils import log | |
| 24 from distutils.core import Distribution, Extension | |
| 25 import os | |
| 26 import tempfile | |
| 27 | |
| 28 if os.name == 'nt': | |
| 29 _FixDistutilsMsvcCompiler() | |
| 30 | |
| 31 build_dir = tempfile.mkdtemp() | |
| 32 # Source file paths must be relative to current path. | |
| 33 cwd = os.getcwd() | |
| 34 src_files = [os.path.relpath(filename, cwd) for filename in sources] | |
| 35 | |
| 36 dist = Distribution({ | |
| 37 'ext_modules': [Extension(extension_name, src_files)] | |
| 38 }) | |
| 39 dist.script_args = ['build_ext', '--build-temp', build_dir, | |
| 40 '--build-lib', output_dir] | |
| 41 dist.parse_command_line() | |
| 42 log.set_threshold(log.DEBUG) | |
| 43 dist.run_commands() | |
| 44 dist.script_args = ['clean', '--build-temp', build_dir, '--all'] | |
| 45 dist.parse_command_line() | |
| 46 log.set_threshold(log.DEBUG) | |
| 47 dist.run_commands() | |
| 48 | |
| 49 | |
| 50 if __name__ == '__main__': | |
| 51 import sys | |
| 52 assert len(sys.argv) > 3, 'Usage: build.py source-files output-dir ext-name' | |
| 53 BuildExtension(sys.argv[1:-2], sys.argv[-2], sys.argv[-1]) | |
| OLD | NEW |