Search in sources :

Example 16 with Runfiles

use of com.google.devtools.build.lib.analysis.Runfiles in project bazel by bazelbuild.

the class SpawnInputExpanderTest method testRunfilesDirectoryStrict.

@Test
public void testRunfilesDirectoryStrict() throws Exception {
    Artifact artifact = new Artifact(fs.getPath("/root/dir/file"), Root.asSourceRoot(fs.getPath("/root")));
    Runfiles runfiles = new Runfiles.Builder("workspace").addArtifact(artifact).build();
    RunfilesSupplier supplier = new RunfilesSupplierImpl(new PathFragment("runfiles"), runfiles);
    ActionInputFileCache mockCache = Mockito.mock(ActionInputFileCache.class);
    Mockito.when(mockCache.isFile(artifact)).thenReturn(false);
    try {
        expander.addRunfilesToInputs(inputMappings, supplier, mockCache);
        fail();
    } catch (IOException expected) {
        assertThat(expected.getMessage().contains("Not a file: /root/dir/file")).isTrue();
    }
}
Also used : Runfiles(com.google.devtools.build.lib.analysis.Runfiles) ActionInputFileCache(com.google.devtools.build.lib.actions.ActionInputFileCache) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) IOException(java.io.IOException) RunfilesSupplierImpl(com.google.devtools.build.lib.analysis.RunfilesSupplierImpl) Artifact(com.google.devtools.build.lib.actions.Artifact) EmptyRunfilesSupplier(com.google.devtools.build.lib.actions.EmptyRunfilesSupplier) RunfilesSupplier(com.google.devtools.build.lib.actions.RunfilesSupplier) Test(org.junit.Test)

Example 17 with Runfiles

use of com.google.devtools.build.lib.analysis.Runfiles in project bazel by bazelbuild.

the class SpawnInputExpanderTest method testRunfilesTwoFiles.

@Test
public void testRunfilesTwoFiles() throws Exception {
    Artifact artifact1 = new Artifact(fs.getPath("/root/dir/file"), Root.asSourceRoot(fs.getPath("/root")));
    Artifact artifact2 = new Artifact(fs.getPath("/root/dir/baz"), Root.asSourceRoot(fs.getPath("/root")));
    Runfiles runfiles = new Runfiles.Builder("workspace").addArtifact(artifact1).addArtifact(artifact2).build();
    RunfilesSupplier supplier = new RunfilesSupplierImpl(new PathFragment("runfiles"), runfiles);
    ActionInputFileCache mockCache = Mockito.mock(ActionInputFileCache.class);
    Mockito.when(mockCache.isFile(artifact1)).thenReturn(true);
    Mockito.when(mockCache.isFile(artifact2)).thenReturn(true);
    expander.addRunfilesToInputs(inputMappings, supplier, mockCache);
    assertThat(inputMappings).hasSize(2);
    assertThat(inputMappings).containsEntry(new PathFragment("runfiles/workspace/dir/file"), artifact1);
    assertThat(inputMappings).containsEntry(new PathFragment("runfiles/workspace/dir/baz"), artifact2);
}
Also used : Runfiles(com.google.devtools.build.lib.analysis.Runfiles) ActionInputFileCache(com.google.devtools.build.lib.actions.ActionInputFileCache) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) RunfilesSupplierImpl(com.google.devtools.build.lib.analysis.RunfilesSupplierImpl) Artifact(com.google.devtools.build.lib.actions.Artifact) EmptyRunfilesSupplier(com.google.devtools.build.lib.actions.EmptyRunfilesSupplier) RunfilesSupplier(com.google.devtools.build.lib.actions.RunfilesSupplier) Test(org.junit.Test)

Example 18 with Runfiles

use of com.google.devtools.build.lib.analysis.Runfiles in project bazel by bazelbuild.

the class SpawnInputExpanderTest method testRunfilesSingleFile.

