Search in sources :

Example 6 with InstrumentedFilesProvider

use of com.google.devtools.build.lib.rules.test.InstrumentedFilesProvider in project bazel by bazelbuild.

the class Filegroup method create.

@Override
public ConfiguredTarget create(RuleContext ruleContext) throws RuleErrorException {
    String outputGroupName = ruleContext.attributes().get("output_group", Type.STRING);
    if (outputGroupName.endsWith(INTERNAL_SUFFIX)) {
        ruleContext.throwWithAttributeError("output_group", String.format(ILLEGAL_OUTPUT_GROUP_ERROR, outputGroupName));
    }
    NestedSet<Artifact> filesToBuild = outputGroupName.isEmpty() ? PrerequisiteArtifacts.nestedSet(ruleContext, "srcs", Mode.TARGET) : getArtifactsForOutputGroup(outputGroupName, ruleContext.getPrerequisites("srcs", Mode.TARGET));
    NestedSet<Artifact> middleman = CompilationHelper.getAggregatingMiddleman(ruleContext, Actions.escapeLabel(ruleContext.getLabel()), filesToBuild);
    InstrumentedFilesProvider instrumentedFilesProvider = InstrumentedFilesCollector.collect(ruleContext, // what do *we* know about whether this is a source file or not
    new InstrumentationSpec(FileTypeSet.ANY_FILE, "srcs", "deps", "data"), InstrumentedFilesCollector.NO_METADATA_COLLECTOR, filesToBuild);
    RunfilesProvider runfilesProvider = RunfilesProvider.withData(new Runfiles.Builder(ruleContext.getWorkspaceName(), ruleContext.getConfiguration().legacyExternalRunfiles()).addRunfiles(ruleContext, RunfilesProvider.DEFAULT_RUNFILES).build(), // If you're visiting a filegroup as data, then we also visit its data as data.
    new Runfiles.Builder(ruleContext.getWorkspaceName(), ruleContext.getConfiguration().legacyExternalRunfiles()).addTransitiveArtifacts(filesToBuild).addDataDeps(ruleContext).build());
    return new RuleConfiguredTargetBuilder(ruleContext).add(RunfilesProvider.class, runfilesProvider).setFilesToBuild(filesToBuild).setRunfilesSupport(null, getExecutable(filesToBuild)).add(InstrumentedFilesProvider.class, instrumentedFilesProvider).add(MiddlemanProvider.class, new MiddlemanProvider(middleman)).add(FilegroupPathProvider.class, new FilegroupPathProvider(getFilegroupPath(ruleContext))).build();
}
Also used : Runfiles(com.google.devtools.build.lib.analysis.Runfiles) InstrumentationSpec(com.google.devtools.build.lib.rules.test.InstrumentedFilesCollector.InstrumentationSpec) NestedSetBuilder(com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder) RuleConfiguredTargetBuilder(com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder) InstrumentedFilesProvider(com.google.devtools.build.lib.rules.test.InstrumentedFilesProvider) RuleConfiguredTargetBuilder(com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder) RunfilesProvider(com.google.devtools.build.lib.analysis.RunfilesProvider) MiddlemanProvider(com.google.devtools.build.lib.analysis.MiddlemanProvider) Artifact(com.google.devtools.build.lib.actions.Artifact)

Example 7 with InstrumentedFilesProvider

use of com.google.devtools.build.lib.rules.test.InstrumentedFilesProvider in project bazel by bazelbuild.

the class IosTest method create.

/**
   * {@inheritDoc}
   *
   * Creates a target, including registering actions, just as {@link #create(RuleContext)} does.
   * The difference between {@link #create(RuleContext)} and this method is that this method does
   * only what is needed to support tests on the environment besides generate the Xcodeproj file
   * and build the app and test {@code .ipa}s. The {@link #create(RuleContext)} method delegates
   * to this method.
   */
