1 import os 2 3 import lit.util # pylint: disable=import-error 4 5 import libcxx.test.config 6 import libcxx.test.target_info 7 import libcxx.android.build 8 import libcxx.ndk.test.format 9 10 11 class AndroidTargetInfo(libcxx.test.target_info.DefaultTargetInfo): 12 def platform(self): 13 return 'android' 14 15 def system(self): 16 raise NotImplementedError 17 18 def add_cxx_compile_flags(self, flags): 19 flags.extend(['-D__STDC_FORMAT_MACROS']) 20 21 def platform_ver(self): 22 raise NotImplementedError 23 24 def platform_name(self): 25 raise NotImplementedError 26 27 def supports_locale(self, loc): 28 raise NotImplementedError 29 30 31 class Configuration(libcxx.test.config.Configuration): 32 def __init__(self, lit_config, config): 33 super(Configuration, self).__init__(lit_config, config) 34 self.cxx_under_test = None 35 self.build_cmds_dir = None 36 self.cxx_template = None 37 self.link_template = None 38 self.with_availability = False 39 40 def configure(self): 41 self.configure_target_info() 42 self.configure_cxx() 43 self.configure_triple() 44 self.configure_src_root() 45 self.configure_obj_root() 46 self.configure_cxx_stdlib_under_test() 47 self.configure_cxx_library_root() 48 self.configure_compile_flags() 49 self.configure_link_flags() 50 self.configure_features() 51 52 def configure_target_info(self): 53 self.target_info = AndroidTargetInfo(self) 54 55 def configure_compile_flags(self): 56 super(Configuration, self).configure_compile_flags() 57 58 unified_headers = self.get_lit_bool('unified_headers') 59 arch = self.get_lit_conf('arch') 60 api = self.get_lit_conf('api') 61 62 sysroot_path = 'platforms/android-{}/arch-{}'.format(api, arch) 63 platform_sysroot = os.path.join(os.environ['NDK'], sysroot_path) 64 if unified_headers: 65 sysroot = os.path.join(os.environ['NDK'], 'sysroot') 66 self.cxx.compile_flags.extend(['--sysroot', sysroot]) 67 68 triple = self.get_lit_conf('target_triple') 69 header_triple = triple.rstrip('0123456789') 70 header_triple = header_triple.replace('armv7', 'arm') 71 arch_includes = os.path.join(sysroot, 'usr/include', header_triple) 72 self.cxx.compile_flags.extend(['-isystem', arch_includes]) 73 74 self.cxx.compile_flags.append('-D__ANDROID_API__={}'.format(api)) 75 76 self.cxx.link_flags.extend(['--sysroot', platform_sysroot]) 77 else: 78 self.cxx.flags.extend(['--sysroot', platform_sysroot]) 79 80 android_support_headers = os.path.join( 81 os.environ['NDK'], 'sources/android/support/include') 82 self.cxx.compile_flags.append('-I' + android_support_headers) 83 84 def configure_link_flags(self): 85 self.cxx.link_flags.append('-nodefaultlibs') 86 87 # Configure libc++ library paths. 88 self.cxx.link_flags.append('-L' + self.cxx_library_root) 89 90 gcc_toolchain = self.get_lit_conf('gcc_toolchain') 91 self.cxx.link_flags.append('-gcc-toolchain') 92 self.cxx.link_flags.append(gcc_toolchain) 93 94 self.cxx.link_flags.append('-landroid_support') 95 triple = self.get_lit_conf('target_triple') 96 if triple.startswith('arm-') or triple.startswith('armv7-'): 97 self.cxx.link_flags.append('-lunwind') 98 self.cxx.link_flags.append('-Wl,--exclude-libs,libunwind.a') 99 100 self.cxx.link_flags.append('-latomic') 101 self.cxx.link_flags.append('-Wl,--exclude-libs,libatomic.a') 102 103 self.cxx.link_flags.append('-lgcc') 104 self.cxx.link_flags.append('-Wl,--exclude-libs,libgcc.a') 105 106 self.cxx.link_flags.append('-lc++_shared') 107 self.cxx.link_flags.append('-lc') 108 self.cxx.link_flags.append('-lm') 109 self.cxx.link_flags.append('-ldl') 110 if self.get_lit_bool('use_pie'): 111 self.cxx.link_flags.append('-pie') 112 113 def configure_features(self): 114 self.config.available_features.add(self.get_lit_conf('std')) 115 self.config.available_features.add('long_tests') 116 117 def get_test_format(self): 118 # Note that we require that the caller has cleaned this directory, 119 # ensured its existence, and copied libc++_shared.so into it. 120 tmp_dir = getattr(self.config, 'device_dir', '/data/local/tmp/libcxx') 121 build_only = self.get_lit_conf('build_only', False) 122 build_dir = self.get_lit_conf('build_dir') 123 124 return libcxx.ndk.test.format.TestFormat( 125 self.cxx, 126 self.libcxx_src_root, 127 self.libcxx_obj_root, 128 build_dir, 129 tmp_dir, 130 getattr(self.config, 'timeout', '300'), 131 exec_env={'LD_LIBRARY_PATH': tmp_dir}, 132 build_only=build_only) 133