Skip to content

Commit f6c4e62

Browse files
oquenchilcopybara-github
authored andcommitted
Changes necessary for Starlark cc_library
Blaze changes for cc_library.bzl RELNOTES:none PiperOrigin-RevId: 427436694
1 parent 6244713 commit f6c4e62

5 files changed

Lines changed: 45 additions & 90 deletions

File tree

src/main/java/com/google/devtools/build/lib/rules/cpp/CcLinkingOutputs.java

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import com.google.common.collect.ImmutableList;
1919
import com.google.common.collect.ImmutableSetMultimap;
2020
import com.google.devtools.build.lib.actions.Artifact;
21-
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
2221
import com.google.devtools.build.lib.starlarkbuildapi.cpp.CcLinkingOutputsApi;
2322
import com.google.devtools.build.lib.vfs.FileSystemUtils;
2423
import javax.annotation.Nullable;
@@ -36,17 +35,14 @@ public class CcLinkingOutputs implements CcLinkingOutputsApi<Artifact, LtoBacken
3635
@Nullable private final Artifact executable;
3736

3837
private final ImmutableList<LtoBackendArtifacts> allLtoArtifacts;
39-
private final ImmutableList<Artifact> linkActionInputs;
4038

4139
private CcLinkingOutputs(
4240
LibraryToLink libraryToLink,
4341
Artifact executable,
44-
ImmutableList<LtoBackendArtifacts> allLtoArtifacts,
45-
ImmutableList<Artifact> linkActionInputs) {
42+
ImmutableList<LtoBackendArtifacts> allLtoArtifacts) {
4643
this.libraryToLink = libraryToLink;
4744
this.executable = executable;
4845
this.allLtoArtifacts = allLtoArtifacts;
49-
this.linkActionInputs = linkActionInputs;
5046
}
5147

5248
@Override
@@ -72,10 +68,6 @@ public Sequence<LtoBackendArtifacts> getAllLtoArtifactsForStarlark(StarlarkThrea
7268
return StarlarkList.immutableCopyOf(getAllLtoArtifacts());
7369
}
7470

75-
public ImmutableList<Artifact> getLinkActionInputs() {
76-
return linkActionInputs;
77-
}
78-
7971
public boolean isEmpty() {
8072
return libraryToLink == null;
8173
}
@@ -129,11 +121,9 @@ private Builder() {
129121
// same list return the .pdb file for Windows.
130122
private final ImmutableList.Builder<LtoBackendArtifacts> allLtoArtifacts =
131123
ImmutableList.builder();
132-
private final ImmutableList.Builder<Artifact> linkActionInputs = ImmutableList.builder();
133124

134125
public CcLinkingOutputs build() {
135-
return new CcLinkingOutputs(
136-
libraryToLink, executable, allLtoArtifacts.build(), linkActionInputs.build());
126+
return new CcLinkingOutputs(libraryToLink, executable, allLtoArtifacts.build());
137127
}
138128

139129
public Builder setLibraryToLink(LibraryToLink libraryToLink) {
@@ -150,10 +140,5 @@ public Builder addAllLtoArtifacts(Iterable<LtoBackendArtifacts> allLtoArtifacts)
150140
this.allLtoArtifacts.addAll(allLtoArtifacts);
151141
return this;
152142
}
153-
154-
public Builder addLinkActionInputs(NestedSet<Artifact> linkActionInputs) {
155-
this.linkActionInputs.addAll(linkActionInputs.toList());
156-
return this;
157-
}
158143
}
159144
}

src/main/java/com/google/devtools/build/lib/rules/cpp/CcModule.java