@Override
public final ConfiguredTarget create(RuleContext ruleContext) throws InterruptedException, RuleErrorException {
    ProtobufSupport protoSupport = new ProtobufSupport(ruleContext).registerGenerationActions().registerCompilationActions();
    Optional<ObjcProvider> protosObjcProvider = protoSupport.getObjcProvider();
    Optional<XcodeProvider> protosXcodeProvider = protoSupport.getXcodeProvider();
    ObjcCommon common = common(ruleContext, protosObjcProvider);
    if (!common.getCompilationArtifacts().get().getArchive().isPresent()) {
        ruleContext.ruleError(REQUIRES_SOURCE_ERROR);
    }
    if (!ruleContext.getFragment(AppleConfiguration.class).getIosMultiCpus().isEmpty()) {
        ruleContext.ruleError(NO_MULTI_CPUS_ERROR);
    }
    XcodeProvider.Builder xcodeProviderBuilder = new XcodeProvider.Builder().addPropagatedDependencies(protosXcodeProvider.asSet());
    NestedSetBuilder<Artifact> filesToBuild = NestedSetBuilder.stableOrder();
    addResourceFilesToBuild(ruleContext, common.getObjcProvider(), filesToBuild);
    XcodeProductType productType = getProductType(ruleContext);
    ExtraLinkArgs extraLinkArgs;
    Iterable<Artifact> extraLinkInputs;
    String bundleFormat;
    if (!isXcTest(ruleContext)) {
        extraLinkArgs = new ExtraLinkArgs();
        extraLinkInputs = ImmutableList.of();
        bundleFormat = ReleaseBundlingSupport.APP_BUNDLE_DIR_FORMAT;
    } else {
        xcodeProviderBuilder.setProductType(productType);
        XcodeProvider appIpaXcodeProvider = ruleContext.getPrerequisite(XCTEST_APP_ATTR, Mode.TARGET, XcodeProvider.class);
        if (appIpaXcodeProvider != null) {
            xcodeProviderBuilder.setTestHost(appIpaXcodeProvider);
        }
        XcTestAppProvider testApp = xcTestAppProvider(ruleContext);
        Artifact bundleLoader = testApp.getBundleLoader();
        // -bundle causes this binary to be linked as a bundle and not require an entry point
        // (i.e. main())
        // -bundle_loader causes the code in this test to have access to the symbols in the test rig,
        // or more specifically, the flag causes ld to consider the given binary when checking for
        // missing symbols.
        // -rpath @loader_path/Frameworks allows test bundles to load dylibs from the app's
        // Frameworks directory.
        extraLinkArgs = new ExtraLinkArgs("-bundle", "-bundle_loader", bundleLoader.getExecPathString(), "-Xlinker", "-rpath", "-Xlinker", "@loader_path/Frameworks");
        extraLinkInputs = ImmutableList.of(bundleLoader);
        bundleFormat = ReleaseBundlingSupport.XCTEST_BUNDLE_DIR_FORMAT;
        filesToBuild.add(testApp.getIpa());
    }
    J2ObjcMappingFileProvider j2ObjcMappingFileProvider = J2ObjcMappingFileProvider.union(ruleContext.getPrerequisites("deps", Mode.TARGET, J2ObjcMappingFileProvider.class));
    J2ObjcEntryClassProvider j2ObjcEntryClassProvider = new J2ObjcEntryClassProvider.Builder().addTransitive(ruleContext.getPrerequisites("deps", Mode.TARGET, J2ObjcEntryClassProvider.class)).build();
    CompilationSupport.create(ruleContext).registerLinkActions(common.getObjcProvider(), j2ObjcMappingFileProvider, j2ObjcEntryClassProvider, extraLinkArgs, extraLinkInputs, DsymOutputType.TEST).registerCompileAndArchiveActions(common).registerFullyLinkAction(common.getObjcProvider(), ruleContext.getImplicitOutputArtifact(CompilationSupport.FULLY_LINKED_LIB)).addXcodeSettings(xcodeProviderBuilder, common).validateAttributes();
    AppleConfiguration appleConfiguration = ruleContext.getFragment(AppleConfiguration.class);
    new ReleaseBundlingSupport(ruleContext, common.getObjcProvider(), LinkedBinary.LOCAL_AND_DEPENDENCIES, bundleFormat, appleConfiguration.getMinimumOsForPlatformType(PlatformType.IOS), appleConfiguration.getMultiArchPlatform(PlatformType.IOS)).registerActions(DsymOutputType.TEST).addXcodeSettings(xcodeProviderBuilder).addFilesToBuild(filesToBuild, Optional.of(DsymOutputType.TEST)).validateResources().validateAttributes();
    new ResourceSupport(ruleContext).validateAttributes().addXcodeSettings(xcodeProviderBuilder);
    new XcodeSupport(ruleContext).addXcodeSettings(xcodeProviderBuilder, common.getObjcProvider(), productType).addDependencies(xcodeProviderBuilder, new Attribute("bundles", Mode.TARGET)).addDependencies(xcodeProviderBuilder, new Attribute("deps", Mode.TARGET)).addNonPropagatedDependencies(xcodeProviderBuilder, new Attribute("non_propagated_deps", Mode.TARGET)).addFilesToBuild(filesToBuild).registerActions(xcodeProviderBuilder.build());
    XcodeProvider xcodeProvider = xcodeProviderBuilder.build();
    NestedSet<Artifact> filesToBuildSet = filesToBuild.build();
    Runfiles.Builder runfilesBuilder = new Runfiles.Builder(ruleContext.getWorkspaceName(), ruleContext.getConfiguration().legacyExternalRunfiles()).addRunfiles(ruleContext, RunfilesProvider.DEFAULT_RUNFILES);
    NestedSetBuilder<Artifact> filesToBuildBuilder = NestedSetBuilder.<Artifact>stableOrder().addTransitive(filesToBuildSet);
    InstrumentedFilesProvider instrumentedFilesProvider = CompilationSupport.create(ruleContext).getInstrumentedFilesProvider(common);
    TestSupport testSupport = new TestSupport(ruleContext).registerTestRunnerActions().addRunfiles(runfilesBuilder, instrumentedFilesProvider).addFilesToBuild(filesToBuildBuilder);
    Artifact executable = testSupport.generatedTestScript();
    Runfiles runfiles = runfilesBuilder.build();
    RunfilesSupport runfilesSupport = RunfilesSupport.withExecutable(ruleContext, runfiles, executable);
    ImmutableMap.Builder<String, String> execInfoMapBuilder = new ImmutableMap.Builder<>();
    execInfoMapBuilder.put(ExecutionRequirements.REQUIRES_DARWIN, "");
    if (ruleContext.getFragment(ObjcConfiguration.class).runMemleaks()) {
        execInfoMapBuilder.put("nosandbox", "");
    }
    return new RuleConfiguredTargetBuilder(ruleContext).setFilesToBuild(filesToBuildBuilder.build()).addProvider(xcodeProvider).addProvider(RunfilesProvider.simple(runfiles)).addNativeDeclaredProvider(new ExecutionInfoProvider(execInfoMapBuilder.build())).addNativeDeclaredProviders(testSupport.getExtraProviders()).addProvider(InstrumentedFilesProvider.class, instrumentedFilesProvider).setRunfilesSupport(runfilesSupport, executable).build();
}
Also used : NestedSetBuilder(com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder) RuleConfiguredTargetBuilder(com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder) AppleConfiguration(com.google.devtools.build.lib.rules.apple.AppleConfiguration) ExecutionInfoProvider(com.google.devtools.build.lib.rules.test.ExecutionInfoProvider) RunfilesSupport(com.google.devtools.build.lib.analysis.RunfilesSupport) ExtraLinkArgs(com.google.devtools.build.lib.rules.objc.CompilationSupport.ExtraLinkArgs) InstrumentedFilesProvider(com.google.devtools.build.lib.rules.test.InstrumentedFilesProvider) Artifact(com.google.devtools.build.lib.actions.Artifact) ImmutableMap(com.google.common.collect.ImmutableMap) Runfiles(com.google.devtools.build.lib.analysis.Runfiles) RuleConfiguredTargetBuilder(com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder)

