Skip to content

Commit

Permalink
Expose more fields in C++ Starkark API
Browse files Browse the repository at this point in the history
This CL exposes APIs related to ExtraLinkTimeLibraries. The necessary fields necessary for re-writing Go that are in the Go package are not exposed here, they should be exposed when the Go rules re-write starts.

Usage is still undocumented and guarded by an internal allow list.

PiperOrigin-RevId: 350572165
  • Loading branch information
oquenchil authored and copybara-github committed Jan 7, 2021
1 parent 887ed4a commit 8a2c937
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.google.devtools.build.lib.packages.SymbolGenerator;
import com.google.devtools.build.lib.packages.semantics.BuildLanguageOptions;
import com.google.devtools.build.lib.starlarkbuildapi.cpp.CcLinkingContextApi;
import com.google.devtools.build.lib.starlarkbuildapi.cpp.ExtraLinkTimeLibraryApi;
import com.google.devtools.build.lib.starlarkbuildapi.cpp.LinkerInputApi;
import com.google.devtools.build.lib.starlarkbuildapi.cpp.LinkstampApi;
import com.google.devtools.build.lib.util.Fingerprint;
Expand Down Expand Up @@ -473,6 +474,22 @@ public Depset getStarlarkNonCodeInputs() {
return Depset.of(Artifact.TYPE, getNonCodeInputs());
}

@Override
public ExtraLinkTimeLibraryApi getGoLinkCArchiveForStarlark(StarlarkThread thread)
throws EvalException {
CcModule.checkPrivateStarlarkificationAllowlist(thread);
ExtraLinkTimeLibrary goLinkCArchive = null;
if (extraLinkTimeLibraries != null) {
for (ExtraLinkTimeLibrary extraLibrary : extraLinkTimeLibraries.getExtraLibraries()) {
if (goLinkCArchive != null) {
throw new EvalException("multiple GoLinkCArchive entries in go_link_c_archive");
}
goLinkCArchive = extraLibrary;
}
}
return goLinkCArchive;
}

