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();
}
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);
}
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);
}
}
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;
}
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()));
}
Aggregations