1# Copyright (C) 2021 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 15load( 16 ":cc_library_common.bzl", 17 "add_lists_defaulting_to_none", 18 "parse_sdk_version", 19 "sanitizer_deps", 20 "system_dynamic_deps_defaults", 21 "system_static_deps_defaults", 22) 23load(":cc_library_static.bzl", "cc_library_static") 24load(":stl.bzl", "stl_info_from_attr") 25load(":stripped_cc_common.bzl", "stripped_binary", "stripped_test") 26load(":versioned_cc_common.bzl", "versioned_binary") 27 28def cc_binary( 29 name, 30 suffix = "", 31 dynamic_deps = [], 32 srcs = [], 33 srcs_c = [], 34 srcs_as = [], 35 copts = [], 36 cppflags = [], 37 conlyflags = [], 38 asflags = [], 39 deps = [], 40 whole_archive_deps = [], 41 system_deps = None, 42 runtime_deps = [], 43 export_includes = [], # @unused 44 export_system_includes = [], # @unused 45 local_includes = [], 46 absolute_includes = [], 47 linkshared = True, 48 linkopts = [], 49 rtti = False, 50 use_libcrt = True, 51 stl = "", 52 cpp_std = "", 53 additional_linker_inputs = None, 54 strip = {}, 55 features = [], 56 target_compatible_with = [], 57 sdk_version = "", # @unused 58 min_sdk_version = "", 59 use_version_lib = False, 60 tags = [], 61 generate_cc_test = False, 62 tidy = None, 63 tidy_checks = None, 64 tidy_checks_as_errors = None, 65 tidy_flags = None, 66 tidy_disabled_srcs = None, 67 tidy_timeout_srcs = None, 68 native_coverage = True, 69 **kwargs): 70 "Bazel macro to correspond with the cc_binary Soong module." 71 72 root_name = name + "__internal_root" 73 unstripped_name = name + "_unstripped" 74 75 toolchain_features = [] 76 toolchain_features.extend(["-pic", "pie"]) 77 if linkshared: 78 toolchain_features.extend(["dynamic_executable", "dynamic_linker"]) 79 else: 80 toolchain_features.extend(["-dynamic_executable", "-dynamic_linker", "static_executable", "static_flag"]) 81 82 if not use_libcrt: 83 toolchain_features.append("-use_libcrt") 84 85 if min_sdk_version: 86 toolchain_features += parse_sdk_version(min_sdk_version) + ["-sdk_version_default"] 87 toolchain_features += features 88 89 system_dynamic_deps = [] 90 system_static_deps = [] 91 if system_deps == None: 92 if linkshared: 93 system_deps = system_dynamic_deps_defaults 94 else: 95 system_deps = system_static_deps_defaults 96 97 if linkshared: 98 system_dynamic_deps = system_deps 99 else: 100 system_static_deps = system_deps 101 102 if not native_coverage: 103 toolchain_features.append("-coverage") 104 else: 105 toolchain_features += select({ 106 "//build/bazel/rules/cc:android_coverage_lib_flag": ["android_coverage_lib"], 107 "//conditions:default": [], 108 }) 109 110 # TODO(b/233660582): deal with the cases where the default lib shouldn't be used 111 whole_archive_deps = whole_archive_deps + select({ 112 "//build/bazel/rules/cc:android_coverage_lib_flag": ["//system/extras/toolchain-extras:libprofile-clang-extras"], 113 "//conditions:default": [], 114 }) 115 116 stl_info = stl_info_from_attr(stl, linkshared, is_binary = True) 117 linkopts = linkopts + stl_info.linkopts 118 copts = copts + stl_info.cppflags 119 120 # The static library at the root of the cc_binary. 121 cc_library_static( 122 name = root_name, 123 absolute_includes = absolute_includes, 124 # alwayslink = True because the compiled objects from cc_library.srcs is expected 125 # to always be linked into the binary itself later (otherwise, why compile them at 126 # the cc_binary level?). 127 # 128 # Concretely, this makes this static library to be wrapped in the --whole_archive 129 # block when linking the cc_binary later. 130 alwayslink = True, 131 asflags = asflags, 132 conlyflags = conlyflags, 133 copts = copts, 134 cpp_std = cpp_std, 135 cppflags = cppflags, 136 deps = deps + stl_info.static_deps + system_static_deps, 137 whole_archive_deps = whole_archive_deps, 138 dynamic_deps = dynamic_deps + stl_info.shared_deps, 139 features = toolchain_features, 140 local_includes = local_includes, 141 rtti = rtti, 142 srcs = srcs, 143 srcs_as = srcs_as, 144 srcs_c = srcs_c, 145 stl = "none", 146 system_dynamic_deps = system_dynamic_deps, 147 target_compatible_with = target_compatible_with, 148 tags = ["manual"], 149 tidy = tidy, 150 tidy_checks = tidy_checks, 151 tidy_checks_as_errors = tidy_checks_as_errors, 152 tidy_flags = tidy_flags, 153 tidy_disabled_srcs = tidy_disabled_srcs, 154 tidy_timeout_srcs = tidy_timeout_srcs, 155 native_coverage = native_coverage, 156 ) 157 158 binary_dynamic_deps = add_lists_defaulting_to_none( 159 dynamic_deps, 160 system_dynamic_deps, 161 stl_info.shared_deps, 162 ) 163 164 sanitizer_deps_name = name + "_sanitizer_deps" 165 sanitizer_deps( 166 name = sanitizer_deps_name, 167 dep = root_name, 168 tags = ["manual"], 169 ) 170 171 # cc_test and cc_binary are almost identical rules, so fork the top level 172 # rule classes here. 173 unstripped_cc_rule = native.cc_binary 174 stripped_cc_rule = stripped_binary 175 if generate_cc_test: 176 unstripped_cc_rule = native.cc_test 177 stripped_cc_rule = stripped_test 178 179 unstripped_cc_rule( 180 name = unstripped_name, 181 deps = [root_name, sanitizer_deps_name] + deps + system_static_deps + stl_info.static_deps, 182 dynamic_deps = binary_dynamic_deps, 183 features = toolchain_features, 184 linkopts = linkopts, 185 additional_linker_inputs = additional_linker_inputs, 186 target_compatible_with = target_compatible_with, 187 tags = ["manual"], 188 **kwargs 189 ) 190 191 versioned_name = name + "_versioned" 192 versioned_binary( 193 name = versioned_name, 194 src = unstripped_name, 195 stamp_build_number = use_version_lib, 196 tags = ["manual"], 197 testonly = generate_cc_test, 198 ) 199 200 stripped_cc_rule( 201 name = name, 202 suffix = suffix, 203 src = versioned_name, 204 runtime_deps = runtime_deps, 205 target_compatible_with = target_compatible_with, 206 tags = tags, 207 unstripped = unstripped_name, 208 testonly = generate_cc_test, 209 androidmk_deps = [root_name], 210 **strip 211 ) 212