Search in sources :

Example 1 with PackageIdentifier

use of com.google.devtools.build.lib.cmdline.PackageIdentifier in project bazel by bazelbuild.

the class SkyQueryEnvironment method bulkGetPackages.

@ThreadSafe
public Map<PackageIdentifier, Package> bulkGetPackages(Iterable<PackageIdentifier> pkgIds) throws InterruptedException {
    Set<SkyKey> pkgKeys = ImmutableSet.copyOf(PackageValue.keys(pkgIds));
    ImmutableMap.Builder<PackageIdentifier, Package> pkgResults = ImmutableMap.builder();
    Map<SkyKey, SkyValue> packages = graph.getSuccessfulValues(pkgKeys);
    for (Map.Entry<SkyKey, SkyValue> pkgEntry : packages.entrySet()) {
        PackageIdentifier pkgId = (PackageIdentifier) pkgEntry.getKey().argument();
        PackageValue pkgValue = (PackageValue) pkgEntry.getValue();
        pkgResults.put(pkgId, Preconditions.checkNotNull(pkgValue.getPackage(), pkgId));
    }
    return pkgResults.build();
}
Also used : SkyKey(com.google.devtools.build.skyframe.SkyKey) SkyValue(com.google.devtools.build.skyframe.SkyValue) PackageIdentifier(com.google.devtools.build.lib.cmdline.PackageIdentifier) PackageValue(com.google.devtools.build.lib.skyframe.PackageValue) Package(com.google.devtools.build.lib.packages.Package) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) ImmutableMap(com.google.common.collect.ImmutableMap) ThreadSafe(com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe)

Example 2 with PackageIdentifier

use of com.google.devtools.build.lib.cmdline.PackageIdentifier in project bazel by bazelbuild.

the class PackageSpecification method fromString.

/**
   * Parses the provided {@link String} into a {@link PackageSpecification}.
   *
   * <p>The {@link String} must have one of the following forms:
   *
   * <ul>
   * <li>The full name of a single package, without repository qualification, prefixed with "//"
   *     (e.g. "//foo/bar"). This results in a {@link PackageSpecification} that contains exactly
   *     the named package.
   * <li>The full name of a single package, without repository qualification, prefixed with "//",
   *     and suffixed with "/..." (e.g. "//foo/bar/...") This results in a {@link
   *     PackageSpecification} that contains all transitive subpackages of that package, inclusive.
   * <li>Exactly "//...". This results in a {@link PackageSpecification} that contains all packages.
   * </ul>
   *
   * <p>If and only if the {@link String} is one of the first two forms, the returned {@link
   * PackageSpecification} is specific to the provided {@link RepositoryName}. Note that it is not
   * possible to construct a repository-specific {@link PackageSpecification} for all transitive
   * subpackages of the root package (i.e. a repository-specific "//...").
   *
   * <p>Throws {@link InvalidPackageSpecificationException} if the {@link String} cannot be parsed.
   */
public static PackageSpecification fromString(RepositoryName repositoryName, String spec) throws InvalidPackageSpecificationException {
    String result = spec;
    boolean allBeneath = false;
    if (result.endsWith(ALL_BENEATH_SUFFIX)) {
        allBeneath = true;
        result = result.substring(0, result.length() - ALL_BENEATH_SUFFIX.length());
        if (result.equals("/")) {
            // spec was "//...".
            return AllPackages.EVERYTHING;
        }
    }
    if (!spec.startsWith("//")) {
        throw new InvalidPackageSpecificationException(String.format("invalid package name '%s': must start with '//'", spec));
    }
    PackageIdentifier packageId;
    try {
        packageId = PackageIdentifier.parse(result);
    } catch (LabelSyntaxException e) {
        throw new InvalidPackageSpecificationException(String.format("invalid package name '%s': %s", spec, e.getMessage()));
    }
    Verify.verify(packageId.getRepository().isDefault());
    PackageIdentifier packageIdForSpecifiedRepository = PackageIdentifier.create(repositoryName, packageId.getPackageFragment());
    return allBeneath ? new AllPackagesBeneath(packageIdForSpecifiedRepository) : new SinglePackage(packageIdForSpecifiedRepository);
}
Also used : LabelSyntaxException(com.google.devtools.build.lib.cmdline.LabelSyntaxException) PackageIdentifier(com.google.devtools.build.lib.cmdline.PackageIdentifier)

