Search in sources :

Example 1 with InputFile

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

the class XmlOutputFormatter method createTargetElement.

/**
   * Creates and returns a new DOM tree for the specified build target.
   *
   * XML structure:
   * - element tag is <source-file>, <generated-file> or <rule
   *   class="cc_library">, following the terminology of
   *   {@link Target#getTargetKind()}.
   * - 'name' attribute is target's label.
   * - 'location' attribute is consistent with output of --output location.
   * - rule attributes are represented in the DOM structure.
   * @throws InterruptedException
   */
private Element createTargetElement(Document doc, Target target) throws InterruptedException {
    Element elem;
    if (target instanceof Rule) {
        Rule rule = (Rule) target;
        elem = doc.createElement("rule");
        elem.setAttribute("class", rule.getRuleClass());
        for (Attribute attr : rule.getAttributes()) {
            PossibleAttributeValues values = getPossibleAttributeValues(rule, attr);
            if (values.source == AttributeValueSource.RULE || options.xmlShowDefaultValues) {
                Element attrElem = createValueElement(doc, attr.getType(), values);
                attrElem.setAttribute("name", attr.getName());
                elem.appendChild(attrElem);
            }
        }
        // host-configuration outputs, and default values.
        for (Label label : rule.getLabels(dependencyFilter)) {
            Element inputElem = doc.createElement("rule-input");
            inputElem.setAttribute("name", label.toString());
            elem.appendChild(inputElem);
        }
        for (Label label : aspectResolver.computeAspectDependencies(target, dependencyFilter).values()) {
            Element inputElem = doc.createElement("rule-input");
            inputElem.setAttribute("name", label.toString());
            elem.appendChild(inputElem);
        }
        for (OutputFile outputFile : rule.getOutputFiles()) {
            Element outputElem = doc.createElement("rule-output");
            outputElem.setAttribute("name", outputFile.getLabel().toString());
            elem.appendChild(outputElem);
        }
        for (String feature : rule.getFeatures()) {
            Element outputElem = doc.createElement("rule-default-setting");
            outputElem.setAttribute("name", feature);
            elem.appendChild(outputElem);
        }
    } else if (target instanceof PackageGroup) {
        PackageGroup packageGroup = (PackageGroup) target;
        elem = doc.createElement("package-group");
        elem.setAttribute("name", packageGroup.getName());
        Element includes = createValueElement(doc, BuildType.LABEL_LIST, packageGroup.getIncludes());
        includes.setAttribute("name", "includes");
        elem.appendChild(includes);
        Element packages = createValueElement(doc, Type.STRING_LIST, packageGroup.getContainedPackages());
        packages.setAttribute("name", "packages");
        elem.appendChild(packages);
    } else if (target instanceof OutputFile) {
        OutputFile outputFile = (OutputFile) target;
        elem = doc.createElement("generated-file");
        elem.setAttribute("generating-rule", outputFile.getGeneratingRule().getLabel().toString());
    } else if (target instanceof InputFile) {
        elem = doc.createElement("source-file");
        InputFile inputFile = (InputFile) target;
        if (inputFile.getName().equals("BUILD")) {
            addSubincludedFilesToElement(doc, elem, inputFile);
            addSkylarkFilesToElement(doc, elem, inputFile);
            addFeaturesToElement(doc, elem, inputFile);
            elem.setAttribute("package_contains_errors", String.valueOf(inputFile.getPackage().containsErrors()));
        }
        addPackageGroupsToElement(doc, elem, inputFile);
    } else if (target instanceof EnvironmentGroup) {
        EnvironmentGroup envGroup = (EnvironmentGroup) target;
        elem = doc.createElement("environment-group");
        elem.setAttribute("name", envGroup.getName());
        Element environments = createValueElement(doc, BuildType.LABEL_LIST, envGroup.getEnvironments());
        environments.setAttribute("name", "environments");
        elem.appendChild(environments);
        Element defaults = createValueElement(doc, BuildType.LABEL_LIST, envGroup.getDefaults());
        defaults.setAttribute("name", "defaults");
        elem.appendChild(defaults);
    } else if (target instanceof FakeSubincludeTarget) {
        elem = doc.createElement("source-file");
    } else {
        throw new IllegalArgumentException(target.toString());
    }
    elem.setAttribute("name", target.getLabel().toString());
    String location = getLocation(target, options.relativeLocations);
    if (!options.xmlLineNumbers) {
        int firstColon = location.indexOf(':');
        if (firstColon != -1) {
            location = location.substring(0, firstColon);
        }
    }
    elem.setAttribute("location", location);
    return elem;
}
Also used : OutputFile(com.google.devtools.build.lib.packages.OutputFile) Attribute(com.google.devtools.build.lib.packages.Attribute) Element(org.w3c.dom.Element) Label(com.google.devtools.build.lib.cmdline.Label) PackageGroup(com.google.devtools.build.lib.packages.PackageGroup) InputFile(com.google.devtools.build.lib.packages.InputFile) EnvironmentGroup(com.google.devtools.build.lib.packages.EnvironmentGroup) FakeSubincludeTarget(com.google.devtools.build.lib.query2.FakeSubincludeTarget) Rule(com.google.devtools.build.lib.packages.Rule)

