mirror of https://github.com/bazelbuild/rules_cc
Introduce flag --@rules_cc//:link_extra_libs
Numerous tools override --custom_malloc to add debugging or monitoring runtimes (see e.g. sanitizers). While this is fine for cases where the tool must also override malloc to function, in other cases it's simply misuse of --custom_malloc where no other mechanism exists to link an extra library. This becomes especially problematic where a runtime library is supposed to be added in certain configurations that should run in production or other performance sensitive builds. In these cases, we should _not_ override malloc, which may also be specified by a cc_binary target. Doing so would introduce unwanted changes, potentially affecting performance negatively. This is the @rules_cc counterpart to the equivalent Bazel tools flag --@bazel_tools//tools/cpp:link_extra_libs. Users that use @rules_cc to build their C++ projects may use both flags interchangably, however, the @rules_cc flag should be preferred. PiperOrigin-RevId: 510103352 Change-Id: Iafccd00ffdb65cb4f953d5acadc451cffc134533
This commit is contained in:
parent
a43f67b72c
commit
d8dfa8b829
20
BUILD
20
BUILD
|
@ -1,5 +1,25 @@
|
||||||
|
load("//cc:defs.bzl", "cc_library")
|
||||||
|
|
||||||
package(default_visibility = ["//visibility:public"])
|
package(default_visibility = ["//visibility:public"])
|
||||||
|
|
||||||
licenses(["notice"])
|
licenses(["notice"])
|
||||||
|
|
||||||
exports_files(["LICENSE"])
|
exports_files(["LICENSE"])
|
||||||
|
|
||||||
|
cc_library(name = "empty_lib")
|
||||||
|
|
||||||
|
# Label flag for extra libraries to be linked into every binary.
|
||||||
|
# TODO(bazel-team): Support passing flag multiple times to build a list.
|
||||||
|
label_flag(
|
||||||
|
name = "link_extra_libs",
|
||||||
|
build_setting_default = ":empty_lib",
|
||||||
|
)
|
||||||
|
|
||||||
|
# The final extra library to be linked into every binary target. This collects
|
||||||
|
# the above flag, but may also include more libraries depending on config.
|
||||||
|
cc_library(
|
||||||
|
name = "link_extra_lib",
|
||||||
|
deps = [
|
||||||
|
":link_extra_libs",
|
||||||
|
],
|
||||||
|
)
|
||||||
|
|
25
cc/defs.bzl
25
cc/defs.bzl
|
@ -20,11 +20,30 @@ load("//cc/private/rules_impl:native.bzl", "NativeCcInfo", "NativeCcToolchainCon
|
||||||
|
|
||||||
_MIGRATION_TAG = "__CC_RULES_MIGRATION_DO_NOT_USE_WILL_BREAK__"
|
_MIGRATION_TAG = "__CC_RULES_MIGRATION_DO_NOT_USE_WILL_BREAK__"
|
||||||
|
|
||||||
def _add_tags(attrs):
|
# TODO(bazel-team): To avoid breaking changes, if the below are no longer
|
||||||
|
# forwarding to native rules, flag @bazel_tools@bazel_tools//tools/cpp:link_extra_libs
|
||||||
|
# should either: (a) alias the flag @rules_cc//:link_extra_libs, or (b) be
|
||||||
|
# added as a dependency to @rules_cc//:link_extra_lib. The intermediate library
|
||||||
|
# @bazel_tools@bazel_tools//tools/cpp:link_extra_lib should either be added as a dependency
|
||||||
|
# to @rules_cc//:link_extra_lib, or removed entirely (if possible).
|
||||||
|
_LINK_EXTRA_LIB = "@rules_cc//:link_extra_lib" # copybara-use-repo-external-label
|
||||||
|
|
||||||
|
def _add_tags(attrs, is_binary = False):
|
||||||
if "tags" in attrs and attrs["tags"] != None:
|
if "tags" in attrs and attrs["tags"] != None:
|
||||||
attrs["tags"] = attrs["tags"] + [_MIGRATION_TAG]
|
attrs["tags"] = attrs["tags"] + [_MIGRATION_TAG]
|
||||||
else:
|
else:
|
||||||
attrs["tags"] = [_MIGRATION_TAG]
|
attrs["tags"] = [_MIGRATION_TAG]
|
||||||
|
|
||||||
|
if is_binary:
|
||||||
|
is_library = "linkshared" in attrs and attrs["linkshared"]
|
||||||
|
|
||||||
|
# Executable builds also include the "link_extra_lib" library.
|
||||||
|
if not is_library:
|
||||||
|
if "deps" in attrs and attrs["deps"] != None:
|
||||||
|
attrs["deps"] = attrs["deps"] + [_LINK_EXTRA_LIB]
|
||||||
|
else:
|
||||||
|
attrs["deps"] = [_LINK_EXTRA_LIB]
|
||||||
|
|
||||||
return attrs
|
return attrs
|
||||||
|
|
||||||
def cc_binary(**attrs):
|
def cc_binary(**attrs):
|
||||||
|
@ -37,7 +56,7 @@ def cc_binary(**attrs):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# buildifier: disable=native-cc
|
# buildifier: disable=native-cc
|
||||||
native.cc_binary(**_add_tags(attrs))
|
native.cc_binary(**_add_tags(attrs, True))
|
||||||
|
|
||||||
def cc_test(**attrs):
|
def cc_test(**attrs):
|
||||||
"""Bazel cc_test rule.
|
"""Bazel cc_test rule.
|
||||||
|
@ -49,7 +68,7 @@ def cc_test(**attrs):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# buildifier: disable=native-cc
|
# buildifier: disable=native-cc
|
||||||
native.cc_test(**_add_tags(attrs))
|
native.cc_test(**_add_tags(attrs, True))
|
||||||
|
|
||||||
def cc_library(**attrs):
|
def cc_library(**attrs):
|
||||||
"""Bazel cc_library rule.
|
"""Bazel cc_library rule.
|
||||||
|
|
|
@ -0,0 +1,28 @@
|
||||||
|
# Copyright 2023 The Bazel Authors. All rights reserved.
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
# you may not use this file except in compliance with the License.
|
||||||
|
# You may obtain a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
# See the License for the specific language governing permissions and
|
||||||
|
# limitations under the License.
|
||||||
|
|
||||||
|
load("//cc:defs.bzl", "cc_binary")
|
||||||
|
|
||||||
|
licenses(["notice"])
|
||||||
|
|
||||||
|
cc_binary(
|
||||||
|
name = "foo",
|
||||||
|
srcs = ["foo.cc"],
|
||||||
|
)
|
||||||
|
|
||||||
|
cc_binary(
|
||||||
|
name = "libfoo.so",
|
||||||
|
srcs = ["foo.cc"],
|
||||||
|
linkshared = 1,
|
||||||
|
)
|
|
@ -0,0 +1,15 @@
|
||||||
|
// Copyright 2023 The Bazel Authors. All rights reserved.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]) { return 0; }
|
Loading…
Reference in New Issue