Search in sources :

Example 1 with TargetPatternEvaluator

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;
    }
}
Also used : TargetPatternEvaluator(com.google.devtools.build.lib.pkgcache.TargetPatternEvaluator) Target(com.google.devtools.build.lib.packages.Target) TargetParsingException(com.google.devtools.build.lib.cmdline.TargetParsingException) BlazeRuntime(com.google.devtools.build.lib.runtime.BlazeRuntime)

Example 2 with TargetPatternEvaluator

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);
}
Also used : TargetPatternEvaluator(com.google.devtools.build.lib.pkgcache.TargetPatternEvaluator) SkyFunction(com.google.devtools.build.skyframe.SkyFunction) Label(com.google.devtools.build.lib.cmdline.Label) ImmutableMap(com.google.common.collect.ImmutableMap) PackageProvider(com.google.devtools.build.lib.pkgcache.PackageProvider) ConfiguredTarget(com.google.devtools.build.lib.analysis.ConfiguredTarget) Target(com.google.devtools.build.lib.packages.Target) PackageIdentifier(com.google.devtools.build.lib.cmdline.PackageIdentifier) Package(com.google.devtools.build.lib.packages.Package) Nullable(javax.annotation.Nullable)

Example 3 with TargetPatternEvaluator

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;
}
Also used : TargetPatternEvaluator(com.google.devtools.build.lib.pkgcache.TargetPatternEvaluator)

Aggregations

TargetPatternEvaluator (com.google.devtools.build.lib.pkgcache.TargetPatternEvaluator)3 Target (com.google.devtools.build.lib.packages.Target)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 ConfiguredTarget (com.google.devtools.build.lib.analysis.ConfiguredTarget)1 Label (com.google.devtools.build.lib.cmdline.Label)1 PackageIdentifier (com.google.devtools.build.lib.cmdline.PackageIdentifier)1 TargetParsingException (com.google.devtools.build.lib.cmdline.TargetParsingException)1 Package (com.google.devtools.build.lib.packages.Package)1 PackageProvider (com.google.devtools.build.lib.pkgcache.PackageProvider)1 BlazeRuntime (com.google.devtools.build.lib.runtime.BlazeRuntime)1 SkyFunction (com.google.devtools.build.skyframe.SkyFunction)1 Nullable (javax.annotation.Nullable)1