Skip to content

Commit 2200e13

Browse files
committed
cmake: refactored scripts with samples building:
- allow installing samples sources on all platforms even if BUILD_EXAMPLES is disabled, fixed minor issues in sources installation process - use 'example_<group>_<name>' scheme for target and binary file naming - use single function for sample executable creation
1 parent 633b0e5 commit 2200e13

File tree

15 files changed

+348
-578
lines changed

15 files changed

+348
-578
lines changed

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ else()
420420
ocv_update(OPENCV_LIB_INSTALL_PATH "${OpenCV_INSTALL_BINARIES_PREFIX}lib${LIB_SUFFIX}")
421421
endif()
422422
ocv_update(OPENCV_3P_LIB_INSTALL_PATH "${OpenCV_INSTALL_BINARIES_PREFIX}staticlib${LIB_SUFFIX}")
423-
ocv_update(OPENCV_SAMPLES_SRC_INSTALL_PATH samples/native)
423+
ocv_update(OPENCV_SAMPLES_SRC_INSTALL_PATH samples)
424424
ocv_update(OPENCV_JAR_INSTALL_PATH java)
425425
ocv_update(OPENCV_OTHER_INSTALL_PATH etc)
426426
ocv_update(OPENCV_CONFIG_INSTALL_PATH ".")
@@ -804,7 +804,7 @@ if(BUILD_opencv_apps)
804804
endif()
805805

806806
# examples
807-
if(BUILD_EXAMPLES OR BUILD_ANDROID_EXAMPLES OR INSTALL_PYTHON_EXAMPLES)
807+
if(BUILD_EXAMPLES OR BUILD_ANDROID_EXAMPLES OR INSTALL_PYTHON_EXAMPLES OR INSTALL_C_EXAMPLES)
808808
add_subdirectory(samples)
809809
endif()
810810

cmake/OpenCVModule.cmake

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,9 +1221,13 @@ function(ocv_add_samples)
12211221
ocv_debug_message("ocv_add_samples(" ${ARGN} ")")
12221222

12231223
set(samples_path "${CMAKE_CURRENT_SOURCE_DIR}/samples")
1224+
if(NOT EXISTS "${samples_path}")
1225+
return()
1226+
endif()
1227+
12241228
string(REGEX REPLACE "^opencv_" "" module_id ${the_module})
12251229

1226-
if(BUILD_EXAMPLES AND EXISTS "${samples_path}")
1230+
if(BUILD_EXAMPLES)
12271231
set(samples_deps ${the_module} ${OPENCV_MODULE_${the_module}_DEPS} opencv_imgcodecs opencv_videoio opencv_highgui ${ARGN})
12281232
ocv_check_dependencies(${samples_deps})
12291233

@@ -1237,15 +1241,14 @@ function(ocv_add_samples)
12371241
ocv_add_executable(${the_target} "${source}")
12381242
ocv_target_include_modules(${the_target} ${samples_deps})
12391243
ocv_target_link_libraries(${the_target} LINK_PRIVATE ${samples_deps})
1240-
set_target_properties(${the_target} PROPERTIES PROJECT_LABEL "(sample) ${name}")
1241-
1242-
set_target_properties(${the_target} PROPERTIES LABELS "${OPENCV_MODULE_${the_module}_LABEL};Sample")
1243-
set_source_files_properties("${source}"
1244-
PROPERTIES LABELS "${OPENCV_MODULE_${the_module}_LABEL};Sample")
12451244

1245+
set_target_properties(${the_target} PROPERTIES
1246+
PROJECT_LABEL "(sample) ${name}"
1247+
LABELS "${OPENCV_MODULE_${the_module}_LABEL};Sample")
1248+
set_source_files_properties("${source}" PROPERTIES
1249+
LABELS "${OPENCV_MODULE_${the_module}_LABEL};Sample")
12461250
if(ENABLE_SOLUTION_FOLDERS)
12471251
set_target_properties(${the_target} PROPERTIES
1248-
OUTPUT_NAME "${module_id}-example-${name}"
12491252
FOLDER "samples/${module_id}")
12501253
endif()
12511254

