use std::ffi::OsString;
use std::path::{Path, PathBuf};
use super::{BuildSettings, OptLevel, ProfileSettings, Subsystem, TARGET_SWITCH};
use crate::Version;
use crate::compiler::{CompilerKind, Family};
#[derive(Clone, Copy, Debug)]
pub struct LinkInput<'a> {
pub kind: CompilerKind,
pub version: Version,
pub build: &'a BuildSettings,
pub profile: &'a ProfileSettings,
pub subsystem: Subsystem,
pub cxx: bool,
}
pub fn library_files(kind: CompilerKind, lib: &str) -> Vec<String> {
match kind {
CompilerKind::Msvc | CompilerKind::ClangCl | CompilerKind::Clang => {
vec![format!("{lib}.lib")]
}
CompilerKind::Gcc => vec![
format!("lib{lib}.dll.a"),
format!("{lib}.dll.a"),
format!("lib{lib}.a"),
format!("{lib}.lib"),
format!("lib{lib}.lib"),
format!("lib{lib}.dll"),
format!("{lib}.dll"),
],
}
}
fn gcc_runtime_args(cxx: bool, version: Version) -> Vec<&'static str> {
let mut args = vec!["-static-libgcc"];
if cxx {
args.push("-static-libstdc++");
}
args.push("-Wl,-Bstatic");
if cxx {
if version.major >= 14 {
args.push("-lstdc++exp");
}
args.push("-lstdc++");
}
args.extend(["-lwinpthread", "-Wl,-Bdynamic"]);
args
}
pub fn link_args(input: &LinkInput<'_>, objects: &[PathBuf], exe: &Path) -> Vec<OsString> {
let build = input.build;
let profile = input.profile;
let lto_level = match profile.opt_level {
OptLevel::O0 => "0",
OptLevel::O1 => "1",
OptLevel::O2 | OptLevel::Size => "2",
OptLevel::O3 => "3",
};
let mut args: Vec<OsString> = Vec::new();
let push = |args: &mut Vec<OsString>, flags: &[&str]| {
args.extend(flags.iter().map(OsString::from));
};
match input.kind {
CompilerKind::Msvc => push(&mut args, &["/nologo"]),
CompilerKind::ClangCl => {
push(&mut args, &["/nologo", "-fuse-ld=lld"]);
if profile.lto {
push(&mut args, &["-flto"]);
}
}
CompilerKind::Clang => {
push(&mut args, &[TARGET_SWITCH, "-fuse-ld=lld"]);
if profile.debug_info {
push(&mut args, &["-g"]);
}
if profile.lto {
push(&mut args, &["-flto"]);
}
}
CompilerKind::Gcc => {
if profile.debug_info {
push(&mut args, &["-g"]);
}
if profile.lto {
push(&mut args, &["-flto=auto"]);
}
}
}
args.extend(objects.iter().map(OsString::from));
match input.kind {
CompilerKind::Clang | CompilerKind::Gcc => {
args.extend(["-o".into(), exe.into()]);
for lib in &build.links {
args.push(format!("-l{lib}").into());
}
for dir in &build.lib_dirs {
let mut arg = OsString::from("-L");
arg.push(dir);
args.push(arg);
}
let gcc = input.kind == CompilerKind::Gcc;
push(
&mut args,
match (input.subsystem, gcc) {
(Subsystem::Console, false) => &["-Wl,/subsystem:console"][..],
(Subsystem::Windows, false) => {
&["-Wl,/subsystem:windows", "-Wl,/entry:mainCRTStartup"]
}
(Subsystem::Console, true) => &["-mconsole"],
(Subsystem::Windows, true) => &["-mwindows"],
},
);
if gcc {
push(&mut args, &gcc_runtime_args(input.cxx, input.version));
} else if profile.lto {
args.push(format!("-Wl,/opt:lldlto={lto_level}").into());
}
for flag in &build.link_flags {
args.extend(["-Xlinker".into(), flag.into()]);
}
return args;
}
CompilerKind::Msvc | CompilerKind::ClangCl => {}
}
let mut out = OsString::from("/Fe:");
out.push(exe);
args.push(out);
args.push("/link".into());
for lib in &build.links {
args.push(library_files(input.kind, lib).remove(0).into());
}
for dir in &build.lib_dirs {
let mut arg = OsString::from("/LIBPATH:");
arg.push(dir);
args.push(arg);
}
push(
&mut args,
match input.subsystem {
Subsystem::Console => &["/SUBSYSTEM:CONSOLE"][..],
Subsystem::Windows => &["/SUBSYSTEM:WINDOWS", "/ENTRY:mainCRTStartup"],
},
);
if profile.debug_info {
push(&mut args, &["/DEBUG"]);
match input.kind.family() {
Family::Msvc => push(&mut args, &["/INCREMENTAL:NO"]),
Family::Llvm | Family::Gnu => {}
}
}
if profile.lto {
match input.kind.family() {
Family::Msvc => push(&mut args, &["/LTCG"]),
Family::Llvm | Family::Gnu => args.push(format!("/opt:lldlto={lto_level}").into()),
}
}
args.extend(build.link_flags.iter().map(OsString::from));
args
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_library_is_the_files_its_linker_tries() {
for kind in CompilerKind::ALL {
let want: Vec<&str> = match kind {
CompilerKind::Msvc | CompilerKind::ClangCl | CompilerKind::Clang => {
vec!["foo.lib"]
}
CompilerKind::Gcc => vec![
"libfoo.dll.a",
"foo.dll.a",
"libfoo.a",
"foo.lib",
"libfoo.lib",
"libfoo.dll",
"foo.dll",
],
};
assert_eq!(library_files(kind, "foo"), want, "{kind:?}");
}
}
use crate::flags::Runtime;
fn build() -> BuildSettings {
BuildSettings {
warnings: crate::flags::Warnings::Default,
warnings_as_errors: false,
defines: vec![],
include_dirs: vec![],
links: vec![],
lib_dirs: vec![],
link_flags: vec![],
flags: vec![],
runtime: Runtime::Dynamic,
exceptions: true,
rtti: true,
}
}
fn profile() -> ProfileSettings {
ProfileSettings {
opt_level: OptLevel::O0,
debug_info: false,
debug_assertions: true,
lto: false,
}
}
fn args(
kind: CompilerKind,
build: &BuildSettings,
profile: &ProfileSettings,
subsystem: Subsystem,
) -> Vec<String> {
args_for(kind, build, profile, subsystem, true)
}
fn args_for(
kind: CompilerKind,
build: &BuildSettings,
profile: &ProfileSettings,
subsystem: Subsystem,
cxx: bool,
) -> Vec<String> {
let input = LinkInput {
kind,
version: Version {
major: 13,
minor: 2,
patch: 0,
},
build,
profile,
subsystem,
cxx,
};
link_args(
&input,
&[PathBuf::from("a.obj"), PathBuf::from("b.obj")],
Path::new("app.exe"),
)
.into_iter()
.map(|a| a.into_string().unwrap())
.collect()
}
#[test]
fn the_defaults_give_each_drivers_link_line() {
let (b, p) = (build(), profile());
assert_eq!(
args(CompilerKind::Msvc, &b, &p, Subsystem::Console),
[
"/nologo",
"a.obj",
"b.obj",
"/Fe:app.exe",
"/link",
"/SUBSYSTEM:CONSOLE"
]
);
assert_eq!(
args(CompilerKind::ClangCl, &b, &p, Subsystem::Console),
[
"/nologo",
"-fuse-ld=lld",
"a.obj",
"b.obj",
"/Fe:app.exe",
"/link",
"/SUBSYSTEM:CONSOLE"
]
);
let static_runtime = BuildSettings {
runtime: Runtime::Static,
..build()
};
for b in [&b, &static_runtime] {
assert_eq!(
args(CompilerKind::Clang, b, &p, Subsystem::Console),
[
"--target=x86_64-pc-windows-msvc",
"-fuse-ld=lld",
"a.obj",
"b.obj",
"-o",
"app.exe",
"-Wl,/subsystem:console"
]
);
}
for b in [&b, &static_runtime] {
assert_eq!(
args_for(CompilerKind::Gcc, b, &p, Subsystem::Console, true),
[
"a.obj",
"b.obj",
"-o",
"app.exe",
"-mconsole",
"-static-libgcc",
"-static-libstdc++",
"-Wl,-Bstatic",
"-lstdc++",
"-lwinpthread",
"-Wl,-Bdynamic"
]
);
assert_eq!(
args_for(CompilerKind::Gcc, b, &p, Subsystem::Console, false),
[
"a.obj",
"b.obj",
"-o",
"app.exe",
"-mconsole",
"-static-libgcc",
"-Wl,-Bstatic",
"-lwinpthread",
"-Wl,-Bdynamic"
]
);
}
}
#[test]
fn every_setting_gives_the_tables_link_arguments() {
let full_build = BuildSettings {
links: vec!["user32".into(), "gdi32".into()],
lib_dirs: vec!["libs".into(), "more".into()],
link_flags: vec!["/OPT:REF,ICF".into(), "/STACK:2097152".into()],
..build()
};
let full_profile = ProfileSettings {
opt_level: OptLevel::O3,
debug_info: true,
lto: true,
..profile()
};
let w = Subsystem::Windows;
assert_eq!(
args(CompilerKind::Msvc, &full_build, &full_profile, w),
[
"/nologo",
"a.obj",
"b.obj",
"/Fe:app.exe",
"/link",
"user32.lib",
"gdi32.lib",
"/LIBPATH:libs",
"/LIBPATH:more",
"/SUBSYSTEM:WINDOWS",
"/ENTRY:mainCRTStartup",
"/DEBUG",
"/INCREMENTAL:NO",
"/LTCG",
"/OPT:REF,ICF",
"/STACK:2097152"
]
);
assert_eq!(
args(CompilerKind::ClangCl, &full_build, &full_profile, w),
[
"/nologo",
"-fuse-ld=lld",
"-flto",
"a.obj",
"b.obj",
"/Fe:app.exe",
"/link",
"user32.lib",
"gdi32.lib",
"/LIBPATH:libs",
"/LIBPATH:more",
"/SUBSYSTEM:WINDOWS",
"/ENTRY:mainCRTStartup",
"/DEBUG",
"/opt:lldlto=3",
"/OPT:REF,ICF",
"/STACK:2097152"
]
);
assert_eq!(
args(CompilerKind::Clang, &full_build, &full_profile, w),
[
"--target=x86_64-pc-windows-msvc",
"-fuse-ld=lld",
"-g",
"-flto",
"a.obj",
"b.obj",
"-o",
"app.exe",
"-luser32",
"-lgdi32",
"-Llibs",
"-Lmore",
"-Wl,/subsystem:windows",
"-Wl,/entry:mainCRTStartup",
"-Wl,/opt:lldlto=3",
"-Xlinker",
"/OPT:REF,ICF",
"-Xlinker",
"/STACK:2097152"
]
);
assert_eq!(
args(CompilerKind::Gcc, &full_build, &full_profile, w),
[
"-g",
"-flto=auto",
"a.obj",
"b.obj",
"-o",
"app.exe",
"-luser32",
"-lgdi32",
"-Llibs",
"-Lmore",
"-mwindows",
"-static-libgcc",
"-static-libstdc++",
"-Wl,-Bstatic",
"-lstdc++",
"-lwinpthread",
"-Wl,-Bdynamic",
"-Xlinker",
"/OPT:REF,ICF",
"-Xlinker",
"/STACK:2097152"
]
);
}
#[test]
fn lto_optimises_at_the_profiles_level() {
for (level, n) in [
(OptLevel::O0, "0"),
(OptLevel::O1, "1"),
(OptLevel::O2, "2"),
(OptLevel::O3, "3"),
(OptLevel::Size, "2"),
] {
let p = ProfileSettings {
opt_level: level,
lto: true,
..profile()
};
let clang_cl = args(CompilerKind::ClangCl, &build(), &p, Subsystem::Console);
assert!(
clang_cl.contains(&format!("/opt:lldlto={n}")),
"{level:?} {clang_cl:?}"
);
let clang = args(CompilerKind::Clang, &build(), &p, Subsystem::Console);
assert!(
clang.contains(&format!("-Wl,/opt:lldlto={n}")),
"{level:?} {clang:?}"
);
let msvc = args(CompilerKind::Msvc, &build(), &p, Subsystem::Console);
assert!(!msvc.iter().any(|a| a.contains("lldlto")), "{msvc:?}");
let gcc = args(CompilerKind::Gcc, &build(), &p, Subsystem::Console);
assert!(!gcc.iter().any(|a| a.contains("lldlto")), "{gcc:?}");
}
}
#[test]
fn gcc_14_links_libstdcxx_exp_for_cxx() {
let v = |major| Version {
major,
minor: 1,
patch: 0,
};
assert_eq!(
gcc_runtime_args(true, v(14)),
[
"-static-libgcc",
"-static-libstdc++",
"-Wl,-Bstatic",
"-lstdc++exp",
"-lstdc++",
"-lwinpthread",
"-Wl,-Bdynamic",
]
);
assert!(!gcc_runtime_args(true, v(13)).contains(&"-lstdc++exp"));
assert!(!gcc_runtime_args(false, v(16)).contains(&"-lstdc++exp"));
}
}