Skip to content

Commit

Permalink
Remove references to getTarget(). Requires threading through an
Browse files Browse the repository at this point in the history
ExtendedEventHandler so we can get the target via the package manager during
rule dumps.

PiperOrigin-RevId: 185042522
  • Loading branch information
mjhalupka authored and Copybara-Service committed Feb 8, 2018
1 parent c7f613b commit 0ab46f0
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import static java.util.stream.Collectors.toList;

import com.google.devtools.build.lib.events.Event;
import com.google.devtools.build.lib.events.ExtendedEventHandler;
import com.google.devtools.build.lib.packages.Attribute;
import com.google.devtools.build.lib.packages.PackageFactory;
import com.google.devtools.build.lib.packages.RuleClass;
Expand Down Expand Up @@ -222,7 +223,7 @@ public BlazeCommandResult exec(CommandEnvironment env, OptionsProvider options)
}

if (dumpOptions.dumpRules) {
dumpRuleStats(env.getBlazeWorkspace(), env.getSkyframeExecutor(), out);
dumpRuleStats(env.getReporter(), env.getBlazeWorkspace(), env.getSkyframeExecutor(), out);
out.println();
}

Expand Down Expand Up @@ -290,8 +291,12 @@ private void dumpRuleClasses(BlazeRuntime runtime, PrintStream out) {
}
}

private void dumpRuleStats(BlazeWorkspace workspace, SkyframeExecutor executor, PrintStream out) {
List<RuleStat> ruleStats = executor.getRuleStats();
private void dumpRuleStats(
ExtendedEventHandler eventHandler,
BlazeWorkspace workspace,
SkyframeExecutor executor,
PrintStream out) {
List<RuleStat> ruleStats = executor.getRuleStats(eventHandler);
if (ruleStats.isEmpty()) {
out.print("No rules in Blaze server, please run a build command first.");
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,11 @@
import com.google.devtools.build.lib.events.ExtendedEventHandler;
import com.google.devtools.build.lib.packages.AspectClass;
import com.google.devtools.build.lib.packages.BuildFileName;
import com.google.devtools.build.lib.packages.NoSuchPackageException;
import com.google.devtools.build.lib.packages.NoSuchTargetException;
import com.google.devtools.build.lib.packages.Package;
import com.google.devtools.build.lib.packages.PackageFactory;
import com.google.devtools.build.lib.packages.Rule;
import com.google.devtools.build.lib.packages.RuleClass;
import com.google.devtools.build.lib.packages.SkylarkSemanticsOptions;
import com.google.devtools.build.lib.pkgcache.PackageCacheOptions;
Expand Down Expand Up @@ -692,7 +695,7 @@ public void clearAnalysisCache(
}

@Override
public List<RuleStat> getRuleStats() {
public List<RuleStat> getRuleStats(ExtendedEventHandler eventHandler) {
Map<String, RuleStat> ruleStats = new HashMap<>();
for (Map.Entry<SkyKey, ? extends NodeEntry> skyKeyAndNodeEntry :
memoizingEvaluator.getGraphMap().entrySet()) {
Expand All @@ -707,8 +710,17 @@ public List<RuleStat> getRuleStats() {
ConfiguredTargetValue ctValue = (ConfiguredTargetValue) entry.getValue();
ConfiguredTarget configuredTarget = ctValue.getConfiguredTarget();
if (configuredTarget instanceof RuleConfiguredTarget) {

Rule rule;
try {
rule =
(Rule) getPackageManager().getTarget(eventHandler, configuredTarget.getLabel());
} catch (NoSuchPackageException | NoSuchTargetException | InterruptedException e) {
throw new IllegalStateException(
"Failed to get Rule target from package when calculating stats.", e);
}
RuleConfiguredTarget ruleConfiguredTarget = (RuleConfiguredTarget) configuredTarget;
RuleClass ruleClass = ruleConfiguredTarget.getTarget().getRuleClassObject();
RuleClass ruleClass = rule.getRuleClassObject();
RuleStat ruleStat =
ruleStats.computeIfAbsent(
ruleClass.getKey(), k -> new RuleStat(k, ruleClass.getName(), true));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,7 @@ public long getActionCount() {
}

/** Computes statistics on heap-resident rules and aspects. */
public abstract List<RuleStat> getRuleStats();
public abstract List<RuleStat> getRuleStats(ExtendedEventHandler eventHandler);

/**
* Removes ConfigurationFragmentValues from the cache.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1193,8 +1193,7 @@ protected Artifact getGenfilesArtifact(
packageRelativePath,
owner
.getConfiguration()
.getGenfilesDirectory(
owner.getTarget().getLabel().getPackageIdentifier().getRepository()),
.getGenfilesDirectory(owner.getLabel().getPackageIdentifier().getRepository()),
(AspectValue.AspectKey)
AspectValue.createAspectKey(
owner.getLabel(),
Expand Down Expand Up @@ -1973,7 +1972,13 @@ protected Artifact getImplicitOutputArtifact(
ConfiguredTarget target,
BuildConfiguration configuration,
SafeImplicitOutputsFunction outputFunction) {
Rule associatedRule = target.getTarget().getAssociatedRule();
Rule rule;
try {
rule = (Rule) skyframeExecutor.getPackageManager().getTarget(reporter, target.getLabel());
} catch (NoSuchPackageException | NoSuchTargetException | InterruptedException e) {
throw new IllegalStateException(e);
}
Rule associatedRule = rule.getAssociatedRule();
RepositoryName repository = associatedRule.getRepository();

ArtifactRoot root;
Expand All @@ -1982,15 +1987,13 @@ protected Artifact getImplicitOutputArtifact(
} else {
root = configuration.getGenfilesDirectory(repository);
}
ArtifactOwner owner =
ConfiguredTargetKey.of(target.getTarget().getLabel(), target.getConfiguration());
ArtifactOwner owner = ConfiguredTargetKey.of(target.getLabel(), target.getConfiguration());

RawAttributeMapper attr = RawAttributeMapper.of(associatedRule);

String path = Iterables.getOnlyElement(outputFunction.getImplicitOutputs(eventCollector, attr));

return view.getArtifactFactory()
.getDerivedArtifact(
target.getTarget().getLabel().getPackageFragment().getRelative(path), root, owner);
.getDerivedArtifact(target.getLabel().getPackageFragment().getRelative(path), root, owner);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.google.devtools.build.lib.packages.BuildFileNotFoundException;
import com.google.devtools.build.lib.packages.util.ResourceLoader;
import com.google.devtools.build.lib.rules.android.AndroidSdkProvider;
import com.google.devtools.build.lib.skyframe.ConfiguredTargetAndTarget;
import com.google.devtools.build.lib.vfs.FileSystemUtils;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -91,8 +92,8 @@ public void testGeneratedAarImport() throws Exception {
")");
invalidatePackages();

ConfiguredTarget aarImportTarget =
getConfiguredTarget("@androidsdk//com.google.android:foo-1.0.0");
ConfiguredTargetAndTarget aarImportTarget =
getConfiguredTargetAndTarget("@androidsdk//com.google.android:foo-1.0.0");
assertThat(aarImportTarget).isNotNull();
assertThat(aarImportTarget.getTarget().getAssociatedRule().getRuleClass())
.isEqualTo("aar_import");
Expand Down

0 comments on commit 0ab46f0

Please sign in to comment.