public NestedSet<LinkOptions> getUserLinkFlags() {
NestedSetBuilder<LinkOptions> userLinkFlags = NestedSetBuilder.linkOrder();
for (LinkerInput linkerInput : linkerInputs.toList()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,7 @@ public CcLinkingContext createCcLinkingInfo(
Object librariesToLinkObject,
Object userLinkFlagsObject,
Object nonCodeInputsObject,
Object goLinkCArchiveObject,
StarlarkThread thread)
throws EvalException {
if (Starlark.isNullOrNone(linkerInputs)) {
Expand Down Expand Up @@ -927,6 +928,15 @@ public CcLinkingContext createCcLinkingInfo(
CcLinkingContext.Builder ccLinkingContextBuilder = CcLinkingContext.builder();
ccLinkingContextBuilder.addTransitiveLinkerInputs(
Depset.noneableCast(linkerInputs, CcLinkingContext.LinkerInput.class, "linker_inputs"));
if (checkObjectsBound(goLinkCArchiveObject)) {
checkPrivateStarlarkificationAllowlist(thread);
}
ExtraLinkTimeLibrary goLinkCArchive =
convertFromNoneable(goLinkCArchiveObject, /* defaultValue= */ null);
if (goLinkCArchive != null) {
ccLinkingContextBuilder.setExtraLinkTimeLibraries(
ExtraLinkTimeLibraries.builder().add(goLinkCArchive).build());
}

@SuppressWarnings("unchecked")
Sequence<LibraryToLink> librariesToLink = nullIfNone(librariesToLinkObject, Sequence.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.google.devtools.build.lib.analysis.RuleContext;
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
import com.google.devtools.build.lib.packages.RuleClass.ConfiguredTargetFactory.RuleErrorException;
import com.google.devtools.build.lib.starlarkbuildapi.cpp.ExtraLinkTimeLibraryApi;

/**
* An extra library to include in a link. The actual library is built at link time.
Expand All @@ -29,7 +30,7 @@
* <p>Any implementations must be immutable (and therefore thread-safe), because this is passed
* between rules and accessed in a multi-threaded context.
*/
public interface ExtraLinkTimeLibrary {
public interface ExtraLinkTimeLibrary extends ExtraLinkTimeLibraryApi {

/** Output of {@link #buildLibraries}. Pair of libraries to link and runtime libraries. */
class BuildLibraryOutput {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.google.devtools.build.lib.collect.nestedset.Depset;
import com.google.devtools.build.lib.packages.semantics.BuildLanguageOptions;
import com.google.devtools.build.lib.starlarkbuildapi.FileApi;
import javax.annotation.Nullable;
import net.starlark.java.annot.StarlarkBuiltin;
import net.starlark.java.annot.StarlarkMethod;
import net.starlark.java.eval.EvalException;
Expand Down Expand Up @@ -66,4 +67,12 @@ public interface CcLinkingContextApi<FileT extends FileApi> extends StarlarkValu

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

@StarlarkMethod(
name = "go_link_c_archive",
documented = false,
allowReturnNones = true,
useStarlarkThread = true)
@Nullable
ExtraLinkTimeLibraryApi getGoLinkCArchiveForStarlark(StarlarkThread thread) throws EvalException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -753,12 +753,23 @@ void checkExperimentalStarlarkCcImport(StarlarkActionFactoryT starlarkActionFact
defaultValue = "None",
valueWhenDisabled = "None",
allowedTypes = {@ParamType(type = NoneType.class), @ParamType(type = Sequence.class)}),
@Param(
name = "go_link_c_archive",
documented = false,
positional = false,
named = true,
defaultValue = "unbound",
allowedTypes = {
@ParamType(type = ExtraLinkTimeLibraryApi.class),
@ParamType(type = NoneType.class)
})
})
LinkingContextT createCcLinkingInfo(
Object linkerInputs,
Object librariesToLinkObject,
Object userLinkFlagsObject,
Object nonCodeInputs, // <FileT> expected
Object goLinkCArchive,
StarlarkThread thread)
throws EvalException, InterruptedException;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2019 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 net.starlark.java.annot.StarlarkBuiltin;
import net.starlark.java.eval.StarlarkValue;

/** Used for linking Go into C++ as a single library. */
@StarlarkBuiltin(
name = "ExtraLinkTimeLibrary",
documented = false,
category = DocCategory.TOP_LEVEL_TYPE)
public interface ExtraLinkTimeLibraryApi extends StarlarkValue {}
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ public CcLinkingContextApi<FileApi> createCcLinkingInfo(
Object librariesToLinkObject,
Object userLinkFlagsObject,
Object nonCodeInputs,
Object goLinkCArchiveObject,
StarlarkThread thread) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7178,4 +7178,34 @@ public void testExpandedLinkstampApiRaisesError() throws Exception {
assertThat(e).hasMessageThat().contains("Rule in 'b' cannot use private API");
}
}

@Test
public void testExtraLinkTimeLibraryApiRaisesError() throws Exception {
scratch.file(
"b/BUILD",
"load('//b:rule.bzl', 'cc_rule')",
"cc_library(name='cc_dep', srcs=['cc_dep.cc'])",
"cc_rule(name='foo', cc_dep=':cc_dep')");
ImmutableList<String> calls =
ImmutableList.of(
"cc_common.create_linking_context(linker_inputs=depset([]), go_link_c_archive=None)",
"linking_context.go_link_c_archive()");
for (String call : calls) {
scratch.overwriteFile(
"b/rule.bzl",
"def _impl(ctx):",
" linking_context = ctx.attr.cc_dep[CcInfo].linking_context",
" " + call,
" return [DefaultInfo()]",
"cc_rule = rule(",
" implementation = _impl,",
" attrs = { ",
" 'cc_dep': attr.label(),",
" },",
")");
invalidatePackages();
AssertionError e = assertThrows(AssertionError.class, () -> getConfiguredTarget("//b:foo"));
assertThat(e).hasMessageThat().contains("Rule in 'b' cannot use private API");
}
}
}

0 comments on commit 8a2c937

Please sign in to comment.