Search in sources :

Example 26 with RuleConfiguredTargetBuilder

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

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

the class SkylarkRuleConfiguredTargetBuilder method createTarget.

// TODO(bazel-team): this whole defaulting - overriding executable, runfiles and files_to_build
// is getting out of hand. Clean this whole mess up.
private static ConfiguredTarget createTarget(RuleContext ruleContext, Object target, Map<String, Class<? extends TransitiveInfoProvider>> registeredProviderTypes) throws EvalException {
    Artifact executable = getExecutable(ruleContext, target);
    RuleConfiguredTargetBuilder builder = new RuleConfiguredTargetBuilder(ruleContext);
    // Set the default files to build.
    NestedSetBuilder<Artifact> filesToBuild = NestedSetBuilder.<Artifact>stableOrder().addAll(ruleContext.getOutputArtifacts());
    if (executable != null) {
        filesToBuild.add(executable);
    }
    builder.setFilesToBuild(filesToBuild.build());
    return addStructFieldsAndBuild(ruleContext, builder, target, executable, registeredProviderTypes);
}
Also used : RuleConfiguredTargetBuilder(com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder) Artifact(com.google.devtools.build.lib.actions.Artifact)

Example 28 with RuleConfiguredTargetBuilder

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

the class AndroidLibrary method create.

