Skip to content

Commit

Permalink
Expose internal methods to CcCompilationOutputs
Browse files Browse the repository at this point in the history
These are undocumented methods and cannot be used externally. They are
guarded by an internal allow list.

PiperOrigin-RevId: 345279870
  • Loading branch information
c-mita authored and copybara-github committed Dec 2, 2020
1 parent be0f366 commit bf574de
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.collect.nestedset.Depset;
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
import com.google.devtools.build.lib.starlarkbuildapi.cpp.CcCompilationOutputsApi;
Expand All @@ -26,6 +27,7 @@
import net.starlark.java.eval.EvalException;
import net.starlark.java.eval.Sequence;
import net.starlark.java.eval.StarlarkList;
import net.starlark.java.eval.StarlarkThread;

/** A structured representation of the compilation outputs of a C++ rule. */
public class CcCompilationOutputs implements CcCompilationOutputsApi<Artifact> {
Expand Down Expand Up @@ -110,6 +112,25 @@ public Sequence<Artifact> getStarlarkPicObjects() throws EvalException {
return StarlarkList.immutableCopyOf(getObjectFiles(/* usePic= */ true));
}

@Override
public Depset getStarlarkTemps(StarlarkThread thread) throws EvalException {
CcModule.checkPrivateStarlarkificationAllowlist(thread);
return Depset.of(Artifact.TYPE, getTemps());
}

@Override
public Depset getStarlarkFilesToCompile(
boolean parseHeaders, boolean usePic, StarlarkThread thread) throws EvalException {
CcModule.checkPrivateStarlarkificationAllowlist(thread);
return Depset.of(Artifact.TYPE, getFilesToCompile(parseHeaders, usePic));
}

@Override
public Sequence<Artifact> getStarlarkHeaderTokens(StarlarkThread thread) throws EvalException {
CcModule.checkPrivateStarlarkificationAllowlist(thread);
return StarlarkList.immutableCopyOf(getHeaderTokenFiles());
}

/** Returns information about bitcode object files resulting from compilation. */
public LtoCompilationContext getLtoCompilationContext() {
return ltoCompilationContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@
package com.google.devtools.build.lib.starlarkbuildapi.cpp;

import com.google.devtools.build.docgen.annot.DocCategory;
import com.google.devtools.build.lib.collect.nestedset.Depset;
import com.google.devtools.build.lib.starlarkbuildapi.FileApi;
import net.starlark.java.annot.Param;
import net.starlark.java.annot.ParamType;
import net.starlark.java.annot.StarlarkBuiltin;
import net.starlark.java.annot.StarlarkMethod;
import net.starlark.java.eval.EvalException;
import net.starlark.java.eval.Sequence;
import net.starlark.java.eval.StarlarkThread;
import net.starlark.java.eval.StarlarkValue;

/** Interface for a structured representation of the compilation outputs of a C++ rule. */
Expand All @@ -42,4 +46,31 @@ public interface CcCompilationOutputsApi<FileT extends FileApi> extends Starlark
documented = true,
structField = true)
Sequence<FileT> getStarlarkPicObjects() throws EvalException;

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

@StarlarkMethod(
name = "files_to_compile",
documented = false,
parameters = {
@Param(
name = "parse_headers",
positional = false,
named = true,
defaultValue = "False",
allowedTypes = {@ParamType(type = Boolean.class)}),
@Param(
name = "use_pic",
positional = false,
named = true,
defaultValue = "False",
allowedTypes = {@ParamType(type = Boolean.class)}),
},
useStarlarkThread = true)
Depset getStarlarkFilesToCompile(boolean parseHeaders, boolean usePic, StarlarkThread thread)
throws EvalException;

@StarlarkMethod(name = "header_tokens", documented = false, useStarlarkThread = true)
Sequence<FileT> getStarlarkHeaderTokens(StarlarkThread thread) throws EvalException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6931,4 +6931,28 @@ public void testExpandedLinkApiRaisesError() throws Exception {
assertThat(e).hasMessageThat().contains("Rule in 'b' cannot use private API");
}
}

@Test
public void testExpandedCcCompilationOutputsApiRaisesError() throws Exception {
scratch.file("b/BUILD", "load('//b:rule.bzl', 'cc_rule')", "cc_rule(", " name='foo',", ")");
ImmutableList<String> calls =
ImmutableList.of(
"comp_outputs.temps()",
"comp_outputs.files_to_compile(parse_headers=False, use_pic=True)",
"comp_outputs.header_tokens()");
for (String call : calls) {
scratch.overwriteFile(
"b/rule.bzl",
"def _impl(ctx):",
" comp_outputs = cc_common.create_compilation_outputs()",
" " + call,
" return [DefaultInfo()]",
"cc_rule = rule(",
" implementation = _impl,",
")");
invalidatePackages();
AssertionError e = assertThrows(AssertionError.class, () -> getConfiguredTarget("//b:foo"));
assertThat(e).hasMessageThat().contains("Rule in 'b' cannot use private API");
}
}
}

0 comments on commit bf574de

Please sign in to comment.