PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/build/win/copy_cdb_to_output.py

https://github.com/chromium/chromium
Python | 127 lines | 95 code | 12 blank | 20 comment | 9 complexity | 2254be1ac4f923767e98c0511a59e3d8 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, Apache-2.0, BSD-3-Clause
  1. #!/usr/bin/env python
  2. # Copyright 2016 The Chromium Authors. 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. from __future__ import print_function
  6. import glob
  7. import hashlib
  8. import os
  9. import shutil
  10. import sys
  11. script_dir = os.path.dirname(os.path.realpath(__file__))
  12. src_build_dir = os.path.abspath(os.path.join(script_dir, os.pardir))
  13. sys.path.insert(0, src_build_dir)
  14. import vs_toolchain
  15. def _HexDigest(file_name):
  16. hasher = hashlib.sha256()
  17. afile = open(file_name, 'rb')
  18. blocksize = 65536
  19. buf = afile.read(blocksize)
  20. while len(buf) > 0:
  21. hasher.update(buf)
  22. buf = afile.read(blocksize)
  23. afile.close()
  24. return hasher.hexdigest()
  25. def _CopyImpl(file_name, target_dir, source_dir, verbose=False):
  26. """Copy |source| to |target| if it doesn't already exist or if it
  27. needs to be updated.
  28. """
  29. target = os.path.join(target_dir, file_name)
  30. source = os.path.join(source_dir, file_name)
  31. if (os.path.isdir(os.path.dirname(target)) and
  32. ((not os.path.isfile(target)) or
  33. _HexDigest(source) != _HexDigest(target))):
  34. if verbose:
  35. print('Copying %s to %s...' % (source, target))
  36. if os.path.exists(target):
  37. os.unlink(target)
  38. shutil.copy(source, target)
  39. def _ConditionalMkdir(output_dir):
  40. if not os.path.isdir(output_dir):
  41. os.makedirs(output_dir)
  42. def _CopyCDBToOutput(output_dir, target_arch):
  43. """Copies the Windows debugging executable cdb.exe to the output
  44. directory, which is created if it does not exist. The output
  45. directory, and target architecture that should be copied, are
  46. passed. Supported values for the target architecture are the GYP
  47. values "ia32", "x64", "arm64" and the GN values "x86", "x64", "arm64".
  48. """
  49. _ConditionalMkdir(output_dir)
  50. vs_toolchain.SetEnvironmentAndGetRuntimeDllDirs()
  51. # If WINDOWSSDKDIR is not set use the default SDK path. This will be the case
  52. # when DEPOT_TOOLS_WIN_TOOLCHAIN=0 and vcvarsall.bat has not been run.
  53. win_sdk_dir = os.path.normpath(
  54. os.environ.get('WINDOWSSDKDIR',
  55. os.path.expandvars('%ProgramFiles(x86)%'
  56. '\\Windows Kits\\10')))
  57. if target_arch == 'ia32' or target_arch == 'x86':
  58. src_arch = 'x86'
  59. elif target_arch in ['x64', 'arm64']:
  60. src_arch = target_arch
  61. else:
  62. print('copy_cdb_to_output.py: unknown target_arch %s' % target_arch)
  63. sys.exit(1)
  64. # We need to copy multiple files, so cache the computed source directory.
  65. src_dir = os.path.join(win_sdk_dir, 'Debuggers', src_arch)
  66. # We need to copy some helper DLLs to get access to the !uniqstack
  67. # command to dump all threads' stacks.
  68. src_winext_dir = os.path.join(src_dir, 'winext')
  69. dst_winext_dir = os.path.join(output_dir, 'winext')
  70. src_winxp_dir = os.path.join(src_dir, 'winxp')
  71. dst_winxp_dir = os.path.join(output_dir, 'winxp')
  72. # Starting with the 10.0.17763 SDK the ucrt files are in a version-named
  73. # directory - this handles both cases.
  74. redist_dir = os.path.join(win_sdk_dir, 'Redist')
  75. version_dirs = glob.glob(os.path.join(redist_dir, '10.*'))
  76. if len(version_dirs) > 0:
  77. version_dirs.sort(reverse=True)
  78. redist_dir = version_dirs[0]
  79. src_crt_dir = os.path.join(redist_dir, 'ucrt', 'DLLs', src_arch)
  80. _ConditionalMkdir(dst_winext_dir)
  81. _ConditionalMkdir(dst_winxp_dir)
  82. # Note that the outputs from the "copy_cdb_to_output" target need to
  83. # be kept in sync with this list.
  84. _CopyImpl('cdb.exe', output_dir, src_dir)
  85. _CopyImpl('dbgeng.dll', output_dir, src_dir)
  86. _CopyImpl('dbghelp.dll', output_dir, src_dir)
  87. _CopyImpl('dbgmodel.dll', output_dir, src_dir)
  88. _CopyImpl('ext.dll', dst_winext_dir, src_winext_dir)
  89. _CopyImpl('uext.dll', dst_winext_dir, src_winext_dir)
  90. _CopyImpl('exts.dll', dst_winxp_dir, src_winxp_dir)
  91. _CopyImpl('ntsdexts.dll', dst_winxp_dir, src_winxp_dir)
  92. if src_arch in ['x64', 'x86']:
  93. # Copy all UCRT files from the debuggers directory, for compatibility with
  94. # the Windows 10 18362 SDK (one UCRT file) and later versions (two UCRT
  95. # files). The new file is api-ms-win-downlevel-kernel32-l2-1-0.dll and
  96. # should be added to the copy_cdb_to_output outputs when we require a newer
  97. # SDK.
  98. for file in glob.glob(os.path.join(src_dir, 'api-ms-win*.dll')):
  99. _CopyImpl(os.path.split(file)[1], output_dir, src_dir)
  100. _CopyImpl('ucrtbase.dll', output_dir, src_crt_dir)
  101. for dll_path in glob.glob(os.path.join(src_crt_dir, 'api-ms-win-*.dll')):
  102. _CopyImpl(os.path.split(dll_path)[1], output_dir, src_crt_dir)
  103. return 0
  104. def main():
  105. if len(sys.argv) < 2:
  106. print('Usage: copy_cdb_to_output.py <output_dir> ' + \
  107. '<target_arch>', file=sys.stderr)
  108. return 1
  109. return _CopyCDBToOutput(sys.argv[1], sys.argv[2])
  110. if __name__ == '__main__':
  111. sys.exit(main())