Skip to content

Commit

Permalink
add option to autoinstall cargo target
Browse files Browse the repository at this point in the history
  • Loading branch information
jschwe committed Sep 6, 2024
1 parent f3d0095 commit 3400338
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 6 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,30 @@ jobs:
working-directory: build
run: ctest --output-on-failure --build-config Debug -j 3 -R "^cxxbridge"

autoinstall_cargo_target:
name: Test Auto-installing Cargo target via rustup
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4
- name: Install CMake
uses: lukka/get-cmake@519de0c7b4812477d74976b2523a9417f552d126
- name: Install Rust
id: install_rust
uses: dtolnay/rust-toolchain@stable
- name: Install Cross Compiler
shell: bash
run: |
echo "::group::apt-install"
sudo apt-get update
sudo apt-get install -y gcc-aarch64-linux-gnu
echo "::endgroup::"
- name: Assert rustup target is not installed
run: rustup show | ( ! grep aarch64)
- name: Configure Corrosion
run: cmake -S. -Bbuild -GNinja -DRust_RUSTUP_INSTALL_MISSING_TARGET=ON --preset "aarch64-unknown-linux-gnu-gcc"
- name: Check rustup target is installed after configuring
run: rustup show | grep aarch64

install:
name: Test Corrosion as a Library
runs-on: ${{ matrix.os }}
Expand Down Expand Up @@ -410,6 +434,7 @@ jobs:
- linux_stage2
- darwin
- test_cxxbridge
- autoinstall_cargo_target
- install
runs-on: ubuntu-latest
# Step copied from: https://github.com/cross-rs/cross/blob/80c9f9109a719ffb0f694060ddc6e371d5b3a540/.github/workflows/ci.yml#L361
Expand Down
32 changes: 27 additions & 5 deletions cmake/Corrosion.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ option(
OFF
)

option(
Rust_RUSTUP_INSTALL_MISSING_TARGET
"Use Rustup to automatically install missing targets instead of giving up"
OFF
)

find_package(Rust REQUIRED)

if(Rust_TOOLCHAIN_IS_RUSTUP_MANAGED)
Expand All @@ -47,13 +53,29 @@ if(Rust_TOOLCHAIN_IS_RUSTUP_MANAGED)
message(DEBUG "Cargo target ${Rust_CARGO_TARGET} is an official target-triple")
message(DEBUG "Installed targets: ${INSTALLED_TARGETS}")
if(NOT ("${Rust_CARGO_TARGET}" IN_LIST INSTALLED_TARGETS))
message(FATAL_ERROR "Target ${Rust_CARGO_TARGET} is not installed for toolchain ${Rust_TOOLCHAIN}.\n"
"Help: Run `rustup target add --toolchain ${Rust_TOOLCHAIN} ${Rust_CARGO_TARGET}` to install "
"the missing target."
)
if(Rust_RUSTUP_INSTALL_MISSING_TARGET)
message(STATUS "Cargo target ${Rust_CARGO_TARGET} is not installed. Installing via rustup.")
execute_process(COMMAND "${Rust_RUSTUP}" target add
--toolchain ${Rust_TOOLCHAIN}
${Rust_CARGO_TARGET}
RESULT_VARIABLE target_add_result
)
if(NOT "${target_add_result}" EQUAL "0")
message(FATAL_ERROR "Target ${Rust_CARGO_TARGET} is not installed for toolchain "
"${Rust_TOOLCHAIN} and automatically installing failed with ${target_add_result}.\n"
"You can try to manually install by running\n"
"`rustup target add --toolchain ${Rust_TOOLCHAIN} ${Rust_CARGO_TARGET}`."
)
endif()
message(STATUS "Installed target ${Rust_CARGO_TARGET_CACHED} successfully.")
else()
message(FATAL_ERROR "Target ${Rust_CARGO_TARGET} is not installed for toolchain ${Rust_TOOLCHAIN}.\n"
"Help: Run `rustup target add --toolchain ${Rust_TOOLCHAIN} ${Rust_CARGO_TARGET}` to install "
"the missing target or configure corrosion with `Rust_RUSTUP_INSTALL_MISSING_TARGET=ON`."
)
endif()
endif()
endif()

endif()

if(CMAKE_GENERATOR MATCHES "Visual Studio"
Expand Down
10 changes: 9 additions & 1 deletion doc/src/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,10 @@ Some configuration options can be specified individually for each target. You ca


### Global Corrosion Options
All of the following variables are evaluated automatically in most cases. In typical cases you

#### Selecting the Rust toolchain and target triple

The following variables are evaluated automatically in most cases. In typical cases you
shouldn't need to alter any of these. If you do want to specify them manually, make sure to set
them **before** `find_package(Corrosion REQUIRED)`.

Expand All @@ -178,6 +181,11 @@ them **before** `find_package(Corrosion REQUIRED)`.
Default: On Visual Studio Generator, the matching triple for `CMAKE_VS_PLATFORM_NAME`. Otherwise,
the default target triple reported by `${Rust_COMPILER} --version --verbose`.

#### Enable Convenience Options

The following options are off by default, but may increase convenience:

- `Rust_RUSTUP_INSTALL_MISSING_TARGET:BOOL`: Automatically install a missing target via `rustup` instead of failing.


#### Developer/Maintainer Options
Expand Down

0 comments on commit 3400338

Please sign in to comment.