Skip to content

Commit

Permalink
Refactor detecting required link libraries
Browse files Browse the repository at this point in the history
Do not rely on cargo new generating a valid Cargo.toml or valid package
and instead ship everything we need ourselves.
  • Loading branch information
jschwe committed Jul 16, 2023
1 parent 1912677 commit 3ff339a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 21 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ project(Corrosion
# tagged release. Users don't need to care about this, it is mainly to
# clearly see in configure logs which version was used, without needing to
# rely on `git`, since Corrosion may be installed or otherwise packaged.
VERSION 0.4.2
VERSION 0.4.2.1
LANGUAGES NONE
HOMEPAGE_URL "https://corrosion-rs.github.io/corrosion/"
)
Expand Down
27 changes: 7 additions & 20 deletions cmake/FindRust.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -130,28 +130,15 @@ function(_corrosion_parse_target_triple target_triple out_arch out_vendor out_os
endfunction()

function(_corrosion_determine_libs_new target_triple out_libs)
set(package_dir "${CMAKE_BINARY_DIR}/corrosion/required_libs")
# Cleanup on reconfigure to get a cleans state (in case we change something in the future)
file(REMOVE_RECURSE "${CMAKE_BINARY_DIR}/corrosion/required_libs")
file(MAKE_DIRECTORY "${CMAKE_BINARY_DIR}/corrosion")
# Create a staticlib application for testing purposes
execute_process(
COMMAND "${Rust_CARGO_CACHED}" new --lib required_libs
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/corrosion"
RESULT_VARIABLE cargo_new_result
ERROR_QUIET
)
if(cargo_new_result)
file(REMOVE_RECURSE "${CMAKE_BINARY_DIR}/corrosion/required_libs")
message(DEBUG "Determining required link libraries: failed to create test library: ${cargo_new_result}")
return()
endif()
# Cargo new may create a Cargo.toml which attempts to reference keys from an outside workspace,
# but our package is not part of that workspace, so we need to overwrite the Cargo.toml with
# our own.
set(manifest_path "${CMAKE_BINARY_DIR}/corrosion/required_libs/Cargo.toml")
file(REMOVE_RECURSE "${package_dir}")
file(MAKE_DIRECTORY "${package_dir}")
set(manifest "[package]\nname = \"required_libs\"\nedition = \"2018\"\nversion = \"0.1.0\"\n")
string(APPEND manifest "\n[lib]\ncrate-type=[\"staticlib\"]\n[workspace]\n")
file(WRITE "${manifest_path}" "${manifest}")
string(APPEND manifest "\n[lib]\ncrate-type=[\"staticlib\"]\npath = \"lib.rs\"\n")
string(APPEND manifest "\n[workspace]\n")
file(WRITE "${package_dir}/Cargo.toml" "${manifest}")
file(WRITE "${package_dir}/lib.rs" "pub fn add(left: usize, right: usize) -> usize {left + right}\n")

execute_process(
COMMAND ${CMAKE_COMMAND} -E env
Expand Down

0 comments on commit 3ff339a

Please sign in to comment.