From f9e0dae30fe84881dd9ec08bccf6743fcd685cd6 Mon Sep 17 00:00:00 2001 From: Khyber Sen Date: Wed, 17 Jul 2024 03:35:11 -0700 Subject: [PATCH 1/4] `build.rs`: Make `-Fdwarf`/`-fwin64` flag based on target os, not host os. Cross-compiles (even of just `rav1d` the library) failed without this. --- build.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/build.rs b/build.rs index 99e84cef2..e9a1421bf 100644 --- a/build.rs +++ b/build.rs @@ -292,10 +292,10 @@ 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"); + 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| { From 416843b470af7fb1ac2145f1603576b26143a2b1 Mon Sep 17 00:00:00 2001 From: Khyber Sen Date: Wed, 17 Jul 2024 03:59:40 -0700 Subject: [PATCH 2/4] `tools/build.rs`: Move `rav1d-lib` specific stuff to its own `build.rs` so `rav1d` can cross compile to/from windows. --- Cargo.lock | 1 + build.rs | 37 ------------------------------------- tools/Cargo.toml | 3 +++ tools/build.rs | 38 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 42 insertions(+), 37 deletions(-) create mode 100644 tools/build.rs diff --git a/Cargo.lock b/Cargo.lock index 396bbecb2..2160d070e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -168,6 +168,7 @@ dependencies = [ name = "rav1d-cli" version = "1.0.0" dependencies = [ + "cc", "cfg-if", "libc", "rav1d", diff --git a/build.rs b/build.rs index e9a1421bf..bdd4f68c2 100644 --- a/build.rs +++ b/build.rs @@ -331,41 +331,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}"); - } - } } diff --git a/tools/Cargo.toml b/tools/Cargo.toml index 7b0189a80..106f393db 100644 --- a/tools/Cargo.toml +++ b/tools/Cargo.toml @@ -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"] diff --git a/tools/build.rs b/tools/build.rs new file mode 100644 index 000000000..7746efacc --- /dev/null +++ b/tools/build.rs @@ -0,0 +1,38 @@ +fn 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}"); + } + } +} From a61cac8e0425187ef3dd7d6f53cd116583fcadb1 Mon Sep 17 00:00:00 2001 From: Khyber Sen Date: Wed, 17 Jul 2024 04:10:52 -0700 Subject: [PATCH 3/4] `tools/build.rs`: Only run when compiling for `*-windows-msvc`, and error if trying to cross compile to `*-windows-msvc`. --- tools/build.rs | 68 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 41 insertions(+), 27 deletions(-) diff --git a/tools/build.rs b/tools/build.rs index 7746efacc..878080ba1 100644 --- a/tools/build.rs +++ b/tools/build.rs @@ -1,38 +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; - 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}"); - } + // 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}"); } + + 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}"); } } From 47059d1efb04244bbbd96de0d27f43256840d499 Mon Sep 17 00:00:00 2001 From: Khyber Sen Date: Sat, 26 Oct 2024 01:19:40 -0700 Subject: [PATCH 4/4] `build.rs`: Add `#[cfg(debug_assertions)]` to `-F`/`-f` flags because they imply `-g` as well. --- build.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/build.rs b/build.rs index bdd4f68c2..3a30df14d 100644 --- a/build.rs +++ b/build.rs @@ -292,6 +292,7 @@ mod asm { nasm.files(asm_file_paths); #[cfg(debug_assertions)] nasm.flag("-g"); + #[cfg(debug_assertions)] nasm.flag(match os { "windows" => "-fwin64", _ => "-Fdwarf",