Example 8 with InstrumentedFilesProvider

use of com.google.devtools.build.lib.rules.test.InstrumentedFilesProvider in project bazel by bazelbuild.

the class SkylarkIntegrationTest method testInstrumentedFilesProviderWithCodeCoverageDiabled.

@Test
public void testInstrumentedFilesProviderWithCodeCoverageDiabled() throws Exception {
    scratch.file("test/skylark/extension.bzl", "def custom_rule_impl(ctx):", "  return struct(instrumented_files=struct(", "      extensions = ['txt'],", "      source_attributes = ['attr1'],", "      dependency_attributes = ['attr2']))", "", "custom_rule = rule(implementation = custom_rule_impl,", "  attrs = {", "      'attr1': attr.label_list(mandatory = True, allow_files=True),", "      'attr2': attr.label_list(mandatory = True)})");
    scratch.file("test/skylark/BUILD", "load('/test/skylark/extension', 'custom_rule')", "", "java_library(name='jl', srcs = [':A.java'])", "custom_rule(name = 'cr', attr1 = [':a.txt', ':a.random'], attr2 = [':jl'])");
    useConfiguration("--nocollect_code_coverage");
    ConfiguredTarget target = getConfiguredTarget("//test/skylark:cr");
    assertEquals("//test/skylark:cr", target.getLabel().toString());
    InstrumentedFilesProvider provider = target.getProvider(InstrumentedFilesProvider.class);
    assertWithMessage("InstrumentedFilesProvider should be set.").that(provider).isNotNull();
    assertThat(ActionsTestUtil.baseArtifactNames(provider.getInstrumentedFiles())).isEmpty();
}
Also used : InstrumentedFilesProvider(com.google.devtools.build.lib.rules.test.InstrumentedFilesProvider) RuleConfiguredTarget(com.google.devtools.build.lib.analysis.RuleConfiguredTarget) ConfiguredTarget(com.google.devtools.build.lib.analysis.ConfiguredTarget) FileConfiguredTarget(com.google.devtools.build.lib.analysis.FileConfiguredTarget) Test(org.junit.Test)

