[libc++] Allow running .sh.cpp tests with SSHExecutors

This commit adds a script that can be used as an %{exec} substitution
such that .sh.cpp tests can now run on remote hosts when using the
SSHExecutor.
This commit is contained in:
Louis Dionne
2020-03-31 12:09:20 -04:00
parent 015c6cd475
commit 07e462526d
10 changed files with 117 additions and 8 deletions

View File

@@ -9,6 +9,7 @@
// Test that we can include each header in two TU's and link them together.
// FILE_DEPENDENCIES: %t.exe
// RUN: %{cxx} -c %s -o %t.first.o %{flags} %{compile_flags}
// RUN: %{cxx} -c %s -o %t.second.o -DWITH_MAIN %{flags} %{compile_flags}
// RUN: %{cxx} -o %t.exe %t.first.o %t.second.o %{flags} %{link_flags}

View File

@@ -39,6 +39,7 @@
// GCC doesn't support the aligned-allocation flags.
// XFAIL: gcc
// FILE_DEPENDENCIES: %t.exe
// RUN: %{build} -faligned-allocation -fsized-deallocation
// RUN: %{run}
// RUN: %{build} -faligned-allocation -fno-sized-deallocation -DNO_SIZE

View File

@@ -7,6 +7,8 @@
//
//===----------------------------------------------------------------------===//
// FILE_DEPENDENCIES: %t.exe
// RUN: %{build}
// RUN: %{exec} %t.exe "HELLO"

View File

@@ -7,6 +7,8 @@
//
//===----------------------------------------------------------------------===//
// FILE_DEPENDENCIES: %t.exe
// RUN: %{build}
// RUN: %{run}

View File

@@ -8,6 +8,7 @@
// Regression test for PR42676.
// FILE_DEPENDENCIES: %t.exe
// RUN: %{cxx} %{flags} %s -o %t.exe %{compile_flags} %{link_flags} -D_LIBCPP_HIDE_FROM_ABI_PER_TU
// RUN: %{run}

View File

@@ -6,6 +6,7 @@
//
//===----------------------------------------------------------------------===//
// FILE_DEPENDENCIES: %t.exe
// RUN: %{build} -O2
// RUN: %{run}

View File

@@ -13,6 +13,7 @@
// class condition_variable_any;
// FILE_DEPENDENCIES: %t.exe
// RUN: %{build}
// RUN: %{run} 1
// RUN: %{run} 2

View File

@@ -1038,13 +1038,21 @@ class Configuration(object):
# Configure exec prefix substitutions.
# Configure run env substitution.
codesign_ident = self.get_lit_conf('llvm_codesign_identity', '')
run_py = os.path.join(self.libcxx_src_root, 'utils', 'run.py')
env_vars = ' '.join('%s=%s' % (k, pipes.quote(v)) for (k, v) in self.exec_env.items())
exec_str = '%s %s --codesign_identity "%s" --working_directory "%%S" ' \
'--dependencies %%{file_dependencies} --env %s -- ' % \
(pipes.quote(sys.executable), pipes.quote(run_py),
codesign_ident, env_vars)
sub.append(('%{exec}', exec_str))
exec_args = [
'--codesign_identity "{}"'.format(codesign_ident),
'--dependencies %{file_dependencies}',
'--env {}'.format(env_vars)
]
if isinstance(self.executor, SSHExecutor):
exec_args.append('--host {}'.format(self.executor.user_prefix + self.executor.host))
executor = os.path.join(self.libcxx_src_root, 'utils', 'ssh.py')
else:
exec_args.append('--working_directory "%S"')
executor = os.path.join(self.libcxx_src_root, 'utils', 'run.py')
sub.append(('%{exec}', '{} {} {} -- '.format(pipes.quote(sys.executable),
pipes.quote(executor),
' '.join(exec_args))))
sub.append(('%{run}', '%{exec} %t.exe'))
# Configure not program substitutions
not_py = os.path.join(self.libcxx_src_root, 'utils', 'not.py')

View File

