PageRenderTime 54ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/node_modules/node-gyp/gyp/pylib/gyp/win_tool.py

https://gitlab.com/blocknotary/IonicInterviews
Python | 314 lines | 252 code | 19 blank | 43 comment | 24 complexity | 562cf1a94c7be433253dd3f07b7273c4 MD5 | raw file
  1. #!/usr/bin/env python
  2. # Copyright (c) 2012 Google Inc. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. """Utility functions for Windows builds.
  6. These functions are executed via gyp-win-tool when using the ninja generator.
  7. """
  8. import os
  9. import re
  10. import shutil
  11. import subprocess
  12. import stat
  13. import string
  14. import sys
  15. BASE_DIR = os.path.dirname(os.path.abspath(__file__))
  16. # A regex matching an argument corresponding to the output filename passed to
  17. # link.exe.
  18. _LINK_EXE_OUT_ARG = re.compile('/OUT:(?P<out>.+)$', re.IGNORECASE)
  19. def main(args):
  20. executor = WinTool()
  21. exit_code = executor.Dispatch(args)
  22. if exit_code is not None:
  23. sys.exit(exit_code)
  24. class WinTool(object):
  25. """This class performs all the Windows tooling steps. The methods can either
  26. be executed directly, or dispatched from an argument list."""
  27. def _UseSeparateMspdbsrv(self, env, args):
  28. """Allows to use a unique instance of mspdbsrv.exe per linker instead of a
  29. shared one."""
  30. if len(args) < 1:
  31. raise Exception("Not enough arguments")
  32. if args[0] != 'link.exe':
  33. return
  34. # Use the output filename passed to the linker to generate an endpoint name
  35. # for mspdbsrv.exe.
  36. endpoint_name = None
  37. for arg in args:
  38. m = _LINK_EXE_OUT_ARG.match(arg)
  39. if m:
  40. endpoint_name = re.sub(r'\W+', '',
  41. '%s_%d' % (m.group('out'), os.getpid()))
  42. break
  43. if endpoint_name is None:
  44. return
  45. # Adds the appropriate environment variable. This will be read by link.exe
  46. # to know which instance of mspdbsrv.exe it should connect to (if it's
  47. # not set then the default endpoint is used).
  48. env['_MSPDBSRV_ENDPOINT_'] = endpoint_name
  49. def Dispatch(self, args):
  50. """Dispatches a string command to a method."""
  51. if len(args) < 1:
  52. raise Exception("Not enough arguments")
  53. method = "Exec%s" % self._CommandifyName(args[0])
  54. return getattr(self, method)(*args[1:])
  55. def _CommandifyName(self, name_string):
  56. """Transforms a tool name like recursive-mirror to RecursiveMirror."""
  57. return name_string.title().replace('-', '')
  58. def _GetEnv(self, arch):
  59. """Gets the saved environment from a file for a given architecture."""
  60. # The environment is saved as an "environment block" (see CreateProcess
  61. # and msvs_emulation for details). We convert to a dict here.
  62. # Drop last 2 NULs, one for list terminator, one for trailing vs. separator.
  63. pairs = open(arch).read()[:-2].split('\0')
  64. kvs = [item.split('=', 1) for item in pairs]
  65. return dict(kvs)
  66. def ExecStamp(self, path):
  67. """Simple stamp command."""
  68. open(path, 'w').close()
  69. def ExecRecursiveMirror(self, source, dest):
  70. """Emulation of rm -rf out && cp -af in out."""
  71. if os.path.exists(dest):
  72. if os.path.isdir(dest):
  73. def _on_error(fn, path, excinfo):
  74. # The operation failed, possibly because the file is set to
  75. # read-only. If that's why, make it writable and try the op again.
  76. if not os.access(path, os.W_OK):
  77. os.chmod(path, stat.S_IWRITE)
  78. fn(path)
  79. shutil.rmtree(dest, onerror=_on_error)
  80. else:
  81. if not os.access(dest, os.W_OK):
  82. # Attempt to make the file writable before deleting it.
  83. os.chmod(dest, stat.S_IWRITE)
  84. os.unlink(dest)
  85. if os.path.isdir(source):
  86. shutil.copytree(source, dest)
  87. else:
  88. shutil.copy2(source, dest)
  89. def ExecLinkWrapper(self, arch, use_separate_mspdbsrv, *args):
  90. """Filter diagnostic output from link that looks like:
  91. ' Creating library ui.dll.lib and object ui.dll.exp'
  92. This happens when there are exports from the dll or exe.
  93. """
  94. env = self._GetEnv(arch)
  95. if use_separate_mspdbsrv == 'True':
  96. self._UseSeparateMspdbsrv(env, args)
  97. link = subprocess.Popen([args[0].replace('/', '\\')] + list(args[1:]),
  98. shell=True,
  99. env=env,
  100. stdout=subprocess.PIPE,
  101. stderr=subprocess.STDOUT)
  102. out, _ = link.communicate()
  103. for line in out.splitlines():
  104. if (not line.startswith(' Creating library ') and
  105. not line.startswith('Generating code') and
  106. not line.startswith('Finished generating code')):
  107. print line
  108. return link.returncode
  109. def ExecLinkWithManifests(self, arch, embed_manifest, out, ldcmd, resname,
  110. mt, rc, intermediate_manifest, *manifests):
  111. """A wrapper for handling creating a manifest resource and then executing
  112. a link command."""
  113. # The 'normal' way to do manifests is to have link generate a manifest
  114. # based on gathering dependencies from the object files, then merge that
  115. # manifest with other manifests supplied as sources, convert the merged
  116. # manifest to a resource, and then *relink*, including the compiled
  117. # version of the manifest resource. This breaks incremental linking, and
  118. # is generally overly complicated. Instead, we merge all the manifests
  119. # provided (along with one that includes what would normally be in the
  120. # linker-generated one, see msvs_emulation.py), and include that into the
  121. # first and only link. We still tell link to generate a manifest, but we
  122. # only use that to assert that our simpler process did not miss anything.
  123. variables = {
  124. 'python': sys.executable,
  125. 'arch': arch,
  126. 'out': out,
  127. 'ldcmd': ldcmd,
  128. 'resname': resname,
  129. 'mt': mt,
  130. 'rc': rc,
  131. 'intermediate_manifest': intermediate_manifest,
  132. 'manifests': ' '.join(manifests),
  133. }
  134. add_to_ld = ''
  135. if manifests:
  136. subprocess.check_call(
  137. '%(python)s gyp-win-tool manifest-wrapper %(arch)s %(mt)s -nologo '
  138. '-manifest %(manifests)s -out:%(out)s.manifest' % variables)
  139. if embed_manifest == 'True':
  140. subprocess.check_call(
  141. '%(python)s gyp-win-tool manifest-to-rc %(arch)s %(out)s.manifest'
  142. ' %(out)s.manifest.rc %(resname)s' % variables)
  143. subprocess.check_call(
  144. '%(python)s gyp-win-tool rc-wrapper %(arch)s %(rc)s '
  145. '%(out)s.manifest.rc' % variables)
  146. add_to_ld = ' %(out)s.manifest.res' % variables
  147. subprocess.check_call(ldcmd + add_to_ld)
  148. # Run mt.exe on the theoretically complete manifest we generated, merging
  149. # it with the one the linker generated to confirm that the linker
  150. # generated one does not add anything. This is strictly unnecessary for
  151. # correctness, it's only to verify that e.g. /MANIFESTDEPENDENCY was not
  152. # used in a #pragma comment.
  153. if manifests:
  154. # Merge the intermediate one with ours to .assert.manifest, then check
  155. # that .assert.manifest is identical to ours.
  156. subprocess.check_call(
  157. '%(python)s gyp-win-tool manifest-wrapper %(arch)s %(mt)s -nologo '
  158. '-manifest %(out)s.manifest %(intermediate_manifest)s '
  159. '-out:%(out)s.assert.manifest' % variables)
  160. assert_manifest = '%(out)s.assert.manifest' % variables
  161. our_manifest = '%(out)s.manifest' % variables
  162. # Load and normalize the manifests. mt.exe sometimes removes whitespace,
  163. # and sometimes doesn't unfortunately.
  164. with open(our_manifest, 'rb') as our_f:
  165. with open(assert_manifest, 'rb') as assert_f:
  166. our_data = our_f.read().translate(None, string.whitespace)
  167. assert_data = assert_f.read().translate(None, string.whitespace)
  168. if our_data != assert_data:
  169. os.unlink(out)
  170. def dump(filename):
  171. sys.stderr.write('%s\n-----\n' % filename)
  172. with open(filename, 'rb') as f:
  173. sys.stderr.write(f.read() + '\n-----\n')
  174. dump(intermediate_manifest)
  175. dump(our_manifest)
  176. dump(assert_manifest)
  177. sys.stderr.write(
  178. 'Linker generated manifest "%s" added to final manifest "%s" '
  179. '(result in "%s"). '
  180. 'Were /MANIFEST switches used in #pragma statements? ' % (
  181. intermediate_manifest, our_manifest, assert_manifest))
  182. return 1
  183. def ExecManifestWrapper(self, arch, *args):
  184. """Run manifest tool with environment set. Strip out undesirable warning
  185. (some XML blocks are recognized by the OS loader, but not the manifest
  186. tool)."""
  187. env = self._GetEnv(arch)
  188. popen = subprocess.Popen(args, shell=True, env=env,
  189. stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  190. out, _ = popen.communicate()
  191. for line in out.splitlines():
  192. if line and 'manifest authoring warning 81010002' not in line:
  193. print line
  194. return popen.returncode
  195. def ExecManifestToRc(self, arch, *args):
  196. """Creates a resource file pointing a SxS assembly manifest.
  197. |args| is tuple containing path to resource file, path to manifest file
  198. and resource name which can be "1" (for executables) or "2" (for DLLs)."""
  199. manifest_path, resource_path, resource_name = args
  200. with open(resource_path, 'wb') as output:
  201. output.write('#include <windows.h>\n%s RT_MANIFEST "%s"' % (
  202. resource_name,
  203. os.path.abspath(manifest_path).replace('\\', '/')))
  204. def ExecMidlWrapper(self, arch, outdir, tlb, h, dlldata, iid, proxy, idl,
  205. *flags):
  206. """Filter noisy filenames output from MIDL compile step that isn't
  207. quietable via command line flags.
  208. """
  209. args = ['midl', '/nologo'] + list(flags) + [
  210. '/out', outdir,
  211. '/tlb', tlb,
  212. '/h', h,
  213. '/dlldata', dlldata,
  214. '/iid', iid,
  215. '/proxy', proxy,
  216. idl]
  217. env = self._GetEnv(arch)
  218. popen = subprocess.Popen(args, shell=True, env=env,
  219. stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  220. out, _ = popen.communicate()
  221. # Filter junk out of stdout, and write filtered versions. Output we want
  222. # to filter is pairs of lines that look like this:
  223. # Processing C:\Program Files (x86)\Microsoft SDKs\...\include\objidl.idl
  224. # objidl.idl
  225. lines = out.splitlines()
  226. prefixes = ('Processing ', '64 bit Processing ')
  227. processing = set(os.path.basename(x)
  228. for x in lines if x.startswith(prefixes))
  229. for line in lines:
  230. if not line.startswith(prefixes) and line not in processing:
  231. print line
  232. return popen.returncode
  233. def ExecAsmWrapper(self, arch, *args):
  234. """Filter logo banner from invocations of asm.exe."""
  235. env = self._GetEnv(arch)
  236. popen = subprocess.Popen(args, shell=True, env=env,
  237. stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  238. out, _ = popen.communicate()
  239. for line in out.splitlines():
  240. if (not line.startswith('Copyright (C) Microsoft Corporation') and
  241. not line.startswith('Microsoft (R) Macro Assembler') and
  242. not line.startswith(' Assembling: ') and
  243. line):
  244. print line
  245. return popen.returncode
  246. def ExecRcWrapper(self, arch, *args):
  247. """Filter logo banner from invocations of rc.exe. Older versions of RC
  248. don't support the /nologo flag."""
  249. env = self._GetEnv(arch)
  250. popen = subprocess.Popen(args, shell=True, env=env,
  251. stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  252. out, _ = popen.communicate()
  253. for line in out.splitlines():
  254. if (not line.startswith('Microsoft (R) Windows (R) Resource Compiler') and
  255. not line.startswith('Copyright (C) Microsoft Corporation') and
  256. line):
  257. print line
  258. return popen.returncode
  259. def ExecActionWrapper(self, arch, rspfile, *dir):
  260. """Runs an action command line from a response file using the environment
  261. for |arch|. If |dir| is supplied, use that as the working directory."""
  262. env = self._GetEnv(arch)
  263. # TODO(scottmg): This is a temporary hack to get some specific variables
  264. # through to actions that are set after gyp-time. http://crbug.com/333738.
  265. for k, v in os.environ.iteritems():
  266. if k not in env:
  267. env[k] = v
  268. args = open(rspfile).read()
  269. dir = dir[0] if dir else None
  270. return subprocess.call(args, shell=True, env=env, cwd=dir)
  271. def ExecClCompile(self, project_dir, selected_files):
  272. """Executed by msvs-ninja projects when the 'ClCompile' target is used to
  273. build selected C/C++ files."""
  274. project_dir = os.path.relpath(project_dir, BASE_DIR)
  275. selected_files = selected_files.split(';')
  276. ninja_targets = [os.path.join(project_dir, filename) + '^^'
  277. for filename in selected_files]
  278. cmd = ['ninja.exe']
  279. cmd.extend(ninja_targets)
  280. return subprocess.call(cmd, shell=True, cwd=BASE_DIR)
  281. if __name__ == '__main__':
  282. sys.exit(main(sys.argv[1:]))