diff --git a/.github/workflows/rust-tests.yml b/.github/workflows/rust-tests.yml index ae93dd043..02666fe48 100644 --- a/.github/workflows/rust-tests.yml +++ b/.github/workflows/rust-tests.yml @@ -12,9 +12,16 @@ concurrency: jobs: rust-tests: - runs-on: ${{ matrix.os }} name: ${{ matrix.os }} / Rust ${{ matrix.rust-version }}${{ matrix.extra-name }} + runs-on: ${{ matrix.os }} container: ${{ matrix.container }} + defaults: + run: + shell: "bash" + env: + CMAKE_CXX_COMPILER: ${{ matrix.cxx }} + CMAKE_C_COMPILER: ${{ matrix.cc }} + CMAKE_GENERATOR: ${{ matrix.cmake_generator }} strategy: matrix: include: @@ -23,6 +30,9 @@ jobs: rust-version: stable rust-target: x86_64-unknown-linux-gnu extra-name: ", no features" + cxx: g++ + cc: gcc + cmake_generator: Unix Makefiles # test with all features (i.e static build + ndarray) - os: ubuntu-20.04 @@ -31,6 +41,9 @@ jobs: cargo-test-flags: --release --all-features do-valgrind: true extra-name: ", all features, release" + cxx: g++ + cc: gcc + cmake_generator: Unix Makefiles # check the build on a stock Ubuntu 20.04, which uses cmake 3.16, and # with our minimal supported rust version @@ -40,24 +53,36 @@ jobs: rust-target: x86_64-unknown-linux-gnu cargo-build-flags: --features=rayon extra-name: ", cmake 3.16" + cxx: g++ + cc: gcc + cmake_generator: Unix Makefiles - os: macos-14 rust-version: stable rust-target: aarch64-apple-darwin cargo-test-flags: --features=rayon extra-name: "" + cxx: clang++ + cc: clang + cmake_generator: Unix Makefiles - os: windows-2019 rust-version: stable rust-target: x86_64-pc-windows-msvc cargo-build-flags: --features=rayon extra-name: " / MSVC" + cxx: cl.exe + cc: cl.exe + cmake_generator: Visual Studio 16 2019 - os: windows-2019 rust-version: stable rust-target: x86_64-pc-windows-gnu cargo-build-flags: --features=rayon extra-name: " / MinGW" + cxx: g++.exe + cc: gcc.exe + cmake_generator: MinGW Makefiles steps: - name: install dependencies in container if: matrix.container == 'ubuntu:20.04' @@ -99,7 +124,18 @@ jobs: echo "CMAKE_CXX_COMPILER_LAUNCHER=sccache" >> $GITHUB_ENV - name: run tests - run: cargo test --package metatensor --package metatensor-core --target ${{ matrix.rust-target }} ${{ matrix.cargo-build-flags }} + run: | + echo $CMAKE_CXX_COMPILER + echo $CMAKE_C_COMPILER + echo $CMAKE_GENERATOR + cargo test --package metatensor --package metatensor-core --target ${{ matrix.rust-target }} ${{ matrix.cargo-build-flags }} + + - name: debug + if: always() + run: | + ls target/*/tmp/cxx-install/cmake-subdirectory/metatensor/metatensor-core/ + ls target/*/tmp/cxx-install/cmake-subdirectory/metatensor/metatensor-core/target + ls target/*/tmp/cxx-install/cmake-subdirectory/metatensor/metatensor-core/target/debug/ - name: check the code can be compiled as a standalone crate if: matrix.os != 'windows-2019' diff --git a/metatensor-core/CMakeLists.txt b/metatensor-core/CMakeLists.txt index e4d409eeb..a918d0fda 100644 --- a/metatensor-core/CMakeLists.txt +++ b/metatensor-core/CMakeLists.txt @@ -146,14 +146,42 @@ endif() set(CARGO_TARGET_DIR ${CMAKE_CURRENT_BINARY_DIR}/target) set(CARGO_BUILD_ARG "${CARGO_BUILD_ARG};--target-dir=${CARGO_TARGET_DIR}") -# Handle cross compilation with RUST_BUILD_TARGET -if ("${RUST_BUILD_TARGET}" STREQUAL "") - if (CARGO_VERSION_RAW MATCHES "host: ([a-zA-Z0-9_\\-]*)\n") - set(RUST_HOST_TARGET "${CMAKE_MATCH_1}") +if (CARGO_VERSION_RAW MATCHES "host: ([a-zA-Z0-9_\\-]*)\n") + set(RUST_HOST_TARGET "${CMAKE_MATCH_1}") + if (RUST_HOST_TARGET MATCHES "([a-zA-Z0-9_]*)\\-") + set(RUST_HOST_ARCH "${CMAKE_MATCH_1}") else() - message(FATAL_ERROR "failed to determine host target, output was: ${CARGO_VERSION_RAW}") + message(FATAL_ERROR "failed to determine host CPU arch, target was: ${RUST_HOST_TARGET}") + endif() +else() + message(FATAL_ERROR "failed to determine host target, output was: ${CARGO_VERSION_RAW}") +endif() + +if (WIN32) + # on Windows, we need to use the same ABI in both CMake and cargo. If the + # user did not explicitly request a target, we can try to set it ourself, + # otherwise we just check that it matches what we expect. + if (MSVC) + if ("${RUST_BUILD_TARGET}" STREQUAL "") + set(RUST_BUILD_TARGET "${RUST_HOST_ARCH}-pc-windows-msvc") + message(STATUS "Setting rust target to ${RUST_BUILD_TARGET}") + elseif(NOT "${RUST_BUILD_TARGET}" MATCHES "-pc-windows-msvc") + message(FATAL_ERROR "CMake is building with MSVC but the Rust target is ${RUST_BUILD_TARGET}") + endif() endif() + if (MINGW) + if ("${RUST_BUILD_TARGET}" STREQUAL "") + set(RUST_BUILD_TARGET "${RUST_HOST_ARCH}-pc-windows-gnu") + message(STATUS "Setting rust target to ${RUST_BUILD_TARGET}") + elseif(NOT "${RUST_BUILD_TARGET}" MATCHES "-pc-windows-gnu") + message(FATAL_ERROR "CMake is building with MinGW but the Rust target is ${RUST_BUILD_TARGET}") + endif() + endif() +endif() + +# Handle cross compilation with RUST_BUILD_TARGET +if ("${RUST_BUILD_TARGET}" STREQUAL "") if (${METATENSOR_MAIN_PROJECT}) message(STATUS "Compiling to host (${RUST_HOST_TARGET})") endif() @@ -169,7 +197,6 @@ else() set(CARGO_OUTPUT_DIR "${CARGO_TARGET_DIR}/${RUST_BUILD_TARGET}/${CARGO_BUILD_TYPE}") endif() - # Get the list of libraries linked by default by cargo/rustc to add when linking # to metatensor::static if (CARGO_VERSION_CHANGED) diff --git a/metatensor-core/tests/check-cxx-install.rs b/metatensor-core/tests/check-cxx-install.rs index 553ca091d..1459717ef 100644 --- a/metatensor-core/tests/check-cxx-install.rs +++ b/metatensor-core/tests/check-cxx-install.rs @@ -69,7 +69,6 @@ fn check_cxx_install() { fn check_cmake_subdirectory() { let _guard = LOCK.lock().expect("mutex was poisoned"); - // Same test as above, but building metatensor in the same CMake project as // the test executable const CARGO_TARGET_TMPDIR: &str = env!("CARGO_TARGET_TMPDIR");