fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
d0d5a861
patching
1"""Unittests for rust rules.""" 2 3load ( "@bazel_skylib//lib:unittest.bzl" , "analysistest" , "asserts" ) 4load ( "@rules_cc//cc:defs.bzl" , "cc_library" ) 5load ( "//rust:defs.bzl" , "rust_binary" , "rust_library" , "rust_proc_macro" , "rust_shared_library" , "rust_static_library" ) 6load ( 7"//test/unit:common.bzl" , 8"assert_argv_contains" , 9"assert_argv_contains_not" , 10"assert_argv_contains_prefix" , 11"assert_argv_contains_prefix_not" , 12"assert_argv_contains_prefix_suffix" , 13"assert_list_contains_adjacent_elements" , 14) 15 16def _get_toolchain ( ctx ): 17return ctx . attr . _toolchain [ platform_common . ToolchainInfo ] 18 19def _get_darwin_component ( arg ): 20# path/to/darwin_x86_64-fastbuild-fastbuild/package -> darwin_x86_64-fastbuild 21darwin_component = [ x for x in arg . split ( "/" ) if x . startswith ( "darwin" )][ 0 ] 22 23# darwin_x86_64-fastbuild -> darwin 24return darwin_component . split ( "-" )[ 0 ] 25 26def _rlib_has_no_native_libs_test_impl ( ctx ): 27env = analysistest . begin ( ctx ) 28tut = analysistest . target_under_test ( env ) 29action = tut . actions [ 0 ] 30assert_argv_contains ( env , action , "--crate-type=rlib" ) 31assert_argv_contains_not ( env , action , "-lstatic=native_dep" ) 32assert_argv_contains_not ( env , action , "-ldylib=native_dep" ) 33assert_argv_contains_prefix_not ( env , action , "--codegen=linker=" ) 34return analysistest . end ( env ) 35 36def _cdylib_has_native_libs_test_impl ( ctx ): 37env = analysistest . begin ( ctx ) 38tut = analysistest . target_under_test ( env ) 39action = tut . actions [ 0 ] 40toolchain = _get_toolchain ( ctx ) 41compilation_mode = ctx . var [ "COMPILATION_MODE" ] 42pic_suffix = _get_pic_suffix ( ctx , compilation_mode ) 43assert_argv_contains_prefix_suffix ( env , action , "-Lnative=" , "/native_deps" ) 44assert_argv_contains ( env , action , "--crate-type=cdylib" ) 45assert_argv_contains ( env , action , "-lstatic=native_dep{}" . format ( pic_suffix )) 46if toolchain . target_os == "windows" : 47if toolchain . target_triple . abi == "msvc" : 48native_link_arg = "-Clink-arg=native_dep.lib" 49else : 50native_link_arg = "-Clink-arg=-lnative_dep.lib" 51else : 52native_link_arg = "-Clink-arg=-lnative_dep{}" . format ( pic_suffix ) 53assert_argv_contains ( env , action , native_link_arg ) 54assert_argv_contains_prefix ( env , action , "--codegen=linker=" ) 55return analysistest . end ( env ) 56 57def _staticlib_has_native_libs_test_impl ( ctx ): 58env = analysistest . begin ( ctx ) 59tut = analysistest . target_under_test ( env ) 60action = tut . actions [ 0 ] 61toolchain = _get_toolchain ( ctx ) 62assert_argv_contains_prefix_suffix ( env , action , "-Lnative=" , "/native_deps" ) 63assert_argv_contains ( env , action , "--crate-type=staticlib" ) 64assert_argv_contains ( env , action , "-lstatic=native_dep" ) 65if toolchain . target_os == "windows" : 66if toolchain . target_triple . abi == "msvc" : 67native_link_arg = "-Clink-arg=native_dep.lib" 68else : 69native_link_arg = "-Clink-arg=-lnative_dep.lib" 70else : 71native_link_arg = "-Clink-arg=-lnative_dep" 72assert_argv_contains ( env , action , native_link_arg ) 73assert_argv_contains_prefix ( env , action , "--codegen=linker=" ) 74return analysistest . end ( env ) 75 76def _proc_macro_has_native_libs_test_impl ( ctx ): 77env = analysistest . begin ( ctx ) 78tut = analysistest . target_under_test ( env ) 79action = tut . actions [ 0 ] 80toolchain = _get_toolchain ( ctx ) 81compilation_mode = ctx . var [ "COMPILATION_MODE" ] 82pic_suffix = _get_pic_suffix ( ctx , compilation_mode ) 83assert_argv_contains_prefix_suffix ( env , action , "-Lnative=" , "/native_deps" ) 84assert_argv_contains ( env , action , "--crate-type=proc-macro" ) 85assert_argv_contains ( env , action , "-lstatic=native_dep{}" . format ( pic_suffix )) 86if toolchain . target_os == "windows" : 87if toolchain . target_triple . abi == "msvc" : 88native_link_arg = "-Clink-arg=native_dep.lib" 89else : 90native_link_arg = "-Clink-arg=-lnative_dep.lib" 91else : 92native_link_arg = "-Clink-arg=-lnative_dep{}" . format ( pic_suffix ) 93assert_argv_contains ( env , action , native_link_arg ) 94assert_argv_contains_prefix ( env , action , "--codegen=linker=" ) 95return analysistest . end ( env ) 96 97def _bin_has_native_libs_test_impl ( ctx ): 98env = analysistest . begin ( ctx ) 99tut = analysistest . target_under_test ( env ) 100action = tut . actions [ 0 ] 101toolchain = _get_toolchain ( ctx ) 102assert_argv_contains_prefix_suffix ( env , action , "-Lnative=" , "/native_deps" ) 103assert_argv_contains ( env , action , "-lstatic=native_dep" ) 104if toolchain . target_os == "windows" : 105if toolchain . target_triple . abi == "msvc" : 106native_link_arg = "-Clink-arg=native_dep.lib" 107else : 108native_link_arg = "-Clink-arg=-lnative_dep.lib" 109else : 110native_link_arg = "-Clink-arg=-lnative_dep" 111assert_argv_contains ( env , action , native_link_arg ) 112assert_argv_contains_prefix ( env , action , "--codegen=linker=" ) 113return analysistest . end ( env ) 114 115def _extract_linker_args ( argv ): 116return [ 117a . removeprefix ( "--codegen=" ). removeprefix ( "-C" ). removeprefix ( "link-arg=" ). removeprefix ( "link-args=" ) 118for a in argv 119if ( 120a . startswith ( "--codegen=link-arg=" ) or 121a . startswith ( "--codegen=link-args=" ) or 122a . startswith ( "-Clink-args=" ) or 123a . startswith ( "-Clink-arg=" ) or 124a . startswith ( "link-args=" ) or 125a . startswith ( "link-arg=" ) or 126a . startswith ( "-l" ) or 127a . endswith ( ".lo" ) or 128a . endswith ( ".o" ) 129) 130] 131 132def _bin_has_native_dep_and_alwayslink_test_impl ( ctx ): 133env = analysistest . begin ( ctx ) 134tut = analysistest . target_under_test ( env ) 135action = tut . actions [ 0 ] 136 137toolchain = _get_toolchain ( ctx ) 138compilation_mode = ctx . var [ "COMPILATION_MODE" ] 139workspace_prefix = "" if ctx . workspace_name == "rules_rust" else "external/rules_rust/" 140link_args = _extract_linker_args ( action . argv ) 141if toolchain . target_os == "darwin" : 142darwin_component = _get_darwin_component ( link_args [ - 1 ]) 143want = [ 144"-lstatic=native_dep" , 145"-lnative_dep" , 146"-Wl,-force_load,bazel-out/{}-{}/bin/{}test/unit/native_deps/libalwayslink.lo" . format ( darwin_component , compilation_mode , workspace_prefix ), 147] 148assert_list_contains_adjacent_elements ( env , link_args , want ) 149elif toolchain . target_os == "windows" : 150if toolchain . target_triple . abi == "msvc" : 151want = [ 152"-lstatic=native_dep" , 153"native_dep.lib" , 154"/WHOLEARCHIVE:bazel-out/x64_windows-{}/bin/{}test/unit/native_deps/alwayslink.lo.lib" . format ( compilation_mode , workspace_prefix ), 155] 156else : 157want = [ 158"-lstatic=native_dep" , 159"native_dep.lib" , 160"-Wl,--whole-archive" , 161"bazel-out/x64_windows-{}/bin/{}test/unit/native_deps/alwayslink.lo.lib" . format ( compilation_mode , workspace_prefix ), 162"-Wl,--no-whole-archive" , 163] 164else : 165want = [ 166"-lstatic=native_dep" , 167"-lnative_dep" , 168"-Wl,--whole-archive" , 169"bazel-out/k8-{}/bin/{}test/unit/native_deps/libalwayslink.lo" . format ( compilation_mode , workspace_prefix ), 170"-Wl,--no-whole-archive" , 171] 172assert_list_contains_adjacent_elements ( env , link_args , want ) 173return analysistest . end ( env ) 174 175def _cdylib_has_native_dep_and_alwayslink_test_impl ( ctx ): 176toolchain = _get_toolchain ( ctx ) 177 178env = analysistest . begin ( ctx ) 179tut = analysistest . target_under_test ( env ) 180action = tut . actions [ 0 ] 181 182linker_args = _extract_linker_args ( action . argv ) 183 184toolchain = _get_toolchain ( ctx ) 185compilation_mode = ctx . var [ "COMPILATION_MODE" ] 186workspace_prefix = "" if ctx . workspace_name == "rules_rust" else "external/rules_rust/" 187pic_suffix = _get_pic_suffix ( ctx , compilation_mode ) 188if toolchain . target_os == "darwin" : 189darwin_component = _get_darwin_component ( linker_args [ - 1 ]) 190want = [ 191"-lstatic=native_dep{}" . format ( pic_suffix ), 192"-lnative_dep{}" . format ( pic_suffix ), 193"-Wl,-force_load,bazel-out/{}-{}/bin/{}test/unit/native_deps/libalwayslink{}.lo" . format ( darwin_component , compilation_mode , workspace_prefix , pic_suffix ), 194] 195elif toolchain . target_os == "windows" : 196if toolchain . target_triple . abi == "msvc" : 197want = [ 198"-lstatic=native_dep" , 199"native_dep.lib" , 200"/WHOLEARCHIVE:bazel-out/x64_windows-{}/bin/{}test/unit/native_deps/alwayslink.lo.lib" . format ( compilation_mode , workspace_prefix ), 201] 202else : 203want = [ 204"-lstatic=native_dep" , 205"native_dep.lib" , 206"-Wl,--whole-archive" , 207"bazel-out/x64_windows-{}/bin/{}test/unit/native_deps/alwayslink.lo.lib" . format ( compilation_mode , workspace_prefix ), 208"-Wl,--no-whole-archive" , 209] 210else : 211want = [ 212"-lstatic=native_dep{}" . format ( pic_suffix ), 213"-lnative_dep{}" . format ( pic_suffix ), 214"-Wl,--whole-archive" , 215"bazel-out/k8-{}/bin/{}test/unit/native_deps/libalwayslink{}.lo" . format ( compilation_mode , workspace_prefix , pic_suffix ), 216"-Wl,--no-whole-archive" , 217] 218assert_list_contains_adjacent_elements ( env , linker_args , want ) 219return analysistest . end ( env ) 220 221def _get_pic_suffix ( ctx , compilation_mode ): 222toolchain = _get_toolchain ( ctx ) 223if toolchain . target_os == "darwin" or toolchain . target_os == "windows" : 224return "" 225return ".pic" if compilation_mode == "opt" else "" 226 227rlib_has_no_native_libs_test = analysistest . make ( _rlib_has_no_native_libs_test_impl ) 228staticlib_has_native_libs_test = analysistest . make ( _staticlib_has_native_libs_test_impl , attrs = { 229"_toolchain" : attr . label ( default = Label ( "//rust/toolchain:current_rust_toolchain" )), 230}) 231cdylib_has_native_libs_test = analysistest . make ( _cdylib_has_native_libs_test_impl , attrs = { 232"_toolchain" : attr . label ( default = Label ( "//rust/toolchain:current_rust_toolchain" )), 233}) 234proc_macro_has_native_libs_test = analysistest . make ( _proc_macro_has_native_libs_test_impl , attrs = { 235"_toolchain" : attr . label ( default = Label ( "//rust/toolchain:current_rust_toolchain" )), 236}) 237bin_has_native_libs_test = analysistest . make ( _bin_has_native_libs_test_impl , attrs = { 238"_toolchain" : attr . label ( default = Label ( "//rust/toolchain:current_rust_toolchain" )), 239}) 240bin_has_native_dep_and_alwayslink_test = analysistest . make ( _bin_has_native_dep_and_alwayslink_test_impl , attrs = { 241"_toolchain" : attr . label ( default = Label ( "//rust/toolchain:current_rust_toolchain" )), 242}) 243cdylib_has_native_dep_and_alwayslink_test = analysistest . make ( _cdylib_has_native_dep_and_alwayslink_test_impl , attrs = { 244"_toolchain" : attr . label ( default = Label ( "//rust/toolchain:current_rust_toolchain" )), 245}) 246 247def _native_dep_test (): 248rust_library ( 249name = "rlib_has_no_native_dep" , 250srcs = [ "lib_using_native_dep.rs" ], 251edition = "2018" , 252deps = [ ":native_dep" ], 253) 254 255rust_static_library ( 256name = "staticlib_has_native_dep" , 257srcs = [ "lib_using_native_dep.rs" ], 258edition = "2018" , 259deps = [ ":native_dep" ], 260) 261 262rust_shared_library ( 263name = "cdylib_has_native_dep" , 264srcs = [ "lib_using_native_dep.rs" ], 265edition = "2018" , 266deps = [ ":native_dep" ], 267) 268 269rust_proc_macro ( 270name = "proc_macro_has_native_dep" , 271srcs = [ "proc_macro_using_native_dep.rs" ], 272edition = "2018" , 273deps = [ ":native_dep" ], 274) 275 276rust_binary ( 277name = "bin_has_native_dep" , 278srcs = [ "bin_using_native_dep.rs" ], 279edition = "2018" , 280deps = [ ":native_dep" ], 281) 282 283rust_binary ( 284name = "bin_has_native_dep_and_alwayslink" , 285srcs = [ "bin_using_native_dep.rs" ], 286edition = "2018" , 287deps = [ ":native_dep" , ":alwayslink" ], 288) 289 290cc_library ( 291name = "native_dep" , 292srcs = [ "native_dep.cc" ], 293visibility = [ "//test/unit:__subpackages__" ], 294) 295 296cc_library ( 297name = "alwayslink" , 298srcs = [ "alwayslink.cc" ], 299alwayslink = 1 , 300) 301 302rust_shared_library ( 303name = "cdylib_has_native_dep_and_alwayslink" , 304srcs = [ "lib_using_native_dep.rs" ], 305edition = "2018" , 306deps = [ ":native_dep" , ":alwayslink" ], 307) 308 309rlib_has_no_native_libs_test ( 310name = "rlib_has_no_native_libs_test" , 311target_under_test = ":rlib_has_no_native_dep" , 312) 313staticlib_has_native_libs_test ( 314name = "staticlib_has_native_libs_test" , 315target_under_test = ":staticlib_has_native_dep" , 316) 317cdylib_has_native_libs_test ( 318name = "cdylib_has_native_libs_test" , 319target_under_test = ":cdylib_has_native_dep" , 320) 321proc_macro_has_native_libs_test ( 322name = "proc_macro_has_native_libs_test" , 323target_under_test = ":proc_macro_has_native_dep" , 324) 325bin_has_native_libs_test ( 326name = "bin_has_native_libs_test" , 327target_under_test = ":bin_has_native_dep" , 328) 329bin_has_native_dep_and_alwayslink_test ( 330name = "bin_has_native_dep_and_alwayslink_test" , 331target_under_test = ":bin_has_native_dep_and_alwayslink" , 332) 333cdylib_has_native_dep_and_alwayslink_test ( 334name = "cdylib_has_native_dep_and_alwayslink_test" , 335target_under_test = ":cdylib_has_native_dep_and_alwayslink" , 336) 337 338def _linkopts_propagate_test_impl ( ctx ): 339env = analysistest . begin ( ctx ) 340tut = analysistest . target_under_test ( env ) 341action = tut . actions [ 0 ] 342 343# Ensure linkopts from direct (-Llinkoptdep1) and transitive 344# (-Llinkoptdep2) dependencies are propagated. 345# Consistently with cc rules, dependency linkopts take precedence over 346# dependent linkopts (i.e. dependency linkopts appear later in the command 347# line). 348 349linkopt_args = _extract_linker_args ( action . argv ) 350assert_list_contains_adjacent_elements ( 351env , 352linkopt_args , 353[ "-Llinkoptdep1" , "-Llinkoptdep2" ], 354) 355return analysistest . end ( env ) 356 357linkopts_propagate_test = analysistest . make ( _linkopts_propagate_test_impl ) 358 359def _linkopts_test (): 360rust_binary ( 361name = "linkopts_rust_bin" , 362srcs = [ "bin_using_native_dep.rs" ], 363edition = "2018" , 364deps = [ ":linkopts_native_dep_a" ], 365) 366 367cc_library ( 368name = "linkopts_native_dep_a" , 369srcs = [ "native_dep.cc" ], 370linkopts = [ "-Llinkoptdep1" ], 371deps = [ ":linkopts_native_dep_b" ], 372) 373 374cc_library ( 375name = "linkopts_native_dep_b" , 376linkopts = [ "-Llinkoptdep2" ], 377) 378 379linkopts_propagate_test ( 380name = "native_linkopts_propagate_test" , 381target_under_test = ":linkopts_rust_bin" , 382) 383 384def _check_additional_deps_test_impl ( ctx , expect_additional_deps ): 385env = analysistest . begin ( ctx ) 386tut = analysistest . target_under_test ( env ) 387action = tut . actions [ 0 ] 388additional_inputs = [ inp . basename for inp in action . inputs . to_list ()] 389asserts . equals ( env , "dynamic.lds" in additional_inputs , expect_additional_deps ) 390return analysistest . end ( env ) 391 392def _has_additional_deps_test_impl ( ctx ): 393return _check_additional_deps_test_impl ( ctx , expect_additional_deps = True ) 394 395def _has_no_additional_deps_test_impl ( ctx ): 396return _check_additional_deps_test_impl ( ctx , expect_additional_deps = False ) 397 398has_additional_deps_test = analysistest . make ( _has_additional_deps_test_impl ) 399has_no_additional_deps_test = analysistest . make ( _has_no_additional_deps_test_impl ) 400 401def _additional_deps_test (): 402rust_binary ( 403name = "bin_additional_deps" , 404srcs = [ "bin_using_native_dep.rs" ], 405edition = "2018" , 406deps = [ ":additional_deps_cc" ], 407) 408 409rust_shared_library ( 410name = "cdylib_additional_deps" , 411srcs = [ "lib_using_native_dep.rs" ], 412edition = "2018" , 413deps = [ ":additional_deps_cc" ], 414) 415 416rust_library ( 417name = "lib_additional_deps" , 418srcs = [ "lib_using_native_dep.rs" ], 419edition = "2018" , 420deps = [ "additional_deps_cc" ], 421) 422 423cc_library ( 424name = "additional_deps_cc" , 425srcs = [ "native_dep.cc" ], 426linkopts = [ "-L$(execpath :dynamic.lds)" ], 427deps = [ ":dynamic.lds" ], 428) 429 430has_additional_deps_test ( 431name = "bin_has_additional_deps_test" , 432target_under_test = ":bin_additional_deps" , 433) 434 435has_additional_deps_test ( 436name = "cdylib_has_additional_deps_test" , 437target_under_test = ":cdylib_additional_deps" , 438) 439 440has_no_additional_deps_test ( 441name = "lib_has_no_additional_deps_test" , 442target_under_test = ":lib_additional_deps" , 443) 444 445def native_deps_test_suite ( name ): 446"""Entry-point macro called from the BUILD file. 447 448Args: 449name: Name of the macro. 450""" 451_native_dep_test () 452_linkopts_test () 453_additional_deps_test () 454 455native . test_suite ( 456name = name , 457tests = [ 458":bin_has_additional_deps_test" , 459":bin_has_native_dep_and_alwayslink_test" , 460":bin_has_native_libs_test" , 461":cdylib_has_additional_deps_test" , 462":cdylib_has_native_dep_and_alwayslink_test" , 463":cdylib_has_native_libs_test" , 464":lib_has_no_additional_deps_test" , 465":native_linkopts_propagate_test" , 466":proc_macro_has_native_libs_test" , 467":rlib_has_no_native_libs_test" , 468":staticlib_has_native_libs_test" , 469], 470)