1*eed53cd4SHONG Yifan# Copyright 2018 The Bazel Authors. All rights reserved. 2*eed53cd4SHONG Yifan# 3*eed53cd4SHONG Yifan# Licensed under the Apache License, Version 2.0 (the "License"); 4*eed53cd4SHONG Yifan# you may not use this file except in compliance with the License. 5*eed53cd4SHONG Yifan# You may obtain a copy of the License at 6*eed53cd4SHONG Yifan# 7*eed53cd4SHONG Yifan# http://www.apache.org/licenses/LICENSE-2.0 8*eed53cd4SHONG Yifan# 9*eed53cd4SHONG Yifan# Unless required by applicable law or agreed to in writing, software 10*eed53cd4SHONG Yifan# distributed under the License is distributed on an "AS IS" BASIS, 11*eed53cd4SHONG Yifan# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12*eed53cd4SHONG Yifan# See the License for the specific language governing permissions and 13*eed53cd4SHONG Yifan# limitations under the License. 14*eed53cd4SHONG Yifan 15*eed53cd4SHONG Yifan"""Starlark rules for building C++ projects.""" 16*eed53cd4SHONG Yifan 17*eed53cd4SHONG Yifanload("//cc/private/rules_impl:cc_flags_supplier.bzl", _cc_flags_supplier = "cc_flags_supplier") 18*eed53cd4SHONG Yifanload("//cc/private/rules_impl:compiler_flag.bzl", _compiler_flag = "compiler_flag") 19*eed53cd4SHONG Yifanload("//cc/private/rules_impl:native.bzl", "NativeCcInfo", "NativeCcToolchainConfigInfo", "NativeDebugPackageInfo", "native_cc_common") 20*eed53cd4SHONG Yifan 21*eed53cd4SHONG Yifan_MIGRATION_TAG = "__CC_RULES_MIGRATION_DO_NOT_USE_WILL_BREAK__" 22*eed53cd4SHONG Yifan 23*eed53cd4SHONG Yifan# TODO(bazel-team): To avoid breaking changes, if the below are no longer 24*eed53cd4SHONG Yifan# forwarding to native rules, flag @bazel_tools@bazel_tools//tools/cpp:link_extra_libs 25*eed53cd4SHONG Yifan# should either: (a) alias the flag @rules_cc//:link_extra_libs, or (b) be 26*eed53cd4SHONG Yifan# added as a dependency to @rules_cc//:link_extra_lib. The intermediate library 27*eed53cd4SHONG Yifan# @bazel_tools@bazel_tools//tools/cpp:link_extra_lib should either be added as a dependency 28*eed53cd4SHONG Yifan# to @rules_cc//:link_extra_lib, or removed entirely (if possible). 29*eed53cd4SHONG Yifan_LINK_EXTRA_LIB = "@rules_cc//:link_extra_lib" # copybara-use-repo-external-label 30*eed53cd4SHONG Yifan 31*eed53cd4SHONG Yifandef _add_tags(attrs, is_binary = False): 32*eed53cd4SHONG Yifan if "tags" in attrs and attrs["tags"] != None: 33*eed53cd4SHONG Yifan attrs["tags"] = attrs["tags"] + [_MIGRATION_TAG] 34*eed53cd4SHONG Yifan else: 35*eed53cd4SHONG Yifan attrs["tags"] = [_MIGRATION_TAG] 36*eed53cd4SHONG Yifan 37*eed53cd4SHONG Yifan if is_binary: 38*eed53cd4SHONG Yifan is_library = "linkshared" in attrs and attrs["linkshared"] 39*eed53cd4SHONG Yifan 40*eed53cd4SHONG Yifan # Executable builds also include the "link_extra_lib" library. 41*eed53cd4SHONG Yifan if not is_library: 42*eed53cd4SHONG Yifan if "deps" in attrs and attrs["deps"] != None: 43*eed53cd4SHONG Yifan attrs["deps"] = attrs["deps"] + [_LINK_EXTRA_LIB] 44*eed53cd4SHONG Yifan else: 45*eed53cd4SHONG Yifan attrs["deps"] = [_LINK_EXTRA_LIB] 46*eed53cd4SHONG Yifan 47*eed53cd4SHONG Yifan return attrs 48*eed53cd4SHONG Yifan 49*eed53cd4SHONG Yifandef cc_binary(**attrs): 50*eed53cd4SHONG Yifan """Bazel cc_binary rule. 51*eed53cd4SHONG Yifan 52*eed53cd4SHONG Yifan https://docs.bazel.build/versions/main/be/c-cpp.html#cc_binary 53*eed53cd4SHONG Yifan 54*eed53cd4SHONG Yifan Args: 55*eed53cd4SHONG Yifan **attrs: Rule attributes 56*eed53cd4SHONG Yifan """ 57*eed53cd4SHONG Yifan 58*eed53cd4SHONG Yifan # buildifier: disable=native-cc 59*eed53cd4SHONG Yifan native.cc_binary(**_add_tags(attrs, True)) 60*eed53cd4SHONG Yifan 61*eed53cd4SHONG Yifandef cc_test(**attrs): 62*eed53cd4SHONG Yifan """Bazel cc_test rule. 63*eed53cd4SHONG Yifan 64*eed53cd4SHONG Yifan https://docs.bazel.build/versions/main/be/c-cpp.html#cc_test 65*eed53cd4SHONG Yifan 66*eed53cd4SHONG Yifan Args: 67*eed53cd4SHONG Yifan **attrs: Rule attributes 68*eed53cd4SHONG Yifan """ 69*eed53cd4SHONG Yifan 70*eed53cd4SHONG Yifan # buildifier: disable=native-cc 71*eed53cd4SHONG Yifan native.cc_test(**_add_tags(attrs, True)) 72*eed53cd4SHONG Yifan 73*eed53cd4SHONG Yifandef cc_library(**attrs): 74*eed53cd4SHONG Yifan """Bazel cc_library rule. 75*eed53cd4SHONG Yifan 76*eed53cd4SHONG Yifan https://docs.bazel.build/versions/main/be/c-cpp.html#cc_library 77*eed53cd4SHONG Yifan 78*eed53cd4SHONG Yifan Args: 79*eed53cd4SHONG Yifan **attrs: Rule attributes 80*eed53cd4SHONG Yifan """ 81*eed53cd4SHONG Yifan 82*eed53cd4SHONG Yifan # buildifier: disable=native-cc 83*eed53cd4SHONG Yifan native.cc_library(**_add_tags(attrs)) 84*eed53cd4SHONG Yifan 85*eed53cd4SHONG Yifandef cc_import(**attrs): 86*eed53cd4SHONG Yifan """Bazel cc_import rule. 87*eed53cd4SHONG Yifan 88*eed53cd4SHONG Yifan https://docs.bazel.build/versions/main/be/c-cpp.html#cc_import 89*eed53cd4SHONG Yifan 90*eed53cd4SHONG Yifan Args: 91*eed53cd4SHONG Yifan **attrs: Rule attributes 92*eed53cd4SHONG Yifan """ 93*eed53cd4SHONG Yifan 94*eed53cd4SHONG Yifan # buildifier: disable=native-cc 95*eed53cd4SHONG Yifan native.cc_import(**_add_tags(attrs)) 96*eed53cd4SHONG Yifan 97*eed53cd4SHONG Yifandef cc_proto_library(**attrs): 98*eed53cd4SHONG Yifan """Bazel cc_proto_library rule. 99*eed53cd4SHONG Yifan 100*eed53cd4SHONG Yifan https://docs.bazel.build/versions/main/be/c-cpp.html#cc_proto_library 101*eed53cd4SHONG Yifan 102*eed53cd4SHONG Yifan Args: 103*eed53cd4SHONG Yifan **attrs: Rule attributes 104*eed53cd4SHONG Yifan """ 105*eed53cd4SHONG Yifan 106*eed53cd4SHONG Yifan # buildifier: disable=native-cc-proto 107*eed53cd4SHONG Yifan native.cc_proto_library(**_add_tags(attrs)) 108*eed53cd4SHONG Yifan 109*eed53cd4SHONG Yifandef fdo_prefetch_hints(**attrs): 110*eed53cd4SHONG Yifan """Bazel fdo_prefetch_hints rule. 111*eed53cd4SHONG Yifan 112*eed53cd4SHONG Yifan https://docs.bazel.build/versions/main/be/c-cpp.html#fdo_prefetch_hints 113*eed53cd4SHONG Yifan 114*eed53cd4SHONG Yifan Args: 115*eed53cd4SHONG Yifan **attrs: Rule attributes 116*eed53cd4SHONG Yifan """ 117*eed53cd4SHONG Yifan 118*eed53cd4SHONG Yifan # buildifier: disable=native-cc 119*eed53cd4SHONG Yifan native.fdo_prefetch_hints(**_add_tags(attrs)) 120*eed53cd4SHONG Yifan 121*eed53cd4SHONG Yifandef fdo_profile(**attrs): 122*eed53cd4SHONG Yifan """Bazel fdo_profile rule. 123*eed53cd4SHONG Yifan 124*eed53cd4SHONG Yifan https://docs.bazel.build/versions/main/be/c-cpp.html#fdo_profile 125*eed53cd4SHONG Yifan 126*eed53cd4SHONG Yifan Args: 127*eed53cd4SHONG Yifan **attrs: Rule attributes 128*eed53cd4SHONG Yifan """ 129*eed53cd4SHONG Yifan 130*eed53cd4SHONG Yifan # buildifier: disable=native-cc 131*eed53cd4SHONG Yifan native.fdo_profile(**_add_tags(attrs)) 132*eed53cd4SHONG Yifan 133*eed53cd4SHONG Yifandef cc_toolchain(**attrs): 134*eed53cd4SHONG Yifan """Bazel cc_toolchain rule. 135*eed53cd4SHONG Yifan 136*eed53cd4SHONG Yifan https://docs.bazel.build/versions/main/be/c-cpp.html#cc_toolchain 137*eed53cd4SHONG Yifan 138*eed53cd4SHONG Yifan Args: 139*eed53cd4SHONG Yifan **attrs: Rule attributes 140*eed53cd4SHONG Yifan """ 141*eed53cd4SHONG Yifan 142*eed53cd4SHONG Yifan # buildifier: disable=native-cc 143*eed53cd4SHONG Yifan native.cc_toolchain(**_add_tags(attrs)) 144*eed53cd4SHONG Yifan 145*eed53cd4SHONG Yifandef cc_toolchain_suite(**attrs): 146*eed53cd4SHONG Yifan """Bazel cc_toolchain_suite rule. 147*eed53cd4SHONG Yifan 148*eed53cd4SHONG Yifan https://docs.bazel.build/versions/main/be/c-cpp.html#cc_toolchain_suite 149*eed53cd4SHONG Yifan 150*eed53cd4SHONG Yifan Args: 151*eed53cd4SHONG Yifan **attrs: Rule attributes 152*eed53cd4SHONG Yifan """ 153*eed53cd4SHONG Yifan 154*eed53cd4SHONG Yifan # buildifier: disable=native-cc 155*eed53cd4SHONG Yifan native.cc_toolchain_suite(**_add_tags(attrs)) 156*eed53cd4SHONG Yifan 157*eed53cd4SHONG Yifandef objc_library(**attrs): 158*eed53cd4SHONG Yifan """Bazel objc_library rule. 159*eed53cd4SHONG Yifan 160*eed53cd4SHONG Yifan https://docs.bazel.build/versions/main/be/objective-c.html#objc_library 161*eed53cd4SHONG Yifan 162*eed53cd4SHONG Yifan Args: 163*eed53cd4SHONG Yifan **attrs: Rule attributes 164*eed53cd4SHONG Yifan """ 165*eed53cd4SHONG Yifan 166*eed53cd4SHONG Yifan # buildifier: disable=native-cc 167*eed53cd4SHONG Yifan native.objc_library(**_add_tags(attrs)) 168*eed53cd4SHONG Yifan 169*eed53cd4SHONG Yifandef objc_import(**attrs): 170*eed53cd4SHONG Yifan """Bazel objc_import rule. 171*eed53cd4SHONG Yifan 172*eed53cd4SHONG Yifan https://docs.bazel.build/versions/main/be/objective-c.html#objc_import 173*eed53cd4SHONG Yifan 174*eed53cd4SHONG Yifan Args: 175*eed53cd4SHONG Yifan **attrs: Rule attributes 176*eed53cd4SHONG Yifan """ 177*eed53cd4SHONG Yifan 178*eed53cd4SHONG Yifan # buildifier: disable=native-cc 179*eed53cd4SHONG Yifan native.objc_import(**_add_tags(attrs)) 180*eed53cd4SHONG Yifan 181*eed53cd4SHONG Yifandef cc_flags_supplier(**attrs): 182*eed53cd4SHONG Yifan """Bazel cc_flags_supplier rule. 183*eed53cd4SHONG Yifan 184*eed53cd4SHONG Yifan Args: 185*eed53cd4SHONG Yifan **attrs: Rule attributes 186*eed53cd4SHONG Yifan """ 187*eed53cd4SHONG Yifan _cc_flags_supplier(**_add_tags(attrs)) 188*eed53cd4SHONG Yifan 189*eed53cd4SHONG Yifandef compiler_flag(**attrs): 190*eed53cd4SHONG Yifan """Bazel compiler_flag rule. 191*eed53cd4SHONG Yifan 192*eed53cd4SHONG Yifan Args: 193*eed53cd4SHONG Yifan **attrs: Rule attributes 194*eed53cd4SHONG Yifan """ 195*eed53cd4SHONG Yifan _compiler_flag(**_add_tags(attrs)) 196*eed53cd4SHONG Yifan 197*eed53cd4SHONG Yifancc_common = native_cc_common 198*eed53cd4SHONG Yifan 199*eed53cd4SHONG YifanCcInfo = NativeCcInfo 200*eed53cd4SHONG Yifan 201*eed53cd4SHONG YifanCcToolchainConfigInfo = NativeCcToolchainConfigInfo 202*eed53cd4SHONG Yifan 203*eed53cd4SHONG YifanDebugPackageInfo = NativeDebugPackageInfo 204