Skip to content

Commit

Permalink
Merge pull request #63 from messense/test-cmake
Browse files Browse the repository at this point in the history
Add cmake toolchain file
  • Loading branch information
messense authored Sep 15, 2022
2 parents e1df82f + fbc5d18 commit 7518b61
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
58 changes: 58 additions & 0 deletions src/zig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down Expand Up @@ -451,6 +464,51 @@ impl Zig {
Ok(())
}

fn setup_cmake_toolchain(target: &str, zig_wrapper: &ZigWrapper) -> Result<PathBuf> {
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<PathBuf> {
match env::var_os("SDKROOT") {
Expand Down
56 changes: 56 additions & 0 deletions tests/hello-cmake/Cargo.lock

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

9 changes: 9 additions & 0 deletions tests/hello-cmake/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"] }
10 changes: 10 additions & 0 deletions tests/hello-cmake/src/main.rs
Original file line number Diff line number Diff line change
@@ -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());
}

0 comments on commit 7518b61

Please sign in to comment.