1"""Analysis tests for debug info in cdylib and bin targets.""" 2 3load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts") 4load("//rust:defs.bzl", "rust_binary", "rust_shared_library", "rust_test") 5 6def _pdb_file_test_impl(ctx, expect_pdb_file): 7 env = analysistest.begin(ctx) 8 target = analysistest.target_under_test(env) 9 files = target[DefaultInfo].files.to_list() 10 11 if not expect_pdb_file: 12 asserts.equals(env, len(files), 0) 13 return analysistest.end(env) 14 15 asserts.equals(env, len(files), 1) 16 file = files[0] 17 asserts.equals(env, file.extension, "pdb") 18 return analysistest.end(env) 19 20def _pdb_file_for_dbg_test_impl(ctx): 21 """Test for dbg compilation mode.""" 22 return _pdb_file_test_impl(ctx, True) 23 24pdb_file_dbg_test = analysistest.make( 25 _pdb_file_for_dbg_test_impl, 26 config_settings = { 27 "//command_line_option:compilation_mode": "dbg", 28 }, 29) 30 31def _pdb_file_for_fastbuild_test_impl(ctx): 32 """Test for fastbuild compilation mode.""" 33 return _pdb_file_test_impl(ctx, True) 34 35pdb_file_fastbuild_test = analysistest.make( 36 _pdb_file_for_fastbuild_test_impl, 37 config_settings = { 38 "//command_line_option:compilation_mode": "fastbuild", 39 }, 40) 41 42def _pdb_file_for_opt_test_impl(ctx): 43 """Test for opt compilation mode.""" 44 return _pdb_file_test_impl(ctx, False) 45 46pdb_file_opt_test = analysistest.make( 47 _pdb_file_for_opt_test_impl, 48 config_settings = { 49 "//command_line_option:compilation_mode": "opt", 50 }, 51) 52 53# Mapping from compilation mode to pdb file test. 54pdb_file_tests = { 55 "dbg": pdb_file_dbg_test, 56 "fastbuild": pdb_file_fastbuild_test, 57 "opt": pdb_file_opt_test, 58} 59 60def _dsym_folder_test_impl(ctx): 61 env = analysistest.begin(ctx) 62 target = analysistest.target_under_test(env) 63 64 files = target[DefaultInfo].files.to_list() 65 asserts.equals(env, len(files), 1) 66 file = files[0] 67 asserts.equals(env, file.extension, "dSYM") 68 69 return analysistest.end(env) 70 71dsym_folder_test = analysistest.make(_dsym_folder_test_impl) 72 73def debug_info_analysis_test_suite(name): 74 """Analysis tests for debug info in cdylib and bin targets. 75 76 Args: 77 name: the test suite name 78 """ 79 rust_shared_library( 80 name = "mylib", 81 srcs = ["lib.rs"], 82 edition = "2018", 83 ) 84 85 native.filegroup( 86 name = "mylib.pdb", 87 srcs = [":mylib"], 88 output_group = "pdb_file", 89 ) 90 91 for compilation_mode, pdb_test in pdb_file_tests.items(): 92 pdb_test( 93 name = "lib_pdb_test_{}".format(compilation_mode), 94 target_under_test = ":mylib.pdb", 95 target_compatible_with = ["@platforms//os:windows"], 96 ) 97 98 native.filegroup( 99 name = "mylib.dSYM", 100 srcs = [":mylib"], 101 output_group = "dsym_folder", 102 ) 103 104 dsym_folder_test( 105 name = "lib_dsym_test", 106 target_under_test = ":mylib.dSYM", 107 target_compatible_with = ["@platforms//os:macos"], 108 ) 109 110 rust_binary( 111 name = "myrustbin", 112 srcs = ["main.rs"], 113 edition = "2018", 114 ) 115 116 native.filegroup( 117 name = "mybin.pdb", 118 srcs = [":myrustbin"], 119 output_group = "pdb_file", 120 ) 121 122 for compilation_mode, pdb_test in pdb_file_tests.items(): 123 pdb_test( 124 name = "bin_pdb_test_{}".format(compilation_mode), 125 target_under_test = ":mybin.pdb", 126 target_compatible_with = ["@platforms//os:windows"], 127 ) 128 129 native.filegroup( 130 name = "mybin.dSYM", 131 srcs = [":myrustbin"], 132 output_group = "dsym_folder", 133 ) 134 135 dsym_folder_test( 136 name = "bin_dsym_test", 137 target_under_test = ":mybin.dSYM", 138 target_compatible_with = ["@platforms//os:macos"], 139 ) 140 141 rust_test( 142 name = "myrusttest", 143 srcs = ["test.rs"], 144 edition = "2018", 145 ) 146 147 native.filegroup( 148 name = "mytest.pdb", 149 srcs = [":myrusttest"], 150 output_group = "pdb_file", 151 testonly = True, 152 ) 153 154 for compilation_mode, pdb_test in pdb_file_tests.items(): 155 pdb_test( 156 name = "test_pdb_test_{}".format(compilation_mode), 157 target_under_test = ":mytest.pdb", 158 target_compatible_with = ["@platforms//os:windows"], 159 ) 160 161 native.filegroup( 162 name = "mytest.dSYM", 163 srcs = [":myrusttest"], 164 output_group = "dsym_folder", 165 testonly = True, 166 ) 167 168 dsym_folder_test( 169 name = "test_dsym_test", 170 target_under_test = ":mytest.dSYM", 171 target_compatible_with = ["@platforms//os:macos"], 172 ) 173 174 native.test_suite( 175 name = name, 176 tests = [ 177 ":lib_dsym_test", 178 ":bin_dsym_test", 179 ":test_dsym_test", 180 ] + [ 181 ":lib_pdb_test_{}".format(compilation_mode) 182 for compilation_mode in pdb_file_tests 183 ] + [ 184 ":bin_pdb_test_{}".format(compilation_mode) 185 for compilation_mode in pdb_file_tests 186 ] + [ 187 ":test_pdb_test_{}".format(compilation_mode) 188 for compilation_mode in pdb_file_tests 189 ], 190 ) 191