xref: /aosp_15_r20/external/bazelbuild-rules_cc/cc/defs.bzl (revision eed53cd41c5909d05eedc7ad9720bb158fd93452)
1# Copyright 2018 The Bazel Authors. All rights reserved.
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"""Starlark rules for building C++ projects."""
16
17load("//cc/private/rules_impl:cc_flags_supplier.bzl", _cc_flags_supplier = "cc_flags_supplier")
18load("//cc/private/rules_impl:compiler_flag.bzl", _compiler_flag = "compiler_flag")
19load("//cc/private/rules_impl:native.bzl", "NativeCcInfo", "NativeCcToolchainConfigInfo", "NativeDebugPackageInfo", "native_cc_common")
20
21_MIGRATION_TAG = "__CC_RULES_MIGRATION_DO_NOT_USE_WILL_BREAK__"
22
23# TODO(bazel-team): To avoid breaking changes, if the below are no longer
24# forwarding to native rules, flag @bazel_tools@bazel_tools//tools/cpp:link_extra_libs
25# should either: (a) alias the flag @rules_cc//:link_extra_libs, or (b) be
26# added as a dependency to @rules_cc//:link_extra_lib. The intermediate library
27# @bazel_tools@bazel_tools//tools/cpp:link_extra_lib should either be added as a dependency
28# to @rules_cc//:link_extra_lib, or removed entirely (if possible).
29_LINK_EXTRA_LIB = "@rules_cc//:link_extra_lib"  # copybara-use-repo-external-label
30
31def _add_tags(attrs, is_binary = False):
32    if "tags" in attrs and attrs["tags"] != None:
33        attrs["tags"] = attrs["tags"] + [_MIGRATION_TAG]
34    else:
35        attrs["tags"] = [_MIGRATION_TAG]
36
37    if is_binary:
38        is_library = "linkshared" in attrs and attrs["linkshared"]
39
40        # Executable builds also include the "link_extra_lib" library.
41        if not is_library:
42            if "deps" in attrs and attrs["deps"] != None:
43                attrs["deps"] = attrs["deps"] + [_LINK_EXTRA_LIB]
44            else:
45                attrs["deps"] = [_LINK_EXTRA_LIB]
46
47    return attrs
48
49def cc_binary(**attrs):
50    """Bazel cc_binary rule.
51
52    https://docs.bazel.build/versions/main/be/c-cpp.html#cc_binary
53
54    Args:
55      **attrs: Rule attributes
56    """
57
58    # buildifier: disable=native-cc
59    native.cc_binary(**_add_tags(attrs, True))
60
61def cc_test(**attrs):
62    """Bazel cc_test rule.
63
64    https://docs.bazel.build/versions/main/be/c-cpp.html#cc_test
65
66    Args:
67      **attrs: Rule attributes
68    """
69
70    # buildifier: disable=native-cc
71    native.cc_test(**_add_tags(attrs, True))
72
73def cc_library(**attrs):
74    """Bazel cc_library rule.
75
76    https://docs.bazel.build/versions/main/be/c-cpp.html#cc_library
77
78    Args:
79      **attrs: Rule attributes
80    """
81
82    # buildifier: disable=native-cc
83    native.cc_library(**_add_tags(attrs))
84
85def cc_import(**attrs):
86    """Bazel cc_import rule.
87
88    https://docs.bazel.build/versions/main/be/c-cpp.html#cc_import
89
90    Args:
91      **attrs: Rule attributes
92    """
93
94    # buildifier: disable=native-cc
95    native.cc_import(**_add_tags(attrs))
96
97def cc_proto_library(**attrs):
98    """Bazel cc_proto_library rule.
99
100    https://docs.bazel.build/versions/main/be/c-cpp.html#cc_proto_library
101
102    Args:
103      **attrs: Rule attributes
104    """
105
106    # buildifier: disable=native-cc-proto
107    native.cc_proto_library(**_add_tags(attrs))
108
109def fdo_prefetch_hints(**attrs):
110    """Bazel fdo_prefetch_hints rule.
111
112    https://docs.bazel.build/versions/main/be/c-cpp.html#fdo_prefetch_hints
113
114    Args:
115      **attrs: Rule attributes
116    """
117
118    # buildifier: disable=native-cc
119    native.fdo_prefetch_hints(**_add_tags(attrs))
120
121def fdo_profile(**attrs):
122    """Bazel fdo_profile rule.
123
124    https://docs.bazel.build/versions/main/be/c-cpp.html#fdo_profile
125
126    Args:
127      **attrs: Rule attributes
128    """
129
130    # buildifier: disable=native-cc
131    native.fdo_profile(**_add_tags(attrs))
132
133def cc_toolchain(**attrs):
134    """Bazel cc_toolchain rule.
135
136    https://docs.bazel.build/versions/main/be/c-cpp.html#cc_toolchain
137
138    Args:
139      **attrs: Rule attributes
140    """
141
142    # buildifier: disable=native-cc
143    native.cc_toolchain(**_add_tags(attrs))
144
145def cc_toolchain_suite(**attrs):
146    """Bazel cc_toolchain_suite rule.
147
148    https://docs.bazel.build/versions/main/be/c-cpp.html#cc_toolchain_suite
149
150    Args:
151      **attrs: Rule attributes
152    """
153
154    # buildifier: disable=native-cc
155    native.cc_toolchain_suite(**_add_tags(attrs))
156
157def objc_library(**attrs):
158    """Bazel objc_library rule.
159
160    https://docs.bazel.build/versions/main/be/objective-c.html#objc_library
161
162    Args:
163      **attrs: Rule attributes
164    """
165
166    # buildifier: disable=native-cc
167    native.objc_library(**_add_tags(attrs))
168
169def objc_import(**attrs):
170    """Bazel objc_import rule.
171
172    https://docs.bazel.build/versions/main/be/objective-c.html#objc_import
173
174    Args:
175      **attrs: Rule attributes
176    """
177
178    # buildifier: disable=native-cc
179    native.objc_import(**_add_tags(attrs))
180
181def cc_flags_supplier(**attrs):
182    """Bazel cc_flags_supplier rule.
183
184    Args:
185      **attrs: Rule attributes
186    """
187    _cc_flags_supplier(**_add_tags(attrs))
188
189def compiler_flag(**attrs):
190    """Bazel compiler_flag rule.
191
192    Args:
193      **attrs: Rule attributes
194    """
195    _compiler_flag(**_add_tags(attrs))
196
197cc_common = native_cc_common
198
199CcInfo = NativeCcInfo
200
201CcToolchainConfigInfo = NativeCcToolchainConfigInfo
202
203DebugPackageInfo = NativeDebugPackageInfo
204