From 6d6537cf110ded38688f75ed3a437dd487d9af8e Mon Sep 17 00:00:00 2001 From: Emily Date: Mon, 22 Jul 2024 11:28:08 +0100 Subject: [PATCH] [IMPROVEMENT] Use Corrosion to build Rust code --- docs/CHANGES.TXT | 1 + src/CMakeLists.txt | 8 ++--- src/rust/CMakeLists.txt | 71 ++++++++++++++++++----------------------- 3 files changed, 36 insertions(+), 44 deletions(-) diff --git a/docs/CHANGES.TXT b/docs/CHANGES.TXT index bf6b6503a..5adce2595 100644 --- a/docs/CHANGES.TXT +++ b/docs/CHANGES.TXT @@ -31,6 +31,7 @@ - Cleanup: Remove the (unmaintained) Nuklear GUI code - Cleanup: Reduce the amount of Windows build options in the project file - Fix: infinite loop in MP4 file type detector. +- Improvement: Use Corrosion to build Rust code 0.94 (2021-12-14) ----------------- diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b1ae689b4..3852799b4 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,6 +1,8 @@ -cmake_minimum_required (VERSION 3.0.2) +cmake_minimum_required (VERSION 3.24.0) project (CCExtractor) +include (CTest) + option (WITH_FFMPEG "Build using FFmpeg demuxer and decoder" OFF) option (WITH_OCR "Build with OCR (Optical Character Recognition) feature" OFF) option (WITH_SHARING "Build with sharing and translation support" OFF) @@ -255,9 +257,7 @@ add_executable (ccextractor ${SOURCEFILE} ${FREETYPE_SOURCE} ${UTF8PROC_SOURCE}) if (PKG_CONFIG_FOUND AND NOT WITHOUT_RUST) add_subdirectory (rust) - get_target_property(RUST_LIB ccx_rust LOCATION) - set (EXTRA_LIBS ${EXTRA_LIBS} ${RUST_LIB}) - add_dependencies(ccextractor ccx_rust) + set (EXTRA_LIBS ${EXTRA_LIBS} ccx_rust) else () set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DDISABLE_RUST") endif (PKG_CONFIG_FOUND AND NOT WITHOUT_RUST) diff --git a/src/rust/CMakeLists.txt b/src/rust/CMakeLists.txt index 7f9c11378..595a3f9c1 100644 --- a/src/rust/CMakeLists.txt +++ b/src/rust/CMakeLists.txt @@ -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 $ test + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} +)