Search in sources :

Example 16 with Package

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

the class TestSuiteExpansionFunction method labelsToTargets.

static ResolvedTargets<Target> labelsToTargets(Environment env, ImmutableSet<Label> labels, boolean hasError) throws InterruptedException {
    Set<PackageIdentifier> pkgIdentifiers = new LinkedHashSet<>();
    for (Label label : labels) {
        pkgIdentifiers.add(label.getPackageIdentifier());
    }
    // Don't bother to check for exceptions - the incoming list should only contain valid targets.
    Map<SkyKey, SkyValue> packages = env.getValues(PackageValue.keys(pkgIdentifiers));
    if (env.valuesMissing()) {
        return null;
    }
    ResolvedTargets.Builder<Target> builder = ResolvedTargets.builder();
    builder.mergeError(hasError);
    Map<PackageIdentifier, Package> packageMap = new HashMap<>();
    for (Entry<SkyKey, SkyValue> entry : packages.entrySet()) {
        packageMap.put((PackageIdentifier) entry.getKey().argument(), ((PackageValue) entry.getValue()).getPackage());
    }
    for (Label label : labels) {
        Package pkg = packageMap.get(label.getPackageIdentifier());
        if (pkg == null) {
            continue;
        }
        try {
            builder.add(pkg.getTarget(label.getName()));
            if (pkg.containsErrors()) {
                builder.setError();
            }
        } catch (NoSuchTargetException e) {
            builder.setError();
        }
    }
    return builder.build();
}
Also used : LinkedHashSet(java.util.LinkedHashSet) SkyKey(com.google.devtools.build.skyframe.SkyKey) HashMap(java.util.HashMap) Label(com.google.devtools.build.lib.cmdline.Label) SkyValue(com.google.devtools.build.skyframe.SkyValue) Target(com.google.devtools.build.lib.packages.Target) PackageIdentifier(com.google.devtools.build.lib.cmdline.PackageIdentifier) ResolvedTargets(com.google.devtools.build.lib.cmdline.ResolvedTargets) NoSuchTargetException(com.google.devtools.build.lib.packages.NoSuchTargetException) Package(com.google.devtools.build.lib.packages.Package)

Example 17 with Package

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

the class TestsInSuiteFunction method getPrerequisites.

/**
   * Adds the set of targets found in the attribute named {@code attrName}, which must be of label
   * list type, of the {@code test_suite} rule named {@code testSuite}. Returns true if the method
   * found a problem during the lookup process; the actual error message is reported to the
   * environment.
   */
private static boolean getPrerequisites(Environment env, Rule testSuite, String attrName, List<Target> targets) throws InterruptedException {
    List<Label> labels = NonconfigurableAttributeMapper.of(testSuite).get(attrName, BuildType.LABEL_LIST);
    Set<PackageIdentifier> pkgIdentifiers = new LinkedHashSet<>();
    for (Label label : labels) {
        pkgIdentifiers.add(label.getPackageIdentifier());
    }
    Map<SkyKey, ValueOrException<BuildFileNotFoundException>> packages = env.getValuesOrThrow(PackageValue.keys(pkgIdentifiers), BuildFileNotFoundException.class);
    if (env.valuesMissing()) {
        return false;
    }
    boolean hasError = false;
    Map<PackageIdentifier, Package> packageMap = new HashMap<>();
    for (Entry<SkyKey, ValueOrException<BuildFileNotFoundException>> entry : packages.entrySet()) {
        try {
            packageMap.put((PackageIdentifier) entry.getKey().argument(), ((PackageValue) entry.getValue().get()).getPackage());
        } catch (BuildFileNotFoundException e) {
            env.getListener().handle(Event.error(e.getMessage()));
            hasError = true;
        }
    }
    for (Label label : labels) {
        Package pkg = packageMap.get(label.getPackageIdentifier());
        if (pkg == null) {
            continue;
        }
        try {
            targets.add(pkg.getTarget(label.getName()));
        } catch (NoSuchTargetException e) {
            env.getListener().handle(Event.error(e.getMessage()));
            hasError = true;
        }
    }
    return hasError;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) SkyKey(com.google.devtools.build.skyframe.SkyKey) BuildFileNotFoundException(com.google.devtools.build.lib.packages.BuildFileNotFoundException) HashMap(java.util.HashMap) Label(com.google.devtools.build.lib.cmdline.Label) ValueOrException(com.google.devtools.build.skyframe.ValueOrException) PackageIdentifier(com.google.devtools.build.lib.cmdline.PackageIdentifier) NoSuchTargetException(com.google.devtools.build.lib.packages.NoSuchTargetException) Package(com.google.devtools.build.lib.packages.Package)

