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