Search in sources :

Example 1 with Builder

use of com.google.devtools.build.lib.rules.cpp.CcCompilationOutputs.Builder in project bazel by bazelbuild.

the class CppModel method createSourceAction.

private Collection<Artifact> createSourceAction(String outputName, CcCompilationOutputs.Builder result, AnalysisEnvironment env, Artifact sourceArtifact, CppCompileActionBuilder builder, ArtifactCategory outputCategory, CppModuleMap cppModuleMap, boolean addObject, boolean enableCoverage, boolean generateDwo, boolean generateDotd, Map<String, String> sourceSpecificBuildVariables) {
    ImmutableList.Builder<Artifact> directOutputs = new ImmutableList.Builder<>();
    PathFragment ccRelativeName = semantics.getEffectiveSourcePath(sourceArtifact);
    if (cppConfiguration.isLipoOptimization()) {
        // TODO(bazel-team): we shouldn't be needing this, merging context with the binary
        // is a superset of necessary information.
        LipoContextProvider lipoProvider = Preconditions.checkNotNull(CppHelper.getLipoContextProvider(ruleContext), outputName);
        builder.setContext(CppCompilationContext.mergeForLipo(lipoProvider.getLipoContext(), context));
    }
    boolean generatePicAction = getGeneratePicActions();
    boolean generateNoPicAction = getGenerateNoPicActions();
    Preconditions.checkState(generatePicAction || generateNoPicAction);
    if (fake) {
        boolean usePic = !generateNoPicAction;
        createFakeSourceAction(outputName, result, env, builder, outputCategory, addObject, ccRelativeName, sourceArtifact.getExecPath(), usePic, generateDotd);
    } else {
        // generate .pic.o, .pic.d, .pic.gcno instead of .o, .d, .gcno.)
        if (generatePicAction) {
            String picOutputBase = CppHelper.getArtifactNameForCategory(ruleContext, ccToolchain, ArtifactCategory.PIC_FILE, outputName);
            CppCompileActionBuilder picBuilder = copyAsPicBuilder(builder, picOutputBase, outputCategory, generateDotd);
            String gcnoFileName = CppHelper.getArtifactNameForCategory(ruleContext, ccToolchain, ArtifactCategory.COVERAGE_DATA_FILE, picOutputBase);
            Artifact gcnoFile = enableCoverage ? CppHelper.getCompileOutputArtifact(ruleContext, gcnoFileName, configuration) : null;
            Artifact dwoFile = generateDwo ? getDwoFile(picBuilder.getOutputFile()) : null;
            setupCompileBuildVariables(picBuilder, /*usePic=*/
            true, ccRelativeName, sourceArtifact.getExecPath(), gcnoFile, dwoFile, cppModuleMap, sourceSpecificBuildVariables);
            if (maySaveTemps) {
                result.addTemps(createTempsActions(sourceArtifact, outputName, picBuilder, /*usePic=*/
                true, /*generateDotd=*/
                generateDotd, ccRelativeName));
            }
            picBuilder.setGcnoFile(gcnoFile);
            picBuilder.setDwoFile(dwoFile);
            semantics.finalizeCompileActionBuilder(ruleContext, picBuilder);
            CppCompileAction picAction = picBuilder.buildAndValidate(ruleContext);
            env.registerAction(picAction);
            directOutputs.add(picAction.getOutputFile());
            if (addObject) {
                result.addPicObjectFile(picAction.getOutputFile());
                if (featureConfiguration.isEnabled(CppRuleClasses.THIN_LTO) && CppFileTypes.LTO_SOURCE.matches(sourceArtifact.getFilename())) {
                    result.addLTOBitcodeFile(picAction.getOutputFile());
                }
            }
            if (dwoFile != null) {
                // Host targets don't produce .dwo files.
                result.addPicDwoFile(dwoFile);
            }
            if (cppConfiguration.isLipoContextCollector() && !generateNoPicAction) {
                result.addLipoScannable(picAction);
            }
        }
        if (generateNoPicAction) {
            Artifact noPicOutputFile = CppHelper.getCompileOutputArtifact(ruleContext, CppHelper.getArtifactNameForCategory(ruleContext, ccToolchain, outputCategory, outputName), configuration);
            builder.setOutputs(ruleContext, outputCategory, outputName, generateDotd);
            String gcnoFileName = CppHelper.getArtifactNameForCategory(ruleContext, ccToolchain, ArtifactCategory.COVERAGE_DATA_FILE, outputName);
            // Create non-PIC compile actions
            Artifact gcnoFile = !cppConfiguration.isLipoOptimization() && enableCoverage ? CppHelper.getCompileOutputArtifact(ruleContext, gcnoFileName, configuration) : null;
            Artifact noPicDwoFile = generateDwo ? getDwoFile(noPicOutputFile) : null;
            setupCompileBuildVariables(builder, /*usePic=*/
            false, ccRelativeName, sourceArtifact.getExecPath(), gcnoFile, noPicDwoFile, cppModuleMap, sourceSpecificBuildVariables);
            if (maySaveTemps) {
                result.addTemps(createTempsActions(sourceArtifact, outputName, builder, /*usePic=*/
                false, /*generateDotd*/
                generateDotd, ccRelativeName));
            }
            builder.setGcnoFile(gcnoFile);
            builder.setDwoFile(noPicDwoFile);
            semantics.finalizeCompileActionBuilder(ruleContext, builder);
            CppCompileAction compileAction = builder.buildAndValidate(ruleContext);
            env.registerAction(compileAction);
            Artifact objectFile = compileAction.getOutputFile();
            directOutputs.add(objectFile);
            if (addObject) {
                result.addObjectFile(objectFile);
                if (featureConfiguration.isEnabled(CppRuleClasses.THIN_LTO) && CppFileTypes.LTO_SOURCE.matches(sourceArtifact.getFilename())) {
                    result.addLTOBitcodeFile(objectFile);
                }
            }
            if (noPicDwoFile != null) {
                // Host targets don't produce .dwo files.
                result.addDwoFile(noPicDwoFile);
            }
            if (cppConfiguration.isLipoContextCollector()) {
                result.addLipoScannable(compileAction);
            }
        }
    }
    return directOutputs.build();
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) Builder(com.google.devtools.build.lib.rules.cpp.CcCompilationOutputs.Builder) StringSequenceBuilder(com.google.devtools.build.lib.rules.cpp.CcToolchainFeatures.Variables.StringSequenceBuilder) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) Artifact(com.google.devtools.build.lib.actions.Artifact)

