"Fossies" - the Fresh Open Source Software Archive

Member "node-v12.18.4-win-x86/node_modules/npm/node_modules/node-gyp/gyp/pylib/gyp/win_tool.py" (18 Feb 2020, 13357 Bytes) of package /windows/www/node-v12.18.4-win-x86.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

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