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 #24625
  • Loading branch information
keith committed Dec 14, 2024
1 parent 48fe861 commit d58ca2d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 11 deletions.
29 changes: 18 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 @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
)

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit d58ca2d

Please sign in to comment.