Skip to content

Commit

Permalink
Propagate linkopts of header-only libraries to cc_shared_library
Browse files Browse the repository at this point in the history
Fixes bazelbuild#21884
Fixes bazelbuild#23053

Closes bazelbuild#23215.

PiperOrigin-RevId: 686518093
Change-Id: I1a6699b169fe4291ad0dda3be7e74f38c8e37bf1
  • Loading branch information
fmeum authored and copybara-github committed Oct 16, 2024
1 parent 2a5a004 commit 0fdb36f
Show file tree
Hide file tree
Showing 70 changed files with 97 additions and 69 deletions.
15 changes: 9 additions & 6 deletions src/main/starlark/builtins_bzl/common/cc/cc_shared_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,15 @@ def _filter_inputs(
continue
linker_inputs_seen[stringified_linker_input] = True
owner = str(linker_input.owner)
if owner in targets_to_be_linked_dynamically_set:
if semantics.is_bazel and not linker_input.libraries:
# Linker inputs that only provide flags, no code, are considered
# safe to link statically multiple times.
# TODO(bazel-team): semantics.should_create_empty_archive() should be
# cleaned up and return False in every case. cc_libraries shouldn't
# produce empty archives. For now issue #19920 is only fixed in Bazel.
_add_linker_input_to_dict(linker_input.owner, linker_input)
linker_inputs_count += 1
elif owner in targets_to_be_linked_dynamically_set:
unused_dynamic_linker_inputs[transitive_exports[owner].owner] = None

# Link the library in this iteration dynamically,
Expand All @@ -455,11 +463,6 @@ def _filter_inputs(
_add_linker_input_to_dict(linker_input.owner, transitive_exports[owner])
linker_inputs_count += 1
elif owner in targets_to_be_linked_statically_map:
if semantics.is_bazel and not linker_input.libraries:
# TODO(bazel-team): semantics.should_create_empty_archive() should be
# cleaned up and return False in every case. cc_libraries shouldn't
# produce empty archives. For now issue #19920 is only fixed in Bazel.
continue
if owner in link_once_static_libs_map:
# We are building a dictionary that will allow us to give
# proper errors for libraries that have been linked multiple
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ NO_BUILD_TAGS = [
licenses(["notice"])

package(
default_visibility = ["//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library:__subpackages__"],
default_visibility = ["//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test:__subpackages__"],
)

py_test(
Expand Down Expand Up @@ -119,7 +119,7 @@ cc_shared_library(
}),
dynamic_deps = [
"bar_so",
"//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library3:diff_pkg_so",
"//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test3:diff_pkg_so",
"private_lib_so",
],
exports_filter = [
Expand Down Expand Up @@ -176,7 +176,7 @@ cc_library(
# Not exported.
"qux",
"prebuilt",
"//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library3:diff_pkg"
"//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test3:diff_pkg"
],
)

Expand Down Expand Up @@ -275,7 +275,7 @@ cc_shared_library(
}),
exports_filter = [
"bar3", # Exported transitive dependency
"//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library3:bar",
"//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test3:bar",
],
features = ["windows_export_all_symbols"],
user_link_flags = select({
Expand Down Expand Up @@ -330,7 +330,7 @@ cc_library(
srcs = ["bar3.cc"],
hdrs = ["bar3.h"],
deps = [
"//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library3:bar",
"//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test3:bar",
],
)

Expand Down Expand Up @@ -371,17 +371,17 @@ additional_inputs_test(
build_failure_test(
name = "link_once_repeated_test_binary",
messages = [
"cc_shared_library/test_cc_shared_library:barX\",",
"cc_shared_library/test:barX\",",
],
target = "//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/failing_targets:should_fail_binary",
target = "//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test/failing_targets:should_fail_binary",
)

build_failure_test(
name = "link_once_repeated_test_shared_lib",
messages = [
"cc_shared_library/test_cc_shared_library:barX\",",
"cc_shared_library/test:barX\",",
],
target = "//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/failing_targets:should_fail_shared_lib",
target = "//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test/failing_targets:should_fail_shared_lib",
)

paths_test(
Expand Down Expand Up @@ -473,6 +473,9 @@ genrule(
cc_library(
name = "hdr_only",
hdrs = [":hdr_only_hdr"],
linkopts = [
"-Wl,-rpath,hdr_only",
],
)

cc_library(
Expand All @@ -486,22 +489,22 @@ cc_library(
cc_shared_library(
name = "external_export_so",
exports_filter = ["@my_test_repo//:__pkg__"],
tags = ["bazel_only"],
deps = [
":external_export",
],
tags = ["bazel_only"],
)

build_failure_test(
name = "two_dynamic_deps_same_export_in_so_test",
message = "Two shared libraries in dependencies export the same symbols",
target = "//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/failing_targets:two_dynamic_deps_same_export_in_so",
target = "//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test/failing_targets:two_dynamic_deps_same_export_in_so",
)

build_failure_test(
name = "two_dynamic_deps_same_export_in_binary_test",
message = "Two shared libraries in dependencies link the same library statically",
target = "//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/failing_targets:two_dynamic_deps_same_export_in_binary",
target = "//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test/failing_targets:two_dynamic_deps_same_export_in_binary",
)

interface_library_output_group_test(
Expand Down Expand Up @@ -532,36 +535,36 @@ check_linking_action_lib_parameters_test(
build_failure_test(
name = "shared_library_without_deps",
message = "'cc_shared_library' must have at least one dependency in 'deps' (or 'roots')",
target = "//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/failing_targets:failing_with_no_deps_so",
target = "//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test/failing_targets:failing_with_no_deps_so",
)

build_failure_test(
name = "direct_dep_with_only_shared_lib_file",
message = "Do not place libraries which only contain a precompiled dynamic library",
target = "//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/failing_targets:failing_only_dynamic_lib",
target = "//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test/failing_targets:failing_only_dynamic_lib",
)

exports_test(
name = "exports_foo_test",
target = ":foo_so",
targets_that_should_be_claimed_to_be_exported = [
"//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library:indirect_dep2",
"//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library:baz",
"//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library:foo",
"//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library:cc_lib_with_no_srcs",
"//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library:nocode_cc_lib",
"//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library:should_not_be_linked_cc_lib",
"//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library:a_suffix",
"//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test:indirect_dep2",
"//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test:baz",
"//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test:foo",
"//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test:cc_lib_with_no_srcs",
"//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test:nocode_cc_lib",
"//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test:should_not_be_linked_cc_lib",
"//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test:a_suffix",
],
)

exports_test(
name = "external_export_exports_test",
target = "external_export_so",
bazel_only = True,
target = "external_export_so",
targets_that_should_be_claimed_to_be_exported = [
"@@test_repo+//:bar",
"//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library:external_export",
"//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test:external_export",
],
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
// 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.
#include "src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/a_suffix.h"
#include "src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test/a_suffix.h"

int a_suffix() { return 42; }
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
// 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.
#include "src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/bar.h"
#include "src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test/bar.h"

int bar() { return 42; }
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
// 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.
#include "src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/bar2.h"
#include "src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test/bar2.h"

int bar2() { return 42; }
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
// 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.
#include "src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/bar3.h"
#include "src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test/bar3.h"

int bar3() { return 42; }
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
// 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.
#include "src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/barX.h"
#include "src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test/barX.h"

int barX() { return 42; }
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
// 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.
#include "src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/baz.h"
#include "src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test/baz.h"

int baz() { return 42; }
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include "src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/direct_so_file_cc_lib.h"
#include "src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test/direct_so_file_cc_lib.h"
int direct_so_file_cc_lib() { return 0; }
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include "src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/direct_so_file_cc_lib2.h"
#include "src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test/direct_so_file_cc_lib2.h"
int direct_so_file_cc_lib2() { return 0; }
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ TAGS = [

cc_binary(
name = "should_fail_binary",
dynamic_deps = ["//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library:bar_so"],
dynamic_deps = ["//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test:bar_so"],
tags = TAGS,
deps = [
"//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library:barX",
"//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test:barX",
],
)

cc_shared_library(
name = "should_fail_shared_lib",
dynamic_deps = ["//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library:bar_so"],
dynamic_deps = ["//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test:bar_so"],
deps = [
":intermediate",
],
Expand All @@ -28,7 +28,7 @@ cc_shared_library(
cc_library(
name = "intermediate",
deps = [
"//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library:barX",
"//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test:barX",
],
)

Expand Down Expand Up @@ -112,4 +112,3 @@ genrule(
outs = ["libabc.so"],
cmd = "touch \"$@\"",
)

Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
// 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.
#include "src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/bar.h"
#include "src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/baz.h"
#include "src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/direct_so_file_cc_lib.h"
#include "src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/direct_so_file_cc_lib2.h"
#include "src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/qux.h"
#include "src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library3/diff_pkg.h"
#include "src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test/bar.h"
#include "src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test/baz.h"
#include "src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test/direct_so_file_cc_lib.h"
#include "src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test/direct_so_file_cc_lib2.h"
#include "src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test/qux.h"
#include "src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test3/diff_pkg.h"

int foo() {
diff_pkg();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.
#include <iostream>

#include "src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/foo.h"
#include "src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test/foo.h"

int main() {
std::cout << "hello " << foo() << std::endl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// 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.
#include "src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/bar.h"
#include "src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test/bar.h"

int foo() {
bar();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
// 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.
#include "src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/qux.h"
#include "src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test/qux.h"

int qux() { return 42; }
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
// 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.
#include "src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/qux2.h"
#include "src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test/qux2.h"

int qux2() { return 42; }
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright 2024 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.

"""Semantics for Bazel cc_shared_library integration tests."""

semantics = struct(
is_bazel = True,
)
Loading

0 comments on commit 0fdb36f

Please sign in to comment.