@Override
public ConfiguredTarget create(RuleContext ruleContext) throws InterruptedException, RuleErrorException {
    validateRuleContext(ruleContext);
    JavaSemantics javaSemantics = createJavaSemantics();
    AndroidSemantics androidSemantics = createAndroidSemantics();
    if (!AndroidSdkProvider.verifyPresence(ruleContext)) {
        return null;
    }
    checkResourceInlining(ruleContext);
    NestedSetBuilder<Aar> transitiveAars = collectTransitiveAars(ruleContext);
    NestedSetBuilder<Artifact> proguardConfigsbuilder = NestedSetBuilder.stableOrder();
    proguardConfigsbuilder.addTransitive(new ProguardLibrary(ruleContext).collectProguardSpecs());
    AndroidIdlHelper.addSupportLibProguardConfigs(ruleContext, proguardConfigsbuilder);
    NestedSet<Artifact> transitiveProguardConfigs = proguardConfigsbuilder.build();
    JavaCommon javaCommon = AndroidCommon.createJavaCommonWithAndroidDataBinding(ruleContext, javaSemantics, true);
    javaSemantics.checkRule(ruleContext, javaCommon);
    AndroidCommon androidCommon = new AndroidCommon(javaCommon);
    boolean definesLocalResources = LocalResourceContainer.definesAndroidResources(ruleContext.attributes());
    if (definesLocalResources) {
        LocalResourceContainer.validateRuleContext(ruleContext);
    }
    final ResourceApk resourceApk;
    if (definesLocalResources) {
        ApplicationManifest applicationManifest = androidSemantics.getManifestForRule(ruleContext).renamePackage(ruleContext, AndroidCommon.getJavaPackage(ruleContext));
        resourceApk = applicationManifest.packWithDataAndResources(null, /* resourceApk -- not needed for library */
        ruleContext, true, /* isLibrary */
        ResourceDependencies.fromRuleDeps(ruleContext, JavaCommon.isNeverLink(ruleContext)), ruleContext.getImplicitOutputArtifact(AndroidRuleClasses.ANDROID_R_TXT), ruleContext.getImplicitOutputArtifact(AndroidRuleClasses.ANDROID_SYMBOLS), ResourceConfigurationFilter.empty(ruleContext), ImmutableList.<String>of(), /* uncompressedExtensions */
        false, /* crunchPng */
        ImmutableList.<String>of(), /* densities */
        false, /* incremental */
        null, /* proguardCfgOut */
        null, /* mainDexProguardCfg */
        ruleContext.getImplicitOutputArtifact(AndroidRuleClasses.ANDROID_PROCESSED_MANIFEST), ruleContext.getImplicitOutputArtifact(AndroidRuleClasses.ANDROID_RESOURCES_ZIP), DataBinding.isEnabled(ruleContext) ? DataBinding.getLayoutInfoFile(ruleContext) : null);
        if (ruleContext.hasErrors()) {
            return null;
        }
    } else {
        resourceApk = ResourceApk.fromTransitiveResources(ResourceDependencies.fromRuleResourceAndDeps(ruleContext, false));
    }
    if (!ruleContext.getFragment(AndroidConfiguration.class).allowSrcsLessAndroidLibraryDeps() && !definesLocalResources && ruleContext.attributes().get("srcs", BuildType.LABEL_LIST).isEmpty() && ruleContext.attributes().get("idl_srcs", BuildType.LABEL_LIST).isEmpty() && !ruleContext.attributes().get("deps", BuildType.LABEL_LIST).isEmpty()) {
        ruleContext.attributeError("deps", "deps not allowed without srcs; move to exports?");
    }
    JavaTargetAttributes javaTargetAttributes = androidCommon.init(javaSemantics, androidSemantics, resourceApk, false, /* addCoverageSupport */
    true, /* collectJavaCompilationArgs */
    false);
    if (javaTargetAttributes == null) {
        return null;
    }
    Artifact classesJar = ruleContext.getImplicitOutputArtifact(AndroidRuleClasses.ANDROID_LIBRARY_CLASS_JAR);
    Artifact aarOut = ruleContext.getImplicitOutputArtifact(AndroidRuleClasses.ANDROID_LIBRARY_AAR);
    final ResourceContainer primaryResources;
    final Aar aar;
    if (definesLocalResources) {
        primaryResources = resourceApk.getPrimaryResource();
        // applicationManifest has already been checked for nullness above in this method
        ApplicationManifest applicationManifest = androidSemantics.getManifestForRule(ruleContext);
        aar = Aar.create(aarOut, applicationManifest.getManifest());
        transitiveAars.add(aar);
    } else if (AndroidCommon.getAndroidResources(ruleContext) != null) {
        primaryResources = Iterables.getOnlyElement(AndroidCommon.getAndroidResources(ruleContext).getDirectAndroidResources());
        aar = Aar.create(aarOut, primaryResources.getManifest());
        transitiveAars.add(aar);
    } else {
        // there are no local resources and resources attribute was not specified either
        aar = null;
        ApplicationManifest applicationManifest = ApplicationManifest.generatedManifest(ruleContext).renamePackage(ruleContext, AndroidCommon.getJavaPackage(ruleContext));
        String javaPackage = AndroidCommon.getJavaPackage(ruleContext);
        ResourceContainer resourceContainer = ResourceContainer.builder().setLabel(ruleContext.getLabel()).setJavaPackageFromString(javaPackage).setManifest(applicationManifest.getManifest()).setJavaSourceJar(ruleContext.getImplicitOutputArtifact(AndroidRuleClasses.ANDROID_JAVA_SOURCE_JAR)).setManifestExported(ruleContext.attributes().get("exports_manifest", Type.BOOLEAN)).setRTxt(ruleContext.getImplicitOutputArtifact(AndroidRuleClasses.ANDROID_R_TXT)).build();
        primaryResources = new AndroidResourcesProcessorBuilder(ruleContext).setLibrary(true).setRTxtOut(resourceContainer.getRTxt()).setManifestOut(ruleContext.getImplicitOutputArtifact(AndroidRuleClasses.ANDROID_PROCESSED_MANIFEST)).setSourceJarOut(resourceContainer.getJavaSourceJar()).setJavaPackage(resourceContainer.getJavaPackage()).withPrimary(resourceContainer).withDependencies(resourceApk.getResourceDependencies()).setDebug(ruleContext.getConfiguration().getCompilationMode() != CompilationMode.OPT).build(ruleContext);
    }
    new AarGeneratorBuilder(ruleContext).withPrimary(primaryResources).withManifest(aar != null ? aar.getManifest() : primaryResources.getManifest()).withRtxt(primaryResources.getRTxt()).withClasses(classesJar).setAAROut(aarOut).build(ruleContext);
    RuleConfiguredTargetBuilder builder = new RuleConfiguredTargetBuilder(ruleContext);
    androidCommon.addTransitiveInfoProviders(builder, androidSemantics, aarOut, resourceApk, null, ImmutableList.<Artifact>of());
    androidSemantics.addTransitiveInfoProviders(builder, ruleContext, javaCommon, androidCommon, null);
    NestedSetBuilder<Artifact> transitiveResourcesJars = collectTransitiveResourceJars(ruleContext);
    if (androidCommon.getResourceClassJar() != null) {
        transitiveResourcesJars.add(androidCommon.getResourceClassJar());
    }
    builder.addProvider(NativeLibsZipsProvider.class, new NativeLibsZipsProvider(AndroidCommon.collectTransitiveNativeLibsZips(ruleContext).build())).add(JavaNeverlinkInfoProvider.class, new JavaNeverlinkInfoProvider(androidCommon.isNeverLink())).add(JavaSourceInfoProvider.class, JavaSourceInfoProvider.fromJavaTargetAttributes(javaTargetAttributes, javaSemantics)).add(AndroidCcLinkParamsProvider.class, AndroidCcLinkParamsProvider.create(androidCommon.getCcLinkParamsStore())).add(JavaPluginInfoProvider.class, JavaCommon.getTransitivePlugins(ruleContext)).add(ProguardSpecProvider.class, new ProguardSpecProvider(transitiveProguardConfigs)).addOutputGroup(OutputGroupProvider.HIDDEN_TOP_LEVEL, transitiveProguardConfigs).add(AndroidLibraryResourceClassJarProvider.class, AndroidLibraryResourceClassJarProvider.create(transitiveResourcesJars.build()));
    if (!JavaCommon.isNeverLink(ruleContext)) {
        builder.add(AndroidLibraryAarProvider.class, AndroidLibraryAarProvider.create(aar, transitiveAars.build()));
    }
    return builder.build();
}
Also used : JavaPluginInfoProvider(com.google.devtools.build.lib.rules.java.JavaPluginInfoProvider) ProguardLibrary(com.google.devtools.build.lib.rules.java.ProguardLibrary) ProguardSpecProvider(com.google.devtools.build.lib.rules.java.ProguardSpecProvider) JavaSemantics(com.google.devtools.build.lib.rules.java.JavaSemantics) Artifact(com.google.devtools.build.lib.actions.Artifact) JavaTargetAttributes(com.google.devtools.build.lib.rules.java.JavaTargetAttributes) Aar(com.google.devtools.build.lib.rules.android.AndroidLibraryAarProvider.Aar) JavaSourceInfoProvider(com.google.devtools.build.lib.rules.java.JavaSourceInfoProvider) RuleConfiguredTargetBuilder(com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder) JavaNeverlinkInfoProvider(com.google.devtools.build.lib.rules.java.JavaNeverlinkInfoProvider) JavaCommon(com.google.devtools.build.lib.rules.java.JavaCommon)

