1import os
2
3import lit.util  # pylint: disable=import-error
4
5from libcxx.android.executors import AdbExecutor
6from libcxx.test.executor import LocalExecutor, TimeoutExecutor
7import libcxx.test.format
8import libcxx.android.adb as adb
9
10
11class HostTestFormat(libcxx.test.format.LibcxxTestFormat):
12    # pylint: disable=super-init-not-called
13    def __init__(self, cxx, libcxx_src_root, libcxx_obj_root, timeout,
14                 exec_env=None):
15        self.cxx = cxx
16        self.libcxx_src_root = libcxx_src_root
17        self.libcxx_obj_root = libcxx_obj_root
18        self.use_verify_for_fail = False
19        self.executor = TimeoutExecutor(timeout, LocalExecutor())
20
21        # We need to use LD_LIBRARY_PATH because the build system's rpath is
22        # relative, which won't work since we're running from /tmp. We can
23        # either scan `cxx_under_test`/`link_template` to determine whether
24        # we're 32-bit or 64-bit, scan testconfig.mk, or just add both
25        # directories and let the linker sort it out. I'm choosing the lazy
26        # option.
27        outdir = os.getenv('ANDROID_HOST_OUT')
28        libpath = os.pathsep.join([
29            os.path.join(outdir, 'lib'),
30            os.path.join(outdir, 'lib64'),
31        ])
32        default_env = {'LD_LIBRARY_PATH': libpath}
33        self.exec_env = default_env if exec_env is None else exec_env
34
35
36class TestFormat(HostTestFormat):
37    def __init__(self, cxx, libcxx_src_root, libcxx_obj_root, device_dir,
38                 timeout, exec_env=None):
39        HostTestFormat.__init__(
40            self,
41            cxx,
42            libcxx_src_root,
43            libcxx_obj_root,
44            timeout,
45            exec_env)
46        self.device_dir = device_dir
47        self.executor = TimeoutExecutor(timeout, AdbExecutor())
48
49    def _working_directory(self, file_name):
50        return os.path.join(self.device_dir, file_name)
51
52    def _wd_path(self, test_name, file_name):
53        return os.path.join(self._working_directory(test_name), file_name)
54
55    def _build(self, exec_path, source_path, compile_only=False,
56               use_verify=False):
57        # pylint: disable=protected-access
58        cmd, report, rc = libcxx.test.format.LibcxxTestFormat._build(
59            self, exec_path, source_path, compile_only, use_verify)
60        if rc != 0:
61            return cmd, report, rc
62
63        try:
64            exec_file = os.path.basename(exec_path)
65
66            adb.mkdir(self._working_directory(exec_file))
67            adb.push(exec_path, self._wd_path(exec_file, exec_file))
68
69            # Push any .dat files in the same directory as the source to the
70            # working directory.
71            src_dir = os.path.dirname(source_path)
72            data_files = [f for f in os.listdir(src_dir) if f.endswith('.dat')]
73            for data_file in data_files:
74                df_path = os.path.join(src_dir, data_file)
75                df_dev_path = self._wd_path(exec_file, data_file)
76                adb.push(df_path, df_dev_path)
77            return cmd, report, rc
78        except adb.AdbError as ex:
79            return self._make_report(ex.cmd, ex.out, ex.err, ex.exit_code)
80
81    def _clean(self, exec_path):
82        exec_file = os.path.basename(exec_path)
83        cmd = ['adb', 'shell', 'rm', '-rf', self._working_directory(exec_file)]
84        lit.util.executeCommand(cmd)
85        try:
86            os.remove(exec_path)
87        except OSError:
88            pass
89
90    def _run(self, exec_path, _, in_dir=None):
91        raise NotImplementedError()
92