Skip to content

Commit

Permalink
Fix linking objc binaries in the root package
Browse files Browse the repository at this point in the history
Fixes bazelbuild#24625

Closes bazelbuild#24698.

PiperOrigin-RevId: 707183228
Change-Id: I369a37e8e652545274ec4949b1c3ac1d2fab38cb
  • Loading branch information
keith authored and bazel-io committed Dec 17, 2024
1 parent df9068c commit 919e820
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 11 deletions.
27 changes: 16 additions & 11 deletions src/main/starlark/builtins_bzl/common/objc/compilation_support.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ load(":common/objc/compilation_artifacts_info.bzl", "CompilationArtifactsInfo")
load(":common/objc/intermediate_artifacts.bzl", "create_intermediate_artifacts")
load(":common/objc/objc_common.bzl", "objc_common")
load(":common/objc/providers.bzl", "J2ObjcEntryClassInfo", "J2ObjcMappingFileInfo")
load(":common/paths.bzl", "paths")
load(":common/xcode/providers.bzl", "XcodeVersionInfo")

objc_internal = _builtins.internal.objc_internal
Expand Down Expand Up @@ -617,9 +618,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,
paths.join(
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
Expand Down Expand Up @@ -674,7 +679,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",
paths.join(ctx.label.package, ctx.label.name + "-linker.objlist"),
build_config.bin_dir,
)

Expand Down Expand Up @@ -712,7 +717,7 @@ def _register_binary_strip_action(
strip_safe = True

stripped_binary = ctx.actions.declare_shareable_artifact(
ctx.label.package + "/" + name,
paths.join(ctx.label.package, name),
build_config.bin_dir,
)
args = ctx.actions.args()
Expand Down Expand Up @@ -773,12 +778,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 = paths.join(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 = paths.join(
stamp_output_dir,
linkstamp_file.short_path[:-len(linkstamp_file.extension)].rstrip(".") + ".o",
)
stamp_output_file = ctx.actions.declare_shareable_artifact(
stamp_output_path,
Expand Down Expand Up @@ -851,12 +856,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",
paths.join(ctx.label.package, name + "_unstripped"),
build_config.bin_dir,
)
else:
binary = ctx.actions.declare_shareable_artifact(
ctx.label.package + "/" + name,
paths.join(ctx.label.package, name),
build_config.bin_dir,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,32 @@ 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"],
)
""");

assertThat(getConfiguredTarget("//:bin")).isNotNull();
}

@Test
public void testObjcRuleCanDependOnArbitraryStarlarkRuleThatProvidesCcInfo() throws Exception {
scratch.file("test_starlark/rule/BUILD");
Expand Down

0 comments on commit 919e820

Please sign in to comment.