win_tool.py 15 KB

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