msvccompiler.py 23 KB

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