Skip to content

Commit

Permalink
Add an "is main repo" field to Roots
Browse files Browse the repository at this point in the history
This is part of prepping for #1262.

--
MOS_MIGRATED_REVID=132553178
  • Loading branch information
kchodorow authored and damienmg committed Sep 9, 2016
1 parent daf62ae commit 3fd3f1b
Show file tree
Hide file tree
Showing 12 changed files with 71 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public Artifact getDerivedArtifact(PathFragment execPath, Path execRoot) {
Preconditions.checkArgument(execPath.isNormalized(), execPath);
// TODO(bazel-team): Check that either BinTools do not change over the life of the Blaze server,
// or require that a legitimate ArtifactOwner be passed in here to allow for ownership.
return getArtifact(execRoot.getRelative(execPath), Root.execRootAsDerivedRoot(execRoot),
return getArtifact(execRoot.getRelative(execPath), Root.execRootAsDerivedRoot(execRoot, true),
execPath, ArtifactOwner.NULL_OWNER, null);
}

Expand Down
57 changes: 44 additions & 13 deletions src/main/java/com/google/devtools/build/lib/actions/Root.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,16 @@ public final class Root implements Comparable<Root>, Serializable {
/**
* Returns the given path as a source root. The path may not be {@code null}.
*/
// TODO(kchodorow): remove once roots don't need to know if they're in the main repo.
public static Root asSourceRoot(Path path, boolean isMainRepo) {
return new Root(null, path, false, isMainRepo);
}

/**
* testonly until {@link #asSourceRoot(Path, boolean)} is deleted.
*/
public static Root asSourceRoot(Path path) {
return new Root(null, path);
return asSourceRoot(path, true);
}

/**
Expand All @@ -64,8 +72,8 @@ public static Root asSourceRoot(Path path) {
*/
@VisibleForTesting
public static Root asDerivedRoot(Path path) {
return new Root(path, path);
}
return new Root(path, path, true);
}

/**
* Returns the given path as a derived root, relative to the given exec root. The root must be a
Expand All @@ -74,42 +82,61 @@ public static Root asDerivedRoot(Path path) {
* <p>Be careful with this method - all derived roots must be registered with the artifact factory
* before the analysis phase.
*/
public static Root asDerivedRoot(Path execRoot, Path root) {
// TODO(kchodorow): remove once roots don't need to know if they're in the main repo.
public static Root asDerivedRoot(Path execRoot, Path root, boolean isMainRepo) {
Preconditions.checkArgument(root.startsWith(execRoot));
Preconditions.checkArgument(!root.equals(execRoot));
return new Root(execRoot, root);
return new Root(execRoot, root, false, isMainRepo);
}

public static Root middlemanRoot(Path execRoot, Path outputDir) {
/**
* testonly until {@link #asDerivedRoot(Path, Path, boolean)} is deleted.
*/
public static Root asDerivedRoot(Path execRoot, Path root) {
return Root.asDerivedRoot(execRoot, root, true);
}

// TODO(kchodorow): remove once roots don't need to know if they're in the main repo.
public static Root middlemanRoot(Path execRoot, Path outputDir, boolean isMainRepo) {
Path root = outputDir.getRelative("internal");
Preconditions.checkArgument(root.startsWith(execRoot));
Preconditions.checkArgument(!root.equals(execRoot));
return new Root(execRoot, root, true);
return new Root(execRoot, root, true, isMainRepo);
}

/**
* testonly until {@link #middlemanRoot(Path, Path, boolean)} is deleted.
*/
public static Root middlemanRoot(Path execRoot, Path outputDir) {
return Root.middlemanRoot(execRoot, outputDir, true);
}

/**
* Returns the exec root as a derived root. The exec root should never be treated as a derived
* root, but this is currently allowed. Do not add any further uses besides the ones that already
* exist!
*/
static Root execRootAsDerivedRoot(Path execRoot) {
return new Root(execRoot, execRoot);
// TODO(kchodorow): remove isMainRepo once roots don't need to know if they're in the main repo.
static Root execRootAsDerivedRoot(Path execRoot, boolean isMainRepo) {
return new Root(execRoot, execRoot, false, isMainRepo);
}

@Nullable private final Path execRoot;
private final Path path;
private final boolean isMiddlemanRoot;
private final boolean isMainRepo;
private final PathFragment execPath;

private Root(@Nullable Path execRoot, Path path, boolean isMiddlemanRoot) {
private Root(@Nullable Path execRoot, Path path, boolean isMiddlemanRoot, boolean isMainRepo) {
this.execRoot = execRoot;
this.path = Preconditions.checkNotNull(path);
this.isMiddlemanRoot = isMiddlemanRoot;
this.isMainRepo = isMainRepo;
this.execPath = isSourceRoot() ? PathFragment.EMPTY_FRAGMENT : path.relativeTo(execRoot);
}

private Root(@Nullable Path execRoot, Path path) {
this(execRoot, path, false);
private Root(@Nullable Path execRoot, Path path, boolean isMainRepo) {
this(execRoot, path, false, isMainRepo);
}

public Path getPath() {
Expand Down Expand Up @@ -139,10 +166,14 @@ public boolean isSourceRoot() {
return execRoot == null;
}

public boolean isMiddlemanRoot() {
boolean isMiddlemanRoot() {
return isMiddlemanRoot;
}

public boolean isMainRepo() {
return isMainRepo;
}

@Override
public int compareTo(Root o) {
return path.compareTo(o.path);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,8 @@ public Path getEmbeddedBinariesRoot() {
* {@link BlazeDirectories} of this server instance. Nothing else should be placed here.
*/
public Root getBuildDataDirectory(String workspaceName) {
return Root.asDerivedRoot(getExecRoot(workspaceName), getOutputPath(workspaceName));
return Root
.asDerivedRoot(getExecRoot(workspaceName), getOutputPath(workspaceName), true);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -826,7 +826,7 @@ public void setArtifactRoots(ImmutableMap<PackageIdentifier, Path> packageRoots)
for (Map.Entry<PackageIdentifier, Path> entry : packageRoots.entrySet()) {
Root root = rootMap.get(entry.getValue());
if (root == null) {
root = Root.asSourceRoot(entry.getValue());
root = Root.asSourceRoot(entry.getValue(), entry.getKey().getRepository().isMain());
rootMap.put(entry.getValue(), root);
}
realPackageRoots.put(entry.getKey(), root);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,8 @@ public final ConfiguredTarget createConfiguredTarget(AnalysisEnvironment analysi
InputFile inputFile = (InputFile) target;
Artifact artifact = artifactFactory.getSourceArtifact(
inputFile.getExecPath(),
Root.asSourceRoot(inputFile.getPackage().getSourceRoot()),
Root.asSourceRoot(inputFile.getPackage().getSourceRoot(),
inputFile.getPackage().getPackageIdentifier().getRepository().isMain()),
new ConfiguredTargetKey(target.getLabel(), config));

return new InputFileConfiguredTarget(targetContext, inputFile, artifact);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -979,17 +979,20 @@ private OutputRoots(BlazeDirectories directories, String outputDirName) {
Path execRoot = directories.getExecRoot();
// configuration-specific output tree
Path outputDir = directories.getOutputPath().getRelative(outputDirName);
this.outputDirectory = Root.asDerivedRoot(execRoot, outputDir);
this.outputDirectory = Root.asDerivedRoot(execRoot, outputDir, true);

// specific subdirs under outputDirectory
this.binDirectory = Root.asDerivedRoot(execRoot, outputDir.getRelative("bin"));
this.genfilesDirectory = Root.asDerivedRoot(execRoot, outputDir.getRelative("genfiles"));
this.binDirectory = Root
.asDerivedRoot(execRoot, outputDir.getRelative("bin"), true);
this.genfilesDirectory = Root.asDerivedRoot(
execRoot, outputDir.getRelative("genfiles"), true);
this.coverageMetadataDirectory = Root.asDerivedRoot(execRoot,
outputDir.getRelative("coverage-metadata"));
this.testLogsDirectory = Root.asDerivedRoot(execRoot, outputDir.getRelative("testlogs"));
this.includeDirectory = Root.asDerivedRoot(execRoot,
outputDir.getRelative(BlazeDirectories.RELATIVE_INCLUDE_DIR));
this.middlemanDirectory = Root.middlemanRoot(execRoot, outputDir);
outputDir.getRelative("coverage-metadata"), true);
this.testLogsDirectory = Root.asDerivedRoot(
execRoot, outputDir.getRelative("testlogs"), true);
this.includeDirectory = Root.asDerivedRoot(
execRoot, outputDir.getRelative(BlazeDirectories.RELATIVE_INCLUDE_DIR), true);
this.middlemanDirectory = Root.middlemanRoot(execRoot, outputDir, true);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,8 @@ public static ArtifactLocation makeArtifactLocation(Artifact artifact) {
}

private static ArtifactLocation makeArtifactLocation(Package pkg) {
Root root = Root.asSourceRoot(pkg.getSourceRoot());
Root root = Root.asSourceRoot(pkg.getSourceRoot(),
pkg.getPackageIdentifier().getRepository().isMain());
PathFragment relativePath = pkg.getBuildFile().getPath().relativeTo(root.getPath());
return makeArtifactLocation(root, relativePath);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,8 @@ public Artifact getVolatileWorkspaceStatus() throws InterruptedException {
)
public String getBuildFileRelativePath() {
Package pkg = ruleContext.getRule().getPackage();
Root root = Root.asSourceRoot(pkg.getSourceRoot());
Root root = Root.asSourceRoot(pkg.getSourceRoot(),
pkg.getPackageIdentifier().getRepository().isMain());
return pkg.getBuildFile().getPath().relativeTo(root.getPath()).getPathString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ static FdoSupport create(
(fdoProfile == null)
? null
: Root.asDerivedRoot(execRoot, execRoot.getRelative(
PrecomputedValue.PRODUCT_NAME.get(env) + "-fdo"));
PrecomputedValue.PRODUCT_NAME.get(env) + "-fdo"), true);

PathFragment fdoRootExecPath = fdoProfile == null
? null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public SkyValue compute(SkyKey skyKey, Environment env)
return null;
}

// TODO(kchodorow): create a SkyFunction to get the main repository name and pass it in here.
Path execRoot = blazeDirectories.getExecRoot();
FdoSupportValue.Key key = (FdoSupportValue.Key) skyKey.argument();
FdoSupport fdoSupport;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,8 @@ public Map<PathFragment, Root> findPackageRootsForFiles(Iterable<PathFragment> e
}
if (value.hasContainingPackage()) {
// We have found corresponding root for current execPath.
result.put(path, Root.asSourceRoot(value.getContainingPackageRoot()));
result.put(path, Root.asSourceRoot(value.getContainingPackageRoot(),
value.getContainingPackageName().getRepository().isMain()));
} else {
// We haven't found corresponding root for current execPath.
result.put(path, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,8 @@ private Map<PathFragment, Root> getArtifactRoots(
ContainingPackageLookupValue value = result.get(ContainingPackageLookupValue.key(
PackageIdentifier.createInMainRepo(forFiles ? execPath.getParentDirectory() : execPath)));
if (value.hasContainingPackage()) {
roots.put(execPath, Root.asSourceRoot(value.getContainingPackageRoot()));
roots.put(execPath, Root.asSourceRoot(value.getContainingPackageRoot(),
value.getContainingPackageName().getRepository().isMain()));
} else {
roots.put(execPath, null);
}
Expand Down

0 comments on commit 3fd3f1b

Please sign in to comment.