Skip to content

Commit

Permalink
Guard CORROSION_TOOLS_RUST_TOOLCHAIN behind rustup check
Browse files Browse the repository at this point in the history
If the toolchain is not managed by rustup,
then resolving the toolchain would fail,
hence we instead ignore the option
and fallback to the default toolchain.
Also simplify the check for cargo / rustc.
We check once in FindRust, then we
can later just check if the variable evaulates to true or not.
  • Loading branch information
jschwe committed Dec 29, 2024
1 parent a88bc9b commit 6be991b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 42 deletions.
89 changes: 48 additions & 41 deletions cmake/Corrosion.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,52 @@ get_property(
TARGET Rust::Cargo PROPERTY IMPORTED_LOCATION
)

set(corrosion_tools_rust_toolchain_docstring "Rust toolchain to use for building helper tools such as cbindgen or cxx-bridge")
if(DEFINED CORROSION_TOOLS_RUST_TOOLCHAIN)
set(cor_default_tools_toolchain "${CORROSION_TOOLS_RUST_TOOLCHAIN}")
else()
set(cor_default_tools_toolchain "${Rust_TOOLCHAIN}")
endif()
set(CORROSION_TOOLS_RUST_TOOLCHAIN "${cor_default_tools_toolchain}" CACHE STRING "${corrosion_tools_rust_toolchain_docstring}" FORCE)
set_property(CACHE CORROSION_TOOLS_RUST_TOOLCHAIN PROPERTY STRINGS "${Rust_RUSTUP_TOOLCHAINS}")

