xref: /aosp_15_r20/art/tools/veridex/appcompat.py (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker#
2*795d594fSAndroid Build Coastguard Worker# Copyright (C) 2024 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker#
4*795d594fSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker# You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker#
8*795d594fSAndroid Build Coastguard Worker#      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker#
10*795d594fSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker# limitations under the License.
15*795d594fSAndroid Build Coastguard Worker
16*795d594fSAndroid Build Coastguard Workerimport os
17*795d594fSAndroid Build Coastguard Workerimport subprocess
18*795d594fSAndroid Build Coastguard Workerimport sys
19*795d594fSAndroid Build Coastguard Workerimport pkgutil
20*795d594fSAndroid Build Coastguard Workerimport tempfile
21*795d594fSAndroid Build Coastguard Workerimport stat
22*795d594fSAndroid Build Coastguard Worker
23*795d594fSAndroid Build Coastguard Workerdef get_resource_path(resource_name, mode=0o644):
24*795d594fSAndroid Build Coastguard Worker    """Retrieves a resource from the package and writes it to a temporary file.
25*795d594fSAndroid Build Coastguard Worker
26*795d594fSAndroid Build Coastguard Worker    Args:
27*795d594fSAndroid Build Coastguard Worker        resource_name: The name of the resource.
28*795d594fSAndroid Build Coastguard Worker        mode: File permissions mode (default is 0o644).
29*795d594fSAndroid Build Coastguard Worker    """
30*795d594fSAndroid Build Coastguard Worker    data = pkgutil.get_data(__name__, resource_name)
31*795d594fSAndroid Build Coastguard Worker    if not data:
32*795d594fSAndroid Build Coastguard Worker        raise FileNotFoundError(f"Resource not found: {resource_name}")
33*795d594fSAndroid Build Coastguard Worker    with tempfile.NamedTemporaryFile(delete=False) as temp_file:
34*795d594fSAndroid Build Coastguard Worker        temp_file.write(data)
35*795d594fSAndroid Build Coastguard Worker        resource_path = temp_file.name
36*795d594fSAndroid Build Coastguard Worker    os.chmod(resource_path, mode)  # Set permissions
37*795d594fSAndroid Build Coastguard Worker    return resource_path
38*795d594fSAndroid Build Coastguard Worker
39*795d594fSAndroid Build Coastguard Workerdef main():
40*795d594fSAndroid Build Coastguard Worker    print("NOTE: appcompat is still under development. It can report")
41*795d594fSAndroid Build Coastguard Worker    print("API uses that do not execute at runtime, and reflection uses")
42*795d594fSAndroid Build Coastguard Worker    print("that do not exist. It can also miss on reflection uses.")
43*795d594fSAndroid Build Coastguard Worker
44*795d594fSAndroid Build Coastguard Worker    script_dir = os.path.dirname(os.path.realpath(__file__))
45*795d594fSAndroid Build Coastguard Worker    veridex_path = get_resource_path("veridex", 0o755)
46*795d594fSAndroid Build Coastguard Worker    hiddenapi_flags_path = get_resource_path("hiddenapi-flags.csv")
47*795d594fSAndroid Build Coastguard Worker    system_stubs_path = get_resource_path("system-stubs.zip")
48*795d594fSAndroid Build Coastguard Worker    http_legacy_stubs_path = get_resource_path("org.apache.http.legacy-stubs.zip")
49*795d594fSAndroid Build Coastguard Worker
50*795d594fSAndroid Build Coastguard Worker    args = [
51*795d594fSAndroid Build Coastguard Worker        veridex_path,
52*795d594fSAndroid Build Coastguard Worker        f"--core-stubs={system_stubs_path}:{http_legacy_stubs_path}",
53*795d594fSAndroid Build Coastguard Worker        f"--api-flags={hiddenapi_flags_path}",
54*795d594fSAndroid Build Coastguard Worker        "--exclude-api-lists=sdk,invalid",
55*795d594fSAndroid Build Coastguard Worker    ] + sys.argv[1:]
56*795d594fSAndroid Build Coastguard Worker
57*795d594fSAndroid Build Coastguard Worker    subprocess.run(args)
58*795d594fSAndroid Build Coastguard Worker
59*795d594fSAndroid Build Coastguard Workerif __name__ == "__main__":
60*795d594fSAndroid Build Coastguard Worker    main()
61