Skip to content

Commit

Permalink
Move rustup target checking and installation to FindRust
Browse files Browse the repository at this point in the history
Not sure why it was added to corrosion
instead of FindRust in the first place
  • Loading branch information
jschwe committed Sep 6, 2024
1 parent 3400338 commit 3db4ffd
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 45 deletions.
45 changes: 0 additions & 45 deletions cmake/Corrosion.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -31,53 +31,8 @@ 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)
execute_process(COMMAND rustup target list --toolchain "${Rust_TOOLCHAIN}"
OUTPUT_VARIABLE AVAILABLE_TARGETS_RAW
)
string(REPLACE "\n" ";" AVAILABLE_TARGETS_RAW "${AVAILABLE_TARGETS_RAW}")
string(REPLACE " (installed)" "" "AVAILABLE_TARGETS" "${AVAILABLE_TARGETS_RAW}")
set(INSTALLED_TARGETS_RAW "${AVAILABLE_TARGETS_RAW}")
list(FILTER INSTALLED_TARGETS_RAW INCLUDE REGEX " \\(installed\\)")
string(REPLACE " (installed)" "" "INSTALLED_TARGETS" "${INSTALLED_TARGETS_RAW}")
list(TRANSFORM INSTALLED_TARGETS STRIP)
if("${Rust_CARGO_TARGET}" IN_LIST AVAILABLE_TARGETS)
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))
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"
AND (NOT CMAKE_VS_PLATFORM_NAME STREQUAL CMAKE_VS_PLATFORM_NAME_DEFAULT)
AND Rust_VERSION VERSION_LESS "1.54")
Expand Down
46 changes: 46 additions & 0 deletions cmake/FindRust.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ concrete Rust version, not a rustup proxy.

cmake_minimum_required(VERSION 3.12)

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

# search for Cargo here and set up a bunch of cool flags and stuff
include(FindPackageHandleStandardArgs)

Expand Down Expand Up @@ -761,6 +767,46 @@ if (NOT Rust_CARGO_TARGET_CACHED)
message(STATUS "Rust Target: ${Rust_CARGO_TARGET_CACHED}")
endif()


if(Rust_TOOLCHAIN_IS_RUSTUP_MANAGED)
execute_process(COMMAND rustup target list --toolchain "${Rust_TOOLCHAIN}"
OUTPUT_VARIABLE AVAILABLE_TARGETS_RAW
)
string(REPLACE "\n" ";" AVAILABLE_TARGETS_RAW "${AVAILABLE_TARGETS_RAW}")
string(REPLACE " (installed)" "" "AVAILABLE_TARGETS" "${AVAILABLE_TARGETS_RAW}")
set(INSTALLED_TARGETS_RAW "${AVAILABLE_TARGETS_RAW}")
list(FILTER INSTALLED_TARGETS_RAW INCLUDE REGEX " \\(installed\\)")
string(REPLACE " (installed)" "" "INSTALLED_TARGETS" "${INSTALLED_TARGETS_RAW}")
list(TRANSFORM INSTALLED_TARGETS STRIP)
if("${Rust_CARGO_TARGET_CACHED}" IN_LIST AVAILABLE_TARGETS)
message(DEBUG "Cargo target ${Rust_CARGO_TARGET} is an official target-triple")
message(DEBUG "Installed targets: ${INSTALLED_TARGETS}")
if(NOT ("${Rust_CARGO_TARGET_CACHED}" IN_LIST INSTALLED_TARGETS))
if(Rust_RUSTUP_INSTALL_MISSING_TARGET)
message(STATUS "Cargo target ${Rust_CARGO_TARGET_CACHED} is not installed. Installing via rustup.")
execute_process(COMMAND "${Rust_RUSTUP}" target add
--toolchain ${Rust_TOOLCHAIN}
${Rust_CARGO_TARGET_CACHED}
RESULT_VARIABLE target_add_result
)
if(NOT "${target_add_result}" EQUAL "0")
message(FATAL_ERROR "Target ${Rust_CARGO_TARGET_CACHED} 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_CACHED} 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(Rust_CARGO_TARGET_CACHED STREQUAL Rust_DEFAULT_HOST_TARGET)
set(Rust_CROSSCOMPILING FALSE CACHE INTERNAL "Rust is configured for cross-compiling")
else()
Expand Down

0 comments on commit 3db4ffd

Please sign in to comment.