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