diff options
author | 2016-09-21 19:57:01 +0000 | |
---|---|---|
committer | 2016-09-22 09:54:11 +0000 | |
commit | f0a716ae7145f1e815cd59b8d71ff037c40a3fa5 (patch) | |
tree | a1b5ff5005c23bc44db7a1a9b12c29e582295d4d /src/main/java/com | |
parent | 9f6563f44471315ed953f698b7da11a61cc84240 (diff) |
Some updates to C++ to support Clif, an automatic interface generator for C++/Python.
--
MOS_MIGRATED_REVID=133863929
Diffstat (limited to 'src/main/java/com')
9 files changed, 110 insertions, 19 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/ArtifactCategory.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/ArtifactCategory.java index 68ec14b4bd..7097b44e67 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/ArtifactCategory.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/ArtifactCategory.java @@ -32,7 +32,10 @@ public enum ArtifactCategory { GENERATED_HEADER("%{output_name}.h"), PREPROCESSED_C_SOURCE("%{output_name}.i"), PREPROCESSED_CPP_SOURCE("%{output_name}.ii"), - COVERAGE_DATA_FILE("%{output_name}.gcno"); + COVERAGE_DATA_FILE("%{output_name}.gcno"), + // A matched-clif protobuf. Typically in binary format, but could be text depending on + // the options passed to the clif_matcher. + CLIF_OUTPUT_PROTO("%{output_name}.opb"); private final String defaultPattern; diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcLibraryHelper.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcLibraryHelper.java index de7373a8ba..c1bd2b1789 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcLibraryHelper.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcLibraryHelper.java @@ -103,6 +103,7 @@ public final class CcLibraryHelper { CppCompileAction.CPP_MODULE_COMPILE, CppCompileAction.ASSEMBLE, CppCompileAction.PREPROCESS_ASSEMBLE, + CppCompileAction.CLIF_MATCH, Link.LinkTargetType.STATIC_LIBRARY.getActionName(), // We need to create pic-specific actions for link actions, as they will produce // differently named outputs. diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppCompileAction.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppCompileAction.java index 21d0d307c5..322ed53c72 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppCompileAction.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppCompileAction.java @@ -156,6 +156,14 @@ public class CppCompileAction extends AbstractAction public static final String ASSEMBLE = "assemble"; public static final String PREPROCESS_ASSEMBLE = "preprocess-assemble"; + /** + * A string constant for the clif actions. Bazel enables different features of the toolchain based + * on the name of the action. This name enables the clif_matcher feature, which switches the + * "compiler" to the clif_matcher and adds some additional arguments as described in the CROSSTOOL + * file. + */ + public static final String CLIF_MATCH = "clif-match"; + // TODO(ulfjack): this is only used to get the local shell environment and to check if coverage is // enabled. Move those two things to local fields and drop this. Accessing anything other than // these fields can impact correctness! @@ -170,6 +178,10 @@ public class CppCompileAction extends AbstractAction private final CppCompilationContext context; private final Iterable<IncludeScannable> lipoScannables; private final ImmutableList<Artifact> builtinIncludeFiles; + // A list of files to include scan that are not source files, pcm files, lipo scannables, or + // included via a command-line "-include file.h". Actions that use non C++ files as source + // files--such as Clif--may use this mechanism. + private final ImmutableList<Artifact> additionalIncludeScannables; @VisibleForTesting public final CppCompileCommandLine cppCompileCommandLine; private final ImmutableSet<String> executionRequirements; private final ImmutableMap<String, String> environment; @@ -206,10 +218,14 @@ public class CppCompileAction extends AbstractAction * Creates a new action to compile C/C++ source files. * * @param owner the owner of the action, usually the configured target that emitted it + * @param features TODO(bazel-team): Add parameter description. + * @param featureConfiguration TODO(bazel-team): Add parameter description. + * @param variables TODO(bazel-team): Add parameter description. * @param sourceFile the source file that should be compiled. {@code mandatoryInputs} must contain * this file * @param shouldScanIncludes a boolean indicating whether scanning of {@code sourceFile} is to be * performed looking for inclusions. + * @param usePic TODO(bazel-team): Add parameter description. * @param sourceLabel the label of the rule the source file is generated by * @param mandatoryInputs any additional files that need to be present for the compilation to * succeed, can be empty but not null, for example, extra sources for FDO. @@ -221,13 +237,21 @@ public class CppCompileAction extends AbstractAction * if Fission mode is disabled) * @param optionalSourceFile an additional optional source file (null if unneeded) * @param configuration the build configurations + * @param cppConfiguration TODO(bazel-team): Add parameter description. * @param context the compilation context + * @param actionContext TODO(bazel-team): Add parameter description. * @param copts options for the compiler * @param coptsFilter regular expression to remove options from {@code copts} + * @param specialInputsHandler TODO(bazel-team): Add parameter description. + * @param lipoScannables List of artifacts to include-scan when this action is a lipo action + * @param additionalIncludeScannables list of additional artifacts to include-scan + * @param actionClassId TODO(bazel-team): Add parameter description * @param executionRequirements out-of-band hints to be passed to the execution backend to signal * platform requirements + * @param environment TODO(bazel-team): Add parameter description * @param actionName a string giving the name of this action for the purpose of toolchain * evaluation + * @param ruleContext The rule-context that produced this action */ protected CppCompileAction( ActionOwner owner, @@ -255,6 +279,7 @@ public class CppCompileAction extends AbstractAction Predicate<String> coptsFilter, SpecialInputsHandler specialInputsHandler, Iterable<IncludeScannable> lipoScannables, + ImmutableList<Artifact> additionalIncludeScannables, UUID actionClassId, ImmutableSet<String> executionRequirements, ImmutableMap<String, String> environment, @@ -304,6 +329,7 @@ public class CppCompileAction extends AbstractAction if (semantics.needsIncludeValidation()) { verifyIncludePaths(ruleContext); } + this.additionalIncludeScannables = ImmutableList.copyOf(additionalIncludeScannables); } /** @@ -391,6 +417,10 @@ public class CppCompileAction extends AbstractAction return builtinIncludeFiles; } + public List<Artifact> getadditionalIncludeScannables() { + return additionalIncludeScannables; + } + public String getHostSystemName() { return cppConfiguration.getHostSystemName(); } @@ -619,6 +649,7 @@ public class CppCompileAction extends AbstractAction builder.addAll(context.getHeaderModuleSrcs()); } else { builder.add(getSourceFile()); + builder.addAll(additionalIncludeScannables); } return builder.build().toCollection(); } @@ -1369,7 +1400,8 @@ public class CppCompileAction extends AbstractAction } if (CppFileTypes.CPP_SOURCE.matches(sourceFilename) || CppFileTypes.CPP_HEADER.matches(sourceFilename) - || CppFileTypes.CPP_MODULE_MAP.matches(sourceFilename)) { + || CppFileTypes.CPP_MODULE_MAP.matches(sourceFilename) + || CppFileTypes.CLIF_INPUT_PROTO.matches(sourceFilename)) { addFilteredOptions(options, toolchain.getCxxOptions(features)); } diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppCompileActionBuilder.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppCompileActionBuilder.java index 00a26ce192..6a4b88a7ac 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppCompileActionBuilder.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppCompileActionBuilder.java @@ -76,6 +76,8 @@ public class CppCompileActionBuilder { private Class<? extends CppCompileActionContext> actionContext; private CppConfiguration cppConfiguration; private ImmutableMap<Artifact, IncludeScannable> lipoScannableMap; + private final ImmutableList.Builder<Artifact> additionalIncludeFiles = + new ImmutableList.Builder<>(); private RuleContext ruleContext = null; private Boolean shouldScanIncludes; private Map<String, String> environment = new LinkedHashMap<>(); @@ -228,6 +230,8 @@ public class CppCompileActionBuilder { return CppCompileAction.ASSEMBLE; } else if (CppFileTypes.ASSEMBLER_WITH_C_PREPROCESSOR.matches(sourcePath)) { return CppCompileAction.PREPROCESS_ASSEMBLE; + } else if (CppFileTypes.CLIF_INPUT_PROTO.matches(sourcePath)) { + return CppCompileAction.CLIF_MATCH; } // CcLibraryHelper ensures CppCompileAction only gets instantiated for supported file types. throw new IllegalStateException(); @@ -334,6 +338,7 @@ public class CppCompileActionBuilder { getNocoptPredicate(nocopts), specialInputsHandler, getLipoScannables(realMandatoryInputs), + additionalIncludeFiles.build(), actionClassId, executionRequirements, ImmutableMap.copyOf(environment), @@ -408,6 +413,11 @@ public class CppCompileActionBuilder { return this; } + public CppCompileActionBuilder addAdditionalIncludes(List<Artifact> includes) { + additionalIncludeFiles.addAll(includes); + return this; + } + public CppCompileActionBuilder setOutputsForTesting(Artifact outputFile, Artifact dotdFile) { this.outputFile = outputFile; this.dotdFile = dotdFile == null ? null : new DotdFile(dotdFile); diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppConfiguration.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppConfiguration.java index 471815a2bd..6c5445e5e0 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppConfiguration.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppConfiguration.java @@ -829,21 +829,23 @@ public class CppConfiguration extends BuildConfiguration.Fragment { } if (!features.contains("preprocessor_defines")) { - TextFormat.merge("" - + "feature {" - + " name: 'preprocessor_defines'" - + " flag_set {" - + " action: 'preprocess-assemble'" - + " action: 'c-compile'" - + " action: 'c++-compile'" - + " action: 'c++-header-parsing'" - + " action: 'c++-header-preprocessing'" - + " action: 'c++-module-compile'" - + " flag_group {" - + " flag: '-D%{preprocessor_defines}'" - + " }" - + " }" - + "}", + TextFormat.merge( + "" + + "feature {" + + " name: 'preprocessor_defines'" + + " flag_set {" + + " action: 'preprocess-assemble'" + + " action: 'c-compile'" + + " action: 'c++-compile'" + + " action: 'c++-header-parsing'" + + " action: 'c++-header-preprocessing'" + + " action: 'c++-module-compile'" + + " action: 'clif-match'" + + " flag_group {" + + " flag: '-D%{preprocessor_defines}'" + + " }" + + " }" + + "}", toolchainBuilder); } if (!features.contains("include_paths")) { @@ -858,6 +860,7 @@ public class CppConfiguration extends BuildConfiguration.Fragment { + " action: 'c++-header-parsing'" + " action: 'c++-header-preprocessing'" + " action: 'c++-module-compile'" + + " action: 'clif-match'" + " action: 'objc-compile'" + " action: 'objc++-compile'" + " flag_group {" diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppFileTypes.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppFileTypes.java index ef6e7a1570..59476636e4 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppFileTypes.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppFileTypes.java @@ -16,7 +16,6 @@ package com.google.devtools.build.lib.rules.cpp; import com.google.common.collect.ImmutableList; import com.google.devtools.build.lib.util.FileType; import com.google.devtools.build.lib.util.FileTypeSet; - import java.util.List; import java.util.regex.Pattern; @@ -149,7 +148,13 @@ public final class CppFileTypes { // Output of the dwp tool public static final FileType DEBUG_INFO_PACKAGE = FileType.of(".dwp"); + public static final FileType CLIF_INPUT_PROTO = FileType.of(".ipb"); + public static final FileType CLIF_OUTPUT_PROTO = FileType.of(".opb"); + public static final boolean mustProduceDotdFile(String source) { - return !(ASSEMBLER.matches(source) || PIC_ASSEMBLER.matches(source)); + return !ASSEMBLER.matches(source) + && !PIC_ASSEMBLER.matches(source) + && !CLIF_INPUT_PROTO.matches(source); } + } diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppModel.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppModel.java index 426089cd93..d7f7e83bc9 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppModel.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppModel.java @@ -69,6 +69,7 @@ public final class CppModel { private CppCompilationContext context; private CppCompilationContext interfaceContext; private final Set<CppSource> sourceFiles = new LinkedHashSet<>(); + private final List<Artifact> mandatoryInputs = new ArrayList<>(); private final List<String> copts = new ArrayList<>(); @Nullable private Pattern nocopts; private boolean fake; @@ -165,6 +166,12 @@ public final class CppModel { return this; } + /** Adds mandatory inputs. */ + public CppModel addMandatoryInputs(Collection<Artifact> artifacts) { + this.mandatoryInputs.addAll(artifacts); + return this; + } + /** * Adds the given copts. */ @@ -527,6 +534,8 @@ public final class CppModel { if (CppFileTypes.CPP_HEADER.matches(source.getSource().getExecPath())) { createHeaderAction(outputName, result, env, builder, CppFileTypes.mustProduceDotdFile(sourceArtifact.getFilename())); + } else if (CppFileTypes.CLIF_INPUT_PROTO.matches(source.getSource().getExecPath())) { + createClifMatchAction(outputName, result, env, builder); } else { createSourceAction( outputName, @@ -571,6 +580,30 @@ public final class CppModel { result.addHeaderTokenFile(tokenFile); } + private void createClifMatchAction( + String outputName, Builder result, AnalysisEnvironment env, CppCompileActionBuilder builder) { + builder + .setOutputs(ArtifactCategory.CLIF_OUTPUT_PROTO, outputName, false) + .setPicMode(false) + // The additional headers in a clif action are both mandatory inputs and + // need to be include-scanned. + .addMandatoryInputs(mandatoryInputs) + .addAdditionalIncludes(mandatoryInputs); + setupCompileBuildVariables( + builder, + /* usePic=*/ false, + /*ccRelativeName=*/ null, + /*autoFdoImportPath=*/ null, + /*gcnoFile=*/ null, + /*dwoFile=*/ null, + /*sourceSpecificBuildVariables=*/ ImmutableMap.<String, String>of()); + semantics.finalizeCompileActionBuilder(ruleContext, builder); + CppCompileAction compileAction = builder.build(); + env.registerAction(compileAction); + Artifact tokenFile = compileAction.getOutputFile(); + result.addHeaderTokenFile(tokenFile); + } + private void createSourceAction( String outputName, CcCompilationOutputs.Builder result, diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppRuleClasses.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppRuleClasses.java index c53434be53..daae7b6f67 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppRuleClasses.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppRuleClasses.java @@ -297,4 +297,7 @@ public class CppRuleClasses { * A string constant for the coverage feature. */ public static final String COVERAGE = "coverage"; + + /** A string constant for the match-clif feature. */ + public static final String MATCH_CLIF = "match_clif"; } diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/FakeCppCompileAction.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/FakeCppCompileAction.java index 631e697331..ad7d5fe735 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/FakeCppCompileAction.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/FakeCppCompileAction.java @@ -111,6 +111,7 @@ public class FakeCppCompileAction extends CppCompileAction { nocopts, VOID_SPECIAL_INPUTS_HANDLER, ImmutableList.<IncludeScannable>of(), + ImmutableList.<Artifact>of(), GUID, ImmutableSet.<String>of(), ImmutableMap.<String, String>of(), |