Aggregations

InstrumentedFilesProvider (com.google.devtools.build.lib.rules.test.InstrumentedFilesProvider)8 Artifact (com.google.devtools.build.lib.actions.Artifact)6 Runfiles (com.google.devtools.build.lib.analysis.Runfiles)4 RuleConfiguredTargetBuilder (com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder)3 RunfilesProvider (com.google.devtools.build.lib.analysis.RunfilesProvider)3 ConfiguredTarget (com.google.devtools.build.lib.analysis.ConfiguredTarget)2 FileConfiguredTarget (com.google.devtools.build.lib.analysis.FileConfiguredTarget)2 RuleConfiguredTarget (com.google.devtools.build.lib.analysis.RuleConfiguredTarget)2 NestedSetBuilder (com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder)2 FeatureConfiguration (com.google.devtools.build.lib.rules.cpp.CcToolchainFeatures.FeatureConfiguration)2 InstrumentationSpec (com.google.devtools.build.lib.rules.test.InstrumentedFilesCollector.InstrumentationSpec)2 PathFragment (com.google.devtools.build.lib.vfs.PathFragment)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 Function (com.google.common.base.Function)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSortedMap (com.google.common.collect.ImmutableSortedMap)1 FailAction (com.google.devtools.build.lib.actions.FailAction)1 Root (com.google.devtools.build.lib.actions.Root)1