1#!/usr/bin/env python3 2 3import tempfile 4import os 5import subprocess 6import gzip 7import shutil 8import time 9 10SCRIPT_DIR = os.path.abspath(os.path.dirname(__file__)) 11AOSP_DIR = os.getenv('ANDROID_BUILD_TOP') 12 13BUILTIN_HEADERS_DIR = ( 14 os.path.join(AOSP_DIR, 'bionic', 'libc', 'include'), 15 os.path.join(AOSP_DIR, 'external', 'libcxx', 'include'), 16 os.path.join(AOSP_DIR, 'prebuilts', 'sdk', 'renderscript', 'clang-include'), 17) 18 19EXPORTED_HEADERS_DIR = ( 20 os.path.join(AOSP_DIR, 'development', 'vndk', 'tools', 'header-checker', 21 'tests'), 22) 23 24SO_EXT = '.so' 25SOURCE_ABI_DUMP_EXT_END = '.lsdump' 26SOURCE_ABI_DUMP_EXT = SO_EXT + SOURCE_ABI_DUMP_EXT_END 27COMPRESSED_SOURCE_ABI_DUMP_EXT = SOURCE_ABI_DUMP_EXT + '.gz' 28VENDOR_SUFFIX = '.vendor' 29 30DEFAULT_CPPFLAGS = ['-x', 'c++', '-std=c++11'] 31DEFAULT_CFLAGS = ['-std=gnu99'] 32 33TARGET_ARCHS = ['arm', 'arm64', 'x86', 'x86_64', 'mips', 'mips64'] 34 35def get_reference_dump_dir(reference_dump_dir_stem, 36 reference_dump_dir_insertion, lib_arch): 37 reference_dump_dir = os.path.join(reference_dump_dir_stem, lib_arch) 38 reference_dump_dir = os.path.join(reference_dump_dir, 39 reference_dump_dir_insertion) 40 return reference_dump_dir 41 42 43def copy_reference_dumps(lib_paths, reference_dir_stem, 44 reference_dump_dir_insertion, lib_arch): 45 reference_dump_dir = get_reference_dump_dir(reference_dir_stem, 46 reference_dump_dir_insertion, 47 lib_arch) 48 num_created = 0 49 for lib_path in lib_paths: 50 copy_reference_dump(lib_path, reference_dump_dir) 51 num_created += 1 52 return num_created 53 54def copy_reference_dump(lib_path, reference_dump_dir): 55 reference_dump_path = os.path.join(reference_dump_dir, 56 os.path.basename(lib_path)) + '.gz' 57 os.makedirs(os.path.dirname(reference_dump_path), exist_ok=True) 58 output_content = read_output_content(lib_path, AOSP_DIR) 59 with gzip.open(reference_dump_path, 'wb') as f: 60 f.write(bytes(output_content, 'utf-8')) 61 print('Created abi dump at ', reference_dump_path) 62 return reference_dump_path 63 64def copy_reference_dump_content(lib_name, output_content, 65 reference_dump_dir_stem, 66 reference_dump_dir_insertion, lib_arch): 67 reference_dump_dir = get_reference_dump_dir(reference_dump_dir_stem, 68 reference_dump_dir_insertion, 69 lib_arch) 70 reference_dump_path = os.path.join(reference_dump_dir, 71 lib_name + SOURCE_ABI_DUMP_EXT) 72 os.makedirs(os.path.dirname(reference_dump_path), exist_ok=True) 73 with open(reference_dump_path, 'w') as f: 74 f.write(output_content) 75 76 print('Created abi dump at ', reference_dump_path) 77 return reference_dump_path 78 79def read_output_content(output_path, replace_str): 80 with open(output_path, 'r') as f: 81 return f.read().replace(replace_str, '') 82 83def run_header_abi_dumper(input_path, remove_absolute_paths, cflags=[], 84 export_include_dirs = EXPORTED_HEADERS_DIR): 85 with tempfile.TemporaryDirectory() as tmp: 86 output_path = os.path.join(tmp, os.path.basename(input_path)) + '.dump' 87 run_header_abi_dumper_on_file(input_path, output_path, 88 export_include_dirs, cflags) 89 with open(output_path, 'r') as f: 90 if remove_absolute_paths: 91 return read_output_content(output_path, AOSP_DIR) 92 else: 93 return f.read() 94 95def run_header_abi_dumper_on_file(input_path, output_path, 96 export_include_dirs = [], cflags =[]): 97 input_name, input_ext = os.path.splitext(input_path) 98 cmd = ['header-abi-dumper', '-o', output_path, input_path,] 99 for dir in export_include_dirs: 100 cmd += ['-I', dir] 101 cmd += ['--'] 102 cmd += cflags 103 if input_ext == '.cpp' or input_ext == '.cc' or input_ext == '.h': 104 cmd += DEFAULT_CPPFLAGS 105 else: 106 cmd += DEFAULT_CFLAGS 107 108 for dir in BUILTIN_HEADERS_DIR: 109 cmd += ['-isystem', dir] 110 # export includes imply local includes 111 for dir in export_include_dirs: 112 cmd += ['-I', dir] 113 subprocess.check_call(cmd) 114 115def run_header_abi_linker(output_path, inputs, version_script, api, arch): 116 """Link inputs, taking version_script into account""" 117 with tempfile.TemporaryDirectory() as tmp: 118 cmd = ['header-abi-linker', '-o', output_path, '-v', version_script, 119 '-api', api, '-arch', arch] 120 cmd += inputs 121 subprocess.check_call(cmd) 122 with open(output_path, 'r') as f: 123 return read_output_content(output_path, AOSP_DIR) 124 125def make_tree(product): 126 # To aid creation of reference dumps. 127 make_cmd = ['build/soong/soong_ui.bash', '--make-mode', '-j', 128 'vndk', 'findlsdumps', 'TARGET_PRODUCT=' + product] 129 subprocess.check_call(make_cmd, cwd=AOSP_DIR) 130 131def make_targets(targets, product): 132 make_cmd = ['build/soong/soong_ui.bash', '--make-mode', '-j'] 133 for target in targets: 134 make_cmd.append(target) 135 make_cmd.append('TARGET_PRODUCT=' + product) 136 subprocess.check_call(make_cmd, cwd=AOSP_DIR, stdout=subprocess.DEVNULL, 137 stderr=subprocess.STDOUT) 138 139def make_libraries(libs, product, llndk_mode): 140 # To aid creation of reference dumps. Makes lib.vendor for the current 141 # configuration. 142 lib_targets = [] 143 for lib in libs: 144 lib = lib if llndk_mode else lib + VENDOR_SUFFIX 145 lib_targets.append(lib) 146 make_targets(lib_targets, product) 147 148def find_lib_lsdumps(target_arch, target_arch_variant, 149 target_cpu_variant, lsdump_paths, 150 core_or_vendor_shared_str, libs): 151 """ Find the lsdump corresponding to lib_name for the given arch parameters 152 if it exists""" 153 assert 'ANDROID_PRODUCT_OUT' in os.environ 154 cpu_variant = '_' + target_cpu_variant 155 arch_variant = '_' + target_arch_variant 156 arch_lsdump_paths = [] 157 if target_cpu_variant == 'generic' or target_cpu_variant is None or\ 158 target_cpu_variant == '': 159 cpu_variant = '' 160 if target_arch_variant == target_arch or target_arch_variant is None or\ 161 target_arch_variant == '': 162 arch_variant = '' 163 164 target_dir = 'android_' + target_arch + arch_variant +\ 165 cpu_variant + core_or_vendor_shared_str 166 for key, value in lsdump_paths.items(): 167 if libs and key not in libs: 168 continue 169 for path in lsdump_paths[key]: 170 if target_dir in path: 171 arch_lsdump_paths.append(os.path.join(AOSP_DIR, path.strip())) 172 return arch_lsdump_paths 173 174def run_abi_diff(old_test_dump_path, new_test_dump_path, arch, lib_name, 175 flags=[]): 176 abi_diff_cmd = ['header-abi-diff', '-new', new_test_dump_path, '-old', 177 old_test_dump_path, '-arch', arch, '-lib', lib_name] 178 with tempfile.TemporaryDirectory() as tmp: 179 output_name = os.path.join(tmp, lib_name) + '.abidiff' 180 abi_diff_cmd += ['-o', output_name] 181 abi_diff_cmd += flags 182 try: 183 subprocess.check_call(abi_diff_cmd) 184 except subprocess.CalledProcessError as err: 185 return err.returncode 186 187 return 0 188 189 190def get_build_vars_for_product(names, product=None): 191 build_vars_list = [] 192 """ Get build system variable for the launched target.""" 193 if product is None and 'ANDROID_PRODUCT_OUT' not in os.environ: 194 return None 195 cmd = '' 196 if product is not None: 197 cmd += 'source build/envsetup.sh>/dev/null && lunch>/dev/null ' + product + '&&' 198 cmd += ' build/soong/soong_ui.bash --dumpvars-mode -vars \"' 199 for name in names: 200 cmd += name + ' ' 201 cmd += '\"' 202 203 proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, 204 stderr=subprocess.DEVNULL, cwd=AOSP_DIR, shell=True) 205 out, err = proc.communicate() 206 207 build_vars = out.decode('utf-8').strip().split('\n') 208 for build_var in build_vars: 209 key, _, value = build_var.partition('=') 210 build_vars_list.append(value.replace('\'', '')) 211 return build_vars_list 212