Skip to content

Commit 545befb

Browse files
katrecopybara-github
authored andcommitted
Allow --toolchain_resolution_debug to match specific targets, as well as specific toolchain types.
This will create more skyframe nodes and edges, but only when the `--toolchain_resolution_debug` flag is set, and so should not have any effect on memory usage during normal non-debugging operation. RELNOTES: The `--toolchain_resolution_debug` flag now accepts regexes matching targets, as well as toolchain types, when choosing what debug messages to print. PiperOrigin-RevId: 394077188
1 parent 9a5bef9 commit 545befb

File tree

9 files changed

+105
-28
lines changed

9 files changed

+105
-28
lines changed

src/main/java/com/google/devtools/build/lib/analysis/PlatformConfiguration.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -115,22 +115,25 @@ public List<Label> getAdditionalExecutionConstraintsFor(Label label) {
115115
return constraints.build();
116116
}
117117

118-
/** Returns true if toolchain resolution debug info should be printed for this toolchain type. */
119-
public boolean debugToolchainResolution(Label toolchainType) {
120-
return debugToolchainResolution(ImmutableList.of(toolchainType));
118+
/**
119+
* Returns true if toolchain resolution debug info should be printed for this label, which could
120+
* be a toolchain type or a specific target.
121+
*/
122+
public boolean debugToolchainResolution(Label label) {
123+
return debugToolchainResolution(ImmutableList.of(label));
121124
}
122125

123126
/**
124-
* Returns true if toolchain resolution debug info should be printed for any of these toolchain
125-
* types.
127+
* Returns true if toolchain resolution debug info should be printed for any of these labels,
128+
* which could be either toolchain types or specific targets.
126129
*/
127-
public boolean debugToolchainResolution(Collection<Label> toolchainTypes) {
128-
if (toolchainTypes.isEmpty()) {
130+
public boolean debugToolchainResolution(Collection<Label> labels) {
131+
if (labels.isEmpty()) {
129132
// Check an empty string, in case the filter is .*
130133
return this.toolchainResolutionDebugRegexFilter.test("");
131134
}
132-
return toolchainTypes.stream()
135+
return labels.stream()
133136
.map(Label::getCanonicalForm)
134-
.anyMatch(toolchainType -> this.toolchainResolutionDebugRegexFilter.test(toolchainType));
137+
.anyMatch(this.toolchainResolutionDebugRegexFilter);
135138
}
136139
}

src/main/java/com/google/devtools/build/lib/analysis/PlatformOptions.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,10 @@ public class PlatformOptions extends FragmentOptions {
154154
effectTags = {OptionEffectTag.TERMINAL_OUTPUT},
155155
help =
156156
"Print debug information during toolchain resolution. The flag takes a regex, which is"
157-
+ " checked against toolchain types to see which to debug. Multiple regexes may be"
158-
+ " separated by commas, and then each regex is checked separately. Note: The output"
159-
+ " of this flag is very complex and will likely only be useful to experts in"
160-
+ " toolchain resolution.")
157+
+ " checked against toolchain types and specific targets to see which to debug. "
158+
+ "Multiple regexes may be separated by commas, and then each regex is checked "
159+
+ "separately. Note: The output of this flag is very complex and will likely only be "
160+
+ "useful to experts in toolchain resolution.")
161161
public RegexFilter toolchainResolutionDebug;
162162

163163
@Option(

src/main/java/com/google/devtools/build/lib/query2/cquery/ConfiguredTargetAccessor.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,9 @@ private static ToolchainCollection<ToolchainContext> getToolchainContexts(
217217
ImmutableSet<Label> execConstraintLabels =
218218
getExecutionPlatformConstraints(rule, config.getFragment(PlatformConfiguration.class));
219219
ImmutableMap<String, ExecGroup> execGroups = rule.getRuleClassObject().getExecGroups();
220+
// Check if this specific target should be debugged for toolchain resolution.
221+
boolean debugTarget =
222+
config.getFragment(PlatformConfiguration.class).debugToolchainResolution(target.getLabel());
220223

221224
ToolchainCollection.Builder<UnloadedToolchainContext> toolchainContexts =
222225
ToolchainCollection.builder();
@@ -231,6 +234,7 @@ private static ToolchainCollection<ToolchainContext> getToolchainContexts(
231234
.configurationKey(configurationKey)
232235
.requiredToolchainTypeLabels(execGroup.requiredToolchains())
233236
.execConstraintLabels(execGroup.execCompatibleWith())
237+
.debugTarget(debugTarget)
234238
.build());
235239
if (context == null) {
236240
return null;
@@ -244,6 +248,7 @@ private static ToolchainCollection<ToolchainContext> getToolchainContexts(
244248
.configurationKey(configurationKey)
245249
.requiredToolchainTypeLabels(requiredToolchains)
246250
.execConstraintLabels(execConstraintLabels)
251+
.debugTarget(debugTarget)
247252
.build());
248253
if (defaultContext == null) {
249254
return null;

src/main/java/com/google/devtools/build/lib/skyframe/ConfiguredTargetFunction.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,11 +528,18 @@ public static ComputedToolchainContexts computeUnloadedToolchainContexts(
528528
Map<String, ToolchainContextKey> toolchainContextKeys = new HashMap<>();
529529
String targetUnloadedToolchainContext = "target-unloaded-toolchain-context";
530530

531+
// Check if this specific target should be debugged for toolchain resolution.
532+
boolean debugTarget =
533+
configuration
534+
.getFragment(PlatformConfiguration.class)
535+
.debugToolchainResolution(targetAndConfig.getLabel());
536+
531537
ToolchainContextKey.Builder toolchainContextKeyBuilder =
532538
ToolchainContextKey.key()
533539
.configurationKey(toolchainConfig)
534540
.requiredToolchainTypeLabels(requiredDefaultToolchains)
535-
.execConstraintLabels(defaultExecConstraintLabels);
541+
.execConstraintLabels(defaultExecConstraintLabels)
542+
.debugTarget(debugTarget);
536543

537544
if (parentToolchainContextKey != null) {
538545
// Find out what execution platform the parent used, and force that.
@@ -560,6 +567,7 @@ public static ComputedToolchainContexts computeUnloadedToolchainContexts(
560567
.configurationKey(toolchainConfig)
561568
.requiredToolchainTypeLabels(execGroup.requiredToolchains())
562569
.execConstraintLabels(execGroup.execCompatibleWith())
570+
.debugTarget(debugTarget)
563571
.build());
564572
}
565573

src/main/java/com/google/devtools/build/lib/skyframe/SingleToolchainResolutionFunction.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,14 @@ public SkyValue compute(SkyKey skyKey, Environment env)
7979
throw new ToolchainResolutionFunctionException(e);
8080
}
8181

82-
// Find the right one.
82+
// Check if we are debugging the target or the toolchain type.
8383
boolean debug =
84-
configuration
85-
.getFragment(PlatformConfiguration.class)
86-
.debugToolchainResolution(key.toolchainTypeLabel());
84+
key.debugTarget()
85+
|| configuration
86+
.getFragment(PlatformConfiguration.class)
87+
.debugToolchainResolution(key.toolchainTypeLabel());
88+
89+
// Find the right one.
8790
return resolveConstraints(
8891
key.toolchainTypeLabel(),
8992
key.availableExecutionPlatformKeys(),

src/main/java/com/google/devtools/build/lib/skyframe/SingleToolchainResolutionValue.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,26 @@ public static SingleToolchainResolutionKey key(
3939
Label toolchainTypeLabel,
4040
ConfiguredTargetKey targetPlatformKey,
4141
List<ConfiguredTargetKey> availableExecutionPlatformKeys) {
42+
return key(
43+
configurationKey,
44+
toolchainTypeLabel,
45+
targetPlatformKey,
46+
availableExecutionPlatformKeys,
47+
false);
48+
}
49+
50+
public static SingleToolchainResolutionKey key(
51+
BuildConfigurationValue.Key configurationKey,
52+
Label toolchainTypeLabel,
53+
ConfiguredTargetKey targetPlatformKey,
54+
List<ConfiguredTargetKey> availableExecutionPlatformKeys,
55+
boolean debugTarget) {
4256
return SingleToolchainResolutionKey.create(
43-
configurationKey, toolchainTypeLabel, targetPlatformKey, availableExecutionPlatformKeys);
57+
configurationKey,
58+
toolchainTypeLabel,
59+
targetPlatformKey,
60+
availableExecutionPlatformKeys,
61+
debugTarget);
4462
}
4563

4664
/** {@link SkyKey} implementation used for {@link SingleToolchainResolutionFunction}. */
@@ -62,17 +80,21 @@ public SkyFunctionName functionName() {
6280

6381
abstract ImmutableList<ConfiguredTargetKey> availableExecutionPlatformKeys();
6482

83+
abstract boolean debugTarget();
84+
6585
@AutoCodec.Instantiator
6686
static SingleToolchainResolutionKey create(
6787
BuildConfigurationValue.Key configurationKey,
6888
Label toolchainTypeLabel,
6989
ConfiguredTargetKey targetPlatformKey,
70-
List<ConfiguredTargetKey> availableExecutionPlatformKeys) {
90+
List<ConfiguredTargetKey> availableExecutionPlatformKeys,
91+
boolean debugTarget) {
7192
return new AutoValue_SingleToolchainResolutionValue_SingleToolchainResolutionKey(
7293
configurationKey,
7394
toolchainTypeLabel,
7495
targetPlatformKey,
75-
ImmutableList.copyOf(availableExecutionPlatformKeys));
96+
ImmutableList.copyOf(availableExecutionPlatformKeys),
97+
debugTarget);
7698
}
7799
}
78100

src/main/java/com/google/devtools/build/lib/skyframe/ToolchainContextKey.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ public static Builder key() {
3333
return new AutoValue_ToolchainContextKey.Builder()
3434
.requiredToolchainTypeLabels(ImmutableSet.of())
3535
.execConstraintLabels(ImmutableSet.of())
36-
.forceExecutionPlatform(Optional.empty());
36+
.forceExecutionPlatform(Optional.empty())
37+
.debugTarget(false);
3738
}
3839

3940
@Override
@@ -49,6 +50,8 @@ public SkyFunctionName functionName() {
4950

5051
abstract Optional<Label> forceExecutionPlatform();
5152

53+
abstract boolean debugTarget();
54+
5255
/** Builder for {@link ToolchainContextKey}. */
5356
@AutoValue.Builder
5457
public interface Builder {
@@ -62,6 +65,12 @@ public interface Builder {
6265

6366
Builder execConstraintLabels(Label... execConstraintLabels);
6467

68+
Builder debugTarget(boolean flag);
69+
70+
default Builder debugTarget() {
71+
return this.debugTarget(true);
72+
}
73+
6574
ToolchainContextKey build();
6675

6776
Builder forceExecutionPlatform(Optional<Label> execPlatform);

src/main/java/com/google/devtools/build/lib/skyframe/ToolchainResolutionFunction.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,10 @@ public UnloadedToolchainContext compute(SkyKey skyKey, Environment env)
8181

8282
// Check if debug output should be generated.
8383
boolean debug =
84-
configuration
85-
.getFragment(PlatformConfiguration.class)
86-
.debugToolchainResolution(key.requiredToolchainTypeLabels());
84+
key.debugTarget()
85+
|| configuration
86+
.getFragment(PlatformConfiguration.class)
87+
.debugToolchainResolution(key.requiredToolchainTypeLabels());
8788

8889
// Load the configured target for the toolchain types to ensure that they are valid and
8990
// resolve aliases.
@@ -115,7 +116,8 @@ public UnloadedToolchainContext compute(SkyKey skyKey, Environment env)
115116
resolvedToolchainTypeLabels,
116117
key.forceExecutionPlatform().map(platformKeys::find),
117118
builder,
118-
platformKeys);
119+
platformKeys,
120+
key.debugTarget());
119121

120122
UnloadedToolchainContext unloadedToolchainContext = builder.build();
121123
if (debug) {
@@ -354,7 +356,8 @@ private static void determineToolchainImplementations(
354356
ImmutableSet<Label> requiredToolchainTypeLabels,
355357
Optional<ConfiguredTargetKey> forcedExecutionPlatform,
356358
UnloadedToolchainContextImpl.Builder builder,
357-
PlatformKeys platformKeys)
359+
PlatformKeys platformKeys,
360+
boolean debugTarget)
358361
throws InterruptedException, ValueMissingException, InvalidPlatformException,
359362
NoMatchingPlatformException, UnresolvedToolchainsException,
360363
InvalidToolchainLabelException {
@@ -367,7 +370,8 @@ private static void determineToolchainImplementations(
367370
configurationKey,
368371
toolchainTypeLabel,
369372
platformKeys.targetPlatformKey(),
370-
platformKeys.executionPlatformKeys()));
373+
platformKeys.executionPlatformKeys(),
374+
debugTarget));
371375
}
372376

373377
Map<SkyKey, ValueOrException2<NoToolchainFoundException, InvalidToolchainLabelException>>

src/test/shell/bazel/toolchain_test.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,29 @@ EOF
540540
expect_log 'Using toolchain: rule message: "this is the rule", toolchain extra_str: "foo from test_toolchain"'
541541
}
542542

543+
function test_toolchain_debug_messages_target {
544+
write_test_toolchain
545+
write_test_rule
546+
write_register_toolchain
547+
548+
mkdir -p demo
549+
cat >> demo/BUILD <<EOF
550+
load('//toolchain:rule_use_toolchain.bzl', 'use_toolchain')
551+
# Use the toolchain.
552+
use_toolchain(
553+
name = 'use',
554+
message = 'this is the rule')
555+
EOF
556+
557+
bazel build \
558+
--toolchain_resolution_debug=demo:use \
559+
--incompatible_auto_configure_host_platform \
560+
//demo:use &> $TEST_log || fail "Build failed"
561+
expect_log 'ToolchainResolution: Type //toolchain:test_toolchain: target platform @local_config_platform//.*: execution @local_config_platform//:host: Selected toolchain //:test_toolchain_impl_1'
562+
expect_log 'ToolchainResolution: Target platform @local_config_platform//.*: Selected execution platform @local_config_platform//:host, type //toolchain:test_toolchain -> toolchain //:test_toolchain_impl_1'
563+
expect_log 'Using toolchain: rule message: "this is the rule", toolchain extra_str: "foo from test_toolchain"'
564+
}
565+
543566
function test_toolchain_use_in_aspect {
544567
write_test_toolchain
545568
write_test_aspect

0 commit comments

Comments
 (0)