From d8dfa8b8290ddc335c198f844b1c67125ef1e85c Mon Sep 17 00:00:00 2001 From: Googler Date: Thu, 16 Feb 2023 04:14:43 -0800 Subject: [PATCH] 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 --- BUILD | 20 ++++++++++++++++++++ cc/defs.bzl | 25 ++++++++++++++++++++++--- tests/simple_binary/BUILD | 28 ++++++++++++++++++++++++++++ tests/simple_binary/foo.cc | 15 +++++++++++++++ 4 files changed, 85 insertions(+), 3 deletions(-) create mode 100644 tests/simple_binary/BUILD create mode 100644 tests/simple_binary/foo.cc diff --git a/BUILD b/BUILD index 001dcb9..1ed7987 100644 --- a/BUILD +++ b/BUILD @@ -1,5 +1,25 @@ +load("//cc:defs.bzl", "cc_library") + package(default_visibility = ["//visibility:public"]) licenses(["notice"]) 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", + ], +) diff --git a/cc/defs.bzl b/cc/defs.bzl index 17140c7..a3acac7 100644 --- a/cc/defs.bzl +++ b/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__" -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: attrs["tags"] = attrs["tags"] + [_MIGRATION_TAG] else: 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 def cc_binary(**attrs): @@ -37,7 +56,7 @@ def cc_binary(**attrs): """ # buildifier: disable=native-cc - native.cc_binary(**_add_tags(attrs)) + native.cc_binary(**_add_tags(attrs, True)) def cc_test(**attrs): """Bazel cc_test rule. @@ -49,7 +68,7 @@ def cc_test(**attrs): """ # buildifier: disable=native-cc - native.cc_test(**_add_tags(attrs)) + native.cc_test(**_add_tags(attrs, True)) def cc_library(**attrs): """Bazel cc_library rule. diff --git a/tests/simple_binary/BUILD b/tests/simple_binary/BUILD new file mode 100644 index 0000000..c8d78a6 --- /dev/null +++ b/tests/simple_binary/BUILD @@ -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, +) diff --git a/tests/simple_binary/foo.cc b/tests/simple_binary/foo.cc new file mode 100644 index 0000000..cc38c10 --- /dev/null +++ b/tests/simple_binary/foo.cc @@ -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; }