Skip to content

Commit 4b59730

Browse files
emilazyIshanGrover2004
authored andcommitted
[IMPROVEMENT] Use Corrosion to build Rust code (#1630)
1 parent 24f7ec8 commit 4b59730

File tree

3 files changed

+36
-44
lines changed

3 files changed

+36
-44
lines changed

docs/CHANGES.TXT

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
- Cleanup: Remove the (unmaintained) Nuklear GUI code
3434
- Cleanup: Reduce the amount of Windows build options in the project file
3535
- Fix: infinite loop in MP4 file type detector.
36+
- Improvement: Use Corrosion to build Rust code
3637
- Improvement: Ignore MXF Caption Essence Container version byte to enhance SRT subtitle extraction compatibility
3738

3839
0.94 (2021-12-14)

src/CMakeLists.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
cmake_minimum_required (VERSION 3.0.2)
1+
cmake_minimum_required (VERSION 3.24.0)
22
project (CCExtractor)
33

4+
include (CTest)
5+
46
option (WITH_FFMPEG "Build using FFmpeg demuxer and decoder" OFF)
57
option (WITH_OCR "Build with OCR (Optical Character Recognition) feature" OFF)
68
option (WITH_SHARING "Build with sharing and translation support" OFF)
@@ -255,9 +257,7 @@ add_executable (ccextractor ${SOURCEFILE} ${FREETYPE_SOURCE} ${UTF8PROC_SOURCE})
255257

256258
if (PKG_CONFIG_FOUND AND NOT WITHOUT_RUST)
257259
add_subdirectory (rust)
258-
get_target_property(RUST_LIB ccx_rust LOCATION)
259-
set (EXTRA_LIBS ${EXTRA_LIBS} ${RUST_LIB})
260-
add_dependencies(ccextractor ccx_rust)
260+
set (EXTRA_LIBS ${EXTRA_LIBS} ccx_rust)
261261
else ()
262262
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DDISABLE_RUST")
263263
endif (PKG_CONFIG_FOUND AND NOT WITHOUT_RUST)

src/rust/CMakeLists.txt

Lines changed: 31 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,40 @@
1-
if (WITH_OCR AND WITH_HARDSUBX)
2-
set(FEATURE_ARG "\"hardsubx_ocr\"")
3-
set(FEATURE_FLAG "--features")
4-
else ()
5-
set(FEATURE_ARG "")
6-
set(FEATURE_FLAG "")
7-
endif ()
1+
include(FetchContent)
2+
FetchContent_Declare(
3+
Corrosion
4+
GIT_REPOSITORY https://github.com/corrosion-rs/corrosion.git
5+
GIT_TAG 64289b1d79d6d19cd2e241db515381a086bb8407 # v0.5.0
6+
FIND_PACKAGE_ARGS
7+
)
8+
FetchContent_MakeAvailable(Corrosion)
89

9-
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
10-
set(CARGO_CMD cargo build ${FEATURE_FLAG} ${FEATURE_ARG})
11-
set(TARGET_DIR "debug")
12-
else ()
13-
set(CARGO_CMD cargo build ${FEATURE_FLAG} ${FEATURE_ARG} --release)
14-
set(TARGET_DIR "release")
15-
endif ()
10+
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
11+
set(PROFILE "debug")
12+
else()
13+
set(PROFILE "release")
14+
endif()
15+
16+
if(WITH_OCR AND WITH_HARDSUBX)
17+
set(FEATURES "hardsubx_ocr")
18+
else()
19+
set(FEATURES "")
20+
endif()
1621

1722
# Check rust version
1823
set(MSRV "1.54.0")
19-
execute_process(COMMAND rustc --version
20-
OUTPUT_VARIABLE Rust_Version)
21-
string(REGEX MATCH "[0-9]+.[0-9]+.[0-9]+" Rust_Version_string ${Rust_Version})
22-
message(STATUS "Detected rustc version ${Rust_Version_string}")
23-
if (Rust_Version_string VERSION_GREATER_EQUAL ${MSRV})
24+
if(Rust_VERSION VERSION_GREATER_EQUAL ${MSRV})
2425
message(STATUS "rustc >= MSRV(${MSRV})")
2526
else()
2627
message(FATAL_ERROR "Minimum supported rust version(MSRV) is ${MSRV}, please upgrade rust")
27-
endif(Rust_Version_string VERSION_GREATER_EQUAL ${MSRV})
28+
endif(Rust_VERSION VERSION_GREATER_EQUAL ${MSRV})
2829

29-
if(WIN32)
30-
set(CCX_RUST_SO "${CMAKE_CURRENT_BINARY_DIR}/${TARGET_DIR}/libccx_rust.lib")
31-
message("${CCX_RUST_SO}")
32-
add_custom_target(ccx_rust ALL
33-
COMMENT "Compiling ccx_rust module"
34-
COMMAND set CARGO_TARGET_DIR=${CMAKE_CURRENT_BINARY_DIR} ${CARGO_CMD}
35-
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
36-
set_target_properties(ccx_rust PROPERTIES LOCATION ${CCX_RUST_SO})
37-
else()
38-
set(CCX_RUST_SO "${CMAKE_CURRENT_BINARY_DIR}/${TARGET_DIR}/libccx_rust.a")
39-
add_custom_target(ccx_rust ALL
40-
COMMENT "Compiling ccx_rust module"
41-
COMMAND CARGO_TARGET_DIR=${CMAKE_CURRENT_BINARY_DIR} ${CARGO_CMD}
42-
COMMAND cp ${CCX_RUST_SO} ${CMAKE_CURRENT_BINARY_DIR}
43-
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
44-
set_target_properties(ccx_rust PROPERTIES LOCATION ${CCX_RUST_SO})
45-
endif()
30+
corrosion_import_crate(
31+
MANIFEST_PATH Cargo.toml
32+
PROFILE ${PROFILE}
33+
FEATURES ${FEATURES}
34+
)
4635

47-
add_test(NAME ccx_rust_test
48-
COMMAND cargo test
49-
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
36+
add_test(
37+
NAME ccx_rust_test
38+
COMMAND $<TARGET_FILE:Rust::Cargo> test
39+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
40+
)

0 commit comments

Comments
 (0)