Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IMPROVEMENT] Use Corrosion to build Rust code #1630

Merged
merged 2 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/CHANGES.TXT
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,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
- Improvement: Ignore MXF Caption Essence Container version byte to enhance SRT subtitle extraction compatibility

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.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)
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
71 changes: 31 additions & 40 deletions src/rust/CMakeLists.txt
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}
)
Loading