use of com.google.devtools.build.lib.vfs.PathFragment in project bazel by bazelbuild.
the class AndroidStudioInfoAspect method derivedArtifact.
private static Artifact derivedArtifact(ConfiguredTarget base, RuleContext ruleContext, String suffix) {
BuildConfiguration configuration = ruleContext.getConfiguration();
assert configuration != null;
Root binDirectory = configuration.getBinDirectory(ruleContext.getRule().getRepository());
PathFragment derivedFilePath = getOutputFilePath(base, ruleContext, suffix);
return ruleContext.getAnalysisEnvironment().getDerivedArtifact(derivedFilePath, binDirectory);
}
use of com.google.devtools.build.lib.vfs.PathFragment in project bazel by bazelbuild.
the class OutputDirectoryLinksUtils method relativize.
// Helper to getPrettyPath. Returns file, relativized w.r.t. the referent of
// "linkname", or null if it was a not a child.
private static PathFragment relativize(Path file, Path workspaceDirectory, String linkname) {
PathFragment link = new PathFragment(linkname);
try {
Path dir = workspaceDirectory.getRelative(link);
PathFragment levelOneLinkTarget = dir.readSymbolicLink();
if (levelOneLinkTarget.isAbsolute() && file.startsWith(dir = file.getRelative(levelOneLinkTarget))) {
return link.getRelative(file.relativeTo(dir));
}
} catch (IOException e) {
/* ignore */
}
return null;
}
use of com.google.devtools.build.lib.vfs.PathFragment in project bazel by bazelbuild.
the class OutputDirectoryLinksUtils method getPrettyPath.
/**
* Returns a convenient path to the specified file, relativizing it and using output-dir symlinks
* if possible. Otherwise, return a path relative to the workspace directory if possible.
* Otherwise, return the absolute path.
*
* <p>This method must be called after the symlinks are created at the end of a build. If called
* before, the pretty path may be incorrect if the symlinks end up pointing somewhere new.
*/
public static PathFragment getPrettyPath(Path file, String workspaceName, Path workspaceDirectory, String symlinkPrefix, String productName) {
for (String link : LINKS) {
PathFragment result = relativize(file, workspaceDirectory, symlinkPrefix + link);
if (result != null) {
return result;
}
}
PathFragment result = relativize(file, workspaceDirectory, execRootSymlink(symlinkPrefix, workspaceName));
if (result != null) {
return result;
}
ImmutableList<String> outputSymlinkNames = getOutputSymlinkNames(productName, symlinkPrefix);
checkArgument(!outputSymlinkNames.isEmpty());
result = relativize(file, workspaceDirectory, outputSymlinkNames.get(0));
if (result != null) {
return result;
}
return file.asFragment();
}
use of com.google.devtools.build.lib.vfs.PathFragment in project bazel by bazelbuild.
the class Label method parseCommandLineLabel.
/**
* Resolves a relative label using a workspace-relative path to the current working directory. The
* method handles these cases:
* <ul>
* <li>The label is absolute.
* <li>The label starts with a colon.
* <li>The label consists of a relative path, a colon, and a local part.
* <li>The label consists only of a local part.
* </ul>
*
* <p>Note that this method does not support any of the special syntactic constructs otherwise
* supported on the command line, like ":all", "/...", and so on.
*
* <p>It would be cleaner to use the TargetPatternEvaluator for this resolution, but that is not
* possible, because it is sometimes necessary to resolve a relative label before the package path
* is setup; in particular, before the tools/defaults package is created.
*
* @throws LabelSyntaxException if the resulting label is not valid
*/
public static Label parseCommandLineLabel(String label, PathFragment workspaceRelativePath) throws LabelSyntaxException {
Preconditions.checkArgument(!workspaceRelativePath.isAbsolute());
if (LabelValidator.isAbsolute(label)) {
return parseAbsolute(label);
}
int index = label.indexOf(':');
if (index < 0) {
index = 0;
label = ":" + label;
}
PathFragment path = workspaceRelativePath.getRelative(label.substring(0, index));
// validity check.
return create(path.getPathString(), label.substring(index + 1));
}
use of com.google.devtools.build.lib.vfs.PathFragment in project bazel by bazelbuild.
the class PackageIdentifier method discoverFromExecPath.
/**
* Tries to infer the package identifier from the given exec path. This method does not perform
* any I/O, but looks solely at the structure of the exec path. The resulting identifier may
* actually be a subdirectory of a package rather than a package, e.g.:
* <pre><code>
* + WORKSPACE
* + foo/BUILD
* + foo/bar/bar.java
* </code></pre>
*
* In this case, this method returns a package identifier for foo/bar, even though that is not a
* package. Callers need to look up the actual package if needed.
*
* @throws LabelSyntaxException if the exec path seems to be for an external repository that doe
* not have a valid repository name (see {@link RepositoryName#create})
*/
public static PackageIdentifier discoverFromExecPath(PathFragment execPath, boolean forFiles) throws LabelSyntaxException {
Preconditions.checkArgument(!execPath.isAbsolute(), execPath);
PathFragment tofind = forFiles ? Preconditions.checkNotNull(execPath.getParentDirectory(), "Must pass in files, not root directory") : execPath;
if (tofind.startsWith(new PathFragment(Label.EXTERNAL_PATH_PREFIX))) {
// TODO(ulfjack): Remove this when kchodorow@'s exec root rearrangement has been rolled out.
RepositoryName repository = RepositoryName.create("@" + tofind.getSegment(1));
return PackageIdentifier.create(repository, tofind.subFragment(2, tofind.segmentCount()));
} else if (!tofind.normalize().isNormalized()) {
RepositoryName repository = RepositoryName.create("@" + tofind.getSegment(1));
return PackageIdentifier.create(repository, tofind.subFragment(2, tofind.segmentCount()));
} else {
return PackageIdentifier.createInMainRepo(tofind);
}
}
Aggregations