Skip to content

Commit d668808

Browse files
messensethomcc
authored andcommitted
Improve cross compiling support
1 parent 163d33f commit d668808

File tree

5 files changed

+144
-11
lines changed

5 files changed

+144
-11
lines changed

.github/workflows/main.yml

+64
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,67 @@ jobs:
4040
git -c user.name='ci' -c user.email='ci' commit -m init
4141
git push -f -q https://git:${{ secrets.github_token }}@github.com/${{ github.repository }} HEAD:gh-pages
4242
if: github.event_name == 'push' && github.event.ref == 'refs/heads/master'
43+
44+
cross_compile_test:
45+
name: Test Cross Compile - ${{ matrix.platform.target }}
46+
needs: [ test ]
47+
runs-on: ubuntu-latest
48+
strategy:
49+
fail-fast: false
50+
matrix:
51+
platform:
52+
# Testable
53+
- target: aarch64-unknown-linux-gnu
54+
test: true
55+
- target: arm-unknown-linux-gnueabihf
56+
test: true
57+
- target: armv7-unknown-linux-gnueabihf
58+
test: true
59+
- target: mips-unknown-linux-gnu
60+
test: true
61+
- target: mips64-unknown-linux-gnuabi64
62+
test: true
63+
- target: mips64el-unknown-linux-gnuabi64
64+
test: true
65+
- target: mipsel-unknown-linux-gnu
66+
test: true
67+
- target: powerpc-unknown-linux-gnu
68+
test: true
69+
- target: powerpc64-unknown-linux-gnu
70+
test: true
71+
- target: powerpc64le-unknown-linux-gnu
72+
test: true
73+
- target: s390x-unknown-linux-gnu
74+
test: true
75+
- target: x86_64-unknown-linux-musl
76+
test: true
77+
- target: aarch64-unknown-linux-musl
78+
test: true
79+
- target: x86_64-pc-windows-gnu
80+
test: true
81+
# Build only
82+
- target: x86_64-unknown-freebsd
83+
test: false
84+
- target: x86_64-unknown-netbsd
85+
test: false
86+
- target: x86_64-sun-solaris
87+
test: false
88+
- target: x86_64-unknown-illumos
89+
test: false
90+
steps:
91+
- uses: actions/checkout@master
92+
- uses: actions-rs/toolchain@v1
93+
with:
94+
toolchain: stable
95+
target: ${{ matrix.platform.target }}
96+
- uses: taiki-e/install-action@v1
97+
with:
98+
tool: cross
99+
- name: cross test
100+
run: cross test -vv --target ${{ matrix.platform.target }}
101+
working-directory: test-crate
102+
if: ${{ matrix.platform.test }}
103+
- name: cross build
104+
run: cross build -vv --target ${{ matrix.platform.target }}
105+
working-directory: test-crate
106+
if: ${{ !matrix.platform.test }}

src/lib.rs

