Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

msvccompiler.py 23KB

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