Skip to content

Commit a91a11e

Browse files
committed
Merge pull request opencv#10818 from mshabunin:install-samples-src
2 parents 2423cdd + c630927 commit a91a11e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+482
-680
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/OpenCVExtraTargets.cmake

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,11 @@ if(BUILD_DOCS)
4343
add_custom_target(install_docs DEPENDS opencv_docs
4444
COMMAND "${CMAKE_COMMAND}" -DCMAKE_INSTALL_COMPONENT=docs -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
4545
endif()
46+
47+
# Samples
48+
if(BUILD_EXAMPLES)
49+
add_custom_target(opencv_samples)
50+
if(ENABLE_SOLUTION_FOLDERS)
51+
set_target_properties(opencv_samples PROPERTIES FOLDER "extra")
52+
endif()
53+
endif()

cmake/OpenCVModule.cmake

Lines changed: 23 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,17 +1241,23 @@ 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()
1254+
# Add single target to build all samples for the module: 'make opencv_samples_bioinspired'
1255+
set(parent_target opencv_samples_${module_id})
1256+
if(NOT TARGET ${parent_target})
1257+
add_custom_target(${parent_target})
1258+
add_dependencies(opencv_samples ${parent_target})
1259+
endif()
1260+
add_dependencies(${parent_target} ${the_target})
12511261

12521262
if(WIN32)
12531263
install(TARGETS ${the_target} RUNTIME DESTINATION "samples/${module_id}" COMPONENT samples)
@@ -1256,8 +1266,8 @@ function(ocv_add_samples)
12561266
endif()
12571267
endif()
12581268

1259-
if(INSTALL_C_EXAMPLES AND NOT WIN32 AND EXISTS "${samples_path}")
1260-
file(GLOB DEPLOY_FILES_AND_DIRS "${samples_path}/*")
1269+
if(INSTALL_C_EXAMPLES)
1270+
file(GLOB DEPLOY_FILES_AND_DIRS "${samples_path}/*")
12611271
foreach(ITEM ${DEPLOY_FILES_AND_DIRS})
12621272
IF( IS_DIRECTORY "${ITEM}" )
12631273
LIST( APPEND sample_dirs "${ITEM}" )
@@ -1266,10 +1276,10 @@ function(ocv_add_samples)
12661276
ENDIF()
12671277
endforeach()
12681278
install(FILES ${sample_files}
1269-
DESTINATION ${OPENCV_SAMPLES_SRC_INSTALL_PATH}/${module_id}
1270-
PERMISSIONS OWNER_READ GROUP_READ WORLD_READ COMPONENT samples)
1279+
DESTINATION "${OPENCV_SAMPLES_SRC_INSTALL_PATH}/${module_id}"
1280+
COMPONENT samples)
12711281
install(DIRECTORY ${sample_dirs}
1272-
DESTINATION ${OPENCV_SAMPLES_SRC_INSTALL_PATH}/${module_id}
1273-
USE_SOURCE_PERMISSIONS COMPONENT samples)
1282+
DESTINATION "${OPENCV_SAMPLES_SRC_INSTALL_PATH}/${module_id}"
1283+
COMPONENT samples)
12741284
endif()
12751285
endfunction()

modules/core/src/opengl.cpp

Lines changed: 31 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -56,53 +56,42 @@ using namespace cv::cuda;
5656

5757
namespace
5858
{
59-
#ifndef HAVE_OPENGL
60-
inline void throw_no_ogl() { CV_Error(cv::Error::OpenGlNotSupported, "The library is compiled without OpenGL support"); }
61-
#else
62-
inline void throw_no_ogl() { CV_Error(cv::Error::OpenGlApiCallError, "OpenGL context doesn't exist"); }
63-
64-
bool checkError(const char* file, const int line, const char* func = 0)
59+
#ifndef HAVE_OPENGL
60+
inline static void throw_no_ogl() { CV_Error(cv::Error::OpenGlNotSupported, "The library is compiled without OpenGL support"); }
61+
#elif defined _DEBUG
62+
inline static bool checkError(const char* file, const int line, const char* func = 0)
63+
{
64+
GLenum err = gl::GetError();
65+
if (err != gl::NO_ERROR_)
6566
{
66-
GLenum err = gl::GetError();
67-
68-
if (err != gl::NO_ERROR_)
67+
const char* msg;
68+
switch (err)
6969
{
70-
const char* msg;
71-
72-
switch (err)
73-
{
74-
case gl::INVALID_ENUM:
75-
msg = "An unacceptable value is specified for an enumerated argument";
76-
break;
77-
78-
case gl::INVALID_VALUE:
79-
msg = "A numeric argument is out of range";
80-
break;
81-
82-
case gl::INVALID_OPERATION:
83-
msg = "The specified operation is not allowed in the current state";
84-
break;
85-
86-
case gl::OUT_OF_MEMORY:
87-
msg = "There is not enough memory left to execute the command";
88-
break;
89-
90-
default:
91-
msg = "Unknown error";
92-
};
93-
94-
cvError(CV_OpenGlApiCallError, func, msg, file, line);
95-
96-
return false;
97-
}
98-
99-
return true;
70+
case gl::INVALID_ENUM:
71+
msg = "An unacceptable value is specified for an enumerated argument";
72+
break;
73+
case gl::INVALID_VALUE:
74+
msg = "A numeric argument is out of range";
75+
break;
76+
case gl::INVALID_OPERATION:
77+
msg = "The specified operation is not allowed in the current state";
78+
break;
79+
case gl::OUT_OF_MEMORY:
80+
msg = "There is not enough memory left to execute the command";
81+
break;
82+
default:
83+
msg = "Unknown error";
84+
};
85+
cvError(CV_OpenGlApiCallError, func, msg, file, line);
86+
return false;
10087
}
101-
#endif
102-
103-
#define CV_CheckGlError() CV_DbgAssert( (checkError(__FILE__, __LINE__, CV_Func)) )
88+
return true;
89+
}
90+
#endif // HAVE_OPENGL
10491
} // namespace
10592

