diff --git a/src/main/java/com/google/devtools/build/lib/rules/objc/AppleBinary.java b/src/main/java/com/google/devtools/build/lib/rules/objc/AppleBinary.java index 5549b9c6c04969..b7f8f8c481b377 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/objc/AppleBinary.java +++ b/src/main/java/com/google/devtools/build/lib/rules/objc/AppleBinary.java @@ -67,6 +67,8 @@ public static AppleLinkingOutputs linkMultiArchBinary( ImmutableList avoidDeps, Iterable extraLinkopts, Iterable extraLinkInputs, + Iterable extraRequestedFeatures, + Iterable extraDisabledFeatures, boolean isStampingEnabled) throws InterruptedException, RuleErrorException, ActionConflictException { Map, List> splitDeps = @@ -137,6 +139,8 @@ public static AppleLinkingOutputs linkMultiArchBinary( dependencySpecificConfiguration, new ExtraLinkArgs(allLinkopts.build()), allLinkInputs.build(), + extraRequestedFeatures, + extraDisabledFeatures, isStampingEnabled, propagatedDeps, outputGroupCollector); diff --git a/src/main/java/com/google/devtools/build/lib/rules/objc/AppleStarlarkCommon.java b/src/main/java/com/google/devtools/build/lib/rules/objc/AppleStarlarkCommon.java index 2f9ddace26945d..d1da07021e1892 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/objc/AppleStarlarkCommon.java +++ b/src/main/java/com/google/devtools/build/lib/rules/objc/AppleStarlarkCommon.java @@ -258,6 +258,8 @@ public StructImpl linkMultiArchBinary( Object avoidDeps, Sequence extraLinkopts, Sequence extraLinkInputs, + Sequence extraRequestedFeatures, + Sequence extraDisabledFeatures, StarlarkInt stamp, StarlarkThread thread) throws EvalException, InterruptedException { @@ -277,6 +279,8 @@ public StructImpl linkMultiArchBinary( avoidDepsList, ImmutableList.copyOf(Sequence.cast(extraLinkopts, String.class, "extra_linkopts")), Sequence.cast(extraLinkInputs, Artifact.class, "extra_link_inputs"), + Sequence.cast(extraRequestedFeatures, String.class, "extra_requested_features"), + Sequence.cast(extraDisabledFeatures, String.class, "extra_disabled_features"), isStampingEnabled); return createStarlarkLinkingOutputs(linkingOutputs, thread); } catch (RuleErrorException | ActionConflictException exception) { diff --git a/src/main/java/com/google/devtools/build/lib/rules/objc/CompilationSupport.java b/src/main/java/com/google/devtools/build/lib/rules/objc/CompilationSupport.java index eaad0dc047d7df..cee9fcae47c294 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/objc/CompilationSupport.java +++ b/src/main/java/com/google/devtools/build/lib/rules/objc/CompilationSupport.java @@ -537,6 +537,8 @@ public CompilationSupport registerLinkActions( J2ObjcEntryClassProvider j2ObjcEntryClassProvider, ExtraLinkArgs extraLinkArgs, Iterable extraLinkInputs, + Iterable extraRequestedFeatures, + Iterable extraDisabledFeatures, boolean isStampingEnabled) throws InterruptedException, RuleErrorException { ObjcProvider objcProviderWithLinkingInfo = null; @@ -585,12 +587,22 @@ public CompilationSupport registerLinkActions( // CppLinkAction too, so create it now. Artifact inputFileList = intermediateArtifacts.linkerObjList(); + ImmutableSet allRequestedFeatures = + new ImmutableSet.Builder() + .addAll(ruleContext.getFeatures()) + .addAll(extraRequestedFeatures) + .build(); + ImmutableSet allDisabledFeatures = + new ImmutableSet.Builder() + .addAll(ruleContext.getDisabledFeatures()) + .addAll(extraDisabledFeatures) + .build(); FeatureConfiguration featureConfiguration = CcCommon.configureFeaturesOrReportRuleError( ruleContext, buildConfiguration, - ruleContext.getFeatures(), - ruleContext.getDisabledFeatures(), + allRequestedFeatures, + allDisabledFeatures, Language.OBJC, toolchain, cppSemantics); diff --git a/src/main/java/com/google/devtools/build/lib/rules/objc/MultiArchBinarySupport.java b/src/main/java/com/google/devtools/build/lib/rules/objc/MultiArchBinarySupport.java index 4d0c22bef2adcd..2dd7c5a2333414 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/objc/MultiArchBinarySupport.java +++ b/src/main/java/com/google/devtools/build/lib/rules/objc/MultiArchBinarySupport.java @@ -136,6 +136,8 @@ public Artifact registerConfigurationSpecificLinkActions( DependencySpecificConfiguration dependencySpecificConfiguration, ExtraLinkArgs extraLinkArgs, Iterable extraLinkInputs, + Iterable extraRequestedFeatures, + Iterable extraDisabledFeatures, boolean isStampingEnabled, Iterable infoCollections, Map> outputMapCollector) @@ -165,6 +167,8 @@ public Artifact registerConfigurationSpecificLinkActions( j2ObjcEntryClassProvider, extraLinkArgs, extraLinkInputs, + extraRequestedFeatures, + extraDisabledFeatures, isStampingEnabled) .validateAttributes(); ruleContext.assertNoErrors(); diff --git a/src/main/java/com/google/devtools/build/lib/starlarkbuildapi/objc/AppleCommonApi.java b/src/main/java/com/google/devtools/build/lib/starlarkbuildapi/objc/AppleCommonApi.java index 7932e1fbf5c5e6..206067ed5e7b27 100644 --- a/src/main/java/com/google/devtools/build/lib/starlarkbuildapi/objc/AppleCommonApi.java +++ b/src/main/java/com/google/devtools/build/lib/starlarkbuildapi/objc/AppleCommonApi.java @@ -390,6 +390,20 @@ AppleExecutableBinaryApi newExecutableBinaryProvider( positional = false, defaultValue = "[]", doc = "Extra files to pass to the linker action."), + @Param( + name = "extra_requested_features", + allowedTypes = {@ParamType(type = Sequence.class, generic1 = String.class)}, + named = true, + positional = false, + defaultValue = "[]", + doc = "Extra requested features to be passed to the linker action."), + @Param( + name = "extra_disabled_features", + allowedTypes = {@ParamType(type = Sequence.class, generic1 = String.class)}, + named = true, + positional = false, + defaultValue = "[]", + doc = "Extra disabled features to be passed to the linker action."), @Param( name = "stamp", named = true, @@ -409,6 +423,8 @@ StructApi linkMultiArchBinary( Object avoidDeps, // Sequence expected. Sequence extraLinkopts, // expected. Sequence extraLinkInputs, // expected. + Sequence extraRequestedFeatures, // expected. + Sequence extraDisabledFeatures, // expected. StarlarkInt stamp, StarlarkThread thread) throws EvalException, InterruptedException; diff --git a/src/test/java/com/google/devtools/build/lib/packages/util/mock/osx_cc_toolchain_config.bzl b/src/test/java/com/google/devtools/build/lib/packages/util/mock/osx_cc_toolchain_config.bzl index e6790e5e74ccce..eb50cceded061f 100644 --- a/src/test/java/com/google/devtools/build/lib/packages/util/mock/osx_cc_toolchain_config.bzl +++ b/src/test/java/com/google/devtools/build/lib/packages/util/mock/osx_cc_toolchain_config.bzl @@ -81,6 +81,39 @@ _parse_headers_feature = feature( name = "parse_headers", ) +_special_linking_feature = feature( + name = "special_linking_feature", +) + +_special_linking_flags_feature = feature( + "special_linking_flags_feature", + enabled = True, + flag_sets = [ + flag_set( + actions = _ALL_LINK_ACTIONS, + flag_groups = [flag_group(flags = ["--special_linking_flag"])], + with_features = [with_feature_set(features = ["special_linking_feature"])], + ), + ], +) + +_default_enabled_linking_feature = feature( + name = "default_enabled_linking_feature", + enabled = True, +) + +_default_enabled_linking_flags_feature = feature( + "default_enabled_linking_flags_feature", + enabled = True, + flag_sets = [ + flag_set( + actions = _ALL_LINK_ACTIONS, + flag_groups = [flag_group(flags = ["--default_enabled_linking_flag"])], + with_features = [with_feature_set(features = ["default_enabled_linking_feature"])], + ), + ], +) + _feature_name_to_feature = { "archive_param_file": _archive_param_file_feature, "default_feature": _default_feature, @@ -89,6 +122,10 @@ _feature_name_to_feature = { "supports_interface_shared_libraries": _supports_interface_shared_libraries_feature, "supports_dynamic_linker": _supports_dynamic_linker_feature, "parse_headers": _parse_headers_feature, + "special_linking_feature": _special_linking_feature, + "special_linking_flags_feature": _special_linking_flags_feature, + "default_enabled_linking_feature": _default_enabled_linking_feature, + "default_enabled_linking_flags_feature": _default_enabled_linking_flags_feature, } _action_name_to_action = {} diff --git a/src/test/java/com/google/devtools/build/lib/rules/objc/AppleBinaryStarlarkApiTest.java b/src/test/java/com/google/devtools/build/lib/rules/objc/AppleBinaryStarlarkApiTest.java index 8b14d33d2cf152..8cf924ef7fc0e6 100644 --- a/src/test/java/com/google/devtools/build/lib/rules/objc/AppleBinaryStarlarkApiTest.java +++ b/src/test/java/com/google/devtools/build/lib/rules/objc/AppleBinaryStarlarkApiTest.java @@ -1330,4 +1330,62 @@ public void testRuntimeLibPostMigration() throws Exception { useConfiguration(linkingInfoMigrationFlag(/* linkingInfoMigration= */ true)); checkRuntimeLib(); } + + @Test + public void testLinkMultiArchBinaryCanEnableFeature() throws Exception { + MockObjcSupport.setupCcToolchainConfig( + mockToolsConfig, + MockObjcSupport.darwinX86_64() + .withFeatures("special_linking_feature", "special_linking_flags_feature")); + scratch.file( + "package/BUILD", + "load('//test_starlark:apple_binary_starlark.bzl', 'apple_binary_starlark')", + "objc_library(", + " name = 'lib',", + " srcs = ['a.m'],", + ")", + "apple_binary_starlark(name = 'test_with_feature',", + " deps = [ ':lib' ],", + " extra_requested_features = [ 'special_linking_feature' ],", + " platform_type = 'macos')", + "apple_binary_starlark(name = 'test_without_feature',", + " deps = [ ':lib' ],", + " platform_type = 'macos')"); + + CommandAction actionWithFeature = linkAction("//package:test_with_feature"); + CommandAction actionWithoutFeature = linkAction("//package:test_without_feature"); + + assertThat(actionWithFeature.getArguments()).contains("--special_linking_flag"); + assertThat(actionWithoutFeature.getArguments()).doesNotContain("--special_linking_flag"); + } + + @Test + public void testLinkMultiArchBinaryCanDisableFeature() throws Exception { + MockObjcSupport.setupCcToolchainConfig( + mockToolsConfig, + MockObjcSupport.darwinX86_64() + .withFeatures( + "default_enabled_linking_feature", "default_enabled_linking_flags_feature")); + scratch.file( + "package/BUILD", + "load('//test_starlark:apple_binary_starlark.bzl', 'apple_binary_starlark')", + "objc_library(", + " name = 'lib',", + " srcs = ['a.m'],", + ")", + "apple_binary_starlark(name = 'test_disabled_feature',", + " deps = [ ':lib' ],", + " extra_disabled_features = [ 'default_enabled_linking_feature' ],", + " platform_type = 'macos')", + "apple_binary_starlark(name = 'test_enabled_feature',", + " deps = [ ':lib' ],", + " platform_type = 'macos')"); + + CommandAction actionDisabledFeature = linkAction("//package:test_disabled_feature"); + CommandAction actionEnabledFeature = linkAction("//package:test_enabled_feature"); + + assertThat(actionDisabledFeature.getArguments()) + .doesNotContain("--default_enabled_linking_flag"); + assertThat(actionEnabledFeature.getArguments()).contains("--default_enabled_linking_flag"); + } } diff --git a/src/test/java/com/google/devtools/build/lib/rules/objc/ObjcRuleTestCase.java b/src/test/java/com/google/devtools/build/lib/rules/objc/ObjcRuleTestCase.java index e3209fb6107620..d1033605fe19de 100644 --- a/src/test/java/com/google/devtools/build/lib/rules/objc/ObjcRuleTestCase.java +++ b/src/test/java/com/google/devtools/build/lib/rules/objc/ObjcRuleTestCase.java @@ -581,6 +581,8 @@ protected static void addAppleBinaryStarlarkRule(Scratch scratch) throws Excepti " avoid_deps = all_avoid_deps,", " extra_linkopts = linkopts,", " extra_link_inputs = link_inputs,", + " extra_requested_features = ctx.attr.extra_requested_features,", + " extra_disabled_features = ctx.attr.extra_disabled_features,", " stamp = ctx.attr.stamp,", " )", " processed_binary = ctx.actions.declare_file('{}_lipobin'.format(ctx.label.name))", @@ -660,6 +662,8 @@ protected static void addAppleBinaryStarlarkRule(Scratch scratch) throws Excepti " cfg=apple_common.multi_arch_split,", " ),", " 'linkopts': attr.string_list(),", + " 'extra_requested_features': attr.string_list(),", + " 'extra_disabled_features': attr.string_list(),", " 'platform_type': attr.string(),", " 'minimum_os_version': attr.string(),", " 'stamp': attr.int(values=[-1,0,1],default=-1),",