fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
92834930
rust-analyzer
1"""End to end tests for rust toolchains.""" 2 3load ( "@bazel_skylib//lib:unittest.bzl" , "analysistest" , "asserts" ) 4load ( "@bazel_skylib//rules:write_file.bzl" , "write_file" ) 5load ( "//rust:defs.bzl" , "rust_library" ) 6load ( "//rust:toolchain.bzl" , "rust_stdlib_filegroup" , "rust_toolchain" ) 7 8EXEC_TOOLCHAIN_FLAG = "missing" 9TOOLCHAIN_FLAG = "before" 10CONFIG_FLAG = "after" 11 12def _toolchain_adds_rustc_flags_impl ( ctx ): 13""" Tests adding extra_rustc_flags on the toolchain, asserts that: 14 15- extra_rustc_flags added by the toolchain are applied BEFORE flags added by a config on the commandline 16- The exec flags from the toolchain don't go on the commandline for a non-exec target 17""" 18env = analysistest . begin ( ctx ) 19target = analysistest . target_under_test ( env ) 20action = target [ DepActionsInfo ]. actions [ 0 ] 21 22asserts . equals ( env , "Rustc" , action . mnemonic ) 23 24asserts . true ( 25env , 26action . argv [ - 2 :] == [ TOOLCHAIN_FLAG , CONFIG_FLAG ], 27 "Unexpected rustc flags: {}\nShould have ended with : {} ". format ( 28action . argv , 29[ TOOLCHAIN_FLAG , CONFIG_FLAG ], 30), 31) 32 33asserts . true ( 34env , 35EXEC_TOOLCHAIN_FLAG not in action . argv , 36"Found exec toolchain flag ({}) in rustc flags: {}" . format ( EXEC_TOOLCHAIN_FLAG , action . argv ), 37) 38 39return analysistest . end ( env ) 40 41toolchain_adds_rustc_flags_test = analysistest . make ( 42_toolchain_adds_rustc_flags_impl , 43config_settings = { 44"@//:extra_rustc_flags" : [ CONFIG_FLAG ], 45}, 46) 47 48def _extra_toolchain_transition_impl ( settings , _attr ): 49return { "//command_line_option:extra_toolchains" : [ 50"@rules_rust//test/toolchain:extra_flags_toolchain" , 51] + settings [ "//command_line_option:extra_toolchains" ]} 52 53_extra_toolchain_transition = transition ( 54implementation = _extra_toolchain_transition_impl , 55inputs = [ "//command_line_option:extra_toolchains" ], 56outputs = [ "//command_line_option:extra_toolchains" ], 57) 58 59DepActionsInfo = provider ( 60"Contains information about dependencies actions." , 61fields = { "actions" : "List[Action]" }, 62) 63 64def _collect_dep_actions_aspect_impl ( target , ctx ): 65actions = [] 66actions . extend ( target . actions ) 67for dep in ctx . rule . attr . deps : 68actions . extend ( dep [ DepActionsInfo ]. actions ) 69return [ DepActionsInfo ( actions = actions )] 70 71collect_dep_actions_aspect = aspect ( 72implementation = _collect_dep_actions_aspect_impl , 73attr_aspects = [ "deps" ], 74) 75 76def _extra_toolchain_wrapper_impl ( ctx ): 77return [ ctx . attr . dep [ DepActionsInfo ]] 78 79extra_toolchain_wrapper = rule ( 80implementation = _extra_toolchain_wrapper_impl , 81attrs = { 82"dep" : attr . label ( aspects = [ collect_dep_actions_aspect ]), 83"_allowlist_function_transition" : attr . label ( 84default = "@bazel_tools//tools/allowlists/function_transition_allowlist" , 85), 86}, 87cfg = _extra_toolchain_transition , 88) 89 90def _define_targets (): 91rust_library ( 92name = "lib" , 93srcs = [ "lib.rs" ], 94edition = "2021" , 95) 96 97native . filegroup ( 98name = "stdlib_srcs" , 99srcs = [ "config.txt" ], 100) 101rust_stdlib_filegroup ( 102name = "std_libs" , 103srcs = [ ":stdlib_srcs" ], 104) 105write_file ( 106name = "mock_rustc" , 107out = "mock_rustc.exe" , 108content = [], 109is_executable = True , 110) 111write_file ( 112name = "mock_rustdoc" , 113out = "mock_rustdoc.exe" , 114content = [], 115is_executable = True , 116) 117 118rust_toolchain ( 119name = "rust_extra_flags_toolchain" , 120binary_ext = "" , 121dylib_ext = ".so" , 122exec_triple = "x86_64-unknown-none" , 123target_triple = "x86_64-unknown-none" , 124os = "linux" , 125rust_doc = ":mock_rustdoc" , 126rust_std = ":std_libs" , 127rustc = ":mock_rustc" , 128staticlib_ext = ".a" , 129stdlib_linkflags = [], 130extra_rustc_flags = [ TOOLCHAIN_FLAG ], 131extra_exec_rustc_flags = [ EXEC_TOOLCHAIN_FLAG ], 132) 133 134native . toolchain ( 135name = "extra_flags_toolchain" , 136toolchain = ":rust_extra_flags_toolchain" , 137toolchain_type = "@rules_rust//rust:toolchain" , 138) 139 140extra_toolchain_wrapper ( 141name = "lib_with_extra_toolchain" , 142dep = ":lib" , 143) 144 145def toolchain_test_suite ( name ): 146_define_targets () 147 148toolchain_adds_rustc_flags_test ( 149name = "toolchain_adds_rustc_flags_test" , 150target_under_test = ":lib_with_extra_toolchain" , 151) 152 153native . test_suite ( 154name = name , 155tests = [ 156":toolchain_adds_rustc_flags_test" , 157], 158)