diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/android/android_ndk_build_file_template.txt b/src/main/java/com/google/devtools/build/lib/bazel/rules/android/android_ndk_build_file_template.txt index b8e58e5d5a6262..382c84a16380c8 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/rules/android/android_ndk_build_file_template.txt +++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/android/android_ndk_build_file_template.txt @@ -24,6 +24,11 @@ filegroup( srcs = ["ndk"], ) +cc_library( + name = "link_extra_lib", + srcs = [], +) + cc_library( name = "malloc", srcs = [], diff --git a/src/main/starlark/builtins_bzl/common/cc/cc_binary_attrs.bzl b/src/main/starlark/builtins_bzl/common/cc/cc_binary_attrs.bzl index 8ac65b939d8941..447022316b3f42 100644 --- a/src/main/starlark/builtins_bzl/common/cc/cc_binary_attrs.bzl +++ b/src/main/starlark/builtins_bzl/common/cc/cc_binary_attrs.bzl @@ -70,6 +70,11 @@ cc_binary_attrs_with_aspects = { default = configuration_field(fragment = "cpp", name = "custom_malloc"), aspects = [graph_structure_aspect], ), + "_link_extra_lib": attr.label( + default = Label("@" + semantics.get_repo() + "//tools/cpp:link_extra_lib"), + providers = [CcInfo], + aspects = [graph_structure_aspect], + ), "stamp": attr.int( values = [-1, 0, 1], default = -1, @@ -112,3 +117,6 @@ cc_binary_attrs_without_aspects["malloc"] = attr.label( cc_binary_attrs_without_aspects["_default_malloc"] = attr.label( default = configuration_field(fragment = "cpp", name = "custom_malloc"), ) +cc_binary_attrs_without_aspects["_link_extra_lib"] = attr.label( + default = Label("@" + semantics.get_repo() + "//tools/cpp:link_extra_lib"), +) diff --git a/src/main/starlark/builtins_bzl/common/cc/semantics.bzl b/src/main/starlark/builtins_bzl/common/cc/semantics.bzl index db2b0a351fe2f0..52421ce4fc363d 100644 --- a/src/main/starlark/builtins_bzl/common/cc/semantics.bzl +++ b/src/main/starlark/builtins_bzl/common/cc/semantics.bzl @@ -132,9 +132,15 @@ def _get_coverage_env(ctx): def _get_cc_runtimes(ctx, is_library): if is_library: return [] + + runtimes = [ctx.attr._link_extra_lib] + if ctx.fragments.cpp.custom_malloc != None: - return [ctx.attr._default_malloc] - return [ctx.attr.malloc] + runtimes.append(ctx.attr._default_malloc) + else: + runtimes.append(ctx.attr.malloc) + + return runtimes def _should_use_legacy_cc_test(_): return True diff --git a/src/test/java/com/google/devtools/build/lib/packages/util/Crosstool.java b/src/test/java/com/google/devtools/build/lib/packages/util/Crosstool.java index a64b2f0902d78d..15f1b8be7c89e9 100644 --- a/src/test/java/com/google/devtools/build/lib/packages/util/Crosstool.java +++ b/src/test/java/com/google/devtools/build/lib/packages/util/Crosstool.java @@ -583,6 +583,8 @@ public void write() throws IOException { " name = 'interface_library_builder',", " srcs = ['build_interface_so'],", ")", + // We add an empty :link_extra_lib target in case we need it. + "cc_library(name = 'link_extra_lib')", // We add an empty :malloc target in case we need it. "cc_library(name = 'malloc')", // Fake targets to get us through loading/analysis. diff --git a/src/test/java/com/google/devtools/build/lib/query2/testutil/AbstractQueryTest.java b/src/test/java/com/google/devtools/build/lib/query2/testutil/AbstractQueryTest.java index fe9c07eac4f8fd..8c24a7e41a6e04 100644 --- a/src/test/java/com/google/devtools/build/lib/query2/testutil/AbstractQueryTest.java +++ b/src/test/java/com/google/devtools/build/lib/query2/testutil/AbstractQueryTest.java @@ -684,6 +684,8 @@ public void testDeps() throws Exception { } String expectedDependencies = helper.getToolsRepository() + + "//tools/cpp:link_extra_lib + " + + helper.getToolsRepository() + "//tools/cpp:malloc + //configurable:main + " + "//configurable:main.cc + //configurable:adep + //configurable:bdep + " + "//configurable:defaultdep + //conditions:a + //conditions:b " @@ -1016,6 +1018,7 @@ public void testNoImplicitDeps() throws Exception { // Implicit dependencies: String hostDepsExpr = helper.getToolsRepository() + "//tools/cpp:malloc"; + hostDepsExpr += " + " + helper.getToolsRepository() + "//tools/cpp:link_extra_lib"; if (!analysisMock.isThisBazel()) { hostDepsExpr += " + //tools/cpp:malloc.cc"; } diff --git a/src/test/shell/bazel/cc_flags_supplier_test.sh b/src/test/shell/bazel/cc_flags_supplier_test.sh index 393eff317abccb..d85ffae31460e2 100755 --- a/src/test/shell/bazel/cc_flags_supplier_test.sh +++ b/src/test/shell/bazel/cc_flags_supplier_test.sh @@ -56,6 +56,11 @@ function write_crosstool() { package(default_visibility = ["//visibility:public"]) load(":cc_toolchain_config.bzl", "cc_toolchain_config") + +cc_library( + name = "link_extra_lib", +) + cc_library( name = "malloc", ) diff --git a/src/test/shell/integration/cpp_test.sh b/src/test/shell/integration/cpp_test.sh index c48ccddfff28e8..4d49349bad63bd 100755 --- a/src/test/shell/integration/cpp_test.sh +++ b/src/test/shell/integration/cpp_test.sh @@ -168,6 +168,10 @@ package(default_visibility = ["//visibility:public"]) load(":toolchain.bzl", "toolchains") +cc_library( + name = "link_extra_lib", +) + cc_library( name = "malloc", ) diff --git a/tools/cpp/BUILD.empty.tpl b/tools/cpp/BUILD.empty.tpl index 3c89a1f1914496..89b491877e7268 100644 --- a/tools/cpp/BUILD.empty.tpl +++ b/tools/cpp/BUILD.empty.tpl @@ -16,6 +16,10 @@ load(":cc_toolchain_config.bzl", "cc_toolchain_config") package(default_visibility = ["//visibility:public"]) +cc_library( + name = "link_extra_lib", +) + cc_library( name = "malloc", ) diff --git a/tools/cpp/BUILD.static.bsd b/tools/cpp/BUILD.static.bsd index b69d3b679cbaee..b0d52e651d29d2 100644 --- a/tools/cpp/BUILD.static.bsd +++ b/tools/cpp/BUILD.static.bsd @@ -19,6 +19,24 @@ load("@rules_cc//cc:defs.bzl", "cc_library", "cc_toolchain", "cc_toolchain_suite package(default_visibility = ["//visibility:public"]) +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", + ], +) + cc_library( name = "malloc", ) diff --git a/tools/cpp/BUILD.tools b/tools/cpp/BUILD.tools index 41b5d666002be7..27c1d3a2b12a6e 100644 --- a/tools/cpp/BUILD.tools +++ b/tools/cpp/BUILD.tools @@ -66,6 +66,28 @@ cc_host_toolchain_alias(name = "current_cc_host_toolchain") cc_libc_top_alias(name = "current_libc_top") +cc_library( + name = "empty_lib", + tags = ["__DONT_DEPEND_ON_DEF_PARSER__"], +) + +# 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", + tags = ["__DONT_DEPEND_ON_DEF_PARSER__"], + deps = [ + ":link_extra_libs", + ], +) + cc_library( name = "malloc", tags = ["__DONT_DEPEND_ON_DEF_PARSER__"], diff --git a/tools/cpp/BUILD.tpl b/tools/cpp/BUILD.tpl index e50013d474e290..384b63a12c0c53 100644 --- a/tools/cpp/BUILD.tpl +++ b/tools/cpp/BUILD.tpl @@ -22,6 +22,24 @@ package(default_visibility = ["//visibility:public"]) licenses(["notice"]) # Apache 2.0 +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", + ], +) + cc_library( name = "malloc", ) diff --git a/tools/cpp/BUILD.windows.tpl b/tools/cpp/BUILD.windows.tpl index dc66fd7c6d3728..0151325835be06 100644 --- a/tools/cpp/BUILD.windows.tpl +++ b/tools/cpp/BUILD.windows.tpl @@ -20,6 +20,24 @@ load(":armeabi_cc_toolchain_config.bzl", "armeabi_cc_toolchain_config") package(default_visibility = ["//visibility:public"]) +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", + ], +) + cc_library( name = "malloc", ) diff --git a/tools/jdk/BUILD.java_tools b/tools/jdk/BUILD.java_tools index e210eff23130fc..119d3287c19ac5 100644 --- a/tools/jdk/BUILD.java_tools +++ b/tools/jdk/BUILD.java_tools @@ -135,21 +135,25 @@ config_setting( visibility = ["//visibility:public"], ) +# Create intermediate cc_library, which does not implicitly depend on "malloc" +# and "link_extra_lib" in @bazel_tools//tools/cpp, and thereby avoids include +# path /Iexternal/tools being used in compiling actions which would result in +# the wrong headers being picked up. cc_library( - name = "malloc", -) - -cc_binary( - name = "ijar_cc_binary", + name = "ijar_cc_binary_main", srcs = [ "java_tools/ijar/classfile.cc", "java_tools/ijar/ijar.cc", ], copts = SUPRESSED_WARNINGS, - # Remove dependency on @bazel_tools//tools/cpp:malloc, which avoid /Iexternal/tools being used - # in compiling actions. - malloc = ":malloc", + linkstatic = 1, # provides main() deps = [":zip"], + alwayslink = 1, +) + +cc_binary( + name = "ijar_cc_binary", + deps = [":ijar_cc_binary_main"], ) cc_library( @@ -364,8 +368,9 @@ cc_library( ##################### singlejar -cc_binary( - name = "singlejar_cc_bin", +# See comment for ":ijar_cc_binary_main". +cc_library( + name = "singlejar_cc_bin_main", srcs = [ "java_tools/src/tools/singlejar/singlejar_main.cc", ], @@ -375,11 +380,7 @@ cc_binary( ":openbsd": ["-lm"], "//conditions:default": [], }), - linkstatic = 1, - # Remove dependency on @bazel_tools//tools/cpp:malloc, which avoid /Iexternal/tools being used - # in compiling actions. - malloc = ":malloc", - visibility = ["//visibility:public"], + linkstatic = 1, # provides main() deps = [ ":combiners", ":diag", @@ -387,6 +388,14 @@ cc_binary( ":output_jar", "//java_tools/zlib", ], + alwayslink = 1, +) + +cc_binary( + name = "singlejar_cc_bin", + visibility = ["//visibility:public"], + linkstatic = 1, + deps = [":singlejar_cc_bin_main"], ) cc_binary( diff --git a/tools/osx/crosstool/BUILD.tpl b/tools/osx/crosstool/BUILD.tpl index 420db4d25d5cb8..593d269628fd44 100644 --- a/tools/osx/crosstool/BUILD.tpl +++ b/tools/osx/crosstool/BUILD.tpl @@ -27,6 +27,10 @@ CC_TOOLCHAINS = [( ("armeabi-v7a", ":cc-compiler-armeabi-v7a"), ] +cc_library( + name = "link_extra_lib", +) + cc_library( name = "malloc", )