use of com.google.devtools.build.lib.pkgcache.TargetPatternEvaluator in project bazel by bazelbuild.
the class CoverageCommand method setDefaultInstrumentationFilter.
/**
* Method implements a heuristic used to set default value of the
* --instrumentation_filter option. Following algorithm is used:
* 1) Identify all test targets on the command line.
* 2) Expand all test suites into the individual test targets
* 3) Calculate list of package names containing all test targets above.
* 4) Replace all "javatests/" substrings in package names with "java/".
* 5) If two packages reside in the same directory, use filter based on
* the parent directory name instead. Doing so significantly simplifies
* instrumentation filter in majority of real-life scenarios (in
* particular when dealing with my/package/... wildcards).
* 6) Set --instrumentation_filter default value to instrument everything
* in those packages.
*/
private void setDefaultInstrumentationFilter(CommandEnvironment env, OptionsParser optionsProvider) throws OptionsParsingException, AbruptExitException {
try {
BlazeRuntime runtime = env.getRuntime();
// Initialize package cache, since it is used by the TargetPatternEvaluator.
// TODO(bazel-team): Don't allow commands to setup the package cache more than once per build.
// We'll have to move it earlier in the process to allow this. Possibly: Move it to
// the command dispatcher and allow commands to annotate "need-packages".
env.setupPackageCache(optionsProvider, runtime.getDefaultsPackageContent(optionsProvider));
// Collect all possible test targets. We don't really care whether there will be parsing
// errors here - they will be reported during actual build.
TargetPatternEvaluator targetPatternEvaluator = env.newTargetPatternEvaluator();
Set<Target> testTargets = targetPatternEvaluator.parseTargetPatternList(env.getReporter(), optionsProvider.getResidue(), FilteringPolicies.FILTER_TESTS, /*keep_going=*/
true).getTargets();
SortedSet<String> packageFilters = Sets.newTreeSet();
collectInstrumentedPackages(env, testTargets, packageFilters);
optimizeFilterSet(packageFilters);
String instrumentationFilter = "//" + Joiner.on(",//").join(packageFilters);
final String instrumentationFilterOptionName = "instrumentation_filter";
if (!packageFilters.isEmpty()) {
env.getReporter().handle(Event.info("Using default value for --instrumentation_filter: \"" + instrumentationFilter + "\"."));
env.getReporter().handle(Event.info("Override the above default with --" + instrumentationFilterOptionName));
optionsProvider.parse(OptionPriority.COMPUTED_DEFAULT, "Instrumentation filter heuristic", ImmutableList.of("--" + instrumentationFilterOptionName + "=" + instrumentationFilter));
}
} catch (TargetParsingException e) {
// We can't compute heuristic - just use default filter.
} catch (InterruptedException e) {
// We cannot quit now because AbstractCommand does not have the
// infrastructure to do that. Just set a flag and return from exec() as
// early as possible. We can do this because there is always an exec()
// after an editOptions().
wasInterrupted = true;
}
}
use of com.google.devtools.build.lib.pkgcache.TargetPatternEvaluator in project bazel by bazelbuild.
the class GenQuery method executeQuery.
@Nullable
private byte[] executeQuery(RuleContext ruleContext, QueryOptions queryOptions, Set<Target> scope, String query) throws InterruptedException {
SkyFunction.Environment env = ruleContext.getAnalysisEnvironment().getSkyframeEnv();
Pair<ImmutableMap<PackageIdentifier, Package>, ImmutableMap<Label, Target>> closureInfo;
try {
closureInfo = constructPackageMap(env, scope);
if (closureInfo == null) {
return null;
}
} catch (BrokenQueryScopeException e) {
ruleContext.ruleError(e.getMessage());
return null;
}
ImmutableMap<PackageIdentifier, Package> packageMap = closureInfo.first;
ImmutableMap<Label, Target> validTargetsMap = closureInfo.second;
PackageProvider packageProvider = new PreloadedMapPackageProvider(packageMap, validTargetsMap);
TargetPatternEvaluator evaluator = new SkyframeEnvTargetPatternEvaluator(env);
Predicate<Label> labelFilter = Predicates.in(validTargetsMap.keySet());
return doQuery(queryOptions, packageProvider, labelFilter, evaluator, query, ruleContext);
}
use of com.google.devtools.build.lib.pkgcache.TargetPatternEvaluator in project bazel by bazelbuild.
the class CommandEnvironment method newTargetPatternEvaluator.
/**
* Creates and returns a new target pattern parser.
*/
public TargetPatternEvaluator newTargetPatternEvaluator() {
TargetPatternEvaluator result = getPackageManager().newTargetPatternEvaluator();
result.updateOffset(relativeWorkingDirectory);
return result;
}
Aggregations