msvccompiler.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. """distutils.msvccompiler
  2. Contains MSVCCompiler, an implementation of the abstract CCompiler class
  3. for the Microsoft Visual Studio.
  4. """
  5. # Written by Perry Stoll
  6. # hacked by Robin Becker and Thomas Heller to do a better job of
  7. # finding DevStudio (through the registry)
  8. import sys, os
  9. from distutils.errors import \
  10. DistutilsExecError, DistutilsPlatformError, \
  11. CompileError, LibError, LinkError
  12. from distutils.ccompiler import \
  13. CCompiler, gen_lib_options
  14. from distutils import log
  15. _can_read_reg = False
  16. try:
  17. import winreg
  18. _can_read_reg = True
  19. hkey_mod = winreg
  20. RegOpenKeyEx = winreg.OpenKeyEx
  21. RegEnumKey = winreg.EnumKey
  22. RegEnumValue = winreg.EnumValue
  23. RegError = winreg.error
  24. except ImportError:
  25. try:
  26. import win32api
  27. import win32con
  28. _can_read_reg = True
  29. hkey_mod = win32con
  30. RegOpenKeyEx = win32api.RegOpenKeyEx
  31. RegEnumKey = win32api.RegEnumKey
  32. RegEnumValue = win32api.RegEnumValue
  33. RegError = win32api.error
  34. except ImportError:
  35. log.info("Warning: Can't read registry to find the "
  36. "necessary compiler setting\n"
  37. "Make sure that Python modules winreg, "
  38. "win32api or win32con are installed.")
  39. if _can_read_reg:
  40. HKEYS = (hkey_mod.HKEY_USERS,
  41. hkey_mod.HKEY_CURRENT_USER,
  42. hkey_mod.HKEY_LOCAL_MACHINE,
  43. hkey_mod.HKEY_CLASSES_ROOT)
  44. def read_keys(base, key):
  45. """Return list of registry keys."""
  46. try:
  47. handle = RegOpenKeyEx(base, key)
  48. except RegError:
  49. return None
  50. L = []
  51. i = 0
  52. while True:
  53. try:
  54. k = RegEnumKey(handle, i)
  55. except RegError:
  56. break
  57. L.append(k)
  58. i += 1
  59. return L
  60. def read_values(base, key):
  61. """Return dict of registry keys and values.
  62. All names are converted to lowercase.
  63. """
  64. try:
  65. handle = RegOpenKeyEx(base, key)
  66. except RegError:
  67. return None
  68. d = {}
  69. i = 0
  70. while True:
  71. try:
  72. name, value, type = RegEnumValue(handle, i)
  73. except RegError:
  74. break
  75. name = name.lower()
  76. d[convert_mbcs(name)] = convert_mbcs(value)
  77. i += 1
  78. return d
  79. def convert_mbcs(s):
  80. dec = getattr(s, "decode", None)
  81. if dec is not None:
  82. try:
  83. s = dec("mbcs")
  84. except UnicodeError:
  85. pass
  86. return s
  87. class MacroExpander:
  88. def __init__(self, version):
  89. self.macros = {}
  90. self.load_macros(version)
  91. def set_macro(self, macro, path, key):
  92. for base in HKEYS:
  93. d = read_values(base, path)
  94. if d:
  95. self.macros["$(%s)" % macro] = d[key]
  96. break
  97. def load_macros(self, version):
  98. vsbase = r"Software\Microsoft\VisualStudio\%0.1f" % version
  99. self.set_macro("VCInstallDir", vsbase + r"\Setup\VC", "productdir")
  100. self.set_macro("VSInstallDir", vsbase + r"\Setup\VS", "productdir")
  101. net = r"Software\Microsoft\.NETFramework"
  102. self.set_macro("FrameworkDir", net, "installroot")
  103. try:
  104. if version > 7.0:
  105. self.set_macro("FrameworkSDKDir", net, "sdkinstallrootv1.1")
  106. else:
  107. self.set_macro("FrameworkSDKDir", net, "sdkinstallroot")
  108. except KeyError as exc: #
  109. raise DistutilsPlatformError(
  110. """Python was built with Visual Studio 2003;
  111. extensions must be built with a compiler than can generate compatible binaries.
  112. Visual Studio 2003 was not found on this system. If you have Cygwin installed,
  113. you can try compiling with MingW32, by passing "-c mingw32" to setup.py.""")
  114. p = r"Software\Microsoft\NET Framework Setup\Product"
  115. for base in HKEYS:
  116. try:
  117. h = RegOpenKeyEx(base, p)
  118. except RegError:
  119. continue
  120. key = RegEnumKey(h, 0)
  121. d = read_values(base, r"%s\%s" % (p, key))
  122. self.macros["$(FrameworkVersion)"] = d["version"]
  123. def sub(self, s):
  124. for k, v in self.macros.items():
  125. s = s.replace(k, v)
  126. return s
  127. def get_build_version():
  128. """Return the version of MSVC that was used to build Python.
  129. For Python 2.3 and up, the version number is included in
  130. sys.version. For earlier versions, assume the compiler is MSVC 6.
  131. """
  132. prefix = "MSC v."
  133. i = sys.version.find(prefix)
  134. if i == -1:
  135. return 6
  136. i = i + len(prefix)
  137. s, rest = sys.version[i:].split(" ", 1)
  138. majorVersion = int(s[:-2]) - 6
  139. if majorVersion >= 13:
  140. # v13 was skipped and should be v14
  141. majorVersion += 1
  142. minorVersion = int(s[2:3]) / 10.0
  143. # I don't think paths are affected by minor version in version 6
  144. if majorVersion == 6:
  145. minorVersion = 0
  146. if majorVersion >= 6:
  147. return majorVersion + minorVersion
  148. # else we don't know what version of the compiler this is
  149. return None
  150. def get_build_architecture():
  151. """Return the processor architecture.
  152. Possible results are "Intel" or "AMD64".
  153. """
  154. prefix = " bit ("
  155. i = sys.version.find(prefix)
  156. if i == -1:
  157. return "Intel"
  158. j = sys.version.find(")", i)
  159. return sys.version[i+len(prefix):j]
  160. def normalize_and_reduce_paths(paths):
  161. """Return a list of normalized paths with duplicates removed.
  162. The current order of paths is maintained.
  163. """
  164. # Paths are normalized so things like: /a and /a/ aren't both preserved.
  165. reduced_paths = []
  166. for p in paths:
  167. np = os.path.normpath(p)
  168. # XXX(nnorwitz): O(n**2), if reduced_paths gets long perhaps use a set.
  169. if np not in reduced_paths:
  170. reduced_paths.append(np)
  171. return reduced_paths
  172. class MSVCCompiler(CCompiler) :
  173. """Concrete class that implements an interface to Microsoft Visual C++,
  174. as defined by the CCompiler abstract class."""
  175. compiler_type = 'msvc'
  176. # Just set this so CCompiler's constructor doesn't barf. We currently
  177. # don't use the 'set_executables()' bureaucracy provided by CCompiler,
  178. # as it really isn't necessary for this sort of single-compiler class.
  179. # Would be nice to have a consistent interface with UnixCCompiler,
  180. # though, so it's worth thinking about.
  181. executables = {}
  182. # Private class data (need to distinguish C from C++ source for compiler)
  183. _c_extensions = ['.c']
  184. _cpp_extensions = ['.cc', '.cpp', '.cxx']
  185. _rc_extensions = ['.rc']
  186. _mc_extensions = ['.mc']
  187. # Needed for the filename generation methods provided by the
  188. # base class, CCompiler.
  189. src_extensions = (_c_extensions + _cpp_extensions +
  190. _rc_extensions + _mc_extensions)
  191. res_extension = '.res'
  192. obj_extension = '.obj'
  193. static_lib_extension = '.lib'
  194. shared_lib_extension = '.dll'
  195. static_lib_format = shared_lib_format = '%s%s'
  196. exe_extension = '.exe'
  197. def __init__(self, verbose=0, dry_run=0, force=0):
  198. CCompiler.__init__ (self, verbose, dry_run, force)
  199. self.__version = get_build_version()
  200. self.__arch = get_build_architecture()
  201. if self.__arch == "Intel":
  202. # x86
  203. if self.__version >= 7:
  204. self.__root = r"Software\Microsoft\VisualStudio"
  205. self.__macros = MacroExpander(self.__version)
  206. else:
  207. self.__root = r"Software\Microsoft\Devstudio"
  208. self.__product = "Visual Studio version %s" % self.__version
  209. else:
  210. # Win64. Assume this was built with the platform SDK
  211. self.__product = "Microsoft SDK compiler %s" % (self.__version + 6)
  212. self.initialized = False
  213. def initialize(self):
  214. self.__paths = []
  215. if "DISTUTILS_USE_SDK" in os.environ and "MSSdk" in os.environ and self.find_exe("cl.exe"):
  216. # Assume that the SDK set up everything alright; don't try to be
  217. # smarter
  218. self.cc = "cl.exe"
  219. self.linker = "link.exe"
  220. self.lib = "lib.exe"
  221. self.rc = "rc.exe"
  222. self.mc = "mc.exe"
  223. else:
  224. self.__paths = self.get_msvc_paths("path")
  225. if len(self.__paths) == 0:
  226. raise DistutilsPlatformError("Python was built with %s, "
  227. "and extensions need to be built with the same "
  228. "version of the compiler, but it isn't installed."
  229. % self.__product)
  230. self.cc = self.find_exe("cl.exe")
  231. self.linker = self.find_exe("link.exe")
  232. self.lib = self.find_exe("lib.exe")
  233. self.rc = self.find_exe("rc.exe") # resource compiler
  234. self.mc = self.find_exe("mc.exe") # message compiler
  235. self.set_path_env_var('lib')
  236. self.set_path_env_var('include')
  237. # extend the MSVC path with the current path
  238. try:
  239. for p in os.environ['path'].split(';'):
  240. self.__paths.append(p)
  241. except KeyError:
  242. pass
  243. self.__paths = normalize_and_reduce_paths(self.__paths)
  244. os.environ['path'] = ";".join(self.__paths)
  245. self.preprocess_options = None
  246. if self.__arch == "Intel":
  247. self.compile_options = [ '/nologo', '/Ox', '/MD', '/W3', '/GX' ,
  248. '/DNDEBUG']
  249. self.compile_options_debug = ['/nologo', '/Od', '/MDd', '/W3', '/GX',
  250. '/Z7', '/D_DEBUG']
  251. else:
  252. # Win64
  253. self.compile_options = [ '/nologo', '/Ox', '/MD', '/W3', '/GS-' ,
  254. '/DNDEBUG']
  255. self.compile_options_debug = ['/nologo', '/Od', '/MDd', '/W3', '/GS-',
  256. '/Z7', '/D_DEBUG']
  257. self.ldflags_shared = ['/DLL', '/nologo', '/INCREMENTAL:NO']
  258. if self.__version >= 7:
  259. self.ldflags_shared_debug = [
  260. '/DLL', '/nologo', '/INCREMENTAL:no', '/DEBUG'
  261. ]
  262. else:
  263. self.ldflags_shared_debug = [
  264. '/DLL', '/nologo', '/INCREMENTAL:no', '/pdb:None', '/DEBUG'
  265. ]
  266. self.ldflags_static = [ '/nologo']
  267. self.initialized = True
  268. # -- Worker methods ------------------------------------------------
  269. def object_filenames(self,
  270. source_filenames,
  271. strip_dir=0,
  272. output_dir=''):
  273. # Copied from ccompiler.py, extended to return .res as 'object'-file
  274. # for .rc input file
  275. if output_dir is None: output_dir = ''
  276. obj_names = []
  277. for src_name in source_filenames:
  278. (base, ext) = os.path.splitext (src_name)
  279. base = os.path.splitdrive(base)[1] # Chop off the drive
  280. base = base[os.path.isabs(base):] # If abs, chop off leading /
  281. if ext not in self.src_extensions:
  282. # Better to raise an exception instead of silently continuing
  283. # and later complain about sources and targets having
  284. # different lengths
  285. raise CompileError ("Don't know how to compile %s" % src_name)
  286. if strip_dir:
  287. base = os.path.basename (base)
  288. if ext in self._rc_extensions:
  289. obj_names.append (os.path.join (output_dir,
  290. base + self.res_extension))
  291. elif ext in self._mc_extensions:
  292. obj_names.append (os.path.join (output_dir,
  293. base + self.res_extension))
  294. else:
  295. obj_names.append (os.path.join (output_dir,
  296. base + self.obj_extension))
  297. return obj_names
  298. def compile(self, sources,
  299. output_dir=None, macros=None, include_dirs=None, debug=0,
  300. extra_preargs=None, extra_postargs=None, depends=None):
  301. if not self.initialized:
  302. self.initialize()
  303. compile_info = self._setup_compile(output_dir, macros, include_dirs,
  304. sources, depends, extra_postargs)
  305. macros, objects, extra_postargs, pp_opts, build = compile_info
  306. compile_opts = extra_preargs or []
  307. compile_opts.append ('/c')
  308. if debug:
  309. compile_opts.extend(self.compile_options_debug)
  310. else:
  311. compile_opts.extend(self.compile_options)
  312. for obj in objects:
  313. try:
  314. src, ext = build[obj]
  315. except KeyError:
  316. continue
  317. if debug:
  318. # pass the full pathname to MSVC in debug mode,
  319. # this allows the debugger to find the source file
  320. # without asking the user to browse for it
  321. src = os.path.abspath(src)
  322. if ext in self._c_extensions:
  323. input_opt = "/Tc" + src
  324. elif ext in self._cpp_extensions:
  325. input_opt = "/Tp" + src
  326. elif ext in self._rc_extensions:
  327. # compile .RC to .RES file
  328. input_opt = src
  329. output_opt = "/fo" + obj
  330. try:
  331. self.spawn([self.rc] + pp_opts +
  332. [output_opt] + [input_opt])
  333. except DistutilsExecError as msg:
  334. raise CompileError(msg)
  335. continue
  336. elif ext in self._mc_extensions:
  337. # Compile .MC to .RC file to .RES file.
  338. # * '-h dir' specifies the directory for the
  339. # generated include file
  340. # * '-r dir' specifies the target directory of the
  341. # generated RC file and the binary message resource
  342. # it includes
  343. #
  344. # For now (since there are no options to change this),
  345. # we use the source-directory for the include file and
  346. # the build directory for the RC file and message
  347. # resources. This works at least for win32all.
  348. h_dir = os.path.dirname(src)
  349. rc_dir = os.path.dirname(obj)
  350. try:
  351. # first compile .MC to .RC and .H file
  352. self.spawn([self.mc] +
  353. ['-h', h_dir, '-r', rc_dir] + [src])
  354. base, _ = os.path.splitext (os.path.basename (src))
  355. rc_file = os.path.join (rc_dir, base + '.rc')
  356. # then compile .RC to .RES file
  357. self.spawn([self.rc] +
  358. ["/fo" + obj] + [rc_file])
  359. except DistutilsExecError as msg:
  360. raise CompileError(msg)
  361. continue
  362. else:
  363. # how to handle this file?
  364. raise CompileError("Don't know how to compile %s to %s"
  365. % (src, obj))
  366. output_opt = "/Fo" + obj
  367. try:
  368. self.spawn([self.cc] + compile_opts + pp_opts +
  369. [input_opt, output_opt] +
  370. extra_postargs)
  371. except DistutilsExecError as msg:
  372. raise CompileError(msg)
  373. return objects
  374. def create_static_lib(self,
  375. objects,
  376. output_libname,
  377. output_dir=None,
  378. debug=0,
  379. target_lang=None):
  380. if not self.initialized:
  381. self.initialize()
  382. (objects, output_dir) = self._fix_object_args(objects, output_dir)
  383. output_filename = self.library_filename(output_libname,
  384. output_dir=output_dir)
  385. if self._need_link(objects, output_filename):
  386. lib_args = objects + ['/OUT:' + output_filename]
  387. if debug:
  388. pass # XXX what goes here?
  389. try:
  390. self.spawn([self.lib] + lib_args)
  391. except DistutilsExecError as msg:
  392. raise LibError(msg)
  393. else:
  394. log.debug("skipping %s (up-to-date)", output_filename)
  395. def link(self,
  396. target_desc,
  397. objects,
  398. output_filename,
  399. output_dir=None,
  400. libraries=None,
  401. library_dirs=None,
  402. runtime_library_dirs=None,
  403. export_symbols=None,
  404. debug=0,
  405. extra_preargs=None,
  406. extra_postargs=None,
  407. build_temp=None,
  408. target_lang=None):
  409. if not self.initialized:
  410. self.initialize()
  411. (objects, output_dir) = self._fix_object_args(objects, output_dir)
  412. fixed_args = self._fix_lib_args(libraries, library_dirs,
  413. runtime_library_dirs)
  414. (libraries, library_dirs, runtime_library_dirs) = fixed_args
  415. if runtime_library_dirs:
  416. self.warn ("I don't know what to do with 'runtime_library_dirs': "
  417. + str (runtime_library_dirs))
  418. lib_opts = gen_lib_options(self,
  419. library_dirs, runtime_library_dirs,
  420. libraries)
  421. if output_dir is not None:
  422. output_filename = os.path.join(output_dir, output_filename)
  423. if self._need_link(objects, output_filename):
  424. if target_desc == CCompiler.EXECUTABLE:
  425. if debug:
  426. ldflags = self.ldflags_shared_debug[1:]
  427. else:
  428. ldflags = self.ldflags_shared[1:]
  429. else:
  430. if debug:
  431. ldflags = self.ldflags_shared_debug
  432. else:
  433. ldflags = self.ldflags_shared
  434. export_opts = []
  435. for sym in (export_symbols or []):
  436. export_opts.append("/EXPORT:" + sym)
  437. ld_args = (ldflags + lib_opts + export_opts +
  438. objects + ['/OUT:' + output_filename])
  439. # The MSVC linker generates .lib and .exp files, which cannot be
  440. # suppressed by any linker switches. The .lib files may even be
  441. # needed! Make sure they are generated in the temporary build
  442. # directory. Since they have different names for debug and release
  443. # builds, they can go into the same directory.
  444. if export_symbols is not None:
  445. (dll_name, dll_ext) = os.path.splitext(
  446. os.path.basename(output_filename))
  447. implib_file = os.path.join(
  448. os.path.dirname(objects[0]),
  449. self.library_filename(dll_name))
  450. ld_args.append ('/IMPLIB:' + implib_file)
  451. if extra_preargs:
  452. ld_args[:0] = extra_preargs
  453. if extra_postargs:
  454. ld_args.extend(extra_postargs)
  455. self.mkpath(os.path.dirname(output_filename))
  456. try:
  457. self.spawn([self.linker] + ld_args)
  458. except DistutilsExecError as msg:
  459. raise LinkError(msg)
  460. else:
  461. log.debug("skipping %s (up-to-date)", output_filename)
  462. # -- Miscellaneous methods -----------------------------------------
  463. # These are all used by the 'gen_lib_options() function, in
  464. # ccompiler.py.
  465. def library_dir_option(self, dir):
  466. return "/LIBPATH:" + dir
  467. def runtime_library_dir_option(self, dir):
  468. raise DistutilsPlatformError(
  469. "don't know how to set runtime library search path for MSVC++")
  470. def library_option(self, lib):
  471. return self.library_filename(lib)
  472. def find_library_file(self, dirs, lib, debug=0):
  473. # Prefer a debugging library if found (and requested), but deal
  474. # with it if we don't have one.
  475. if debug:
  476. try_names = [lib + "_d", lib]
  477. else:
  478. try_names = [lib]
  479. for dir in dirs:
  480. for name in try_names:
  481. libfile = os.path.join(dir, self.library_filename (name))
  482. if os.path.exists(libfile):
  483. return libfile
  484. else:
  485. # Oops, didn't find it in *any* of 'dirs'
  486. return None
  487. # Helper methods for using the MSVC registry settings
  488. def find_exe(self, exe):
  489. """Return path to an MSVC executable program.
  490. Tries to find the program in several places: first, one of the
  491. MSVC program search paths from the registry; next, the directories
  492. in the PATH environment variable. If any of those work, return an
  493. absolute path that is known to exist. If none of them work, just
  494. return the original program name, 'exe'.
  495. """
  496. for p in self.__paths:
  497. fn = os.path.join(os.path.abspath(p), exe)
  498. if os.path.isfile(fn):
  499. return fn
  500. # didn't find it; try existing path
  501. for p in os.environ['Path'].split(';'):
  502. fn = os.path.join(os.path.abspath(p),exe)
  503. if os.path.isfile(fn):
  504. return fn
  505. return exe
  506. def get_msvc_paths(self, path, platform='x86'):
  507. """Get a list of devstudio directories (include, lib or path).
  508. Return a list of strings. The list will be empty if unable to
  509. access the registry or appropriate registry keys not found.
  510. """
  511. if not _can_read_reg:
  512. return []
  513. path = path + " dirs"
  514. if self.__version >= 7:
  515. key = (r"%s\%0.1f\VC\VC_OBJECTS_PLATFORM_INFO\Win32\Directories"
  516. % (self.__root, self.__version))
  517. else:
  518. key = (r"%s\6.0\Build System\Components\Platforms"
  519. r"\Win32 (%s)\Directories" % (self.__root, platform))
  520. for base in HKEYS:
  521. d = read_values(base, key)
  522. if d:
  523. if self.__version >= 7:
  524. return self.__macros.sub(d[path]).split(";")
  525. else:
  526. return d[path].split(";")
  527. # MSVC 6 seems to create the registry entries we need only when
  528. # the GUI is run.
  529. if self.__version == 6:
  530. for base in HKEYS:
  531. if read_values(base, r"%s\6.0" % self.__root) is not None:
  532. self.warn("It seems you have Visual Studio 6 installed, "
  533. "but the expected registry settings are not present.\n"
  534. "You must at least run the Visual Studio GUI once "
  535. "so that these entries are created.")
  536. break
  537. return []
  538. def set_path_env_var(self, name):
  539. """Set environment variable 'name' to an MSVC path type value.
  540. This is equivalent to a SET command prior to execution of spawned
  541. commands.
  542. """
  543. if name == "lib":
  544. p = self.get_msvc_paths("library")
  545. else:
  546. p = self.get_msvc_paths(name)
  547. if p:
  548. os.environ[name] = ';'.join(p)
  549. if get_build_version() >= 8.0:
  550. log.debug("Importing new compiler from distutils.msvc9compiler")
  551. OldMSVCCompiler = MSVCCompiler
  552. from distutils.msvc9compiler import MSVCCompiler
  553. # get_build_architecture not really relevant now we support cross-compile
  554. from distutils.msvc9compiler import MacroExpander