Skip to content

Commit

Permalink
build.rs: Fix windows cross compilation (#1323)
Browse files Browse the repository at this point in the history
@thedataking, I only noticed after you merged #1295 a few things.

This should let us cross-compile from windows, cross-compile to
`*-windows-gnu` (I think; the linker wasn't installed so I couldn't test
all the way), and print a clear error when trying to cross-compile to
`*-windows-msvc`. It also lets us do full cross-compilation on `rav1d`,
as the `snprintf` stuff for windows is only for `rav1d-cli`.

* Fixes #1365.
  • Loading branch information
kkysen authored Oct 31, 2024
2 parents c7d127e + 47059d1 commit b8715b6
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 41 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 5 additions & 41 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,10 +292,11 @@ mod asm {
nasm.files(asm_file_paths);
#[cfg(debug_assertions)]
nasm.flag("-g");
#[cfg(all(debug_assertions, not(windows)))]
nasm.flag("-Fdwarf");
#[cfg(all(debug_assertions, windows))]
nasm.flag("-fwin64");
#[cfg(debug_assertions)]
nasm.flag(match os {
"windows" => "-fwin64",
_ => "-Fdwarf",
});
nasm.flag(&format!("-I{}/", out_dir.to_str().unwrap()));
nasm.flag("-Isrc/");
let obj = nasm.compile_objects().unwrap_or_else(|e| {
Expand Down Expand Up @@ -331,41 +332,4 @@ fn main() {
{
asm::main();
}

// NOTE: we rely on libraries that are only distributed for Windows so
// targeting Windows/MSVC is not supported when cross compiling.
#[cfg(all(target_os = "windows", target_env = "msvc"))]
{
use cc::windows_registry;
use std::env;

let os = env::var("CARGO_CFG_TARGET_OS").expect("missing CARGO_CFG_TARGET_OS");
let target = env::var("TARGET").expect("missing TARGET");
if os == "windows" {
// for sprintf, snprintf, etc.
println!("cargo:rustc-link-lib=static=oldnames");
let tool = windows_registry::find_tool(&target, "cl.exe")
.expect("couldn't find cl.exe; are the Visual Studio C++ tools installed?");
let lib_paths = &tool
.env()
.iter()
.find(|(key, _val)| key == "LIB")
.expect("LIB path not found")
.1;
for path in lib_paths.to_str().unwrap().split(';') {
if path != "" {
println!("cargo:rustc-link-search={path}");
}
}

let getopt = "getopt";
cc::Build::new()
.files([&"tools/compat/getopt.c"])
.include("include/compat")
.debug(cfg!(debug_assertions))
.compile(&getopt);
// cc automatically outputs the following line
// println!("cargo:rustc-link-lib=static={getopt}");
}
}
}
3 changes: 3 additions & 0 deletions tools/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ cfg-if = "1.0.0"
libc = "0.2"
rav1d = { path = "../", version = "1.0.0", default-features = false }

[build-dependencies]
cc = "1.0.79"

[features]
default = ["asm", "asm_arm64_dotprod", "asm_arm64_i8mm", "bitdepth_8", "bitdepth_16"]
asm = ["rav1d/asm"]
Expand Down
52 changes: 52 additions & 0 deletions tools/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use std::env;

fn main() {
let os = env::var("CARGO_CFG_TARGET_OS").unwrap();
let env = env::var("CARGO_CFG_TARGET_ENV").unwrap();

let os = os.as_str();
let env = env.as_str();

match (os, env) {
("windows", "msvc") => {
if !cfg!(all(target_os = "windows", target_env = "msvc")) {
panic!("Cannot cross compile to *-windows-msvc");
}
}
#[allow(clippy::needless_return)]
_ => return,
}

// NOTE: we rely on libraries that are only distributed for Windows so
// targeting Windows/MSVC is not supported when cross compiling.
#[cfg(all(target_os = "windows", target_env = "msvc"))]
{
use cc::windows_registry;

// for sprintf, snprintf, etc.
let target = env::var("TARGET").unwrap();
println!("cargo:rustc-link-lib=static=oldnames");
let tool = windows_registry::find_tool(&target, "cl.exe")
.expect("couldn't find cl.exe; are the Visual Studio C++ tools installed?");
let lib_paths = &tool
.env()
.iter()
.find(|(key, _val)| key == "LIB")
.expect("LIB path not found")
.1;
for path in lib_paths.to_str().unwrap().split(';') {
if path != "" {
println!("cargo:rustc-link-search={path}");
}
}

let getopt = "getopt";
cc::Build::new()
.files([&"../tools/compat/getopt.c"])
.include("../include/compat")
.debug(cfg!(debug_assertions))
.compile(&getopt);
// cc automatically outputs the following line
// println!("cargo:rustc-link-lib=static={getopt}");
}
}

0 comments on commit b8715b6

Please sign in to comment.