From d58ca2dabf77470da9e531c37ad9b31183c9670e Mon Sep 17 00:00:00 2001 From: Keith Smiley Date: Fri, 13 Dec 2024 17:00:30 -0800 Subject: [PATCH] Fix linking objc binaries in the root package Fixes https://github.com/bazelbuild/bazel/issues/24625 --- .../common/objc/compilation_support.bzl | 29 ++++++++++++------- .../lib/rules/objc/ObjcStarlarkTest.java | 27 +++++++++++++++++ 2 files changed, 45 insertions(+), 11 deletions(-) diff --git a/src/main/starlark/builtins_bzl/common/objc/compilation_support.bzl b/src/main/starlark/builtins_bzl/common/objc/compilation_support.bzl index a37dc5b7b53dca..298694c970acdc 100644 --- a/src/main/starlark/builtins_bzl/common/objc/compilation_support.bzl +++ b/src/main/starlark/builtins_bzl/common/objc/compilation_support.bzl @@ -28,6 +28,9 @@ load(":common/xcode/providers.bzl", "XcodeVersionInfo") objc_internal = _builtins.internal.objc_internal +def _join_path(*path): + return "/".join([p for p in path if p != ""]) + def _build_variable_extensions(ctx, arc_enabled): extensions = {} if hasattr(ctx.attr, "pch") and ctx.attr.pch != None: @@ -642,9 +645,13 @@ def _register_j2objc_dead_code_removal_actions(common_variables, deps, build_con replace_libs = {} for j2objc_archive in common_variables.objc_provider.j2objc_library.to_list(): pruned_j2objc_archive = ctx.actions.declare_shareable_artifact( - ctx.label.package + "/_j2objc_pruned/" + ctx.label.name + "/" + - j2objc_archive.short_path[:-len(j2objc_archive.extension)].strip(".") + - "_pruned." + j2objc_archive.extension, + _join_path( + ctx.label.package, + "_j2objc_pruned", + ctx.label.name, + j2objc_archive.short_path[:-len(j2objc_archive.extension)].strip(".") + + "_pruned." + j2objc_archive.extension, + ), build_config.bin_dir, ) replace_libs[j2objc_archive] = pruned_j2objc_archive @@ -699,7 +706,7 @@ def _register_obj_filelist_action(ctx, build_config, obj_files): This File is suitable to signal symbols to archive in a libtool archiving invocation. """ obj_list = ctx.actions.declare_shareable_artifact( - ctx.label.package + "/" + ctx.label.name + "-linker.objlist", + _join_path(ctx.label.package, ctx.label.name + "-linker.objlist"), build_config.bin_dir, ) @@ -737,7 +744,7 @@ def _register_binary_strip_action( strip_safe = True stripped_binary = ctx.actions.declare_shareable_artifact( - ctx.label.package + "/" + name, + _join_path(ctx.label.package, name), build_config.bin_dir, ) args = ctx.actions.args() @@ -778,12 +785,12 @@ def _linkstamp_map(ctx, linkstamps, output, build_config): # create linkstamps_map - mapping from linkstamps to object files linkstamps_map = {} - stamp_output_dir = ctx.label.package + "/_objs/" + output.basename + "/" + stamp_output_dir = _join_path(ctx.label.package, "_objs", output.basename) for linkstamp in linkstamps.to_list(): linkstamp_file = linkstamp.file() - stamp_output_path = ( - stamp_output_dir + - linkstamp_file.short_path[:-len(linkstamp_file.extension)].rstrip(".") + ".o" + stamp_output_path = _join_path( + stamp_output_dir, + linkstamp_file.short_path[:-len(linkstamp_file.extension)].rstrip(".") + ".o", ) stamp_output_file = ctx.actions.declare_shareable_artifact( stamp_output_path, @@ -840,12 +847,12 @@ def _register_configuration_specific_link_actions( if (ctx.fragments.cpp.objc_enable_binary_stripping() and ctx.fragments.cpp.compilation_mode() == "opt"): binary = ctx.actions.declare_shareable_artifact( - ctx.label.package + "/" + name + "_unstripped", + _join_path(ctx.label.package, name + "_unstripped"), build_config.bin_dir, ) else: binary = ctx.actions.declare_shareable_artifact( - ctx.label.package + "/" + name, + _join_path(ctx.label.package, name), build_config.bin_dir, ) diff --git a/src/test/java/com/google/devtools/build/lib/rules/objc/ObjcStarlarkTest.java b/src/test/java/com/google/devtools/build/lib/rules/objc/ObjcStarlarkTest.java index 241cbfd6d777bd..0902b2e141379c 100644 --- a/src/test/java/com/google/devtools/build/lib/rules/objc/ObjcStarlarkTest.java +++ b/src/test/java/com/google/devtools/build/lib/rules/objc/ObjcStarlarkTest.java @@ -293,6 +293,33 @@ def my_rule_impl(ctx): .contains("test_starlark/apple_starlark/liblib.a"); } + @Test + public void testStarlarkLinkBinaryInRootPackage() throws Exception { + scratch.file("a.m"); + addAppleBinaryStarlarkRule(scratch); + scratch.file( + "BUILD", + """ + load("//test_starlark:apple_binary_starlark.bzl", "apple_binary_starlark") + + package(default_visibility = ["//visibility:public"]) + + objc_library( + name = "lib", + srcs = ["a.m"], + ) + + apple_binary_starlark( + name = "bin", + platform_type = "macos", + deps = [":lib"], + ) + """); + + ConfiguredTarget binaryTarget = getConfiguredTarget("//:bin"); + assertThat(getConfiguredTarget("//:bin")).isNotNull(); + } + @Test public void testObjcRuleCanDependOnArbitraryStarlarkRuleThatProvidesCcInfo() throws Exception { scratch.file("test_starlark/rule/BUILD");