1# Copyright (C) 2023 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#       http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""Common features and helper functions for configuring CC toolchain for Android kernel."""
16
17load(
18    "@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl",
19    "action_config",
20    "feature",
21    "flag_group",
22    "flag_set",
23)
24load(
25    "@rules_cc//cc:action_names.bzl",
26    "ACTION_NAMES",
27    "ALL_CC_COMPILE_ACTION_NAMES",
28    "ALL_CC_LINK_ACTION_NAMES",
29)
30
31def _action_configs(ctx):
32    compile = action_config(
33        action_name = ACTION_NAMES.c_compile,
34        tools = [
35            struct(
36                type_name = "tool",
37                tool = ctx.file.clang,
38            ),
39        ],
40    )
41    compile_plus_plus = action_config(
42        action_name = ACTION_NAMES.cpp_compile,
43        tools = [
44            struct(
45                type_name = "tool",
46                tool = ctx.file.clang_plus_plus,
47            ),
48        ],
49    )
50    link = action_config(
51        action_name = ACTION_NAMES.cpp_link_executable,
52        tools = [
53            struct(
54                type_name = "tool",
55                tool = ctx.file.ld,
56            ),
57        ],
58    )
59    ar = action_config(
60        action_name = ACTION_NAMES.cpp_link_static_library,
61        tools = [
62            struct(
63                type_name = "tool",
64                tool = ctx.file.ar,
65            ),
66        ],
67        implies = [
68            "archiver_flags",
69        ],
70    )
71    strip = action_config(
72        action_name = ACTION_NAMES.strip,
73        tools = [
74            struct(
75                type_name = "tool",
76                tool = ctx.file.strip,
77            ),
78        ],
79    )
80    objcopy = action_config(
81        # TODO(b/310843869): Use objcopy_embed_data from ACTION_NAMES
82        action_name = "objcopy_embed_data",
83        tools = [
84            struct(
85                type_name = "tool",
86                tool = ctx.file.objcopy,
87            ),
88        ],
89    )
90
91    return [
92        compile,
93        compile_plus_plus,
94        link,
95        ar,
96        strip,
97        objcopy,
98    ]
99
100def _tool_attrs():
101    return {
102        "clang": attr.label(allow_single_file = True),
103        "clang_plus_plus": attr.label(allow_single_file = True),
104        "ld": attr.label(allow_single_file = True),
105        "strip": attr.label(allow_single_file = True),
106        "ar": attr.label(allow_single_file = True),
107        "objcopy": attr.label(allow_single_file = True),
108    }
109
110def _common_cflags():
111    return feature(
112        name = "kleaf-no-canonical-prefixes",
113        enabled = True,
114        flag_sets = [
115            flag_set(
116                actions = ALL_CC_COMPILE_ACTION_NAMES,
117                flag_groups = [
118                    flag_group(
119                        flags = [
120                            # Work around https://github.com/bazelbuild/bazel/issues/4605
121                            # "cxx_builtin_include_directory doesn't work with non-absolute path"
122                            "-no-canonical-prefixes",
123                        ],
124                    ),
125                ],
126            ),
127        ],
128    )
129
130def _lld():
131    return feature(
132        name = "kleaf-lld",
133        enabled = False,  # Not enabled unless implied by individual os
134        flag_sets = [
135            flag_set(
136                actions = ALL_CC_LINK_ACTION_NAMES,
137                flag_groups = [
138                    flag_group(
139                        flags = [
140                            "-fuse-ld=lld",
141                        ],
142                    ),
143                ],
144            ),
145        ],
146    )
147
148def _lld_compiler_rt():
149    return feature(
150        name = "kleaf-lld-compiler-rt",
151        enabled = False,  # Not enabled unless implied by individual os
152        flag_sets = [
153            flag_set(
154                actions = ALL_CC_LINK_ACTION_NAMES,
155                flag_groups = [
156                    flag_group(
157                        flags = [
158                            "--rtlib=compiler-rt",
159                        ],
160                    ),
161                ],
162            ),
163        ],
164        implies = [
165            "kleaf-lld",
166        ],
167    )
168
169def _common_features(_ctx):
170    """Features that applies to both android and linux toolchain."""
171    return [
172        _common_cflags(),
173        _lld(),
174        _lld_compiler_rt(),
175    ]
176
177common = struct(
178    features = _common_features,
179    action_configs = _action_configs,
180    tool_attrs = _tool_attrs,
181)
182