Skip to content

Commit

Permalink
Enable automatic trimming of test configuration when entering non-tes…
Browse files Browse the repository at this point in the history
…t rules.

Note that this is not sufficient to see caching between builds on its own;
currently when any flag changes, the analysis cache is reset. A follow-up
will turn off this behavior when only test flags change while
trim_test_configuration is on.

config_settings which examine test options are treated the same as test rules;
that is, they can only be successfully analyzed at the top level or when
connected to the top level by an unbroken chain of test rules.

RELNOTES: None.
PiperOrigin-RevId: 200614584
  • Loading branch information
mstaib authored and Copybara-Service committed Jun 14, 2018
1 parent 2b015c5 commit 0412a9f
Show file tree
Hide file tree
Showing 5 changed files with 695 additions and 0 deletions.
Expand Up @@ -85,6 +85,20 @@ public static class TestOptions extends FragmentOptions {
)
public int testResultExpiration;

@Option(
name = "trim_test_configuration",
defaultValue = "false",
documentationCategory = OptionDocumentationCategory.BUILD_TIME_OPTIMIZATION,
effectTags = {
OptionEffectTag.LOADING_AND_ANALYSIS,
OptionEffectTag.LOSES_INCREMENTAL_STATE,
},
help = "When enabled, test-related options will be cleared below the top level of the build. "
+ "When this flag is active, tests cannot be built as dependencies of non-test rules, "
+ "but changes to test-related options will not cause non-test rules to be re-analyzed."
)
public boolean trimTestConfiguration;

@Option(
name = "test_arg",
allowMultiple = true,
Expand Down Expand Up @@ -193,6 +207,9 @@ public static class Loader implements ConfigurationFragmentFactory {
@Override
public Fragment create(BuildOptions buildOptions)
throws InvalidConfigurationException {
if (!buildOptions.contains(TestOptions.class)) {
return null;
}
return new TestConfiguration(buildOptions.get(TestOptions.class));
}

Expand Down
@@ -0,0 +1,85 @@
// Copyright 2018 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.devtools.build.lib.analysis.test;

import com.google.common.collect.ImmutableSet;
import com.google.devtools.build.lib.analysis.config.BuildOptions;
import com.google.devtools.build.lib.analysis.config.FragmentOptions;
import com.google.devtools.build.lib.analysis.config.transitions.NoTransition;
import com.google.devtools.build.lib.analysis.config.transitions.PatchTransition;
import com.google.devtools.build.lib.analysis.test.TestConfiguration.TestOptions;
import com.google.devtools.build.lib.packages.Rule;
import com.google.devtools.build.lib.packages.RuleClass;
import com.google.devtools.build.lib.packages.RuleTransitionFactory;
import com.google.devtools.common.options.Options;
import java.util.LinkedHashSet;
import java.util.Set;

/**
* Trimming transition factory which removes the test config fragment when entering a non-test rule.
*/
public final class TestTrimmingTransitionFactory implements RuleTransitionFactory {

private static final Set<String> TEST_OPTIONS =
ImmutableSet.copyOf(Options.getDefaults(TestOptions.class).asMap().keySet());

/**
* Trimming transition which removes the test config fragment if --trim_test_configuration is on.
*/
public static enum TestTrimmingTransition implements PatchTransition {
INSTANCE;

@Override
public BuildOptions patch(BuildOptions originalOptions) {
if (!originalOptions.contains(TestOptions.class)) {
// nothing to do, already trimmed this fragment
return originalOptions;
}
TestOptions originalTestOptions = originalOptions.get(TestOptions.class);
if (!originalTestOptions.trimTestConfiguration) {
// nothing to do, trimming is disabled
return originalOptions;
}
BuildOptions.Builder builder = BuildOptions.builder();
for (FragmentOptions options : originalOptions.getOptions()) {
if (!(options instanceof TestOptions)) {
builder.add(options);
}
}
return builder.build();
}
}

@Override
public PatchTransition buildTransitionFor(Rule rule) {
RuleClass ruleClass = rule.getRuleClassObject();
if (ruleClass
.getConfigurationFragmentPolicy()
.isLegalConfigurationFragment(TestConfiguration.class)) {
// Test rule; no need to trim here.
return NoTransition.INSTANCE;
}

Set<String> referencedTestOptions =
new LinkedHashSet<String>(ruleClass.getOptionReferenceFunction().apply(rule));
referencedTestOptions.retainAll(TEST_OPTIONS);
if (!referencedTestOptions.isEmpty()) {
// Test-option-referencing config_setting; no need to trim here.
return NoTransition.INSTANCE;
}

// Non-test rule. Trim it!
return TestTrimmingTransition.INSTANCE;
}
}
Expand Up @@ -18,6 +18,7 @@
import com.google.devtools.build.lib.analysis.ConfiguredRuleClassProvider;
import com.google.devtools.build.lib.analysis.ConfiguredRuleClassProvider.RuleSet;
import com.google.devtools.build.lib.analysis.test.TestConfiguration;
import com.google.devtools.build.lib.analysis.test.TestTrimmingTransitionFactory;

/** A set of basic rules - Bazel won't work correctly without these. */
public final class CoreRules implements RuleSet {
Expand All @@ -30,6 +31,7 @@ private CoreRules() {
@Override
public void init(ConfiguredRuleClassProvider.Builder builder) {
builder.addConfig(TestConfiguration.TestOptions.class, new TestConfiguration.Loader());
builder.addTrimmingTransitionFactory(new TestTrimmingTransitionFactory());
builder.addRuleDefinition(new BaseRuleClasses.RootRule());
builder.addRuleDefinition(new BaseRuleClasses.BaseRule());
builder.addRuleDefinition(new BaseRuleClasses.RuleBase());
Expand Down
Expand Up @@ -20,13 +20,17 @@
import com.google.devtools.build.lib.analysis.BaseRuleClasses;
import com.google.devtools.build.lib.analysis.RuleDefinition;
import com.google.devtools.build.lib.analysis.RuleDefinitionEnvironment;
import com.google.devtools.build.lib.analysis.test.TestConfiguration;
import com.google.devtools.build.lib.packages.RuleClass;

/** Rule object implementing "test_suite". */
public final class TestSuiteRule implements RuleDefinition {
@Override
public RuleClass build(RuleClass.Builder builder, RuleDefinitionEnvironment env) {
return builder
// Technically, test_suite does not use TestConfiguration. But the tests it depends on
// will always depend on TestConfiguration, so requiring it here simply acknowledges that.
.requiresConfigurationFragments(TestConfiguration.class)
.override(
attr("testonly", BOOLEAN)
.value(true)
Expand Down

0 comments on commit 0412a9f

Please sign in to comment.