public CppCompileActionBuilder setDotdFile(PathFragment outputName, String extension) { if (CppFileTypes.mustProduceDotdFile(outputName.toString())) { if (configuration.getFragment(CppConfiguration.class).getInmemoryDotdFiles()) { // Just set the path, no artifact is constructed PathFragment file = FileSystemUtils.replaceExtension(outputName, extension); Root root = configuration.getBinDirectory(); dotdFile = new DotdFile(root.getExecPath().getRelative(file)); } else { dotdFile = new DotdFile(ruleContext.getRelatedArtifact(outputName, extension)); } } else { dotdFile = null; } return this; }
/** * Constructs the C++ compiler actions. It generally creates one action for every specified source * file. It takes into account LIPO, fake-ness, coverage, and PIC, in addition to using the * settings specified on the current object. This method should only be called once. */ public CcCompilationOutputs createCcCompileActions() { CcCompilationOutputs.Builder result = new CcCompilationOutputs.Builder(); Preconditions.checkNotNull(context); AnalysisEnvironment env = ruleContext.getAnalysisEnvironment(); if (shouldProvideHeaderModules()) { Artifact moduleMapArtifact = context.getCppModuleMap().getArtifact(); Label moduleMapLabel = Label.parseAbsoluteUnchecked(context.getCppModuleMap().getName()); CppCompileActionBuilder builder = initializeCompileAction(moduleMapArtifact, moduleMapLabel, /*forInterface=*/ true); // A header module compile action is just like a normal compile action, but: // - the compiled source file is the module map // - it creates a header module (.pcm file). createSourceAction( FileSystemUtils.removeExtension(semantics.getEffectiveSourcePath(moduleMapArtifact)) .getPathString(), result, env, moduleMapArtifact, builder, ArtifactCategory.CPP_MODULE, /*addObject=*/ false, /*enableCoverage=*/ false, /*generateDwo=*/ false, CppFileTypes.mustProduceDotdFile(moduleMapArtifact.getFilename()), ImmutableMap.<String, String>of()); } for (CppSource source : sourceFiles) { Artifact sourceArtifact = source.getSource(); Label sourceLabel = source.getLabel(); String outputName = FileSystemUtils.removeExtension(semantics.getEffectiveSourcePath(sourceArtifact)) .getPathString(); CppCompileActionBuilder builder = initializeCompileAction(sourceArtifact, sourceLabel, /*forInterface=*/ false); if (CppFileTypes.CPP_HEADER.matches(source.getSource().getExecPath())) { createHeaderAction( outputName, result, env, builder, CppFileTypes.mustProduceDotdFile(sourceArtifact.getFilename())); } else { createSourceAction( outputName, result, env, sourceArtifact, builder, ArtifactCategory.OBJECT_FILE, /*addObject=*/ true, isCodeCoverageEnabled(), /*generateDwo=*/ cppConfiguration.useFission(), CppFileTypes.mustProduceDotdFile(sourceArtifact.getFilename()), source.getBuildVariables()); } } compilationOutputs = result.build(); return compilationOutputs; }
private void setupCompileBuildVariables( CppCompileActionBuilder builder, boolean usePic, PathFragment ccRelativeName, PathFragment autoFdoImportPath, Artifact gcnoFile, Artifact dwoFile, Map<String, String> sourceSpecificBuildVariables) { CcToolchainFeatures.Variables.Builder buildVariables = new CcToolchainFeatures.Variables.Builder(); // TODO(bazel-team): Pull out string constants for all build variables. CppCompilationContext builderContext = builder.getContext(); CppModuleMap cppModuleMap = builderContext.getCppModuleMap(); Artifact sourceFile = builder.getSourceFile(); Artifact outputFile = builder.getOutputFile(); String realOutputFilePath; buildVariables.addVariable("source_file", sourceFile.getExecPathString()); buildVariables.addVariable("output_file", outputFile.getExecPathString()); if (builder.getTempOutputFile() != null) { realOutputFilePath = builder.getTempOutputFile().getPathString(); } else { realOutputFilePath = builder.getOutputFile().getExecPathString(); } if (FileType.contains(outputFile, CppFileTypes.ASSEMBLER, CppFileTypes.PIC_ASSEMBLER)) { buildVariables.addVariable("output_assembly_file", realOutputFilePath); } else if (FileType.contains( outputFile, CppFileTypes.PREPROCESSED_C, CppFileTypes.PREPROCESSED_CPP, CppFileTypes.PIC_PREPROCESSED_C, CppFileTypes.PIC_PREPROCESSED_CPP)) { buildVariables.addVariable("output_preprocess_file", realOutputFilePath); } else { buildVariables.addVariable("output_object_file", realOutputFilePath); } DotdFile dotdFile = CppFileTypes.mustProduceDotdFile(sourceFile.getPath().toString()) ? Preconditions.checkNotNull(builder.getDotdFile()) : null; // Set dependency_file to enable <object>.d file generation. if (dotdFile != null) { buildVariables.addVariable("dependency_file", dotdFile.getSafeExecPath().getPathString()); } if (featureConfiguration.isEnabled(CppRuleClasses.MODULE_MAPS) && cppModuleMap != null) { // If the feature is enabled and cppModuleMap is null, we are about to fail during analysis // in any case, but don't crash. buildVariables.addVariable("module_name", cppModuleMap.getName()); buildVariables.addVariable("module_map_file", cppModuleMap.getArtifact().getExecPathString()); CcToolchainFeatures.Variables.ValueSequence.Builder sequence = new CcToolchainFeatures.Variables.ValueSequence.Builder(); for (Artifact artifact : builderContext.getDirectModuleMaps()) { sequence.addValue(artifact.getExecPathString()); } buildVariables.addSequence("dependent_module_map_files", sequence.build()); } if (featureConfiguration.isEnabled(CppRuleClasses.USE_HEADER_MODULES)) { buildVariables.addSequenceVariable("module_files", getHeaderModulePaths(builder, usePic)); } if (featureConfiguration.isEnabled(CppRuleClasses.INCLUDE_PATHS)) { buildVariables.addSequenceVariable( "include_paths", getSafePathStrings(builderContext.getIncludeDirs())); buildVariables.addSequenceVariable( "quote_include_paths", getSafePathStrings(builderContext.getQuoteIncludeDirs())); buildVariables.addSequenceVariable( "system_include_paths", getSafePathStrings(builderContext.getSystemIncludeDirs())); } if (featureConfiguration.isEnabled(CppRuleClasses.PREPROCESSOR_DEFINES)) { String fdoBuildStamp = CppHelper.getFdoBuildStamp(ruleContext); ImmutableList<String> defines; if (fdoBuildStamp != null) { // Stamp FDO builds with FDO subtype string defines = ImmutableList.<String>builder() .addAll(builderContext.getDefines()) .add( CppConfiguration.FDO_STAMP_MACRO + "=\"" + CppHelper.getFdoBuildStamp(ruleContext) + "\"") .build(); } else { defines = builderContext.getDefines(); } buildVariables.addSequenceVariable("preprocessor_defines", defines); } if (usePic) { if (!featureConfiguration.isEnabled(CppRuleClasses.PIC)) { ruleContext.ruleError("PIC compilation is requested but the toolchain does not support it"); } buildVariables.addVariable("pic", ""); } if (ccRelativeName != null) { CppHelper.getFdoSupport(ruleContext) .configureCompilation( builder, buildVariables, ruleContext, ccRelativeName, autoFdoImportPath, usePic, featureConfiguration); } if (gcnoFile != null) { buildVariables.addVariable("gcov_gcno_file", gcnoFile.getExecPathString()); } if (dwoFile != null) { buildVariables.addVariable("per_object_debug_info_file", dwoFile.getExecPathString()); } buildVariables.addAllVariables(CppHelper.getToolchain(ruleContext).getBuildVariables()); buildVariables.addAllVariables(sourceSpecificBuildVariables); for (VariablesExtension extension : variablesExtensions) { extension.addVariables(buildVariables); } CcToolchainFeatures.Variables variables = buildVariables.build(); builder.setVariables(variables); }