PageRenderTime 30ms CodeModel.GetById 41ms RepoModel.GetById 0ms app.codeStats 1ms

/utils/sil-opt-verify-all-modules.py

https://gitlab.com/yenny.prathivi/swift
Python | 180 lines | 146 code | 21 blank | 13 comment | 12 complexity | 44c873210a1ff6bf5abb614c92f18cc8 MD5 | raw file
  1. #!/usr/bin/env python
  2. #===------------------------------------------------------------------------===#
  3. #
  4. # This source file is part of the Swift.org open source project
  5. #
  6. # Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
  7. # Licensed under Apache License v2.0 with Runtime Library Exception
  8. #
  9. # See http://swift.org/LICENSE.txt for license information
  10. # See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
  11. #
  12. #===------------------------------------------------------------------------===#
  13. from __future__ import print_function
  14. import argparse
  15. import glob
  16. import multiprocessing
  17. import os
  18. import shutil
  19. import subprocess
  20. import sys
  21. import tempfile
  22. import pipes
  23. def get_verify_toolchain_modules_commands(toolchain_dir, sil_opt):
  24. if sil_opt is None:
  25. sil_opt = os.path.join(toolchain_dir, 'usr', 'bin', 'sil-opt')
  26. toolchain_basename = os.path.basename(toolchain_dir)
  27. if toolchain_basename.startswith('Legacy'):
  28. return []
  29. if toolchain_basename.startswith('XcodeDefault'):
  30. toolchain_name = 'XcodeDefault'
  31. if toolchain_basename.startswith('tvOS'):
  32. toolchain_name = 'tvOS'
  33. if toolchain_basename.startswith('OSX'):
  34. toolchain_name = 'OSX'
  35. if toolchain_basename.startswith('watchOS'):
  36. toolchain_name = 'watchOS'
  37. if toolchain_basename.startswith('iOS'):
  38. toolchain_name = 'iOS'
  39. return get_verify_resource_dir_modules_commands(
  40. os.path.join(toolchain_dir, 'usr', 'lib', 'swift'),
  41. os.path.join(toolchain_dir, 'usr', 'bin', 'sil-opt'),
  42. toolchain_name)
  43. def get_verify_build_dir_commands(build_dir, toolchain_name='XcodeDefault'):
  44. return get_verify_resource_dir_modules_commands(
  45. os.path.join(build_dir, 'lib', 'swift'),
  46. os.path.join(build_dir, 'bin', 'sil-opt'),
  47. toolchain_name)
  48. def get_verify_resource_dir_modules_commands(
  49. resource_dir, sil_opt, toolchain_name):
  50. print("================================================================")
  51. print("Resource dir: " + resource_dir)
  52. print("sil-opt path: " + sil_opt)
  53. known_platforms = [
  54. ( 'appletvos', 'arm64', 'arm64-apple-tvos9.0' ),
  55. ( 'appletvsimulator', 'x86_64', 'x86_64-apple-tvos9.0' ),
  56. ( 'iphoneos', 'armv7', 'armv7-apple-ios7.0' ),
  57. ( 'iphoneos', 'armv7s', 'armv7s-apple-ios7.0' ),
  58. ( 'iphoneos', 'arm64', 'arm64-apple-ios7.0' ),
  59. ( 'iphonesimulator', 'i386', 'i386-apple-ios7.0' ),
  60. ( 'iphonesimulator', 'x86_64', 'x86_64-apple-ios7.0' ),
  61. ( 'macosx', 'x86_64', 'x86_64-apple-macosx10.9' ),
  62. ( 'watchos', 'armv7k', 'armv7k-apple-watchos2.0' ),
  63. ( 'watchsimulator', 'i386', 'i386-apple-watchos2.0' ),
  64. ]
  65. commands = []
  66. module_cache_dir = tempfile.mkdtemp(prefix="swift-testsuite-clang-module-cache")
  67. for (subdir, arch, triple) in known_platforms:
  68. platform_frameworks_dir = os.path.join(
  69. subprocess.check_output([
  70. 'xcrun', '--toolchain', toolchain_name, '--show-sdk-platform-path'
  71. ]).strip(),
  72. 'Developer', 'Library', 'Frameworks')
  73. modules_dir = os.path.join(resource_dir, subdir, arch)
  74. print(modules_dir)
  75. modules = glob.glob(os.path.join(modules_dir, '*.swiftmodule'))
  76. for module_file_name in modules:
  77. if module_file_name.endswith('XCTest.swiftmodule'):
  78. # FIXME: sil-opt does not have the '-F' option.
  79. continue
  80. commands.append([
  81. 'xcrun', '--toolchain', toolchain_name, '--sdk', subdir,
  82. sil_opt,
  83. '-target', triple,
  84. '-resource-dir', resource_dir,
  85. '-module-cache-path', module_cache_dir,
  86. '-verify',
  87. module_file_name,
  88. ])
  89. return commands
  90. def quote_shell_command(args):
  91. return " ".join([ pipes.quote(a) for a in args ])
  92. def run_commands_in_parallel(commands):
  93. makefile = ".DEFAULT_GOAL := all\n"
  94. targets = []
  95. for c in commands:
  96. target_name = "target" + str(len(targets))
  97. targets.append(target_name)
  98. makefile += target_name + ":\n"
  99. makefile += \
  100. "\t" + quote_shell_command(c) + \
  101. " > {target}.stdout\n".format(target=target_name)
  102. makefile += "all: " + " ".join(targets) + "\n"
  103. temp_dir = tempfile.mkdtemp(prefix="swift-testsuite-main")
  104. makefile_file = open(os.path.join(temp_dir, 'Makefile'), 'w')
  105. makefile_file.write(makefile)
  106. makefile_file.close()
  107. max_processes = multiprocessing.cpu_count()
  108. subprocess.check_call([
  109. 'make',
  110. '-C', temp_dir,
  111. '-j', str(max_processes),
  112. '--keep-going'
  113. ])
  114. def main():
  115. parser = argparse.ArgumentParser(
  116. formatter_class=argparse.RawDescriptionHelpFormatter,
  117. description="""Verifies Swift modules.""")
  118. parser.add_argument("--sil-opt",
  119. help="use the specified 'sil-opt' binary",
  120. metavar="PATH")
  121. parser.add_argument("--verify-build-dir",
  122. help="verify the Swift resource directory under the given build directory",
  123. metavar="PATH")
  124. parser.add_argument("--verify-xcode",
  125. help="verify the Xcode.app that is currently xcode-select'ed",
  126. action="store_true")
  127. args = parser.parse_args()
  128. if args.verify_build_dir is not None and args.verify_xcode:
  129. print("--verify-build-dir and --verify-xcode can't be used together")
  130. return 1
  131. if args.verify_build_dir is not None:
  132. commands = get_verify_build_dir_commands(args.verify_build_dir)
  133. if args.verify_xcode:
  134. # Find Xcode.
  135. swift_path = subprocess.check_output([ 'xcrun', '--find', 'swift' ])
  136. xcode_path = swift_path
  137. for i in range(0, 7):
  138. xcode_path = os.path.dirname(xcode_path)
  139. toolchains_dir = os.path.join(
  140. xcode_path, 'Contents', 'Developer', 'Toolchains')
  141. toolchains = glob.glob(os.path.join(toolchains_dir, '*.xctoolchain'))
  142. commands = []
  143. for toolchain_dir in toolchains:
  144. commands += get_verify_toolchain_modules_commands(
  145. toolchain_dir, args.sil_opt)
  146. run_commands_in_parallel(commands)
  147. return 0
  148. if __name__ == "__main__":
  149. sys.exit(main())