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 host binaries.""" 16 17load( 18 "@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl", 19 "feature", 20 "feature_set", 21 "flag_group", 22 "flag_set", 23) 24load( 25 "@rules_cc//cc:action_names.bzl", 26 "ALL_CC_LINK_ACTION_NAMES", 27 "ALL_CPP_COMPILE_ACTION_NAMES", 28) 29 30# Note: openssl (via boringssl) and elfutils should be added explicitly 31# via //prebuilts/kernel-build-tools:imported_libs 32# Hence not listed here. 33# See example in //build/kernel/kleaf/tests/cc_testing:openssl_client 34 35def _linux_features(ctx): 36 # Global feature flags. 37 features = [] 38 39 # The cc toolchain supports building C++ code for cc_* rules. 40 features.append(feature( 41 name = "kleaf-host-cc", 42 enabled = True, 43 )) 44 45 # By default, musl stuff are disabled 46 # unless enabled by clang_config.extra_features in the musl toolchain. 47 features.append(feature( 48 name = "kleaf-host-musl", 49 enabled = False, 50 )) 51 52 # Flags applied to C++ code 53 54 extra_compile_flags = [] 55 for bin_dir in ctx.files.bin_dirs: 56 extra_compile_flags.append("-B" + bin_dir.path) 57 58 extra_link_flags = list(extra_compile_flags) 59 for lib_dir in ctx.files.lib_dirs: 60 extra_link_flags.append("-L" + lib_dir.path) 61 62 features.append(feature( 63 name = "kleaf-host-cc-rules-flags", 64 # If all requirements are satisified, enable this by default. 65 enabled = True, 66 requires = [feature_set(features = [ 67 "kleaf-host-cc", 68 ])], 69 flag_sets = [ 70 flag_set( 71 actions = ALL_CC_LINK_ACTION_NAMES, 72 flag_groups = [ 73 flag_group( 74 flags = [ 75 "--target={}".format(ctx.attr.target), 76 "-stdlib=libc++", 77 # Using -static-libstdc++ removes the complication of adding 78 # libc++ to runfiles for cc_binary, adjusting rpath, and 79 # packaging libc++ along with the cc_binary when it is 80 # mentioned in a pkg_* or copy_to_dist_dir rule. 81 # Can't use static_link_cpp_runtimes because 82 # https://github.com/bazelbuild/bazel/issues/14342 83 "-static-libstdc++", 84 ] + extra_link_flags, 85 ), 86 ], 87 ), 88 flag_set( 89 # Applies to C++ code only. 90 actions = ALL_CPP_COMPILE_ACTION_NAMES, 91 flag_groups = [ 92 flag_group( 93 flags = [ 94 "--target={}".format(ctx.attr.target), 95 "-stdlib=libc++", 96 ] + extra_compile_flags, 97 ), 98 ], 99 ), 100 ], 101 )) 102 103 # Flags applied to C++ code when it is built against musl libc. 104 features.append(feature( 105 name = "kleaf-host-cc-rules-musl", 106 # If all requirements are satisified, enable this by default. 107 enabled = True, 108 requires = [feature_set(features = [ 109 "kleaf-host-cc", 110 "kleaf-host-musl", 111 ])], 112 flag_sets = [ 113 flag_set( 114 # Applies to C++ code only. 115 actions = ALL_CPP_COMPILE_ACTION_NAMES, 116 flag_groups = [ 117 flag_group( 118 flags = [ 119 "-D_LIBCPP_HAS_MUSL_LIBC=ON", 120 ], 121 ), 122 ], 123 ), 124 ], 125 )) 126 return features 127 128linux = struct( 129 features = _linux_features, 130) 131