Example 2 with Builder

use of com.google.devtools.build.lib.rules.cpp.CcCompilationOutputs.Builder in project bazel by bazelbuild.

the class CppModel method createCcCompileActions.

/**
   * 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()) {
        Collection<Artifact> modules = createModuleAction(result, context.getCppModuleMap());
        if (featureConfiguration.isEnabled(CppRuleClasses.HEADER_MODULE_CODEGEN)) {
            for (Artifact module : modules) {
                createModuleCodegenAction(result, module);
            }
        }
    } else if (context.getVerificationModuleMap() != null) {
        Collection<Artifact> modules = createModuleAction(result, context.getVerificationModuleMap());
        for (Artifact module : modules) {
            result.addHeaderTokenFile(module);
        }
    }
    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);
        builder.setSemantics(semantics);
        if (!sourceArtifact.isTreeArtifact()) {
            switch(source.getType()) {
                case HEADER:
                    createHeaderAction(outputName, result, env, builder, CppFileTypes.mustProduceDotdFile(sourceArtifact));
                    break;
                case CLIF_INPUT_PROTO:
                    createClifMatchAction(outputName, result, env, builder);
                    break;
                default:
                    boolean bitcodeOutput = featureConfiguration.isEnabled(CppRuleClasses.THIN_LTO) && CppFileTypes.LTO_SOURCE.matches(sourceArtifact.getFilename());
                    createSourceAction(outputName, result, env, sourceArtifact, builder, ArtifactCategory.OBJECT_FILE, context.getCppModuleMap(), /*addObject=*/
                    true, isCodeCoverageEnabled(), /*generateDwo=*/
                    cppConfiguration.useFission() && !bitcodeOutput, CppFileTypes.mustProduceDotdFile(sourceArtifact), source.getBuildVariables());
                    break;
            }
        } else {
            switch(source.getType()) {
                case HEADER:
                    Artifact headerTokenFile = createCompileActionTemplate(env, source, builder, ImmutableList.of(ArtifactCategory.GENERATED_HEADER, ArtifactCategory.PROCESSED_HEADER));
                    result.addHeaderTokenFile(headerTokenFile);
                    break;
                case SOURCE:
                    Artifact objectFile = createCompileActionTemplate(env, source, builder, ImmutableList.of(ArtifactCategory.OBJECT_FILE));
                    result.addObjectFile(objectFile);
                    break;
                default:
                    throw new IllegalStateException("Encountered invalid source types when creating CppCompileActionTemplates");
            }
        }
    }
    compilationOutputs = result.build();
    return compilationOutputs;
}
Also used : Builder(com.google.devtools.build.lib.rules.cpp.CcCompilationOutputs.Builder) StringSequenceBuilder(com.google.devtools.build.lib.rules.cpp.CcToolchainFeatures.Variables.StringSequenceBuilder) Label(com.google.devtools.build.lib.cmdline.Label) Builder(com.google.devtools.build.lib.rules.cpp.CcCompilationOutputs.Builder) Artifact(com.google.devtools.build.lib.actions.Artifact) AnalysisEnvironment(com.google.devtools.build.lib.analysis.AnalysisEnvironment) TransitiveInfoCollection(com.google.devtools.build.lib.analysis.TransitiveInfoCollection) Collection(java.util.Collection)

