Skip to content

Commit 5cfe52b

Browse files
committed
CMake: Add ENABLE_OPENCV option, use targets
1 parent 5b5763e commit 5cfe52b

File tree

5 files changed

+40
-77
lines changed

5 files changed

+40
-77
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ option(ENABLE_COVERAGE "Scan test coverage using gcov and report" OFF)
7979
option(ENABLE_DOCS "Build API documentation (requires Doxygen)" ON)
8080
option(APPIMAGE_BUILD "Build to install in an AppImage (Linux only)" OFF)
8181
option(ENABLE_MAGICK "Use ImageMagick, if available" ON)
82+
option(ENABLE_OPENCV "Build with OpenCV algorithms (requires Boost, Protobuf 3)" ON)
8283

8384
# Legacy commandline override
8485
if (DISABLE_TESTS)

bindings/python/CMakeLists.txt

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,6 @@ if (CMAKE_VERSION VERSION_LESS 3.12)
5454
"${PROJECT_BINARY_DIR}/src")
5555
endif()
5656

57-
################ OPENCV ##################
58-
find_package( OpenCV 4 )
59-
if (OpenCV_FOUND)
60-
message("\nCOMPILING WITH OPENCV\n")
61-
set(CMAKE_SWIG_FLAGS "-DUSE_OPENCV=1")
62-
add_definitions( -DUSE_OPENCV=1 )
63-
else()
64-
message("\nOPENCV NOT FOUND, SOME FUNCTIONALITIES WILL BE DISABLED\n")
65-
endif()
66-
6757
### Enable C++ in SWIG
6858
set_property(SOURCE openshot.i PROPERTY CPLUSPLUS ON)
6959
set_property(SOURCE openshot.i PROPERTY SWIG_MODULE_NAME openshot)

src/CMakeLists.txt

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -392,36 +392,39 @@ if (ENABLE_BLACKMAGIC)
392392
endif()
393393

394394
################## OPENCV ###################
395-
396-
find_package( OpenCV 4 )
397-
if (OpenCV_FOUND)
398-
message("\nCOMPILING WITH OPENCV\n")
399-
list(APPEND CMAKE_SWIG_FLAGS -DUSE_OPENCV=1)
400-
target_compile_definitions(openshot PUBLIC USE_OPENCV=1)
401-
else()
402-
message("\nOPENCV NOT FOUND, SOME FUNCTIONALITIES WILL BE DISABLED\n")
403-
endif()
404-
405-
################## PROTOBUF ##################
406-
if (OpenCV_FOUND)
407-
find_package(Protobuf 3)
408-
409-
if (NOT Protobuf_FOUND)
410-
# Protobuf is required when compiling with opencv
411-
message(FATAL_ERROR "\nPLEASE INSTALL PROTOBUF. Protobuf is required when compiling with opencv.\n")
395+
if(ENABLE_OPENCV)
396+
find_package(OpenCV 4)
397+
if(NOT OpenCV_FOUND)
398+
set(ENABLE_OPENCV FALSE CACHE BOOL
399+
"Build with OpenCV algorithms (requires Boost, Protobuf 3)" FORCE)
400+
else()
401+
find_package(Protobuf 3 REQUIRED)
402+
find_package(Boost REQUIRED)
403+
set(PROTOBUF_SOURCE_FILES
404+
protobuf_messages/objdetectdata.proto
405+
protobuf_messages/stabilizedata.proto
406+
protobuf_messages/trackerdata.proto
407+
)
408+
protobuf_generate_cpp(ProtoSources ProtoHeaders ${PROTOBUF_SOURCE_FILES})
409+
# Add OpenCV source files and generated protobuf sources
410+
target_sources(openshot PRIVATE
411+
${OPENSHOT_CV_SOURCES}
412+
${ProtoSources}
413+
${ProtoHeaders}
414+
)
415+
list(APPEND CMAKE_SWIG_FLAGS -DUSE_OPENCV=1)
416+
target_compile_definitions(openshot PUBLIC USE_OPENCV=1)
417+
target_link_libraries(openshot PUBLIC
418+
opencv_core
419+
opencv_video
420+
opencv_highgui
421+
opencv_dnn
422+
opencv_tracking
423+
protobuf::libprotobuf
424+
)
412425
endif()
413-
414-
add_subdirectory(protobuf_messages)
415-
416-
target_include_directories(openshot PUBLIC ${ProtobufMessagePath})
417-
418-
# Add OpenCV target sources
419-
target_sources(openshot PRIVATE
420-
${OPENSHOT_CV_SOURCES})
421-
422-
# Link libopenshot with OpenCV libs
423-
target_link_libraries(openshot PUBLIC ProtoMessages ${OpenCV_LIBS} ${PROTOBUF_LIBRARY} ${Protobuf_INCLUDE_DIRS})
424426
endif()
427+
add_feature_info("OpenCV algorithms" ENABLE_OPENCV "Use OpenCV algorithms")
425428