Example 2 with InputFile

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

the class CppConfigurationLoader method createParameters.

@Nullable
protected CppConfigurationParameters createParameters(ConfigurationEnvironment env, BuildOptions options) throws InvalidConfigurationException, InterruptedException {
    BlazeDirectories directories = env.getBlazeDirectories();
    if (directories == null) {
        return null;
    }
    Label crosstoolTopLabel = RedirectChaser.followRedirects(env, options.get(CppOptions.class).crosstoolTop, "crosstool_top");
    if (crosstoolTopLabel == null) {
        return null;
    }
    CrosstoolConfigurationLoader.CrosstoolFile file = CrosstoolConfigurationLoader.readCrosstool(env, crosstoolTopLabel);
    if (file == null) {
        return null;
    }
    CrosstoolConfig.CToolchain toolchain = CrosstoolConfigurationLoader.selectToolchain(file.getProto(), options, cpuTransformer);
    // FDO
    // TODO(bazel-team): move this to CppConfiguration.prepareHook
    CppOptions cppOptions = options.get(CppOptions.class);
    Path fdoZip;
    if (cppOptions.fdoOptimize == null) {
        fdoZip = null;
    } else if (cppOptions.fdoOptimize.startsWith("//")) {
        try {
            Target target = env.getTarget(Label.parseAbsolute(cppOptions.fdoOptimize));
            if (target == null) {
                return null;
            }
            if (!(target instanceof InputFile)) {
                throw new InvalidConfigurationException("--fdo_optimize cannot accept targets that do not refer to input files");
            }
            fdoZip = env.getPath(target.getPackage(), target.getName());
            if (fdoZip == null) {
                throw new InvalidConfigurationException("The --fdo_optimize parameter you specified resolves to a file that does not exist");
            }
        } catch (NoSuchPackageException | NoSuchTargetException | LabelSyntaxException e) {
            env.getEventHandler().handle(Event.error(e.getMessage()));
            throw new InvalidConfigurationException(e);
        }
    } else {
        fdoZip = directories.getWorkspace().getRelative(cppOptions.fdoOptimize);
        try {
            // We don't check for file existence, but at least the filename should be well-formed.
            FileSystemUtils.checkBaseName(fdoZip.getBaseName());
        } catch (IllegalArgumentException e) {
            throw new InvalidConfigurationException(e);
        }
    }
    Label ccToolchainLabel;
    Target crosstoolTop;
    try {
        crosstoolTop = env.getTarget(crosstoolTopLabel);
    } catch (NoSuchThingException e) {
        // Should have been found out during redirect chasing
        throw new IllegalStateException(e);
    }
    if (crosstoolTop instanceof Rule && ((Rule) crosstoolTop).getRuleClass().equals("cc_toolchain_suite")) {
        Rule ccToolchainSuite = (Rule) crosstoolTop;
        ccToolchainLabel = NonconfigurableAttributeMapper.of(ccToolchainSuite).get("toolchains", BuildType.LABEL_DICT_UNARY).get(toolchain.getTargetCpu() + "|" + toolchain.getCompiler());
        if (ccToolchainLabel == null) {
            throw new InvalidConfigurationException(String.format("cc_toolchain_suite '%s' does not contain a toolchain for CPU '%s' and compiler '%s'", crosstoolTopLabel, toolchain.getTargetCpu(), toolchain.getCompiler()));
        }
    } else {
        throw new InvalidConfigurationException(String.format("The specified --crosstool_top '%s' is not a valid cc_toolchain_suite rule", crosstoolTopLabel));
    }
    Target ccToolchain;
    try {
        ccToolchain = env.getTarget(ccToolchainLabel);
        if (ccToolchain == null) {
            return null;
        }
    } catch (NoSuchThingException e) {
        throw new InvalidConfigurationException(String.format("The toolchain rule '%s' does not exist", ccToolchainLabel));
    }
    if (!(ccToolchain instanceof Rule) || !CcToolchainRule.isCcToolchain(ccToolchain)) {
        throw new InvalidConfigurationException(String.format("The label '%s' is not a cc_toolchain rule", ccToolchainLabel));
    }
    return new CppConfigurationParameters(toolchain, file.getMd5(), options, fdoZip, crosstoolTopLabel, ccToolchainLabel);
}
Also used : Path(com.google.devtools.build.lib.vfs.Path) Label(com.google.devtools.build.lib.cmdline.Label) InputFile(com.google.devtools.build.lib.packages.InputFile) InvalidConfigurationException(com.google.devtools.build.lib.analysis.config.InvalidConfigurationException) BlazeDirectories(com.google.devtools.build.lib.analysis.BlazeDirectories) Target(com.google.devtools.build.lib.packages.Target) NoSuchThingException(com.google.devtools.build.lib.packages.NoSuchThingException) Rule(com.google.devtools.build.lib.packages.Rule) CrosstoolConfig(com.google.devtools.build.lib.view.config.crosstool.CrosstoolConfig) Nullable(javax.annotation.Nullable)

Example 3 with InputFile

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

