Make toolchain.toolchain configurable.

This attribute was previously non-configurable but as it turns out this restriction is not necessary. Loosening it allows toolchain authors to more easily switch between different toolchain implementations based on a select().

RELNOTES: None.
PiperOrigin-RevId: 262599150
diff --git a/src/main/java/com/google/devtools/build/lib/rules/platform/ToolchainRule.java b/src/main/java/com/google/devtools/build/lib/rules/platform/ToolchainRule.java
index c1aa78b..d5703f2 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/platform/ToolchainRule.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/platform/ToolchainRule.java
@@ -84,10 +84,7 @@
         <!-- #END_BLAZE_RULE.ATTRIBUTE --> */
         // This needs to not introduce a dependency so that we can load the toolchain only if it is
         // needed.
-        .add(
-            attr(TOOLCHAIN_ATTR, BuildType.NODEP_LABEL)
-                .mandatory()
-                .nonconfigurable("part of toolchain configuration"))
+        .add(attr(TOOLCHAIN_ATTR, BuildType.NODEP_LABEL).mandatory())
         .build();
   }
 
diff --git a/src/test/shell/bazel/toolchain_test.sh b/src/test/shell/bazel/toolchain_test.sh
index 1b75ce1..c37fe28 100755
--- a/src/test/shell/bazel/toolchain_test.sh
+++ b/src/test/shell/bazel/toolchain_test.sh
@@ -1559,6 +1559,84 @@
   grep "config2 selected" bazel-bin/demo.log || fail "config2 expected"
 }
 
+function test_toolchain_modes {
+  write_test_toolchain foo_toolchain
+  write_test_rule test_rule foo_toolchain
+
+  mkdir -p project
+  cat > project/flags.bzl <<EOF
+def _impl(ctx):
+  pass
+
+string_flag = rule(
+    implementation = _impl,
+    build_setting = config.string(flag = True),
+)
+EOF
+
+  cat >> project/BUILD <<EOF
+load('//toolchain:toolchain_foo_toolchain.bzl', 'foo_toolchain')
+load('//toolchain:rule_test_rule.bzl', 'test_rule')
+load('//project:flags.bzl', 'string_flag')
+
+string_flag(
+  name = 'version',
+  build_setting_default = 'production'
+)
+
+config_setting(
+  name = 'production',
+  flag_values = {
+    ':version': 'production'
+  }
+)
+
+config_setting(
+  name = 'unstable',
+  flag_values = {
+    ':version': 'unstable'
+  }
+)
+
+filegroup(name = 'dep')
+foo_toolchain(
+    name = 'production_toolchain',
+    extra_label = ':dep',
+    extra_str = 'production',
+)
+
+foo_toolchain(
+    name = 'unstable_toolchain',
+    extra_label = ':dep',
+    extra_str = 'unstable',
+)
+
+toolchain(
+    name = 'toolchain',
+    toolchain_type = '//toolchain:foo_toolchain',
+    toolchain = select({
+      ':production': ':production_toolchain',
+      ':unstable': ':unstable_toolchain',
+    })
+)
+
+test_rule(
+  name = 'test',
+  message = 'hello',
+)
+EOF
+
+  cat >> WORKSPACE <<EOF
+register_toolchains('//project:toolchain')
+EOF
+
+  bazel build //project:test &> "${TEST_log}" || fail "Build failed"
+  expect_log 'Using toolchain: rule message: "hello", toolchain extra_str: "production"'
+
+  bazel build --//project:version="unstable" //project:test &> "${TEST_log}" || fail "Build failed"
+  expect_log 'Using toolchain: rule message: "hello", toolchain extra_str: "unstable"'
+}
+
 # TODO(katre): Test using toolchain-provided make variables from a genrule.
 
 run_suite "toolchain tests"