• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2024 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"""cc_binary rule"""
15
16# TODO(bazel-team): To avoid breaking changes, if the below are no longer
17# forwarding to native rules, flag @bazel_tools@bazel_tools//tools/cpp:link_extra_libs
18# should either: (a) alias the flag @rules_cc//:link_extra_libs, or (b) be
19# added as a dependency to @rules_cc//:link_extra_lib. The intermediate library
20# @bazel_tools@bazel_tools//tools/cpp:link_extra_lib should either be added as a dependency
21# to @rules_cc//:link_extra_lib, or removed entirely (if possible).
22_LINK_EXTRA_LIB = Label("//:link_extra_lib")
23
24def cc_binary(**attrs):
25    """Bazel cc_binary rule.
26
27    https://docs.bazel.build/versions/main/be/c-cpp.html#cc_binary
28
29    Args:
30      **attrs: Rule attributes
31    """
32
33    is_library = "linkshared" in attrs and attrs["linkshared"]
34
35    # Executable builds also include the "link_extra_lib" library.
36    if not is_library:
37        if "deps" in attrs and attrs["deps"] != None:
38            attrs["deps"] = attrs["deps"] + [_LINK_EXTRA_LIB]
39        else:
40            attrs["deps"] = [_LINK_EXTRA_LIB]
41
42    # buildifier: disable=native-cc
43    native.cc_binary(**attrs)
44