Example 29 with RuleConfiguredTargetBuilder

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

the class AndroidSdk method create.

@Override
public ConfiguredTarget create(RuleContext ruleContext) throws InterruptedException, RuleErrorException {
    // If the user didn't specify --proguard_top, go with the proguard attribute in the android_sdk
    // rule. Otherwise, use what she told us to.
    FilesToRunProvider proguard = ruleContext.getFragment(JavaConfiguration.class).getProguardBinary() == null ? ruleContext.getExecutablePrerequisite("proguard", Mode.HOST) : ruleContext.getExecutablePrerequisite(":proguard", Mode.HOST);
    String buildToolsVersion = AggregatingAttributeMapper.of(ruleContext.getRule()).get("build_tools_version", Type.STRING);
    Revision parsedBuildToolsVersion = null;
    try {
        parsedBuildToolsVersion = Strings.isNullOrEmpty(buildToolsVersion) ? null : Revision.parseRevision(buildToolsVersion);
    } catch (NumberFormatException nfe) {
        ruleContext.attributeError("build_tools_version", "Invalid version: " + buildToolsVersion);
    }
    boolean aaptSupportsMainDexGeneration = parsedBuildToolsVersion == null || parsedBuildToolsVersion.compareTo(new Revision(24)) >= 0;
    FilesToRunProvider aidl = ruleContext.getExecutablePrerequisite("aidl", Mode.HOST);
    FilesToRunProvider aapt = ruleContext.getExecutablePrerequisite("aapt", Mode.HOST);
    FilesToRunProvider apkBuilder = ruleContext.getExecutablePrerequisite("apkbuilder", Mode.HOST);
    FilesToRunProvider apkSigner = ruleContext.getExecutablePrerequisite("apksigner", Mode.HOST);
    FilesToRunProvider adb = ruleContext.getExecutablePrerequisite("adb", Mode.HOST);
    FilesToRunProvider dx = ruleContext.getExecutablePrerequisite("dx", Mode.HOST);
    FilesToRunProvider mainDexListCreator = ruleContext.getExecutablePrerequisite("main_dex_list_creator", Mode.HOST);
    FilesToRunProvider zipalign = ruleContext.getExecutablePrerequisite("zipalign", Mode.HOST);
    FilesToRunProvider jack = ruleContext.getExecutablePrerequisite("jack", Mode.HOST);
    FilesToRunProvider jill = ruleContext.getExecutablePrerequisite("jill", Mode.HOST);
    FilesToRunProvider resourceExtractor = ruleContext.getExecutablePrerequisite("resource_extractor", Mode.HOST);
    Artifact frameworkAidl = ruleContext.getPrerequisiteArtifact("framework_aidl", Mode.HOST);
    TransitiveInfoCollection aidlLib = ruleContext.getPrerequisite("aidl_lib", Mode.TARGET);
    Artifact androidJar = ruleContext.getPrerequisiteArtifact("android_jar", Mode.HOST);
    Artifact shrinkedAndroidJar = ruleContext.getPrerequisiteArtifact("shrinked_android_jar", Mode.HOST);
    // Because all Jack actions using this android_sdk will need Jack versions of the Android and
    // Java classpaths, pre-translate the jars for Android and Java targets here. (They will only
    // be run if needed, as usual for Bazel.)
    NestedSet<Artifact> androidBaseClasspathForJack = convertClasspathJarsToJack(ruleContext, jack, jill, resourceExtractor, ImmutableList.of(androidJar));
    NestedSet<Artifact> javaBaseClasspathForJack = convertClasspathJarsToJack(ruleContext, jack, jill, resourceExtractor, JavaCompilationHelper.getBootClasspath(JavaToolchainProvider.fromRuleContext(ruleContext)));
    Artifact annotationsJar = ruleContext.getPrerequisiteArtifact("annotations_jar", Mode.HOST);
    Artifact mainDexClasses = ruleContext.getPrerequisiteArtifact("main_dex_classes", Mode.HOST);
    if (ruleContext.hasErrors()) {
        return null;
    }
    return new RuleConfiguredTargetBuilder(ruleContext).add(AndroidSdkProvider.class, AndroidSdkProvider.create(buildToolsVersion, aaptSupportsMainDexGeneration, frameworkAidl, aidlLib, androidJar, shrinkedAndroidJar, androidBaseClasspathForJack, javaBaseClasspathForJack, annotationsJar, mainDexClasses, adb, dx, mainDexListCreator, aidl, aapt, apkBuilder, apkSigner, proguard, zipalign, jack, jill, resourceExtractor)).add(RunfilesProvider.class, RunfilesProvider.EMPTY).setFilesToBuild(NestedSetBuilder.<Artifact>emptySet(Order.STABLE_ORDER)).build();
}
Also used : FilesToRunProvider(com.google.devtools.build.lib.analysis.FilesToRunProvider) Revision(com.android.repository.Revision) RuleConfiguredTargetBuilder(com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder) JavaConfiguration(com.google.devtools.build.lib.rules.java.JavaConfiguration) TransitiveInfoCollection(com.google.devtools.build.lib.analysis.TransitiveInfoCollection) Artifact(com.google.devtools.build.lib.actions.Artifact)