@Test
public void testRunfilesSingleFile() throws Exception {
    Artifact artifact = new Artifact(fs.getPath("/root/dir/file"), Root.asSourceRoot(fs.getPath("/root")));
    Runfiles runfiles = new Runfiles.Builder("workspace").addArtifact(artifact).build();
    RunfilesSupplier supplier = new RunfilesSupplierImpl(new PathFragment("runfiles"), runfiles);
    ActionInputFileCache mockCache = Mockito.mock(ActionInputFileCache.class);
    Mockito.when(mockCache.isFile(artifact)).thenReturn(true);
    expander.addRunfilesToInputs(inputMappings, supplier, mockCache);
    assertThat(inputMappings).hasSize(1);
    assertThat(inputMappings).containsEntry(new PathFragment("runfiles/workspace/dir/file"), artifact);
}
Also used : Runfiles(com.google.devtools.build.lib.analysis.Runfiles) ActionInputFileCache(com.google.devtools.build.lib.actions.ActionInputFileCache) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) RunfilesSupplierImpl(com.google.devtools.build.lib.analysis.RunfilesSupplierImpl) Artifact(com.google.devtools.build.lib.actions.Artifact) EmptyRunfilesSupplier(com.google.devtools.build.lib.actions.EmptyRunfilesSupplier) RunfilesSupplier(com.google.devtools.build.lib.actions.RunfilesSupplier) Test(org.junit.Test)

Example 19 with Runfiles

use of com.google.devtools.build.lib.analysis.Runfiles in project bazel by bazelbuild.

the class SkylarkRuleConfiguredTargetBuilder method addSimpleProviders.

private static void addSimpleProviders(RuleConfiguredTargetBuilder builder, RuleContext ruleContext, Location loc, Artifact executable, Runfiles statelessRunfiles, Runfiles dataRunfiles, Runfiles defaultRunfiles, SkylarkClassObject defaultProvider) throws EvalException {
    if ((statelessRunfiles != null) && (dataRunfiles != null || defaultRunfiles != null)) {
        throw new EvalException(loc, "Cannot specify the provider 'runfiles' " + "together with 'data_runfiles' or 'default_runfiles'");
    }
    if (statelessRunfiles == null && dataRunfiles == null && defaultRunfiles == null) {
        // No runfiles specified, set default
        statelessRunfiles = Runfiles.EMPTY;
    }
    RunfilesProvider runfilesProvider = statelessRunfiles != null ? RunfilesProvider.simple(merge(statelessRunfiles, executable, ruleContext)) : RunfilesProvider.withData(// This is to keep skylark genrule consistent with the original genrule.
    defaultRunfiles != null ? defaultRunfiles : Runfiles.EMPTY, dataRunfiles != null ? dataRunfiles : Runfiles.EMPTY);
    builder.addProvider(RunfilesProvider.class, runfilesProvider);
    Runfiles computedDefaultRunfiles = runfilesProvider.getDefaultRunfiles();
    // This works because we only allowed to call a rule *_test iff it's a test type rule.
    boolean testRule = TargetUtils.isTestRuleName(ruleContext.getRule().getRuleClass());
    if (testRule && computedDefaultRunfiles.isEmpty()) {
        throw new EvalException(loc, "Test rules have to define runfiles");
    }
    if (executable != null || testRule) {
        RunfilesSupport runfilesSupport = computedDefaultRunfiles.isEmpty() ? null : RunfilesSupport.withExecutable(ruleContext, computedDefaultRunfiles, executable);
        builder.setRunfilesSupport(runfilesSupport, executable);
    }
    if (ruleContext.getRule().getRuleClassObject().isSkylarkTestable()) {
        SkylarkClassObject actions = ActionsProvider.create(ruleContext.getAnalysisEnvironment().getRegisteredActions());
        builder.addSkylarkDeclaredProvider(actions, loc);
    }
    // Populate default provider fields and build it
    ImmutableMap.Builder<String, Object> attrBuilder = new ImmutableMap.Builder<>();
    // TODO: Add actual attributes that users expect to access from default providers
    attrBuilder.put("runfiles", runfilesProvider);
    SkylarkClassObject statelessDefaultProvider = new SkylarkClassObject(SkylarkRuleContext.getDefaultProvider(), attrBuilder.build());
    // Add the default provider
    builder.addSkylarkDeclaredProvider(statelessDefaultProvider, (defaultProvider == null) ? loc : Optional.fromNullable(defaultProvider.getCreationLocOrNull()).or(loc));
}
Also used : Runfiles(com.google.devtools.build.lib.analysis.Runfiles) RunfilesSupport(com.google.devtools.build.lib.analysis.RunfilesSupport) SkylarkClassObject(com.google.devtools.build.lib.packages.SkylarkClassObject) NestedSetBuilder(com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder) RuleConfiguredTargetBuilder(com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder) SkylarkClassObject(com.google.devtools.build.lib.packages.SkylarkClassObject) ClassObject(com.google.devtools.build.lib.syntax.ClassObject) EvalException(com.google.devtools.build.lib.syntax.EvalException) RunfilesProvider(com.google.devtools.build.lib.analysis.RunfilesProvider) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 20 with Runfiles

