Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implicit HeaderMaps #3712

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Sets;
import com.google.common.collect.Streams;
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.actions.Artifact.SpecialArtifact;
import com.google.devtools.build.lib.analysis.AnalysisEnvironment;
Expand Down Expand Up @@ -1035,6 +1037,79 @@ public CcCompilationInfo initializeCcCompilationInfo() {
}
}

// Setup Experimental implicit header maps if needed
if (ruleContext.getFragment(CppConfiguration.class).experimentalEnableImplicitHeaderMaps()) {
ImmutableList.Builder<Artifact> headerMapsBuilder = ImmutableList.builder();
String targetName = ruleContext.getTarget().getName();

HeaderMapInfo.Builder internalHeaderMapInfo = new HeaderMapInfo.Builder();
internalHeaderMapInfo.addHeaders(publicHeaders.getHeaders());
internalHeaderMapInfo.addHeaders(privateHeaders);
internalHeaderMapInfo.addHeaders(publicTextualHeaders);
Artifact internalHeaderMap = ruleContext.getPackageRelativeArtifact(PathFragment.create(targetName + "_internal.hmap"),
ruleContext
.getConfiguration()
.getGenfilesDirectory(ruleContext.getRule().getRepository()));
ruleContext.registerAction(
new HeaderMapAction(ruleContext.getActionOwner(),
internalHeaderMapInfo.build().getSources(),
internalHeaderMap));
ccCompilationInfoBuilder.addQuoteIncludeDir(internalHeaderMap.getExecPath());
headerMapsBuilder.add(internalHeaderMap);

String includePrefix;
if (ruleContext.attributes().has("include_prefix")) {
includePrefix = ruleContext.attributes().get("include_prefix", Type.STRING);
} else {
includePrefix = ruleContext.getRule().getName();
}

// Construct the dep headermap.
// This header map additionally contains include prefixed headers so that a user
// can import headers of the form IncludePrefix/Header.h from headers within
// the current target.
HeaderMapInfo.Builder depHeaderMapInfo = new HeaderMapInfo.Builder();
depHeaderMapInfo.setIncludePrefix(includePrefix);
depHeaderMapInfo.addIncludePrefixdHeaders(publicHeaders.getHeaders());
depHeaderMapInfo.addIncludePrefixdHeaders(privateHeaders);
depHeaderMapInfo.addIncludePrefixdHeaders(publicTextualHeaders);

// Flatten virtual headers into the headermap.
boolean flattenVirtualHeaders = ruleContext.attributes().has("flatten_virtual_headers") &&
ruleContext.attributes().get("flatten_virtual_headers", Type.BOOLEAN);
if (flattenVirtualHeaders) {
depHeaderMapInfo.addHeaders(publicTextualHeaders);
depHeaderMapInfo.addHeaders(publicHeaders.getHeaders());
depHeaderMapInfo.addHeaders(privateHeaders);
}

// Merge all of the header map info from deps. The headers within a given
// target have precedence over over dep headers ( See
// HeaderMapInfo.build() ).
if (ruleContext.attributes().has("deps")){
for (HeaderMapInfoProvider hmapProvider : ruleContext.getPrerequisites("deps", Mode.TARGET, HeaderMapInfoProvider.class)) {
depHeaderMapInfo.mergeHeaderMapInfo(hmapProvider.getInfo());
}
}
Artifact depHeaderMap = ruleContext.getPackageRelativeArtifact(PathFragment.create(targetName + ".hmap"),
ruleContext
.getConfiguration()
.getGenfilesDirectory(ruleContext.getRule().getRepository()));
ruleContext.registerAction(
new HeaderMapAction(ruleContext.getActionOwner(),
depHeaderMapInfo.build().getSources(),
depHeaderMap));
ccCompilationInfoBuilder.addIncludeDir(depHeaderMap.getExecPath());
headerMapsBuilder.add(depHeaderMap);

// If we have headermaps, then we need to add an include of
// the working directory ( i.e. exec root ) in this form
// and it must be after including the header map files
ccCompilationInfoBuilder.addIncludeDir(PathFragment.create("."));
ImmutableList headerMaps = headerMapsBuilder.build();
ccCompilationInfoBuilder.setHeaderMaps(headerMaps);
}

