xref: /aosp_15_r20/tools/asuite/aidegen/lib/native_project_info.py (revision c2e18aaa1096c836b086f94603d04f4eb9cf37f5)
1*c2e18aaaSAndroid Build Coastguard Worker#!/usr/bin/env python3
2*c2e18aaaSAndroid Build Coastguard Worker#
3*c2e18aaaSAndroid Build Coastguard Worker# Copyright 2020 - 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 project information."""
18*c2e18aaaSAndroid Build Coastguard Worker
19*c2e18aaaSAndroid Build Coastguard Workerfrom __future__ import absolute_import
20*c2e18aaaSAndroid Build Coastguard Worker
21*c2e18aaaSAndroid Build Coastguard Workerimport logging
22*c2e18aaaSAndroid Build Coastguard Worker
23*c2e18aaaSAndroid Build Coastguard Workerfrom aidegen.lib import common_util
24*c2e18aaaSAndroid Build Coastguard Workerfrom aidegen.lib import native_module_info
25*c2e18aaaSAndroid Build Coastguard Workerfrom aidegen.lib import project_config
26*c2e18aaaSAndroid Build Coastguard Workerfrom aidegen.lib import project_info
27*c2e18aaaSAndroid Build Coastguard Worker
28*c2e18aaaSAndroid Build Coastguard Worker
29*c2e18aaaSAndroid Build Coastguard Workerclass NativeProjectInfo:
30*c2e18aaaSAndroid Build Coastguard Worker    """Native project information.
31*c2e18aaaSAndroid Build Coastguard Worker
32*c2e18aaaSAndroid Build Coastguard Worker    Class attributes:
33*c2e18aaaSAndroid Build Coastguard Worker        modules_info: An AidegenModuleInfo instance whose name_to_module_info is
34*c2e18aaaSAndroid Build Coastguard Worker                      a dictionary of module_bp_cc_deps.json.
35*c2e18aaaSAndroid Build Coastguard Worker
36*c2e18aaaSAndroid Build Coastguard Worker    Attributes:
37*c2e18aaaSAndroid Build Coastguard Worker        module_names: A list of the native project's module names.
38*c2e18aaaSAndroid Build Coastguard Worker        need_builds: A set of module names need to be built.
39*c2e18aaaSAndroid Build Coastguard Worker    """
40*c2e18aaaSAndroid Build Coastguard Worker
41*c2e18aaaSAndroid Build Coastguard Worker    modules_info = None
42*c2e18aaaSAndroid Build Coastguard Worker
43*c2e18aaaSAndroid Build Coastguard Worker    def __init__(self, target):
44*c2e18aaaSAndroid Build Coastguard Worker        """ProjectInfo initialize.
45*c2e18aaaSAndroid Build Coastguard Worker
46*c2e18aaaSAndroid Build Coastguard Worker        Args:
47*c2e18aaaSAndroid Build Coastguard Worker            target: A native module or project path from users' input will be
48*c2e18aaaSAndroid Build Coastguard Worker                    checked if they contain include paths need to be generated,
49*c2e18aaaSAndroid Build Coastguard Worker                    e.g., in 'libui' module.
50*c2e18aaaSAndroid Build Coastguard Worker            'out/soong/../[email protected]_genc++_headers/gen'
51*c2e18aaaSAndroid Build Coastguard Worker                    we should call 'm [email protected]' to
52*c2e18aaaSAndroid Build Coastguard Worker                    generate the include header files in,
53*c2e18aaaSAndroid Build Coastguard Worker                    '[email protected]_genc++_headers/gen'
54*c2e18aaaSAndroid Build Coastguard Worker                    directory.
55*c2e18aaaSAndroid Build Coastguard Worker        """
56*c2e18aaaSAndroid Build Coastguard Worker        self.module_names = [target] if self.modules_info.is_module(
57*c2e18aaaSAndroid Build Coastguard Worker            target) else self.modules_info.get_module_names_in_targets_paths(
58*c2e18aaaSAndroid Build Coastguard Worker                [target])
59*c2e18aaaSAndroid Build Coastguard Worker        self.need_builds = {
60*c2e18aaaSAndroid Build Coastguard Worker            mod_name
61*c2e18aaaSAndroid Build Coastguard Worker            for mod_name in self.module_names
62*c2e18aaaSAndroid Build Coastguard Worker            if self.modules_info.is_module_need_build(mod_name)
63*c2e18aaaSAndroid Build Coastguard Worker        }
64*c2e18aaaSAndroid Build Coastguard Worker
65*c2e18aaaSAndroid Build Coastguard Worker    @classmethod
66*c2e18aaaSAndroid Build Coastguard Worker    def _init_modules_info(cls):
67*c2e18aaaSAndroid Build Coastguard Worker        """Initializes the class attribute: modules_info."""
68*c2e18aaaSAndroid Build Coastguard Worker        if cls.modules_info:
69*c2e18aaaSAndroid Build Coastguard Worker            return
70*c2e18aaaSAndroid Build Coastguard Worker        cls.modules_info = native_module_info.NativeModuleInfo()
71*c2e18aaaSAndroid Build Coastguard Worker
72*c2e18aaaSAndroid Build Coastguard Worker    @classmethod
73*c2e18aaaSAndroid Build Coastguard Worker    def generate_projects(cls, targets):
74*c2e18aaaSAndroid Build Coastguard Worker        """Generates a list of projects in one time by a list of module names.
75*c2e18aaaSAndroid Build Coastguard Worker
76*c2e18aaaSAndroid Build Coastguard Worker        The method will collect all needed to build modules and build their
77*c2e18aaaSAndroid Build Coastguard Worker        source and include files for them. But if users set the skip build flag
78*c2e18aaaSAndroid Build Coastguard Worker        it won't build anything.
79*c2e18aaaSAndroid Build Coastguard Worker        Usage:
80*c2e18aaaSAndroid Build Coastguard Worker            Call this method before native IDE project files are generated.
81*c2e18aaaSAndroid Build Coastguard Worker            For example,
82*c2e18aaaSAndroid Build Coastguard Worker            native_project_info.NativeProjectInfo.generate_projects(targets)
83*c2e18aaaSAndroid Build Coastguard Worker            native_project_file = native_util.generate_clion_projects(targets)
84*c2e18aaaSAndroid Build Coastguard Worker            ...
85*c2e18aaaSAndroid Build Coastguard Worker
86*c2e18aaaSAndroid Build Coastguard Worker        Args:
87*c2e18aaaSAndroid Build Coastguard Worker            targets: A list of native modules or project paths which will be
88*c2e18aaaSAndroid Build Coastguard Worker                     checked if they contain source or include paths need to be
89*c2e18aaaSAndroid Build Coastguard Worker                     generated.
90*c2e18aaaSAndroid Build Coastguard Worker        """
91*c2e18aaaSAndroid Build Coastguard Worker        config = project_config.ProjectConfig.get_instance()
92*c2e18aaaSAndroid Build Coastguard Worker        cls._init_modules_info()
93*c2e18aaaSAndroid Build Coastguard Worker        need_builds = cls._get_need_builds(targets)
94*c2e18aaaSAndroid Build Coastguard Worker        if config.is_skip_build:
95*c2e18aaaSAndroid Build Coastguard Worker            if need_builds:
96*c2e18aaaSAndroid Build Coastguard Worker                print('{} {}'.format(
97*c2e18aaaSAndroid Build Coastguard Worker                    common_util.COLORED_INFO('Warning:'),
98*c2e18aaaSAndroid Build Coastguard Worker                    'Native modules build skipped:\n{}.'.format(
99*c2e18aaaSAndroid Build Coastguard Worker                        '\n'.join(need_builds))))
100*c2e18aaaSAndroid Build Coastguard Worker            return
101*c2e18aaaSAndroid Build Coastguard Worker        if need_builds:
102*c2e18aaaSAndroid Build Coastguard Worker            logging.info('\nThe batch_build_dependencies function is called by '
103*c2e18aaaSAndroid Build Coastguard Worker                         'NativeProjectInfo\'s generate_projects method.')
104*c2e18aaaSAndroid Build Coastguard Worker            project_info.batch_build_dependencies(need_builds)
105*c2e18aaaSAndroid Build Coastguard Worker
106*c2e18aaaSAndroid Build Coastguard Worker    @classmethod
107*c2e18aaaSAndroid Build Coastguard Worker    def _get_need_builds(cls, targets):
108*c2e18aaaSAndroid Build Coastguard Worker        """Gets need to be built modules from targets.
109*c2e18aaaSAndroid Build Coastguard Worker
110*c2e18aaaSAndroid Build Coastguard Worker        Args:
111*c2e18aaaSAndroid Build Coastguard Worker            targets: A list of native modules or project paths which will be
112*c2e18aaaSAndroid Build Coastguard Worker                     checked if they contain source or include paths need to be
113*c2e18aaaSAndroid Build Coastguard Worker                     generated.
114*c2e18aaaSAndroid Build Coastguard Worker
115*c2e18aaaSAndroid Build Coastguard Worker        Returns:
116*c2e18aaaSAndroid Build Coastguard Worker            A set of module names which need to be built.
117*c2e18aaaSAndroid Build Coastguard Worker        """
118*c2e18aaaSAndroid Build Coastguard Worker        need_builds = set()
119*c2e18aaaSAndroid Build Coastguard Worker        for target in targets:
120*c2e18aaaSAndroid Build Coastguard Worker            project = NativeProjectInfo(target)
121*c2e18aaaSAndroid Build Coastguard Worker            if project.need_builds:
122*c2e18aaaSAndroid Build Coastguard Worker                need_builds.update(project.need_builds)
123*c2e18aaaSAndroid Build Coastguard Worker        return need_builds
124