Search in sources :

Example 36 with Rule

use of com.google.devtools.build.lib.packages.Rule in project bazel by bazelbuild.

the class ConstraintSemantics method maybeGetRuleClassDefaults.

/**
   * Returns the rule class defaults specified for this rule, or null if there are
   * no such defaults.
   */
@Nullable
private static EnvironmentCollector maybeGetRuleClassDefaults(RuleContext ruleContext) {
    Rule rule = ruleContext.getRule();
    String restrictionAttr = RuleClass.DEFAULT_RESTRICTED_ENVIRONMENT_ATTR;
    String compatibilityAttr = RuleClass.DEFAULT_COMPATIBLE_ENVIRONMENT_ATTR;
    if (rule.isAttrDefined(restrictionAttr, BuildType.LABEL_LIST) || rule.isAttrDefined(compatibilityAttr, BuildType.LABEL_LIST)) {
        return new EnvironmentCollector(ruleContext, restrictionAttr, compatibilityAttr, new GroupDefaultsProvider());
    } else {
        return null;
    }
}
Also used : Rule(com.google.devtools.build.lib.packages.Rule) Nullable(javax.annotation.Nullable)

Example 37 with Rule

use of com.google.devtools.build.lib.packages.Rule in project bazel by bazelbuild.

the class MavenServerFunction method compute.