use of com.google.devtools.build.lib.analysis.Runfiles in project bazel by bazelbuild.

the class CcLibraryHelper method build.

/**
   * Create the C++ compile and link actions, and the corresponding C++-related providers.
   *
   * @throws RuleErrorException
   */
public Info build() throws RuleErrorException, InterruptedException {
    // Fail early if there is no lipo context collector on the rule - otherwise we end up failing
    // in lipo optimization.
    Preconditions.checkState(// 'cc_inc_library' rules do not compile, and thus are not affected by LIPO.
    ruleContext.getRule().getRuleClass().equals("cc_inc_library") || ruleContext.isAttrDefined(":lipo_context_collector", BuildType.LABEL));
    if (checkDepsGenerateCpp) {
        for (LanguageDependentFragment dep : AnalysisUtils.getProviders(deps, LanguageDependentFragment.class)) {
            LanguageDependentFragment.Checker.depSupportsLanguage(ruleContext, dep, CppRuleClasses.LANGUAGE);
        }
    }
    CppModel model = initializeCppModel();
    CppCompilationContext cppCompilationContext = initializeCppCompilationContext(model);
    model.setContext(cppCompilationContext);
    boolean compileHeaderModules = featureConfiguration.isEnabled(CppRuleClasses.HEADER_MODULES);
    Preconditions.checkState(!compileHeaderModules || cppCompilationContext.getCppModuleMap() != null, "All cc rules must support module maps.");
    // Create compile actions (both PIC and non-PIC).
    CcCompilationOutputs ccOutputs = model.createCcCompileActions();
    if (!objectFiles.isEmpty() || !picObjectFiles.isEmpty()) {
        // Merge the pre-compiled object files into the compiler outputs.
        ccOutputs = new CcCompilationOutputs.Builder().merge(ccOutputs).addLTOBitcodeFile(ccOutputs.getLtoBitcodeFiles()).addObjectFiles(objectFiles).addPicObjectFiles(picObjectFiles).build();
    }
    // Create link actions (only if there are object files or if explicitly requested).
    CcLinkingOutputs ccLinkingOutputs = CcLinkingOutputs.EMPTY;
    if (emitLinkActions && (emitLinkActionsIfEmpty || !ccOutputs.isEmpty())) {
        // generate any link actions, effectively disabling header checking in some cases.
        if (linkType.staticness() == Staticness.STATIC) {
            // TODO(bazel-team): This can't create the link action for a cc_binary yet.
            ccLinkingOutputs = model.createCcLinkActions(ccOutputs, nonCodeLinkerInputs);
        }
    }
    CcLinkingOutputs originalLinkingOutputs = ccLinkingOutputs;
    if (!(staticLibraries.isEmpty() && picStaticLibraries.isEmpty() && dynamicLibraries.isEmpty())) {
        CcLinkingOutputs.Builder newOutputsBuilder = new CcLinkingOutputs.Builder();
        if (!ccOutputs.isEmpty()) {
            // Add the linked outputs of this rule iff we had anything to put in them, but then
            // make sure we're not colliding with some library added from the inputs.
            newOutputsBuilder.merge(originalLinkingOutputs);
            ImmutableSetMultimap<String, LibraryToLink> precompiledLibraryMap = CcLinkingOutputs.getLibrariesByIdentifier(Iterables.concat(staticLibraries, picStaticLibraries, dynamicLibraries));
            ImmutableSetMultimap<String, LibraryToLink> linkedLibraryMap = originalLinkingOutputs.getLibrariesByIdentifier();
            for (String matchingIdentifier : Sets.intersection(precompiledLibraryMap.keySet(), linkedLibraryMap.keySet())) {
                Iterable<Artifact> matchingInputLibs = LinkerInputs.toNonSolibArtifacts(precompiledLibraryMap.get(matchingIdentifier));
                Iterable<Artifact> matchingOutputLibs = LinkerInputs.toNonSolibArtifacts(linkedLibraryMap.get(matchingIdentifier));
                ruleContext.ruleError("Can't put " + Joiner.on(", ").join(Iterables.transform(matchingInputLibs, FileType.TO_FILENAME)) + " into the srcs of a " + ruleContext.getRuleClassNameForLogging() + " with the same name (" + ruleContext.getRule().getName() + ") which also contains other code or objects to link; it shares a name with " + Joiner.on(", ").join(Iterables.transform(matchingOutputLibs, FileType.TO_FILENAME)) + " (output compiled and linked from the non-library sources of this rule), " + "which could cause confusion");
            }
        }
        // Merge the pre-compiled libraries (static & dynamic) into the linker outputs.
        ccLinkingOutputs = newOutputsBuilder.addStaticLibraries(staticLibraries).addPicStaticLibraries(picStaticLibraries).addDynamicLibraries(dynamicLibraries).addExecutionDynamicLibraries(dynamicLibraries).build();
    }
    DwoArtifactsCollector dwoArtifacts = DwoArtifactsCollector.transitiveCollector(ruleContext, ccOutputs, deps, /*generateDwo=*/
    false, /*ltoBackendArtifactsUsePic=*/
    false, /*ltoBackendArtifacts=*/
    ImmutableList.<LTOBackendArtifacts>of());
    Runfiles cppStaticRunfiles = collectCppRunfiles(ccLinkingOutputs, true);
    Runfiles cppSharedRunfiles = collectCppRunfiles(ccLinkingOutputs, false);
    // By very careful when adding new providers here - it can potentially affect a lot of rules.
    // We should consider merging most of these providers into a single provider.
    TransitiveInfoProviderMap.Builder providers = TransitiveInfoProviderMap.builder().add(new CppRunfilesProvider(cppStaticRunfiles, cppSharedRunfiles), cppCompilationContext, new CppDebugFileProvider(dwoArtifacts.getDwoArtifacts(), dwoArtifacts.getPicDwoArtifacts()), collectTransitiveLipoInfo(ccOutputs));
    Map<String, NestedSet<Artifact>> outputGroups = new TreeMap<>();
    if (shouldAddLinkerOutputArtifacts(ruleContext, ccOutputs)) {
        addLinkerOutputArtifacts(outputGroups, ccOutputs);
    }
    outputGroups.put(OutputGroupProvider.TEMP_FILES, getTemps(ccOutputs));
    CppConfiguration cppConfiguration = ruleContext.getFragment(CppConfiguration.class);
    if (emitCompileProviders) {
        boolean isLipoCollector = cppConfiguration.isLipoContextCollector();
        boolean processHeadersInDependencies = cppConfiguration.processHeadersInDependencies();
        boolean usePic = CppHelper.usePic(ruleContext, false);
        outputGroups.put(OutputGroupProvider.FILES_TO_COMPILE, ccOutputs.getFilesToCompile(isLipoCollector, processHeadersInDependencies, usePic));
        outputGroups.put(OutputGroupProvider.COMPILATION_PREREQUISITES, CcCommon.collectCompilationPrerequisites(ruleContext, cppCompilationContext));
    }
    // used.
    if (emitCcNativeLibrariesProvider) {
        providers.add(new CcNativeLibraryProvider(collectNativeCcLibraries(ccLinkingOutputs)));
    }
    providers.put(CcExecutionDynamicLibrariesProvider.class, collectExecutionDynamicLibraryArtifacts(ccLinkingOutputs.getExecutionDynamicLibraries()));
    boolean forcePic = cppConfiguration.forcePic();
    if (emitCcSpecificLinkParamsProvider) {
        providers.add(new CcSpecificLinkParamsProvider(createCcLinkParamsStore(ccLinkingOutputs, cppCompilationContext, forcePic)));
    } else {
        providers.add(new CcLinkParamsProvider(createCcLinkParamsStore(ccLinkingOutputs, cppCompilationContext, forcePic)));
    }
    return new Info(providers.build(), outputGroups, ccOutputs, ccLinkingOutputs, originalLinkingOutputs, cppCompilationContext);
}
Also used : TransitiveInfoProviderMap(com.google.devtools.build.lib.analysis.TransitiveInfoProviderMap) NestedSet(com.google.devtools.build.lib.collect.nestedset.NestedSet) NestedSetBuilder(com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder) LanguageDependentFragment(com.google.devtools.build.lib.analysis.LanguageDependentFragment) TreeMap(java.util.TreeMap) Artifact(com.google.devtools.build.lib.actions.Artifact) LibraryToLink(com.google.devtools.build.lib.rules.cpp.LinkerInputs.LibraryToLink) Runfiles(com.google.devtools.build.lib.analysis.Runfiles)

