fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
c3d71edd
cleanup
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" , "rust_shared_library" ) 6load ( "//rust:toolchain.bzl" , "rust_stdlib_filegroup" , "rust_toolchain" ) 7 8EXEC_TOOLCHAIN_FLAG = "missing" 9TOOLCHAIN_FLAG = "before" 10CONFIG_FLAG = "after" 11CRATE_FLAGS = { "cdylib" : [ "cdylib_flag" ], "rlib" : [ "rlib_flag" ]} 12TARGET_FLAG = "-Ccodegen-units=1" 13 14def _toolchain_adds_rustc_flags_impl ( ctx , crate_type ): 15""" Tests adding extra_rustc_flags on the toolchain, asserts that: 16 17- extra_rustc_flags added by the toolchain are applied BEFORE flags added by a config on the commandline 18- The exec flags from the toolchain don't go on the commandline for a non-exec target 19- crate type rustc flags are added 20- target specific rustc flags are added AFTER the crate type rustc flags 21""" 22env = analysistest . begin ( ctx ) 23target = analysistest . target_under_test ( env ) 24action = target [ DepActionsInfo ]. actions [ 0 ] 25 26asserts . equals ( env , "Rustc" , action . mnemonic ) 27 28asserts . true ( 29env , 30action . argv [ - 3 :] == [ TOOLCHAIN_FLAG , CONFIG_FLAG , TARGET_FLAG ], 31 "Unexpected rustc flags: {}\nShould have ended with : {} ". format ( 32action . argv , 33[ TOOLCHAIN_FLAG , CONFIG_FLAG , TARGET_FLAG ], 34), 35) 36 37asserts . true ( 38env , 39action . argv [ - 4 ] == CRATE_FLAGS [ crate_type ][ 0 ], 40 "Unexpected rustc flags: {}\nShould have contained : {} ". format ( 41action . argv , 42CRATE_FLAGS [ "rlib" ], 43), 44) 45 46for type in CRATE_FLAGS . keys (): 47if type == crate_type : 48 continue 49asserts . false ( 50env , 51CRATE_FLAGS [ type ][ 0 ] in action . argv , 52 "Unexpected rustc flags: {}\nShould not contain : {} ". format ( 53action . argv , 54CRATE_FLAGS [ type ], 55), 56) 57 58asserts . true ( 59env , 60EXEC_TOOLCHAIN_FLAG not in action . argv , 61"Found exec toolchain flag ({}) in rustc flags: {}" . format ( EXEC_TOOLCHAIN_FLAG , action . argv ), 62) 63 64found_sysroot = False 65for arg in action . argv : 66if arg . startswith ( "--sysroot" ) and arg . endswith ( "test/toolchain/rust_extra_flags_toolchain" ): 67found_sysroot = True 68asserts . true ( 69env , 70found_sysroot , 71"Missing --sysroot flag or --sysroot does not point to correct sysroot directory" , 72) 73 74return analysistest . end ( env ) 75 76def _toolchain_adds_rustc_flags_lib_impl ( ctx ): 77return _toolchain_adds_rustc_flags_impl ( ctx , "rlib" ) 78 79def _toolchain_adds_rustc_flags_shared_lib_impl ( ctx ): 80return _toolchain_adds_rustc_flags_impl ( ctx , "cdylib" ) 81 82toolchain_adds_rustc_flags_lib_test = analysistest . make ( 83_toolchain_adds_rustc_flags_lib_impl , 84config_settings = { 85str ( Label ( "//rust/settings:extra_rustc_flags" )): [ CONFIG_FLAG ], 86str ( Label ( "//rust/settings:toolchain_generated_sysroot" )): True , 87}, 88) 89 90toolchain_adds_rustc_flags_shared_lib_test = analysistest . make ( 91_toolchain_adds_rustc_flags_shared_lib_impl , 92config_settings = { 93str ( Label ( "//rust/settings:extra_rustc_flags" )): [ CONFIG_FLAG ], 94str ( Label ( "//rust/settings:toolchain_generated_sysroot" )): True , 95}, 96) 97 98def _extra_toolchain_transition_impl ( settings , _attr ): 99return { "//command_line_option:extra_toolchains" : [ 100"@rules_rust//test/toolchain:extra_flags_toolchain" , 101] + settings [ "//command_line_option:extra_toolchains" ]} 102 103_extra_toolchain_transition = transition ( 104implementation = _extra_toolchain_transition_impl , 105inputs = [ "//command_line_option:extra_toolchains" ], 106outputs = [ "//command_line_option:extra_toolchains" ], 107) 108 109DepActionsInfo = provider ( 110"Contains information about dependencies actions." , 111fields = { "actions" : "List[Action]" }, 112) 113 114def _collect_dep_actions_aspect_impl ( target , ctx ): 115actions = [] 116actions . extend ( target . actions ) 117for dep in ctx . rule . attr . deps : 118actions . extend ( dep [ DepActionsInfo ]. actions ) 119return [ DepActionsInfo ( actions = actions )] 120 121collect_dep_actions_aspect = aspect ( 122implementation = _collect_dep_actions_aspect_impl , 123attr_aspects = [ "deps" ], 124) 125 126def _extra_toolchain_wrapper_impl ( ctx ): 127return [ ctx . attr . dep [ DepActionsInfo ]] 128 129extra_toolchain_wrapper = rule ( 130implementation = _extra_toolchain_wrapper_impl , 131attrs = { 132"dep" : attr . label ( aspects = [ collect_dep_actions_aspect ]), 133"_allowlist_function_transition" : attr . label ( 134default = "@bazel_tools//tools/allowlists/function_transition_allowlist" , 135), 136}, 137cfg = _extra_toolchain_transition , 138) 139 140def _define_targets (): 141rust_library ( 142name = "lib" , 143srcs = [ "lib.rs" ], 144edition = "2021" , 145rustc_flags = [ TARGET_FLAG ], 146) 147 148rust_shared_library ( 149name = "shared_lib" , 150srcs = [ "lib.rs" ], 151edition = "2021" , 152rustc_flags = [ TARGET_FLAG ], 153) 154 155native . filegroup ( 156name = "stdlib_srcs" , 157srcs = [ "config.txt" ], 158) 159rust_stdlib_filegroup ( 160name = "std_libs" , 161srcs = [ ":stdlib_srcs" ], 162) 163write_file ( 164name = "mock_rustc" , 165out = "mock_rustc.exe" , 166content = [], 167is_executable = True , 168) 169write_file ( 170name = "mock_rustdoc" , 171out = "mock_rustdoc.exe" , 172content = [], 173is_executable = True , 174) 175 176rust_toolchain ( 177name = "rust_extra_flags_toolchain" , 178binary_ext = "" , 179dylib_ext = ".so" , 180exec_triple = "x86_64-unknown-none" , 181target_triple = "x86_64-unknown-none" , 182rust_doc = ":mock_rustdoc" , 183rust_std = ":std_libs" , 184rustc = ":mock_rustc" , 185staticlib_ext = ".a" , 186stdlib_linkflags = [], 187extra_rustc_flags = [ TOOLCHAIN_FLAG ], 188extra_exec_rustc_flags = [ EXEC_TOOLCHAIN_FLAG ], 189extra_rustc_flags_for_crate_types = CRATE_FLAGS , 190visibility = [ "//visibility:public" ], 191) 192 193native . toolchain ( 194name = "extra_flags_toolchain" , 195toolchain = ":rust_extra_flags_toolchain" , 196toolchain_type = "@rules_rust//rust:toolchain" , 197) 198 199extra_toolchain_wrapper ( 200name = "lib_with_extra_toolchain" , 201dep = ":lib" , 202) 203 204extra_toolchain_wrapper ( 205name = "shared_lib_with_extra_toolchain" , 206dep = ":shared_lib" , 207) 208 209def _rust_stdlib_filegroup_provides_runfiles_test_impl ( ctx ): 210env = analysistest . begin ( ctx ) 211target = analysistest . target_under_test ( env ) 212runfiles = target [ DefaultInfo ]. default_runfiles 213asserts . true ( env , len ( runfiles . files . to_list ()) > 0 ) 214 215return analysistest . end ( env ) 216 217rust_stdlib_filegroup_provides_runfiles_test = analysistest . make ( 218_rust_stdlib_filegroup_provides_runfiles_test_impl , 219) 220 221def toolchain_test_suite ( name ): 222""" Instantiates tests for rust toolchains. 223 224Args: 225name: a name for the test suite 226""" 227_define_targets () 228 229toolchain_adds_rustc_flags_lib_test ( 230name = "toolchain_adds_rustc_flags_lib_test" , 231target_under_test = ":lib_with_extra_toolchain" , 232) 233 234toolchain_adds_rustc_flags_shared_lib_test ( 235name = "toolchain_adds_rustc_flags_shared_lib_test" , 236target_under_test = ":shared_lib_with_extra_toolchain" , 237) 238 239rust_stdlib_filegroup_provides_runfiles_test ( 240name = "rust_stdlib_filegroup_provides_runfiles_test" , 241target_under_test = ":std_libs" , 242) 243 244native . test_suite ( 245name = name , 246tests = [ 247":toolchain_adds_rustc_flags_lib_test" , 248":toolchain_adds_rustc_flags_shared_lib_test" , 249":rust_stdlib_filegroup_provides_runfiles_test" , 250], 251)