xref: /aosp_15_r20/tools/asuite/aidegen/lib/native_project_info.py (revision c2e18aaa1096c836b086f94603d04f4eb9cf37f5)
1#!/usr/bin/env python3
2#
3# Copyright 2020 - The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17"""Native project information."""
18
19from __future__ import absolute_import
20
21import logging
22
23from aidegen.lib import common_util
24from aidegen.lib import native_module_info
25from aidegen.lib import project_config
26from aidegen.lib import project_info
27
28
29class NativeProjectInfo:
30    """Native project information.
31
32    Class attributes:
33        modules_info: An AidegenModuleInfo instance whose name_to_module_info is
34                      a dictionary of module_bp_cc_deps.json.
35
36    Attributes:
37        module_names: A list of the native project's module names.
38        need_builds: A set of module names need to be built.
39    """
40
41    modules_info = None
42
43    def __init__(self, target):
44        """ProjectInfo initialize.
45
46        Args:
47            target: A native module or project path from users' input will be
48                    checked if they contain include paths need to be generated,
49                    e.g., in 'libui' module.
50            'out/soong/../[email protected]_genc++_headers/gen'
51                    we should call 'm [email protected]' to
52                    generate the include header files in,
53                    '[email protected]_genc++_headers/gen'
54                    directory.
55        """
56        self.module_names = [target] if self.modules_info.is_module(
57            target) else self.modules_info.get_module_names_in_targets_paths(
58                [target])
59        self.need_builds = {
60            mod_name
61            for mod_name in self.module_names
62            if self.modules_info.is_module_need_build(mod_name)
63        }
64
65    @classmethod
66    def _init_modules_info(cls):
67        """Initializes the class attribute: modules_info."""
68        if cls.modules_info:
69            return
70        cls.modules_info = native_module_info.NativeModuleInfo()
71
72    @classmethod
73    def generate_projects(cls, targets):
74        """Generates a list of projects in one time by a list of module names.
75
76        The method will collect all needed to build modules and build their
77        source and include files for them. But if users set the skip build flag
78        it won't build anything.
79        Usage:
80            Call this method before native IDE project files are generated.
81            For example,
82            native_project_info.NativeProjectInfo.generate_projects(targets)
83            native_project_file = native_util.generate_clion_projects(targets)
84            ...
85
86        Args:
87            targets: A list of native modules or project paths which will be
88                     checked if they contain source or include paths need to be
89                     generated.
90        """
91        config = project_config.ProjectConfig.get_instance()
92        cls._init_modules_info()
93        need_builds = cls._get_need_builds(targets)
94        if config.is_skip_build:
95            if need_builds:
96                print('{} {}'.format(
97                    common_util.COLORED_INFO('Warning:'),
98                    'Native modules build skipped:\n{}.'.format(
99                        '\n'.join(need_builds))))
100            return
101        if need_builds:
102            logging.info('\nThe batch_build_dependencies function is called by '
103                         'NativeProjectInfo\'s generate_projects method.')
104            project_info.batch_build_dependencies(need_builds)
105
106    @classmethod
107    def _get_need_builds(cls, targets):
108        """Gets need to be built modules from targets.
109
110        Args:
111            targets: A list of native modules or project paths which will be
112                     checked if they contain source or include paths need to be
113                     generated.
114
115        Returns:
116            A set of module names which need to be built.
117        """
118        need_builds = set()
119        for target in targets:
120            project = NativeProjectInfo(target)
121            if project.need_builds:
122                need_builds.update(project.need_builds)
123        return need_builds
124