fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
69ddb09d
main
1"""Unittests for rust linkstamp support.""" 2 3load ( "@bazel_skylib//lib:unittest.bzl" , "analysistest" , "asserts" ) 4load ( "@rules_cc//cc:defs.bzl" , "cc_library" ) 5load ( "//rust:defs.bzl" , "rust_binary" , "rust_common" , "rust_library" , "rust_test" ) 6load ( "//test/unit:common.bzl" , "assert_action_mnemonic" ) 7 8def _is_running_on_linux ( ctx ): 9return ctx . target_platform_has_constraint ( ctx . attr . _linux [ platform_common . ConstraintValueInfo ]) 10 11def _get_workspace_prefix ( ctx ): 12return "" if ctx . workspace_name in [ "rules_rust" , "_main" ] else "/external/rules_rust" 13 14def _supports_linkstamps_test ( ctx ): 15env = analysistest . begin ( ctx ) 16tut = analysistest . target_under_test ( env ) 17if not _is_running_on_linux ( ctx ): 18# Skipping linkstamps tests on an unsupported (non-Linux) platform 19return analysistest . end ( env ) 20 21linkstamp_action = tut . actions [ 0 ] 22assert_action_mnemonic ( env , linkstamp_action , "CppLinkstampCompile" ) 23linkstamp_out = linkstamp_action . outputs . to_list ()[ 0 ] 24asserts . equals ( env , linkstamp_out . basename , "linkstamp.o" ) 25tut_out = tut . files . to_list ()[ 0 ] 26is_test = tut [ rust_common . crate_info ]. is_test 27workspace_prefix = _get_workspace_prefix ( ctx ) 28 29# Rust compilation outputs coming from a test are put in test-{hash} directory 30# which we need to remove in order to obtain the linkstamp file path. 31dirname = "/" . join ( tut_out . dirname . split ( "/" )[: - 1 ]) if is_test else tut_out . dirname 32expected_linkstamp_path = dirname + "/_objs/" + tut_out . basename + workspace_prefix + "/test/unit/linkstamps/linkstamp.o" 33asserts . equals ( 34env , 35linkstamp_out . path , 36expected_linkstamp_path , 37"Expected linkstamp output '{actual_path}' to match '{expected_path}'" . format ( 38actual_path = linkstamp_out . path , 39expected_path = expected_linkstamp_path , 40), 41) 42 43rustc_action = tut . actions [ 1 ] 44assert_action_mnemonic ( env , rustc_action , "Rustc" ) 45rustc_inputs = rustc_action . inputs . to_list () 46asserts . true ( 47env , 48linkstamp_out in rustc_inputs , 49"Expected linkstamp output '{output}' to be among the binary inputs '{inputs}'" . format ( 50output = linkstamp_out , 51inputs = rustc_inputs , 52), 53) 54return analysistest . end ( env ) 55 56supports_linkstamps_test = analysistest . make ( 57_supports_linkstamps_test , 58attrs = { 59"_linux" : attr . label ( 60default = Label ( "@platforms//os:linux" ), 61), 62}, 63) 64 65def _linkstamps_test (): 66# Native linkstamps are only supported by the builtin Linux C++ toolchain. Ideally, 67# we would be able to inspect the feature_configuration of the target to see if 68# it has the "linkstamp" feature, but there is no way to get that feature 69# configuration. 70cc_library ( 71name = "cc_lib_with_linkstamp" , 72linkstamp = select ({ 73"//rust/platform:linux" : "linkstamp.cc" , 74"//conditions:default" : None , 75}), 76) 77 78rust_binary ( 79name = "some_rust_binary" , 80srcs = [ "foo.rs" ], 81edition = "2018" , 82deps = [ ":cc_lib_with_linkstamp" ], 83) 84 85supports_linkstamps_test ( 86name = "rust_binary_supports_linkstamps_test" , 87target_under_test = ":some_rust_binary" , 88) 89 90rust_library ( 91name = "some_rust_library_with_linkstamp_transitively" , 92srcs = [ "foo.rs" ], 93edition = "2018" , 94deps = [ ":cc_lib_with_linkstamp" ], 95) 96 97rust_binary ( 98name = "some_rust_binary_with_linkstamp_transitively" , 99srcs = [ "foo.rs" ], 100edition = "2018" , 101deps = [ ":some_rust_library_with_linkstamp_transitively" ], 102) 103 104supports_linkstamps_test ( 105name = "rust_binary_with_linkstamp_transitively" , 106target_under_test = ":some_rust_binary_with_linkstamp_transitively" , 107) 108 109cc_library ( 110name = "cc_lib_with_linkstamp_transitively" , 111deps = [ ":cc_lib_with_linkstamp" ], 112) 113 114rust_binary ( 115name = "some_rust_binary_with_multiple_paths_to_a_linkstamp" , 116srcs = [ "foo.rs" ], 117edition = "2018" , 118deps = [ ":cc_lib_with_linkstamp" , ":cc_lib_with_linkstamp_transitively" ], 119) 120 121supports_linkstamps_test ( 122name = "rust_binary_supports_duplicated_linkstamps" , 123target_under_test = ":some_rust_binary_with_multiple_paths_to_a_linkstamp" , 124) 125 126rust_test ( 127name = "some_rust_test1" , 128srcs = [ "foo.rs" ], 129edition = "2018" , 130deps = [ ":cc_lib_with_linkstamp" ], 131) 132 133supports_linkstamps_test ( 134name = "rust_test_supports_linkstamps_test1" , 135target_under_test = ":some_rust_test1" , 136) 137 138rust_test ( 139name = "some_rust_test2" , 140srcs = [ "foo.rs" ], 141edition = "2018" , 142deps = [ ":cc_lib_with_linkstamp" ], 143) 144 145supports_linkstamps_test ( 146name = "rust_test_supports_linkstamps_test2" , 147target_under_test = ":some_rust_test2" , 148) 149 150def linkstamps_test_suite ( name ): 151"""Entry-point macro called from the BUILD file. 152 153Args: 154name: Name of the macro. 155""" 156 157# Older versions of Bazel do not support Starlark linkstamps. 158if not hasattr ( cc_common , "register_linkstamp_compile_action" ): 159# buildifier: disable=print 160( "Skipping linkstamps tests since this Bazel version does not support Starlark linkstamps." ) 161return 162 163_linkstamps_test () 164 165native . test_suite ( 166name = name , 167tests = [ 168":rust_binary_supports_linkstamps_test" , 169":rust_binary_supports_duplicated_linkstamps" , 170":rust_test_supports_linkstamps_test1" , 171":rust_test_supports_linkstamps_test2" , 172], 173)