msvc9compiler.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  1. """distutils.msvc9compiler
  2. Contains MSVCCompiler, an implementation of the abstract CCompiler class
  3. for the Microsoft Visual Studio 2008.
  4. The module is compatible with VS 2005 and VS 2008. You can find legacy support
  5. for older versions of VS in distutils.msvccompiler.
  6. """
  7. # Written by Perry Stoll
  8. # hacked by Robin Becker and Thomas Heller to do a better job of
  9. # finding DevStudio (through the registry)
  10. # ported to VS2005 and VS 2008 by Christian Heimes
  11. import os
  12. import subprocess
  13. import sys
  14. import re
  15. import warnings
  16. from distutils.errors import (
  17. DistutilsExecError,
  18. DistutilsPlatformError,
  19. CompileError,
  20. LibError,
  21. LinkError,
  22. )
  23. from distutils.ccompiler import CCompiler, gen_lib_options
  24. from distutils import log
  25. from distutils.util import get_platform
  26. import winreg
  27. warnings.warn(
  28. "msvc9compiler is deprecated and slated to be removed "
  29. "in the future. Please discontinue use or file an issue "
  30. "with pypa/distutils describing your use case.",
  31. DeprecationWarning,
  32. )
  33. RegOpenKeyEx = winreg.OpenKeyEx
  34. RegEnumKey = winreg.EnumKey
  35. RegEnumValue = winreg.EnumValue
  36. RegError = winreg.error
  37. HKEYS = (
  38. winreg.HKEY_USERS,
  39. winreg.HKEY_CURRENT_USER,
  40. winreg.HKEY_LOCAL_MACHINE,
  41. winreg.HKEY_CLASSES_ROOT,
  42. )
  43. NATIVE_WIN64 = sys.platform == 'win32' and sys.maxsize > 2**32
  44. if NATIVE_WIN64:
  45. # Visual C++ is a 32-bit application, so we need to look in
  46. # the corresponding registry branch, if we're running a
  47. # 64-bit Python on Win64
  48. VS_BASE = r"Software\Wow6432Node\Microsoft\VisualStudio\%0.1f"
  49. WINSDK_BASE = r"Software\Wow6432Node\Microsoft\Microsoft SDKs\Windows"
  50. NET_BASE = r"Software\Wow6432Node\Microsoft\.NETFramework"
  51. else:
  52. VS_BASE = r"Software\Microsoft\VisualStudio\%0.1f"
  53. WINSDK_BASE = r"Software\Microsoft\Microsoft SDKs\Windows"
  54. NET_BASE = r"Software\Microsoft\.NETFramework"
  55. # A map keyed by get_platform() return values to values accepted by
  56. # 'vcvarsall.bat'. Note a cross-compile may combine these (eg, 'x86_amd64' is
  57. # the param to cross-compile on x86 targeting amd64.)
  58. PLAT_TO_VCVARS = {
  59. 'win32': 'x86',
  60. 'win-amd64': 'amd64',
  61. }
  62. class Reg:
  63. """Helper class to read values from the registry"""
  64. def get_value(cls, path, key):
  65. for base in HKEYS:
  66. d = cls.read_values(base, path)
  67. if d and key in d:
  68. return d[key]
  69. raise KeyError(key)
  70. get_value = classmethod(get_value)
  71. def read_keys(cls, base, key):
  72. """Return list of registry keys."""
  73. try:
  74. handle = RegOpenKeyEx(base, key)
  75. except RegError:
  76. return None
  77. L = []
  78. i = 0
  79. while True:
  80. try:
  81. k = RegEnumKey(handle, i)
  82. except RegError:
  83. break
  84. L.append(k)
  85. i += 1
  86. return L
  87. read_keys = classmethod(read_keys)
  88. def read_values(cls, base, key):
  89. """Return dict of registry keys and values.
  90. All names are converted to lowercase.
  91. """
  92. try:
  93. handle = RegOpenKeyEx(base, key)
  94. except RegError:
  95. return None
  96. d = {}
  97. i = 0
  98. while True:
  99. try:
  100. name, value, type = RegEnumValue(handle, i)
  101. except RegError:
  102. break
  103. name = name.lower()
  104. d[cls.convert_mbcs(name)] = cls.convert_mbcs(value)
  105. i += 1
  106. return d
  107. read_values = classmethod(read_values)
  108. def convert_mbcs(s):
  109. dec = getattr(s, "decode", None)
  110. if dec is not None:
  111. try:
  112. s = dec("mbcs")
  113. except UnicodeError:
  114. pass
  115. return s
  116. convert_mbcs = staticmethod(convert_mbcs)
  117. class MacroExpander:
  118. def __init__(self, version):
  119. self.macros = {}
  120. self.vsbase = VS_BASE % version
  121. self.load_macros(version)
  122. def set_macro(self, macro, path, key):
  123. self.macros["$(%s)" % macro] = Reg.get_value(path, key)
  124. def load_macros(self, version):
  125. self.set_macro("VCInstallDir", self.vsbase + r"\Setup\VC", "productdir")
  126. self.set_macro("VSInstallDir", self.vsbase + r"\Setup\VS", "productdir")
  127. self.set_macro("FrameworkDir", NET_BASE, "installroot")
  128. try:
  129. if version >= 8.0:
  130. self.set_macro("FrameworkSDKDir", NET_BASE, "sdkinstallrootv2.0")
  131. else:
  132. raise KeyError("sdkinstallrootv2.0")
  133. except KeyError:
  134. raise DistutilsPlatformError(
  135. """Python was built with Visual Studio 2008;
  136. extensions must be built with a compiler than can generate compatible binaries.
  137. Visual Studio 2008 was not found on this system. If you have Cygwin installed,
  138. you can try compiling with MingW32, by passing "-c mingw32" to setup.py."""
  139. )
  140. if version >= 9.0:
  141. self.set_macro("FrameworkVersion", self.vsbase, "clr version")
  142. self.set_macro("WindowsSdkDir", WINSDK_BASE, "currentinstallfolder")
  143. else:
  144. p = r"Software\Microsoft\NET Framework Setup\Product"
  145. for base in HKEYS:
  146. try:
  147. h = RegOpenKeyEx(base, p)
  148. except RegError:
  149. continue
  150. key = RegEnumKey(h, 0)
  151. d = Reg.get_value(base, r"{}\{}".format(p, key))
  152. self.macros["$(FrameworkVersion)"] = d["version"]
  153. def sub(self, s):
  154. for k, v in self.macros.items():
  155. s = s.replace(k, v)
  156. return s
  157. def get_build_version():
  158. """Return the version of MSVC that was used to build Python.
  159. For Python 2.3 and up, the version number is included in
  160. sys.version. For earlier versions, assume the compiler is MSVC 6.
  161. """
  162. prefix = "MSC v."
  163. i = sys.version.find(prefix)
  164. if i == -1:
  165. return 6
  166. i = i + len(prefix)
  167. s, rest = sys.version[i:].split(" ", 1)
  168. majorVersion = int(s[:-2]) - 6
  169. if majorVersion >= 13:
  170. # v13 was skipped and should be v14
  171. majorVersion += 1
  172. minorVersion = int(s[2:3]) / 10.0
  173. # I don't think paths are affected by minor version in version 6
  174. if majorVersion == 6:
  175. minorVersion = 0
  176. if majorVersion >= 6:
  177. return majorVersion + minorVersion
  178. # else we don't know what version of the compiler this is
  179. return None
  180. def normalize_and_reduce_paths(paths):
  181. """Return a list of normalized paths with duplicates removed.
  182. The current order of paths is maintained.
  183. """
  184. # Paths are normalized so things like: /a and /a/ aren't both preserved.
  185. reduced_paths = []
  186. for p in paths:
  187. np = os.path.normpath(p)
  188. # XXX(nnorwitz): O(n**2), if reduced_paths gets long perhaps use a set.
  189. if np not in reduced_paths:
  190. reduced_paths.append(np)
  191. return reduced_paths
  192. def removeDuplicates(variable):
  193. """Remove duplicate values of an environment variable."""
  194. oldList = variable.split(os.pathsep)
  195. newList = []
  196. for i in oldList:
  197. if i not in newList:
  198. newList.append(i)
  199. newVariable = os.pathsep.join(newList)
  200. return newVariable
  201. def find_vcvarsall(version):
  202. """Find the vcvarsall.bat file
  203. At first it tries to find the productdir of VS 2008 in the registry. If
  204. that fails it falls back to the VS90COMNTOOLS env var.
  205. """
  206. vsbase = VS_BASE % version
  207. try:
  208. productdir = Reg.get_value(r"%s\Setup\VC" % vsbase, "productdir")
  209. except KeyError:
  210. log.debug("Unable to find productdir in registry")
  211. productdir = None
  212. if not productdir or not os.path.isdir(productdir):
  213. toolskey = "VS%0.f0COMNTOOLS" % version
  214. toolsdir = os.environ.get(toolskey, None)
  215. if toolsdir and os.path.isdir(toolsdir):
  216. productdir = os.path.join(toolsdir, os.pardir, os.pardir, "VC")
  217. productdir = os.path.abspath(productdir)
  218. if not os.path.isdir(productdir):
  219. log.debug("%s is not a valid directory" % productdir)
  220. return None
  221. else:
  222. log.debug("Env var %s is not set or invalid" % toolskey)
  223. if not productdir:
  224. log.debug("No productdir found")
  225. return None
  226. vcvarsall = os.path.join(productdir, "vcvarsall.bat")
  227. if os.path.isfile(vcvarsall):
  228. return vcvarsall
  229. log.debug("Unable to find vcvarsall.bat")
  230. return None
  231. def query_vcvarsall(version, arch="x86"):
  232. """Launch vcvarsall.bat and read the settings from its environment"""
  233. vcvarsall = find_vcvarsall(version)
  234. interesting = {"include", "lib", "libpath", "path"}
  235. result = {}
  236. if vcvarsall is None:
  237. raise DistutilsPlatformError("Unable to find vcvarsall.bat")
  238. log.debug("Calling 'vcvarsall.bat %s' (version=%s)", arch, version)
  239. popen = subprocess.Popen(
  240. '"{}" {} & set'.format(vcvarsall, arch),
  241. stdout=subprocess.PIPE,
  242. stderr=subprocess.PIPE,
  243. )
  244. try:
  245. stdout, stderr = popen.communicate()
  246. if popen.wait() != 0:
  247. raise DistutilsPlatformError(stderr.decode("mbcs"))
  248. stdout = stdout.decode("mbcs")
  249. for line in stdout.split("\n"):
  250. line = Reg.convert_mbcs(line)
  251. if '=' not in line:
  252. continue
  253. line = line.strip()
  254. key, value = line.split('=', 1)
  255. key = key.lower()
  256. if key in interesting:
  257. if value.endswith(os.pathsep):
  258. value = value[:-1]
  259. result[key] = removeDuplicates(value)
  260. finally:
  261. popen.stdout.close()
  262. popen.stderr.close()
  263. if len(result) != len(interesting):
  264. raise ValueError(str(list(result.keys())))
  265. return result
  266. # More globals
  267. VERSION = get_build_version()
  268. # MACROS = MacroExpander(VERSION)
  269. class MSVCCompiler(CCompiler):
  270. """Concrete class that implements an interface to Microsoft Visual C++,
  271. as defined by the CCompiler abstract class."""
  272. compiler_type = 'msvc'
  273. # Just set this so CCompiler's constructor doesn't barf. We currently
  274. # don't use the 'set_executables()' bureaucracy provided by CCompiler,
  275. # as it really isn't necessary for this sort of single-compiler class.
  276. # Would be nice to have a consistent interface with UnixCCompiler,
  277. # though, so it's worth thinking about.
  278. executables = {}
  279. # Private class data (need to distinguish C from C++ source for compiler)
  280. _c_extensions = ['.c']
  281. _cpp_extensions = ['.cc', '.cpp', '.cxx']
  282. _rc_extensions = ['.rc']
  283. _mc_extensions = ['.mc']
  284. # Needed for the filename generation methods provided by the
  285. # base class, CCompiler.
  286. src_extensions = _c_extensions + _cpp_extensions + _rc_extensions + _mc_extensions
  287. res_extension = '.res'
  288. obj_extension = '.obj'
  289. static_lib_extension = '.lib'
  290. shared_lib_extension = '.dll'
  291. static_lib_format = shared_lib_format = '%s%s'
  292. exe_extension = '.exe'
  293. def __init__(self, verbose=0, dry_run=0, force=0):
  294. super().__init__(verbose, dry_run, force)
  295. self.__version = VERSION
  296. self.__root = r"Software\Microsoft\VisualStudio"
  297. # self.__macros = MACROS
  298. self.__paths = []
  299. # target platform (.plat_name is consistent with 'bdist')
  300. self.plat_name = None
  301. self.__arch = None # deprecated name
  302. self.initialized = False
  303. def initialize(self, plat_name=None): # noqa: C901
  304. # multi-init means we would need to check platform same each time...
  305. assert not self.initialized, "don't init multiple times"
  306. if self.__version < 8.0:
  307. raise DistutilsPlatformError(
  308. "VC %0.1f is not supported by this module" % self.__version
  309. )
  310. if plat_name is None:
  311. plat_name = get_platform()
  312. # sanity check for platforms to prevent obscure errors later.
  313. ok_plats = 'win32', 'win-amd64'
  314. if plat_name not in ok_plats:
  315. raise DistutilsPlatformError(
  316. "--plat-name must be one of {}".format(ok_plats)
  317. )
  318. if (
  319. "DISTUTILS_USE_SDK" in os.environ
  320. and "MSSdk" in os.environ
  321. and self.find_exe("cl.exe")
  322. ):
  323. # Assume that the SDK set up everything alright; don't try to be
  324. # smarter
  325. self.cc = "cl.exe"
  326. self.linker = "link.exe"
  327. self.lib = "lib.exe"
  328. self.rc = "rc.exe"
  329. self.mc = "mc.exe"
  330. else:
  331. # On x86, 'vcvars32.bat amd64' creates an env that doesn't work;
  332. # to cross compile, you use 'x86_amd64'.
  333. # On AMD64, 'vcvars32.bat amd64' is a native build env; to cross
  334. # compile use 'x86' (ie, it runs the x86 compiler directly)
  335. if plat_name == get_platform() or plat_name == 'win32':
  336. # native build or cross-compile to win32
  337. plat_spec = PLAT_TO_VCVARS[plat_name]
  338. else:
  339. # cross compile from win32 -> some 64bit
  340. plat_spec = (
  341. PLAT_TO_VCVARS[get_platform()] + '_' + PLAT_TO_VCVARS[plat_name]
  342. )
  343. vc_env = query_vcvarsall(VERSION, plat_spec)
  344. self.__paths = vc_env['path'].split(os.pathsep)
  345. os.environ['lib'] = vc_env['lib']
  346. os.environ['include'] = vc_env['include']
  347. if len(self.__paths) == 0:
  348. raise DistutilsPlatformError(
  349. "Python was built with %s, "
  350. "and extensions need to be built with the same "
  351. "version of the compiler, but it isn't installed." % self.__product
  352. )
  353. self.cc = self.find_exe("cl.exe")
  354. self.linker = self.find_exe("link.exe")
  355. self.lib = self.find_exe("lib.exe")
  356. self.rc = self.find_exe("rc.exe") # resource compiler
  357. self.mc = self.find_exe("mc.exe") # message compiler
  358. # self.set_path_env_var('lib')
  359. # self.set_path_env_var('include')
  360. # extend the MSVC path with the current path
  361. try:
  362. for p in os.environ['path'].split(';'):
  363. self.__paths.append(p)
  364. except KeyError:
  365. pass
  366. self.__paths = normalize_and_reduce_paths(self.__paths)
  367. os.environ['path'] = ";".join(self.__paths)
  368. self.preprocess_options = None
  369. if self.__arch == "x86":
  370. self.compile_options = ['/nologo', '/O2', '/MD', '/W3', '/DNDEBUG']
  371. self.compile_options_debug = [
  372. '/nologo',
  373. '/Od',
  374. '/MDd',
  375. '/W3',
  376. '/Z7',
  377. '/D_DEBUG',
  378. ]
  379. else:
  380. # Win64
  381. self.compile_options = ['/nologo', '/O2', '/MD', '/W3', '/GS-', '/DNDEBUG']
  382. self.compile_options_debug = [
  383. '/nologo',
  384. '/Od',
  385. '/MDd',
  386. '/W3',
  387. '/GS-',
  388. '/Z7',
  389. '/D_DEBUG',
  390. ]
  391. self.ldflags_shared = ['/DLL', '/nologo', '/INCREMENTAL:NO']
  392. if self.__version >= 7:
  393. self.ldflags_shared_debug = ['/DLL', '/nologo', '/INCREMENTAL:no', '/DEBUG']
  394. self.ldflags_static = ['/nologo']
  395. self.initialized = True
  396. # -- Worker methods ------------------------------------------------
  397. def object_filenames(self, source_filenames, strip_dir=0, output_dir=''):
  398. # Copied from ccompiler.py, extended to return .res as 'object'-file
  399. # for .rc input file
  400. if output_dir is None:
  401. output_dir = ''
  402. obj_names = []
  403. for src_name in source_filenames:
  404. (base, ext) = os.path.splitext(src_name)
  405. base = os.path.splitdrive(base)[1] # Chop off the drive
  406. base = base[os.path.isabs(base) :] # If abs, chop off leading /
  407. if ext not in self.src_extensions:
  408. # Better to raise an exception instead of silently continuing
  409. # and later complain about sources and targets having
  410. # different lengths
  411. raise CompileError("Don't know how to compile %s" % src_name)
  412. if strip_dir:
  413. base = os.path.basename(base)
  414. if ext in self._rc_extensions:
  415. obj_names.append(os.path.join(output_dir, base + self.res_extension))
  416. elif ext in self._mc_extensions:
  417. obj_names.append(os.path.join(output_dir, base + self.res_extension))
  418. else:
  419. obj_names.append(os.path.join(output_dir, base + self.obj_extension))
  420. return obj_names
  421. def compile( # noqa: C901
  422. self,
  423. sources,
  424. output_dir=None,
  425. macros=None,
  426. include_dirs=None,
  427. debug=0,
  428. extra_preargs=None,
  429. extra_postargs=None,
  430. depends=None,
  431. ):
  432. if not self.initialized:
  433. self.initialize()
  434. compile_info = self._setup_compile(
  435. output_dir, macros, include_dirs, sources, depends, extra_postargs
  436. )
  437. macros, objects, extra_postargs, pp_opts, build = compile_info
  438. compile_opts = extra_preargs or []
  439. compile_opts.append('/c')
  440. if debug:
  441. compile_opts.extend(self.compile_options_debug)
  442. else:
  443. compile_opts.extend(self.compile_options)
  444. for obj in objects:
  445. try:
  446. src, ext = build[obj]
  447. except KeyError:
  448. continue
  449. if debug:
  450. # pass the full pathname to MSVC in debug mode,
  451. # this allows the debugger to find the source file
  452. # without asking the user to browse for it
  453. src = os.path.abspath(src)
  454. if ext in self._c_extensions:
  455. input_opt = "/Tc" + src
  456. elif ext in self._cpp_extensions:
  457. input_opt = "/Tp" + src
  458. elif ext in self._rc_extensions:
  459. # compile .RC to .RES file
  460. input_opt = src
  461. output_opt = "/fo" + obj
  462. try:
  463. self.spawn([self.rc] + pp_opts + [output_opt] + [input_opt])
  464. except DistutilsExecError as msg:
  465. raise CompileError(msg)
  466. continue
  467. elif ext in self._mc_extensions:
  468. # Compile .MC to .RC file to .RES file.
  469. # * '-h dir' specifies the directory for the
  470. # generated include file
  471. # * '-r dir' specifies the target directory of the
  472. # generated RC file and the binary message resource
  473. # it includes
  474. #
  475. # For now (since there are no options to change this),
  476. # we use the source-directory for the include file and
  477. # the build directory for the RC file and message
  478. # resources. This works at least for win32all.
  479. h_dir = os.path.dirname(src)
  480. rc_dir = os.path.dirname(obj)
  481. try:
  482. # first compile .MC to .RC and .H file
  483. self.spawn([self.mc] + ['-h', h_dir, '-r', rc_dir] + [src])
  484. base, _ = os.path.splitext(os.path.basename(src))
  485. rc_file = os.path.join(rc_dir, base + '.rc')
  486. # then compile .RC to .RES file
  487. self.spawn([self.rc] + ["/fo" + obj] + [rc_file])
  488. except DistutilsExecError as msg:
  489. raise CompileError(msg)
  490. continue
  491. else:
  492. # how to handle this file?
  493. raise CompileError(
  494. "Don't know how to compile {} to {}".format(src, obj)
  495. )
  496. output_opt = "/Fo" + obj
  497. try:
  498. self.spawn(
  499. [self.cc]
  500. + compile_opts
  501. + pp_opts
  502. + [input_opt, output_opt]
  503. + extra_postargs
  504. )
  505. except DistutilsExecError as msg:
  506. raise CompileError(msg)
  507. return objects
  508. def create_static_lib(
  509. self, objects, output_libname, output_dir=None, debug=0, target_lang=None
  510. ):
  511. if not self.initialized:
  512. self.initialize()
  513. (objects, output_dir) = self._fix_object_args(objects, output_dir)
  514. output_filename = self.library_filename(output_libname, output_dir=output_dir)
  515. if self._need_link(objects, output_filename):
  516. lib_args = objects + ['/OUT:' + output_filename]
  517. if debug:
  518. pass # XXX what goes here?
  519. try:
  520. self.spawn([self.lib] + lib_args)
  521. except DistutilsExecError as msg:
  522. raise LibError(msg)
  523. else:
  524. log.debug("skipping %s (up-to-date)", output_filename)
  525. def link( # noqa: C901
  526. self,
  527. target_desc,
  528. objects,
  529. output_filename,
  530. output_dir=None,
  531. libraries=None,
  532. library_dirs=None,
  533. runtime_library_dirs=None,
  534. export_symbols=None,
  535. debug=0,
  536. extra_preargs=None,
  537. extra_postargs=None,
  538. build_temp=None,
  539. target_lang=None,
  540. ):
  541. if not self.initialized:
  542. self.initialize()
  543. (objects, output_dir) = self._fix_object_args(objects, output_dir)
  544. fixed_args = self._fix_lib_args(libraries, library_dirs, runtime_library_dirs)
  545. (libraries, library_dirs, runtime_library_dirs) = fixed_args
  546. if runtime_library_dirs:
  547. self.warn(
  548. "I don't know what to do with 'runtime_library_dirs': "
  549. + str(runtime_library_dirs)
  550. )
  551. lib_opts = gen_lib_options(self, library_dirs, runtime_library_dirs, libraries)
  552. if output_dir is not None:
  553. output_filename = os.path.join(output_dir, output_filename)
  554. if self._need_link(objects, output_filename):
  555. if target_desc == CCompiler.EXECUTABLE:
  556. if debug:
  557. ldflags = self.ldflags_shared_debug[1:]
  558. else:
  559. ldflags = self.ldflags_shared[1:]
  560. else:
  561. if debug:
  562. ldflags = self.ldflags_shared_debug
  563. else:
  564. ldflags = self.ldflags_shared
  565. export_opts = []
  566. for sym in export_symbols or []:
  567. export_opts.append("/EXPORT:" + sym)
  568. ld_args = (
  569. ldflags + lib_opts + export_opts + objects + ['/OUT:' + output_filename]
  570. )
  571. # The MSVC linker generates .lib and .exp files, which cannot be
  572. # suppressed by any linker switches. The .lib files may even be
  573. # needed! Make sure they are generated in the temporary build
  574. # directory. Since they have different names for debug and release
  575. # builds, they can go into the same directory.
  576. build_temp = os.path.dirname(objects[0])
  577. if export_symbols is not None:
  578. (dll_name, dll_ext) = os.path.splitext(
  579. os.path.basename(output_filename)
  580. )
  581. implib_file = os.path.join(build_temp, self.library_filename(dll_name))
  582. ld_args.append('/IMPLIB:' + implib_file)
  583. self.manifest_setup_ldargs(output_filename, build_temp, ld_args)
  584. if extra_preargs:
  585. ld_args[:0] = extra_preargs
  586. if extra_postargs:
  587. ld_args.extend(extra_postargs)
  588. self.mkpath(os.path.dirname(output_filename))
  589. try:
  590. self.spawn([self.linker] + ld_args)
  591. except DistutilsExecError as msg:
  592. raise LinkError(msg)
  593. # embed the manifest
  594. # XXX - this is somewhat fragile - if mt.exe fails, distutils
  595. # will still consider the DLL up-to-date, but it will not have a
  596. # manifest. Maybe we should link to a temp file? OTOH, that
  597. # implies a build environment error that shouldn't go undetected.
  598. mfinfo = self.manifest_get_embed_info(target_desc, ld_args)
  599. if mfinfo is not None:
  600. mffilename, mfid = mfinfo
  601. out_arg = '-outputresource:{};{}'.format(output_filename, mfid)
  602. try:
  603. self.spawn(['mt.exe', '-nologo', '-manifest', mffilename, out_arg])
  604. except DistutilsExecError as msg:
  605. raise LinkError(msg)
  606. else:
  607. log.debug("skipping %s (up-to-date)", output_filename)
  608. def manifest_setup_ldargs(self, output_filename, build_temp, ld_args):
  609. # If we need a manifest at all, an embedded manifest is recommended.
  610. # See MSDN article titled
  611. # "How to: Embed a Manifest Inside a C/C++ Application"
  612. # (currently at http://msdn2.microsoft.com/en-us/library/ms235591(VS.80).aspx)
  613. # Ask the linker to generate the manifest in the temp dir, so
  614. # we can check it, and possibly embed it, later.
  615. temp_manifest = os.path.join(
  616. build_temp, os.path.basename(output_filename) + ".manifest"
  617. )
  618. ld_args.append('/MANIFESTFILE:' + temp_manifest)
  619. def manifest_get_embed_info(self, target_desc, ld_args):
  620. # If a manifest should be embedded, return a tuple of
  621. # (manifest_filename, resource_id). Returns None if no manifest
  622. # should be embedded. See http://bugs.python.org/issue7833 for why
  623. # we want to avoid any manifest for extension modules if we can)
  624. for arg in ld_args:
  625. if arg.startswith("/MANIFESTFILE:"):
  626. temp_manifest = arg.split(":", 1)[1]
  627. break
  628. else:
  629. # no /MANIFESTFILE so nothing to do.
  630. return None
  631. if target_desc == CCompiler.EXECUTABLE:
  632. # by default, executables always get the manifest with the
  633. # CRT referenced.
  634. mfid = 1
  635. else:
  636. # Extension modules try and avoid any manifest if possible.
  637. mfid = 2
  638. temp_manifest = self._remove_visual_c_ref(temp_manifest)
  639. if temp_manifest is None:
  640. return None
  641. return temp_manifest, mfid
  642. def _remove_visual_c_ref(self, manifest_file):
  643. try:
  644. # Remove references to the Visual C runtime, so they will
  645. # fall through to the Visual C dependency of Python.exe.
  646. # This way, when installed for a restricted user (e.g.
  647. # runtimes are not in WinSxS folder, but in Python's own
  648. # folder), the runtimes do not need to be in every folder
  649. # with .pyd's.
  650. # Returns either the filename of the modified manifest or
  651. # None if no manifest should be embedded.
  652. manifest_f = open(manifest_file)
  653. try:
  654. manifest_buf = manifest_f.read()
  655. finally:
  656. manifest_f.close()
  657. pattern = re.compile(
  658. r"""<assemblyIdentity.*?name=("|')Microsoft\."""
  659. r"""VC\d{2}\.CRT("|').*?(/>|</assemblyIdentity>)""",
  660. re.DOTALL,
  661. )
  662. manifest_buf = re.sub(pattern, "", manifest_buf)
  663. pattern = r"<dependentAssembly>\s*</dependentAssembly>"
  664. manifest_buf = re.sub(pattern, "", manifest_buf)
  665. # Now see if any other assemblies are referenced - if not, we
  666. # don't want a manifest embedded.
  667. pattern = re.compile(
  668. r"""<assemblyIdentity.*?name=(?:"|')(.+?)(?:"|')"""
  669. r""".*?(?:/>|</assemblyIdentity>)""",
  670. re.DOTALL,
  671. )
  672. if re.search(pattern, manifest_buf) is None:
  673. return None
  674. manifest_f = open(manifest_file, 'w')
  675. try:
  676. manifest_f.write(manifest_buf)
  677. return manifest_file
  678. finally:
  679. manifest_f.close()
  680. except OSError:
  681. pass
  682. # -- Miscellaneous methods -----------------------------------------
  683. # These are all used by the 'gen_lib_options() function, in
  684. # ccompiler.py.
  685. def library_dir_option(self, dir):
  686. return "/LIBPATH:" + dir
  687. def runtime_library_dir_option(self, dir):
  688. raise DistutilsPlatformError(
  689. "don't know how to set runtime library search path for MSVC++"
  690. )
  691. def library_option(self, lib):
  692. return self.library_filename(lib)
  693. def find_library_file(self, dirs, lib, debug=0):
  694. # Prefer a debugging library if found (and requested), but deal
  695. # with it if we don't have one.
  696. if debug:
  697. try_names = [lib + "_d", lib]
  698. else:
  699. try_names = [lib]
  700. for dir in dirs:
  701. for name in try_names:
  702. libfile = os.path.join(dir, self.library_filename(name))
  703. if os.path.exists(libfile):
  704. return libfile
  705. else:
  706. # Oops, didn't find it in *any* of 'dirs'
  707. return None
  708. # Helper methods for using the MSVC registry settings
  709. def find_exe(self, exe):
  710. """Return path to an MSVC executable program.
  711. Tries to find the program in several places: first, one of the
  712. MSVC program search paths from the registry; next, the directories
  713. in the PATH environment variable. If any of those work, return an
  714. absolute path that is known to exist. If none of them work, just
  715. return the original program name, 'exe'.
  716. """
  717. for p in self.__paths:
  718. fn = os.path.join(os.path.abspath(p), exe)
  719. if os.path.isfile(fn):
  720. return fn
  721. # didn't find it; try existing path
  722. for p in os.environ['Path'].split(';'):
  723. fn = os.path.join(os.path.abspath(p), exe)
  724. if os.path.isfile(fn):
  725. return fn
  726. return exe