summaryrefslogtreecommitdiffstats
path: root/vndk/tools/header-checker/utils/utils.py
blob: fd414132544650c6795601c202fe1ffbd2799f07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/env python3

import tempfile
import os
import subprocess

SCRIPT_DIR = os.path.abspath(os.path.dirname(__file__))
AOSP_DIR = os.path.abspath(os.path.join(SCRIPT_DIR, *['..'] * 5))

BUILTIN_HEADERS_DIR = (
    os.path.join(AOSP_DIR, 'bionic', 'libc', 'include'),
    os.path.join(AOSP_DIR, 'external', 'libcxx', 'include'),
    os.path.join(AOSP_DIR, 'prebuilts', 'sdk', 'renderscript', 'clang-include'),
)

EXPORTED_HEADERS_DIR = (
    os.path.join(AOSP_DIR, 'development', 'vndk', 'tools', 'header-checker',
                 'tests'),
)

SOURCE_ABI_DUMP_EXT = ".so.lsdump"

TARGET_ARCHS = ['arm', 'arm64', 'x86', 'x86_64', 'mips', 'mips64']

def get_reference_dump_dir(reference_dump_dir_stem,
                            reference_dump_dir_insertion, lib_arch):
    reference_dump_dir = os.path.join(reference_dump_dir_stem, lib_arch)
    reference_dump_dir = os.path.join(reference_dump_dir,
                                      reference_dump_dir_insertion)
    return reference_dump_dir

def copy_reference_dump(lib_path, reference_dump_dir_stem,
                        reference_dump_dir_insertion, lib_arch):
    if lib_path is None:
        return 0
    reference_dump_dir = get_reference_dump_dir(reference_dump_dir_stem,
                                                reference_dump_dir_insertion,
                                                lib_arch)
    reference_dump_path = os.path.join(reference_dump_dir,
                                       os.path.basename(lib_path))
    os.makedirs(os.path.dirname(reference_dump_path), exist_ok=True)
    output_content = read_output_content(lib_path, AOSP_DIR)
    with open(reference_dump_path, 'w') as f:
        f.write(output_content)
    print('Created abi dump at ', reference_dump_path)
    return 1

def copy_reference_dump_content(lib_name, output_content,
                                reference_dump_dir_stem,
                                reference_dump_dir_insertion, lib_arch):
    reference_dump_dir = get_reference_dump_dir(reference_dump_dir_stem,
                                                reference_dump_dir_insertion,
                                                lib_arch)
    reference_dump_path = os.path.join(reference_dump_dir,
                                       lib_name + SOURCE_ABI_DUMP_EXT)
    os.makedirs(os.path.dirname(reference_dump_path), exist_ok=True)
    with open(reference_dump_path, 'w') as f:
        f.write(output_content)
    print('Created abi dump at ', reference_dump_path)
    return 1

def read_output_content(output_path, replace_str):
    with open(output_path, 'r') as f:
        return f.read().replace(replace_str, '')

def run_header_abi_dumper(input_path, remove_absolute_paths, cflags=[],
                          export_include_dirs = EXPORTED_HEADERS_DIR):
    with tempfile.TemporaryDirectory() as tmp:
        output_path = os.path.join(tmp, os.path.basename(input_path)) + '.dump'
        run_header_abi_dumper_on_file(input_path, output_path,
                                      export_include_dirs, cflags)
        with open(output_path, 'r') as f:
            if remove_absolute_paths:
                return read_output_content(output_path, AOSP_DIR)
            else:
                return f.read()

def run_header_abi_dumper_on_file(input_path, output_path,
                                  export_include_dirs = [], cflags =[]):
    cmd = ['header-abi-dumper', '-o', output_path, input_path,]
    for dir in export_include_dirs:
        cmd += ['-I', dir]
    cmd += ['--']
    cmd += cflags
    for dir in BUILTIN_HEADERS_DIR:
        cmd += ['-isystem', dir]
    # export includes imply local includes
    for dir in export_include_dirs:
        cmd += ['-I', dir]
    subprocess.check_call(cmd)

def run_header_abi_linker(output_path, inputs, version_script, api, arch):
    """Link inputs, taking version_script into account"""
    with tempfile.TemporaryDirectory() as tmp:
        cmd = ['header-abi-linker', '-o', output_path, '-v', version_script,
               '-api', api, '-arch', arch]
        cmd += inputs
        subprocess.check_call(cmd)
        with open(output_path, 'r') as f:
            return read_output_content(output_path, AOSP_DIR)

def make_library(lib_name):
    # Create reference dumps for integration tests
    make_cmd = ['make', '-j', lib_name]
    subprocess.check_call(make_cmd, cwd=AOSP_DIR)

def find_lib_lsdump(lib_name, target_arch, target_arch_variant,
                    target_cpu_variant):
    """ Find the lsdump corresponding to lib_name for the given arch parameters
        if it exists"""
    assert 'ANDROID_PRODUCT_OUT' in os.environ
    cpu_variant = '_' + target_cpu_variant
    arch_variant = '_' + target_arch_variant

    if target_cpu_variant == 'generic' or target_cpu_variant is None or\
        target_cpu_variant == '':
        cpu_variant = ''
    if target_arch_variant == target_arch or target_arch_variant is None or\
        target_arch_variant == '':
        arch_variant = ''

    target_dir = 'android_' + target_arch + arch_variant +\
    cpu_variant + '_shared_core'
    soong_dir = os.path.join(AOSP_DIR, 'out', 'soong', '.intermediates')
    expected_lsdump_name = lib_name + SOURCE_ABI_DUMP_EXT
    for base, dirnames, filenames in os.walk(soong_dir):
        for filename in filenames:
            if filename == expected_lsdump_name:
                path = os.path.join(base, filename)
                if target_dir in os.path.dirname(path):
                    return path
    return None

def run_abi_diff(old_test_dump_path, new_test_dump_path, arch, lib_name,
                 flags=[]):
    abi_diff_cmd = ['header-abi-diff', '-new', new_test_dump_path, '-old',
                     old_test_dump_path, '-arch', arch, '-lib', lib_name]
    with tempfile.TemporaryDirectory() as tmp:
        output_name = os.path.join(tmp, lib_name) + '.abidiff'
        abi_diff_cmd += ['-o', output_name]
        abi_diff_cmd += flags
        try:
            subprocess.check_call(abi_diff_cmd)
        except subprocess.CalledProcessError as err:
            return err.returncode

    return 0


def get_build_var(name):
    """Get build system variable for the launched target."""
    if 'ANDROID_PRODUCT_OUT' not in os.environ:
        return None

    cmd = ['build/soong/soong_ui.bash', '--dumpvar-mode', name]

    proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
                            stderr=subprocess.PIPE, cwd=AOSP_DIR)
    out, err = proc.communicate()
    return out.decode('utf-8').strip()