Example 3 with PackageIdentifier

use of com.google.devtools.build.lib.cmdline.PackageIdentifier in project bazel by bazelbuild.

the class SkylarkImports method create.

/**
   * Creates and syntactically validates a {@link SkylarkImports} instance from a string.
   * <p>
   * There four syntactic import variants: Absolute paths, relative paths, absolute labels, and
   * relative labels
   *
   * @throws SkylarkImportSyntaxException if the string is not a valid Skylark import.
   */
public static SkylarkImport create(String importString) throws SkylarkImportSyntaxException {
    if (importString.startsWith("//") || importString.startsWith("@")) {
        // Absolute label.
        Label importLabel;
        try {
            importLabel = Label.parseAbsolute(importString, false);
        } catch (LabelSyntaxException e) {
            throw new SkylarkImportSyntaxException(INVALID_LABEL_PREFIX + e.getMessage());
        }
        String targetName = importLabel.getName();
        if (!targetName.endsWith(".bzl")) {
            throw new SkylarkImportSyntaxException(MUST_HAVE_BZL_EXT_MSG);
        }
        PackageIdentifier packageId = importLabel.getPackageIdentifier();
        if (packageId.equals(Label.EXTERNAL_PACKAGE_IDENTIFIER)) {
            throw new SkylarkImportSyntaxException(EXTERNAL_PKG_NOT_ALLOWED_MSG);
        }
        return new AbsoluteLabelImport(importString, importLabel);
    } else if (importString.startsWith("/")) {
        // Absolute path.
        if (importString.endsWith(".bzl")) {
            throw new SkylarkImportSyntaxException(INVALID_PATH_SYNTAX);
        }
        PathFragment importPath = new PathFragment(importString + ".bzl");
        return new AbsolutePathImport(importString, importPath);
    } else if (importString.startsWith(":")) {
        // Relative label. We require that relative labels use an explicit ':' prefix to distinguish
        // them from relative paths, which have a different semantics.
        String importTarget = importString.substring(1);
        if (!importTarget.endsWith(".bzl")) {
            throw new SkylarkImportSyntaxException(MUST_HAVE_BZL_EXT_MSG);
        }
        String maybeErrMsg = LabelValidator.validateTargetName(importTarget);
        if (maybeErrMsg != null) {
            // Null indicates successful target validation.
            throw new SkylarkImportSyntaxException(INVALID_TARGET_PREFIX + maybeErrMsg);
        }
        return new RelativeLabelImport(importString, importTarget);
    } else {
        // Relative path.
        if (importString.endsWith(".bzl") || importString.contains("/")) {
            throw new SkylarkImportSyntaxException(INVALID_PATH_SYNTAX);
        }
        String importTarget = importString + ".bzl";
        String maybeErrMsg = LabelValidator.validateTargetName(importTarget);
        if (maybeErrMsg != null) {
            // Null indicates successful target validation.
            throw new SkylarkImportSyntaxException(INVALID_FILENAME_PREFIX + maybeErrMsg);
        }
        return new RelativePathImport(importString, importTarget);
    }
}
Also used : LabelSyntaxException(com.google.devtools.build.lib.cmdline.LabelSyntaxException) PackageIdentifier(com.google.devtools.build.lib.cmdline.PackageIdentifier) Label(com.google.devtools.build.lib.cmdline.Label) PathFragment(com.google.devtools.build.lib.vfs.PathFragment)

Example 4 with PackageIdentifier

use of com.google.devtools.build.lib.cmdline.PackageIdentifier in project bazel by bazelbuild.

the class CcCommon method getSystemIncludeDirs.

