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

Improve cross compiling support #158

Merged
merged 3 commits into from
Oct 28, 2022
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
69 changes: 68 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@ on: [push, pull_request]
jobs:
test:
name: Test
runs-on: ubuntu-latest
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
rust: [stable, beta, nightly]
steps:
- uses: actions/checkout@master
- name: Install Rust
run: rustup update ${{ matrix.rust }} && rustup default ${{ matrix.rust }}
- run: cargo test
- name: Integration test
run: cargo test --manifest-path test-crate/Cargo.toml

rustfmt:
name: Rustfmt
Expand Down Expand Up @@ -40,3 +43,67 @@ jobs:
git -c user.name='ci' -c user.email='ci' commit -m init
git push -f -q https://git:${{ secrets.github_token }}@github.com/${{ github.repository }} HEAD:gh-pages
if: github.event_name == 'push' && github.event.ref == 'refs/heads/master'

cross_compile_test:
name: Test Cross Compile - ${{ matrix.platform.target }}
needs: [ test ]
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
platform:
# Testable
- target: aarch64-unknown-linux-gnu
test: true
- target: arm-unknown-linux-gnueabihf
test: true
- target: armv7-unknown-linux-gnueabihf
test: true
- target: mips-unknown-linux-gnu
test: true
- target: mips64-unknown-linux-gnuabi64
test: true
- target: mips64el-unknown-linux-gnuabi64
test: true
- target: mipsel-unknown-linux-gnu
test: true
- target: powerpc-unknown-linux-gnu
test: true
- target: powerpc64-unknown-linux-gnu
test: true
- target: powerpc64le-unknown-linux-gnu
test: true
- target: s390x-unknown-linux-gnu
test: true
- target: x86_64-unknown-linux-musl
test: true
- target: aarch64-unknown-linux-musl
test: true
- target: x86_64-pc-windows-gnu
test: true
# Build only
- target: x86_64-unknown-freebsd
test: false
- target: x86_64-unknown-netbsd
test: false
- target: x86_64-sun-solaris
test: false
- target: x86_64-unknown-illumos
test: false
steps:
- uses: actions/checkout@master
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
target: ${{ matrix.platform.target }}
- uses: taiki-e/install-action@v1
with:
tool: cross
- name: cross test
run: cross test -vv --target ${{ matrix.platform.target }}
working-directory: test-crate
if: ${{ matrix.platform.test }}
- name: cross build
run: cross build -vv --target ${{ matrix.platform.target }}
working-directory: test-crate
if: ${{ !matrix.platform.test }}
60 changes: 49 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,55 @@ impl Config {
if !self.defined("CMAKE_TOOLCHAIN_FILE") {
if let Some(s) = self.getenv_target_os("CMAKE_TOOLCHAIN_FILE") {
self.define("CMAKE_TOOLCHAIN_FILE", s);
} else {
if target.contains("redox") {
if !self.defined("CMAKE_SYSTEM_NAME") {
self.define("CMAKE_SYSTEM_NAME", "Generic");
}
} else if target != host && !self.defined("CMAKE_SYSTEM_NAME") {
// Set CMAKE_SYSTEM_NAME and CMAKE_SYSTEM_PROCESSOR when cross compiling
let os = getenv_unwrap("CARGO_CFG_TARGET_OS");
let arch = getenv_unwrap("CARGO_CFG_TARGET_ARCH");
// CMAKE_SYSTEM_NAME list
// https://gitlab.kitware.com/cmake/cmake/-/issues/21489#note_1077167
//
// CMAKE_SYSTEM_PROCESSOR
// some of the values come from https://en.wikipedia.org/wiki/Uname
let (system_name, system_processor) = match (os.as_str(), arch.as_str()) {
("android", arch) => ("Android", arch),
("dragonfly", arch) => ("DragonFly", arch),
("macos", "x86_64") => ("Darwin", "x86_64"),
("macos", "aarch64") => ("Darwin", "arm64"),
("freebsd", "x86_64") => ("FreeBSD", "amd64"),
("freebsd", arch) => ("FreeBSD", arch),
("fuchsia", arch) => ("Fuchsia", arch),
("haiku", arch) => ("Haiku", arch),
("ios", "aarch64") => ("iOS", "arm64"),
("ios", arch) => ("iOS", arch),
("linux", arch) => {
let name = "Linux";
match arch {
"powerpc" => (name, "ppc"),
"powerpc64" => (name, "ppc64"),
"powerpc64le" => (name, "ppc64le"),
_ => (name, arch),
}
}
("netbsd", arch) => ("NetBSD", arch),
("openbsd", "x86_64") => ("OpenBSD", "amd64"),
("openbsd", arch) => ("OpenBSD", arch),
("solaris", arch) => ("SunOS", arch),
("tvos", arch) => ("tvOS", arch),
("watchos", arch) => ("watchOS", arch),
("windows", "x86_64") => ("Windows", "AMD64"),
("windows", "i686") => ("Windows", "X86"),
("windows", "aarch64") => ("Windows", "ARM64"),
// Others
(os, arch) => (os, arch),
};
self.define("CMAKE_SYSTEM_NAME", system_name);
self.define("CMAKE_SYSTEM_PROCESSOR", system_processor);
}
}
}

Expand Down Expand Up @@ -561,9 +610,6 @@ impl Config {
// variables which will hopefully get things to succeed. Some
// systems may need the `windres` or `dlltool` variables set, so
// set them if possible.
if !self.defined("CMAKE_SYSTEM_NAME") {
cmd.arg("-DCMAKE_SYSTEM_NAME=Windows");
}
if !self.defined("CMAKE_RC_COMPILER") {
let exe = find_exe(c_compiler.path());
if let Some(name) = exe.file_name().unwrap().to_str() {
Expand Down Expand Up @@ -614,14 +660,6 @@ impl Config {
panic!("unsupported msvc target: {}", target);
}
}
} else if target.contains("redox") {
if !self.defined("CMAKE_SYSTEM_NAME") {
cmd.arg("-DCMAKE_SYSTEM_NAME=Generic");
}
} else if target.contains("solaris") {
if !self.defined("CMAKE_SYSTEM_NAME") {
cmd.arg("-DCMAKE_SYSTEM_NAME=SunOS");
}
} else if target.contains("apple-ios") || target.contains("apple-tvos") {
// These two flags prevent CMake from adding an OSX sysroot, which messes up compilation.
if !self.defined("CMAKE_OSX_SYSROOT") && !self.defined("CMAKE_OSX_DEPLOYMENT_TARGET") {
Expand Down
2 changes: 2 additions & 0 deletions test-crate/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/target
/Cargo.lock
12 changes: 12 additions & 0 deletions test-crate/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "test-crate"
version = "0.1.0"
edition = "2021"

# 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"] }

[patch.crates-io]
cmake = { path = ".." }
13 changes: 13 additions & 0 deletions test-crate/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#[cfg(test)]
mod tests {
use libz_sys::zlibVersion;
use std::ffi::CStr;

#[test]
fn zlib_version() {
let ver = unsafe { zlibVersion() };
let ver_cstr = unsafe { CStr::from_ptr(ver) };
let version = ver_cstr.to_str().unwrap();
assert!(!version.is_empty());
}
}