[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.
Cr-Mirrored-From: https://chromium.googlesource.com/external/github.com/llvm/llvm-project
Cr-Mirrored-Commit: 07e462526d0cbae40b320e1a4307ce11e197fb0a
diff --git a/utils/libcxx/test/config.py b/utils/libcxx/test/config.py
index b593707..9c154ba 100644
--- a/utils/libcxx/test/config.py
+++ b/utils/libcxx/test/config.py
@@ -1038,13 +1038,21 @@
# 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')
diff --git a/utils/libcxx/test/format.py b/utils/libcxx/test/format.py
index a9eb7d9..ff23273 100644
--- a/utils/libcxx/test/format.py
+++ b/utils/libcxx/test/format.py
@@ -18,6 +18,7 @@
# 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 @@
# 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)
diff --git a/utils/ssh.py b/utils/ssh.py
new file mode 100644
index 0000000..20acaeb
--- /dev/null
+++ b/utils/ssh.py
@@ -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())