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