93+
#define CV_CheckGlError() CV_DbgAssert( (checkError(__FILE__, __LINE__, CV_Func)) )
94+
10695
#ifdef HAVE_OPENGL
10796
namespace
10897
{

samples/CMakeLists.txt

Lines changed: 72 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,104 @@
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+
# Add single target to build all samples in the group: 'make opencv_samples_cpp'
19+
set(parent_target opencv_samples_${sub})
20+
if(NOT TARGET ${parent_target})
21+
add_custom_target(${parent_target})
22+
if(TARGET opencv_samples)
23+
add_dependencies(opencv_samples ${parent_target})
24+
endif()
25+
endif()
26+
add_dependencies(${parent_target} ${the_target})
27+
set(${out_target} ${the_target} PARENT_SCOPE)
28+
endfunction()
29+
230
if(NOT CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_LIST_DIR)
31+
#===================================================================================================
332
#
4-
# BUILD CASE 1: Build samples with library sources
5-
#
6-
7-
8-
# ----------------------------------------------------------------------------
9-
# CMake file for samples. See root CMakeLists.txt
33+
# Build as part of OpenCV
1034
#
11-
# ----------------------------------------------------------------------------
35+
#===================================================================================================
36+
37+
function(ocv_install_example_src relpath)
38+
if(INSTALL_C_EXAMPLES)
39+
file(GLOB files ${ARGN})
40+
install(FILES ${files}
41+
DESTINATION "${OPENCV_SAMPLES_SRC_INSTALL_PATH}/${relpath}"
42+
COMPONENT samples)
43+
endif()
44+
endfunction()
1245

1346
add_subdirectory(cpp)
1447
add_subdirectory(java/tutorial_code)
1548
add_subdirectory(dnn)
1649
add_subdirectory(gpu)
1750
add_subdirectory(tapi)
18-
51+
add_subdirectory(opencl)
1952
if(WIN32 AND HAVE_DIRECTX)
2053
add_subdirectory(directx)
2154
endif()
22-
2355
if((NOT ANDROID) AND HAVE_OPENGL)
2456
add_subdirectory(opengl)
2557
endif()
26-
2758
if(HAVE_OPENVX)
2859
add_subdirectory(openvx)
2960
endif()
30-
3161
if(UNIX AND NOT ANDROID AND (HAVE_VA OR HAVE_VA_INTEL))
3262
add_subdirectory(va_intel)
3363
endif()
34-
3564
if(ANDROID AND BUILD_ANDROID_EXAMPLES)
3665
add_subdirectory(android)
3766
endif()
38-
3967
if(INSTALL_PYTHON_EXAMPLES)
4068
add_subdirectory(python)
4169
endif()
4270

43-
#
44-
# END OF BUILD CASE 1: Build samples with library sources
45-
#
71+
ocv_install_example_src("." CMakeLists.txt)
72+
if(INSTALL_C_EXAMPLES)
73+
install(DIRECTORY data
74+
DESTINATION "${OPENCV_SAMPLES_SRC_INSTALL_PATH}/data"
75+
COMPONENT samples_data)
76+
endif()
77+
4678
else()
79+
#===================================================================================================
4780
#
48-
# BUILD CASE 2: Build samples with library binaries
81+
# Standalone mode
4982
#
83+
#===================================================================================================
5084
cmake_minimum_required(VERSION 2.8)
5185

5286
project(samples C CXX)
5387
option(BUILD_EXAMPLES "Build samples" ON)
5488

55-
find_package(OpenCV REQUIRED)
89+
# Assuming following installation folder structure (default for UNIX):
90+
# <install_root>/share/
91+
# └── OpenCV/ <-- OPENCV_CONFIG_INSTALL_PATH
92+
# ├── OpenCVConfig.cmake <-- file to be found by find_package
93+
# ├── ...
94+
# ├── samples/ <-- OPENCV_SAMPLES_SRC_INSTALL_PATH
95+
# │   ├── CMakeLists.txt <-- this file
96+
# │   ├── cpp/
97+
find_package(OpenCV REQUIRED PATHS "..")
98+
99+
function(ocv_install_example_src)
100+
# not used in this branch
101+
endfunction()
56102

57103
if(MSVC)
58104
if(NOT ENABLE_BUILD_HARDENING)
@@ -80,16 +126,15 @@ if(MSVC)
80126
endif()
81127

82128
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-
88129
if(WIN32)
89130
add_subdirectory(directx)
90131
endif()
132+
add_subdirectory(dnn)
133+
# add_subdirectory(gpu)
134+
add_subdirectory(opencl)
135+
# add_subdirectory(opengl)
136+
# add_subdirectory(openvx)
137+
add_subdirectory(tapi)
138+
# add_subdirectory(va_intel)
91139

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

0 commit comments

Comments
 (0)