Skip to content

Commit

Permalink
Add flag to turn off builtin objc strip action
Browse files Browse the repository at this point in the history
This is so we can migrate strip action to be part of linking, and controlled by
the clang driver.

PiperOrigin-RevId: 716810851
Change-Id: Ic4599e8313fbe097048423f3274a5eb8152c3546
  • Loading branch information
googlewalt authored and copybara-github committed Jan 17, 2025
1 parent 5653583 commit 7d50755
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public final class BuiltinRestriction {
BuiltinRestriction.allowlistEntry("build_bazel_rules_android", ""),

// Apple rules
BuiltinRestriction.allowlistEntry("", "third_party/apple_crosstool"),
BuiltinRestriction.allowlistEntry("", "third_party/bazel_rules/rules_apple"),
BuiltinRestriction.allowlistEntry("rules_apple", ""),
BuiltinRestriction.allowlistEntry("build_bazel_rules_apple", ""),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,13 @@ public class ObjcCommandLineOptions extends FragmentOptions {
"If true, strip action for executables will use flag -x, which does not break dynamic "
+ "symbol resolution.")
public boolean incompatibleStripExecutableSafely;

@Option(
name = "incompatible_builtin_objc_strip_action",
defaultValue = "true",
documentationCategory = OptionDocumentationCategory.TOOLCHAIN,
effectTags = {OptionEffectTag.ACTION_COMMAND_LINES},
metadataTags = {OptionMetadataTag.INCOMPATIBLE_CHANGE},
help = "Whether to emit a strip action as part of objc linking.")
public boolean incompatibleBuiltinObjcStripAction;
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public class ObjcConfiguration extends Fragment implements ObjcConfigurationApi
private final boolean disallowSdkFrameworksAttributes;
private final boolean alwayslinkByDefault;
private final boolean stripExecutableSafely;
private final boolean builtinObjcStripAction;

public ObjcConfiguration(BuildOptions buildOptions) {
CoreOptions options = buildOptions.get(CoreOptions.class);
Expand All @@ -81,6 +82,7 @@ public ObjcConfiguration(BuildOptions buildOptions) {
this.disallowSdkFrameworksAttributes = objcOptions.incompatibleDisallowSdkFrameworksAttributes;
this.alwayslinkByDefault = objcOptions.incompatibleObjcAlwayslinkByDefault;
this.stripExecutableSafely = objcOptions.incompatibleStripExecutableSafely;
this.builtinObjcStripAction = objcOptions.incompatibleBuiltinObjcStripAction;
}

/**
Expand Down Expand Up @@ -191,4 +193,10 @@ public boolean targetShouldAlwayslink(StarlarkRuleContext ruleContext, StarlarkT
public boolean stripExecutableSafely() {
return stripExecutableSafely;
}

/** Returns whether to emit a strip action as part of objc linking. */
@Override
public boolean builtinObjcStripAction() {
return builtinObjcStripAction;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,10 @@ boolean targetShouldAlwayslink(StarlarkRuleContext ruleContext, StarlarkThread t
"Returns whether executable strip action should use flag -x, which does not break "
+ "dynamic symbol resolution.")
boolean stripExecutableSafely();

@StarlarkMethod(
name = "builtin_objc_strip_action",
structField = true,
doc = "Returns whether to emit a strip action as part of objc linking.")
boolean builtinObjcStripAction();
}
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@ bazel_fragments["JavaOptions"] = fragment(
bazel_fragments["ObjcCommandLineOptions"] = fragment(
propagate = [
"//command_line_option:incompatible_avoid_hardcoded_objc_compilation_flags",
"//command_line_option:incompatible_builtin_objc_strip_action",
"//command_line_option:incompatible_disallow_sdk_frameworks_attributes",
"//command_line_option:incompatible_objc_alwayslink_by_default",
"//command_line_option:incompatible_strip_executable_safely",
Expand Down
31 changes: 21 additions & 10 deletions src/main/starlark/builtins_bzl/common/objc/compilation_support.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,13 @@ def _classify_libraries(libraries_to_link):
}
return always_link_libraries.keys(), as_needed_libraries.keys()

def _emit_builtin_objc_strip_action(ctx):
return (
ctx.fragments.objc.builtin_objc_strip_action and
ctx.fragments.cpp.objc_enable_binary_stripping() and
ctx.fragments.cpp.compilation_mode() == "opt"
)

def _register_configuration_specific_link_actions(
name,
common_variables,
Expand Down Expand Up @@ -717,8 +724,7 @@ def _register_configuration_specific_link_actions(
# symbols for dead-code removal. The binary is also used to generate dSYM bundle if
# --apple_generate_dsym is specified. A symbol strip action is later registered to strip
# the symbol table from the unstripped binary.
if (ctx.fragments.cpp.objc_enable_binary_stripping() and
ctx.fragments.cpp.compilation_mode() == "opt"):
if _emit_builtin_objc_strip_action(ctx):
binary = ctx.actions.declare_shareable_artifact(
paths.join(ctx.label.package, name + "_unstripped"),
build_config.bin_dir,
Expand Down Expand Up @@ -817,10 +823,7 @@ def _register_configuration_specific_link_actions_with_cpp_variables(
variables_extension = user_variable_extensions,
)

if not (ctx.fragments.cpp.objc_enable_binary_stripping() and
ctx.fragments.cpp.compilation_mode() == "opt"):
return binary
else:
if _emit_builtin_objc_strip_action(ctx):
return _register_binary_strip_action(
ctx,
name,
Expand All @@ -829,6 +832,8 @@ def _register_configuration_specific_link_actions_with_cpp_variables(
build_config,
extra_link_args,
)
else:
return binary

def _dedup_link_flags(flags, seen_flags = {}):
new_flags = []
Expand Down Expand Up @@ -985,11 +990,17 @@ def _register_configuration_specific_link_actions_with_objc_variables(
main_output = binary,
)

if not (ctx.fragments.cpp.objc_enable_binary_stripping() and
ctx.fragments.cpp.compilation_mode() == "opt"):
return binary
if _emit_builtin_objc_strip_action(ctx):
return _register_binary_strip_action(
ctx,
name,
binary,
feature_configuration,
build_config,
extra_link_args,
)
else:
return _register_binary_strip_action(ctx, name, binary, feature_configuration, build_config, extra_link_args)
return binary

compilation_support = struct(
register_compile_and_archive_actions = _register_compile_and_archive_actions,
Expand Down

0 comments on commit 7d50755

Please sign in to comment.