Skip to content

Commit

Permalink
Expose CcNativeLibraryProvider to Starlark internally via cc_common.
Browse files Browse the repository at this point in the history
This is not injected into the toplevel namespace because this is not
meant to be used externally. Its usage is controlled via the same allow
list as the other internal APIs.

PiperOrigin-RevId: 352781106
  • Loading branch information
c-mita authored and copybara-github committed Jan 20, 2021
1 parent 903c272 commit dba2fce
Show file tree
Hide file tree
Showing 11 changed files with 140 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1179,8 +1179,7 @@ private static void addTransitiveInfoProviders(
.setFilesToBuild(filesToBuild)
.addNativeDeclaredProvider(
CcInfo.builder().setCcCompilationContext(ccCompilationContext).build())
.addProvider(
CcNativeLibraryProvider.class,
.addNativeDeclaredProvider(
new CcNativeLibraryProvider(collectTransitiveCcNativeLibraries(ruleContext, libraries)))
.addNativeDeclaredProvider(instrumentedFilesProvider)
.addOutputGroup(OutputGroupInfo.VALIDATION, headerTokens)
Expand All @@ -1194,7 +1193,7 @@ private static NestedSet<LibraryToLink> collectTransitiveCcNativeLibraries(
NestedSetBuilder<LibraryToLink> builder = NestedSetBuilder.linkOrder();
builder.addAll(libraries);
for (CcNativeLibraryProvider dep :
ruleContext.getPrerequisites("deps", CcNativeLibraryProvider.class)) {
ruleContext.getPrerequisites("deps", CcNativeLibraryProvider.PROVIDER)) {
builder.addTransitive(dep.getTransitiveCcNativeLibraries());
}
return builder.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ public static void init(
CcStarlarkApiProvider.maybeAdd(ruleContext, targetBuilder);
targetBuilder
.setFilesToBuild(filesToBuild)
.addProvider(ccNativeLibraryProvider)
.addNativeDeclaredProvider(ccNativeLibraryProvider)
.addNativeDeclaredProvider(
CcInfo.builder()
.setCcCompilationContext(compilationInfo.getCcCompilationContext())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1824,6 +1824,12 @@ public CcDebugInfoContext mergeCcDebugInfoFromStarlark(
Sequence.cast(debugInfos, CcDebugInfoContext.class, "debug_infos"));
}

@Override
public Object getCcNativeLibraryProvider(StarlarkThread thread) throws EvalException {
checkPrivateStarlarkificationAllowlist(thread);
return CcNativeLibraryProvider.PROVIDER;
}

public static void checkPrivateStarlarkificationAllowlist(StarlarkThread thread)
throws EvalException {
String rulePackage =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,38 @@

package com.google.devtools.build.lib.rules.cpp;

import com.google.devtools.build.lib.analysis.TransitiveInfoProvider;
import com.google.devtools.build.lib.collect.nestedset.Depset;
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
import com.google.devtools.build.lib.packages.BuiltinProvider;
import com.google.devtools.build.lib.packages.NativeInfo;
import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec;
import com.google.devtools.build.lib.starlarkbuildapi.cpp.CcNativeLibraryProviderApi;

/**
* A target that provides native libraries in the transitive closure of its deps that are needed for
* executing C++ code.
*/
@Immutable
@AutoCodec
public final class CcNativeLibraryProvider implements TransitiveInfoProvider {
public final class CcNativeLibraryProvider extends NativeInfo
implements CcNativeLibraryProviderApi {

public static final BuiltinProvider<CcNativeLibraryProvider> PROVIDER =
new BuiltinProvider<CcNativeLibraryProvider>(
"CcNativeLibraryProvider", CcNativeLibraryProvider.class) {};

private final NestedSet<LibraryToLink> transitiveCcNativeLibraries;

public CcNativeLibraryProvider(NestedSet<LibraryToLink> transitiveCcNativeLibraries) {
this.transitiveCcNativeLibraries = transitiveCcNativeLibraries;
}

@Override
public BuiltinProvider<CcNativeLibraryProvider> getProvider() {
return PROVIDER;
}

/**
* Collects native libraries in the transitive closure of its deps that are needed for executing
* C/C++ code.
Expand All @@ -41,4 +55,9 @@ public CcNativeLibraryProvider(NestedSet<LibraryToLink> transitiveCcNativeLibrar
public NestedSet<LibraryToLink> getTransitiveCcNativeLibraries() {
return transitiveCcNativeLibraries;
}

@Override
public Depset getTransitiveCcNativeLibrariesStarlark() {
return Depset.of(LibraryToLink.TYPE, getTransitiveCcNativeLibraries());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -999,7 +999,7 @@ public static CcNativeLibraryProvider collectNativeCcLibraries(
NestedSetBuilder<LibraryToLink> result = NestedSetBuilder.linkOrder();
result.addAll(libraries);
for (CcNativeLibraryProvider dep :
AnalysisUtils.getProviders(deps, CcNativeLibraryProvider.class)) {
AnalysisUtils.getProviders(deps, CcNativeLibraryProvider.PROVIDER)) {
result.addTransitive(dep.getTransitiveCcNativeLibraries());
}
return new CcNativeLibraryProvider(result.build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public NativeLibraryNestedSetBuilder addJavaTarget(TransitiveInfoCollection dep)
return this;
}

CcNativeLibraryProvider ccProvider = dep.getProvider(CcNativeLibraryProvider.class);
CcNativeLibraryProvider ccProvider = dep.get(CcNativeLibraryProvider.PROVIDER);
if (ccProvider != null) {
builder.addTransitive(ccProvider.getTransitiveCcNativeLibraries());
return this;
Expand All @@ -80,7 +80,7 @@ public NativeLibraryNestedSetBuilder addCcTargets(

/** Include native Java libraries of a specified target into the nested set. */
private void addCcTarget(TransitiveInfoCollection dep) {
CcNativeLibraryProvider provider = dep.getProvider(CcNativeLibraryProvider.class);
CcNativeLibraryProvider provider = dep.get(CcNativeLibraryProvider.PROVIDER);
if (provider != null) {
builder.addTransitive(provider.getTransitiveCcNativeLibraries());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1255,4 +1255,10 @@ LtoBackendArtifactsT createLtoBackendArtifacts(
Sequence<?> argv,
StarlarkThread thread)
throws EvalException;

@StarlarkMethod(
name = "get_CcNativeLibraryProvider",
documented = false,
useStarlarkThread = true)
Object getCcNativeLibraryProvider(StarlarkThread thread) throws EvalException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2021 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

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.core.StructApi;
import net.starlark.java.annot.StarlarkBuiltin;
import net.starlark.java.annot.StarlarkMethod;
import net.starlark.java.eval.EvalException;

/**
* Provider for native libraries in the transitive closure of a target that are needed for executing
* C++ code.
*/
@StarlarkBuiltin(name = "CcNativeLibrary", documented = false, category = DocCategory.PROVIDER)
public interface CcNativeLibraryProviderApi extends StructApi {

@StarlarkMethod(name = "libs", structField = true, documented = false)
Depset getTransitiveCcNativeLibrariesStarlark() throws EvalException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.google.devtools.build.lib.starlarkbuildapi.cpp.CcLinkingContextApi;
import com.google.devtools.build.lib.starlarkbuildapi.cpp.CcLinkingOutputsApi;
import com.google.devtools.build.lib.starlarkbuildapi.cpp.CcModuleApi;
import com.google.devtools.build.lib.starlarkbuildapi.cpp.CcNativeLibraryProviderApi;
import com.google.devtools.build.lib.starlarkbuildapi.cpp.CcToolchainConfigInfoApi;
import com.google.devtools.build.lib.starlarkbuildapi.cpp.CcToolchainProviderApi;
import com.google.devtools.build.lib.starlarkbuildapi.cpp.CcToolchainVariablesApi;
Expand Down Expand Up @@ -406,4 +407,10 @@ public LtoBackendArtifactsApi<FileApi> createLtoBackendArtifacts(
throws EvalException {
return null;
}

@Override
public CcNativeLibraryProviderApi getCcNativeLibraryProvider(StarlarkThread thread)
throws EvalException {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ public void testFilesToBuild() throws Exception {
.containsExactly(archive, implSharedObject, implInterfaceSharedObject);
assertThat(
LibraryToLink.getDynamicLibrariesForLinking(
hello.getProvider(CcNativeLibraryProvider.class).getTransitiveCcNativeLibraries()))
hello.get(CcNativeLibraryProvider.PROVIDER).getTransitiveCcNativeLibraries()))
.containsExactly(implInterfaceSharedObjectLink);
assertThat(
hello
Expand Down Expand Up @@ -265,7 +265,7 @@ public void testFilesToBuildWithInterfaceSharedObjects() throws Exception {
.containsExactly(archive, sharedObject, implSharedObject);
assertThat(
LibraryToLink.getDynamicLibrariesForLinking(
hello.getProvider(CcNativeLibraryProvider.class).getTransitiveCcNativeLibraries()))
hello.get(CcNativeLibraryProvider.PROVIDER).getTransitiveCcNativeLibraries()))
.containsExactly(sharedObjectLink);
assertThat(
hello
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.google.devtools.build.lib.actions.CommandLineExpansionException;
import com.google.devtools.build.lib.actions.util.ActionsTestUtil;
import com.google.devtools.build.lib.analysis.ConfiguredTarget;
import com.google.devtools.build.lib.analysis.DefaultInfo;
import com.google.devtools.build.lib.analysis.RuleContext;
import com.google.devtools.build.lib.analysis.util.AnalysisMock;
import com.google.devtools.build.lib.analysis.util.AnalysisTestUtil;
Expand Down Expand Up @@ -7211,6 +7212,64 @@ public void testExtraLinkTimeLibraryApiRaisesError() throws Exception {
}
}

@Test
public void testDisallowedCcNativeLibraryRaisesError() throws Exception {
scratch.file(
"b/BUILD", "load('//b:rule.bzl', 'test_rule')", "test_rule(", " name = 'test',", ")");
scratch.file(
"b/rule.bzl",
"def _impl(ctx):",
" cc_common.get_CcNativeLibraryProvider()",
" return DefaultInfo()",
"test_rule = rule(implementation = _impl)");

AssertionError e = assertThrows(AssertionError.class, () -> getConfiguredTarget("//b:test"));
assertThat(e).hasMessageThat().contains("Rule in 'b' cannot use private API");
}

@Test
public void testAllowedCcNativeLibraryProviderIsUsable() throws Exception {
scratch.file(
"b/BUILD",
"load('//bazel_internal/test_rules/cc:rule.bzl', 'test_rule')",
"test_rule(",
" name = 'test',",
" cc_dep = ':foo',",
")",
"cc_library(",
" name = 'foo',",
" srcs = ['foo.cc'],",
")");
scratch.file("bazel_internal/test_rules/cc/BUILD");
scratch.file(
"bazel_internal/test_rules/cc/rule.bzl",
"def _impl(ctx):",
" CcNativeLibraryProvider = cc_common.get_CcNativeLibraryProvider()",
" libs = ctx.attr.cc_dep[CcNativeLibraryProvider].libs",
" files = []",
" for l in libs.to_list():",
" files.append(l.dynamic_library)",
" files.append(l.interface_library)",
" files.append(l.static_library)",
" files.append(l.pic_static_library)",
" files = [f for f in files if f != None]",
" runfiles = ctx.runfiles(files=files)",
" return [DefaultInfo(runfiles=runfiles)]",
"test_rule = rule(",
" implementation = _impl,",
" attrs = {",
" 'cc_dep': attr.label(),",
" },",
")");

ConfiguredTarget test = getConfiguredTarget("//b:test");

assertThat(
test.get(DefaultInfo.PROVIDER).getDefaultRunfiles().getAllArtifacts().toList().stream()
.map(Artifact::getFilename))
.containsExactly("libfoo.a");
}

@Test
public void testAllowedVariableExtensionCompileApi() throws Exception {
runTestVariableExtension(/* call= */ "compile", /* allowed= */ true);
Expand Down

0 comments on commit dba2fce

Please sign in to comment.