@Nullable
@Override
public SkyValue compute(SkyKey skyKey, Environment env) throws InterruptedException, RepositoryFunctionException {
    String repository = (String) skyKey.argument();
    Rule repositoryRule = null;
    try {
        repositoryRule = RepositoryFunction.getRule(repository, env);
    } catch (RepositoryNotFoundException ex) {
    // Ignored. We throw a new one below.
    }
    BlazeDirectories directories = PrecomputedValue.BLAZE_DIRECTORIES.get(env);
    if (env.valuesMissing()) {
        return null;
    }
    String serverName;
    String url;
    Map<String, FileValue> settingsFiles;
    boolean foundRepoRule = repositoryRule != null && repositoryRule.getRuleClass().equals(MavenServerRule.NAME);
    if (!foundRepoRule) {
        if (repository.equals(MavenServerValue.DEFAULT_ID)) {
            settingsFiles = getDefaultSettingsFile(directories, env);
            serverName = MavenServerValue.DEFAULT_ID;
            url = MavenConnector.getMavenCentralRemote().getUrl();
        } else {
            throw new RepositoryFunctionException(new IOException("Could not find maven repository " + repository), Transience.TRANSIENT);
        }
    } else {
        WorkspaceAttributeMapper mapper = WorkspaceAttributeMapper.of(repositoryRule);
        serverName = repositoryRule.getName();
        try {
            url = mapper.get("url", Type.STRING);
            if (!mapper.isAttributeValueExplicitlySpecified("settings_file")) {
                settingsFiles = getDefaultSettingsFile(directories, env);
            } else {
                PathFragment settingsFilePath = new PathFragment(mapper.get("settings_file", Type.STRING));
                RootedPath settingsPath = RootedPath.toRootedPath(directories.getWorkspace().getRelative(settingsFilePath), PathFragment.EMPTY_FRAGMENT);
                FileValue fileValue = (FileValue) env.getValue(FileValue.key(settingsPath));
                if (fileValue == null) {
                    return null;
                }
                if (!fileValue.exists()) {
                    throw new RepositoryFunctionException(new IOException("Could not find settings file " + settingsPath), Transience.TRANSIENT);
                }
                settingsFiles = ImmutableMap.<String, FileValue>builder().put(USER_KEY, fileValue).build();
            }
        } catch (EvalException e) {
            throw new RepositoryFunctionException(e, Transience.PERSISTENT);
        }
    }
    if (settingsFiles == null) {
        return null;
    }
    Fingerprint fingerprint = new Fingerprint();
    try {
        for (Map.Entry<String, FileValue> entry : settingsFiles.entrySet()) {
            fingerprint.addString(entry.getKey());
            Path path = entry.getValue().realRootedPath().asPath();
            if (path.exists()) {
                fingerprint.addBoolean(true);
                fingerprint.addBytes(path.getDigest());
            } else {
                fingerprint.addBoolean(false);
            }
        }
    } catch (IOException e) {
        throw new RepositoryFunctionException(e, Transience.TRANSIENT);
    }
    byte[] fingerprintBytes = fingerprint.digestAndReset();
    if (settingsFiles.isEmpty()) {
        return new MavenServerValue(serverName, url, new Server(), fingerprintBytes);
    }
    DefaultSettingsBuildingRequest request = new DefaultSettingsBuildingRequest();
    if (settingsFiles.containsKey(SYSTEM_KEY)) {
        request.setGlobalSettingsFile(settingsFiles.get(SYSTEM_KEY).realRootedPath().asPath().getPathFile());
    }
    if (settingsFiles.containsKey(USER_KEY)) {
        request.setUserSettingsFile(settingsFiles.get(USER_KEY).realRootedPath().asPath().getPathFile());
    }
    DefaultSettingsBuilder builder = (new DefaultSettingsBuilderFactory()).newInstance();
    SettingsBuildingResult result;
    try {
        result = builder.build(request);
    } catch (SettingsBuildingException e) {
        throw new RepositoryFunctionException(new IOException("Error parsing settings files: " + e.getMessage()), Transience.TRANSIENT);
    }
    if (!result.getProblems().isEmpty()) {
        throw new RepositoryFunctionException(new IOException("Errors interpreting settings file: " + Arrays.toString(result.getProblems().toArray())), Transience.PERSISTENT);
    }
    Settings settings = result.getEffectiveSettings();
    Server server = settings.getServer(serverName);
    server = server == null ? new Server() : server;
    return new MavenServerValue(serverName, url, server, fingerprintBytes);
}
Also used : FileValue(com.google.devtools.build.lib.skyframe.FileValue) Server(org.apache.maven.settings.Server) SettingsBuildingResult(org.apache.maven.settings.building.SettingsBuildingResult) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) RootedPath(com.google.devtools.build.lib.vfs.RootedPath) RepositoryFunctionException(com.google.devtools.build.lib.rules.repository.RepositoryFunction.RepositoryFunctionException) Settings(org.apache.maven.settings.Settings) RootedPath(com.google.devtools.build.lib.vfs.RootedPath) Path(com.google.devtools.build.lib.vfs.Path) SettingsBuildingException(org.apache.maven.settings.building.SettingsBuildingException) Fingerprint(com.google.devtools.build.lib.util.Fingerprint) DefaultSettingsBuildingRequest(org.apache.maven.settings.building.DefaultSettingsBuildingRequest) RepositoryNotFoundException(com.google.devtools.build.lib.rules.repository.RepositoryFunction.RepositoryNotFoundException) IOException(java.io.IOException) EvalException(com.google.devtools.build.lib.syntax.EvalException) DefaultSettingsBuilderFactory(org.apache.maven.settings.building.DefaultSettingsBuilderFactory) BlazeDirectories(com.google.devtools.build.lib.analysis.BlazeDirectories) DefaultSettingsBuilder(org.apache.maven.settings.building.DefaultSettingsBuilder) MavenServerRule(com.google.devtools.build.lib.bazel.rules.workspace.MavenServerRule) Rule(com.google.devtools.build.lib.packages.Rule) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) WorkspaceAttributeMapper(com.google.devtools.build.lib.rules.repository.WorkspaceAttributeMapper) Nullable(javax.annotation.Nullable)

Example 38 with Rule

use of com.google.devtools.build.lib.packages.Rule in project bazel by bazelbuild.

the class TransitiveBaseTraversalFunction method getLabelDeps.

