|
| 1 | +"""Analysistests for Windows-specific library naming and link flags.""" |
| 2 | + |
| 3 | +load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts") |
| 4 | +load("//rust/private:rustc.bzl", "portable_link_flags_for_testing", "symlink_for_ambiguous_lib_for_testing") |
| 5 | +load("//rust/private:utils.bzl", "get_lib_name_default", "get_lib_name_for_windows") |
| 6 | + |
| 7 | +LinkFlagsInfo = provider(fields = {"flags": "List[str]"}) |
| 8 | +SymlinkInfo = provider(fields = {"symlink": "File"}) |
| 9 | + |
| 10 | +def _portable_link_flags_probe_impl(ctx): |
| 11 | + lib_artifact = ctx.actions.declare_file(ctx.attr.lib_basename) |
| 12 | + ctx.actions.write(lib_artifact, "", is_executable = False) |
| 13 | + library_to_link = struct( |
| 14 | + static_library = lib_artifact, |
| 15 | + pic_static_library = None, |
| 16 | + dynamic_library = None, |
| 17 | + interface_library = None, |
| 18 | + alwayslink = False, |
| 19 | + ) |
| 20 | + |
| 21 | + get_lib_name = get_lib_name_for_windows if ctx.attr.flavor_msvc else get_lib_name_default |
| 22 | + flags = portable_link_flags_for_testing( |
| 23 | + lib = library_to_link, |
| 24 | + use_pic = False, |
| 25 | + ambiguous_libs = {}, |
| 26 | + get_lib_name = get_lib_name, |
| 27 | + for_windows = True, |
| 28 | + flavor_msvc = ctx.attr.flavor_msvc, |
| 29 | + ) |
| 30 | + |
| 31 | + return [ |
| 32 | + DefaultInfo(files = depset([])), |
| 33 | + LinkFlagsInfo(flags = flags), |
| 34 | + ] |
| 35 | + |
| 36 | +portable_link_flags_probe = rule( |
| 37 | + implementation = _portable_link_flags_probe_impl, |
| 38 | + attrs = { |
| 39 | + "flavor_msvc": attr.bool(default = False), |
| 40 | + "lib_basename": attr.string(mandatory = True), |
| 41 | + }, |
| 42 | +) |
| 43 | + |
| 44 | +def _symlink_probe_impl(ctx): |
| 45 | + lib_artifact = ctx.actions.declare_file(ctx.attr.lib_basename) |
| 46 | + ctx.actions.write(lib_artifact, "", is_executable = False) |
| 47 | + crate_output = ctx.actions.declare_file("crate.rlib") |
| 48 | + ctx.actions.write(crate_output, "", is_executable = False) |
| 49 | + symlink = symlink_for_ambiguous_lib_for_testing( |
| 50 | + ctx.actions, |
| 51 | + toolchain = struct(target_abi = ctx.attr.target_abi), |
| 52 | + crate_info = struct(output = crate_output), |
| 53 | + lib = lib_artifact, |
| 54 | + ) |
| 55 | + |
| 56 | + return [ |
| 57 | + SymlinkInfo(symlink = symlink), |
| 58 | + DefaultInfo(files = depset([symlink])), |
| 59 | + ] |
| 60 | + |
| 61 | +symlink_probe = rule( |
| 62 | + implementation = _symlink_probe_impl, |
| 63 | + attrs = { |
| 64 | + "lib_basename": attr.string(mandatory = True), |
| 65 | + "target_abi": attr.string(mandatory = True), |
| 66 | + }, |
| 67 | +) |
| 68 | + |
| 69 | +def _portable_link_flags_windows_gnu_test_impl(ctx): |
| 70 | + env = analysistest.begin(ctx) |
| 71 | + flags = analysistest.target_under_test(env)[LinkFlagsInfo].flags |
| 72 | + |
| 73 | + asserts.equals( |
| 74 | + env, |
| 75 | + ["-lstatic=foo.dll", "-Clink-arg=-lfoo.dll"], |
| 76 | + flags, |
| 77 | + ) |
| 78 | + return analysistest.end(env) |
| 79 | + |
| 80 | +portable_link_flags_windows_gnu_test = analysistest.make( |
| 81 | + _portable_link_flags_windows_gnu_test_impl, |
| 82 | +) |
| 83 | + |
| 84 | +def _portable_link_flags_windows_msvc_test_impl(ctx): |
| 85 | + env = analysistest.begin(ctx) |
| 86 | + flags = analysistest.target_under_test(env)[LinkFlagsInfo].flags |
| 87 | + |
| 88 | + asserts.equals( |
| 89 | + env, |
| 90 | + ["-lstatic=libfoo.dll", "-Clink-arg=libfoo.dll.lib"], |
| 91 | + flags, |
| 92 | + ) |
| 93 | + return analysistest.end(env) |
| 94 | + |
| 95 | +portable_link_flags_windows_msvc_test = analysistest.make( |
| 96 | + _portable_link_flags_windows_msvc_test_impl, |
| 97 | +) |
| 98 | + |
| 99 | +def _symlink_name_windows_gnu_test_impl(ctx): |
| 100 | + env = analysistest.begin(ctx) |
| 101 | + symlink = analysistest.target_under_test(env)[SymlinkInfo].symlink |
| 102 | + |
| 103 | + asserts.true(env, symlink.basename.startswith("libfoo.dll-")) |
| 104 | + asserts.true(env, symlink.basename.endswith(".a")) |
| 105 | + asserts.false(env, symlink.basename.startswith("liblib")) |
| 106 | + |
| 107 | + return analysistest.end(env) |
| 108 | + |
| 109 | +symlink_name_windows_gnu_test = analysistest.make(_symlink_name_windows_gnu_test_impl) |
| 110 | + |
| 111 | +def _symlink_name_windows_msvc_test_impl(ctx): |
| 112 | + env = analysistest.begin(ctx) |
| 113 | + symlink = analysistest.target_under_test(env)[SymlinkInfo].symlink |
| 114 | + |
| 115 | + asserts.true(env, symlink.basename.startswith("native_dep-")) |
| 116 | + asserts.true(env, symlink.basename.endswith(".lib")) |
| 117 | + |
| 118 | + return analysistest.end(env) |
| 119 | + |
| 120 | +symlink_name_windows_msvc_test = analysistest.make(_symlink_name_windows_msvc_test_impl) |
| 121 | + |
| 122 | +def _define_targets(): |
| 123 | + portable_link_flags_probe( |
| 124 | + name = "portable_link_flags_windows_gnu_probe", |
| 125 | + flavor_msvc = False, |
| 126 | + lib_basename = "libfoo.dll.a", |
| 127 | + ) |
| 128 | + portable_link_flags_probe( |
| 129 | + name = "portable_link_flags_windows_msvc_probe", |
| 130 | + flavor_msvc = True, |
| 131 | + lib_basename = "libfoo.dll.lib", |
| 132 | + ) |
| 133 | + |
| 134 | + symlink_probe( |
| 135 | + name = "symlink_windows_gnu_probe", |
| 136 | + lib_basename = "libfoo.dll.a", |
| 137 | + target_abi = "gnu", |
| 138 | + ) |
| 139 | + symlink_probe( |
| 140 | + name = "symlink_windows_msvc_probe", |
| 141 | + lib_basename = "native_dep.lib", |
| 142 | + target_abi = "msvc", |
| 143 | + ) |
| 144 | + |
| 145 | +def windows_lib_name_test_suite(name): |
| 146 | + """Entry-point macro for Windows library naming tests.""" |
| 147 | + _define_targets() |
| 148 | + |
| 149 | + portable_link_flags_windows_gnu_test( |
| 150 | + name = "portable_link_flags_windows_gnu_test", |
| 151 | + target_under_test = ":portable_link_flags_windows_gnu_probe", |
| 152 | + ) |
| 153 | + portable_link_flags_windows_msvc_test( |
| 154 | + name = "portable_link_flags_windows_msvc_test", |
| 155 | + target_under_test = ":portable_link_flags_windows_msvc_probe", |
| 156 | + ) |
| 157 | + symlink_name_windows_gnu_test( |
| 158 | + name = "symlink_name_windows_gnu_test", |
| 159 | + target_under_test = ":symlink_windows_gnu_probe", |
| 160 | + ) |
| 161 | + symlink_name_windows_msvc_test( |
| 162 | + name = "symlink_name_windows_msvc_test", |
| 163 | + target_under_test = ":symlink_windows_msvc_probe", |
| 164 | + ) |
| 165 | + |
| 166 | + native.test_suite( |
| 167 | + name = name, |
| 168 | + tests = [ |
| 169 | + ":portable_link_flags_windows_gnu_test", |
| 170 | + ":portable_link_flags_windows_msvc_test", |
| 171 | + ":symlink_name_windows_gnu_test", |
| 172 | + ":symlink_name_windows_msvc_test", |
| 173 | + ], |
| 174 | + ) |
0 commit comments