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