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; }