Example 30 with RuleConfiguredTargetBuilder

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

the class ActionListener method create.

@Override
public ConfiguredTarget create(RuleContext ruleContext) throws RuleErrorException {
    // This rule doesn't produce any output when listed as a build target.
    // Only when used via the --experimental_action_listener flag,
    // this rule instructs the build system to add additional outputs.
    List<ExtraActionSpec> extraActions;
    Multimap<String, ExtraActionSpec> extraActionMap;
    Set<String> mnemonics = Sets.newHashSet(ruleContext.attributes().get("mnemonics", Type.STRING_LIST));
    extraActions = retrieveAndValidateExtraActions(ruleContext);
    ImmutableSortedKeyListMultimap.Builder<String, ExtraActionSpec> extraActionMapBuilder = ImmutableSortedKeyListMultimap.builder();
    for (String mnemonic : mnemonics) {
        extraActionMapBuilder.putAll(mnemonic, extraActions);
    }
    extraActionMap = extraActionMapBuilder.build();
    return new RuleConfiguredTargetBuilder(ruleContext).add(RunfilesProvider.class, RunfilesProvider.simple(Runfiles.EMPTY)).add(ExtraActionMapProvider.class, new ExtraActionMapProvider(extraActionMap)).build();
}
Also used : RuleConfiguredTargetBuilder(com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder) ImmutableSortedKeyListMultimap(com.google.devtools.build.lib.collect.ImmutableSortedKeyListMultimap)

