xref: /aosp_15_r20/art/tools/veridex/appcompat.py (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1#
2# Copyright (C) 2024 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 subprocess
18import sys
19import pkgutil
20import tempfile
21import stat
22
23def get_resource_path(resource_name, mode=0o644):
24    """Retrieves a resource from the package and writes it to a temporary file.
25
26    Args:
27        resource_name: The name of the resource.
28        mode: File permissions mode (default is 0o644).
29    """
30    data = pkgutil.get_data(__name__, resource_name)
31    if not data:
32        raise FileNotFoundError(f"Resource not found: {resource_name}")
33    with tempfile.NamedTemporaryFile(delete=False) as temp_file:
34        temp_file.write(data)
35        resource_path = temp_file.name
36    os.chmod(resource_path, mode)  # Set permissions
37    return resource_path
38
39def main():
40    print("NOTE: appcompat is still under development. It can report")
41    print("API uses that do not execute at runtime, and reflection uses")
42    print("that do not exist. It can also miss on reflection uses.")
43
44    script_dir = os.path.dirname(os.path.realpath(__file__))
45    veridex_path = get_resource_path("veridex", 0o755)
46    hiddenapi_flags_path = get_resource_path("hiddenapi-flags.csv")
47    system_stubs_path = get_resource_path("system-stubs.zip")
48    http_legacy_stubs_path = get_resource_path("org.apache.http.legacy-stubs.zip")
49
50    args = [
51        veridex_path,
52        f"--core-stubs={system_stubs_path}:{http_legacy_stubs_path}",
53        f"--api-flags={hiddenapi_flags_path}",
54        "--exclude-api-lists=sdk,invalid",
55    ] + sys.argv[1:]
56
57    subprocess.run(args)
58
59if __name__ == "__main__":
60    main()
61