the class BuildTool method validateLicensingForTargets.

/**
   * Takes a set of configured targets, and checks if the distribution methods
   * declared for the targets are compatible with the constraints imposed by
   * their prerequisites' licenses.
   *
   * @param configuredTargets the targets to check
   * @param keepGoing if false, and a licensing error is encountered, both
   *        generates an error message on the reporter, <em>and</em> throws an
   *        exception. If true, then just generates a message on the reporter.
   * @throws ViewCreationFailedException if the license checking failed (and not
   *         --keep_going)
   */
private void validateLicensingForTargets(Iterable<ConfiguredTarget> configuredTargets, boolean keepGoing) throws ViewCreationFailedException {
    for (ConfiguredTarget configuredTarget : configuredTargets) {
        final Target target = configuredTarget.getTarget();
        if (TargetUtils.isTestRule(target)) {
            // Tests are exempt from license checking
            continue;
        }
        final Set<DistributionType> distribs = target.getDistributions();
        StaticallyLinkedMarkerProvider markerProvider = configuredTarget.getProvider(StaticallyLinkedMarkerProvider.class);
        boolean staticallyLinked = markerProvider != null && markerProvider.isLinkedStatically();
        LicensesProvider provider = configuredTarget.getProvider(LicensesProvider.class);
        if (provider != null) {
            NestedSet<TargetLicense> licenses = provider.getTransitiveLicenses();
            for (TargetLicense targetLicense : licenses) {
                if (!targetLicense.getLicense().checkCompatibility(distribs, target, targetLicense.getLabel(), getReporter(), staticallyLinked)) {
                    if (!keepGoing) {
                        throw new ViewCreationFailedException("Build aborted due to licensing error");
                    }
                }
            }
        } else if (configuredTarget.getTarget() instanceof InputFile) {
            // Input file targets do not provide licenses because they do not
            // depend on the rule where their license is taken from. This is usually
            // not a problem, because the transitive collection of licenses always
            // hits the rule they come from, except when the input file is a
            // top-level target. Thus, we need to handle that case specially here.
            //
            // See FileTarget#getLicense for more information about the handling of
            // license issues with File targets.
            License license = configuredTarget.getTarget().getLicense();
            if (!license.checkCompatibility(distribs, target, configuredTarget.getLabel(), getReporter(), staticallyLinked)) {
                if (!keepGoing) {
                    throw new ViewCreationFailedException("Build aborted due to licensing error");
                }
            }
        }
    }
}
Also used : ConfiguredTarget(com.google.devtools.build.lib.analysis.ConfiguredTarget) Target(com.google.devtools.build.lib.packages.Target) ViewCreationFailedException(com.google.devtools.build.lib.analysis.ViewCreationFailedException) TargetLicense(com.google.devtools.build.lib.analysis.LicensesProvider.TargetLicense) TargetLicense(com.google.devtools.build.lib.analysis.LicensesProvider.TargetLicense) License(com.google.devtools.build.lib.packages.License) LicensesProvider(com.google.devtools.build.lib.analysis.LicensesProvider) ConfiguredTarget(com.google.devtools.build.lib.analysis.ConfiguredTarget) StaticallyLinkedMarkerProvider(com.google.devtools.build.lib.analysis.StaticallyLinkedMarkerProvider) DistributionType(com.google.devtools.build.lib.packages.License.DistributionType) InputFile(com.google.devtools.build.lib.packages.InputFile)

Example 4 with InputFile

use of com.google.devtools.build.lib.packages.InputFile 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 5 with InputFile

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

InputFile (com.google.devtools.build.lib.packages.InputFile)7 Rule (com.google.devtools.build.lib.packages.Rule)6 OutputFile (com.google.devtools.build.lib.packages.OutputFile)5 PackageGroup (com.google.devtools.build.lib.packages.PackageGroup)5 Label (com.google.devtools.build.lib.cmdline.Label)4 EnvironmentGroup (com.google.devtools.build.lib.packages.EnvironmentGroup)4 Target (com.google.devtools.build.lib.packages.Target)4 Attribute (com.google.devtools.build.lib.packages.Attribute)3 FakeSubincludeTarget (com.google.devtools.build.lib.query2.FakeSubincludeTarget)2 Nullable (javax.annotation.Nullable)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Artifact (com.google.devtools.build.lib.actions.Artifact)1 BlazeDirectories (com.google.devtools.build.lib.analysis.BlazeDirectories)1 ConfiguredTarget (com.google.devtools.build.lib.analysis.ConfiguredTarget)1 LicensesProvider (com.google.devtools.build.lib.analysis.LicensesProvider)1 TargetLicense (com.google.devtools.build.lib.analysis.LicensesProvider.TargetLicense)1 StaticallyLinkedMarkerProvider (com.google.devtools.build.lib.analysis.StaticallyLinkedMarkerProvider)1 ViewCreationFailedException (com.google.devtools.build.lib.analysis.ViewCreationFailedException)1 BuildConfiguration (com.google.devtools.build.lib.analysis.config.BuildConfiguration)1 InvalidConfigurationException (com.google.devtools.build.lib.analysis.config.InvalidConfigurationException)1