Skip to content

Commit

Permalink
Convert --toolchain_resolution_debug to take a regex of toolchain typ…
Browse files Browse the repository at this point in the history
…es to debug

Work towards #12110.
Fixes #7713.

RELNOTES: The flag `--toolchain_resolution_debug` now takes a regex argument, which is used to check which toolchain types should have debug info printed. You may use `.*` as an argument to keep the current behavior of debugging every toolchain type.

Closes #13207.

PiperOrigin-RevId: 362413466
  • Loading branch information
katre authored and copybara-github committed Mar 12, 2021
1 parent 82dfcf0 commit fbf9f9d
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 38 deletions.
9 changes: 5 additions & 4 deletions site/docs/toolchains.md
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,8 @@ independently to each version of the target.

## Debugging toolchains

When adding toolchain support to an existing rule, use the
`--toolchain_resolution_debug` flag to make toolchain resolution verbose. Bazel
will output names of toolchains it is checking and skipping during the
resolution process.
If you are adding toolchain support to an existing rule, use the
`--toolchain_resolution_debug=regex` flag. During toolchain resolution, the flag
provides verbose output for toolchain types that match the regex variable. You
can use `.*` to output all information. Bazel will output names of toolchains it
checks and skips during the resolution process.
9 changes: 5 additions & 4 deletions site/docs/user-manual.html
Original file line number Diff line number Diff line change
Expand Up @@ -1627,11 +1627,12 @@ <h4 id="flag--extra_toolchains"><code class='flag'>--extra_toolchains=<var>label
register_toolchains()</a>.
</p>

<h4 id="flag--toolchain_resolution_debug"><code class='flag'>--toolchain_resolution_debug=false</code></h4>
<h4 id="flag--toolchain_resolution_debug"><code class='flag'>--toolchain_resolution_debug=<var>regex</var></code></h4>
<p>
Print debug information while finding toolchains for a rule. This might help
developers of Bazel or Starlark rules with debugging failures due to missing
toolchains.
Print debug information while finding toolchains if the toolchain type matches
the regex. Multiple regexes can be separated by commas. The regex can be
negated by using a <code>-</code> at the beginning. This might help developers
of Bazel or Starlark rules with debugging failures due to missing toolchains.
</p>

<h3 id='misc_build_options'>Miscellaneous</h3>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.google.devtools.build.lib.events.EventHandler;
import com.google.devtools.build.lib.starlarkbuildapi.platform.PlatformConfigurationApi;
import com.google.devtools.build.lib.util.RegexFilter;
import java.util.Collection;
import java.util.List;
import java.util.Map;

Expand All @@ -36,6 +37,7 @@ public class PlatformConfiguration extends Fragment implements PlatformConfigura
private final Label targetPlatform;
private final ImmutableList<String> extraToolchains;
private final List<Map.Entry<RegexFilter, List<Label>>> targetFilterToAdditionalExecConstraints;
private final RegexFilter toolchainResolutionDebugRegexFilter;

public PlatformConfiguration(BuildOptions buildOptions) {
PlatformOptions platformOptions = buildOptions.get(PlatformOptions.class);
Expand All @@ -46,6 +48,7 @@ public PlatformConfiguration(BuildOptions buildOptions) {
this.extraToolchains = ImmutableList.copyOf(platformOptions.extraToolchains);
this.targetFilterToAdditionalExecConstraints =
platformOptions.targetFilterToAdditionalExecConstraints;
this.toolchainResolutionDebugRegexFilter = platformOptions.toolchainResolutionDebug;
}

@Override
Expand Down Expand Up @@ -111,4 +114,23 @@ public List<Label> getAdditionalExecutionConstraintsFor(Label label) {
}
return constraints.build();
}

/** Returns true if toolchain resolution debug info should be printed for this toolchain type. */
public boolean debugToolchainResolution(Label toolchainType) {
return debugToolchainResolution(ImmutableList.of(toolchainType));
}

