Skip to content

Commit 07e4625

Browse files
committed
[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.
1 parent 015c6cd commit 07e4625

File tree

10 files changed

+117
-8
lines changed

10 files changed

+117
-8
lines changed

libcxx/test/libcxx/double_include.sh.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
// Test that we can include each header in two TU's and link them together.
1111

12+
// FILE_DEPENDENCIES: %t.exe
1213
// RUN: %{cxx} -c %s -o %t.first.o %{flags} %{compile_flags}
1314
// RUN: %{cxx} -c %s -o %t.second.o -DWITH_MAIN %{flags} %{compile_flags}
1415
// RUN: %{cxx} -o %t.exe %t.first.o %t.second.o %{flags} %{link_flags}

libcxx/test/libcxx/language.support/support.dynamic/libcpp_deallocate.sh.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
// GCC doesn't support the aligned-allocation flags.
4040
// XFAIL: gcc
4141

42+
// FILE_DEPENDENCIES: %t.exe
4243
// RUN: %{build} -faligned-allocation -fsized-deallocation
4344
// RUN: %{run}
4445
// RUN: %{build} -faligned-allocation -fno-sized-deallocation -DNO_SIZE

libcxx/test/libcxx/selftest/exec.sh.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
//
88
//===----------------------------------------------------------------------===//
99

10+
// FILE_DEPENDENCIES: %t.exe
11+
1012
// RUN: %{build}
1113
// RUN: %{exec} %t.exe "HELLO"
1214

libcxx/test/libcxx/selftest/test.sh.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
//
88
//===----------------------------------------------------------------------===//
99

10+
// FILE_DEPENDENCIES: %t.exe
11+
1012
// RUN: %{build}
1113
// RUN: %{run}
1214

libcxx/test/libcxx/strings/basic.string/PR42676.sh.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
// Regression test for PR42676.
1010

11+
// FILE_DEPENDENCIES: %t.exe
1112
// RUN: %{cxx} %{flags} %s -o %t.exe %{compile_flags} %{link_flags} -D_LIBCPP_HIDE_FROM_ABI_PER_TU
1213
// RUN: %{run}
1314

libcxx/test/std/containers/associative/map/PR28469_undefined_behavior_segfault.sh.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9+
// FILE_DEPENDENCIES: %t.exe
910
// RUN: %{build} -O2
1011
// RUN: %{run}
1112

libcxx/test/std/thread/thread.condition/thread.condition.condvarany/wait_terminates.sh.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
// class condition_variable_any;
1515

16+
// FILE_DEPENDENCIES: %t.exe
1617
// RUN: %{build}
1718
// RUN: %{run} 1
1819
// RUN: %{run} 2

libcxx/utils/libcxx/test/config.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,13 +1038,21 @@ def configure_substitutions(self):
10381038
# Configure exec prefix substitutions.
10391039
# Configure run env substitution.
10401040
codesign_ident = self.get_lit_conf('llvm_codesign_identity', '')
1041-
run_py = os.path.join(self.libcxx_src_root, 'utils', 'run.py')
10421041
env_vars = ' '.join('%s=%s' % (k, pipes.quote(v)) for (k, v) in self.exec_env.items())
1043-
exec_str = '%s %s --codesign_identity "%s" --working_directory "%%S" ' \
1044-
'--dependencies %%{file_dependencies} --env %s -- ' % \
1045-
(pipes.quote(sys.executable), pipes.quote(run_py),
1046-
codesign_ident, env_vars)
1047-
sub.append(('%{exec}', exec_str))
1042+
exec_args = [
1043+
'--codesign_identity "{}"'.format(codesign_ident),
1044+
'--dependencies %{file_dependencies}',
1045+
'--env {}'.format(env_vars)
1046+
]
1047+
if isinstance(self.executor, SSHExecutor):
1048+
exec_args.append('--host {}'.format(self.executor.user_prefix + self.executor.host))
1049+
executor = os.path.join(self.libcxx_src_root, 'utils', 'ssh.py')
1050+
else:
1051+
exec_args.append('--working_directory "%S"')
1052+
executor = os.path.join(self.libcxx_src_root, 'utils', 'run.py')
1053+
sub.append(('%{exec}', '{} {} {} -- '.format(pipes.quote(sys.executable),
1054+
pipes.quote(executor),
1055+
' '.join(exec_args))))
10481056
sub.append(('%{run}', '%{exec} %t.exe'))
10491057
# Configure not program substitutions
10501058
not_py = os.path.join(self.libcxx_src_root, 'utils', 'not.py')

libcxx/utils/libcxx/test/format.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
# pylint: disable=import-error
1919

2020
from libcxx.test.executor import LocalExecutor as LocalExecutor
21+
from libcxx.test.executor import SSHExecutor as SSHExecutor
2122
import libcxx.util
2223

2324

@@ -168,8 +169,9 @@ def _execute(self, test, lit_config):
168169

169170
# Dispatch the test based on its suffix.
170171
if is_sh_test:
171-
if not isinstance(self.executor, LocalExecutor):
172-
# We can't run ShTest tests with a executor yet.
172+
if not isinstance(self.executor, LocalExecutor) and not isinstance(self.executor, SSHExecutor):
173+
# We can't run ShTest tests with other executors than
174+
# LocalExecutor and SSHExecutor yet.
173175
# For now, bail on trying to run them
174176
return lit.Test.UNSUPPORTED, 'ShTest format not yet supported'
175177
test.config.environment = self.executor.merge_environments(os.environ, self.exec_env)

libcxx/utils/ssh.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#===----------------------------------------------------------------------===##
2+
#
3+
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
# See https://llvm.org/LICENSE.txt for license information.
5+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
#
7+
#===----------------------------------------------------------------------===##
8+
9+
"""
10+
Runs an executable on a remote host.
11+
12+
This is meant to be used as an executor when running the C++ Standard Library
13+
conformance test suite.
14+
"""
15+
16+
import argparse
17+
import os
18+
import subprocess
19+
import sys
20+
21+
22+
def main():
23+
parser = argparse.ArgumentParser()
24+
parser.add_argument('--host', type=str, required=True)
25+
parser.add_argument('--codesign_identity', type=str, required=False)
26+
parser.add_argument('--dependencies', type=str, nargs='*', required=True)
27+
parser.add_argument('--env', type=str, nargs='*', required=True)
28+
(args, remaining) = parser.parse_known_args(sys.argv[1:])
29+
30+
if len(remaining) < 2:
31+
sys.stderr.write('Missing actual commands to run')
32+
exit(1)
33+
remaining = remaining[1:] # Skip the '--'
34+
35+
# HACK:
36+
# If the first argument is a file that ends in `.tmp.exe`, assume it is
37+
# the name of an executable generated by a test file. This allows us to
38+
# do custom processing like codesigning the executable and changing its
39+
# path when running on the remote host. It's possible for there to be no
40+
# such executable, for example in the case of a .sh.cpp test.
41+
exe = None
42+
if os.path.exists(remaining[0]) and remaining[0].endswith('.tmp.exe'):
43+
exe = remaining.pop(0)
44+
45+
# If there's an executable, do any necessary codesigning.
46+
if exe and args.codesign_identity:
47+
rc = subprocess.call(['xcrun', 'codesign', '-f', '-s', args.codesign_identity, exe], env={})
48+
if rc != 0:
49+
sys.stderr.write('Failed to codesign: {}'.format(exe))
50+
return rc
51+
52+
ssh = lambda command: ['ssh', '-oBatchMode=yes', args.host, command]
53+
scp = lambda src, dst: ['scp', '-oBatchMode=yes', '-r', src, '{}:{}'.format(args.host, dst)]
54+
55+
# Create a temporary directory where the test will be run
56+
tmp = subprocess.check_output(ssh('mktemp -d /tmp/libcxx.XXXXXXXXXX')).strip()
57+
58+
# Ensure the test dependencies exist and scp them to the temporary directory.
59+
# Test dependencies can be either files or directories, so the `scp` command
60+
# needs to use `-r`.
61+
for dep in args.dependencies:
62+
if not os.path.exists(dep):
63+
sys.stderr.write('Missing file or directory {} marked as a dependency of a test'.format(dep))
64+
exit(1)
65+
subprocess.call(scp(dep, tmp))
66+
67+
# If there's an executable, change its path to be in the temporary directory.
68+
# We know it has been copied to the remote host when we handled the test
69+
# dependencies above.
70+
if exe:
71+
exe = os.path.join(tmp, os.path.basename(exe))
72+
73+
# If there's an executable, make sure it has 'execute' permissions on the
74+
# remote host. The host that compiled the executable might not have a notion
75+
# of 'executable' permissions.
76+
if exe:
77+
subprocess.call(ssh('chmod +x {}'.format(exe)))
78+
79+
# Execute the command through SSH in the temporary directory, with the
80+
# correct environment.
81+
command = [exe] + remaining if exe else remaining
82+
res = subprocess.call(ssh('cd {} && env -i {} {}'.format(tmp, ' '.join(args.env), ' '.join(command))))
83+
84+
# Remove the temporary directory when we're done.
85+
subprocess.call(ssh('rm -r {}'.format(tmp)))
86+
87+
return res
88+
89+
if __name__ == '__main__':
90+
exit(main())

0 commit comments

Comments
 (0)