Skip to content

Commit

Permalink
[IMPROVEMENT] Use Corrosion to build Rust code
Browse files Browse the repository at this point in the history
  • Loading branch information
emilazy committed Aug 1, 2024
1 parent 34bb9dd commit de9cef5
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 44 deletions.
1 change: 1 addition & 0 deletions docs/CHANGES.TXT
Original file line number Diff line number Diff line change
Expand Up @@ -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)
-----------------
Expand Down
8 changes: 4 additions & 4 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
cmake_minimum_required (VERSION 3.0.2)
cmake_minimum_required (VERSION 3.15.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)
Expand Down Expand Up @@ -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)
Expand Down
74 changes: 34 additions & 40 deletions src/rust/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,49 +1,43 @@
if (WITH_OCR AND WITH_HARDSUBX)
set(FEATURE_ARG "\"hardsubx_ocr\"")
set(FEATURE_FLAG "--features")
else ()
set(FEATURE_ARG "")
set(FEATURE_FLAG "")
endif ()
find_package(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(NOT Corrosion_FOUND)
include(FetchContent)
FetchContent_Declare(
Corrosion
GIT_REPOSITORY https://github.com/corrosion-rs/corrosion.git
GIT_TAG v0.5
)
FetchContent_MakeAvailable(Corrosion)
endif(NOT Corrosion_FOUND)

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}
)

0 comments on commit de9cef5

Please sign in to comment.