Index: CMakeLists.txt =================================================================== --- CMakeLists.txt +++ CMakeLists.txt @@ -44,6 +44,8 @@ pythonize_bool(LIBCXX_BUILD_32_BITS) pythonize_bool(LIBCXX_ENABLE_THREADS) pythonize_bool(LIBCXX_ENABLE_MONOTONIC_CLOCK) + set(LIBCXX_EXECUTOR "None" CACHE STRING + "Executor to use when running tests.") set(AUTO_GEN_COMMENT "## Autogenerated by libcxx configuration.\n# Do not edit!") Index: libcxx/test/config.py =================================================================== --- libcxx/test/config.py +++ libcxx/test/config.py @@ -11,7 +11,9 @@ from libcxx.test.format import LibcxxTestFormat from libcxx.compiler import CXXCompiler - +from libcxx.test.executor import * +from libcxx.test.target_info import * +from libcxx.test.tracing import * def loadSiteConfig(lit_config, config, param_name, env_name): # We haven't loaded the site specific configuration (the user is @@ -57,9 +59,6 @@ self.long_tests = None self.execute_external = False - if platform.system() not in ('Darwin', 'FreeBSD', 'Linux'): - self.lit_config.fatal("unrecognized system") - def get_lit_conf(self, name, default=None): val = self.lit_config.params.get(name, None) if val is None: @@ -80,6 +79,8 @@ "parameter '{}' should be true or false".format(name)) def configure(self): + self.configure_executor() + self.configure_target_info() self.configure_cxx() self.configure_triple() self.configure_src_root() @@ -114,8 +115,35 @@ self.cxx, self.use_clang_verify, self.execute_external, + self.executor, exec_env=self.env) + def configure_executor(self): + exec_str = self.get_lit_conf('executor', "None") + te = eval(exec_str) + if te: + self.lit_config.note("inferred executor as: %r" % exec_str) + if self.lit_config.useValgrind: + # We have no way of knowing where in the chain the + # ValgrindExecutor is supposed to go. It is likely + # that the user wants it at the end, but we have no + # way of getting at that easily. + selt.lit_config.fatal("Cannot infer how to create a Valgrind " + " executor.") + else: + te = LocalExecutor() + if self.lit_config.useValgrind: + te = ValgrindExecutor(self.lit_config.valgrindArgs, te) + self.executor = te + + def configure_target_info(self): + info_str = self.get_lit_conf('target_info', None) + if info_str: + self.target_info = eval(info_str) + self.lit_config.note("inferred target_info as: %r" % info_str) + else: + self.target_info = LocalTI() + def configure_cxx(self): # Gather various compiler parameters. cxx = self.get_lit_conf('cxx_under_test') @@ -203,7 +231,7 @@ # Figure out which of the required locales we support locales = { - 'Darwin': { + 'darwin': { 'en_US.UTF-8': 'en_US.UTF-8', 'cs_CZ.ISO8859-2': 'cs_CZ.ISO8859-2', 'fr_FR.UTF-8': 'fr_FR.UTF-8', @@ -211,7 +239,7 @@ 'ru_RU.UTF-8': 'ru_RU.UTF-8', 'zh_CN.UTF-8': 'zh_CN.UTF-8', }, - 'FreeBSD': { + 'freebsd': { 'en_US.UTF-8': 'en_US.UTF-8', 'cs_CZ.ISO8859-2': 'cs_CZ.ISO8859-2', 'fr_FR.UTF-8': 'fr_FR.UTF-8', @@ -219,7 +247,7 @@ 'ru_RU.UTF-8': 'ru_RU.UTF-8', 'zh_CN.UTF-8': 'zh_CN.UTF-8', }, - 'Linux': { + 'linux': { 'en_US.UTF-8': 'en_US.UTF-8', 'cs_CZ.ISO8859-2': 'cs_CZ.ISO-8859-2', 'fr_FR.UTF-8': 'fr_FR.UTF-8', @@ -227,7 +255,7 @@ 'ru_RU.UTF-8': 'ru_RU.UTF-8', 'zh_CN.UTF-8': 'zh_CN.UTF-8', }, - 'Windows': { + 'windows': { 'en_US.UTF-8': 'English_United States.1252', 'cs_CZ.ISO8859-2': 'Czech_Czech Republic.1250', 'fr_FR.UTF-8': 'French_France.1252', @@ -237,16 +265,18 @@ }, } + target_platform = self.target_info.platform() + default_locale = locale.setlocale(locale.LC_ALL) - for feature, loc in locales[platform.system()].items(): - try: - locale.setlocale(locale.LC_ALL, loc) - self.config.available_features.add( - 'locale.{0}'.format(feature)) - except locale.Error: - self.lit_config.warning('The locale {0} is not supported by ' - 'your platform. Some tests will be ' - 'unsupported.'.format(loc)) + if target_platform in locales: + for feature, loc in locales[target_platform].items(): + if self.target_info.supports_locale(loc): + self.config.available_features.add( + 'locale.{0}'.format(feature)) + else: + self.lit_config.warning('The locale {0} is not supported by' + ' your platform. Some tests will be' + ' unsupported.'.format(loc)) locale.setlocale(locale.LC_ALL, default_locale) # Write an "available feature" that combines the triple when @@ -258,20 +288,14 @@ 'with_system_cxx_lib=%s' % self.config.target_triple) # Insert the platform name into the available features as a lower case. - # Strip the '2' from linux2. - if sys.platform.startswith('linux'): - platform_name = 'linux' - else: - platform_name = sys.platform - self.config.available_features.add(platform_name.lower()) + self.config.available_features.add(target_platform) # Some linux distributions have different locale data than others. # Insert the distributions name and name-version into the available # features to allow tests to XFAIL on them. - if sys.platform.startswith('linux'): - name, ver, _ = platform.linux_distribution() - name = name.lower().strip() - ver = ver.lower().strip() + if target_platform == 'linux': + name = self.target_info.platform_name() + ver = self.target_info.platform_ver() if name: self.config.available_features.add(name) if name and ver: @@ -307,7 +331,7 @@ # Configure include paths self.cxx.compile_flags += ['-nostdinc++'] self.configure_compile_flags_header_includes() - if sys.platform.startswith('linux'): + if self.target_info.platform() == 'linux': self.cxx.compile_flags += ['-D__STDC_FORMAT_MACROS', '-D__STDC_LIMIT_MACROS', '-D__STDC_CONSTANT_MACROS'] @@ -439,9 +463,10 @@ def configure_extra_library_flags(self): enable_threads = self.get_lit_bool('enable_threads', True) llvm_unwinder = self.get_lit_bool('llvm_unwinder', False) - if sys.platform == 'darwin': + target_platform = self.target_info.platform() + if target_platform == 'darwin': self.cxx.link_flags += ['-lSystem'] - elif sys.platform.startswith('linux'): + elif target_platform == 'linux': if not llvm_unwinder: self.cxx.link_flags += ['-lgcc_eh'] self.cxx.link_flags += ['-lc', '-lm'] @@ -452,10 +477,11 @@ self.cxx.link_flags += ['-lunwind', '-ldl'] else: self.cxx.link_flags += ['-lgcc_s'] - elif sys.platform.startswith('freebsd'): + elif target_platform.startswith('freebsd'): self.cxx.link_flags += ['-lc', '-lm', '-lpthread', '-lgcc_s'] else: - self.lit_config.fatal("unrecognized system: %r" % sys.platform) + self.lit_config.fatal("unrecognized system: %r" % + target_platform) def configure_warnings(self): enable_warnings = self.get_lit_bool('enable_warnings', False) @@ -480,7 +506,7 @@ symbolizer_search_paths) # Setup the sanitizer compile flags self.cxx.flags += ['-g', '-fno-omit-frame-pointer'] - if sys.platform.startswith('linux'): + if self.target_info.platform() == 'linux': self.cxx.link_flags += ['-ldl'] if san == 'Address': self.cxx.flags += ['-fsanitize=address'] @@ -565,15 +591,16 @@ # linux-gnu is needed in the triple to properly identify linuxes # that use GLIBC. Handle redhat and opensuse triples as special # cases and append the missing `-gnu` portion. - if target_triple.endswith('redhat-linux') or \ - target_triple.endswith('suse-linux'): + if (target_triple.endswith('redhat-linux') or + target_triple.endswith('suse-linux')): target_triple += '-gnu' self.config.target_triple = target_triple self.lit_config.note( "inferred target_triple as: %r" % self.config.target_triple) def configure_env(self): - if sys.platform == 'darwin' and not self.use_system_cxx_lib: + if (self.target_info.platform() == 'darwin' and + not self.use_system_cxx_lib): libcxx_library = self.get_lit_conf('libcxx_library') if libcxx_library: cxx_library_root = os.path.dirname(libcxx_library) Index: libcxx/test/executor.py =================================================================== --- libcxx/test/executor.py +++ libcxx/test/executor.py @@ -0,0 +1,182 @@ +import os + +import tracing + +from lit.util import executeCommand # pylint: disable=import-error + + +class Executor(object): + def run(self, exe_path, cmd, local_cwd, env=None): + """Execute a command. + + Args: + exe_path: str: Local path to the executable to be run + cmd: [str]: subprocess.call style command + local_cwd: str: Local path to the working directory + env: {str: str}: Environment variables to execute under + Returns: + out, err, exitCode + """ + raise NotImplementedError + + +class LocalExecutor(Executor): + def __init__(self): + super(LocalExecutor, self).__init__() + + def run(self, exe_path, cmd=None, work_dir='.', env=None): + cmd = cmd or [exe_path] + env_cmd = [] + if env: + env_cmd += ['env'] + env_cmd += ['%s=%s' % (k, v) for k, v in env.items()] + if work_dir == '.': + work_dir = os.getcwd() + return executeCommand(env_cmd + cmd, cwd=work_dir) + + +class PrefixExecutor(Executor): + """Prefix an executor with some other command wrapper. + + Most useful for setting ulimits on commands, or running an emulator like + qemu and valgrind. + """ + def __init__(self, commandPrefix, chain): + super(PrefixExecutor, self).__init__() + + self.commandPrefix = commandPrefix + self.chain = chain + + def run(self, exe_path, cmd=None, work_dir='.', env=None): + cmd = cmd or [exe_path] + return self.chain.run(self.commandPrefix + cmd, work_dir, env=env) + + +class PostfixExecutor(Executor): + """Postfix an executor with some args.""" + def __init__(self, commandPostfix, chain): + super(PostfixExecutor, self).__init__() + + self.commandPostfix = commandPostfix + self.chain = chain + + def run(self, exe_path, cmd=None, work_dir='.', env=None): + cmd = cmd or [exe_path] + return self.chain.run(cmd + self.commandPostfix, work_dir, env=env) + + + +class TimeoutExecutor(PrefixExecutor): + """Execute another action under a timeout. + + Deprecated. http://reviews.llvm.org/D6584 adds timeouts to LIT. + """ + def __init__(self, duration, chain): + super(TimeoutExecutor, self).__init__( + ['timeout', duration], chain) + + +class SSHExecutor(Executor): + def __init__(self, host, username=None): + super(SSHExecutor, self).__init__() + + self.user_prefix = username + '@' if username else '' + self.host = host + self.scp_command = 'scp' + self.ssh_command = 'ssh' + + # Maps from local->remote to make sure we consistently return + # the same name for a given local version of the same file/dir + self.filemap = {} + self.dirmap = {} + + self.local_run = executeCommand + # TODO(jroelofs): switch this on some -super-verbose-debug config flag + if False: + self.local_run = tracing.trace_function( + self.local_run, log_calls=True, log_results=True, + label='ssh_local') + + def remote_temp_dir(self): + return self._remote_temp(is_dir=True) + + def remote_temp_file(self): + return self._remote_temp(is_dir=False) + + def _remote_temp(self, is_dir): + # TODO: detect what the target system is, and use the correct + # mktemp command for it. (linux and darwin differ here, and I'm + # sure windows has another way to do it) + + # Not sure how to do suffix on osx yet + dir_arg = '-d' if is_dir else '' + cmd = 'mktemp -q {} /tmp/libcxx.XXXXXXXXXX'.format(dir_arg) + temp_path, err, exitCode = self.__execute_command_remote([cmd]) + temp_path = temp_path.strip() + if exitCode != 0: + raise RuntimeError(err) + return temp_path + + def remote_from_local_dir(self, local_dir): + if local_dir not in self.dirmap: + remote_dir = self.remote_temp_dir() + self.dirmap[local_dir] = remote_dir + return self.dirmap[local_dir] + + def remote_from_local_file(self, local_file): + if not local_file in self.filemap: + remote_file = self.remote_temp_file() + self.filemap[local_file] = remote_file + return self.filemap[local_file] + + def copy_in(self, local_srcs, remote_dsts): + scp = self.scp_command + remote = self.host + remote = self.user_prefix + remote + + # This could be wrapped up in a tar->scp->untar for performance + # if there are lots of files to be copied/moved + for src, dst in zip(local_srcs, remote_dsts): + cmd = [scp, '-p', src, remote + ':' + dst] + self.local_run(cmd) + + def delete_remote(self, remote): + try: + self.__execute_command_remote(['rm', '-rf', remote]) + except OSError: + # TODO: Log failure to delete? + pass + + def run(self, exe_path, cmd=None, work_dir='.', env=None): + target_exe_path = None + target_cwd = None + try: + target_exe_path = self.remote_from_local_file(exe_path) + target_cwd = self.remote_from_local_dir(work_dir) + if cmd: + # Replace exe_path with target_exe_path. + cmd = [c if c != exe_path else target_exe_path for c in cmd] + else: + cmd = [target_exe_path] + self.copy_in([exe_path], [target_exe_path]) + return self.__execute_command_remote(cmd, target_cwd, env) + except: + raise + finally: + if target_exe_path: + self.delete_remote(target_exe_path) + if target_cwd: + self.delete_remote(target_cwd) + + def __execute_command_remote(self, cmd, remote_work_dir='.', env=None): + remote = self.user_prefix + self.host + ssh_cmd = [self.ssh_command, '-oBatchMode=yes', remote] + if env: + env_cmd = ['env'] + ['%s=%s' % (k, v) for k, v in env.items()] + else: + env_cmd = [] + remote_cmd = ' '.join(env_cmd + cmd) + if remote_work_dir != '.': + remote_cmd = 'cd ' + remote_work_dir + ' && ' + remote_cmd + return self.local_run(ssh_cmd + [remote_cmd]) + Index: libcxx/test/format.py =================================================================== --- libcxx/test/format.py +++ libcxx/test/format.py @@ -20,10 +20,12 @@ FOO.sh.cpp - A test that uses LIT's ShTest format. """ - def __init__(self, cxx, use_verify_for_fail, execute_external, exec_env): + def __init__(self, cxx, use_verify_for_fail, execute_external, + executor, exec_env): self.cxx = cxx self.use_verify_for_fail = use_verify_for_fail self.execute_external = execute_external + self.executor = executor self.exec_env = dict(exec_env) # TODO: Move this into lit's FileBasedTest @@ -73,6 +75,10 @@ # Dispatch the test based on its suffix. if is_sh_test: + if self.executor: + # We can't run ShTest tests with a executor yet. + # For now, bail on trying to run them + return lit.Test.UNSUPPORTED, 'ShTest format not yet supported' return lit.TestRunner._runShTest(test, lit_config, self.execute_external, script, tmpBase, execDir) @@ -104,15 +110,12 @@ report += "Compilation failed unexpectedly!" return lit.Test.FAIL, report # Run the test - cmd = [] + local_cwd = os.path.dirname(source_path) + env = None if self.exec_env: - cmd += ['env'] - cmd += ['%s=%s' % (k, v) for k, v in self.exec_env.items()] - if lit_config.useValgrind: - cmd = lit_config.valgrindArgs + cmd - cmd += [exec_path] - out, err, rc = lit.util.executeCommand( - cmd, cwd=os.path.dirname(source_path)) + env = self.exec_env + out, err, rc = self.executor.run(exec_path, [exec_path], + local_cwd, env) if rc != 0: report = libcxx.util.makeReport(cmd, out, err, rc) report = "Compiled With: %s\n%s" % (compile_cmd, report) Index: libcxx/test/target_info.py =================================================================== --- libcxx/test/target_info.py +++ libcxx/test/target_info.py @@ -0,0 +1,48 @@ +import platform +import locale + +class TargetInfo(object): + def platform(self): + raise NotImplementedError + + def platform_ver(self): + raise NotImplementedError + + def platform_name(self): + raise NotImplementedError + + def supports_locale(self, loc): + raise NotImplementedError + + +class LocalTI(TargetInfo): + def platform(self): + platform_name = platform.system().lower().strip() + # Strip the '2' from linux2. + if platform_name.startswith('linux'): + platform_name = 'linux' + return platform_name + + def platform_name(self): + if platform() == 'linux': + name, _, _ = platform.linux_distribution() + name = name.lower().strip() + if name: + return name + return None + + def platform_ver(self): + if platform() == 'linux': + _, ver, _ = platform.linux_distribution() + ver = ver.lower().strip() + if ver: + return ver + return None + + def supports_locale(self, loc): + try: + locale.setlocale(locale.LC_ALL, loc) + return True + except locale.Error: + return False + Index: libcxx/test/tracing.py =================================================================== --- libcxx/test/tracing.py +++ libcxx/test/tracing.py @@ -0,0 +1,33 @@ +import inspect + + +def trace_function(function, log_calls, log_results, label=''): + def wrapper(*args, **kwargs): + kwarg_strs = ['{}={}'.format(k, v) for (k, v) in kwargs] + arg_str = ', '.join([str(a) for a in args] + kwarg_strs) + call_str = '{}({})'.format(function.func_name, arg_str) + + # Perform the call itself, logging before, after, and anything thrown. + try: + if log_calls: + print '{}: Calling {}'.format(label, call_str) + res = function(*args, **kwargs) + if log_results: + print '{}: {} -> {}'.format(label, call_str, res) + return res + except Exception as ex: + if log_results: + print '{}: {} raised {}'.format(label, call_str, type(ex)) + raise ex + + return wrapper + + +def trace_object(obj, log_calls, log_results, label=''): + for name, member in inspect.getmembers(obj): + if inspect.ismethod(member): + # Skip meta-functions, decorate everything else + if not member.func_name.startswith('__'): + setattr(obj, name, trace_function(member, log_calls, + log_results, label)) + return obj Index: lit.site.cfg.in =================================================================== --- lit.site.cfg.in +++ lit.site.cfg.in @@ -17,6 +17,7 @@ config.target_triple = "@LIBCXX_TARGET_TRIPLE@" config.sysroot = "@LIBCXX_SYSROOT@" config.gcc_toolchain = "@LIBCXX_GCC_TOOLCHAIN@" +config.executor = "@LIBCXX_EXECUTOR@" # Let the main config do the real work. lit_config.load_config(config, "@LIBCXX_SOURCE_DIR@/test/lit.cfg")