if(NOT "$CACHE{CORROSION_TOOLS_RUST_TOOLCHAIN}" IN_LIST Rust_RUSTUP_TOOLCHAINS)
if("$CACHE{CORROSION_TOOLS_RUST_TOOLCHAIN}-${Rust_CARGO_HOST_TARGET}" IN_LIST Rust_RUSTUP_TOOLCHAINS)
set(CORROSION_TOOLS_RUST_TOOLCHAIN "$CACHE{CORROSION_TOOLS_RUST_TOOLCHAIN}-${Rust_CARGO_HOST_TARGET}"
CACHE PATH "${corrosion_tools_rust_toolchain_docstring}" FORCE)
if(Rust_TOOLCHAIN_IS_RUSTUP_MANAGED AND DEFINED Rust_RUSTUP_TOOLCHAINS)
set(corrosion_tools_rust_toolchain_docstring "Rust toolchain to use for building helper tools such as cbindgen or cxx-bridge")
if(DEFINED CORROSION_TOOLS_RUST_TOOLCHAIN)
set(cor_default_tools_toolchain "${CORROSION_TOOLS_RUST_TOOLCHAIN}")
else()
message(FATAL_ERROR "CORROSION_TOOLS_RUST_TOOLCHAIN must be set to a valid rustup managed toolchain path."
"You can select a valid toolchain "
set(cor_default_tools_toolchain "${Rust_TOOLCHAIN}")
endif()
set(CORROSION_TOOLS_RUST_TOOLCHAIN "${cor_default_tools_toolchain}" CACHE STRING
"${corrosion_tools_rust_toolchain_docstring}" FORCE)
set_property(CACHE CORROSION_TOOLS_RUST_TOOLCHAIN PROPERTY STRINGS "${Rust_RUSTUP_TOOLCHAINS}")
if(NOT "$CACHE{CORROSION_TOOLS_RUST_TOOLCHAIN}" IN_LIST Rust_RUSTUP_TOOLCHAINS)
if("$CACHE{CORROSION_TOOLS_RUST_TOOLCHAIN}-${Rust_CARGO_HOST_TARGET}" IN_LIST Rust_RUSTUP_TOOLCHAINS)
set(CORROSION_TOOLS_RUST_TOOLCHAIN "$CACHE{CORROSION_TOOLS_RUST_TOOLCHAIN}-${Rust_CARGO_HOST_TARGET}"
CACHE PATH "${corrosion_tools_rust_toolchain_docstring}" FORCE)
else()
message(FATAL_ERROR "CORROSION_TOOLS_RUST_TOOLCHAIN must be set to a valid rustup managed toolchain path."
"Rust_RUSTUP_TOOLCHAINS contains a list of valid installed toolchains."
)
endif()
endif()
foreach(toolchain tc_rustc tc_cargo IN ZIP_LISTS Rust_RUSTUP_TOOLCHAINS Rust_RUSTUP_TOOLCHAINS_RUSTC_PATH Rust_RUSTUP_TOOLCHAINS_CARGO_PATH)
if("${toolchain}" STREQUAL $CACHE{CORROSION_TOOLS_RUST_TOOLCHAIN})
# Minimum CMake version 3.29 for `IS_EXECUTABLE`.
if(NOT (tc_cargo AND tc_rustc ))
message(FATAL_ERROR "Failed to find executable rustc or cargo for toolchain `$CACHE{CORROSION_TOOLS_RUST_TOOLCHAIN}`")
endif()
set(CORROSION_TOOLS_RUSTC "${tc_rustc}" CACHE INTERNAL "" FORCE)
set(CORROSION_TOOLS_CARGO "${tc_cargo}" CACHE INTERNAL "" FORCE)
break()
endif()
endforeach()
if(NOT DEFINED CACHE{CORROSION_TOOLS_CARGO})
message(FATAL_ERROR "Internal error: Failed to find toolchain $CACHE{CORROSION_TOOLS_RUST_TOOLCHAIN} in "
"list of rustup managed toolchains: ${Rust_RUSTUP_TOOLCHAINS}"
)
endif()
else()
# Fallback to the default project toolchain if rust is not rustup managed.
if(DEFINED CORROSION_TOOLS_RUST_TOOLCHAIN)
message(DEBUG "Ignoring `CORROSION_TOOLS_RUST_TOOLCHAIN=${CORROSION_TOOLS_RUST_TOOLCHAIN}` "
"since the toolchains are not rustup managed. Falling back to the default rust toolchain"
" for this project."
)
endif()
set(CORROSION_TOOLS_RUSTC "${RUSTC_EXECUTABLE}" CACHE INTERNAL "" FORCE)
set(CORROSION_TOOLS_CARGO "${CARGO_EXECUTABLE}" CACHE INTERNAL "" FORCE)
endif()

function(_corrosion_bin_target_suffix target_name out_var_suffix)
Expand Down Expand Up @@ -1555,25 +1583,7 @@ set_target_properties(${INSTALL_TARGET}-shared
endif()
endfunction()

# Helper function which places the cargo and rustc exectuble paths into the variable names specified by
# `out_cargo` and `out_rustc` based on the value of the CORROSION_TOOLS_RUST_TOOLCHAIN cache variable.
function(_corrosion_get_tools_rust_toolchain out_cargo out_rustc)
foreach(toolchain tc_rustc tc_cargo IN ZIP_LISTS Rust_RUSTUP_TOOLCHAINS Rust_RUSTUP_TOOLCHAINS_RUSTC_PATH Rust_RUSTUP_TOOLCHAINS_CARGO_PATH)
if("${toolchain}" STREQUAL $CACHE{CORROSION_TOOLS_RUST_TOOLCHAIN})
# Minimum CMake version 3.29 for `IS_EXECUTABLE`.
if(NOT (EXISTS "${tc_cargo}" AND EXISTS "${tc_rustc}" ))
message(FATAL_ERROR "Failed to find executable rustc or cargo for toolchain `$CACHE{CORROSION_TOOLS_RUST_TOOLCHAIN}`")
endif()
set("${out_cargo}" "${tc_cargo}" PARENT_SCOPE)
set( "${out_rustc}" "${tc_rustc}" PARENT_SCOPE)
return()
endif()
endforeach()

message(FATAL_ERROR "Internal error: Failed to find toolchain $CACHE{CORROSION_TOOLS_RUST_TOOLCHAIN} in "
"list of rustup managed toolchains: ${Rust_RUSTUP_TOOLCHAINS}"
)
endfunction()

#[=======================================================================[.md:
** EXPERIMENTAL **: This function is currently still considered experimental
Expand Down Expand Up @@ -1701,14 +1711,14 @@ function(corrosion_add_cxxbridge cxx_target)
if(Rust_CARGO_HOST_OS STREQUAL "windows")
set(executable_postfix ".exe")
endif()
_corrosion_get_tools_rust_toolchain(builder_cargo builder_rustc)

add_custom_command(OUTPUT "${CMAKE_BINARY_DIR}/corrosion/cxxbridge_v${cxx_required_version}/bin/cxxbridge${executable_postfix}"
COMMAND
${CMAKE_COMMAND} -E make_directory "${CMAKE_BINARY_DIR}/corrosion/cxxbridge_v${cxx_required_version}"
COMMAND
${CMAKE_COMMAND} -E env
"CARGO_BUILD_RUSTC=${builder_rustc}"
${builder_cargo} install
"CARGO_BUILD_RUSTC=$CACHE{CORROSION_TOOLS_RUSTC}"
$CACHE{CORROSION_TOOLS_CARGO} install
cxxbridge-cmd
--version "${cxx_required_version}"
--locked
Expand Down Expand Up @@ -1940,15 +1950,12 @@ function(corrosion_experimental_cbindgen)

if(NOT TARGET "_corrosion_cbindgen")
file(MAKE_DIRECTORY "${local_cbindgen_install_dir}")
unset(cbindgen_cargo)
unset(cbindgen_rustc)
_corrosion_get_tools_rust_toolchain(cbindgen_cargo cbindgen_rustc)

add_custom_command(OUTPUT "${cbindgen}"
COMMAND ${CMAKE_COMMAND}
-E env
"CARGO_BUILD_RUSTC=${cbindgen_rustc}"
${cbindgen_cargo} install
"CARGO_BUILD_RUSTC=$CACHE{CORROSION_TOOLS_RUSTC}"
$CACHE{CORROSION_TOOLS_CARGO} install
cbindgen
--locked
--root "${local_cbindgen_install_dir}"
Expand Down
6 changes: 5 additions & 1 deletion cmake/FindRust.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,11 @@ if (Rust_RESOLVE_RUSTUP_TOOLCHAINS)
else()
set(_TOOLCHAIN_${_TOOLCHAIN}_IS_NIGHTLY "FALSE")
endif()
if(EXISTS "${_TOOLCHAIN_PATH}/bin/cargo")
set(_suffix "")
if(CMAKE_HOST_WIN32)
set(_suffix ".exe")
endif()
if(EXISTS "${_TOOLCHAIN_PATH}/bin/cargo${_suffix}")
list(APPEND _DISCOVERED_TOOLCHAINS_CARGO_PATH "${_TOOLCHAIN_PATH}/bin/cargo")
else()
list(APPEND _DISCOVERED_TOOLCHAINS_CARGO_PATH "NOTFOUND")
Expand Down

0 comments on commit 6be991b

Please sign in to comment.