Example 3 with Builder

use of com.google.devtools.build.lib.rules.cpp.CcCompilationOutputs.Builder in project bazel by bazelbuild.

the class CppModel method setupCompileBuildVariables.

private void setupCompileBuildVariables(CppCompileActionBuilder builder, boolean usePic, PathFragment ccRelativeName, PathFragment autoFdoImportPath, Artifact gcnoFile, Artifact dwoFile, CppModuleMap cppModuleMap, 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();
    Artifact sourceFile = builder.getSourceFile();
    Artifact outputFile = builder.getOutputFile();
    String realOutputFilePath;
    buildVariables.addStringVariable("source_file", sourceFile.getExecPathString());
    buildVariables.addStringVariable("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.addStringVariable("output_assembly_file", realOutputFilePath);
    } else if (FileType.contains(outputFile, CppFileTypes.PREPROCESSED_C, CppFileTypes.PREPROCESSED_CPP, CppFileTypes.PIC_PREPROCESSED_C, CppFileTypes.PIC_PREPROCESSED_CPP)) {
        buildVariables.addStringVariable("output_preprocess_file", realOutputFilePath);
    } else {
        buildVariables.addStringVariable("output_object_file", realOutputFilePath);
    }
    DotdFile dotdFile = CppFileTypes.mustProduceDotdFile(sourceFile) ? Preconditions.checkNotNull(builder.getDotdFile()) : null;
    // Set dependency_file to enable <object>.d file generation.
    if (dotdFile != null) {
        buildVariables.addStringVariable("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.addStringVariable("module_name", cppModuleMap.getName());
        buildVariables.addStringVariable("module_map_file", cppModuleMap.getArtifact().getExecPathString());
        StringSequenceBuilder sequence = new StringSequenceBuilder();
        for (Artifact artifact : builderContext.getDirectModuleMaps()) {
            sequence.addValue(artifact.getExecPathString());
        }
        buildVariables.addCustomBuiltVariable("dependent_module_map_files", sequence);
    }
    if (featureConfiguration.isEnabled(CppRuleClasses.USE_HEADER_MODULES)) {
        // Module inputs will be set later when the action is executed.
        buildVariables.addStringSequenceVariable("module_files", ImmutableSet.<String>of());
    }
    if (featureConfiguration.isEnabled(CppRuleClasses.INCLUDE_PATHS)) {
        buildVariables.addStringSequenceVariable("include_paths", getSafePathStrings(builderContext.getIncludeDirs()));
        buildVariables.addStringSequenceVariable("quote_include_paths", getSafePathStrings(builderContext.getQuoteIncludeDirs()));
        buildVariables.addStringSequenceVariable("system_include_paths", getSafePathStrings(builderContext.getSystemIncludeDirs()));
    }
    if (featureConfiguration.isEnabled(CppRuleClasses.PREPROCESSOR_DEFINES)) {
        String fdoBuildStamp = CppHelper.getFdoBuildStamp(ruleContext, fdoSupport.getFdoSupport());
        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, fdoSupport.getFdoSupport()) + "\"").build();
        } else {
            defines = builderContext.getDefines();
        }
        buildVariables.addStringSequenceVariable("preprocessor_defines", defines);
    }
    if (usePic) {
        if (!featureConfiguration.isEnabled(CppRuleClasses.PIC)) {
            ruleContext.ruleError("PIC compilation is requested but the toolchain does not support it");
        }
        buildVariables.addStringVariable("pic", "");
    }
    if (ccRelativeName != null) {
        fdoSupport.getFdoSupport().configureCompilation(builder, buildVariables, ruleContext, ccRelativeName, autoFdoImportPath, usePic, featureConfiguration, fdoSupport);
    }
    if (gcnoFile != null) {
        buildVariables.addStringVariable("gcov_gcno_file", gcnoFile.getExecPathString());
    }
    if (dwoFile != null) {
        buildVariables.addStringVariable("per_object_debug_info_file", dwoFile.getExecPathString());
    }
    buildVariables.addAllStringVariables(ccToolchain.getBuildVariables());
    buildVariables.addAllStringVariables(sourceSpecificBuildVariables);
    for (VariablesExtension extension : variablesExtensions) {
        extension.addVariables(buildVariables);
    }
    CcToolchainFeatures.Variables variables = buildVariables.build();
    builder.setVariables(variables);
}
Also used : StringSequenceBuilder(com.google.devtools.build.lib.rules.cpp.CcToolchainFeatures.Variables.StringSequenceBuilder) VariablesExtension(com.google.devtools.build.lib.rules.cpp.CcToolchainFeatures.Variables.VariablesExtension) Builder(com.google.devtools.build.lib.rules.cpp.CcCompilationOutputs.Builder) StringSequenceBuilder(com.google.devtools.build.lib.rules.cpp.CcToolchainFeatures.Variables.StringSequenceBuilder) Artifact(com.google.devtools.build.lib.actions.Artifact) DotdFile(com.google.devtools.build.lib.rules.cpp.CppCompileAction.DotdFile)

Aggregations

Artifact (com.google.devtools.build.lib.actions.Artifact)3 Builder (com.google.devtools.build.lib.rules.cpp.CcCompilationOutputs.Builder)3 StringSequenceBuilder (com.google.devtools.build.lib.rules.cpp.CcToolchainFeatures.Variables.StringSequenceBuilder)3 ImmutableList (com.google.common.collect.ImmutableList)1 AnalysisEnvironment (com.google.devtools.build.lib.analysis.AnalysisEnvironment)1 TransitiveInfoCollection (com.google.devtools.build.lib.analysis.TransitiveInfoCollection)1 Label (com.google.devtools.build.lib.cmdline.Label)1 VariablesExtension (com.google.devtools.build.lib.rules.cpp.CcToolchainFeatures.Variables.VariablesExtension)1 DotdFile (com.google.devtools.build.lib.rules.cpp.CppCompileAction.DotdFile)1 PathFragment (com.google.devtools.build.lib.vfs.PathFragment)1 Collection (java.util.Collection)1