@@ -18,6 +18,7 @@ from lit.TestRunner import ParserKind, IntegratedTestKeywordParser \
# pylint: disable=import-error
from libcxx.test.executor import LocalExecutor as LocalExecutor
from libcxx.test.executor import SSHExecutor as SSHExecutor
import libcxx.util
@@ -168,8 +169,9 @@ class LibcxxTestFormat(object):
# Dispatch the test based on its suffix.
if is_sh_test:
if not isinstance(self.executor, LocalExecutor):
# We can't run ShTest tests with a executor yet.
if not isinstance(self.executor, LocalExecutor) and not isinstance(self.executor, SSHExecutor):
# We can't run ShTest tests with other executors than
# LocalExecutor and SSHExecutor yet.
# For now, bail on trying to run them
return lit.Test.UNSUPPORTED, 'ShTest format not yet supported'
test.config.environment = self.executor.merge_environments(os.environ, self.exec_env)

90
libcxx/utils/ssh.py Normal file
View File

@@ -0,0 +1,90 @@
#===----------------------------------------------------------------------===##
#
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#
#===----------------------------------------------------------------------===##
"""
Runs an executable on a remote host.
This is meant to be used as an executor when running the C++ Standard Library
conformance test suite.
"""
import argparse
import os
import subprocess
import sys
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--host', type=str, required=True)
parser.add_argument('--codesign_identity', type=str, required=False)
parser.add_argument('--dependencies', type=str, nargs='*', required=True)
parser.add_argument('--env', type=str, nargs='*', required=True)
(args, remaining) = parser.parse_known_args(sys.argv[1:])
if len(remaining) < 2:
sys.stderr.write('Missing actual commands to run')
exit(1)
remaining = remaining[1:] # Skip the '--'
# HACK:
# If the first argument is a file that ends in `.tmp.exe`, assume it is
# the name of an executable generated by a test file. This allows us to
# do custom processing like codesigning the executable and changing its
# path when running on the remote host. It's possible for there to be no
# such executable, for example in the case of a .sh.cpp test.
exe = None
if os.path.exists(remaining[0]) and remaining[0].endswith('.tmp.exe'):
exe = remaining.pop(0)
# If there's an executable, do any necessary codesigning.
if exe and args.codesign_identity:
rc = subprocess.call(['xcrun', 'codesign', '-f', '-s', args.codesign_identity, exe], env={})
if rc != 0:
sys.stderr.write('Failed to codesign: {}'.format(exe))
return rc
ssh = lambda command: ['ssh', '-oBatchMode=yes', args.host, command]
scp = lambda src, dst: ['scp', '-oBatchMode=yes', '-r', src, '{}:{}'.format(args.host, dst)]
# Create a temporary directory where the test will be run
tmp = subprocess.check_output(ssh('mktemp -d /tmp/libcxx.XXXXXXXXXX')).strip()
# Ensure the test dependencies exist and scp them to the temporary directory.
# Test dependencies can be either files or directories, so the `scp` command
# needs to use `-r`.
for dep in args.dependencies:
if not os.path.exists(dep):
sys.stderr.write('Missing file or directory {} marked as a dependency of a test'.format(dep))
exit(1)
subprocess.call(scp(dep, tmp))
# If there's an executable, change its path to be in the temporary directory.
# We know it has been copied to the remote host when we handled the test
# dependencies above.
if exe:
exe = os.path.join(tmp, os.path.basename(exe))
# If there's an executable, make sure it has 'execute' permissions on the
# remote host. The host that compiled the executable might not have a notion
# of 'executable' permissions.
if exe:
subprocess.call(ssh('chmod +x {}'.format(exe)))
# Execute the command through SSH in the temporary directory, with the
# correct environment.
command = [exe] + remaining if exe else remaining
res = subprocess.call(ssh('cd {} && env -i {} {}'.format(tmp, ' '.join(args.env), ' '.join(command))))
# Remove the temporary directory when we're done.
subprocess.call(ssh('rm -r {}'.format(tmp)))
return res
if __name__ == '__main__':
exit(main())