@@ -1256,8 +1259,8 @@ function(ocv_add_samples)
12561259
endif()
12571260
endif()
12581261

1259-
if(INSTALL_C_EXAMPLES AND NOT WIN32 AND EXISTS "${samples_path}")
1260-
file(GLOB DEPLOY_FILES_AND_DIRS "${samples_path}/*")
1262+
if(INSTALL_C_EXAMPLES)
1263+
file(GLOB DEPLOY_FILES_AND_DIRS "${samples_path}/*")
12611264
foreach(ITEM ${DEPLOY_FILES_AND_DIRS})
12621265
IF( IS_DIRECTORY "${ITEM}" )
12631266
LIST( APPEND sample_dirs "${ITEM}" )
@@ -1266,10 +1269,10 @@ function(ocv_add_samples)
12661269
ENDIF()
12671270
endforeach()
12681271
install(FILES ${sample_files}
1269-
DESTINATION ${OPENCV_SAMPLES_SRC_INSTALL_PATH}/${module_id}
1270-
PERMISSIONS OWNER_READ GROUP_READ WORLD_READ COMPONENT samples)
1272+
DESTINATION "${OPENCV_SAMPLES_SRC_INSTALL_PATH}/${module_id}"
1273+
COMPONENT samples)
12711274
install(DIRECTORY ${sample_dirs}
1272-
DESTINATION ${OPENCV_SAMPLES_SRC_INSTALL_PATH}/${module_id}
1273-
USE_SOURCE_PERMISSIONS COMPONENT samples)
1275+
DESTINATION "${OPENCV_SAMPLES_SRC_INSTALL_PATH}/${module_id}"
1276+
COMPONENT samples)
12741277
endif()
12751278
endfunction()

samples/CMakeLists.txt

