From f93423986f0c9bfb6ad4ec1b28702ec7461fdcd3 Mon Sep 17 00:00:00 2001 From: messense Date: Wed, 14 Sep 2022 22:07:15 +0800 Subject: [PATCH 1/2] Add cmake test case --- .github/workflows/CI.yml | 1 + tests/hello-cmake/Cargo.lock | 56 +++++++++++++++++++++++++++++++++++ tests/hello-cmake/Cargo.toml | 9 ++++++ tests/hello-cmake/src/main.rs | 10 +++++++ 4 files changed, 76 insertions(+) create mode 100644 tests/hello-cmake/Cargo.lock create mode 100644 tests/hello-cmake/Cargo.toml create mode 100644 tests/hello-cmake/src/main.rs diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index f2e80d5..762ca76 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -122,6 +122,7 @@ jobs: cargo run zigbuild --target aarch64-unknown-linux-gnu cargo run zigbuild --target aarch64-unknown-linux-gnu.2.17 + cargo run zigbuild --target aarch64-unknown-linux-gnu --manifest-path tests/hello-cmake/Cargo.toml cargo run zigbuild --target aarch64-unknown-linux-gnu --manifest-path tests/hello-rustls/Cargo.toml cargo run zigbuild --target armv7-unknown-linux-gnueabihf --manifest-path tests/hello-rustls/Cargo.toml cargo run zigbuild --target arm-unknown-linux-gnueabihf --manifest-path tests/hello-rustls/Cargo.toml diff --git a/tests/hello-cmake/Cargo.lock b/tests/hello-cmake/Cargo.lock new file mode 100644 index 0000000..720c22a --- /dev/null +++ b/tests/hello-cmake/Cargo.lock @@ -0,0 +1,56 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "cc" +version = "1.0.73" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2fff2a6927b3bb87f9595d67196a70493f627687a71d87a0d692242c33f58c11" + +[[package]] +name = "cmake" +version = "0.1.48" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8ad8cef104ac57b68b89df3208164d228503abbdce70f6880ffa3d970e7443a" +dependencies = [ + "cc", +] + +[[package]] +name = "hello-cmake" +version = "0.1.0" +dependencies = [ + "libz-sys", +] + +[[package]] +name = "libc" +version = "0.2.132" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8371e4e5341c3a96db127eb2465ac681ced4c433e01dd0e938adbef26ba93ba5" + +[[package]] +name = "libz-sys" +version = "1.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9702761c3935f8cc2f101793272e202c72b99da8f4224a19ddcf1279a6450bbf" +dependencies = [ + "cc", + "cmake", + "libc", + "pkg-config", + "vcpkg", +] + +[[package]] +name = "pkg-config" +version = "0.3.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1df8c4ec4b0627e53bdf214615ad287367e482558cf84b109250b37464dc03ae" + +[[package]] +name = "vcpkg" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" diff --git a/tests/hello-cmake/Cargo.toml b/tests/hello-cmake/Cargo.toml new file mode 100644 index 0000000..552f525 --- /dev/null +++ b/tests/hello-cmake/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "hello-cmake" +version = "0.1.0" +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +libz-sys = { version = "1.1.8", default-features = false, features = ["zlib-ng"] } diff --git a/tests/hello-cmake/src/main.rs b/tests/hello-cmake/src/main.rs new file mode 100644 index 0000000..b7ac35b --- /dev/null +++ b/tests/hello-cmake/src/main.rs @@ -0,0 +1,10 @@ +use std::ffi::CStr; + +use libz_sys::zlibVersion; + +fn main() { + let ver = unsafe { zlibVersion() }; + let ver_cstr = unsafe { CStr::from_ptr(ver) }; + let version = ver_cstr.to_str().unwrap(); + assert!(!version.is_empty()); +} From fbc5d186768781a7f6e011d1653247beb8bd3509 Mon Sep 17 00:00:00 2001 From: messense Date: Thu, 15 Sep 2022 11:17:00 +0800 Subject: [PATCH 2/2] Setup cmake toolchain file --- src/zig.rs | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/src/zig.rs b/src/zig.rs index ca7cd37..38f06f2 100644 --- a/src/zig.rs +++ b/src/zig.rs @@ -375,6 +375,19 @@ impl Zig { Self::setup_os_deps(cargo)?; + let cmake_toolchain_file_env = format!("CMAKE_TOOLCHAIN_FILE_{}", env_target); + if env::var_os(&cmake_toolchain_file_env).is_none() + && env::var_os(format!("CMAKE_TOOLCHAIN_FILE_{}", parsed_target)).is_none() + && env::var_os("TARGET_CMAKE_TOOLCHAIN_FILE").is_none() + && env::var_os("CMAKE_TOOLCHAIN_FILE").is_none() + { + if let Ok(cmake_toolchain_file) = + Self::setup_cmake_toolchain(parsed_target, &zig_wrapper) + { + cmd.env(cmake_toolchain_file_env, cmake_toolchain_file); + } + } + if raw_target.contains("windows-gnu") { cmd.env("WINAPI_NO_BUNDLED_LIBRARIES", "1"); } @@ -451,6 +464,51 @@ impl Zig { Ok(()) } + fn setup_cmake_toolchain(target: &str, zig_wrapper: &ZigWrapper) -> Result { + let cmake = cache_dir()?.join("cmake"); + fs::create_dir_all(&cmake)?; + + let toolchain_file = cmake.join(format!("{}-toolchain.cmake", target)); + let triple: Triple = target.parse()?; + let os = triple.operating_system.to_string(); + let arch = triple.architecture.to_string(); + let (system_name, system_processor) = match (os.as_str(), arch.as_str()) { + ("darwin", "x86_64") => ("Darwin", "x86_64"), + ("darwin", "aarch64") => ("Darwin", "arm64"), + ("linux", arch) => { + let cmake_arch = match arch { + "powerpc" => "ppc", + "powerpc64" => "ppc64", + "powerpc64le" => "ppc64le", + _ => arch, + }; + ("Linux", cmake_arch) + } + ("windows", "x86_64") => ("Windows", "AMD64"), + ("windows", "i686") => ("Windows", "X86"), + ("windows", "aarch64") => ("Windows", "ARM64"), + (os, arch) => (os, arch), + }; + let content = format!( + r#" +set(CMAKE_SYSTEM_NAME {system_name}) +set(CMAKE_SYSTEM_PROCESSOR {system_processor}) +set(CMAKE_C_COMPILER {cc}) +set(CMAKE_CXX_COMPILER {cxx}) +set(CMAKE_AR {ar}) +set(CMAKE_RANLIB {ranlib}) + "#, + system_name = system_name, + system_processor = system_processor, + cc = zig_wrapper.cc.display(), + cxx = zig_wrapper.cxx.display(), + ar = zig_wrapper.ar.display(), + ranlib = zig_wrapper.ranlib.display(), + ); + fs::write(&toolchain_file, content)?; + Ok(toolchain_file) + } + #[cfg(target_os = "macos")] fn macos_sdk_root() -> Option { match env::var_os("SDKROOT") {