1# Copyright 2024 The Bazel Authors. All rights reserved. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14"""Tests for the cc_tool_map rule.""" 15 16load("//cc/toolchains:cc_toolchain_info.bzl", "ActionTypeInfo", "ToolConfigInfo") 17load("//cc/toolchains:tool_map.bzl", "cc_tool_map") 18load("//tests/rule_based_toolchain:subjects.bzl", "subjects") 19load("//tests/rule_based_toolchain:testing_rules.bzl", "analysis_test", "expect_failure_test", "helper_target") 20 21_ALL_ACTIONS = "//cc/toolchains/actions:all_actions" 22_C_COMPILE = "//cc/toolchains/actions:c_compile" 23_CPP_COMPILE = "//cc/toolchains/actions:cpp_compile" 24_ALL_CPP_COMPILE = "//cc/toolchains/actions:cpp_compile_actions" 25_STRIP = "//cc/toolchains/actions:strip" 26_LINK_DYNAMIC_LIBRARY = "//cc/toolchains/actions:cpp_link_executable" 27_BIN = "//tests/rule_based_toolchain/testdata:bin" 28_BIN_WRAPPER = "//tests/rule_based_toolchain/testdata:bin_wrapper" 29 30def valid_config_test(name): 31 subject_name = "_%s_subject" % name 32 cc_tool_map( 33 name = subject_name, 34 tools = { 35 _LINK_DYNAMIC_LIBRARY: _BIN, 36 _C_COMPILE: _BIN_WRAPPER, 37 _ALL_CPP_COMPILE: _BIN, 38 }, 39 ) 40 41 analysis_test( 42 name = name, 43 impl = _valid_config_test_impl, 44 targets = { 45 "c_compile": _C_COMPILE, 46 "cpp_compile": _CPP_COMPILE, 47 "link_dynamic_library": _LINK_DYNAMIC_LIBRARY, 48 "strip": _STRIP, 49 "subject": subject_name, 50 }, 51 ) 52 53def _valid_config_test_impl(env, targets): 54 configs = env.expect.that_target(targets.subject).provider(ToolConfigInfo).configs() 55 56 configs.contains(targets.strip[ActionTypeInfo]).equals(False) 57 configs.get(targets.c_compile[ActionTypeInfo]).exe().path().split("/").offset(-1, subjects.str).equals("bin_wrapper") 58 configs.get(targets.cpp_compile[ActionTypeInfo]).exe().path().split("/").offset(-1, subjects.str).equals("bin") 59 configs.get(targets.link_dynamic_library[ActionTypeInfo]).exe().path().split("/").offset(-1, subjects.str).equals("bin") 60 61def duplicate_action_test(name): 62 subject_name = "_%s_subject" % name 63 helper_target( 64 cc_tool_map, 65 name = subject_name, 66 tools = { 67 _C_COMPILE: _BIN_WRAPPER, 68 _ALL_ACTIONS: _BIN, 69 }, 70 ) 71 72 expect_failure_test( 73 name = name, 74 target = subject_name, 75 failure_message = "appears multiple times in your tool_map", 76 ) 77