// TODO(bazel-team): Unify this logic with that in LabelVisitor, and possibly DependencyResolver.
private static Iterable<Label> getLabelDeps(Target target) throws InterruptedException {
    final Set<Label> labels = new HashSet<>();
    if (target instanceof OutputFile) {
        Rule rule = ((OutputFile) target).getGeneratingRule();
        labels.add(rule.getLabel());
        visitTargetVisibility(target, labels);
    } else if (target instanceof InputFile) {
        visitTargetVisibility(target, labels);
    } else if (target instanceof Rule) {
        visitTargetVisibility(target, labels);
        visitRule(target, labels);
    } else if (target instanceof PackageGroup) {
        visitPackageGroup((PackageGroup) target, labels);
    }
    return labels;
}
Also used : OutputFile(com.google.devtools.build.lib.packages.OutputFile) Label(com.google.devtools.build.lib.cmdline.Label) Rule(com.google.devtools.build.lib.packages.Rule) PackageGroup(com.google.devtools.build.lib.packages.PackageGroup) HashSet(java.util.HashSet) InputFile(com.google.devtools.build.lib.packages.InputFile)

Example 39 with Rule

use of com.google.devtools.build.lib.packages.Rule in project bazel by bazelbuild.

the class LicensesProviderImpl method of.

/**
   * Create the appropriate {@link LicensesProvider} for a rule based on its {@code RuleContext}
   */
public static LicensesProvider of(RuleContext ruleContext) {
    if (!ruleContext.getConfiguration().checkLicenses()) {
        return EMPTY;
    }
    NestedSetBuilder<TargetLicense> builder = NestedSetBuilder.linkOrder();
    BuildConfiguration configuration = ruleContext.getConfiguration();
    Rule rule = ruleContext.getRule();
    AttributeMap attributes = ruleContext.attributes();
    License toolOutputLicense = rule.getToolOutputLicense(attributes);
    TargetLicense outputLicenses = toolOutputLicense == null ? null : new TargetLicense(rule.getLabel(), toolOutputLicense);
    if (configuration.isHostConfiguration() && toolOutputLicense != null) {
        if (toolOutputLicense != License.NO_LICENSE) {
            builder.add(outputLicenses);
        }
    } else {
        if (rule.getLicense() != License.NO_LICENSE) {
            builder.add(new TargetLicense(rule.getLabel(), rule.getLicense()));
        }
        ListMultimap<String, ? extends TransitiveInfoCollection> configuredMap = ruleContext.getConfiguredTargetMap();
        for (String depAttrName : attributes.getAttributeNames()) {
            // Only add the transitive licenses for the attributes that do not have the output_licenses.
            Attribute attribute = attributes.getAttributeDefinition(depAttrName);
            for (TransitiveInfoCollection dep : configuredMap.get(depAttrName)) {
                LicensesProvider provider = dep.getProvider(LicensesProvider.class);
                if (provider == null) {
                    continue;
                }
                if (useOutputLicenses(attribute, configuration) && provider.hasOutputLicenses()) {
                    builder.add(provider.getOutputLicenses());
                } else {
                    builder.addTransitive(provider.getTransitiveLicenses());
                }
            }
        }
    }
    return new LicensesProviderImpl(builder.build(), outputLicenses);
}
Also used : BuildConfiguration(com.google.devtools.build.lib.analysis.config.BuildConfiguration) AttributeMap(com.google.devtools.build.lib.packages.AttributeMap) Attribute(com.google.devtools.build.lib.packages.Attribute) License(com.google.devtools.build.lib.packages.License) Rule(com.google.devtools.build.lib.packages.Rule)

Example 40 with Rule

use of com.google.devtools.build.lib.packages.Rule in project bazel by bazelbuild.

the class ConfiguredTargetFactory method createConfiguredTarget.

/**
   * Invokes the appropriate constructor to create a {@link ConfiguredTarget} instance.
   * <p>For use in {@code ConfiguredTargetFunction}.
   *
   * <p>Returns null if Skyframe deps are missing or upon certain errors.
   */
