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): 15 return 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): 18 env = analysistest.begin(ctx) 19 tut = analysistest.target_under_test(env) 20 rustc_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. 24 link_args = [arg for arg in rustc_action.argv if arg.startswith("-lstatic=native_dep-")] 25 asserts.equals(env, 2, len(link_args)) 26 asserts.false(env, link_args[0] == link_args[1]) 27 28 for_shared_library = _get_crate_info(tut).type in ("dylib", "cdylib", "proc-macro") 29 extension = _get_pic_suffix(ctx, for_shared_library) 30 31 asserts.true(env, link_args[0].endswith(extension)) 32 asserts.true(env, link_args[1].endswith(extension)) 33 34 return analysistest.end(env) 35 36def _get_pic_suffix(ctx, for_shared_library): 37 if ctx.target_platform_has_constraint( 38 ctx.attr._windows_constraint[platform_common.ConstraintValueInfo], 39 ) or ctx.target_platform_has_constraint( 40 ctx.attr._macos_constraint[platform_common.ConstraintValueInfo], 41 ): 42 return "" 43 else: 44 compilation_mode = ctx.var["COMPILATION_MODE"] 45 return ".pic" if compilation_mode == "opt" and for_shared_library else "" 46 47ambiguous_deps_test = analysistest.make( 48 _ambiguous_deps_test_impl, 49 attrs = { 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(): 56 rust_library( 57 name = "rlib_with_ambiguous_deps", 58 srcs = ["foo.rs"], 59 edition = "2018", 60 deps = [ 61 "//test/unit/ambiguous_libs/first_dep:native_dep", 62 "//test/unit/ambiguous_libs/second_dep:native_dep", 63 ], 64 ) 65 66 rust_binary( 67 name = "binary", 68 srcs = ["bin.rs"], 69 edition = "2018", 70 deps = [":rlib_with_ambiguous_deps"], 71 ) 72 73 rust_proc_macro( 74 name = "proc_macro", 75 srcs = ["foo.rs"], 76 edition = "2018", 77 deps = [":rlib_with_ambiguous_deps"], 78 ) 79 80 rust_shared_library( 81 name = "shared_library", 82 srcs = ["foo.rs"], 83 edition = "2018", 84 deps = [":rlib_with_ambiguous_deps"], 85 ) 86 87 rust_static_library( 88 name = "static_library", 89 srcs = ["foo.rs"], 90 edition = "2018", 91 deps = [":rlib_with_ambiguous_deps"], 92 ) 93 94 ambiguous_deps_test( 95 name = "bin_with_ambiguous_deps_test", 96 target_under_test = ":binary", 97 ) 98 ambiguous_deps_test( 99 name = "staticlib_with_ambiguous_deps_test", 100 target_under_test = ":static_library", 101 ) 102 ambiguous_deps_test( 103 name = "proc_macro_with_ambiguous_deps_test", 104 target_under_test = ":proc_macro", 105 ) 106 ambiguous_deps_test( 107 name = "cdylib_with_ambiguous_deps_test", 108 target_under_test = ":shared_library", 109 ) 110 111def ambiguous_libs_test_suite(name): 112 """Entry-point macro called from the BUILD file. 113 114 Args: 115 name: Name of the macro. 116 """ 117 _create_test_targets() 118 119 native.test_suite( 120 name = name, 121 tests = [ 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 ) 128