#!/bin/bash # # Copyright 2017 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. # # Test the providers and rules related to toolchains. # # Load the test setup defined in the parent directory CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" source "${CURRENT_DIR}/../integration_test_setup.sh" \ || { echo "integration_test_setup.sh not found!" >&2; exit 1; } function set_up() { create_new_workspace # Create shared report rule for printing toolchain info. mkdir report touch report/BUILD cat >>report/report.bzl <> toolchain/toolchain_${toolchain_name}.bzl <> toolchain/BUILD <> toolchain/rule_${rule_name}.bzl <> toolchain/aspect_${aspect_name}.bzl <> WORKSPACE <> BUILD <> BUILD < $TEST_log || fail "Build failed" expect_log 'extra_label = "//:dep_rule"' expect_log 'extra_str = "bar"' } function test_toolchain_use_in_rule { write_test_toolchain write_test_rule write_register_toolchain mkdir -p demo cat >> demo/BUILD < $TEST_log || fail "Build failed" expect_log 'Using toolchain: rule message: "this is the rule", toolchain extra_str: "foo from test_toolchain"' } function test_toolchain_use_in_rule_missing { write_test_toolchain write_test_rule #rite_register_toolchain # Do not register test_toolchain to trigger the error. mkdir -p demo cat >> demo/BUILD < $TEST_log && fail "Build failure expected" expect_log 'While resolving toolchains for target //demo:use: no matching toolchains found for types //toolchain:test_toolchain' } function test_multiple_toolchain_use_in_rule { write_test_toolchain test_toolchain_1 write_test_toolchain test_toolchain_2 write_register_toolchain test_toolchain_1 write_register_toolchain test_toolchain_2 # The rule uses two separate toolchains. mkdir -p toolchain cat >> toolchain/rule_use_toolchains.bzl <> demo/BUILD < $TEST_log || fail "Build failed" expect_log 'Using toolchain: rule message: "this is the rule", toolchain 1 extra_str: "foo from test_toolchain_1", toolchain 2 extra_str: "foo from test_toolchain_2"' } function test_multiple_toolchain_use_in_rule_one_missing { write_test_toolchain test_toolchain_1 write_test_toolchain test_toolchain_2 write_register_toolchain test_toolchain_1 # Do not register test_toolchain_2 to cause the error, # The rule uses two separate toolchains. mkdir -p toolchain cat >> toolchain/rule_use_toolchains.bzl <> demo/BUILD < $TEST_log && fail "Build failure expected" expect_log 'While resolving toolchains for target //demo:use: no matching toolchains found for types //toolchain:test_toolchain_2' } function test_toolchain_use_in_rule_non_required_toolchain { write_test_toolchain write_register_toolchain # The rule argument toolchains requires one toolchain, but the implementation requests a different # one. mkdir -p toolchain cat >> toolchain/rule_use_toolchain.bzl <> demo/BUILD < $TEST_log && fail "Build failure expected" expect_log 'In use_toolchain rule //demo:use, toolchain type //toolchain:wrong_toolchain was requested but only types \[//toolchain:test_toolchain\] are configured' } function test_toolchain_debug_messages { write_test_toolchain write_test_rule write_register_toolchain mkdir -p demo cat >> demo/BUILD < $TEST_log || fail "Build failed" expect_log 'ToolchainResolution: Looking for toolchain of type //toolchain:test_toolchain' expect_log 'ToolchainResolution: For toolchain type //toolchain:test_toolchain, possible execution platforms and toolchains: {@bazel_tools//platforms:host_platform -> //:test_toolchain_impl_1}' expect_log 'ToolchainUtil: Selected execution platform @bazel_tools//platforms:host_platform, type //toolchain:test_toolchain -> toolchain //:test_toolchain_impl_1' expect_log 'Using toolchain: rule message: "this is the rule", toolchain extra_str: "foo from test_toolchain"' } function test_toolchain_use_in_aspect { write_test_toolchain write_test_aspect write_register_toolchain mkdir -p demo cat >> demo/demo.bzl <> demo/BUILD < $TEST_log || fail "Build failed" expect_log 'Using toolchain in aspect: rule message: "bar from demo", toolchain extra_str: "foo from test_toolchain"' } function test_toolchain_use_in_aspect_non_required_toolchain { write_test_toolchain write_register_toolchain # The aspect argument toolchains requires one toolchain, but the implementation requests a # different one. mkdir -p toolchain cat >> toolchain/aspect_use_toolchain.bzl <> demo/demo.bzl <> demo/BUILD < $TEST_log && fail "Build failure expected" expect_log 'In aspect //toolchain:aspect_use_toolchain.bzl%use_toolchain applied to demo rule //demo:demo, toolchain type //toolchain:wrong_toolchain was requested but only types \[//toolchain:test_toolchain\] are configured' } function test_toolchain_constraints() { write_test_toolchain write_test_rule cat >> WORKSPACE <> BUILD <> demo/BUILD < $TEST_log || fail "Build failed" expect_log 'Using toolchain: rule message: "this is the rule", toolchain extra_str: "foo from 1"' # This should use toolchain_2. bazel build \ --host_platform=//:platform2 \ --platforms=//:platform1 \ //demo:use &> $TEST_log || fail "Build failed" expect_log 'Using toolchain: rule message: "this is the rule", toolchain extra_str: "foo from 2"' # This should not match any toolchains. bazel build \ --host_platform=//:platform1 \ --platforms=//:platform1 \ //demo:use &> $TEST_log && fail "Build failure expected" expect_log 'While resolving toolchains for target //demo:use: no matching toolchains found for types //toolchain:test_toolchain' expect_not_log 'Using toolchain: rule message:' } function test_register_toolchain_error_invalid_label() { write_test_toolchain write_test_rule write_register_toolchain cat >> WORKSPACE <> demo/BUILD < $TEST_log && fail "Build failure expected" expect_log "invalid registered toolchain '/:invalid:label:syntax': not a valid absolute pattern" } function test_register_toolchain_error_invalid_target() { write_test_toolchain write_test_rule write_register_toolchain cat > WORKSPACE <> demo/BUILD < $TEST_log && fail "Build failure expected" expect_log "While resolving toolchains for target //demo:use: invalid registered toolchain '//demo:not_a_target': no such target '//demo:not_a_target': target 'not_a_target' not declared in package 'demo'" } function test_register_toolchain_error_target_not_a_toolchain() { write_test_toolchain write_test_rule write_register_toolchain cat >> WORKSPACE <> demo/out.log<> demo/BUILD < $TEST_log && fail "Build failure expected" expect_log "While resolving toolchains for target //demo:use: invalid registered toolchain '//demo:invalid': target does not provide the DeclaredToolchainInfo provider" } function test_register_toolchain_error_invalid_pattern() { cat >WORKSPACE <rules.bzl <BUILD < $TEST_log && fail "Build failure expected" # It's uncertain which error will happen first, so handle either. expect_log "While resolving toolchains for target //:foo: invalid registered toolchain '//:bad[12]': no such target" } function test_toolchain_error_invalid_target() { write_test_toolchain write_test_rule # Write toolchain with an invalid target. mkdir -p invalid cat > invalid/BUILD < WORKSPACE <> demo/BUILD < $TEST_log && fail "Build failure expected" expect_log "While resolving toolchains for target //demo:use: no such target '//toolchain:does_not_exist': target 'does_not_exist' not declared in package 'toolchain'" } function test_platforms_options_error_invalid_target() { write_test_toolchain write_test_rule write_register_toolchain mkdir -p demo cat >> demo/BUILD <> platform/BUILD < $TEST_log && fail "Build failure expected" expect_log "While resolving toolchains for target //demo:use: Target //platform:not_a_platform was referenced as a platform, but does not provide PlatformInfo" bazel build --host_platform=//platform:not_a_platform //demo:use &> $TEST_log && fail "Build failure expected" expect_log "While resolving toolchains for target //demo:use: Target //platform:not_a_platform was referenced as a platform, but does not provide PlatformInfo" } function test_native_rule_target_exec_constraints() { mkdir -p platform cat >> platform/BUILD <> demo/BUILD < \$@ """, exec_compatible_with = [ "//platform:test_enabled", ], ) EOF # When no platform has the constraint, an error bazel build \ --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 \ //demo:target &> $TEST_log || fail "Build failed" expect_log "ToolchainUtil: Selected execution platform //platform:test_platform" } function test_rule_with_default_execution_constraints() { write_test_toolchain write_register_toolchain # Add test platforms. mkdir -p platforms cat >> platforms/BUILD <> demo/rule.bzl <> demo/BUILD < $TEST_log || fail "Build failed" expect_log "Selected execution platform //platforms:platform2" } function test_target_with_execution_constraints() { write_test_toolchain write_register_toolchain # Add test platforms. mkdir -p platforms cat >> platforms/BUILD <> demo/rule.bzl <> demo/BUILD < $TEST_log || fail "Build failed" expect_log "Selected execution platform //platforms:platform2" } function test_rule_and_target_with_execution_constraints() { write_test_toolchain write_register_toolchain # Add test platforms. mkdir -p platforms cat >> platforms/BUILD <> demo/rule.bzl <> demo/BUILD < $TEST_log || fail "Build failed" expect_log "Selected execution platform //platforms:platform2_4" } run_suite "toolchain tests"