Skip to content

Commit

Permalink
Add the incompatible flag to disallow the uses of sdk_frameworks and …
Browse files Browse the repository at this point in the history
…weak_sdk_frameworks attributes.

PiperOrigin-RevId: 508131635
Change-Id: I80910b01c52b0368394f47089d26c52ba67156ce
  • Loading branch information
Googler authored and copybara-github committed Feb 8, 2023
1 parent a878efa commit 66121a7
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,12 @@ static void addSdkAttributesFromRuleContext(Builder builder, RuleContext ruleCon
for (String explicit : ruleContext.attributes().get("sdk_frameworks", Type.STRING_LIST)) {
frameworks.add(explicit);
}
if (ruleContext.getFragment(ObjcConfiguration.class).disallowSdkFrameworksAttributes()
&& !frameworks.isEmpty()) {
ruleContext.attributeError(
"sdk_frameworks",
"sdk_frameworks attribute is disallowed. Use explicit dependencies instead.");

This comment has been minimized.

Copy link
@keith

keith Feb 13, 2023

Member

@googlewalt I guess this error is more for internal google users than external users? as explicit modules aren't supported in any public bazel stuff today

}
builder.addSdkFrameworks(frameworks.build());
}

Expand All @@ -237,6 +243,12 @@ static void addSdkAttributesFromRuleContext(Builder builder, RuleContext ruleCon
ruleContext.attributes().get("weak_sdk_frameworks", Type.STRING_LIST)) {
weakFrameworks.add(frameworkName);
}
if (ruleContext.getFragment(ObjcConfiguration.class).disallowSdkFrameworksAttributes()
&& !weakFrameworks.isEmpty()) {
ruleContext.attributeError(
"weak_sdk_frameworks",
"weak_sdk_frameworks attribute is disallowed. Use explicit dependencies instead.");
}
builder.addWeakSdkFrameworks(weakFrameworks.build());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,17 @@ public class ObjcCommandLineOptions extends FragmentOptions {
+ "details and migration information")
public boolean incompatibleObjcLinkingInfoMigration;

@Option(
name = "incompatible_disallow_sdk_frameworks_attributes",
defaultValue = "false",
documentationCategory = OptionDocumentationCategory.STARLARK_SEMANTICS,
effectTags = {OptionEffectTag.BUILD_FILE_SEMANTICS},
metadataTags = {OptionMetadataTag.INCOMPATIBLE_CHANGE},
help =
"If true, disallow sdk_frameworks and weak_sdk_frameworks attributes in objc_library and"
+ "objc_import.")
public boolean incompatibleDisallowSdkFrameworksAttributes;

/** @deprecated delete when we are sure it's not used anywhere. */
@Deprecated
@Option(
Expand All @@ -201,6 +212,7 @@ public FragmentOptions getExec() {
exec.incompatibleAvoidHardcodedObjcCompilationFlags =
incompatibleAvoidHardcodedObjcCompilationFlags;
exec.incompatibleObjcLinkingInfoMigration = incompatibleObjcLinkingInfoMigration;
exec.incompatibleDisallowSdkFrameworksAttributes = incompatibleDisallowSdkFrameworksAttributes;
return exec;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public class ObjcConfiguration extends Fragment implements ObjcConfigurationApi<
private final boolean deviceDebugEntitlements;
private final boolean avoidHardcodedCompilationFlags;
private final boolean linkingInfoMigration;
private final boolean disallowSdkFrameworksAttributes;

public ObjcConfiguration(BuildOptions buildOptions) {
CoreOptions options = buildOptions.get(CoreOptions.class);
Expand All @@ -91,6 +92,7 @@ public ObjcConfiguration(BuildOptions buildOptions) {
this.avoidHardcodedCompilationFlags =
objcOptions.incompatibleAvoidHardcodedObjcCompilationFlags;
this.linkingInfoMigration = objcOptions.incompatibleObjcLinkingInfoMigration;
this.disallowSdkFrameworksAttributes = objcOptions.incompatibleDisallowSdkFrameworksAttributes;
}

/**
Expand Down Expand Up @@ -228,4 +230,10 @@ public boolean useDeviceDebugEntitlements() {
public boolean linkingInfoMigration() {
return linkingInfoMigration;
}

/** Returns whether sdk_frameworks and weak_sdk_frameworks attributes are disallowed. */
@Override
public boolean disallowSdkFrameworksAttributes() {
return disallowSdkFrameworksAttributes;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,10 @@ public interface ObjcConfigurationApi<ApplePlatformTypeApiT extends ApplePlatfor
"Returns whether Objective C builtin rules should get their linking info from CcInfo "
+ "instead of ObjcProvider.")
boolean linkingInfoMigration();

@StarlarkMethod(
name = "disallow_sdk_frameworks_attributes",
structField = true,
doc = "Returns whether sdk_frameworks and weak_sdk_frameworks are disallowed attributes.")
boolean disallowSdkFrameworksAttributes();
}
Original file line number Diff line number Diff line change
Expand Up @@ -1569,4 +1569,48 @@ public void testDynamicFrameworkApi() throws Exception {
assertThat(objc.dynamicFrameworkNames().toList()).containsExactly("fx1", "fx2");
assertThat(objc.dynamicFrameworkPaths().toList()).containsExactly("fx");
}

@Test
public void testDisallowSDKFrameworkAttribute() throws Exception {
useConfiguration("--incompatible_disallow_sdk_frameworks_attributes");

scratch.file(
"examples/apple_starlark/BUILD",
"objc_library(",
" name = 'lib',",
" srcs = ['a.m'],",
" sdk_frameworks = ['Accelerate', 'GLKit'],",
")");
AssertionError e =
assertThrows(
AssertionError.class, () -> getConfiguredTarget("//examples/apple_starlark:lib"));
assertThat(e)
.hasMessageThat()
.contains(
"ERROR /workspace/examples/apple_starlark/BUILD:1:13: in sdk_frameworks attribute of"
+ " objc_library rule //examples/apple_starlark:lib: sdk_frameworks attribute is"
+ " disallowed. Use explicit dependencies instead.");
}

@Test
public void testDisallowWeakSDKFrameworksAttribute() throws Exception {
useConfiguration("--incompatible_disallow_sdk_frameworks_attributes");

scratch.file(
"examples/apple_starlark/BUILD",
"objc_library(",
" name = 'lib',",
" srcs = ['a.m'],",
" weak_sdk_frameworks = ['XCTest'],",
")");
AssertionError e =
assertThrows(
AssertionError.class, () -> getConfiguredTarget("//examples/apple_starlark:lib"));
assertThat(e)
.hasMessageThat()
.contains(
"ERROR /workspace/examples/apple_starlark/BUILD:1:13: in weak_sdk_frameworks attribute"
+ " of objc_library rule //examples/apple_starlark:lib: weak_sdk_frameworks"
+ " attribute is disallowed. Use explicit dependencies instead.");
}
}

0 comments on commit 66121a7

Please sign in to comment.