+53-11
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,59 @@ impl Config {
443443
if !self.defined("CMAKE_TOOLCHAIN_FILE") {
444444
if let Some(s) = self.getenv_target_os("CMAKE_TOOLCHAIN_FILE") {
445445
self.define("CMAKE_TOOLCHAIN_FILE", s);
446+
} else {
447+
if target.contains("redox") {
448+
if !self.defined("CMAKE_SYSTEM_NAME") {
449+
self.define("CMAKE_SYSTEM_NAME", "Generic");
450+
}
451+
} else if target.contains("solaris") {
452+
if !self.defined("CMAKE_SYSTEM_NAME") {
453+
self.define("CMAKE_SYSTEM_NAME", "SunOS");
454+
}
455+
} else if target != host && !self.defined("CMAKE_SYSTEM_NAME") {
456+
// Set CMAKE_SYSTEM_NAME and CMAKE_SYSTEM_PROCESSOR when cross compiling
457+
let os = getenv_unwrap("CARGO_CFG_TARGET_OS");
458+
let arch = getenv_unwrap("CARGO_CFG_TARGET_ARCH");
459+
// CMAKE_SYSTEM_NAME list
460+
// https://gitlab.kitware.com/cmake/cmake/-/issues/21489#note_1077167
461+
//
462+
// CMAKE_SYSTEM_PROCESSOR
463+
// some of the values come from https://en.wikipedia.org/wiki/Uname
464+
let (system_name, system_processor) = match (os.as_str(), arch.as_str()) {
465+
("android", arch) => ("Android", arch),
466+
("dragonfly", arch) => ("DragonFly", arch),
467+
("macos", "x86_64") => ("Darwin", "x86_64"),
468+
("macos", "aarch64") => ("Darwin", "arm64"),
469+
("freebsd", "x86_64") => ("FreeBSD", "amd64"),
470+
("freebsd", arch) => ("FreeBSD", arch),
471+
("fuchsia", arch) => ("Fuchsia", arch),
472+
("haiku", arch) => ("Haiku", arch),
473+
("ios", "aarch64") => ("iOS", "arm64"),
474+
("ios", arch) => ("iOS", arch),
475+
("linux", arch) => {
476+
let name = "Linux";
477+
match arch {
478+
"powerpc" => (name, "ppc"),
479+
"powerpc64" => (name, "ppc64"),
480+
"powerpc64le" => (name, "ppc64le"),
481+
_ => (name, arch),
482+
}
483+
}
484+
("netbsd", arch) => ("NetBSD", arch),
485+
("openbsd", "x86_64") => ("OpenBSD", "amd64"),
486+
("openbsd", arch) => ("OpenBSD", arch),
487+
("solaris", arch) => ("SunOS", arch),
488+
("tvos", arch) => ("tvOS", arch),
489+
("watchos", arch) => ("watchOS", arch),
490+
("windows", "x86_64") => ("Windows", "AMD64"),
491+
("windows", "i686") => ("Windows", "X86"),
492+
("windows", "aarch64") => ("Windows", "ARM64"),
493+
// Others
494+
(os, arch) => (os, arch),
495+
};
496+
self.define("CMAKE_SYSTEM_NAME", system_name);
497+
self.define("CMAKE_SYSTEM_PROCESSOR", system_processor);
498+
}
446499
}
447500
}
448501

@@ -561,9 +614,6 @@ impl Config {
561614
// variables which will hopefully get things to succeed. Some
562615
// systems may need the `windres` or `dlltool` variables set, so
563616
// set them if possible.
564-
if !self.defined("CMAKE_SYSTEM_NAME") {
565-
cmd.arg("-DCMAKE_SYSTEM_NAME=Windows");
566-
}
567617
if !self.defined("CMAKE_RC_COMPILER") {
568618
let exe = find_exe(c_compiler.path());
569619
if let Some(name) = exe.file_name().unwrap().to_str() {
@@ -614,14 +664,6 @@ impl Config {
614664
panic!("unsupported msvc target: {}", target);
615665
}
616666
}
617-
} else if target.contains("redox") {
618-
if !self.defined("CMAKE_SYSTEM_NAME") {
619-
cmd.arg("-DCMAKE_SYSTEM_NAME=Generic");
620-
}
621-
} else if target.contains("solaris") {
622-
if !self.defined("CMAKE_SYSTEM_NAME") {
623-
cmd.arg("-DCMAKE_SYSTEM_NAME=SunOS");
624-
}
625667
} else if target.contains("apple-ios") || target.contains("apple-tvos") {
626668
// These two flags prevent CMake from adding an OSX sysroot, which messes up compilation.
627669
if !self.defined("CMAKE_OSX_SYSROOT") && !self.defined("CMAKE_OSX_DEPLOYMENT_TARGET") {

test-crate/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/target
2+
/Cargo.lock

test-crate/Cargo.toml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
name = "test-crate"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
libz-sys = { version = "1.1.8", default-features = false, features = ["zlib-ng"] }
10+
11+
[patch.crates-io]
12+
cmake = { path = ".." }

test-crate/src/lib.rs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#[cfg(test)]
2+
mod tests {
3+
use libz_sys::zlibVersion;
4+
use std::ffi::CStr;
5+
6+
#[test]
7+
fn zlib_version() {
8+
let ver = unsafe { zlibVersion() };
9+
let ver_cstr = unsafe { CStr::from_ptr(ver) };
10+
let version = ver_cstr.to_str().unwrap();
11+
assert!(!version.is_empty());
12+
}
13+
}

0 commit comments

Comments
 (0)