Skip to content

Commit

Permalink
C++: Makes Skylark CcCompilationInfo accept headers
Browse files Browse the repository at this point in the history
The Skylark constructor of CcCompilationInfo now accepts headers. This may be
the last piece needed to get a working prototype of foreign C++ libraries. Next
step would be open sourcing the sandwich.

RELNOTES:none
PiperOrigin-RevId: 202306252
  • Loading branch information
oquenchil authored and Copybara-Service committed Jun 27, 2018
1 parent 1f319ff commit 38fa83c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ public Builder addDeclaredIncludeSrc(Artifact header) {
*
* <p>Filters out fileset directory artifacts, which are not valid inputs.
*/
public Builder addDeclaredIncludeSrcs(Collection<Artifact> declaredIncludeSrcs) {
public Builder addDeclaredIncludeSrcs(Iterable<Artifact> declaredIncludeSrcs) {
for (Artifact source : declaredIncludeSrcs) {
addDeclaredIncludeSrc(source);
}
Expand Down Expand Up @@ -636,8 +636,11 @@ public CcCompilationContext build(ActionOwner owner, MiddlemanFactory middlemanF
ImmutableList.copyOf(quoteIncludeDirs),
ImmutableList.copyOf(systemIncludeDirs),
ImmutableList.copyOf(defines)),
// TODO(b/110873917): We don't have the middle man compilation prerequisite, therefore, we
// use the compilation prerequisites as they were passed to the builder, i.e. we use every
// header instead of a middle man.
prerequisiteStampFile == null
? ImmutableSet.of()
? ImmutableSet.copyOf(compilationPrerequisites)
: ImmutableSet.of(prerequisiteStampFile),
declaredIncludeDirs.build(),
declaredIncludeWarnDirs.build(),
Expand All @@ -659,7 +662,7 @@ public CcCompilationContext build(ActionOwner owner, MiddlemanFactory middlemanF
*/
private Artifact createMiddleman(ActionOwner owner,
MiddlemanFactory middlemanFactory) {
if (compilationPrerequisites.isEmpty()) {
if (middlemanFactory == null || compilationPrerequisites.isEmpty()) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.analysis.AnalysisUtils;
import com.google.devtools.build.lib.analysis.TransitiveInfoCollection;
import com.google.devtools.build.lib.analysis.TransitiveInfoProvider;
Expand All @@ -29,7 +30,10 @@
import com.google.devtools.build.lib.syntax.Environment;
import com.google.devtools.build.lib.syntax.EvalException;
import com.google.devtools.build.lib.syntax.FunctionSignature;
import com.google.devtools.build.lib.syntax.Runtime;
import com.google.devtools.build.lib.syntax.SkylarkNestedSet;
import com.google.devtools.build.lib.syntax.SkylarkType;
import javax.annotation.Nullable;

/** Wrapper for every C++ compilation provider. */
@Immutable
Expand All @@ -40,12 +44,22 @@ public final class CcCompilationInfo extends NativeInfo implements CcCompilation
FunctionSignature.of(
/* numMandatoryPositionals= */ 0,
/* numOptionalPositionals= */ 0,
/* numMandatoryNamedOnly= */ 1,
/* numMandatoryNamedOnly= */ 0,
/* starArg= */ false,
/* kwArg= */ false,
"cc_compilation_info"),
/* defaultValues= */ ImmutableList.of(),
/* types= */ ImmutableList.of(SkylarkType.of(CcCompilationContext.class)));
"headers"),
/* defaultValues= */ ImmutableList.of(Runtime.NONE),
/* types= */ ImmutableList.of(SkylarkType.of(SkylarkNestedSet.class)));

@Nullable
private static Object nullIfNone(Object object) {
return nullIfNone(object, Object.class);
}

@Nullable
private static <T> T nullIfNone(Object object, Class<T> type) {
return object != Runtime.NONE ? type.cast(object) : null;
}

public static final NativeProvider<CcCompilationInfo> PROVIDER =
new NativeProvider<CcCompilationInfo>(
Expand All @@ -56,8 +70,13 @@ protected CcCompilationInfo createInstanceFromSkylark(
Object[] args, Environment env, Location loc) throws EvalException {
CcCommon.checkLocationWhitelisted(loc);
CcCompilationInfo.Builder ccCompilationInfoBuilder = CcCompilationInfo.Builder.create();
ccCompilationInfoBuilder.setCcCompilationContext(
((CcCompilationInfo) args[0]).getCcCompilationContext());
SkylarkNestedSet headers = (SkylarkNestedSet) nullIfNone(args[0]);
CcCompilationContext.Builder ccCompilationContext =
new CcCompilationContext.Builder(/* ruleContext= */ null);
if (headers != null) {
ccCompilationContext.addDeclaredIncludeSrcs(headers.getSet(Artifact.class));
}
ccCompilationInfoBuilder.setCcCompilationContext(ccCompilationContext.build());
return ccCompilationInfoBuilder.build();
}
};
Expand Down

0 comments on commit 38fa83c

Please sign in to comment.