@Nullable
public final ConfiguredTarget createConfiguredTarget(AnalysisEnvironment analysisEnvironment, ArtifactFactory artifactFactory, Target target, BuildConfiguration config, BuildConfiguration hostConfig, OrderedSetMultimap<Attribute, ConfiguredTarget> prerequisiteMap, ImmutableMap<Label, ConfigMatchingProvider> configConditions) throws InterruptedException {
    if (target instanceof Rule) {
        return createRule(analysisEnvironment, (Rule) target, config, hostConfig, prerequisiteMap, configConditions);
    }
    // Visibility, like all package groups, doesn't have a configuration
    NestedSet<PackageSpecification> visibility = convertVisibility(prerequisiteMap, analysisEnvironment.getEventHandler(), target, null);
    TargetContext targetContext = new TargetContext(analysisEnvironment, target, config, prerequisiteMap.get(null), visibility);
    if (target instanceof OutputFile) {
        OutputFile outputFile = (OutputFile) target;
        boolean isFileset = outputFile.getGeneratingRule().getRuleClass().equals("Fileset");
        Artifact artifact = getOutputArtifact(outputFile, config, isFileset, artifactFactory);
        TransitiveInfoCollection rule = targetContext.findDirectPrerequisite(outputFile.getGeneratingRule().getLabel(), config);
        if (isFileset) {
            return new FilesetOutputConfiguredTarget(targetContext, outputFile, rule, artifact, rule.getProvider(FilesetProvider.class).getTraversals());
        } else {
            return new OutputFileConfiguredTarget(targetContext, outputFile, rule, artifact);
        }
    } else if (target instanceof InputFile) {
        InputFile inputFile = (InputFile) target;
        Artifact artifact = artifactFactory.getSourceArtifact(inputFile.getExecPath(), Root.asSourceRoot(inputFile.getPackage().getSourceRoot(), inputFile.getPackage().getPackageIdentifier().getRepository().isMain()), new ConfiguredTargetKey(target.getLabel(), config));
        return new InputFileConfiguredTarget(targetContext, inputFile, artifact);
    } else if (target instanceof PackageGroup) {
        PackageGroup packageGroup = (PackageGroup) target;
        return new PackageGroupConfiguredTarget(targetContext, packageGroup);
    } else if (target instanceof EnvironmentGroup) {
        return new EnvironmentGroupConfiguredTarget(targetContext, (EnvironmentGroup) target);
    } else {
        throw new AssertionError("Unexpected target class: " + target.getClass().getName());
    }
}
Also used : OutputFile(com.google.devtools.build.lib.packages.OutputFile) Artifact(com.google.devtools.build.lib.actions.Artifact) PackageGroup(com.google.devtools.build.lib.packages.PackageGroup) InputFile(com.google.devtools.build.lib.packages.InputFile) EnvironmentGroup(com.google.devtools.build.lib.packages.EnvironmentGroup) Rule(com.google.devtools.build.lib.packages.Rule) ConfiguredTargetKey(com.google.devtools.build.lib.skyframe.ConfiguredTargetKey) PackageSpecification(com.google.devtools.build.lib.packages.PackageSpecification) Nullable(javax.annotation.Nullable)

Aggregations

Rule (com.google.devtools.build.lib.packages.Rule)79 Test (org.junit.Test)27 Label (com.google.devtools.build.lib.cmdline.Label)26 Attribute (com.google.devtools.build.lib.packages.Attribute)20 Target (com.google.devtools.build.lib.packages.Target)19 Nullable (javax.annotation.Nullable)10 RawAttributeMapper (com.google.devtools.build.lib.packages.RawAttributeMapper)9 PathFragment (com.google.devtools.build.lib.vfs.PathFragment)9 OutputFile (com.google.devtools.build.lib.packages.OutputFile)8 BuildConfiguration (com.google.devtools.build.lib.analysis.config.BuildConfiguration)7 NoSuchThingException (com.google.devtools.build.lib.packages.NoSuchThingException)7 SkyKey (com.google.devtools.build.skyframe.SkyKey)7 ImmutableList (com.google.common.collect.ImmutableList)6 InputFile (com.google.devtools.build.lib.packages.InputFile)6 IOException (java.io.IOException)6 LinkedHashSet (java.util.LinkedHashSet)6 AggregatingAttributeMapper (com.google.devtools.build.lib.packages.AggregatingAttributeMapper)5 Package (com.google.devtools.build.lib.packages.Package)5 Artifact (com.google.devtools.build.lib.actions.Artifact)4 ConfiguredTarget (com.google.devtools.build.lib.analysis.ConfiguredTarget)4