1#
2# Copyright (C) 2015 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16import os
17import shutil
18import site
19import subprocess
20import tempfile
21
22site.addsitedir(os.path.join(os.environ['NDK'], 'build/lib'))
23
24import build_support  # pylint: disable=import-error
25
26
27def make_standalone_toolchain(arch, platform, toolchain, install_dir):
28    ndk_dir = os.environ['NDK']
29    make_standalone_toolchain_path = os.path.join(
30        ndk_dir, 'build/tools/make-standalone-toolchain.sh')
31
32    cmd = [make_standalone_toolchain_path, '--install-dir=' + install_dir]
33
34    if arch is not None:
35        cmd.append('--arch=' + arch)
36    if platform is not None:
37        cmd.append('--platform=' + platform)
38
39    if toolchain is not None:
40        toolchain_triple = build_support.arch_to_toolchain(arch)
41        name = '{}-{}'.format(toolchain_triple, toolchain)
42        cmd.append('--toolchain=' + name)
43
44    subprocess.check_call(cmd)
45
46
47def test_standalone_toolchain(arch, toolchain, install_dir):
48    if toolchain == '4.9':
49        triple = build_support.arch_to_triple(arch)
50        # x86 toolchain names are dumb: http://b/25800583
51        if arch == 'x86':
52            triple = 'i686-linux-android'
53        compiler_name = triple + '-g++'
54    elif toolchain == 'clang':
55        compiler_name = 'clang++'
56    else:
57        raise ValueError
58
59    compiler = os.path.join(install_dir, 'bin', compiler_name)
60    test_source = 'foo.cpp'
61    proc = subprocess.Popen([compiler, '-shared', test_source],
62                            stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
63    out, _ = proc.communicate()
64    return proc.returncode == 0, out
65
66
67def run_test(abi=None, platform=None, toolchain=None,
68             build_flags=None):  # pylint: disable=unused-argument
69    arch = 'arm'
70    if abi is not None:
71        arch = build_support.abi_to_arch(abi)
72
73    install_dir = tempfile.mkdtemp()
74    try:
75        make_standalone_toolchain(arch, platform, toolchain, install_dir)
76        return test_standalone_toolchain(arch, toolchain, install_dir)
77    finally:
78        shutil.rmtree(install_dir)
79