run_tests.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #!/usr/bin/env python3
  2. # Copyright 2012-2017 The Meson development team
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. # Unless required by applicable law or agreed to in writing, software
  8. # distributed under the License is distributed on an "AS IS" BASIS,
  9. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. # See the License for the specific language governing permissions and
  11. # limitations under the License.
  12. import os
  13. import sys
  14. import time
  15. import shutil
  16. import subprocess
  17. import tempfile
  18. import platform
  19. from mesonbuild import mesonlib
  20. from mesonbuild import mesonmain
  21. from mesonbuild import mlog
  22. from mesonbuild.environment import detect_ninja
  23. from io import StringIO
  24. from enum import Enum
  25. from glob import glob
  26. Backend = Enum('Backend', 'ninja vs xcode')
  27. if mesonlib.is_windows() or mesonlib.is_cygwin():
  28. exe_suffix = '.exe'
  29. else:
  30. exe_suffix = ''
  31. def get_backend_args_for_dir(backend, builddir):
  32. '''
  33. Visual Studio backend needs to be given the solution to build
  34. '''
  35. if backend is Backend.vs:
  36. sln_name = glob(os.path.join(builddir, '*.sln'))[0]
  37. return [os.path.split(sln_name)[-1]]
  38. return []
  39. def find_vcxproj_with_target(builddir, target):
  40. import re, fnmatch
  41. t, ext = os.path.splitext(target)
  42. if ext:
  43. p = '<TargetName>{}</TargetName>\s*<TargetExt>\{}</TargetExt>'.format(t, ext)
  44. else:
  45. p = '<TargetName>{}</TargetName>'.format(t)
  46. for root, dirs, files in os.walk(builddir):
  47. for f in fnmatch.filter(files, '*.vcxproj'):
  48. f = os.path.join(builddir, f)
  49. with open(f, 'r', encoding='utf-8') as o:
  50. if re.search(p, o.read(), flags=re.MULTILINE):
  51. return f
  52. raise RuntimeError('No vcxproj matching {!r} in {!r}'.format(p, builddir))
  53. def get_builddir_target_args(backend, builddir, target):
  54. dir_args = []
  55. if not target:
  56. dir_args = get_backend_args_for_dir(backend, builddir)
  57. if target is None:
  58. return dir_args
  59. if backend is Backend.vs:
  60. vcxproj = find_vcxproj_with_target(builddir, target)
  61. target_args = [vcxproj]
  62. elif backend is Backend.xcode:
  63. target_args = ['-target', target]
  64. elif backend is Backend.ninja:
  65. target_args = [target]
  66. else:
  67. raise AssertionError('Unknown backend: {!r}'.format(backend))
  68. return target_args + dir_args
  69. def get_backend_commands(backend, debug=False):
  70. install_cmd = []
  71. uninstall_cmd = []
  72. if backend is Backend.vs:
  73. cmd = ['msbuild']
  74. clean_cmd = cmd + ['/target:Clean']
  75. test_cmd = cmd + ['RUN_TESTS.vcxproj']
  76. elif backend is Backend.xcode:
  77. cmd = ['xcodebuild']
  78. clean_cmd = cmd + ['-alltargets', 'clean']
  79. test_cmd = cmd + ['-target', 'RUN_TESTS']
  80. elif backend is Backend.ninja:
  81. # We need at least 1.6 because of -w dupbuild=err
  82. cmd = [detect_ninja('1.6'), '-w', 'dupbuild=err']
  83. if cmd[0] is None:
  84. raise RuntimeError('Could not find Ninja v1.6 or newer')
  85. if debug:
  86. cmd += ['-v']
  87. clean_cmd = cmd + ['clean']
  88. test_cmd = cmd + ['test', 'benchmark']
  89. install_cmd = cmd + ['install']
  90. uninstall_cmd = cmd + ['uninstall']
  91. else:
  92. raise AssertionError('Unknown backend: {!r}'.format(backend))
  93. return cmd, clean_cmd, test_cmd, install_cmd, uninstall_cmd
  94. def ensure_backend_detects_changes(backend):
  95. # This is needed to increase the difference between build.ninja's
  96. # timestamp and the timestamp of whatever you changed due to a Ninja
  97. # bug: https://github.com/ninja-build/ninja/issues/371
  98. if backend is Backend.ninja:
  99. time.sleep(1)
  100. def get_fake_options(prefix):
  101. import argparse
  102. opts = argparse.Namespace()
  103. opts.cross_file = None
  104. opts.wrap_mode = None
  105. opts.prefix = prefix
  106. return opts
  107. def should_run_linux_cross_tests():
  108. return shutil.which('arm-linux-gnueabihf-gcc-6') and not platform.machine().startswith('arm')
  109. def run_configure_inprocess(commandlist):
  110. old_stdout = sys.stdout
  111. sys.stdout = mystdout = StringIO()
  112. old_stderr = sys.stderr
  113. sys.stderr = mystderr = StringIO()
  114. try:
  115. returncode = mesonmain.run(commandlist[0], commandlist[1:])
  116. finally:
  117. sys.stdout = old_stdout
  118. sys.stderr = old_stderr
  119. return returncode, mystdout.getvalue(), mystderr.getvalue()
  120. class FakeEnvironment(object):
  121. def __init__(self):
  122. self.cross_info = None
  123. self.coredata = lambda: None
  124. self.coredata.compilers = {}
  125. def is_cross_build(self):
  126. return False
  127. if __name__ == '__main__':
  128. # Enable coverage early...
  129. enable_coverage = '--cov' in sys.argv
  130. if enable_coverage:
  131. os.makedirs('.coverage', exist_ok=True)
  132. sys.argv.remove('--cov')
  133. import coverage
  134. coverage.process_startup()
  135. returncode = 0
  136. # Iterate over list in reverse order to find the last --backend arg
  137. backend = Backend.ninja
  138. for arg in reversed(sys.argv[1:]):
  139. if arg.startswith('--backend'):
  140. if arg.startswith('--backend=vs'):
  141. backend = Backend.vs
  142. elif arg == '--backend=xcode':
  143. backend = Backend.xcode
  144. break
  145. # Running on a developer machine? Be nice!
  146. if not mesonlib.is_windows() and 'TRAVIS' not in os.environ:
  147. os.nice(20)
  148. # Appveyor sets the `platform` environment variable which completely messes
  149. # up building with the vs2010 and vs2015 backends.
  150. #
  151. # Specifically, MSBuild reads the `platform` environment variable to set
  152. # the configured value for the platform (Win32/x64/arm), which breaks x86
  153. # builds.
  154. #
  155. # Appveyor setting this also breaks our 'native build arch' detection for
  156. # Windows in environment.py:detect_windows_arch() by overwriting the value
  157. # of `platform` set by vcvarsall.bat.
  158. #
  159. # While building for x86, `platform` should be unset.
  160. if 'APPVEYOR' in os.environ and os.environ['arch'] == 'x86':
  161. os.environ.pop('platform')
  162. # Run tests
  163. print(mlog.bold('Running unittests.').get_text(mlog.colorize_console))
  164. print()
  165. units = ['InternalTests', 'AllPlatformTests', 'FailureTests']
  166. if mesonlib.is_linux():
  167. units += ['LinuxlikeTests']
  168. if should_run_linux_cross_tests():
  169. units += ['LinuxArmCrossCompileTests']
  170. elif mesonlib.is_windows():
  171. units += ['WindowsTests']
  172. # Can't pass arguments to unit tests, so set the backend to use in the environment
  173. env = os.environ.copy()
  174. env['MESON_UNIT_TEST_BACKEND'] = backend.name
  175. with tempfile.TemporaryDirectory() as td:
  176. # Enable coverage on all subsequent processes.
  177. if enable_coverage:
  178. with open(os.path.join(td, 'usercustomize.py'), 'w') as f:
  179. f.write('import coverage\n'
  180. 'coverage.process_startup()\n')
  181. env['COVERAGE_PROCESS_START'] = '.coveragerc'
  182. env['PYTHONPATH'] = os.pathsep.join([td] + env.get('PYTHONPATH', []))
  183. returncode += subprocess.call([sys.executable, 'run_unittests.py', '-v'] + units, env=env)
  184. # Ubuntu packages do not have a binary without -6 suffix.
  185. if should_run_linux_cross_tests():
  186. print(mlog.bold('Running cross compilation tests.').get_text(mlog.colorize_console))
  187. print()
  188. returncode += subprocess.call([sys.executable, 'run_cross_test.py', 'cross/ubuntu-armhf.txt'], env=env)
  189. returncode += subprocess.call([sys.executable, 'run_project_tests.py'] + sys.argv[1:], env=env)
  190. sys.exit(returncode)