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