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