From 9d7d499a793022ff2dc2a9f136cad474b233427e Mon Sep 17 00:00:00 2001 From: Jonathan Schwender Date: Thu, 25 Apr 2024 19:15:23 +0200 Subject: [PATCH 1/5] Detect OpenHarmony Rust target The OpenHarmony SDK contains a CMake toolchain file, which among others sets CMAKE_OHOS_ARCH_ABI. We can use this to automatically infer the correct Rust target. The SDK can be obtained from the release notes on gitee: https://gitee.com/openharmony/docs/tree/master/en/release-notes --- cmake/FindRust.cmake | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/cmake/FindRust.cmake b/cmake/FindRust.cmake index 97c65dbc..c6479c15 100644 --- a/cmake/FindRust.cmake +++ b/cmake/FindRust.cmake @@ -713,6 +713,19 @@ if (NOT Rust_CARGO_TARGET_CACHED) if (_Rust_ANDROID_TARGET) set(Rust_CARGO_TARGET_CACHED "${_Rust_ANDROID_TARGET}" CACHE STRING "Target triple") endif() + elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "OHOS") + if(CMAKE_OHOS_ARCH_ABI STREQUAL arm64-v8a) + set(_RUST_OHOS_TARGET aarch64-unknown-linux-ohos) + elseif(CMAKE_OHOS_ARCH_ABI STREQUAL armeabi-v7a) + set(_RUST_OHOS_TARGET armv7-unknown-linux-ohos) + elseif(CMAKE_OHOS_ARCH_ABI STREQUAL x86_64) + set(_RUST_OHOS_TARGET x86_64-unknown-linux-ohos) + else() + message(WARNING "unrecognized OHOS architecture: ${OHOS_ARCH}") + endif() + if(_RUST_OHOS_TARGET) + set(Rust_CARGO_TARGET_CACHED "${_RUST_OHOS_TARGET}" CACHE STRING "Target triple") + endif() endif() # Fallback to the default host target if(NOT Rust_CARGO_TARGET_CACHED) From 6e9505c5c71c5923ff97476f1344e96927e4c04d Mon Sep 17 00:00:00 2001 From: Jonathan Schwender Date: Thu, 25 Apr 2024 19:35:30 +0200 Subject: [PATCH 2/5] CI: Adjust presets for new M1 runner macos-latest now maps to macos-14, which is an M1 mac. Adjust the aarch64 file to not enable CMake cross-compiling mode, and add a toolchain file for x86_64 apple. --- .../scripts/toolchains/aarch64-apple-darwin-clang.cmake | 2 -- .github/scripts/toolchains/x86_64-apple-darwin-clang.cmake | 7 +++++++ CMakePresets.json | 3 ++- 3 files changed, 9 insertions(+), 3 deletions(-) create mode 100644 .github/scripts/toolchains/x86_64-apple-darwin-clang.cmake diff --git a/.github/scripts/toolchains/aarch64-apple-darwin-clang.cmake b/.github/scripts/toolchains/aarch64-apple-darwin-clang.cmake index f66343d0..51fc2e24 100644 --- a/.github/scripts/toolchains/aarch64-apple-darwin-clang.cmake +++ b/.github/scripts/toolchains/aarch64-apple-darwin-clang.cmake @@ -2,6 +2,4 @@ set(CMAKE_C_COMPILER "clang") set(CMAKE_CXX_COMPILER "clang++") set(CMAKE_C_COMPILER_TARGET "aarch64-apple-darwin") set(CMAKE_CXX_COMPILER_TARGET "aarch64-apple-darwin") -set(CMAKE_SYSTEM_NAME "Darwin") -set(CMAKE_SYSTEM_VERSION ${CMAKE_HOST_SYSTEM_VERSION}) set(CMAKE_OSX_ARCHITECTURES "arm64" CACHE STRING "") diff --git a/.github/scripts/toolchains/x86_64-apple-darwin-clang.cmake b/.github/scripts/toolchains/x86_64-apple-darwin-clang.cmake new file mode 100644 index 00000000..582131e4 --- /dev/null +++ b/.github/scripts/toolchains/x86_64-apple-darwin-clang.cmake @@ -0,0 +1,7 @@ +set(CMAKE_C_COMPILER "clang") +set(CMAKE_CXX_COMPILER "clang++") +set(CMAKE_C_COMPILER_TARGET "x86_64-apple-darwin") +set(CMAKE_CXX_COMPILER_TARGET "x86_64-apple-darwin") +set(CMAKE_SYSTEM_NAME "Darwin") +set(CMAKE_SYSTEM_VERSION ${CMAKE_HOST_SYSTEM_VERSION}) +set(CMAKE_OSX_ARCHITECTURES "x86_64" CACHE STRING "") diff --git a/CMakePresets.json b/CMakePresets.json index d014dad8..d51bec75 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -304,7 +304,8 @@ }, { "name": "x86_64-apple-darwin-clang", - "inherits": ["x86_64-apple-darwin", "clang"] + "inherits": ["x86_64-apple-darwin", "clang"], + "toolchainFile": "${sourceDir}/.github/scripts/toolchains/${presetName}.cmake" }, { "name": "aarch64-apple-darwin-clang", From ffd53c5a9d3bfd0c06319ae1dd6c8f1a8cf87d00 Mon Sep 17 00:00:00 2001 From: Jonathan Schwender Date: Tue, 5 Mar 2024 07:29:43 +0100 Subject: [PATCH 3/5] Fix Ninja Multi-Config Cross-Config support The default output directory was the same for all `$`s, which caused conflicting build rules. The issue probably still exists for the rare case when: 1. The user specified an `OUTPUT_DIRECTORY` property AND 2. did not specify an `OUTPUT_DIRECTORY_$` property AND 3. The value of the `OUTPUT_DIRECTORY` does not contain a genex, depending on the `$`. Corrosion could detect and append a `$` in this case, but I think leaving this up to the user is reasonable for now. --- cmake/Corrosion.cmake | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmake/Corrosion.cmake b/cmake/Corrosion.cmake index fa58d7b8..4dc8c4c6 100644 --- a/cmake/Corrosion.cmake +++ b/cmake/Corrosion.cmake @@ -125,7 +125,7 @@ function(_corrosion_set_imported_location_deferred target_name base_property out elseif(output_directory) set(curr_out_dir "${output_directory}") else() - set(curr_out_dir "${CMAKE_CURRENT_BINARY_DIR}") + set(curr_out_dir "${CMAKE_CURRENT_BINARY_DIR}/$") endif() string(REPLACE "\$" "${config_type}" curr_out_dir "${curr_out_dir}") message(DEBUG "Setting ${base_property}_${config_type_upper} for target ${target_name}" @@ -235,7 +235,7 @@ function(_corrosion_copy_byproduct_deferred target_name output_dir_prop_names ca # Fallback to the default directory. We do not append the configuration directory here # and instead let CMake do this, since otherwise the resolving of dynamic library # imported paths may fail. - set(curr_out_dir "${CMAKE_CURRENT_BINARY_DIR}") + set(curr_out_dir "${CMAKE_CURRENT_BINARY_DIR}/$") endif() set(multiconfig_out_dir_genex "${multiconfig_out_dir_genex}$<$:${curr_out_dir}>") endforeach() From fe023cf777352df2f2b9636b8808b140533a2561 Mon Sep 17 00:00:00 2001 From: Jonathan Schwender Date: Wed, 6 Mar 2024 18:21:35 +0100 Subject: [PATCH 4/5] Add Ninja Multi-Config Cross-Config test Add a test with CMAKE_CROSS_CONFIGS set --- .github/workflows/test.yaml | 22 ++++++++++++++++++++++ test/CMakeLists.txt | 6 ++++++ test/ConfigureAndBuild.cmake | 8 ++++++++ 3 files changed, 36 insertions(+) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 5201d930..3a351c02 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -251,6 +251,28 @@ jobs: working-directory: build run: ctest --output-on-failure --build-config Debug -j 3 + ninja_multi_config_cross_config: + name: Test Ninja Multi-Config Cross-Configs + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Install CMake + uses: corrosion-rs/install-cmake@v2 + with: + cmake: 3.22.6 + ninja: 1.10.0 + # Install cbindgen before Rust to use recent default Rust version. + - name: Install cbindgen + run: cargo install cbindgen + - name: Install Rust + id: install_rust + uses: dtolnay/rust-toolchain@stable + - name: Configure + run: cmake -S. -Bbuild -G "Ninja Multi-Config" -DCMAKE_CROSS_CONFIGS="Debug;Release" -DCMAKE_DEFAULT_CONFIGS="Debug;Release" + - name: Run Tests + working-directory: build + run: ctest --output-on-failure -j 3 --build-config Debug + test_cxxbridge: name: Test cxxbridge integration diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 1a42ba4d..eb08deb7 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -112,6 +112,12 @@ function(corrosion_tests_add_test test_name bin_names) if(CMAKE_TOOLCHAIN_FILE) list(APPEND configure_cmake_args TOOLCHAIN_FILE "${CMAKE_TOOLCHAIN_FILE}") endif() + if(CMAKE_CROSS_CONFIGS) + list(APPEND configure_cmake_args CMAKE_CROSS_CONFIGS "${CMAKE_CROSS_CONFIGS}") + endif() + if(CMAKE_DEFAULT_CONFIGS) + list(APPEND configure_cmake_args CMAKE_DEFAULT_CONFIGS "${CMAKE_DEFAULT_CONFIGS}") + endif() add_test(NAME "${test_name}_build" COMMAND diff --git a/test/ConfigureAndBuild.cmake b/test/ConfigureAndBuild.cmake index c3017074..4845731c 100644 --- a/test/ConfigureAndBuild.cmake +++ b/test/ConfigureAndBuild.cmake @@ -23,6 +23,8 @@ set(oneValueArgs CARGO_PROFILE OSX_ARCHITECTURES TOOLCHAIN_FILE + CMAKE_CROSS_CONFIGS + CMAKE_DEFAULT_CONFIGS ) set(multiValueArgs "PASS_THROUGH_ARGS") cmake_parse_arguments(TEST "${options}" "${oneValueArgs}" @@ -59,6 +61,12 @@ endif() if(TEST_TOOLCHAIN_FILE) list(APPEND configure_args "-DCMAKE_TOOLCHAIN_FILE=${TEST_TOOLCHAIN_FILE}") endif() +if(TEST_CMAKE_CROSS_CONFIGS) + list(APPEND configure_args "-DCMAKE_CROSS_CONFIGS=${TEST_CMAKE_CROSS_CONFIGS}") +endif() +if(TEST_CMAKE_DEFAULT_CONFIGS) + list(APPEND configure_args "-DCMAKE_DEFAULT_CONFIGS=${TEST_CMAKE_DEFAULT_CONFIGS}") +endif() if(TEST_CARGO_PROFILE) list(APPEND configure_args "-DCARGO_PROFILE=${TEST_CARGO_PROFILE}") endif() From 85a0bd0dc0b1924034f5e0024d3596305a863f97 Mon Sep 17 00:00:00 2001 From: Jonathan Schwender Date: Wed, 6 Mar 2024 19:40:00 +0100 Subject: [PATCH 5/5] bump cmake version for ninja MC test --- .github/workflows/test.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 3a351c02..0f1ea188 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -259,7 +259,7 @@ jobs: - name: Install CMake uses: corrosion-rs/install-cmake@v2 with: - cmake: 3.22.6 + cmake: 3.23.5 ninja: 1.10.0 # Install cbindgen before Rust to use recent default Rust version. - name: Install cbindgen