Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build.rs: Fix windows cross compilation #1323

Merged
merged 4 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
kkysen marked this conversation as resolved.
Show resolved Hide resolved
"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}");
}
}
Loading