6c05fab288d0bbe50e54ef981e4ecb560b0e4c3c0b82f4c4c3f89cb5c0e9b40dcb33e73e6d8dae59c1db332dbbfb62f34aa4b43011a990c2cdde6433069fa7 15 KB

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