fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
aa0815dc
patching
1"""Unittests for ambiguous native dependencies.""" 2 3load ( "@bazel_skylib//lib:unittest.bzl" , "analysistest" , "asserts" ) 4load ( 5"//rust:defs.bzl" , 6"rust_binary" , 7"rust_common" , 8"rust_library" , 9"rust_proc_macro" , 10"rust_shared_library" , 11"rust_static_library" , 12) 13 14def _get_crate_info ( target ): 15return target [ rust_common . crate_info ] if rust_common . crate_info in target else target [ rust_common . test_crate_info ]. crate 16 17def _ambiguous_deps_test_impl ( ctx ): 18env = analysistest . begin ( ctx ) 19tut = analysistest . target_under_test ( env ) 20rustc_action = [ action for action in tut . actions if action . mnemonic == "Rustc" ][ 0 ] 21 22# We depend on two C++ libraries named "native_dep", which we need to pass to the command line 23# in the form of "-lstatic=native-dep-{hash} "-lstatic=native-dep-{hash}.pic. 24link_args = [ arg for arg in rustc_action . argv if arg . startswith ( "-lstatic=native_dep-" )] 25asserts . equals ( env , 2 , len ( link_args )) 26asserts . false ( env , link_args [ 0 ] == link_args [ 1 ]) 27 28for_shared_library = _get_crate_info ( tut ). type in ( "dylib" , "cdylib" , "proc-macro" ) 29extension = _get_pic_suffix ( ctx , for_shared_library ) 30 31asserts . true ( env , link_args [ 0 ]. endswith ( extension )) 32asserts . true ( env , link_args [ 1 ]. endswith ( extension )) 33 34return analysistest . end ( env ) 35 36def _get_pic_suffix ( ctx , for_shared_library ): 37if ctx . target_platform_has_constraint ( 38ctx . attr . _windows_constraint [ platform_common . ConstraintValueInfo ], 39) or ctx . target_platform_has_constraint ( 40ctx . attr . _macos_constraint [ platform_common . ConstraintValueInfo ], 41): 42return "" 43else : 44compilation_mode = ctx . var [ "COMPILATION_MODE" ] 45return ".pic" if compilation_mode == "opt" and for_shared_library else "" 46 47ambiguous_deps_test = analysistest . make ( 48_ambiguous_deps_test_impl , 49attrs = { 50"_macos_constraint" : attr . label ( default = Label ( "@platforms//os:macos" )), 51"_windows_constraint" : attr . label ( default = Label ( "@platforms//os:windows" )), 52}, 53) 54 55def _create_test_targets (): 56rust_library ( 57name = "rlib_with_ambiguous_deps" , 58srcs = [ "foo.rs" ], 59edition = "2018" , 60deps = [ 61"//test/unit/ambiguous_libs/first_dep:native_dep" , 62"//test/unit/ambiguous_libs/second_dep:native_dep" , 63], 64) 65 66rust_binary ( 67name = "binary" , 68srcs = [ "bin.rs" ], 69edition = "2018" , 70deps = [ ":rlib_with_ambiguous_deps" ], 71) 72 73rust_proc_macro ( 74name = "proc_macro" , 75srcs = [ "foo.rs" ], 76edition = "2018" , 77deps = [ ":rlib_with_ambiguous_deps" ], 78) 79 80rust_shared_library ( 81name = "shared_library" , 82srcs = [ "foo.rs" ], 83edition = "2018" , 84deps = [ ":rlib_with_ambiguous_deps" ], 85) 86 87rust_static_library ( 88name = "static_library" , 89srcs = [ "foo.rs" ], 90edition = "2018" , 91deps = [ ":rlib_with_ambiguous_deps" ], 92) 93 94ambiguous_deps_test ( 95name = "bin_with_ambiguous_deps_test" , 96target_under_test = ":binary" , 97) 98ambiguous_deps_test ( 99name = "staticlib_with_ambiguous_deps_test" , 100target_under_test = ":static_library" , 101) 102ambiguous_deps_test ( 103name = "proc_macro_with_ambiguous_deps_test" , 104target_under_test = ":proc_macro" , 105) 106ambiguous_deps_test ( 107name = "cdylib_with_ambiguous_deps_test" , 108target_under_test = ":shared_library" , 109) 110 111def ambiguous_libs_test_suite ( name ): 112"""Entry-point macro called from the BUILD file. 113 114Args: 115name: Name of the macro. 116""" 117_create_test_targets () 118 119native . test_suite ( 120name = name , 121tests = [ 122":bin_with_ambiguous_deps_test" , 123":staticlib_with_ambiguous_deps_test" , 124":proc_macro_with_ambiguous_deps_test" , 125":cdylib_with_ambiguous_deps_test" , 126], 127)