From 3201ea89d6e95f1434de204a1d8a4818f26770cb Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Tue, 14 May 2019 15:46:44 +0300 Subject: [PATCH] cmake: enable threading in samples - moved code into separate file due issue with handling of CMake policies --- samples/CMakeLists.txt | 48 ++++++++++++++++++--------------------------- samples/samples_utils.cmake | 31 +++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 29 deletions(-) create mode 100644 samples/samples_utils.cmake diff --git a/samples/CMakeLists.txt b/samples/CMakeLists.txt index 5daa941..a7fd21e 100644 --- a/samples/CMakeLists.txt +++ b/samples/CMakeLists.txt @@ -1,32 +1,3 @@ -# Utility function: adds sample executable target with name "example__" -# Usage: -# ocv_define_sample( ) -function(ocv_define_sample out_target source sub) - get_filename_component(name "${source}" NAME_WE) - set(the_target "example_${sub}_${name}") - add_executable(${the_target} "${source}") - set_target_properties(${the_target} PROPERTIES PROJECT_LABEL "(sample) ${name}") - if(ENABLE_SOLUTION_FOLDERS) - set_target_properties(${the_target} PROPERTIES FOLDER "samples/${sub}") - endif() - if(WIN32 AND MSVC AND NOT BUILD_SHARED_LIBS) - set_target_properties(${the_target} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:atlthunk.lib /NODEFAULTLIB:atlsd.lib /DEBUG") - endif() - if(WIN32) - install(TARGETS ${the_target} RUNTIME DESTINATION "samples/${sub}" COMPONENT samples) - endif() - # Add single target to build all samples in the group: 'make opencv_samples_cpp' - set(parent_target opencv_samples_${sub}) - if(NOT TARGET ${parent_target}) - add_custom_target(${parent_target}) - if(TARGET opencv_samples) - add_dependencies(opencv_samples ${parent_target}) - endif() - endif() - add_dependencies(${parent_target} ${the_target}) - set(${out_target} ${the_target} PARENT_SCOPE) -endfunction() - if(NOT CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_LIST_DIR) #=================================================================================================== # @@ -34,6 +5,8 @@ if(NOT CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_LIST_DIR) # #=================================================================================================== +include("${CMAKE_CURRENT_LIST_DIR}/samples_utils.cmake") + function(ocv_install_example_src relpath) if(INSTALL_C_EXAMPLES) file(GLOB files ${ARGN}) @@ -43,6 +16,10 @@ function(ocv_install_example_src relpath) endif() endfunction() +if((TARGET Threads::Threads OR HAVE_PTHREAD OR MSVC OR APPLE) AND NOT OPENCV_EXAMPLES_DISABLE_THREADS) + add_definitions(-DHAVE_THREADS=1) +endif() + add_subdirectory(cpp) add_subdirectory(java/tutorial_code) add_subdirectory(dnn) @@ -94,6 +71,8 @@ option(BUILD_EXAMPLES "Build samples" ON) # │   ├── cpp/ find_package(OpenCV REQUIRED PATHS "..") +include("${CMAKE_CURRENT_LIST_DIR}/samples_utils.cmake") + function(ocv_install_example_src) # not used in this branch endfunction() @@ -125,6 +104,17 @@ endif() add_definitions(-DDISABLE_OPENCV_24_COMPATIBILITY=1) # Avoid C-like legacy API +if(OPENCV_EXAMPLES_DISABLE_THREADS) + # nothing +elseif(MSVC OR APPLE) + set(HAVE_THREADS 1) +else() + find_package(Threads) +endif() +if((TARGET Threads::Threads OR HAVE_THREADS) AND NOT OPENCV_EXAMPLES_DISABLE_THREADS) + add_definitions(-DHAVE_THREADS=1) +endif() + add_subdirectory(cpp) if(WIN32) add_subdirectory(directx) diff --git a/samples/samples_utils.cmake b/samples/samples_utils.cmake new file mode 100644 index 0000000..b39996a --- /dev/null +++ b/samples/samples_utils.cmake @@ -0,0 +1,31 @@ +# Utility function: adds sample executable target with name "example__" +# Usage: +# ocv_define_sample( ) +function(ocv_define_sample out_target source sub) + get_filename_component(name "${source}" NAME_WE) + set(the_target "example_${sub}_${name}") + add_executable(${the_target} "${source}") + if(TARGET Threads::Threads AND NOT OPENCV_EXAMPLES_DISABLE_THREADS) + target_link_libraries(${the_target} LINK_PRIVATE Threads::Threads) + endif() + set_target_properties(${the_target} PROPERTIES PROJECT_LABEL "(sample) ${name}") + if(ENABLE_SOLUTION_FOLDERS) + set_target_properties(${the_target} PROPERTIES FOLDER "samples/${sub}") + endif() + if(WIN32 AND MSVC AND NOT BUILD_SHARED_LIBS) + set_target_properties(${the_target} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:atlthunk.lib /NODEFAULTLIB:atlsd.lib /DEBUG") + endif() + if(WIN32) + install(TARGETS ${the_target} RUNTIME DESTINATION "samples/${sub}" COMPONENT samples) + endif() + # Add single target to build all samples in the group: 'make opencv_samples_cpp' + set(parent_target opencv_samples_${sub}) + if(NOT TARGET ${parent_target}) + add_custom_target(${parent_target}) + if(TARGET opencv_samples) + add_dependencies(opencv_samples ${parent_target}) + endif() + endif() + add_dependencies(${parent_target} ${the_target}) + set(${out_target} ${the_target} PARENT_SCOPE) +endfunction() -- 2.7.4