fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
3d1856b6
cleanup
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] 164elif toolchain . target_arch == "s390x" : 165want = [ 166"-lstatic=native_dep" , 167"link-arg=-Wl,--whole-archive" , 168"link-arg=bazel-out/s390x-{}/bin/{}test/unit/native_deps/libalwayslink.lo" . format ( compilation_mode , workspace_prefix ), 169"link-arg=-Wl,--no-whole-archive" , 170] 171else : 172want = [ 173"-lstatic=native_dep" , 174"-lnative_dep" , 175"-Wl,--whole-archive" , 176"bazel-out/k8-{}/bin/{}test/unit/native_deps/libalwayslink.lo" . format ( compilation_mode , workspace_prefix ), 177"-Wl,--no-whole-archive" , 178] 179assert_list_contains_adjacent_elements ( env , link_args , want ) 180return analysistest . end ( env ) 181 182def _cdylib_has_native_dep_and_alwayslink_test_impl ( ctx ): 183toolchain = _get_toolchain ( ctx ) 184 185env = analysistest . begin ( ctx ) 186tut = analysistest . target_under_test ( env ) 187action = tut . actions [ 0 ] 188 189linker_args = _extract_linker_args ( action . argv ) 190 191toolchain = _get_toolchain ( ctx ) 192compilation_mode = ctx . var [ "COMPILATION_MODE" ] 193workspace_prefix = "" if ctx . workspace_name == "rules_rust" else "external/rules_rust/" 194pic_suffix = _get_pic_suffix ( ctx , compilation_mode ) 195if toolchain . target_os == "darwin" : 196darwin_component = _get_darwin_component ( linker_args [ - 1 ]) 197want = [ 198"-lstatic=native_dep{}" . format ( pic_suffix ), 199"-lnative_dep{}" . format ( pic_suffix ), 200"-Wl,-force_load,bazel-out/{}-{}/bin/{}test/unit/native_deps/libalwayslink{}.lo" . format ( darwin_component , compilation_mode , workspace_prefix , pic_suffix ), 201] 202elif toolchain . target_os == "windows" : 203if toolchain . target_triple . abi == "msvc" : 204want = [ 205"-lstatic=native_dep" , 206"native_dep.lib" , 207"/WHOLEARCHIVE:bazel-out/x64_windows-{}/bin/{}test/unit/native_deps/alwayslink.lo.lib" . format ( compilation_mode , workspace_prefix ), 208] 209else : 210want = [ 211"-lstatic=native_dep" , 212"native_dep.lib" , 213"-Wl,--whole-archive" , 214"bazel-out/x64_windows-{}/bin/{}test/unit/native_deps/alwayslink.lo.lib" . format ( compilation_mode , workspace_prefix ), 215"-Wl,--no-whole-archive" , 216] 217elif toolchain . target_arch == "s390x" : 218want = [ 219"-lstatic=native_dep{}" . format ( pic_suffix ), 220"link-arg=-Wl,--whole-archive" , 221"link-arg=bazel-out/s390x-{}/bin/{}test/unit/native_deps/libalwayslink{}.lo" . format ( compilation_mode , workspace_prefix , pic_suffix ), 222"link-arg=-Wl,--no-whole-archive" , 223] 224else : 225want = [ 226"-lstatic=native_dep{}" . format ( pic_suffix ), 227"-lnative_dep{}" . format ( pic_suffix ), 228"-Wl,--whole-archive" , 229"bazel-out/k8-{}/bin/{}test/unit/native_deps/libalwayslink{}.lo" . format ( compilation_mode , workspace_prefix , pic_suffix ), 230"-Wl,--no-whole-archive" , 231] 232assert_list_contains_adjacent_elements ( env , linker_args , want ) 233return analysistest . end ( env ) 234 235def _get_pic_suffix ( ctx , compilation_mode ): 236toolchain = _get_toolchain ( ctx ) 237if toolchain . target_os == "darwin" or toolchain . target_os == "windows" : 238return "" 239return ".pic" if compilation_mode == "opt" else "" 240 241rlib_has_no_native_libs_test = analysistest . make ( _rlib_has_no_native_libs_test_impl ) 242staticlib_has_native_libs_test = analysistest . make ( _staticlib_has_native_libs_test_impl , attrs = { 243"_toolchain" : attr . label ( default = Label ( "//rust/toolchain:current_rust_toolchain" )), 244}) 245cdylib_has_native_libs_test = analysistest . make ( _cdylib_has_native_libs_test_impl , attrs = { 246"_toolchain" : attr . label ( default = Label ( "//rust/toolchain:current_rust_toolchain" )), 247}) 248proc_macro_has_native_libs_test = analysistest . make ( _proc_macro_has_native_libs_test_impl , attrs = { 249"_toolchain" : attr . label ( default = Label ( "//rust/toolchain:current_rust_toolchain" )), 250}) 251bin_has_native_libs_test = analysistest . make ( _bin_has_native_libs_test_impl , attrs = { 252"_toolchain" : attr . label ( default = Label ( "//rust/toolchain:current_rust_toolchain" )), 253}) 254bin_has_native_dep_and_alwayslink_test = analysistest . make ( _bin_has_native_dep_and_alwayslink_test_impl , attrs = { 255"_toolchain" : attr . label ( default = Label ( "//rust/toolchain:current_rust_toolchain" )), 256}) 257cdylib_has_native_dep_and_alwayslink_test = analysistest . make ( _cdylib_has_native_dep_and_alwayslink_test_impl , attrs = { 258"_toolchain" : attr . label ( default = Label ( "//rust/toolchain:current_rust_toolchain" )), 259}) 260 261def _native_dep_test (): 262rust_library ( 263name = "rlib_has_no_native_dep" , 264srcs = [ "lib_using_native_dep.rs" ], 265edition = "2018" , 266deps = [ ":native_dep" ], 267) 268 269rust_static_library ( 270name = "staticlib_has_native_dep" , 271srcs = [ "lib_using_native_dep.rs" ], 272edition = "2018" , 273deps = [ ":native_dep" ], 274) 275 276rust_shared_library ( 277name = "cdylib_has_native_dep" , 278srcs = [ "lib_using_native_dep.rs" ], 279edition = "2018" , 280deps = [ ":native_dep" ], 281) 282 283rust_proc_macro ( 284name = "proc_macro_has_native_dep" , 285srcs = [ "proc_macro_using_native_dep.rs" ], 286edition = "2018" , 287deps = [ ":native_dep" ], 288) 289 290rust_binary ( 291name = "bin_has_native_dep" , 292srcs = [ "bin_using_native_dep.rs" ], 293edition = "2018" , 294deps = [ ":native_dep" ], 295) 296 297rust_binary ( 298name = "bin_has_native_dep_and_alwayslink" , 299srcs = [ "bin_using_native_dep.rs" ], 300edition = "2018" , 301deps = [ ":native_dep" , ":alwayslink" ], 302) 303 304cc_library ( 305name = "native_dep" , 306srcs = [ "native_dep.cc" ], 307visibility = [ "//test/unit:__subpackages__" ], 308) 309 310cc_library ( 311name = "alwayslink" , 312srcs = [ "alwayslink.cc" ], 313alwayslink = 1 , 314) 315 316rust_shared_library ( 317name = "cdylib_has_native_dep_and_alwayslink" , 318srcs = [ "lib_using_native_dep.rs" ], 319edition = "2018" , 320deps = [ ":native_dep" , ":alwayslink" ], 321) 322 323rlib_has_no_native_libs_test ( 324name = "rlib_has_no_native_libs_test" , 325target_under_test = ":rlib_has_no_native_dep" , 326) 327staticlib_has_native_libs_test ( 328name = "staticlib_has_native_libs_test" , 329target_under_test = ":staticlib_has_native_dep" , 330) 331cdylib_has_native_libs_test ( 332name = "cdylib_has_native_libs_test" , 333target_under_test = ":cdylib_has_native_dep" , 334) 335proc_macro_has_native_libs_test ( 336name = "proc_macro_has_native_libs_test" , 337target_under_test = ":proc_macro_has_native_dep" , 338) 339bin_has_native_libs_test ( 340name = "bin_has_native_libs_test" , 341target_under_test = ":bin_has_native_dep" , 342) 343bin_has_native_dep_and_alwayslink_test ( 344name = "bin_has_native_dep_and_alwayslink_test" , 345target_under_test = ":bin_has_native_dep_and_alwayslink" , 346) 347cdylib_has_native_dep_and_alwayslink_test ( 348name = "cdylib_has_native_dep_and_alwayslink_test" , 349target_under_test = ":cdylib_has_native_dep_and_alwayslink" , 350) 351 352def _linkopts_propagate_test_impl ( ctx ): 353env = analysistest . begin ( ctx ) 354tut = analysistest . target_under_test ( env ) 355action = tut . actions [ 0 ] 356 357# Ensure linkopts from direct (-Llinkoptdep1) and transitive 358# (-Llinkoptdep2) dependencies are propagated. 359# Consistently with cc rules, dependency linkopts take precedence over 360# dependent linkopts (i.e. dependency linkopts appear later in the command 361# line). 362 363linkopt_args = _extract_linker_args ( action . argv ) 364assert_list_contains_adjacent_elements ( 365env , 366linkopt_args , 367[ "-Llinkoptdep1" , "-Llinkoptdep2" ], 368) 369return analysistest . end ( env ) 370 371linkopts_propagate_test = analysistest . make ( _linkopts_propagate_test_impl ) 372 373def _linkopts_test (): 374rust_binary ( 375name = "linkopts_rust_bin" , 376srcs = [ "bin_using_native_dep.rs" ], 377edition = "2018" , 378deps = [ ":linkopts_native_dep_a" ], 379) 380 381cc_library ( 382name = "linkopts_native_dep_a" , 383srcs = [ "native_dep.cc" ], 384linkopts = [ "-Llinkoptdep1" ], 385deps = [ ":linkopts_native_dep_b" ], 386) 387 388cc_library ( 389name = "linkopts_native_dep_b" , 390linkopts = [ "-Llinkoptdep2" ], 391) 392 393linkopts_propagate_test ( 394name = "native_linkopts_propagate_test" , 395target_under_test = ":linkopts_rust_bin" , 396) 397 398def _check_additional_deps_test_impl ( ctx , expect_additional_deps ): 399env = analysistest . begin ( ctx ) 400tut = analysistest . target_under_test ( env ) 401action = tut . actions [ 0 ] 402additional_inputs = [ inp . basename for inp in action . inputs . to_list ()] 403asserts . equals ( env , "dynamic.lds" in additional_inputs , expect_additional_deps ) 404return analysistest . end ( env ) 405 406def _has_additional_deps_test_impl ( ctx ): 407return _check_additional_deps_test_impl ( ctx , expect_additional_deps = True ) 408 409def _has_no_additional_deps_test_impl ( ctx ): 410return _check_additional_deps_test_impl ( ctx , expect_additional_deps = False ) 411 412has_additional_deps_test = analysistest . make ( _has_additional_deps_test_impl ) 413has_no_additional_deps_test = analysistest . make ( _has_no_additional_deps_test_impl ) 414 415def _additional_deps_test (): 416rust_binary ( 417name = "bin_additional_deps" , 418srcs = [ "bin_using_native_dep.rs" ], 419edition = "2018" , 420deps = [ ":additional_deps_cc" ], 421) 422 423rust_shared_library ( 424name = "cdylib_additional_deps" , 425srcs = [ "lib_using_native_dep.rs" ], 426edition = "2018" , 427deps = [ ":additional_deps_cc" ], 428) 429 430rust_library ( 431name = "lib_additional_deps" , 432srcs = [ "lib_using_native_dep.rs" ], 433edition = "2018" , 434deps = [ "additional_deps_cc" ], 435) 436 437cc_library ( 438name = "additional_deps_cc" , 439srcs = [ "native_dep.cc" ], 440linkopts = [ "-L$(execpath :dynamic.lds)" ], 441deps = [ ":dynamic.lds" ], 442) 443 444has_additional_deps_test ( 445name = "bin_has_additional_deps_test" , 446target_under_test = ":bin_additional_deps" , 447) 448 449has_additional_deps_test ( 450name = "cdylib_has_additional_deps_test" , 451target_under_test = ":cdylib_additional_deps" , 452) 453 454has_no_additional_deps_test ( 455name = "lib_has_no_additional_deps_test" , 456target_under_test = ":lib_additional_deps" , 457) 458 459def native_deps_test_suite ( name ): 460"""Entry-point macro called from the BUILD file. 461 462Args: 463name: Name of the macro. 464""" 465_native_dep_test () 466_linkopts_test () 467_additional_deps_test () 468 469native . test_suite ( 470name = name , 471tests = [ 472":bin_has_additional_deps_test" , 473":bin_has_native_dep_and_alwayslink_test" , 474":bin_has_native_libs_test" , 475":cdylib_has_additional_deps_test" , 476":cdylib_has_native_dep_and_alwayslink_test" , 477":cdylib_has_native_libs_test" , 478":lib_has_no_additional_deps_test" , 479":native_linkopts_propagate_test" , 480":proc_macro_has_native_libs_test" , 481":rlib_has_no_native_libs_test" , 482":staticlib_has_native_libs_test" , 483], 484)