Aggregations

Runfiles (com.google.devtools.build.lib.analysis.Runfiles)28 Artifact (com.google.devtools.build.lib.actions.Artifact)24 RuleConfiguredTargetBuilder (com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder)15 PathFragment (com.google.devtools.build.lib.vfs.PathFragment)11 NestedSetBuilder (com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder)10 RunfilesProvider (com.google.devtools.build.lib.analysis.RunfilesProvider)9 RunfilesSupplierImpl (com.google.devtools.build.lib.analysis.RunfilesSupplierImpl)7 TransitiveInfoCollection (com.google.devtools.build.lib.analysis.TransitiveInfoCollection)7 Test (org.junit.Test)7 ActionInputFileCache (com.google.devtools.build.lib.actions.ActionInputFileCache)6 EmptyRunfilesSupplier (com.google.devtools.build.lib.actions.EmptyRunfilesSupplier)6 RunfilesSupplier (com.google.devtools.build.lib.actions.RunfilesSupplier)6 RunfilesSupport (com.google.devtools.build.lib.analysis.RunfilesSupport)6 ArrayList (java.util.ArrayList)5 ImmutableMap (com.google.common.collect.ImmutableMap)3 SkylarkClassObject (com.google.devtools.build.lib.packages.SkylarkClassObject)3 LibraryToLink (com.google.devtools.build.lib.rules.cpp.LinkerInputs.LibraryToLink)3 InstrumentedFilesProvider (com.google.devtools.build.lib.rules.test.InstrumentedFilesProvider)3 Label (com.google.devtools.build.lib.cmdline.Label)2 NestedSet (com.google.devtools.build.lib.collect.nestedset.NestedSet)2