426429
############### LINK LIBRARY #################
427430
# Link remaining dependency libraries
@@ -450,6 +453,9 @@ install(
450453
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/libopenshot
451454
FILES_MATCHING PATTERN "*.h"
452455
)
456+
install(FILES ${ProtobufHeaders}
457+
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/libopenshot
458+
)
453459

454460
############### CPACK PACKAGING ##############
455461
if(MINGW)
@@ -464,5 +470,3 @@ endif()
464470
set(CPACK_DEBIAN_PACKAGE_MAINTAINER "Jonathan Thomas") #required
465471

466472
include(CPack)
467-
468-

src/protobuf_messages/compileProtobufMessages.sh

Lines changed: 0 additions & 6 deletions
This file was deleted.

tests/CMakeLists.txt

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -56,25 +56,6 @@ if(ENABLE_BLACKMAGIC)
5656
endif()
5757
endif()
5858

59-
####################### OPENCV ##########################
60-
find_package( OpenCV 4 )
61-
if (OpenCV_FOUND)
62-
add_definitions( -DUSE_OPENCV=1 )
63-
else()
64-
message("\nOPENCV NOT FOUND, OPENCV RELATED TESTS ARE DISABLED\n")
65-
endif()
66-
67-
################ PROTOBUF ##################
68-
if (OpenCV_FOUND)
69-
find_package(Protobuf 3)
70-
71-
if (NOT Protobuf_FOUND)
72-
# Protobuf is required when compiling with opencv
73-
message(FATAL_ERROR "\nPLEASE INSTALL PROTOBUF. Protobuf is required when compiling with opencv.\n")
74-
endif()
75-
include_directories(${PROTOBUF_INCLUDE_DIR})
76-
endif()
77-
7859
############### SET TEST SOURCE FILES #################
7960
set(OPENSHOT_TEST_FILES
8061
Cache_Tests.cpp
@@ -95,15 +76,11 @@ set(OPENSHOT_TEST_FILES
9576
Timeline_Tests.cpp)
9677

9778
########## SET OPENCV RELATED TEST FILES ###############
98-
if(OpenCV_FOUND)
99-
set(OPENSHOT_CV_TEST_FILES
79+
if(ENABLE_OPENCV)
80+
list(APPEND OPENSHOT_TEST_FILES
10081
CVTracker_Tests.cpp
10182
CVStabilizer_Tests.cpp
10283
# CVObjectDetection_Tests.cpp
103-
)
104-
set(OPENSHOT_CV_LIBRARIES
105-
${OpenCV_LIBS}
106-
${PROTOBUF_LIBRARY}
10784
)
10885
endif()
10986

@@ -113,21 +90,18 @@ message (STATUS "Tests enabled, test executable will be built as tests/openshot-
11390

11491
add_executable(openshot-test
11592
tests.cpp
116-
${OPENSHOT_TEST_FILES}
117-
${OPENSHOT_CV_TEST_FILES}
118-
)
93+
${OPENSHOT_TEST_FILES}
94+
)
11995

12096
# Link libraries to the new executable
12197
target_link_libraries(openshot-test
12298
openshot
12399
${UnitTest++_LIBRARIES}
124-
${OPENSHOT_CV_LIBRARIES}
125-
)
100+
)
126101

127102
##### RUNNING TESTS (make os_test / make test) #####
128103
# Hook up the 'make os_test' target to the 'openshot-test' executable,
129104
# if we aren't defining it as the coverage target
130105
if(NOT ENABLE_COVERAGE)
131106
add_custom_target(os_test COMMAND openshot-test)
132107
endif()
133-

0 commit comments

Comments
 (0)