1"""Unittest to verify compile_data (attribute) propagation""" 2 3load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts") 4load("//rust:defs.bzl", "rust_library") 5load("//test/unit:common.bzl", "assert_argv_contains", "assert_argv_contains_not") 6 7EXTRA_FLAG = "--codegen=linker-plugin-lto" 8 9def target_action_contains_not_flag(env, target): 10 action = target.actions[0] 11 asserts.equals(env, "Rustc", action.mnemonic) 12 13 assert_argv_contains_not( 14 env = env, 15 action = action, 16 flag = EXTRA_FLAG, 17 ) 18 19def target_action_contains_flag(env, target): 20 action = target.actions[0] 21 asserts.equals(env, "Rustc", action.mnemonic) 22 23 assert_argv_contains( 24 env = env, 25 action = action, 26 flag = EXTRA_FLAG, 27 ) 28 29def _extra_rustc_flags_not_present_test(ctx): 30 env = analysistest.begin(ctx) 31 target = analysistest.target_under_test(env) 32 target_action_contains_not_flag(env, target) 33 34 return analysistest.end(env) 35 36def _extra_rustc_flags_present_test(ctx): 37 env = analysistest.begin(ctx) 38 target = analysistest.target_under_test(env) 39 target_action_contains_flag(env, target) 40 41 # Check the exec configuration target does NOT contain. 42 target = ctx.attr.lib_exec 43 target_action_contains_not_flag(env, target) 44 45 return analysistest.end(env) 46 47extra_rustc_flags_not_present_test = analysistest.make(_extra_rustc_flags_not_present_test) 48extra_rustc_flags_present_test = analysistest.make( 49 _extra_rustc_flags_present_test, 50 attrs = { 51 "lib_exec": attr.label( 52 mandatory = True, 53 cfg = "exec", 54 ), 55 }, 56 config_settings = { 57 str(Label("//:extra_rustc_flags")): [EXTRA_FLAG], 58 }, 59) 60 61extra_rustc_flag_present_test = analysistest.make( 62 _extra_rustc_flags_present_test, 63 attrs = { 64 "lib_exec": attr.label( 65 mandatory = True, 66 cfg = "exec", 67 ), 68 }, 69 config_settings = { 70 str(Label("//:extra_rustc_flag")): [EXTRA_FLAG], 71 str(Label("//:extra_rustc_flags")): [], 72 }, 73) 74 75def _define_test_targets(): 76 rust_library( 77 name = "lib", 78 srcs = ["lib.rs"], 79 edition = "2018", 80 ) 81 82def extra_rustc_flags_test_suite(name): 83 """Entry-point macro called from the BUILD file. 84 85 Args: 86 name (str): Name of the macro. 87 """ 88 89 _define_test_targets() 90 91 extra_rustc_flags_not_present_test( 92 name = "extra_rustc_flags_not_present_test", 93 target_under_test = ":lib", 94 ) 95 96 extra_rustc_flags_present_test( 97 name = "extra_rustc_flags_present_test", 98 target_under_test = ":lib", 99 lib_exec = ":lib", 100 ) 101 102 extra_rustc_flag_present_test( 103 name = "extra_rustc_flag_present_test", 104 target_under_test = ":lib", 105 lib_exec = ":lib", 106 ) 107 108 native.test_suite( 109 name = name, 110 tests = [ 111 ":extra_rustc_flags_not_present_test", 112 ":extra_rustc_flags_present_test", 113 ], 114 ) 115