msvccompiler.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. from __future__ import division, absolute_import, print_function
  2. import os
  3. from distutils.msvccompiler import MSVCCompiler as _MSVCCompiler
  4. from .system_info import platform_bits
  5. def _merge(old, new):
  6. """Concatenate two environment paths avoiding repeats.
  7. Here `old` is the environment string before the base class initialize
  8. function is called and `new` is the string after the call. The new string
  9. will be a fixed string if it is not obtained from the current environment,
  10. or the same as the old string if obtained from the same environment. The aim
  11. here is not to append the new string if it is already contained in the old
  12. string so as to limit the growth of the environment string.
  13. Parameters
  14. ----------
  15. old : string
  16. Previous environment string.
  17. new : string
  18. New environment string.
  19. Returns
  20. -------
  21. ret : string
  22. Updated environment string.
  23. """
  24. if new in old:
  25. return old
  26. if not old:
  27. return new
  28. # Neither new nor old is empty. Give old priority.
  29. return ';'.join([old, new])
  30. class MSVCCompiler(_MSVCCompiler):
  31. def __init__(self, verbose=0, dry_run=0, force=0):
  32. _MSVCCompiler.__init__(self, verbose, dry_run, force)
  33. def initialize(self):
  34. # The 'lib' and 'include' variables may be overwritten
  35. # by MSVCCompiler.initialize, so save them for later merge.
  36. environ_lib = os.getenv('lib', '')
  37. environ_include = os.getenv('include', '')
  38. _MSVCCompiler.initialize(self)
  39. # Merge current and previous values of 'lib' and 'include'
  40. os.environ['lib'] = _merge(environ_lib, os.environ['lib'])
  41. os.environ['include'] = _merge(environ_include, os.environ['include'])
  42. # msvc9 building for 32 bits requires SSE2 to work around a
  43. # compiler bug.
  44. if platform_bits == 32:
  45. self.compile_options += ['/arch:SSE2']
  46. self.compile_options_debug += ['/arch:SSE2']