Example 18 with Package

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

the class TargetMarkerFunction method computeTargetMarkerValue.

@Nullable
static TargetMarkerValue computeTargetMarkerValue(SkyKey key, Environment env) throws NoSuchTargetException, NoSuchPackageException, InterruptedException {
    Label label = (Label) key.argument();
    PathFragment pkgForLabel = label.getPackageFragment();
    if (label.getName().contains("/")) {
        // This target is in a subdirectory, therefore it could potentially be invalidated by
        // a new BUILD file appearing in the hierarchy.
        PathFragment containingDirectory = label.toPathFragment().getParentDirectory();
        ContainingPackageLookupValue containingPackageLookupValue;
        try {
            PackageIdentifier newPkgId = PackageIdentifier.create(label.getPackageIdentifier().getRepository(), containingDirectory);
            containingPackageLookupValue = (ContainingPackageLookupValue) env.getValueOrThrow(ContainingPackageLookupValue.key(newPkgId), BuildFileNotFoundException.class, InconsistentFilesystemException.class);
        } catch (InconsistentFilesystemException e) {
            throw new NoSuchTargetException(label, e.getMessage());
        }
        if (containingPackageLookupValue == null) {
            return null;
        }
        if (!containingPackageLookupValue.hasContainingPackage()) {
            // trying to build the target for label 'a:b/foo'.
            throw new BuildFileNotFoundException(label.getPackageIdentifier(), "BUILD file not found on package path for '" + pkgForLabel.getPathString() + "'");
        }
        if (!containingPackageLookupValue.getContainingPackageName().equals(label.getPackageIdentifier())) {
            throw new NoSuchTargetException(label, String.format("Label '%s' crosses boundary of subpackage '%s'", label, containingPackageLookupValue.getContainingPackageName()));
        }
    }
    SkyKey pkgSkyKey = PackageValue.key(label.getPackageIdentifier());
    PackageValue value = (PackageValue) env.getValueOrThrow(pkgSkyKey, NoSuchPackageException.class);
    if (value == null) {
        return null;
    }
    Package pkg = value.getPackage();
    Target target = pkg.getTarget(label.getName());
    if (pkg.containsErrors()) {
        // if one of its targets was in error).
        throw new NoSuchTargetException(target);
    }
    return TargetMarkerValue.TARGET_MARKER_INSTANCE;
}
Also used : SkyKey(com.google.devtools.build.skyframe.SkyKey) BuildFileNotFoundException(com.google.devtools.build.lib.packages.BuildFileNotFoundException) Target(com.google.devtools.build.lib.packages.Target) PackageIdentifier(com.google.devtools.build.lib.cmdline.PackageIdentifier) NoSuchTargetException(com.google.devtools.build.lib.packages.NoSuchTargetException) NoSuchPackageException(com.google.devtools.build.lib.packages.NoSuchPackageException) Label(com.google.devtools.build.lib.cmdline.Label) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) Package(com.google.devtools.build.lib.packages.Package) Nullable(javax.annotation.Nullable)

Example 19 with Package

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

the class PackageFactoryApparatus method evalAndReturnGlobCache.

/**
   * Evaluates the {@code buildFileAST} into a {@link Package}.
   */
