xref: /aosp_15_r20/tools/asuite/aidegen/lib/native_util.py (revision c2e18aaa1096c836b086f94603d04f4eb9cf37f5)
1*c2e18aaaSAndroid Build Coastguard Worker#!/usr/bin/env python3
2*c2e18aaaSAndroid Build Coastguard Worker#
3*c2e18aaaSAndroid Build Coastguard Worker# Copyright 2019 - The Android Open Source Project
4*c2e18aaaSAndroid Build Coastguard Worker#
5*c2e18aaaSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
6*c2e18aaaSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
7*c2e18aaaSAndroid Build Coastguard Worker# You may obtain a copy of the License at
8*c2e18aaaSAndroid Build Coastguard Worker#
9*c2e18aaaSAndroid Build Coastguard Worker#     http://www.apache.org/licenses/LICENSE-2.0
10*c2e18aaaSAndroid Build Coastguard Worker#
11*c2e18aaaSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
12*c2e18aaaSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
13*c2e18aaaSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14*c2e18aaaSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
15*c2e18aaaSAndroid Build Coastguard Worker# limitations under the License.
16*c2e18aaaSAndroid Build Coastguard Worker
17*c2e18aaaSAndroid Build Coastguard Worker"""native_util
18*c2e18aaaSAndroid Build Coastguard Worker
19*c2e18aaaSAndroid Build Coastguard WorkerThis module has a collection of functions that provide helper functions for
20*c2e18aaaSAndroid Build Coastguard Workerlaunching native projects in IDE.
21*c2e18aaaSAndroid Build Coastguard Worker"""
22*c2e18aaaSAndroid Build Coastguard Worker
23*c2e18aaaSAndroid Build Coastguard Workerimport os
24*c2e18aaaSAndroid Build Coastguard Worker
25*c2e18aaaSAndroid Build Coastguard Workerfrom aidegen import constant
26*c2e18aaaSAndroid Build Coastguard Workerfrom aidegen.lib import clion_project_file_gen
27*c2e18aaaSAndroid Build Coastguard Workerfrom aidegen.lib import common_util
28*c2e18aaaSAndroid Build Coastguard Workerfrom aidegen.lib import native_module_info
29*c2e18aaaSAndroid Build Coastguard Workerfrom aidegen.lib import project_info
30*c2e18aaaSAndroid Build Coastguard Worker
31*c2e18aaaSAndroid Build Coastguard Worker_RUST_JSON_NOT_EXIST = 'The json file: {} does not exist.'
32*c2e18aaaSAndroid Build Coastguard Worker_RUST_DICT_BROKEN = 'The rust dictionary does not have "{}" key. It\'s broken.'
33*c2e18aaaSAndroid Build Coastguard Worker_CRATES_KEY = 'crates'
34*c2e18aaaSAndroid Build Coastguard Worker_ROOT_MODULE = 'root_module'
35*c2e18aaaSAndroid Build Coastguard Worker_DISPLAY_NAME = 'display_name'
36*c2e18aaaSAndroid Build Coastguard Worker
37*c2e18aaaSAndroid Build Coastguard Worker
38*c2e18aaaSAndroid Build Coastguard Workerdef generate_clion_projects(targets):
39*c2e18aaaSAndroid Build Coastguard Worker    """Generates CLion projects by targets.
40*c2e18aaaSAndroid Build Coastguard Worker
41*c2e18aaaSAndroid Build Coastguard Worker    Generates base CLion project file in the common parent directory of the
42*c2e18aaaSAndroid Build Coastguard Worker    targets.
43*c2e18aaaSAndroid Build Coastguard Worker
44*c2e18aaaSAndroid Build Coastguard Worker    Usages:
45*c2e18aaaSAndroid Build Coastguard Worker        >>>aidegen frameworks/native/libs/ui frameworks/native/lib/gui
46*c2e18aaaSAndroid Build Coastguard Worker        the base will be generated in,
47*c2e18aaaSAndroid Build Coastguard Worker            out/development/ide/clion/frameworks/native/libs/
48*c2e18aaaSAndroid Build Coastguard Worker        >>>aidegen frameworks/native/libs/ui art/libnativeloader
49*c2e18aaaSAndroid Build Coastguard Worker        the base will be generated in,
50*c2e18aaaSAndroid Build Coastguard Worker            out/development/ide/clion/
51*c2e18aaaSAndroid Build Coastguard Worker        but we expect normally native devs rarely use it in this way.
52*c2e18aaaSAndroid Build Coastguard Worker
53*c2e18aaaSAndroid Build Coastguard Worker    Args:
54*c2e18aaaSAndroid Build Coastguard Worker        targets: A list of targets to check and generate their native projects.
55*c2e18aaaSAndroid Build Coastguard Worker
56*c2e18aaaSAndroid Build Coastguard Worker    Returns:
57*c2e18aaaSAndroid Build Coastguard Worker        A symbolic link CLion project file path.
58*c2e18aaaSAndroid Build Coastguard Worker    """
59*c2e18aaaSAndroid Build Coastguard Worker    cc_module_info = native_module_info.NativeModuleInfo()
60*c2e18aaaSAndroid Build Coastguard Worker    parent_dir, targets = _get_merged_native_target(cc_module_info, targets)
61*c2e18aaaSAndroid Build Coastguard Worker    rel_path = os.path.relpath(parent_dir, common_util.get_android_root_dir())
62*c2e18aaaSAndroid Build Coastguard Worker    # If the relative path is Android root, we won't show '.' in the path.
63*c2e18aaaSAndroid Build Coastguard Worker    if rel_path == '.':
64*c2e18aaaSAndroid Build Coastguard Worker        rel_path = ''
65*c2e18aaaSAndroid Build Coastguard Worker    module_names = []
66*c2e18aaaSAndroid Build Coastguard Worker    for target in targets:
67*c2e18aaaSAndroid Build Coastguard Worker        mod_info = cc_module_info.get_module_info(target)
68*c2e18aaaSAndroid Build Coastguard Worker        clion_gen = clion_project_file_gen.CLionProjectFileGenerator(
69*c2e18aaaSAndroid Build Coastguard Worker            mod_info, rel_path)
70*c2e18aaaSAndroid Build Coastguard Worker        clion_gen.generate_cmakelists_file()
71*c2e18aaaSAndroid Build Coastguard Worker        module_names.append(mod_info[constant.KEY_MODULE_NAME])
72*c2e18aaaSAndroid Build Coastguard Worker    return clion_project_file_gen.generate_base_cmakelists_file(
73*c2e18aaaSAndroid Build Coastguard Worker        cc_module_info, rel_path, module_names)
74*c2e18aaaSAndroid Build Coastguard Worker
75*c2e18aaaSAndroid Build Coastguard Worker
76*c2e18aaaSAndroid Build Coastguard Workerdef _find_parent(abs_path, current_parent):
77*c2e18aaaSAndroid Build Coastguard Worker    """Finds parent directory of two directories.
78*c2e18aaaSAndroid Build Coastguard Worker
79*c2e18aaaSAndroid Build Coastguard Worker    Args:
80*c2e18aaaSAndroid Build Coastguard Worker        abs_path: A string of an absolute path of a directory.
81*c2e18aaaSAndroid Build Coastguard Worker        current_parent: A string of the absolute path of current parent
82*c2e18aaaSAndroid Build Coastguard Worker                        directory. It could be None int the beginning.
83*c2e18aaaSAndroid Build Coastguard Worker
84*c2e18aaaSAndroid Build Coastguard Worker    Returns:
85*c2e18aaaSAndroid Build Coastguard Worker        A string of new parent directory.
86*c2e18aaaSAndroid Build Coastguard Worker    """
87*c2e18aaaSAndroid Build Coastguard Worker    if not current_parent:
88*c2e18aaaSAndroid Build Coastguard Worker        return abs_path
89*c2e18aaaSAndroid Build Coastguard Worker    if common_util.is_source_under_relative_path(abs_path, current_parent):
90*c2e18aaaSAndroid Build Coastguard Worker        return current_parent
91*c2e18aaaSAndroid Build Coastguard Worker    if common_util.is_source_under_relative_path(current_parent, abs_path):
92*c2e18aaaSAndroid Build Coastguard Worker        return abs_path
93*c2e18aaaSAndroid Build Coastguard Worker    return _find_parent(
94*c2e18aaaSAndroid Build Coastguard Worker        os.path.dirname(abs_path), os.path.dirname(current_parent))
95*c2e18aaaSAndroid Build Coastguard Worker
96*c2e18aaaSAndroid Build Coastguard Worker
97*c2e18aaaSAndroid Build Coastguard Workerdef _filter_out_modules(targets, filter_func):
98*c2e18aaaSAndroid Build Coastguard Worker    """Filters out target from targets if it passes the filter function.
99*c2e18aaaSAndroid Build Coastguard Worker
100*c2e18aaaSAndroid Build Coastguard Worker    Args:
101*c2e18aaaSAndroid Build Coastguard Worker        targets: A list of targets to be analyzed.
102*c2e18aaaSAndroid Build Coastguard Worker        filter_func: A filter function reference.
103*c2e18aaaSAndroid Build Coastguard Worker
104*c2e18aaaSAndroid Build Coastguard Worker    Returns:
105*c2e18aaaSAndroid Build Coastguard Worker        A tuple of a list of filtered module target and a list of lefted
106*c2e18aaaSAndroid Build Coastguard Worker        targets.
107*c2e18aaaSAndroid Build Coastguard Worker    """
108*c2e18aaaSAndroid Build Coastguard Worker    jtargets = []
109*c2e18aaaSAndroid Build Coastguard Worker    lefts = []
110*c2e18aaaSAndroid Build Coastguard Worker    for target in targets:
111*c2e18aaaSAndroid Build Coastguard Worker        if filter_func(target):
112*c2e18aaaSAndroid Build Coastguard Worker            jtargets.append(target)
113*c2e18aaaSAndroid Build Coastguard Worker            continue
114*c2e18aaaSAndroid Build Coastguard Worker        lefts.append(target)
115*c2e18aaaSAndroid Build Coastguard Worker    return jtargets, lefts
116*c2e18aaaSAndroid Build Coastguard Worker
117*c2e18aaaSAndroid Build Coastguard Worker
118*c2e18aaaSAndroid Build Coastguard Workerdef _get_merged_native_target(cc_module_info, targets):
119*c2e18aaaSAndroid Build Coastguard Worker    """Gets merged native parent target from original native targets.
120*c2e18aaaSAndroid Build Coastguard Worker
121*c2e18aaaSAndroid Build Coastguard Worker    If a target is a module, we put it directly into the new list. If a target
122*c2e18aaaSAndroid Build Coastguard Worker    is a path we put all the native modules under the path into the new list.
123*c2e18aaaSAndroid Build Coastguard Worker
124*c2e18aaaSAndroid Build Coastguard Worker    Args:
125*c2e18aaaSAndroid Build Coastguard Worker        cc_module_info: A ModuleInfo instance contains the data of
126*c2e18aaaSAndroid Build Coastguard Worker                        module_bp_cc_deps.json.
127*c2e18aaaSAndroid Build Coastguard Worker        targets: A list of targets to be merged.
128*c2e18aaaSAndroid Build Coastguard Worker
129*c2e18aaaSAndroid Build Coastguard Worker    Returns:
130*c2e18aaaSAndroid Build Coastguard Worker        A tuple of a string of merged native project's relative path and a list
131*c2e18aaaSAndroid Build Coastguard Worker        of new targets we have dealt with.
132*c2e18aaaSAndroid Build Coastguard Worker    """
133*c2e18aaaSAndroid Build Coastguard Worker    parent_folder = None
134*c2e18aaaSAndroid Build Coastguard Worker    new_targets = []
135*c2e18aaaSAndroid Build Coastguard Worker    for target in targets:
136*c2e18aaaSAndroid Build Coastguard Worker        _, abs_path = common_util.get_related_paths(cc_module_info, target)
137*c2e18aaaSAndroid Build Coastguard Worker        parent_folder = _find_parent(abs_path, parent_folder)
138*c2e18aaaSAndroid Build Coastguard Worker        if cc_module_info.is_module(target):
139*c2e18aaaSAndroid Build Coastguard Worker            new_targets.append(target)
140*c2e18aaaSAndroid Build Coastguard Worker            continue
141*c2e18aaaSAndroid Build Coastguard Worker        mod_names = cc_module_info.get_module_names_in_targets_paths([target])
142*c2e18aaaSAndroid Build Coastguard Worker        new_targets.extend(mod_names)
143*c2e18aaaSAndroid Build Coastguard Worker    return parent_folder, new_targets
144*c2e18aaaSAndroid Build Coastguard Worker
145*c2e18aaaSAndroid Build Coastguard Worker
146*c2e18aaaSAndroid Build Coastguard Workerdef get_java_cc_and_rust_projects(atest_module_info, cc_module_info, targets):
147*c2e18aaaSAndroid Build Coastguard Worker    """Gets native and java projects from targets.
148*c2e18aaaSAndroid Build Coastguard Worker
149*c2e18aaaSAndroid Build Coastguard Worker    Separates native and java projects from targets.
150*c2e18aaaSAndroid Build Coastguard Worker    1. If it's a native module, add it to native projects.
151*c2e18aaaSAndroid Build Coastguard Worker    2. If it's a java module, add it to java projects.
152*c2e18aaaSAndroid Build Coastguard Worker    3. If it's a rust module, add it to rust targets.
153*c2e18aaaSAndroid Build Coastguard Worker
154*c2e18aaaSAndroid Build Coastguard Worker    Args:
155*c2e18aaaSAndroid Build Coastguard Worker        atest_module_info: A ModuleInfo instance contains the merged data of
156*c2e18aaaSAndroid Build Coastguard Worker                           module-info.json and module_bp_java_deps.json.
157*c2e18aaaSAndroid Build Coastguard Worker        cc_module_info: A ModuleInfo instance contains the data of
158*c2e18aaaSAndroid Build Coastguard Worker                        module_bp_cc_deps.json.
159*c2e18aaaSAndroid Build Coastguard Worker        targets: A list of targets to be analyzed.
160*c2e18aaaSAndroid Build Coastguard Worker
161*c2e18aaaSAndroid Build Coastguard Worker    Returns:
162*c2e18aaaSAndroid Build Coastguard Worker        A tuple of a list of java build targets, a list of C/C++ build
163*c2e18aaaSAndroid Build Coastguard Worker        targets and a list of rust build targets.
164*c2e18aaaSAndroid Build Coastguard Worker    """
165*c2e18aaaSAndroid Build Coastguard Worker    rtargets = _filter_out_rust_projects(targets)
166*c2e18aaaSAndroid Build Coastguard Worker    ctargets, lefts = _filter_out_modules(targets, cc_module_info.is_module)
167*c2e18aaaSAndroid Build Coastguard Worker    jtargets, lefts = _filter_out_modules(lefts, atest_module_info.is_module)
168*c2e18aaaSAndroid Build Coastguard Worker    path_info = cc_module_info.path_to_module_info
169*c2e18aaaSAndroid Build Coastguard Worker    jtars, ctars = _analyze_native_and_java_projects(
170*c2e18aaaSAndroid Build Coastguard Worker        atest_module_info, path_info, lefts)
171*c2e18aaaSAndroid Build Coastguard Worker    ctargets.extend(ctars)
172*c2e18aaaSAndroid Build Coastguard Worker    jtargets.extend(jtars)
173*c2e18aaaSAndroid Build Coastguard Worker    return jtargets, ctargets, rtargets
174*c2e18aaaSAndroid Build Coastguard Worker
175*c2e18aaaSAndroid Build Coastguard Worker
176*c2e18aaaSAndroid Build Coastguard Workerdef _analyze_native_and_java_projects(atest_module_info, path_info, targets):
177*c2e18aaaSAndroid Build Coastguard Worker    """Analyzes native and java projects from targets.
178*c2e18aaaSAndroid Build Coastguard Worker
179*c2e18aaaSAndroid Build Coastguard Worker    Args:
180*c2e18aaaSAndroid Build Coastguard Worker        atest_module_info: A ModuleInfo instance contains the merged data of
181*c2e18aaaSAndroid Build Coastguard Worker                           module-info.json and module_bp_java_deps.json.
182*c2e18aaaSAndroid Build Coastguard Worker        path_info: A dictionary contains C/C++ projects' path as key
183*c2e18aaaSAndroid Build Coastguard Worker                   and module's info dictionary as value.
184*c2e18aaaSAndroid Build Coastguard Worker        targets: A list of targets to be analyzed.
185*c2e18aaaSAndroid Build Coastguard Worker
186*c2e18aaaSAndroid Build Coastguard Worker    Returns:
187*c2e18aaaSAndroid Build Coastguard Worker        A tuple of a list of java build targets and a list of C/C++ build
188*c2e18aaaSAndroid Build Coastguard Worker        targets.
189*c2e18aaaSAndroid Build Coastguard Worker    """
190*c2e18aaaSAndroid Build Coastguard Worker    jtargets = []
191*c2e18aaaSAndroid Build Coastguard Worker    ctargets = []
192*c2e18aaaSAndroid Build Coastguard Worker    for target in targets:
193*c2e18aaaSAndroid Build Coastguard Worker        rel_path, abs_path = common_util.get_related_paths(
194*c2e18aaaSAndroid Build Coastguard Worker            atest_module_info, target)
195*c2e18aaaSAndroid Build Coastguard Worker        if common_util.check_java_or_kotlin_file_exists(abs_path):
196*c2e18aaaSAndroid Build Coastguard Worker            jtargets.append(target)
197*c2e18aaaSAndroid Build Coastguard Worker        if _check_native_project_exists(path_info, rel_path):
198*c2e18aaaSAndroid Build Coastguard Worker            ctargets.append(target)
199*c2e18aaaSAndroid Build Coastguard Worker    return jtargets, ctargets
200*c2e18aaaSAndroid Build Coastguard Worker
201*c2e18aaaSAndroid Build Coastguard Worker
202*c2e18aaaSAndroid Build Coastguard Workerdef _check_native_project_exists(path_to_module_info, rel_path):
203*c2e18aaaSAndroid Build Coastguard Worker    """Checks if any C/C++ project exists in a rel_path directory.
204*c2e18aaaSAndroid Build Coastguard Worker
205*c2e18aaaSAndroid Build Coastguard Worker    Args:
206*c2e18aaaSAndroid Build Coastguard Worker        path_to_module_info: A dictionary contains data of relative path as key
207*c2e18aaaSAndroid Build Coastguard Worker                             and module info dictionary as value.
208*c2e18aaaSAndroid Build Coastguard Worker        rel_path: A string of relative path of a directory to be checked.
209*c2e18aaaSAndroid Build Coastguard Worker
210*c2e18aaaSAndroid Build Coastguard Worker    Returns:
211*c2e18aaaSAndroid Build Coastguard Worker        True if any C/C++ project exists otherwise False.
212*c2e18aaaSAndroid Build Coastguard Worker    """
213*c2e18aaaSAndroid Build Coastguard Worker    for path in path_to_module_info:
214*c2e18aaaSAndroid Build Coastguard Worker        if common_util.is_source_under_relative_path(path, rel_path):
215*c2e18aaaSAndroid Build Coastguard Worker            return True
216*c2e18aaaSAndroid Build Coastguard Worker    return False
217*c2e18aaaSAndroid Build Coastguard Worker
218*c2e18aaaSAndroid Build Coastguard Worker
219*c2e18aaaSAndroid Build Coastguard Workerdef _filter_out_rust_projects(targets):
220*c2e18aaaSAndroid Build Coastguard Worker    """Filters out if the input targets contain any Rust project.
221*c2e18aaaSAndroid Build Coastguard Worker
222*c2e18aaaSAndroid Build Coastguard Worker    Args:
223*c2e18aaaSAndroid Build Coastguard Worker        targets: A list of targets to be checked.
224*c2e18aaaSAndroid Build Coastguard Worker
225*c2e18aaaSAndroid Build Coastguard Worker    Returns:
226*c2e18aaaSAndroid Build Coastguard Worker        A list of Rust projects.
227*c2e18aaaSAndroid Build Coastguard Worker    """
228*c2e18aaaSAndroid Build Coastguard Worker    root_dir = common_util.get_android_root_dir()
229*c2e18aaaSAndroid Build Coastguard Worker    rust_project_json = os.path.join(
230*c2e18aaaSAndroid Build Coastguard Worker        root_dir,
231*c2e18aaaSAndroid Build Coastguard Worker        common_util.get_blueprint_json_path(constant.RUST_PROJECT_JSON))
232*c2e18aaaSAndroid Build Coastguard Worker    if not os.path.isfile(rust_project_json):
233*c2e18aaaSAndroid Build Coastguard Worker        message = _RUST_JSON_NOT_EXIST.format(rust_project_json)
234*c2e18aaaSAndroid Build Coastguard Worker        print(constant.WARN_MSG.format(
235*c2e18aaaSAndroid Build Coastguard Worker            common_util.COLORED_INFO('Warning:'), message))
236*c2e18aaaSAndroid Build Coastguard Worker        return None
237*c2e18aaaSAndroid Build Coastguard Worker    rust_dict = common_util.get_json_dict(rust_project_json)
238*c2e18aaaSAndroid Build Coastguard Worker    if _CRATES_KEY not in rust_dict:
239*c2e18aaaSAndroid Build Coastguard Worker        message = _RUST_DICT_BROKEN.format(_CRATES_KEY)
240*c2e18aaaSAndroid Build Coastguard Worker        print(constant.WARN_MSG.format(
241*c2e18aaaSAndroid Build Coastguard Worker            common_util.COLORED_INFO('Warning:'), message))
242*c2e18aaaSAndroid Build Coastguard Worker        return None
243*c2e18aaaSAndroid Build Coastguard Worker    return _get_rust_targets(targets, rust_dict[_CRATES_KEY], root_dir)
244*c2e18aaaSAndroid Build Coastguard Worker
245*c2e18aaaSAndroid Build Coastguard Worker
246*c2e18aaaSAndroid Build Coastguard Workerdef _get_rust_targets(targets, rust_modules_info, root_dir):
247*c2e18aaaSAndroid Build Coastguard Worker    """Gets Rust targets by checking input targets with a rust info dictionary.
248*c2e18aaaSAndroid Build Coastguard Worker
249*c2e18aaaSAndroid Build Coastguard Worker    Collects targets' relative rust modules and rebuild them.
250*c2e18aaaSAndroid Build Coastguard Worker
251*c2e18aaaSAndroid Build Coastguard Worker    Args:
252*c2e18aaaSAndroid Build Coastguard Worker        targets: A list of targets to be checked.
253*c2e18aaaSAndroid Build Coastguard Worker        rust_modules_info: A list of the Android Rust modules info.
254*c2e18aaaSAndroid Build Coastguard Worker        root_dir: A string of the Android root directory.
255*c2e18aaaSAndroid Build Coastguard Worker
256*c2e18aaaSAndroid Build Coastguard Worker    Returns:
257*c2e18aaaSAndroid Build Coastguard Worker        A list of Rust targets.
258*c2e18aaaSAndroid Build Coastguard Worker    """
259*c2e18aaaSAndroid Build Coastguard Worker    rtargets = set()
260*c2e18aaaSAndroid Build Coastguard Worker    rebuild_targets = set()
261*c2e18aaaSAndroid Build Coastguard Worker    for target in targets:
262*c2e18aaaSAndroid Build Coastguard Worker        # The Rust project can be expressed only in the path but not the module
263*c2e18aaaSAndroid Build Coastguard Worker        # right now.
264*c2e18aaaSAndroid Build Coastguard Worker        rel_target = _get_relative_path(target, root_dir)
265*c2e18aaaSAndroid Build Coastguard Worker        if not os.path.isdir(os.path.join(root_dir, rel_target)):
266*c2e18aaaSAndroid Build Coastguard Worker            continue
267*c2e18aaaSAndroid Build Coastguard Worker        for mod_info in rust_modules_info:
268*c2e18aaaSAndroid Build Coastguard Worker            if _ROOT_MODULE not in mod_info or _DISPLAY_NAME not in mod_info:
269*c2e18aaaSAndroid Build Coastguard Worker                continue
270*c2e18aaaSAndroid Build Coastguard Worker            path = mod_info[_ROOT_MODULE]
271*c2e18aaaSAndroid Build Coastguard Worker            if common_util.is_source_under_relative_path(path, target):
272*c2e18aaaSAndroid Build Coastguard Worker                rtargets.add(target)
273*c2e18aaaSAndroid Build Coastguard Worker            if _is_target_relative_module(path, rel_target):
274*c2e18aaaSAndroid Build Coastguard Worker                rebuild_targets.add(mod_info[_DISPLAY_NAME])
275*c2e18aaaSAndroid Build Coastguard Worker    project_info.batch_build_dependencies(rebuild_targets)
276*c2e18aaaSAndroid Build Coastguard Worker    return list(rtargets)
277*c2e18aaaSAndroid Build Coastguard Worker
278*c2e18aaaSAndroid Build Coastguard Worker
279*c2e18aaaSAndroid Build Coastguard Workerdef _get_relative_path(target, root_dir):
280*c2e18aaaSAndroid Build Coastguard Worker    """Gets a target's relative path from root and if it needs to add os sep.
281*c2e18aaaSAndroid Build Coastguard Worker
282*c2e18aaaSAndroid Build Coastguard Worker    Args:
283*c2e18aaaSAndroid Build Coastguard Worker        target: A string of a target's path to be checked.
284*c2e18aaaSAndroid Build Coastguard Worker        root_dir: A string of the Android root directory.
285*c2e18aaaSAndroid Build Coastguard Worker
286*c2e18aaaSAndroid Build Coastguard Worker    Returns:
287*c2e18aaaSAndroid Build Coastguard Worker        A string of the relative path of target to root_dir.
288*c2e18aaaSAndroid Build Coastguard Worker    """
289*c2e18aaaSAndroid Build Coastguard Worker    if target == '.':
290*c2e18aaaSAndroid Build Coastguard Worker        target = os.getcwd()
291*c2e18aaaSAndroid Build Coastguard Worker    return os.path.relpath(target, root_dir)
292*c2e18aaaSAndroid Build Coastguard Worker
293*c2e18aaaSAndroid Build Coastguard Worker
294*c2e18aaaSAndroid Build Coastguard Workerdef _is_target_relative_module(path, target):
295*c2e18aaaSAndroid Build Coastguard Worker    """Checks if a module's path is contains a rust target.
296*c2e18aaaSAndroid Build Coastguard Worker
297*c2e18aaaSAndroid Build Coastguard Worker    Args:
298*c2e18aaaSAndroid Build Coastguard Worker        path: A string of a module's path to be checked.
299*c2e18aaaSAndroid Build Coastguard Worker        target: A string of a target's path  without os.sep in the end.
300*c2e18aaaSAndroid Build Coastguard Worker
301*c2e18aaaSAndroid Build Coastguard Worker    Returns:
302*c2e18aaaSAndroid Build Coastguard Worker        A boolean of True if path contains the path of target otherwise False.
303*c2e18aaaSAndroid Build Coastguard Worker    """
304*c2e18aaaSAndroid Build Coastguard Worker    return target == path or target + os.sep in path
305