Skip to content

Commit

Permalink
Cherry-pick missing things.
Browse files Browse the repository at this point in the history
  • Loading branch information
comius committed Jul 14, 2022
1 parent da94473 commit 5cfba19
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
import com.google.devtools.build.lib.vfs.FileSystemUtils;
import com.google.devtools.build.lib.vfs.PathFragment;
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;

/** Utility functions for proto_library and proto aspect implementations. */
public class ProtoCommon {
Expand Down Expand Up @@ -518,4 +522,14 @@ public static boolean areDepsStrict(RuleContext ruleContext) {
StrictDepsMode getBool = ruleContext.getFragment(ProtoConfiguration.class).strictProtoDeps();
return getBool != StrictDepsMode.OFF && getBool != StrictDepsMode.DEFAULT;
}

public static void checkPrivateStarlarkificationAllowlist(StarlarkThread thread)
throws EvalException {
Label label =
((BazelModuleContext) Module.ofInnermostEnclosingStarlarkFunction(thread).getClientData())
.label();
if (!label.getPackageIdentifier().getRepository().toString().equals("@_builtins")) {
throw Starlark.errorf("Rule in '%s' cannot use private API", label.getPackageName());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import com.google.devtools.build.lib.starlarkbuildapi.ProtoInfoApi;
import com.google.devtools.build.lib.starlarkbuildapi.proto.ProtoBootstrap;
import com.google.devtools.build.lib.vfs.PathFragment;
import net.starlark.java.eval.EvalException;
import net.starlark.java.eval.StarlarkThread;

/**
* Configured target classes that implement this class can contribute .proto files to the
Expand Down Expand Up @@ -133,6 +135,12 @@ public String getDirectProtoSourceRoot() {
return Depset.of(Artifact.TYPE, getTransitiveProtoSources());
}

@Override
public Depset getTransitiveSourcesForStarlark(StarlarkThread thread) throws EvalException {
ProtoCommon.checkPrivateStarlarkificationAllowlist(thread);
return Depset.of(ProtoSource.TYPE, transitiveSources);
}

/**
* The {@code .proto} source files in this {@code proto_library}'s {@code srcs} and all of its
* transitive dependencies.
Expand Down Expand Up @@ -222,6 +230,12 @@ public NestedSet<ProtoSource> getStrictImportableSources() {
return strictImportableSources;
}

@Override
public Depset getStrictImportableSourcesForStarlark(StarlarkThread thread) throws EvalException {
ProtoCommon.checkPrivateStarlarkificationAllowlist(thread);
return Depset.of(ProtoSource.TYPE, strictImportableSources);
}

/**
* Returns a set of {@code .proto} sources that may be re-exported by this {@code proto_library}'s
* direct sources.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package com.google.devtools.build.lib.rules.proto;

import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.collect.nestedset.Depset;
import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec;
import com.google.devtools.build.lib.vfs.PathFragment;
Expand All @@ -24,6 +25,8 @@
@Immutable
@AutoCodec
class ProtoSource implements StarlarkValue {
public static final Depset.ElementType TYPE = Depset.ElementType.of(ProtoSource.class);

private final Artifact sourceFile;
private final Artifact originalSourceFile;
private final PathFragment sourceRoot;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import com.google.devtools.build.lib.starlarkbuildapi.core.StructApi;
import net.starlark.java.annot.StarlarkBuiltin;
import net.starlark.java.annot.StarlarkMethod;
import net.starlark.java.eval.EvalException;
import net.starlark.java.eval.StarlarkThread;

/** Info object propagating information about protocol buffer sources. */
@StarlarkBuiltin(
Expand Down Expand Up @@ -105,4 +107,10 @@ interface ProtoInfoProviderApi extends ProviderApi {
+ " as a source, that source file would be imported as 'import c/d.proto'",
structField = true)
String getDirectProtoSourceRoot();

@StarlarkMethod(name = "strict_importable_sources", documented = false, useStarlarkThread = true)
Depset getStrictImportableSourcesForStarlark(StarlarkThread thread) throws EvalException;

@StarlarkMethod(name = "transitive_proto_sources", documented = false, useStarlarkThread = true)
Depset getTransitiveSourcesForStarlark(StarlarkThread thread) throws EvalException;
}
2 changes: 1 addition & 1 deletion src/main/starlark/builtins_bzl/common/exports.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ exported_rules = {
"+apple_static_library": apple_static_library,
"+cc_shared_library": cc_shared_library,
"+cc_shared_library_permissions": cc_shared_library_permissions,
"+proto_lang_toolchain": proto_lang_toolchain,
"proto_lang_toolchain": proto_lang_toolchain,
}
exported_to_java = {
"register_compile_and_archive_actions_for_j2objc": compilation_support.register_compile_and_archive_actions_for_j2objc,
Expand Down

0 comments on commit 5cfba19

Please sign in to comment.