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"""For Android kernel builds, configure CC toolchain for target binaries."""
16
17load(
18    "@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl",
19    "feature",
20    "flag_group",
21    "flag_set",
22)
23load(
24    "@rules_cc//cc:action_names.bzl",
25    "ALL_CC_COMPILE_ACTION_NAMES",
26    "ALL_CC_LINK_ACTION_NAMES",
27)
28
29def _ldflags(target):
30    # From _setup_env.sh
31    # USERLDFLAGS
32    return feature(
33        name = "kleaf-android-ldflags",
34        enabled = True,
35        flag_sets = [
36            flag_set(
37                actions = ALL_CC_LINK_ACTION_NAMES,
38                flag_groups = [
39                    flag_group(
40                        flags = [
41                            "--target={}".format(target),
42                        ],
43                    ),
44                ],
45            ),
46        ],
47        implies = [
48            # We need to set -fuse-ld=lld for Android's build env since AOSP LLVM's
49            # clang is not configured to use LLD by default, and BFD has been
50            # intentionally removed. This way CC_CAN_LINK can properly link the test in
51            # scripts/cc-can-link.sh.
52            "kleaf-lld-compiler-rt",
53        ],
54    )
55
56def _clfags(target):
57    # From _setup_env.sh
58    # USERCFLAGS
59    return feature(
60        name = "kleaf-android-cflags",
61        enabled = True,
62        flag_sets = [
63            flag_set(
64                # Applies to C, C++ and assembly code.
65                actions = ALL_CC_COMPILE_ACTION_NAMES,
66                flag_groups = [
67                    flag_group(
68                        flags = [
69                            "--target={}".format(target),
70                            # Some kernel headers trigger -Wunused-function for unused static
71                            # functions with clang; GCC does not warn about unused static inline
72                            # functions. The kernel sets __attribute__((maybe_unused)) on such
73                            # functions when W=1 is not set.
74                            "-Wno-unused-function",
75
76                            # To help debug these flags, consider commenting back in the following,
77                            # and add `echo $@ > /tmp/log.txt` and `2>>/tmp/log.txt` to the
78                            # invocation of $@ in scripts/cc-can-link.sh.
79                            # "-Wl,--verbose",
80                            # "-v",
81                        ],
82                    ),
83                ],
84            ),
85        ],
86    )
87
88def _features(ctx):
89    if ctx.attr.target:
90        return [
91            _ldflags(ctx.attr.target),
92            _clfags(ctx.attr.target),
93        ]
94    return []
95
96android = struct(
97    features = _features,
98)
99