Search in sources :

Example 6 with RunfilesSupport

use of com.google.devtools.build.lib.analysis.RunfilesSupport 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 7 with RunfilesSupport

use of com.google.devtools.build.lib.analysis.RunfilesSupport 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 RunfilesSupport

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

the class BinaryLinkingTargetFactory method create.

@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);
    ObjcProvider objcProvider = common.getObjcProvider();
    assertLibraryOrSources(objcProvider, ruleContext);
    XcodeProvider.Builder xcodeProviderBuilder = new XcodeProvider.Builder().addPropagatedDependencies(protosXcodeProvider.asSet());
    IntermediateArtifacts intermediateArtifacts = ObjcRuleClasses.intermediateArtifacts(ruleContext);
    NestedSetBuilder<Artifact> filesToBuild = NestedSetBuilder.<Artifact>stableOrder().add(intermediateArtifacts.strippedSingleArchitectureBinary());
    new ResourceSupport(ruleContext).validateAttributes().addXcodeSettings(xcodeProviderBuilder);
    ruleContext.assertNoErrors();
    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 compilationSupport = CompilationSupport.create(ruleContext).validateAttributes().addXcodeSettings(xcodeProviderBuilder, common).registerCompileAndArchiveActions(common).registerFullyLinkAction(common.getObjcProvider(), ruleContext.getImplicitOutputArtifact(CompilationSupport.FULLY_LINKED_LIB)).registerLinkActions(objcProvider, j2ObjcMappingFileProvider, j2ObjcEntryClassProvider, getExtraLinkArgs(ruleContext), ImmutableList.<Artifact>of(), DsymOutputType.APP);
    Optional<XcTestAppProvider> xcTestAppProvider;
    Optional<RunfilesSupport> maybeRunfilesSupport = Optional.absent();
    switch(hasReleaseBundlingSupport) {
        case YES:
            AppleConfiguration appleConfiguration = ruleContext.getFragment(AppleConfiguration.class);
            // TODO(bazel-team): Remove once all bundle users are migrated to ios_application.
            ReleaseBundlingSupport releaseBundlingSupport = new ReleaseBundlingSupport(ruleContext, objcProvider, LinkedBinary.LOCAL_AND_DEPENDENCIES, ReleaseBundlingSupport.APP_BUNDLE_DIR_FORMAT, appleConfiguration.getMinimumOsForPlatformType(PlatformType.IOS), appleConfiguration.getSingleArchPlatform());
            releaseBundlingSupport.registerActions(DsymOutputType.APP).addXcodeSettings(xcodeProviderBuilder).addFilesToBuild(filesToBuild, Optional.of(DsymOutputType.APP)).validateResources().validateAttributes();
            xcTestAppProvider = Optional.of(releaseBundlingSupport.xcTestAppProvider());
            if (appleConfiguration.getMultiArchPlatform(PlatformType.IOS) == Platform.IOS_SIMULATOR) {
                Artifact runnerScript = intermediateArtifacts.runnerScript();
                Artifact ipaFile = ruleContext.getImplicitOutputArtifact(ReleaseBundlingSupport.IPA);
                releaseBundlingSupport.registerGenerateRunnerScriptAction(runnerScript, ipaFile);
                maybeRunfilesSupport = Optional.of(releaseBundlingSupport.runfilesSupport(runnerScript));
            }
            break;
        case NO:
            xcTestAppProvider = Optional.absent();
            break;
        default:
            throw new AssertionError();
    }
    XcodeSupport xcodeSupport = new XcodeSupport(ruleContext).addXcodeSettings(xcodeProviderBuilder, objcProvider, 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);
    if (productType != XcodeProductType.LIBRARY_STATIC) {
        xcodeSupport.generateCompanionLibXcodeTarget(xcodeProviderBuilder);
    }
    XcodeProvider xcodeProvider = xcodeProviderBuilder.build();
    xcodeSupport.registerActions(xcodeProvider);
    RuleConfiguredTargetBuilder targetBuilder = ObjcRuleClasses.ruleConfiguredTarget(ruleContext, filesToBuild.build()).addProvider(XcodeProvider.class, xcodeProvider).addProvider(ObjcProvider.class, objcProvider).addProvider(InstrumentedFilesProvider.class, compilationSupport.getInstrumentedFilesProvider(common));
    if (xcTestAppProvider.isPresent()) {
        // TODO(bazel-team): Stop exporting an XcTestAppProvider once objc_binary no longer creates an
        // application bundle.
        targetBuilder.addProvider(XcTestAppProvider.class, xcTestAppProvider.get());
    }
    if (maybeRunfilesSupport.isPresent()) {
        RunfilesSupport runfilesSupport = maybeRunfilesSupport.get();
        targetBuilder.setRunfilesSupport(runfilesSupport, runfilesSupport.getExecutable());
    }
    configureTarget(targetBuilder, ruleContext);
    return targetBuilder.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) RunfilesSupport(com.google.devtools.build.lib.analysis.RunfilesSupport) Artifact(com.google.devtools.build.lib.actions.Artifact) RuleConfiguredTargetBuilder(com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder)