Aggregations

RuleConfiguredTargetBuilder (com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder)48 Artifact (com.google.devtools.build.lib.actions.Artifact)36 NestedSetBuilder (com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder)21 RunfilesProvider (com.google.devtools.build.lib.analysis.RunfilesProvider)19 Runfiles (com.google.devtools.build.lib.analysis.Runfiles)17 TransitiveInfoCollection (com.google.devtools.build.lib.analysis.TransitiveInfoCollection)11 RunfilesSupport (com.google.devtools.build.lib.analysis.RunfilesSupport)7 AppleConfiguration (com.google.devtools.build.lib.rules.apple.AppleConfiguration)6 CcLinkParamsProvider (com.google.devtools.build.lib.rules.cpp.CcLinkParamsProvider)5 ArrayList (java.util.ArrayList)5 ImmutableMap (com.google.common.collect.ImmutableMap)4 FileProvider (com.google.devtools.build.lib.analysis.FileProvider)4 FilesToRunProvider (com.google.devtools.build.lib.analysis.FilesToRunProvider)4 Label (com.google.devtools.build.lib.cmdline.Label)4 CcLinkParamsStore (com.google.devtools.build.lib.rules.cpp.CcLinkParamsStore)4 PathFragment (com.google.devtools.build.lib.vfs.PathFragment)4 BuildConfiguration (com.google.devtools.build.lib.analysis.config.BuildConfiguration)3 JavaRuleOutputJarsProvider (com.google.devtools.build.lib.rules.java.JavaRuleOutputJarsProvider)3 CompositeRunfilesSupplier (com.google.devtools.build.lib.actions.CompositeRunfilesSupplier)2 CommandHelper (com.google.devtools.build.lib.analysis.CommandHelper)2