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