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