public Pair<Package, GlobCache> evalAndReturnGlobCache(String packageName, Path buildFile, BuildFileAST buildFileAST) throws InterruptedException {
    PackageIdentifier packageId = PackageIdentifier.createInMainRepo(packageName);
    GlobCache globCache = new GlobCache(buildFile.getParentDirectory(), packageId, getPackageLocator(), null, TestUtils.getPool(), -1);
    LegacyGlobber globber = PackageFactory.createLegacyGlobber(globCache);
    Package externalPkg = factory.newExternalPackageBuilder(buildFile.getParentDirectory().getRelative("WORKSPACE"), "TESTING").build();
    Builder resultBuilder = factory.evaluateBuildFile(externalPkg.getWorkspaceName(), packageId, buildFileAST, buildFile, globber, ImmutableList.<Event>of(), ConstantRuleVisibility.PUBLIC, false, new MakeEnvironment.Builder(), ImmutableMap.<String, Extension>of(), ImmutableList.<Label>of());
    Package result = resultBuilder.build();
    Event.replayEventsOn(eventHandler, result.getEvents());
    return Pair.of(result, globCache);
}
Also used : PackageIdentifier(com.google.devtools.build.lib.cmdline.PackageIdentifier) LegacyGlobber(com.google.devtools.build.lib.packages.PackageFactory.LegacyGlobber) Builder(com.google.devtools.build.lib.packages.Package.Builder) MakeEnvironment(com.google.devtools.build.lib.packages.MakeEnvironment) Package(com.google.devtools.build.lib.packages.Package) GlobCache(com.google.devtools.build.lib.packages.GlobCache)

Example 20 with Package

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

the class PackageFactoryTestBase method assertGlobProducesError.

protected void assertGlobProducesError(String pattern, boolean errorExpected) throws Exception {
    events.setFailFast(false);
    Package pkg = evaluateGlob(ImmutableList.of(pattern), Collections.<String>emptyList(), false, "").first;
    assertEquals(errorExpected, pkg.containsErrors());
    boolean foundError = false;
    for (Event event : events.collector()) {
        if (event.getMessage().contains("glob")) {
            if (!errorExpected) {
                fail("error not expected for glob pattern " + pattern + ", but got: " + event);
                return;
            }
            foundError = errorExpected;
            break;
        }
    }
    assertEquals(errorExpected, foundError);
}
Also used : Event(com.google.devtools.build.lib.events.Event) Package(com.google.devtools.build.lib.packages.Package)

Aggregations

Package (com.google.devtools.build.lib.packages.Package)61 SkyKey (com.google.devtools.build.skyframe.SkyKey)25 Test (org.junit.Test)20 PackageIdentifier (com.google.devtools.build.lib.cmdline.PackageIdentifier)14 NoSuchPackageException (com.google.devtools.build.lib.packages.NoSuchPackageException)14 Label (com.google.devtools.build.lib.cmdline.Label)13 Path (com.google.devtools.build.lib.vfs.Path)13 RootedPath (com.google.devtools.build.lib.vfs.RootedPath)13 Target (com.google.devtools.build.lib.packages.Target)12 NoSuchTargetException (com.google.devtools.build.lib.packages.NoSuchTargetException)11 Nullable (javax.annotation.Nullable)10 BuildFileContainsErrorsException (com.google.devtools.build.lib.packages.BuildFileContainsErrorsException)8 ImmutableMap (com.google.common.collect.ImmutableMap)7 ConfiguredTarget (com.google.devtools.build.lib.analysis.ConfiguredTarget)5 BuildFileNotFoundException (com.google.devtools.build.lib.packages.BuildFileNotFoundException)5 LinkedHashSet (java.util.LinkedHashSet)5 LabelSyntaxException (com.google.devtools.build.lib.cmdline.LabelSyntaxException)4 SkyValue (com.google.devtools.build.skyframe.SkyValue)4 InvalidConfigurationException (com.google.devtools.build.lib.analysis.config.InvalidConfigurationException)3 Attribute (com.google.devtools.build.lib.packages.Attribute)3