From 802077b39a721f4dd92575da4810a4931441eb23 Mon Sep 17 00:00:00 2001 From: Kornel Date: Mon, 22 Nov 2021 00:44:08 +0000 Subject: [PATCH 1/8] Fix unwrap on compiler names without - --- build.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/build.rs b/build.rs index ed95444..53d6a56 100644 --- a/build.rs +++ b/build.rs @@ -207,10 +207,12 @@ fn build() -> io::Result<()> { let cc = cc::Build::new(); let compiler = cc.get_compiler(); let compiler = compiler.path().file_stem().unwrap().to_str().unwrap(); - let suffix_pos = compiler.rfind('-').unwrap(); // cut off "-gcc" - let prefix = compiler[0..suffix_pos].trim_end_matches("-wr"); // "wr-c++" compiler + if let Some(suffix_pos) = compiler.rfind('-') { + // cut off "-gcc" + let prefix = compiler[0..suffix_pos].trim_end_matches("-wr"); // "wr-c++" compiler + configure.arg(format!("--cross-prefix={}-", prefix)); + } - configure.arg(format!("--cross-prefix={}-", prefix)); configure.arg(format!( "--arch={}", env::var("CARGO_CFG_TARGET_ARCH").unwrap() From d6facaa6082a734e523722c386d8e9d186ed2311 Mon Sep 17 00:00:00 2001 From: Kornel Date: Mon, 22 Nov 2021 00:45:01 +0000 Subject: [PATCH 2/8] Check for actual target OS --- build.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/build.rs b/build.rs index 53d6a56..8468aa7 100644 --- a/build.rs +++ b/build.rs @@ -625,7 +625,7 @@ fn maybe_search_include(include_paths: &[PathBuf], header: &str) -> Option = if env::var("CARGO_FEATURE_BUILD").is_ok() { println!( "cargo:rustc-link-search=native={}", search().join("lib").to_string_lossy() ); - link_to_libraries(statik); + link_to_libraries(statik, &target_os); if fs::metadata(&search().join("lib").join("libavutil.a")).is_err() { fs::create_dir_all(&output()).expect("failed to create build directory"); fetch().unwrap(); @@ -684,7 +685,7 @@ fn main() { "cargo:rustc-link-search=native={}", ffmpeg_dir.join("lib").to_string_lossy() ); - link_to_libraries(statik); + link_to_libraries(statik, &target_os); vec![ffmpeg_dir.join("include")] } else if let Some(paths) = try_vcpkg(statik) { // vcpkg doesn't detect the "system" dependencies @@ -739,7 +740,7 @@ fn main() { .include_paths }; - if statik && cfg!(target_os = "macos") { + if statik && target_os == "macos" { let frameworks = vec![ "AppKit", "AudioToolbox", From 57c190e7de44cbd6f389e33ba0815ddeb3b8143a Mon Sep 17 00:00:00 2001 From: Kornel Date: Mon, 22 Nov 2021 00:45:18 +0000 Subject: [PATCH 3/8] Use the os from the target triple, rather than Rust's nickname --- build.rs | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/build.rs b/build.rs index 8468aa7..384b95e 100644 --- a/build.rs +++ b/build.rs @@ -189,7 +189,7 @@ fn switch(configure: &mut Command, feature: &str, name: &str) { configure.arg(arg.to_string() + name); } -fn build() -> io::Result<()> { +fn build(target_os: &str) -> io::Result<()> { let source_dir = source(); // Command's path is not relative to command's current_dir @@ -217,10 +217,7 @@ fn build() -> io::Result<()> { "--arch={}", env::var("CARGO_CFG_TARGET_ARCH").unwrap() )); - configure.arg(format!( - "--target_os={}", - env::var("CARGO_CFG_TARGET_OS").unwrap() - )); + configure.arg(format!("--target_os={}", target_os)); } // control debug build @@ -385,6 +382,18 @@ fn build() -> io::Result<()> { Ok(()) } +fn os_from_triple(triple: &str) -> &str { + let platform = triple.splitn(2, '-').nth(1).expect("bad triple"); + platform + .trim_start_matches("unknown-") + .trim_start_matches("pc-") + .trim_start_matches("wrs-") + .trim_start_matches("uwp-") + .split('-') + .next() + .unwrap() +} + #[cfg(not(target_env = "msvc"))] fn try_vcpkg(_statik: bool) -> Option> { None @@ -641,7 +650,8 @@ fn link_to_libraries(statik: bool, target_os: &str) { fn main() { let statik = env::var("CARGO_FEATURE_STATIC").is_ok(); let ffmpeg_major_version: u32 = env!("CARGO_PKG_VERSION_MAJOR").parse().unwrap(); - let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap(); + let target = env::var("TARGET").unwrap(); + let target_os = os_from_triple(&target); // it's different than Rust's target_os! but ./configure likes these better let include_paths: Vec = if env::var("CARGO_FEATURE_BUILD").is_ok() { println!( @@ -652,7 +662,7 @@ fn main() { if fs::metadata(&search().join("lib").join("libavutil.a")).is_err() { fs::create_dir_all(&output()).expect("failed to create build directory"); fetch().unwrap(); - build().unwrap(); + build(&target_os).unwrap(); } // Check additional required libraries. @@ -740,7 +750,7 @@ fn main() { .include_paths }; - if statik && target_os == "macos" { + if statik && target_os == "darwin" { let frameworks = vec![ "AppKit", "AudioToolbox", From e9f53e4ee09622e35ae07256f61ef56d5e61d30f Mon Sep 17 00:00:00 2001 From: Kornel Date: Mon, 22 Nov 2021 01:43:58 +0000 Subject: [PATCH 4/8] Explicitly enable cross-compilation in ./configure --- build.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/build.rs b/build.rs index 384b95e..febb9fd 100644 --- a/build.rs +++ b/build.rs @@ -200,7 +200,11 @@ fn build(target_os: &str) -> io::Result<()> { configure.arg(format!("--prefix={}", search().to_string_lossy())); - if env::var("TARGET").unwrap() != env::var("HOST").unwrap() { + let target = env::var("TARGET").unwrap(); + let host = env::var("HOST").unwrap(); + if target != host { + configure.arg("--enable-cross-compile"); + // Rust targets are subtly different than naming scheme for compiler prefixes. // The cc crate has the messy logic of guessing a working prefix, // and this is a messy way of reusing that logic. From a2b4ca7651d82d289a209c3cc4d283c4fbab7e48 Mon Sep 17 00:00:00 2001 From: Kornel Date: Mon, 22 Nov 2021 01:44:30 +0000 Subject: [PATCH 5/8] Set --target flag for Apple's clang when cross-compiling --- build.rs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/build.rs b/build.rs index febb9fd..552db65 100644 --- a/build.rs +++ b/build.rs @@ -209,6 +209,14 @@ fn build(target_os: &str) -> io::Result<()> { // The cc crate has the messy logic of guessing a working prefix, // and this is a messy way of reusing that logic. let cc = cc::Build::new(); + + // Apple-clang needs this, -arch is not enough. + let target_flag = format!("--target={}", target); + if cc.is_flag_supported(&target_flag).unwrap_or(false) { + configure.arg(format!("--extra-cflags={}", target_flag)); + configure.arg(format!("--extra-ldflags={}", target_flag)); + } + let compiler = cc.get_compiler(); let compiler = compiler.path().file_stem().unwrap().to_str().unwrap(); if let Some(suffix_pos) = compiler.rfind('-') { @@ -346,6 +354,8 @@ fn build(target_os: &str) -> io::Result<()> { // configure misc build options enable!(configure, "BUILD_PIC", "pic"); + println!("configure cmd: {:?}", configure); + // run ./configure let output = configure .output() @@ -392,6 +402,7 @@ fn os_from_triple(triple: &str) -> &str { .trim_start_matches("unknown-") .trim_start_matches("pc-") .trim_start_matches("wrs-") + .trim_start_matches("apple-") .trim_start_matches("uwp-") .split('-') .next() @@ -662,11 +673,11 @@ fn main() { "cargo:rustc-link-search=native={}", search().join("lib").to_string_lossy() ); - link_to_libraries(statik, &target_os); + link_to_libraries(statik, target_os); if fs::metadata(&search().join("lib").join("libavutil.a")).is_err() { fs::create_dir_all(&output()).expect("failed to create build directory"); fetch().unwrap(); - build(&target_os).unwrap(); + build(target_os).unwrap(); } // Check additional required libraries. @@ -699,7 +710,7 @@ fn main() { "cargo:rustc-link-search=native={}", ffmpeg_dir.join("lib").to_string_lossy() ); - link_to_libraries(statik, &target_os); + link_to_libraries(statik, target_os); vec![ffmpeg_dir.join("include")] } else if let Some(paths) = try_vcpkg(statik) { // vcpkg doesn't detect the "system" dependencies From 9f75f57a0b059c4ad58e216990c3d07e2a3d097d Mon Sep 17 00:00:00 2001 From: chrox Date: Tue, 4 Jan 2022 21:56:38 +0800 Subject: [PATCH 6/8] Fix cross-compilation for Windows on Linux --- build.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/build.rs b/build.rs index 552db65..78c89b9 100644 --- a/build.rs +++ b/build.rs @@ -229,7 +229,13 @@ fn build(target_os: &str) -> io::Result<()> { "--arch={}", env::var("CARGO_CFG_TARGET_ARCH").unwrap() )); - configure.arg(format!("--target_os={}", target_os)); + if target_os == "windows" { + // fix `configure: Unknown OS 'windows'` + configure.arg(format!("--target_os={}", "mingw32")); + } else { + configure.arg(format!("--target_os={}", target_os)); + } + } // control debug build From 2c0ff86e7bb5e3d9d179dbfef93a5b65f3cb4f59 Mon Sep 17 00:00:00 2001 From: Kornel Date: Wed, 19 Jan 2022 14:41:23 +0000 Subject: [PATCH 7/8] Clippy lint --- build.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.rs b/build.rs index 78c89b9..a5ff194 100644 --- a/build.rs +++ b/build.rs @@ -403,7 +403,7 @@ fn build(target_os: &str) -> io::Result<()> { } fn os_from_triple(triple: &str) -> &str { - let platform = triple.splitn(2, '-').nth(1).expect("bad triple"); + let platform = triple.split_once('-').map(|x| x.1).expect("bad triple"); platform .trim_start_matches("unknown-") .trim_start_matches("pc-") From c6dc629aecebfcc12893aa1bc4338f99e9b71bef Mon Sep 17 00:00:00 2001 From: Kornel Date: Sun, 6 Feb 2022 13:36:12 +0000 Subject: [PATCH 8/8] rustfmt --- build.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/build.rs b/build.rs index a5ff194..401afb7 100644 --- a/build.rs +++ b/build.rs @@ -235,7 +235,6 @@ fn build(target_os: &str) -> io::Result<()> { } else { configure.arg(format!("--target_os={}", target_os)); } - } // control debug build