• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#
2# Copyright (C) 2017 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#
16
17import collections
18import logging
19import os
20
21# The tags in VNDK list:
22# Low-level NDK libraries that can be used by framework and vendor modules.
23LL_NDK = "LLNDK"
24
25# LL-NDK dependencies that vendor modules cannot directly access.
26LL_NDK_PRIVATE = "LLNDK-private"
27
28# Same-process HAL implementation in vendor partition.
29SP_HAL = "SP-HAL"
30
31# Framework libraries that can be used by vendor modules except same-process HAL
32# and its dependencies in vendor partition.
33VNDK = "VNDK-core"
34
35# VNDK dependencies that vendor modules cannot directly access.
36VNDK_PRIVATE = "VNDK-core-private"
37
38# Same-process HAL dependencies in framework.
39VNDK_SP = "VNDK-SP"
40
41# VNDK-SP dependencies that vendor modules cannot directly access.
42VNDK_SP_PRIVATE = "VNDK-SP-private"
43
44# The ABI dump directories. 64-bit comes before 32-bit in order to sequentially
45# search for longest prefix.
46_ABI_NAMES = ("arm64", "arm", "mips64", "mips", "x86_64", "x86")
47
48# The data directory.
49_GOLDEN_DIR = os.path.join("vts", "testcases", "vndk", "golden")
50
51# Regular expression prefix for library name patterns.
52_REGEX_PREFIX = "[regex]"
53
54def LoadDefaultVndkVersion(data_file_path):
55    """Loads the name of the data directory for devices with no VNDK version.
56
57    Args:
58        data_file_path: The path to VTS data directory.
59
60    Returns:
61        A string, the directory name.
62        None if fails to load the name.
63    """
64    try:
65        with open(os.path.join(data_file_path, _GOLDEN_DIR,
66                               "platform_vndk_version.txt"), "r") as f:
67            return f.read().strip()
68    except IOError:
69        logging.error("Cannot load default VNDK version.")
70        return None
71
72
73def GetAbiDumpDirectory(data_file_path, version, binder_bitness, abi_name,
74                        abi_bitness):
75    """Returns the VNDK dump directory on host.
76
77    Args:
78        data_file_path: The path to VTS data directory.
79        version: A string, the VNDK version.
80        binder_bitness: A string or an integer, 32 or 64.
81        abi_name: A string, the ABI of the library dump.
82        abi_bitness: A string or an integer, 32 or 64.
83
84    Returns:
85        A string, the path to the dump directory.
86        None if there is no directory for the version and ABI.
87    """
88    try:
89        abi_dir = next(x for x in _ABI_NAMES if abi_name.startswith(x))
90    except StopIteration:
91        logging.warning("Unknown ABI %s.", abi_name)
92        return None
93
94    version_dir = (version if version else
95                   LoadDefaultVndkVersion(data_file_path))
96    if not version_dir:
97        return None
98
99    dump_dir = os.path.join(
100        data_file_path, _GOLDEN_DIR, version_dir,
101        "binder64" if str(binder_bitness) == "64" else "binder32",
102        abi_dir, "lib64" if str(abi_bitness) == "64" else "lib")
103
104    if not os.path.isdir(dump_dir):
105        logging.warning("%s is not a directory.", dump_dir)
106        return None
107
108    return dump_dir
109
110
111def _LoadVndkLibraryListsFile(vndk_lists, tags, vndk_lib_list_path):
112    """Load VNDK libraries from the file to the specified tuple.
113
114    Args:
115        vndk_lists: The output tuple of lists containing library names.
116        tags: Strings, the tags of the libraries to find.
117        vndk_lib_list_path: The path to load the VNDK library list.
118    """
119
120    lib_sets = collections.defaultdict(set)
121
122    # Load VNDK tags from the list.
123    with open(vndk_lib_list_path) as vndk_lib_list_file:
124        for line in vndk_lib_list_file:
125            # Ignore comments.
126            if line.startswith('#'):
127                continue
128
129            # Split columns.
130            cells = line.split(': ', 1)
131            if len(cells) < 2:
132                continue
133            tag = cells[0]
134            lib_name = cells[1].strip()
135
136            lib_sets[tag].add(lib_name)
137
138    # Compute VNDK-core-private and VNDK-SP-private.
139    private = lib_sets.get('VNDK-private', set())
140
141    lib_sets[LL_NDK_PRIVATE].update(lib_sets[LL_NDK] & private)
142    lib_sets[VNDK_PRIVATE].update(lib_sets[VNDK] & private)
143    lib_sets[VNDK_SP_PRIVATE].update(lib_sets[VNDK_SP] & private)
144
145    lib_sets[LL_NDK].difference_update(private)
146    lib_sets[VNDK].difference_update(private)
147    lib_sets[VNDK_SP].difference_update(private)
148
149    # Update the output entries.
150    for index, tag in enumerate(tags):
151        for lib_name in lib_sets.get(tag, tuple()):
152            if lib_name.startswith(_REGEX_PREFIX):
153                lib_name = lib_name[len(_REGEX_PREFIX):]
154            vndk_lists[index].append(lib_name)
155
156
157def LoadVndkLibraryLists(data_file_path, version, *tags):
158    """Find the VNDK libraries with specific tags.
159
160    Args:
161        data_file_path: The path to VTS data directory.
162        version: A string, the VNDK version.
163        *tags: Strings, the tags of the libraries to find.
164
165    Returns:
166        A tuple of lists containing library names. Each list corresponds to
167        one tag in the argument. For SP-HAL, the returned names are regular
168        expressions.
169        None if the spreadsheet for the version is not found.
170    """
171    version_dir = (version if version else
172                   LoadDefaultVndkVersion(data_file_path))
173    if not version_dir:
174        return None
175
176    vndk_lib_list_path = os.path.join(
177        data_file_path, _GOLDEN_DIR, version_dir, "vndk-lib-list.txt")
178    if not os.path.isfile(vndk_lib_list_path):
179        logging.warning("Cannot load %s.", vndk_lib_list_path)
180        return None
181
182    vndk_lib_extra_list_path = os.path.join(
183        data_file_path, _GOLDEN_DIR, version_dir, "vndk-lib-extra-list.txt")
184    if not os.path.isfile(vndk_lib_extra_list_path):
185        logging.warning("Cannot load %s.", vndk_lib_extra_list_path)
186        return None
187
188    vndk_lists = tuple([] for x in tags)
189
190    _LoadVndkLibraryListsFile(vndk_lists, tags, vndk_lib_list_path)
191    _LoadVndkLibraryListsFile(vndk_lists, tags, vndk_lib_extra_list_path)
192    return vndk_lists
193