/**
* Returns true if toolchain resolution debug info should be printed for any of these toolchain
* types.
*/
public boolean debugToolchainResolution(Collection<Label> toolchainTypes) {
if (toolchainTypes.isEmpty()) {
// Check an empty string, in case the filter is .*
return this.toolchainResolutionDebugRegexFilter.test("");
}
return toolchainTypes.stream()
.map(Label::getCanonicalForm)
.anyMatch(toolchainType -> this.toolchainResolutionDebugRegexFilter.test(toolchainType));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,15 @@ public class PlatformOptions extends FragmentOptions {

@Option(
name = "toolchain_resolution_debug",
defaultValue = "false",
defaultValue = "-.*", // By default, exclude everything.
converter = RegexFilter.RegexFilterConverter.class,
documentationCategory = OptionDocumentationCategory.LOGGING,
effectTags = {OptionEffectTag.TERMINAL_OUTPUT},
help =
"Print debug information while finding toolchains for a rule. This might help developers "
+ "of Bazel or Starlark rules with debugging failures due to missing toolchains.")
public boolean toolchainResolutionDebug;
"Print debug information during toolchain resolution. The flag takes a regex, which is"
+ " checked against toolchain types to see which to debug. Multiple regexes may be"
+ " separated by commas, and then each regex is checked separately.")
public RegexFilter toolchainResolutionDebug;

@Option(
name = "incompatible_auto_configure_host_platform",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.devtools.build.lib.analysis.PlatformOptions;
import com.google.devtools.build.lib.analysis.PlatformConfiguration;
import com.google.devtools.build.lib.analysis.config.BuildConfiguration;
import com.google.devtools.build.lib.analysis.config.ConfigMatchingProvider;
import com.google.devtools.build.lib.analysis.platform.ConstraintCollection;
Expand Down Expand Up @@ -80,7 +80,10 @@ public SkyValue compute(SkyKey skyKey, Environment env)
}

// Find the right one.
boolean debug = configuration.getOptions().get(PlatformOptions.class).toolchainResolutionDebug;
boolean debug =
configuration
.getFragment(PlatformConfiguration.class)
.debugToolchainResolution(key.toolchainTypeLabel());
return resolveConstraints(
key.toolchainTypeLabel(),
key.availableExecutionPlatformKeys(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import com.google.common.collect.Iterables;
import com.google.common.collect.Table;
import com.google.devtools.build.lib.analysis.PlatformConfiguration;
import com.google.devtools.build.lib.analysis.PlatformOptions;
import com.google.devtools.build.lib.analysis.config.BuildConfiguration;
import com.google.devtools.build.lib.analysis.platform.ConstraintValueInfo;
import com.google.devtools.build.lib.analysis.platform.PlatformInfo;
Expand Down Expand Up @@ -81,7 +80,9 @@ public UnloadedToolchainContext compute(SkyKey skyKey, Environment env)

// Check if debug output should be generated.
boolean debug =
configuration.getOptions().get(PlatformOptions.class).toolchainResolutionDebug;
configuration
.getFragment(PlatformConfiguration.class)
.debugToolchainResolution(key.requiredToolchainTypeLabels());

// Load the configured target for the toolchain types to ensure that they are valid and
// resolve aliases.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ protected void useConfiguration(ImmutableMap<String, Object> starlarkOptions, St
ImmutableList.Builder<String> fullArgs = ImmutableList.builder();
fullArgs.add("--incompatible_enable_android_toolchain_resolution");
// Uncomment the below to get more info when tests fail because of toolchain resolution.
// fullArgs.add("--toolchain_resolution_debug");
// fullArgs.add("--toolchain_resolution_debug=tools/android:.*toolchain_type");
boolean hasPlatform = false;
for (String arg : args) {
if (arg.startsWith("--android_sdk=")) {
Expand Down
6 changes: 5 additions & 1 deletion src/test/shell/bazel/bazel_java_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1552,7 +1552,11 @@ platform(
}
)
EOF
bazel build --extra_execution_platforms=":my_platform" --toolchain_resolution_debug :a --execution_log_json_file out.txt &> $TEST_log || fail "Build failed"
bazel build \
--extra_execution_platforms=":my_platform" \
--toolchain_resolution_debug=.* \
--execution_log_json_file out.txt \
:a &> $TEST_log || fail "Build failed"
grep "key3" out.txt || fail "Did not find the target attribute key"
grep "child_value" out.txt || fail "Did not find the overriding value"
grep "key2" out.txt || fail "Did not find the platform key"
Expand Down
40 changes: 27 additions & 13 deletions src/test/shell/bazel/bazel_with_jdk_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -168,16 +168,22 @@ EOF
export JAVA_HOME="$PWD/jdk"
export PATH="$PWD/jdk/bin:$PATH"

bazel cquery --toolchain_resolution_debug --java_runtime_version=8 '//java/main:JavaExample' \
&>"${TEST_log}" || fail "Autodetecting a fake JDK version 8 and selecting it failed"
bazel cquery \
--toolchain_resolution_debug=tools/jdk:runtime_toolchain_type \
--java_runtime_version=8 \
//java/main:JavaExample &>"${TEST_log}" || fail "Autodetecting a fake JDK version 8 and selecting it failed"
expect_log "@bazel_tools//tools/jdk:runtime_toolchain_type -> toolchain @local_jdk//:jdk"

bazel cquery --toolchain_resolution_debug --java_runtime_version=local_jdk_8 '//java/main:JavaExample' \
&>"${TEST_log}" || fail "Autodetecting a fake JDK version 8 and selecting it failed"
bazel cquery \
--toolchain_resolution_debug=tools/jdk:runtime_toolchain_type \
--java_runtime_version=local_jdk_8 \
//java/main:JavaExample &>"${TEST_log}" || fail "Autodetecting a fake JDK version 8 and selecting it failed"
expect_log "@bazel_tools//tools/jdk:runtime_toolchain_type -> toolchain @local_jdk//:jdk"

bazel cquery --toolchain_resolution_debug --java_runtime_version=11 '//java/main:JavaExample' \
&>"${TEST_log}" || fail "Selecting prepackaged JDK version 11 failed"
bazel cquery \
--toolchain_resolution_debug=tools/jdk:runtime_toolchain_type \
--java_runtime_version=11 \
//java/main:JavaExample &>"${TEST_log}" || fail "Autodetecting a fake JDK version 8 and selecting it failed"
expect_not_log "@bazel_tools//tools/jdk:runtime_toolchain_type -> toolchain @local_jdk//:jdk"
}

Expand All @@ -197,16 +203,22 @@ EOF
export JAVA_HOME="$PWD/jdk"
export PATH="$PWD/jdk/bin:$PATH"

bazel cquery --toolchain_resolution_debug --java_runtime_version=11 '//java/main:JavaExample' \
&>"${TEST_log}" || fail "Autodetecting a fake JDK version 11 and selecting it failed"
bazel cquery \
--toolchain_resolution_debug=tools/jdk:runtime_toolchain_type \
--java_runtime_version=11 \
//java/main:JavaExample &>"${TEST_log}" || fail "Autodetecting a fake JDK version 11 and selecting it failed"
expect_log "@bazel_tools//tools/jdk:runtime_toolchain_type -> toolchain @local_jdk//:jdk"

bazel cquery --toolchain_resolution_debug --java_runtime_version=local_jdk_11 '//java/main:JavaExample' \
&>"${TEST_log}" || fail "Autodetecting a fake JDK version 8 and selecting it failed"
bazel cquery \
--toolchain_resolution_debug=tools/jdk:runtime_toolchain_type \
--java_runtime_version=local_jdk_11 \
//java/main:JavaExample &>"${TEST_log}" || fail "Autodetecting a fake JDK version 11 and selecting it failed"
expect_log "@bazel_tools//tools/jdk:runtime_toolchain_type -> toolchain @local_jdk//:jdk"

bazel cquery --toolchain_resolution_debug --java_runtime_version=15 '//java/main:JavaExample' \
&>"${TEST_log}" || fail "Selecting prepackaged JDK version 15 failed"
bazel cquery \
--toolchain_resolution_debug=tools/jdk:runtime_toolchain_type \
--java_runtime_version=15 \
//java/main:JavaExample &>"${TEST_log}" || fail "Autodetecting a fake JDK version 11 and selecting it failed"
expect_not_log "@bazel_tools//tools/jdk:runtime_toolchain_type -> toolchain @local_jdk//:jdk"
}

Expand All @@ -226,7 +238,9 @@ EOF
export JAVA_HOME="$PWD/jdk"
export PATH="$PWD/jdk/bin:$PATH"

bazel cquery --toolchain_resolution_debug '//java/main:JavaExample' &>"${TEST_log}" \
bazel cquery \
--toolchain_resolution_debug=tools/jdk:runtime_toolchain_type \
//java/main:JavaExample &>"${TEST_log}" \
|| fail "Failed to resolve Java toolchain when version cannot be detected"
expect_log "@bazel_tools//tools/jdk:runtime_toolchain_type -> toolchain @local_jdk//:jdk"
}
Expand Down
6 changes: 5 additions & 1 deletion src/test/shell/bazel/platforms_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,11 @@ platform(
}
)
EOF
bazel build --extra_execution_platforms=":my_platform" --toolchain_resolution_debug :a --execution_log_json_file out.txt &> $TEST_log || fail "Build failed"
bazel build \
--extra_execution_platforms=":my_platform" \
--toolchain_resolution_debug=.* \
--execution_log_json_file out.txt \
:a &> $TEST_log || fail "Build failed"
grep "key3" out.txt || fail "Did not find the target attribute key"
grep "child_value" out.txt || fail "Did not find the overriding value"
grep "key2" out.txt || fail "Did not find the platform key"
Expand Down
12 changes: 6 additions & 6 deletions src/test/shell/bazel/toolchain_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ use_toolchain(
EOF

bazel build \
--toolchain_resolution_debug \
--toolchain_resolution_debug=toolchain:test_toolchain \
--incompatible_auto_configure_host_platform \
//demo:use &> $TEST_log || fail "Build failed"
expect_log 'ToolchainResolution: Type //toolchain:test_toolchain: target platform @local_config_platform//.*: execution @local_config_platform//:host: Selected toolchain //:test_toolchain_impl_1'
Expand Down Expand Up @@ -904,14 +904,14 @@ EOF

# When no platform has the constraint, an error
bazel build \
--toolchain_resolution_debug \
--toolchain_resolution_debug=.* \
//demo:target &> $TEST_log && fail "Build failure expected"
expect_log "While resolving toolchains for target //demo:target: .* from available execution platforms \[\]"

# When the platform exists, it is used.
bazel build \
--extra_execution_platforms=//platform:test_platform \
--toolchain_resolution_debug \
--toolchain_resolution_debug=.* \
//demo:target &> $TEST_log || fail "Build failed"
expect_log "Selected execution platform //platform:test_platform"
}
Expand Down Expand Up @@ -964,7 +964,7 @@ EOF
# Build the target, using debug messages to verify the correct platform was selected.
bazel build \
--extra_execution_platforms=//platforms:all \
--toolchain_resolution_debug \
--toolchain_resolution_debug=toolchain:test_toolchain \
//demo:use &> $TEST_log || fail "Build failed"
expect_log "Selected execution platform //platforms:platform2"
}
Expand Down Expand Up @@ -1020,7 +1020,7 @@ EOF
# Build the target, using debug messages to verify the correct platform was selected.
bazel build \
--extra_execution_platforms=//platforms:all \
--toolchain_resolution_debug \
--toolchain_resolution_debug=toolchain:test_toolchain \
//demo:use &> $TEST_log || fail "Build failed"
expect_log "Selected execution platform //platforms:platform2"
}
Expand Down Expand Up @@ -1090,7 +1090,7 @@ EOF
# Build the target, using debug messages to verify the correct platform was selected.
bazel build \
--extra_execution_platforms=//platforms:all \
--toolchain_resolution_debug \
--toolchain_resolution_debug=toolchain:test_toolchain \
//demo:use &> $TEST_log || fail "Build failed"
expect_log "Selected execution platform //platforms:platform2_4"
}
Expand Down

0 comments on commit fbf9f9d

Please sign in to comment.