if (featureConfiguration.isEnabled(CppRuleClasses.MODULE_MAPS)) {
if (cppModuleMap == null) {
cppModuleMap = CppHelper.createDefaultCppModuleMap(ruleContext, /*suffix=*/ "");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.actions.MiddlemanFactory;
import com.google.devtools.build.lib.analysis.RuleContext;
import com.google.devtools.build.lib.analysis.TransitiveInfoProvider;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
import com.google.devtools.build.lib.collect.nestedset.Order;
Expand Down Expand Up @@ -84,6 +86,7 @@ public final class CcCompilationInfo extends NativeInfo {

private final CppModuleMap cppModuleMap;
private final CppModuleMap verificationModuleMap;
private final ImmutableList<Artifact> headerMaps;

private final boolean propagateModuleMapAsActionInput;

Expand All @@ -105,7 +108,8 @@ public final class CcCompilationInfo extends NativeInfo {
NestedSet<Artifact> directModuleMaps,
CppModuleMap cppModuleMap,
@Nullable CppModuleMap verificationModuleMap,
boolean propagateModuleMapAsActionInput) {
boolean propagateModuleMapAsActionInput,
ImmutableList<Artifact> headerMaps) {
super(PROVIDER);
Preconditions.checkNotNull(commandLineCcCompilationInfo);
this.commandLineCcCompilationInfo = commandLineCcCompilationInfo;
Expand All @@ -117,6 +121,7 @@ public final class CcCompilationInfo extends NativeInfo {
this.moduleInfo = moduleInfo;
this.picModuleInfo = picModuleInfo;
this.cppModuleMap = cppModuleMap;
this.headerMaps = headerMaps;
this.nonCodeInputs = nonCodeInputs;
this.verificationModuleMap = verificationModuleMap;
this.compilationPrerequisites = compilationPrerequisites;
Expand Down Expand Up @@ -233,6 +238,9 @@ public NestedSet<Artifact> getAdditionalInputs() {
if (cppModuleMap != null && propagateModuleMapAsActionInput) {
builder.add(cppModuleMap.getArtifact());
}
if (headerMaps != null) {
builder.addAll(headerMaps);
}
return builder.build();
}

Expand Down Expand Up @@ -282,7 +290,8 @@ public static CcCompilationInfo disallowUndeclaredHeaders(CcCompilationInfo ccCo
ccCompilationInfo.directModuleMaps,
ccCompilationInfo.cppModuleMap,
ccCompilationInfo.verificationModuleMap,
ccCompilationInfo.propagateModuleMapAsActionInput);
ccCompilationInfo.propagateModuleMapAsActionInput,
ccCompilationInfo.headerMaps);
}

/**
Expand Down Expand Up @@ -335,7 +344,8 @@ public static CcCompilationInfo mergeForLipo(
mergeSets(ownerCcCompilationInfo.directModuleMaps, libCcCompilationInfo.directModuleMaps),
libCcCompilationInfo.cppModuleMap,
libCcCompilationInfo.verificationModuleMap,
libCcCompilationInfo.propagateModuleMapAsActionInput);
libCcCompilationInfo.propagateModuleMapAsActionInput,
libCcCompilationInfo.headerMaps);
}

/**
Expand Down Expand Up @@ -405,6 +415,8 @@ public static class Builder {
private final Set<String> defines = new LinkedHashSet<>();
private CppModuleMap cppModuleMap;
private CppModuleMap verificationModuleMap;
private ImmutableList<Artifact> headerMaps;

private boolean propagateModuleMapAsActionInput = true;

/** The rule that owns the context */
Expand Down Expand Up @@ -637,6 +649,11 @@ public Builder setCppModuleMap(CppModuleMap cppModuleMap) {
return this;
}

public Builder setHeaderMaps(ImmutableList<Artifact> headerMaps) {
this.headerMaps = headerMaps;
return this;
}

/** Sets the C++ module map used to verify that headers are modules compatible. */
public Builder setVerificationModuleMap(CppModuleMap verificationModuleMap) {
this.verificationModuleMap = verificationModuleMap;
Expand Down Expand Up @@ -705,7 +722,8 @@ public CcCompilationInfo build(ActionOwner owner, MiddlemanFactory middlemanFact
directModuleMaps.build(),
cppModuleMap,
verificationModuleMap,
propagateModuleMapAsActionInput);
propagateModuleMapAsActionInput,
headerMaps);
}

/**
Expand Down
Loading