// Copyright 2014 The Bazel Authors. All rights reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package com.google.devtools.build.lib.packages; import com.google.common.annotations.VisibleForTesting; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSortedSet; import com.google.common.collect.Iterables; import com.google.common.collect.Lists; import com.google.common.collect.Maps; import com.google.common.collect.Sets; import com.google.devtools.build.lib.cmdline.Label; import com.google.devtools.build.lib.cmdline.LabelSyntaxException; import com.google.devtools.build.lib.cmdline.PackageIdentifier; import com.google.devtools.build.lib.collect.CollectionUtils; import com.google.devtools.build.lib.collect.ImmutableSortedKeyMap; import com.google.devtools.build.lib.events.Event; import com.google.devtools.build.lib.events.EventHandler; import com.google.devtools.build.lib.events.Location; import com.google.devtools.build.lib.packages.AttributeMap.AcceptsLabelAttribute; import com.google.devtools.build.lib.packages.License.DistributionType; import com.google.devtools.build.lib.util.Preconditions; import com.google.devtools.build.lib.util.SpellChecker; import com.google.devtools.build.lib.vfs.Canonicalizer; import com.google.devtools.build.lib.vfs.Path; import com.google.devtools.build.lib.vfs.PathFragment; import java.io.PrintStream; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; import javax.annotation.Nullable; /** * A package, which is a container of {@link Rule}s, each of * which contains a dictionary of named attributes. * *

Package instances are intended to be immutable and for all practical * purposes can be treated as such. Note, however, that some member variables * exposed via the public interface are not strictly immutable, so until their * types are guaranteed immutable we're not applying the {@code @Immutable} * annotation here. */ public class Package { /** * Common superclass for all name-conflict exceptions. */ public static class NameConflictException extends Exception { protected NameConflictException(String message) { super(message); } } /** * The repository identifier for this package. */ private final PackageIdentifier packageIdentifier; /** * The name of the package, e.g. "foo/bar". */ protected final String name; /** * Like name, but in the form of a PathFragment. */ private final PathFragment nameFragment; /** * The filename of this package's BUILD file. */ protected Path filename; /** * The directory in which this package's BUILD file resides. All InputFile * members of the packages are located relative to this directory. */ private Path packageDirectory; /** * The name of the workspace this package is in. Used as a prefix for the runfiles directory. * This can be set in the WORKSPACE file. This must be a valid target name. */ protected String workspaceName; /** * The root of the source tree in which this package was found. It is an invariant that * {@code sourceRoot.getRelative(packageId.getPathFragment()).equals(packageDirectory)}. */ private Path sourceRoot; /** * The "Make" environment of this package, containing package-local * definitions of "Make" variables. */ private MakeEnvironment makeEnv; /** * The collection of all targets defined in this package, indexed by name. */ protected Map targets; /** * Default visibility for rules that do not specify it. */ private RuleVisibility defaultVisibility; private boolean defaultVisibilitySet; /** * Default package-level 'testonly' value for rules that do not specify it. */ private boolean defaultTestOnly = false; /** * Default package-level 'deprecation' value for rules that do not specify it. */ private String defaultDeprecation; /** * Default header strictness checking for rules that do not specify it. */ private String defaultHdrsCheck; /** * Default copts for cc_* rules. The rules' individual copts will append to * this value. */ private ImmutableList defaultCopts; /** * The InputFile target corresponding to this package's BUILD file. */ private InputFile buildFile; /** * True iff this package's BUILD files contained lexical or grammatical * errors, or experienced errors during evaluation, or semantic errors during * the construction of any rule. * *

Note: A package containing errors does not necessarily prevent a build; * if all the rules needed for a given build were constructed prior to the * first error, the build may proceed. */ private boolean containsErrors; /** * The set of labels subincluded by this package. */ private Set

