diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index e5b2ffc..c42c813 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -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 @@ -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 }} diff --git a/src/lib.rs b/src/lib.rs index 3c6bfd9..63297c9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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); + } } } @@ -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() { @@ -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") { diff --git a/test-crate/.gitignore b/test-crate/.gitignore new file mode 100644 index 0000000..4fffb2f --- /dev/null +++ b/test-crate/.gitignore @@ -0,0 +1,2 @@ +/target +/Cargo.lock diff --git a/test-crate/Cargo.toml b/test-crate/Cargo.toml new file mode 100644 index 0000000..f3900ad --- /dev/null +++ b/test-crate/Cargo.toml @@ -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 = ".." } diff --git a/test-crate/src/lib.rs b/test-crate/src/lib.rs new file mode 100644 index 0000000..036604f --- /dev/null +++ b/test-crate/src/lib.rs @@ -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()); + } +}