100644100755
Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -635,12 +635,6 @@ public LibraryToLink createLibraryLinkerInput(
635635
"If you pass '%s_library', you must also pass a 'feature_configuration'", library);
636636
}
637637
}
638-
if (nopicObjects != null && staticLibrary == null) {
639-
throw Starlark.errorf("If you pass 'objects' you must also pass a 'static_library'");
640-
}
641-
if (picObjects != null && picStaticLibrary == null) {
642-
throw Starlark.errorf("If you pass 'pic_objects' you must also pass a 'pic_static_library'");
643-
}
644638
if (notNullArtifactForIdentifier == null) {
645639
throw Starlark.errorf("Must pass at least one artifact");
646640
}
@@ -1804,7 +1798,7 @@ public Tuple createLinkingContextFromCompilationOutputs(
18041798
CcToolchainProvider starlarkCcToolchainProvider,
18051799
CcCompilationOutputs compilationOutputs,
18061800
Sequence<?> userLinkFlags, // <String> expected
1807-
Sequence<?> linkingContexts, // <CcLinkingContext> expected
1801+
Sequence<?> linkingContextsObjects, // <CcLinkingContext> expected
18081802
String name,
18091803
String languageString,
18101804
boolean alwayslink,
@@ -1814,9 +1808,11 @@ public Tuple createLinkingContextFromCompilationOutputs(
18141808
Object grepIncludes,
18151809
Object variablesExtension,
18161810
Object stamp,
1811+
Object linkedDllNameSuffix,
1812+
Object winDefFile,
18171813
StarlarkThread thread)
18181814
throws InterruptedException, EvalException {
1819-
if (checkObjectsBound(stamp)) {
1815+
if (checkObjectsBound(stamp, linkedDllNameSuffix, winDefFile)) {
18201816
CcModule.checkPrivateStarlarkificationAllowlist(thread);
18211817
}
18221818
Language language = parseLanguage(languageString);
@@ -1845,6 +1841,8 @@ public Tuple createLinkingContextFromCompilationOutputs(
18451841
} else {
18461842
staticLinkTargetType = LinkTargetType.OBJC_ARCHIVE;
18471843
}
1844+
List<CcLinkingContext> ccLinkingContexts =
1845+
Sequence.cast(linkingContextsObjects, CcLinkingContext.class, "linking_contexts");
18481846
CcLinkingHelper helper =
18491847
new CcLinkingHelper(
18501848
actions.getActionConstructionContext().getRuleErrorConsumer(),
@@ -1868,43 +1866,42 @@ public Tuple createLinkingContextFromCompilationOutputs(
18681866
.addNonCodeLinkerInputs(
18691867
Sequence.cast(additionalInputs, Artifact.class, "additional_inputs"))
18701868
.setShouldCreateStaticLibraries(!disallowStaticLibraries)
1869+
.addCcLinkingContexts(ccLinkingContexts)
18711870
.setShouldCreateDynamicLibrary(
18721871
!disallowDynamicLibraries
1873-
&& !featureConfiguration
1874-
.getFeatureConfiguration()
1875-
.isEnabled(CppRuleClasses.TARGETS_WINDOWS))
1872+
&& (!featureConfiguration
1873+
.getFeatureConfiguration()
1874+
.isEnabled(CppRuleClasses.TARGETS_WINDOWS)
1875+
|| winDefFile != null))
18761876
.setStaticLinkType(staticLinkTargetType)
18771877
.setDynamicLinkType(LinkTargetType.NODEPS_DYNAMIC_LIBRARY)
18781878
.emitInterfaceSharedLibraries(true)
1879+
.setLinkedDLLNameSuffix(
1880+
convertFromNoneable(linkedDllNameSuffix, /* defaultValue= */ ""))
1881+
.setDefFile(convertFromNoneable(winDefFile, /* defaultValue= */ null))
18791882
.setIsStampingEnabled(isStampingEnabled)
18801883
.addLinkopts(Sequence.cast(userLinkFlags, String.class, "user_link_flags"));
18811884
if (!asDict(variablesExtension).isEmpty()) {
18821885
helper.addVariableExtension(new UserVariablesExtension(asDict(variablesExtension)));
18831886
}
18841887
try {
1885-
CcLinkingOutputs ccLinkingOutputs = CcLinkingOutputs.EMPTY;
18861888
ImmutableList<LibraryToLink> libraryToLink = ImmutableList.of();
1887-
if (!compilationOutputs.isEmpty()) {
1888-
ccLinkingOutputs = helper.link(compilationOutputs);
1889-
if (!ccLinkingOutputs.isEmpty()) {
1890-
libraryToLink =
1891-
ImmutableList.of(
1892-
ccLinkingOutputs.getLibraryToLink().toBuilder()
1893-
.setAlwayslink(alwayslink)
1894-
.build());
1895-
}
1889+
CcLinkingOutputs ccLinkingOutputs = helper.link(compilationOutputs);
1890+
if (!ccLinkingOutputs.isEmpty()) {
1891+
LibraryToLink rewrappedForAlwaysLink =
1892+
ccLinkingOutputs.getLibraryToLink().toBuilder().setAlwayslink(alwayslink).build();
1893+
ccLinkingOutputs =
1894+
CcLinkingOutputs.builder()
1895+
.setExecutable(ccLinkingOutputs.getExecutable())
1896+
.setLibraryToLink(rewrappedForAlwaysLink)
1897+
.addAllLtoArtifacts(ccLinkingOutputs.getAllLtoArtifacts())
1898+
.build();
1899+
libraryToLink = ImmutableList.of(rewrappedForAlwaysLink);
18961900
}
18971901
CcLinkingContext linkingContext =
18981902
helper.buildCcLinkingContextFromLibrariesToLink(
18991903
libraryToLink, CcCompilationContext.EMPTY);
1900-
return Tuple.of(
1901-
CcLinkingContext.merge(
1902-
ImmutableList.<CcLinkingContext>builder()
1903-
.add(linkingContext)
1904-
.addAll(
1905-
Sequence.cast(linkingContexts, CcLinkingContext.class, "linking_contexts"))
1906-
.build()),
1907-
ccLinkingOutputs);
1904+
return Tuple.of(linkingContext, ccLinkingOutputs);
19081905
} catch (RuleErrorException e) {
19091906
throw Starlark.errorf("%s", e.getMessage());
19101907
}
@@ -2002,7 +1999,7 @@ protected Tuple compile(
20021999
Object textualHeadersStarlarkObject,
20032000
Object additionalExportedHeadersObject,
20042001
Sequence<?> includes, // <String> expected
2005-
Object starlarkIncludes,
2002+
Object starlarkLooseIncludes,
20062003
Sequence<?> quoteIncludes, // <String> expected
20072004
Sequence<?> systemIncludes, // <String> expected
20082005
Sequence<?> frameworkIncludes, // <String> expected
@@ -2042,15 +2039,15 @@ protected Tuple compile(
20422039
hdrsCheckingModeObject,
20432040
implementationCcCompilationContextsObject,
20442041
coptsFilterObject,
2045-
starlarkIncludes)) {
2042+
starlarkLooseIncludes)) {
20462043
CcModule.checkPrivateStarlarkificationAllowlist(thread);
20472044
}
20482045

20492046
StarlarkActionFactory actions = starlarkActionFactoryApi;
20502047
CcToolchainProvider ccToolchainProvider =
20512048
convertFromNoneable(starlarkCcToolchainProvider, null);
20522049

2053-
ImmutableList<String> looseIncludes = asClassImmutableList(starlarkIncludes);
2050+
ImmutableList<String> looseIncludes = asClassImmutableList(starlarkLooseIncludes);
20542051
CppModuleMap moduleMap = convertFromNoneable(moduleMapNoneable, /* defaultValue= */ null);
20552052
ImmutableList<CppModuleMap> additionalModuleMaps =
20562053
asClassImmutableList(additionalModuleMapsNoneable);

src/main/java/com/google/devtools/build/lib/rules/cpp/LibraryToLink.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -363,18 +363,6 @@ public abstract static class Builder implements LibraryToLink.Builder {
363363
public final LibraryToLink build() {
364364
LibraryToLink result = autoBuild();
365365
Preconditions.checkNotNull(result.getLibraryIdentifier(), result);
366-
Preconditions.checkState(
367-
(result.getObjectFiles() == null
368-
&& result.getLtoCompilationContext() == null
369-
&& result.getSharedNonLtoBackends() == null)
370-
|| result.getStaticLibrary() != null,
371-
result);
372-
Preconditions.checkState(
373-
(result.getPicObjectFiles() == null
374-
&& result.getPicLtoCompilationContext() == null
375-
&& result.getPicSharedNonLtoBackends() == null)
376-
|| result.getPicStaticLibrary() != null,
377-
result);
378366
Preconditions.checkState(
379367
result.getResolvedSymlinkDynamicLibrary() == null || result.getDynamicLibrary() != null,
380368
result);

src/main/java/com/google/devtools/build/lib/starlarkbuildapi/cpp/CcModuleApi.java

100644100755
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,6 +1236,18 @@ CcToolchainConfigInfoT ccToolchainConfigInfoFromStarlark(
12361236
named = true,
12371237
documented = false,
12381238
defaultValue = "unbound"),
1239+
@Param(
1240+
name = "linked_dll_name_suffix",
1241+
positional = false,
1242+
named = true,
1243+
documented = false,
1244+
defaultValue = "unbound"),
1245+
@Param(
1246+
name = "win_def_file",
1247+
documented = false,
1248+
positional = false,
1249+
named = true,
1250+
defaultValue = "unbound"),
12391251
})
12401252
Tuple createLinkingContextFromCompilationOutputs(
12411253
StarlarkActionFactoryT starlarkActionFactoryApi,
@@ -1253,6 +1265,8 @@ Tuple createLinkingContextFromCompilationOutputs(
12531265
Object grepIncludes,
12541266
Object variablesExtension,
12551267
Object stamp,
1268+
Object linkedDllNameSuffix,
1269+
Object winDefFile,
12561270
StarlarkThread thread)
12571271
throws InterruptedException, EvalException;
12581272

src/test/java/com/google/devtools/build/lib/rules/cpp/StarlarkCcCommonTest.java

100644100755
Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6745,35 +6745,6 @@ public void testObjectFilesInCreateLibrary() throws Exception {
67456745
.containsExactly("object.pic.o");
67466746
}
67476747

6748-
@Test
6749-
public void testObjectFilesInCreateLibraryWithoutStaticLibrary() throws Exception {
6750-
setUpCcLinkingContextTest(true);
6751-
scratch.file(
6752-
"b/BUILD",
6753-
"load('//tools/build_defs/cc:rule.bzl', 'crule')",
6754-
"crule(name='import_objects_no_lib',",
6755-
" objects = ['object.o'],",
6756-
")");
6757-
6758-
checkError(
6759-
"//b:import_objects_no_lib", "If you pass 'objects' you must also pass a 'static_library'");
6760-
}
6761-
6762-
@Test
6763-
public void testObjectFilesInCreateLibraryWithoutPicStaticLibrary() throws Exception {
6764-
setUpCcLinkingContextTest(true);
6765-
scratch.file(
6766-
"b/BUILD",
6767-
"load('//tools/build_defs/cc:rule.bzl', 'crule')",
6768-
"crule(name='import_objects_no_pic_lib',",
6769-
" pic_objects = ['object.pic.o'],",
6770-
")");
6771-
6772-
checkError(
6773-
"//b:import_objects_no_pic_lib",
6774-
"If you pass 'pic_objects' you must also pass a 'pic_static_library'");
6775-
}
6776-
67776748
private void setupDebugPackageProviderTest(String fission) throws Exception {
67786749
getAnalysisMock()
67796750
.ccSupport()

0 commit comments

Comments
 (0)