As part of initialization, {@link Builder} constructs {@link InputFile} * and {@link PackageGroup} instances that require a valid Package instance where * {@link Package#getNameFragment()} is accessible. That's why these settings are * applied here at the start. * * @precondition {@code name} must be a suffix of * {@code filename.getParentDirectory())}. */ protected Package(PackageIdentifier packageId, String runfilesPrefix) { this.packageIdentifier = packageId; this.workspaceName = runfilesPrefix; this.nameFragment = Canonicalizer.fragments().intern(packageId.getPackageFragment()); this.name = nameFragment.getPathString(); } /** Returns this packages' identifier. */ public PackageIdentifier getPackageIdentifier() { return packageIdentifier; } /** * Package initialization: part 2 of 3: sets this package's default header * strictness checking. * *

This is needed to support C++-related rule classes * which accesses {@link #getDefaultHdrsCheck} from the still-under-construction * package. */ protected void setDefaultHdrsCheck(String defaultHdrsCheck) { this.defaultHdrsCheck = defaultHdrsCheck; } /** * Set the default 'testonly' value for this package. */ protected void setDefaultTestOnly(boolean testOnly) { defaultTestOnly = testOnly; } /** * Set the default 'deprecation' value for this package. */ protected void setDefaultDeprecation(String deprecation) { defaultDeprecation = deprecation; } /** * Sets the default value to use for a rule's {@link RuleClass#COMPATIBLE_ENVIRONMENT_ATTR} * attribute when not explicitly specified by the rule. */ protected void setDefaultCompatibleWith(Set

Only after this method is called can this package be considered "complete" * and be shared publicly. */ protected void finishInit(Builder builder) { // If any error occurred during evaluation of this package, consider all // rules in the package to be "in error" also (even if they were evaluated // prior to the error). This behaviour is arguably stricter than need be, // but stopping a build only for some errors but not others creates user // confusion. if (builder.containsErrors) { for (Rule rule : builder.getTargets(Rule.class)) { rule.setContainsErrors(); } } this.filename = builder.getFilename(); this.packageDirectory = filename.getParentDirectory(); this.sourceRoot = getSourceRoot(filename, packageIdentifier.getPathFragment()); if ((sourceRoot == null || !sourceRoot.getRelative(packageIdentifier.getPathFragment()).equals(packageDirectory)) && !filename.getBaseName().equals("WORKSPACE")) { throw new IllegalArgumentException( "Invalid BUILD file name for package '" + packageIdentifier + "': " + filename); } this.makeEnv = builder.makeEnv.build(); this.targets = ImmutableSortedKeyMap.copyOf(builder.targets); this.defaultVisibility = builder.defaultVisibility; this.defaultVisibilitySet = builder.defaultVisibilitySet; if (builder.defaultCopts == null) { this.defaultCopts = ImmutableList.of(); } else { this.defaultCopts = ImmutableList.copyOf(builder.defaultCopts); } this.buildFile = builder.buildFile; this.containsErrors = builder.containsErrors; this.subincludes = builder.subincludes.keySet(); this.skylarkFileDependencies = builder.skylarkFileDependencies; this.defaultLicense = builder.defaultLicense; this.defaultDistributionSet = builder.defaultDistributionSet; this.features = ImmutableSortedSet.copyOf(builder.features); this.events = ImmutableList.copyOf(builder.events); } /** * Returns the list of subincluded labels on which the validity of this package depends. */ public Set

Assumes invariant: * {@code getSourceRoot().getRelative(packageId.getPathFragment()).equals(getPackageDirectory())} */ public Path getSourceRoot() { return sourceRoot; } /** * Returns the directory containing the package's BUILD file. */ public Path getPackageDirectory() { return packageDirectory; } /** * Returns the name of this package. If this build is using external repositories then this name * may not be unique! */ public String getName() { return name; } /** * Like {@link #getName}, but has type {@code PathFragment}. */ public PathFragment getNameFragment() { return nameFragment; } /** * Returns the "Make" value from the package's make environment whose name * is "varname", or null iff the variable is not defined in the environment. */ public String lookupMakeVariable(String varname, String platform) { return makeEnv.lookup(varname, platform); } /** * Returns the make environment. This should only ever be used for serialization -- how the * make variables are implemented is an implementation detail. */ MakeEnvironment getMakeEnvironment() { return makeEnv; } /** * Returns all make variables for a given platform. */ public Map getAllMakeVariables(String platform) { ImmutableMap.Builder map = ImmutableMap.builder(); for (String var : makeEnv.getBindings().keySet()) { map.put(var, makeEnv.lookup(var, platform)); } return map.build(); } /** * Returns the label of this package's BUILD file. * *

Typically getBuildFileLabel().getName().equals("BUILD") -- * though not necessarily: data in a subdirectory of a test package may use a * different filename to avoid inadvertently creating a new package. */ public Label getBuildFileLabel() { return buildFile.getLabel(); } /** * Returns the InputFile target for this package's BUILD file. */ public InputFile getBuildFile() { return buildFile; } /** * Returns true if errors were encountered during evaluation of this package. * (The package may be incomplete and its contents should not be relied upon * for critical operations. However, any Rules belonging to the package are * guaranteed to be intact, unless their containsErrors() flag * is set.) */ public boolean containsErrors() { return containsErrors; } public List getEvents() { return events; } /** * Returns an (immutable, unordered) view of all the targets belonging to this package. */ public Collection getTargets() { return getTargets(targets); } /** * Common getTargets implementation, accessible by both {@link Package} and * {@link Package.Builder}. */ private static Collection getTargets(Map targetMap) { return Collections.unmodifiableCollection(targetMap.values()); } /** * Returns a (read-only, unordered) iterator of all the targets belonging * to this package which are instances of the specified class. */ public Iterable getTargets(Class targetClass) { return getTargets(targets, targetClass); } /** * Common getTargets implementation, accessible by both {@link Package} and * {@link Package.Builder}. */ private static Iterable getTargets(Map targetMap, Class targetClass) { return Iterables.filter(targetMap.values(), targetClass); } /** * Returns the rule that corresponds to a particular BUILD target name. Useful * for walking through the dependency graph of a target. * Fails if the target is not a Rule. */ @VisibleForTesting // Should be package-private public Rule getRule(String targetName) { return (Rule) targets.get(targetName); } /** * Returns this package's workspace name. * *

Package-private to encourage callers to get their workspace name from a rule, not a * package.

*/ public String getWorkspaceName() { return workspaceName; } /** * Returns the features specified in the package() declaration. */ public ImmutableSet getFeatures() { return features; } /** * Returns the target (a member of this package) whose name is "targetName". * First rules are searched, then output files, then input files. The target * name must be valid, as defined by {@code LabelValidator#validateTargetName}. * * @throws NoSuchTargetException if the specified target was not found. */ public Target getTarget(String targetName) throws NoSuchTargetException { Target target = targets.get(targetName); if (target != null) { return target; } // No such target. // If there's a file on the disk that's not mentioned in the BUILD file, // produce a more informative error. NOTE! this code path is only executed // on failure, which is (relatively) very rare. In the common case no // stat(2) is executed. Path filename = getPackageDirectory().getRelative(targetName); String suffix; if (!new PathFragment(targetName).isNormalized()) { // Don't check for file existence in this case because the error message // would be confusing and wrong. If the targetName is "foo/bar/.", and // there is a directory "foo/bar", it doesn't mean that "//pkg:foo/bar/." // is a valid label. suffix = ""; } else if (filename.isDirectory()) { suffix = "; however, a source directory of this name exists. (Perhaps add " + "'exports_files([\"" + targetName + "\"])' to " + name + "/BUILD, or define a " + "filegroup?)"; } else if (filename.exists()) { suffix = "; however, a source file of this name exists. (Perhaps add " + "'exports_files([\"" + targetName + "\"])' to " + name + "/BUILD?)"; } else { String suggestion = SpellChecker.suggest(targetName, targets.keySet()); if (suggestion != null) { suffix = " (did you mean '" + suggestion + "'?)"; } else { suffix = ""; } } throw makeNoSuchTargetException(targetName, suffix); } protected NoSuchTargetException makeNoSuchTargetException(String targetName, String suffix) { Label label; try { label = createLabel(targetName); } catch (LabelSyntaxException e) { throw new IllegalArgumentException(targetName); } String msg = String.format( "target '%s' not declared in package '%s'%s defined by %s", targetName, name, suffix, filename); return new NoSuchTargetException(label, msg); } /** * Creates a label for a target inside this package. * * @throws LabelSyntaxException if the {@code targetName} is invalid */ public Label createLabel(String targetName) throws LabelSyntaxException { return Label.create(packageIdentifier, targetName); } /** * Returns the default visibility for this package. */ public RuleVisibility getDefaultVisibility() { return defaultVisibility; } /** * Returns the default testonly value. */ public Boolean getDefaultTestOnly() { return defaultTestOnly; } /** * Returns the default deprecation value. */ public String getDefaultDeprecation() { return defaultDeprecation; } /** * Gets the default header checking mode. */ public String getDefaultHdrsCheck() { return defaultHdrsCheck != null ? defaultHdrsCheck : "strict"; } /** * Returns the default copts value, to which rules should append their * specific copts. */ public ImmutableList getDefaultCopts() { return defaultCopts; } /** * Returns whether the default header checking mode has been set or it is the * default value. */ public boolean isDefaultHdrsCheckSet() { return defaultHdrsCheck != null; } public boolean isDefaultVisibilitySet() { return defaultVisibilitySet; } /** * Gets the parsed license object for the default license * declared by this package. */ public License getDefaultLicense() { return defaultLicense; } /** * Returns the parsed set of distributions declared as the default for this * package. */ public Set getDefaultDistribs() { return defaultDistributionSet; } /** * Returns the default value to use for a rule's {@link RuleClass#COMPATIBLE_ENVIRONMENT_ATTR} * attribute when not explicitly specified by the rule. */ public Set