File: rmake.rs

package info (click to toggle)
rustc 1.85.0%2Bdfsg2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 893,176 kB
  • sloc: xml: 158,127; python: 35,830; javascript: 19,497; cpp: 19,002; sh: 17,245; ansic: 13,127; asm: 4,376; makefile: 1,051; lisp: 29; perl: 29; ruby: 19; sql: 11
file content (52 lines) | stat: -rw-r--r-- 1,753 bytes parent folder | download | duplicates (14)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//! This is a regression test for #129020
//! It ensures we can use `/WHOLEARCHIVE` to link a rust staticlib into DLL
//! using the MSVC linker

//@ only-msvc
// Reason: this is testing the MSVC linker

use std::path::PathBuf;

use run_make_support::{cc, cmd, env_var, extra_c_flags, rustc};

fn main() {
    // Build the staticlib
    rustc().crate_type("staticlib").input("static.rs").output("static.lib").run();
    // Build an empty object to pass to the linker.
    cc().input("c.c").output("c.obj").args(["-c"]).run();

    // Find the C toolchain's linker.
    let mut linker = PathBuf::from(env_var("CC"));
    let linker_flavour = if linker.file_stem().is_some_and(|s| s == "cl") {
        linker.set_file_name("link.exe");
        "msvc"
    } else if linker.file_stem().is_some_and(|s| s == "clang-cl") {
        linker.set_file_name("lld-link.exe");
        "llvm"
    } else {
        panic!("unknown C toolchain");
    };

    // As a sanity check, make sure this works without /WHOLEARCHIVE.
    // Otherwise the actual test failure may be caused by something else.
    cmd(&linker)
        .args(["c.obj", "./static.lib", "-dll", "-def:dll.def", "-out:dll.dll"])
        .args(extra_c_flags())
        .run();

    // FIXME(@ChrisDenton): this doesn't currently work with llvm's lld-link for other reasons.
    // May need LLVM patches.
    if linker_flavour == "msvc" {
        // Link in the staticlib using `/WHOLEARCHIVE` and produce a DLL.
        cmd(&linker)
            .args([
                "c.obj",
                "-WHOLEARCHIVE:./static.lib",
                "-dll",
                "-def:dll.def",
                "-out:dll_whole_archive.dll",
            ])
            .args(extra_c_flags())
            .run();
    }
}