-
Notifications
You must be signed in to change notification settings - Fork 447
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[IMPROVEMENT] Use Corrosion to build Rust code
- Loading branch information
Showing
3 changed files
with
36 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,40 @@ | ||
if (WITH_OCR AND WITH_HARDSUBX) | ||
set(FEATURE_ARG "\"hardsubx_ocr\"") | ||
set(FEATURE_FLAG "--features") | ||
else () | ||
set(FEATURE_ARG "") | ||
set(FEATURE_FLAG "") | ||
endif () | ||
include(FetchContent) | ||
FetchContent_Declare( | ||
Corrosion | ||
GIT_REPOSITORY https://github.com/corrosion-rs/corrosion.git | ||
GIT_TAG 64289b1d79d6d19cd2e241db515381a086bb8407 # v0.5.0 | ||
FIND_PACKAGE_ARGS | ||
) | ||
FetchContent_MakeAvailable(Corrosion) | ||
|
||
if (CMAKE_BUILD_TYPE STREQUAL "Debug") | ||
set(CARGO_CMD cargo build ${FEATURE_FLAG} ${FEATURE_ARG}) | ||
set(TARGET_DIR "debug") | ||
else () | ||
set(CARGO_CMD cargo build ${FEATURE_FLAG} ${FEATURE_ARG} --release) | ||
set(TARGET_DIR "release") | ||
endif () | ||
if(CMAKE_BUILD_TYPE STREQUAL "Debug") | ||
set(PROFILE "debug") | ||
else() | ||
set(PROFILE "release") | ||
endif() | ||
|
||
if(WITH_OCR AND WITH_HARDSUBX) | ||
set(FEATURES "hardsubx_ocr") | ||
else() | ||
set(FEATURES "") | ||
endif() | ||
|
||
# Check rust version | ||
set(MSRV "1.54.0") | ||
execute_process(COMMAND rustc --version | ||
OUTPUT_VARIABLE Rust_Version) | ||
string(REGEX MATCH "[0-9]+.[0-9]+.[0-9]+" Rust_Version_string ${Rust_Version}) | ||
message(STATUS "Detected rustc version ${Rust_Version_string}") | ||
if (Rust_Version_string VERSION_GREATER_EQUAL ${MSRV}) | ||
if(Rust_VERSION VERSION_GREATER_EQUAL ${MSRV}) | ||
message(STATUS "rustc >= MSRV(${MSRV})") | ||
else() | ||
message(FATAL_ERROR "Minimum supported rust version(MSRV) is ${MSRV}, please upgrade rust") | ||
endif(Rust_Version_string VERSION_GREATER_EQUAL ${MSRV}) | ||
endif(Rust_VERSION VERSION_GREATER_EQUAL ${MSRV}) | ||
|
||
if(WIN32) | ||
set(CCX_RUST_SO "${CMAKE_CURRENT_BINARY_DIR}/${TARGET_DIR}/libccx_rust.lib") | ||
message("${CCX_RUST_SO}") | ||
add_custom_target(ccx_rust ALL | ||
COMMENT "Compiling ccx_rust module" | ||
COMMAND set CARGO_TARGET_DIR=${CMAKE_CURRENT_BINARY_DIR} ${CARGO_CMD} | ||
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) | ||
set_target_properties(ccx_rust PROPERTIES LOCATION ${CCX_RUST_SO}) | ||
else() | ||
set(CCX_RUST_SO "${CMAKE_CURRENT_BINARY_DIR}/${TARGET_DIR}/libccx_rust.a") | ||
add_custom_target(ccx_rust ALL | ||
COMMENT "Compiling ccx_rust module" | ||
COMMAND CARGO_TARGET_DIR=${CMAKE_CURRENT_BINARY_DIR} ${CARGO_CMD} | ||
COMMAND cp ${CCX_RUST_SO} ${CMAKE_CURRENT_BINARY_DIR} | ||
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) | ||
set_target_properties(ccx_rust PROPERTIES LOCATION ${CCX_RUST_SO}) | ||
endif() | ||
corrosion_import_crate( | ||
MANIFEST_PATH Cargo.toml | ||
PROFILE ${PROFILE} | ||
FEATURES ${FEATURES} | ||
) | ||
|
||
add_test(NAME ccx_rust_test | ||
COMMAND cargo test | ||
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) | ||
add_test( | ||
NAME ccx_rust_test | ||
COMMAND $<TARGET_FILE:Rust::Cargo> test | ||
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} | ||
) |