Skip to content

Commit

Permalink
Expose removeDeadCode from J2ObjcConfiguration
Browse files Browse the repository at this point in the history
Needed for Starlarkification of j2objc_library

PiperOrigin-RevId: 529415395
Change-Id: Ia428ed84707a9cbec694fca64710ab8790465e43
  • Loading branch information
kotlaja authored and Copybara-Service committed May 4, 2023
1 parent e8bef8c commit d228ca0
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,18 @@
import com.google.devtools.build.lib.analysis.config.Fragment;
import com.google.devtools.build.lib.analysis.config.RequiresOptions;
import com.google.devtools.build.lib.analysis.starlark.annotations.StarlarkConfigurationField;
import com.google.devtools.build.lib.cmdline.BazelModuleContext;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
import com.google.devtools.build.lib.events.Event;
import com.google.devtools.build.lib.events.EventHandler;
import com.google.devtools.build.lib.starlarkbuildapi.apple.J2ObjcConfigurationApi;
import java.util.Collections;
import javax.annotation.Nullable;
import net.starlark.java.eval.EvalException;
import net.starlark.java.eval.Module;
import net.starlark.java.eval.Starlark;
import net.starlark.java.eval.StarlarkThread;

/**
* A J2ObjC transpiler configuration fragment containing J2ObjC translation flags. This
Expand Down Expand Up @@ -125,6 +130,21 @@ public boolean removeDeadCode() {
return removeDeadCode;
}

protected static void checkPrivateAccess(StarlarkThread thread) throws EvalException {
Label label =
((BazelModuleContext) Module.ofInnermostEnclosingStarlarkFunction(thread).getClientData())
.label();
if (!label.getPackageIdentifier().getRepository().getName().equals("_builtins")) {
throw Starlark.errorf("Rule in '%s' cannot use private API", label.getPackageName());
}
}

@Override
public boolean getRemoveDeadCodeForStarlark(StarlarkThread thread) throws EvalException {
checkPrivateAccess(thread);
return removeDeadCode;
}

/**
* Returns whether to generate J2ObjC header map in a separate action in parallel of the J2ObjC
* transpilation action.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import com.google.devtools.build.docgen.annot.DocCategory;
import net.starlark.java.annot.StarlarkBuiltin;
import net.starlark.java.annot.StarlarkMethod;
import net.starlark.java.eval.EvalException;
import net.starlark.java.eval.StarlarkThread;
import net.starlark.java.eval.StarlarkValue;

/** A configuration fragment for j2objc. */
Expand All @@ -32,4 +34,7 @@ public interface J2ObjcConfigurationApi extends StarlarkValue {
structField = true,
doc = "The list of flags to be used when the j2objc compiler is invoked. ")
ImmutableList<String> getTranslationFlags();

@StarlarkMethod(name = "remove_dead_code", documented = false, useStarlarkThread = true)
boolean getRemoveDeadCodeForStarlark(StarlarkThread thread) throws EvalException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3464,6 +3464,26 @@ public void testRunIjarIsPrivateApi() throws Exception {
assertContainsEvent("Rule in 'foo' cannot use private API");
}

@Test
public void testGetRemoveDeadCodeFromJ2ObjcConfigurationForStarlarkIsPrivateAPI()
throws Exception {
scratch.file(
"foo/rule.bzl",
"def _impl(ctx):",
" ctx.fragments.j2objc.remove_dead_code()",
" return []",
"myrule = rule(",
" implementation=_impl,",
" fragments = ['j2objc']",
")");
scratch.file("foo/BUILD", "load(':rule.bzl', 'myrule')", "myrule(name='myrule')");
reporter.removeHandler(failFastHandler);

getConfiguredTarget("//foo:myrule");

assertContainsEvent("Rule in 'foo' cannot use private API");
}

@Test
public void testGetBuildInfoArtifacts() throws Exception {
scratch.file(
Expand Down

0 comments on commit d228ca0

Please sign in to comment.