List<PathFragment> getSystemIncludeDirs() {
    List<PathFragment> result = new ArrayList<>();
    PackageIdentifier packageIdentifier = ruleContext.getLabel().getPackageIdentifier();
    PathFragment packageFragment = packageIdentifier.getPathUnderExecRoot();
    for (String includesAttr : ruleContext.attributes().get("includes", Type.STRING_LIST)) {
        includesAttr = ruleContext.expandMakeVariables("includes", includesAttr);
        if (includesAttr.startsWith("/")) {
            ruleContext.attributeWarning("includes", "ignoring invalid absolute path '" + includesAttr + "'");
            continue;
        }
        PathFragment includesPath = packageFragment.getRelative(includesAttr).normalize();
        if (!includesPath.isNormalized()) {
            ruleContext.attributeError("includes", "Path references a path above the execution root.");
        }
        if (includesPath.segmentCount() == 0) {
            ruleContext.attributeError("includes", "'" + includesAttr + "' resolves to the workspace root, which would allow this rule and all of its " + "transitive dependents to include any file in your workspace. Please include only" + " what you need");
        } else if (!includesPath.startsWith(packageFragment)) {
            ruleContext.attributeWarning("includes", "'" + includesAttr + "' resolves to '" + includesPath + "' not below the relative path of its package '" + packageFragment + "'. This will be an error in the future");
        }
        result.add(includesPath);
        result.add(ruleContext.getConfiguration().getGenfilesFragment().getRelative(includesPath));
    }
    return result;
}
Also used : PackageIdentifier(com.google.devtools.build.lib.cmdline.PackageIdentifier) ArrayList(java.util.ArrayList) PathFragment(com.google.devtools.build.lib.vfs.PathFragment)

Example 5 with PackageIdentifier

use of com.google.devtools.build.lib.cmdline.PackageIdentifier in project bazel by bazelbuild.

the class PackageProgressReceiverTest method testPackageVisible.

@Test
public void testPackageVisible() {
    // If there is only a single package being loaded, it is visible in
    // the activity part of the progress state.
    PackageIdentifier id = PackageIdentifier.createInMainRepo("foo/bar/baz");
    PackageProgressReceiver progress = new PackageProgressReceiver();
    progress.startReadPackage(id);
    String activity = progress.progressState().getSecond();
    assertTrue("Unfinished package '" + id + "' should be visible in activity: " + activity, activity.contains(id.toString()));
}
Also used : PackageIdentifier(com.google.devtools.build.lib.cmdline.PackageIdentifier) Test(org.junit.Test)

Aggregations

PackageIdentifier (com.google.devtools.build.lib.cmdline.PackageIdentifier)49 SkyKey (com.google.devtools.build.skyframe.SkyKey)17 PathFragment (com.google.devtools.build.lib.vfs.PathFragment)16 Package (com.google.devtools.build.lib.packages.Package)14 Label (com.google.devtools.build.lib.cmdline.Label)12 Path (com.google.devtools.build.lib.vfs.Path)11 HashMap (java.util.HashMap)11 Test (org.junit.Test)11 ImmutableMap (com.google.common.collect.ImmutableMap)10 NoSuchPackageException (com.google.devtools.build.lib.packages.NoSuchPackageException)8 BuildFileNotFoundException (com.google.devtools.build.lib.packages.BuildFileNotFoundException)7 Target (com.google.devtools.build.lib.packages.Target)7 Nullable (javax.annotation.Nullable)7 NoSuchTargetException (com.google.devtools.build.lib.packages.NoSuchTargetException)6 LabelSyntaxException (com.google.devtools.build.lib.cmdline.LabelSyntaxException)5 Map (java.util.Map)5 RootedPath (com.google.devtools.build.lib.vfs.RootedPath)4 BlazeDirectories (com.google.devtools.build.lib.analysis.BlazeDirectories)3 ConfiguredTarget (com.google.devtools.build.lib.analysis.ConfiguredTarget)3 ResolvedTargets (com.google.devtools.build.lib.cmdline.ResolvedTargets)3