Example 9 with RunfilesSupport

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

the class RunCommand method ensureRunfilesBuilt.

/**
   * Ensures that runfiles are built for the specified target. If they already
   * are, does nothing, otherwise builds them.
   *
   * @param target the target to build runfiles for.
   * @return the path of the runfiles directory.
   * @throws CommandException
   */
private Path ensureRunfilesBuilt(CommandEnvironment env, ConfiguredTarget target) throws CommandException {
    FilesToRunProvider provider = target.getProvider(FilesToRunProvider.class);
    RunfilesSupport runfilesSupport = provider == null ? null : provider.getRunfilesSupport();
    if (runfilesSupport == null) {
        return env.getWorkingDirectory();
    }
    Artifact manifest = runfilesSupport.getRunfilesManifest();
    PathFragment runfilesDir = runfilesSupport.getRunfilesDirectoryExecPath();
    Path workingDir = env.getExecRoot().getRelative(runfilesDir);
    // Workspace name directory doesn't exist, so don't add it.
    if (target.getConfiguration().runfilesEnabled()) {
        workingDir = workingDir.getRelative(runfilesSupport.getRunfiles().getSuffix());
    }
    // a handy way to check whether runfiles were built or not.
    if (!RUNFILES_MANIFEST.matches(manifest.getFilename())) {
        // Runfiles already built, nothing to do.
        return workingDir;
    }
    SymlinkTreeHelper helper = new SymlinkTreeHelper(manifest.getPath(), runfilesSupport.getRunfilesDirectory(), false);
    helper.createSymlinksUsingCommand(env.getExecRoot(), target.getConfiguration(), env.getBlazeWorkspace().getBinTools());
    return workingDir;
}
Also used : Path(com.google.devtools.build.lib.vfs.Path) FilesToRunProvider(com.google.devtools.build.lib.analysis.FilesToRunProvider) SymlinkTreeHelper(com.google.devtools.build.lib.exec.SymlinkTreeHelper) RunfilesSupport(com.google.devtools.build.lib.analysis.RunfilesSupport) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) Artifact(com.google.devtools.build.lib.actions.Artifact)

Aggregations

RunfilesSupport (com.google.devtools.build.lib.analysis.RunfilesSupport)9 Artifact (com.google.devtools.build.lib.actions.Artifact)7 RuleConfiguredTargetBuilder (com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder)7 Runfiles (com.google.devtools.build.lib.analysis.Runfiles)6 NestedSetBuilder (com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder)5 RunfilesProvider (com.google.devtools.build.lib.analysis.RunfilesProvider)4 PathFragment (com.google.devtools.build.lib.vfs.PathFragment)4 ArrayList (java.util.ArrayList)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 FilesToRunProvider (com.google.devtools.build.lib.analysis.FilesToRunProvider)2 AppleConfiguration (com.google.devtools.build.lib.rules.apple.AppleConfiguration)2 ExecutionInfoProvider (com.google.devtools.build.lib.rules.test.ExecutionInfoProvider)2 Path (com.google.devtools.build.lib.vfs.Path)2 ConfiguredTarget (com.google.devtools.build.lib.analysis.ConfiguredTarget)1 RuleContext (com.google.devtools.build.lib.analysis.RuleContext)1 TransitiveInfoCollection (com.google.devtools.build.lib.analysis.TransitiveInfoCollection)1 ExecutableSymlinkAction (com.google.devtools.build.lib.analysis.actions.ExecutableSymlinkAction)1 BuildConfiguration (com.google.devtools.build.lib.analysis.config.BuildConfiguration)1 RunUnder (com.google.devtools.build.lib.analysis.config.RunUnder)1 BuildRequest (com.google.devtools.build.lib.buildtool.BuildRequest)1