Skip to content

Commit

Permalink
Manually wire up _repo_mapping root symlink
Browse files Browse the repository at this point in the history
  • Loading branch information
fmeum committed Nov 7, 2022
1 parent 4ae1965 commit 46f5df9
Show file tree
Hide file tree
Showing 15 changed files with 130 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -386,11 +386,12 @@ static Map<PathFragment, Artifact> filterListForObscuringSymlinks(
* normal source tree entries, or runfile conflicts. May be null, in which case obscuring
* symlinks are silently discarded, and conflicts are overwritten.
* @param location Location for eventHandler warnings. Ignored if eventHandler is null.
* @param repoMappingManifest repository mapping manifest to add as a root symlink
* @return Map<PathFragment, Artifact> path fragment to artifact, of normal source tree entries
* and elements that live outside the source tree. Null values represent empty input files.
*/
public Map<PathFragment, Artifact> getRunfilesInputs(
EventHandler eventHandler, Location location) {
EventHandler eventHandler, Location location, @Nullable Artifact repoMappingManifest) {
ConflictChecker checker = new ConflictChecker(conflictPolicy, eventHandler, location);
Map<PathFragment, Artifact> manifest = getSymlinksAsMap(checker);
// Add artifacts (committed to inclusion on construction of runfiles).
Expand All @@ -417,6 +418,9 @@ public Map<PathFragment, Artifact> getRunfilesInputs(
checker = new ConflictChecker(ConflictPolicy.WARN, eventHandler, location);
}
builder.add(getRootSymlinksAsMap(checker), checker);
if (repoMappingManifest != null) {
checker.put(builder.manifest, PathFragment.create("_repo_mapping"), repoMappingManifest);
}
return builder.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,20 +134,13 @@ private static RunfilesSupport create(

Artifact repoMappingManifest =
createRepoMappingManifestAction(ruleContext, runfiles, owningExecutable);
if (repoMappingManifest != null) {
runfiles = new Runfiles.Builder(ruleContext.getWorkspaceName(),
ruleContext.getConfiguration().legacyExternalRunfiles())
.merge(runfiles)
.addRootSymlink(PathFragment.create("_repo_mapping"), repoMappingManifest)
.build();
}

Artifact runfilesInputManifest;
Artifact runfilesManifest;
if (createManifest) {
runfilesInputManifest = createRunfilesInputManifestArtifact(ruleContext, owningExecutable);
runfilesManifest =
createRunfilesAction(ruleContext, runfiles, buildRunfileLinks, runfilesInputManifest);
runfilesManifest = createRunfilesAction(ruleContext, runfiles, buildRunfileLinks,
runfilesInputManifest, repoMappingManifest);
} else {
runfilesInputManifest = null;
runfilesManifest = null;
Expand Down Expand Up @@ -395,7 +388,8 @@ private static Artifact createRunfilesAction(
ActionConstructionContext context,
Runfiles runfiles,
boolean createSymlinks,
Artifact inputManifest) {
Artifact inputManifest,
@Nullable Artifact repoMappingManifest) {
// Compute the names of the runfiles directory and its MANIFEST file.
context
.getAnalysisEnvironment()
Expand All @@ -405,6 +399,7 @@ private static Artifact createRunfilesAction(
context.getActionOwner(),
inputManifest,
runfiles,
repoMappingManifest,
context.getConfiguration().remotableSourceManifestActions()));

if (!createSymlinks) {
Expand All @@ -431,7 +426,8 @@ private static Artifact createRunfilesAction(
inputManifest,
runfiles,
outputManifest,
/*filesetRoot=*/ null));
repoMappingManifest,
/*filesetRoot=*/ null));
return outputManifest;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,17 @@
import java.util.function.Supplier;
import javax.annotation.Nullable;

/** {@link RunfilesSupplier} implementation wrapping a single {@link Runfiles} directory mapping. */
/**
* {@link RunfilesSupplier} implementation wrapping a single {@link Runfiles} directory mapping.
*/
@AutoCodec
public final class SingleRunfilesSupplier implements RunfilesSupplier {

private final PathFragment runfilesDir;
private final Runfiles runfiles;
private final Supplier<Map<PathFragment, Artifact>> runfilesInputs;
@Nullable private final Artifact manifest;
@Nullable private final Artifact repoMappingManifest;
private final boolean buildRunfileLinks;
private final boolean runfileLinksEnabled;

Expand All @@ -50,28 +54,32 @@ public static SingleRunfilesSupplier create(RunfilesSupport runfilesSupport) {
runfilesSupport.getRunfiles(),
/*runfilesCachingEnabled=*/ false,
/*manifest=*/ null,
runfilesSupport.getRepoMappingManifest(),
runfilesSupport.isBuildRunfileLinks(),
runfilesSupport.isRunfilesEnabled());
}

/**
* Same as {@link SingleRunfilesSupplier#SingleRunfilesSupplier(PathFragment, Runfiles, Artifact,
* boolean, boolean)}, except adds caching for {@linkplain Runfiles#getRunfilesInputs runfiles
* inputs}.
* Same as
* {@link SingleRunfilesSupplier#SingleRunfilesSupplier(PathFragment, Runfiles, Artifact,
* Artifact, boolean, boolean)}, except adds caching for
* {@linkplain Runfiles#getRunfilesInputs runfiles inputs}.
*
* <p>The runfiles inputs are computed lazily and softly cached. Caching is shared across
* instances created via {@link #withOverriddenRunfilesDir}.
*/
public static SingleRunfilesSupplier createCaching(
PathFragment runfilesDir,
Runfiles runfiles,
@Nullable Artifact repoMappingManifest,
boolean buildRunfileLinks,
boolean runfileLinksEnabled) {
return new SingleRunfilesSupplier(
runfilesDir,
runfiles,
/*runfilesCachingEnabled=*/ true,
/*manifest=*/ null,
repoMappingManifest,
buildRunfileLinks,
runfileLinksEnabled);
}
Expand All @@ -92,13 +100,15 @@ public SingleRunfilesSupplier(
PathFragment runfilesDir,
Runfiles runfiles,
@Nullable Artifact manifest,
@Nullable Artifact repoMappingManifest,
boolean buildRunfileLinks,
boolean runfileLinksEnabled) {
this(
runfilesDir,
runfiles,
/*runfilesCachingEnabled=*/ false,
manifest,
repoMappingManifest,
buildRunfileLinks,
runfileLinksEnabled);
}
Expand All @@ -108,15 +118,18 @@ private SingleRunfilesSupplier(
Runfiles runfiles,
boolean runfilesCachingEnabled,
@Nullable Artifact manifest,
@Nullable Artifact repoMappingManifest,
boolean buildRunfileLinks,
boolean runfileLinksEnabled) {
this(
runfilesDir,
runfiles,
runfilesCachingEnabled
? new RunfilesCacher(runfiles)
: () -> runfiles.getRunfilesInputs(/*eventHandler=*/ null, /*location=*/ null),
? new RunfilesCacher(runfiles, repoMappingManifest)
: () -> runfiles.getRunfilesInputs(/*eventHandler=*/ null, /*location=*/ null,
repoMappingManifest),
manifest,
repoMappingManifest,
buildRunfileLinks,
runfileLinksEnabled);
}
Expand All @@ -126,13 +139,15 @@ private SingleRunfilesSupplier(
Runfiles runfiles,
Supplier<Map<PathFragment, Artifact>> runfilesInputs,
@Nullable Artifact manifest,
@Nullable Artifact repoMappingManifest,
boolean buildRunfileLinks,
boolean runfileLinksEnabled) {
checkArgument(!runfilesDir.isAbsolute());
this.runfilesDir = checkNotNull(runfilesDir);
this.runfiles = checkNotNull(runfiles);
this.runfilesInputs = checkNotNull(runfilesInputs);
this.manifest = manifest;
this.repoMappingManifest = repoMappingManifest;
this.buildRunfileLinks = buildRunfileLinks;
this.runfileLinksEnabled = runfileLinksEnabled;
}
Expand Down Expand Up @@ -199,17 +214,24 @@ public SingleRunfilesSupplier withOverriddenRunfilesDir(PathFragment newRunfiles
runfiles,
runfilesInputs,
manifest,
repoMappingManifest,
buildRunfileLinks,
runfileLinksEnabled);
}

/** Softly caches the result of {@link Runfiles#getRunfilesInputs}. */
/**
* Softly caches the result of {@link Runfiles#getRunfilesInputs}.
*/
private static final class RunfilesCacher implements Supplier<Map<PathFragment, Artifact>> {

private final Runfiles runfiles;
@Nullable
private final Artifact repoMappingManifest;
private volatile SoftReference<Map<PathFragment, Artifact>> ref = new SoftReference<>(null);

RunfilesCacher(Runfiles runfiles) {
RunfilesCacher(Runfiles runfiles, @Nullable Artifact repoMappingManifest) {
this.runfiles = runfiles;
this.repoMappingManifest = repoMappingManifest;
}

@Override
Expand All @@ -221,7 +243,8 @@ public Map<PathFragment, Artifact> get() {
synchronized (this) {
result = ref.get();
if (result == null) {
result = runfiles.getRunfilesInputs(/*eventHandler=*/ null, /*location=*/ null);
result = runfiles.getRunfilesInputs(/*eventHandler=*/ null, /*location=*/ null,
repoMappingManifest);
ref = new SoftReference<>(result);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public final class SourceManifestAction extends AbstractFileWriteAction {

private static final Comparator<Map.Entry<PathFragment, Artifact>> ENTRY_COMPARATOR =
(path1, path2) -> path1.getKey().getPathString().compareTo(path2.getKey().getPathString());

private final Artifact repoMappingManifest;
/**
* Interface for defining manifest formatting and reporting specifics. Implementations must be
* immutable.
Expand Down Expand Up @@ -118,28 +118,32 @@ void writeEntry(
@VisibleForTesting
SourceManifestAction(
ManifestWriter manifestWriter, ActionOwner owner, Artifact primaryOutput, Runfiles runfiles) {
this(manifestWriter, owner, primaryOutput, runfiles, /*remotableSourceManifestActions=*/ false);
this(manifestWriter, owner, primaryOutput, runfiles, /*remotableSourceManifestActions=*/ null,
false);
}

/**
* Creates a new AbstractSourceManifestAction instance using latin1 encoding to write the manifest
* file and with a specified root path for manifest entries.
*
* @param manifestWriter the strategy to use to write manifest entries
* @param owner the action owner
* @param primaryOutput the file to which to write the manifest
* @param runfiles runfiles
* @param manifestWriter the strategy to use to write manifest entries
* @param owner the action owner
* @param primaryOutput the file to which to write the manifest
* @param runfiles runfiles
* @param repoMappingManifest the repository mapping manifest for runfiles
*/
public SourceManifestAction(
ManifestWriter manifestWriter,
ActionOwner owner,
Artifact primaryOutput,
Runfiles runfiles,
@Nullable Artifact repoMappingManifest,
boolean remotableSourceManifestActions) {
// The real set of inputs is computed in #getInputs().
super(owner, NestedSetBuilder.emptySet(Order.STABLE_ORDER), primaryOutput, false);
this.manifestWriter = manifestWriter;
this.runfiles = runfiles;
this.repoMappingManifest = repoMappingManifest;
this.remotableSourceManifestActions = remotableSourceManifestActions;
}

Expand Down Expand Up @@ -180,7 +184,8 @@ public synchronized NestedSet<Artifact> getInputs() {
@VisibleForTesting
public void writeOutputFile(OutputStream out, @Nullable EventHandler eventHandler)
throws IOException {
writeFile(out, runfiles.getRunfilesInputs(eventHandler, getOwner().getLocation()));
writeFile(out,
runfiles.getRunfilesInputs(eventHandler, getOwner().getLocation(), repoMappingManifest));
}

/**
Expand All @@ -201,8 +206,8 @@ public String getStarlarkContent() throws IOException {

@Override
public DeterministicWriter newDeterministicWriter(ActionExecutionContext ctx) {
final Map<PathFragment, Artifact> runfilesInputs =
runfiles.getRunfilesInputs(ctx.getEventHandler(), getOwner().getLocation());
final Map<PathFragment, Artifact> runfilesInputs = runfiles.getRunfilesInputs(
ctx.getEventHandler(), getOwner().getLocation(), repoMappingManifest);
return out -> writeFile(out, runfilesInputs);
}

Expand Down Expand Up @@ -247,6 +252,10 @@ protected void computeKey(
fp.addString(GUID);
fp.addBoolean(remotableSourceManifestActions);
runfiles.fingerprint(fp);
fp.addBoolean(repoMappingManifest != null);
if (repoMappingManifest != null) {
fp.addPath(repoMappingManifest.getExecPath());
}
}

@Override
Expand Down
Loading

0 comments on commit 46f5df9

Please sign in to comment.