Lines changed: 63 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,95 @@
1-
# Detect if we want to build samples with library binaries or not
1+
# Utility function: adds sample executable target with name "example_<group>_<file_name>"
2+
# Usage:
3+
# ocv_define_sample(<output target> <relative filename> <group>)
4+
function(ocv_define_sample out_target source sub)
5+
get_filename_component(name "${source}" NAME_WE)
6+
set(the_target "example_${sub}_${name}")
7+
add_executable(${the_target} "${source}")
8+
set_target_properties(${the_target} PROPERTIES PROJECT_LABEL "(sample) ${name}")
9+
if(ENABLE_SOLUTION_FOLDERS)
10+
set_target_properties(${the_target} PROPERTIES FOLDER "samples/${sub}")
11+
endif()
12+
if(WIN32 AND MSVC AND NOT BUILD_SHARED_LIBS)
13+
set_target_properties(${the_target} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:atlthunk.lib /NODEFAULTLIB:atlsd.lib /DEBUG")
14+
endif()
15+
if(WIN32)
16+
install(TARGETS ${the_target} RUNTIME DESTINATION "samples/${sub}" COMPONENT samples)
17+
endif()
18+
set(${out_target} ${the_target} PARENT_SCOPE)
19+
endfunction()
20+
221
if(NOT CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_LIST_DIR)
22+
#===================================================================================================
323
#
4-
# BUILD CASE 1: Build samples with library sources
24+
# Build as part of OpenCV
525
#
6-
7-
8-
# ----------------------------------------------------------------------------
9-
# CMake file for samples. See root CMakeLists.txt
10-
#
11-
# ----------------------------------------------------------------------------
26+
#===================================================================================================
27+
28+
function(ocv_install_example_src relpath)
29+
if(INSTALL_C_EXAMPLES)
30+
file(GLOB files ${ARGN})
31+
install(FILES ${files}
32+
DESTINATION "${OPENCV_SAMPLES_SRC_INSTALL_PATH}/${relpath}"
33+
COMPONENT samples)
34+
endif()
35+
endfunction()
1236

1337
add_subdirectory(cpp)
1438
add_subdirectory(java/tutorial_code)
1539
add_subdirectory(dnn)
1640
add_subdirectory(gpu)
1741
add_subdirectory(tapi)
18-
42+
add_subdirectory(opencl)
1943
if(WIN32 AND HAVE_DIRECTX)
2044
add_subdirectory(directx)
2145
endif()
22-
2346
if((NOT ANDROID) AND HAVE_OPENGL)
2447
add_subdirectory(opengl)
2548
endif()
26-
2749
if(HAVE_OPENVX)
2850
add_subdirectory(openvx)
2951
endif()
30-
3152
if(UNIX AND NOT ANDROID AND (HAVE_VA OR HAVE_VA_INTEL))
3253
add_subdirectory(va_intel)
3354
endif()
34-
3555
if(ANDROID AND BUILD_ANDROID_EXAMPLES)
3656
add_subdirectory(android)
3757
endif()
38-
3958
if(INSTALL_PYTHON_EXAMPLES)
4059
add_subdirectory(python)
4160
endif()
4261

43-
#
44-
# END OF BUILD CASE 1: Build samples with library sources
45-
#
62+
ocv_install_example_src("." CMakeLists.txt)
63+
if(INSTALL_C_EXAMPLES)
64+
install(DIRECTORY data
65+
DESTINATION "${OPENCV_SAMPLES_SRC_INSTALL_PATH}/data"
66+
COMPONENT samples)
67+
endif()
68+
4669
else()
70+
#===================================================================================================
4771
#
48-
# BUILD CASE 2: Build samples with library binaries
72+
# Standalone mode
4973
#
74+
#===================================================================================================
5075
cmake_minimum_required(VERSION 2.8)
5176

5277
project(samples C CXX)
5378
option(BUILD_EXAMPLES "Build samples" ON)
5479

55-
find_package(OpenCV REQUIRED)
80+
# Assuming following installation folder structure (default for UNIX):
81+
# <install_root>/share/
82+
# └── OpenCV/ <-- OPENCV_CONFIG_INSTALL_PATH
83+
# ├── OpenCVConfig.cmake <-- file to be found by find_package
84+
# ├── ...
85+
# ├── samples/ <-- OPENCV_SAMPLES_SRC_INSTALL_PATH
86+
# │   ├── CMakeLists.txt <-- this file
87+
# │   ├── cpp/
88+
find_package(OpenCV REQUIRED PATHS "..")
89+
90+
function(ocv_install_example_src)
91+
# not used in this branch
92+
endfunction()
5693

5794
if(MSVC)
5895
if(NOT ENABLE_BUILD_HARDENING)
@@ -80,16 +117,15 @@ if(MSVC)
80117
endif()
81118

82119
add_subdirectory(cpp)
83-
add_subdirectory(dnn)
84-
# FIXIT: can't use cvconfig.h in samples: add_subdirectory(gpu)
85-
86-
add_subdirectory(opencl)
87-
88120
if(WIN32)
89121
add_subdirectory(directx)
90122
endif()
123+
add_subdirectory(dnn)
124+
# add_subdirectory(gpu)
125+
add_subdirectory(opencl)
126+
# add_subdirectory(opengl)
127+
# add_subdirectory(openvx)
128+
add_subdirectory(tapi)
129+
# add_subdirectory(va_intel)
91130

92-
#
93-
# END OF BUILD CASE 2: Build samples with library binaries
94-
#
95131
endif()

samples/cpp/CMakeLists.txt

Lines changed: 56 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1,117 +1,65 @@
1-
# ----------------------------------------------------------------------------
2-
# CMake file for C samples. See root CMakeLists.txt
3-
#
4-
# ----------------------------------------------------------------------------
5-
6-
SET(OPENCV_CPP_SAMPLES_REQUIRED_DEPS opencv_core opencv_imgproc opencv_flann
7-
opencv_imgcodecs opencv_videoio opencv_highgui opencv_ml opencv_video
8-
opencv_objdetect opencv_photo opencv_features2d opencv_calib3d
9-
opencv_stitching opencv_videostab opencv_shape ${OPENCV_MODULES_PUBLIC} ${OpenCV_LIB_COMPONENTS})
10-
1+
ocv_install_example_src(cpp *.cpp *.hpp CMakeLists.txt)
2+
3+
set(OPENCV_CPP_SAMPLES_REQUIRED_DEPS
4+
opencv_core
5+
opencv_imgproc
6+
opencv_flann
7+
opencv_imgcodecs
8+
opencv_videoio
9+
opencv_highgui
10+
opencv_ml
11+
opencv_video
12+
opencv_objdetect
13+
opencv_photo
14+
opencv_features2d
15+
opencv_calib3d
16+
opencv_stitching
17+
opencv_videostab
18+
opencv_shape
19+
${OPENCV_MODULES_PUBLIC}
20+
${OpenCV_LIB_COMPONENTS})
1121
ocv_check_dependencies(${OPENCV_CPP_SAMPLES_REQUIRED_DEPS})
1222

23+
if(NOT BUILD_EXAMPLES OR NOT OCV_DEPENDENCIES_FOUND)
24+
return()
25+
endif()
1326

14-
if(BUILD_EXAMPLES AND OCV_DEPENDENCIES_FOUND)
15-
project(cpp_samples)
16-
17-
ocv_include_directories("${OpenCV_SOURCE_DIR}/include")#for opencv.hpp
18-
ocv_include_modules_recurse(${OPENCV_CPP_SAMPLES_REQUIRED_DEPS})
19-
20-
if(HAVE_opencv_cudaoptflow)
21-
ocv_include_directories("${OpenCV_SOURCE_DIR}/modules/cudaoptflow/include")
27+
project(cpp_samples)
28+
ocv_include_modules_recurse(${OPENCV_CPP_SAMPLES_REQUIRED_DEPS})
29+
file(GLOB_RECURSE cpp_samples RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp)
30+
if(NOT HAVE_OPENGL)
31+
ocv_list_filterout(cpp_samples Qt_sample)
32+
endif()
33+
if(NOT HAVE_opencv_cudaarithm OR NOT HAVE_opencv_cudafilters)
34+
ocv_list_filterout(cpp_samples "/gpu/")
35+
endif()
36+
if(NOT TARGET opencv_viz)
37+
ocv_list_filterout(cpp_samples "/viz/")
38+
endif()
39+
if(NOT HAVE_IPP_A)
40+
ocv_list_filterout(cpp_samples "/ippasync/")
41+
endif()
42+
ocv_list_filterout(cpp_samples "real_time_pose_estimation/")
43+
foreach(sample_filename ${cpp_samples})
44+
if(sample_filename MATCHES "viz/" AND VTK_USE_FILE)
45+
include(${VTK_USE_FILE})
2246
endif()
23-
if(HAVE_opencv_cudaimgproc)
24-
ocv_include_directories("${OpenCV_SOURCE_DIR}/modules/cudaimgproc/include")
47+
set(package "cpp")
48+
if(sample_filename MATCHES "tutorial_code")
49+
set(package "tutorial")
2550
endif()
26-
if(HAVE_opencv_cudaarithm)
27-
ocv_include_directories("${OpenCV_SOURCE_DIR}/modules/cudaarithm/include")
51+
ocv_define_sample(tgt ${sample_filename} ${package})
52+
ocv_target_link_libraries(${tgt} ${OPENCV_LINKER_LIBS} ${OPENCV_CPP_SAMPLES_REQUIRED_DEPS})
53+
if(sample_filename MATCHES "gpu/" AND HAVE_opencv_cudaarithm AND HAVE_opencv_cuda_filters)
54+
ocv_target_link_libraries(${tgt} opencv_cudaarithm opencv_cudafilters)
2855
endif()
29-
if(HAVE_opencv_cudafilters)
30-
ocv_include_directories("${OpenCV_SOURCE_DIR}/modules/cudafilters/include")
56+
if(sample_filename MATCHES "viz/" AND VTK_USE_FILE)
57+
ocv_target_link_libraries(${tgt} ${VTK_LIBRARIES})
58+
target_compile_definitions(${tgt} PRIVATE -DUSE_VTK)
3159
endif()
32-
33-
if(CMAKE_COMPILER_IS_GNUCXX AND NOT ENABLE_NOISY_WARNINGS)
34-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-function -Wno-missing-declarations")
35-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-function -Wno-missing-declarations")
60+
if(HAVE_OPENGL AND sample_filename MATCHES "detect_mser")
61+
target_compile_definitions(${tgt} PRIVATE HAVE_OPENGL)
3662
endif()
63+
endforeach()
3764

38-
# ---------------------------------------------
39-
# Define executable targets
40-
# ---------------------------------------------
41-
MACRO(OPENCV_DEFINE_CPP_EXAMPLE name srcs)
42-
43-
if("${srcs}" MATCHES "tutorial_code")
44-
set(sample_kind tutorial)
45-
set(sample_KIND TUTORIAL)
46-
set(sample_subfolder "tutorials")
47-
else()
48-
set(sample_kind example)
49-
set(sample_KIND EXAMPLE)
50-
set(sample_subfolder "cpp")
51-
endif()
52-
53-
set(the_target "${sample_kind}_${name}")
54-
add_executable(${the_target} ${srcs})
55-
ocv_target_link_libraries(${the_target} ${OPENCV_LINKER_LIBS} ${OPENCV_CPP_SAMPLES_REQUIRED_DEPS})
56-
57-
if("${srcs}" MATCHES "gpu/")
58-
ocv_target_link_libraries(${the_target} opencv_cudaarithm opencv_cudafilters)
59-
endif()
60-
61-
if("${srcs}" MATCHES "viz/" AND VTK_USE_FILE)
62-
include(${VTK_USE_FILE})
63-
ocv_target_link_libraries(${the_target} ${VTK_LIBRARIES})
64-
add_definitions(-DUSE_VTK)
65-
endif()
66-
67-
set_target_properties(${the_target} PROPERTIES
68-
OUTPUT_NAME "cpp-${sample_kind}-${name}"
69-
PROJECT_LABEL "(${sample_KIND}) ${name}")
70-
71-
if(ENABLE_SOLUTION_FOLDERS)
72-
set_target_properties(${the_target} PROPERTIES FOLDER "samples/${sample_subfolder}")
73-
endif()
74-
75-
if(WIN32)
76-
if (MSVC AND NOT BUILD_SHARED_LIBS)
77-
set_target_properties(${the_target} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:atlthunk.lib /NODEFAULTLIB:atlsd.lib /DEBUG")
78-
endif()
79-
install(TARGETS ${the_target}
80-
RUNTIME DESTINATION "${OPENCV_SAMPLES_BIN_INSTALL_PATH}/${sample_subfolder}" COMPONENT samples)
81-
endif()
82-
ENDMACRO()
83-
84-
file(GLOB_RECURSE cpp_samples RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp)
85-
86-
if(NOT HAVE_OPENGL)
87-
ocv_list_filterout(cpp_samples Qt_sample)
88-
endif()
89-
90-
if(NOT HAVE_opencv_cudaarithm OR NOT HAVE_opencv_cudafilters)
91-
ocv_list_filterout(cpp_samples "/gpu/")
92-
endif()
93-
94-
if(NOT TARGET opencv_viz)
95-
ocv_list_filterout(cpp_samples "/viz/")
96-
endif()
97-
98-
if(NOT HAVE_IPP_A)
99-
ocv_list_filterout(cpp_samples "/ippasync/")
100-
endif()
101-
102-
foreach(sample_filename ${cpp_samples})
103-
if(NOT "${sample_filename}" MATCHES "real_time_pose_estimation/")
104-
get_filename_component(sample ${sample_filename} NAME_WE)
105-
OPENCV_DEFINE_CPP_EXAMPLE(${sample} ${sample_filename})
106-
endif()
107-
endforeach()
108-
109-
include("tutorial_code/calib3d/real_time_pose_estimation/CMakeLists.txt")
110-
endif()
111-
112-
if(INSTALL_C_EXAMPLES AND NOT WIN32)
113-
file(GLOB C_SAMPLES *.c *.cpp *.jpg *.png *.data makefile.* build_all.sh *.dsp *.cmd )
114-
install(FILES ${C_SAMPLES}
115-
DESTINATION ${OPENCV_SAMPLES_SRC_INSTALL_PATH}/cpp
116-
PERMISSIONS OWNER_READ GROUP_READ WORLD_READ COMPONENT samples)
117-
endif()
65+
include("tutorial_code/calib3d/real_time_pose_estimation/CMakeLists.txt" OPTIONAL)

0 commit comments

Comments
 (0)