From 13f4e70e9546af9a8b276c919cf88bfcc0cb2758 Mon Sep 17 00:00:00 2001
From: Andrey Kamaev <no@email>
Date: Tue, 24 Jan 2012 16:55:47 +0000
Subject: [PATCH] Updating opencv module definition in cmake

---
 CMakeLists.txt                 |  19 ++---
 cmake/OpenCVModule.cmake       | 137 ++++++++++++++++++---------------
 cmake/OpenCVUtils.cmake        |  11 +++
 modules/gpu/CMakeLists.txt     |  61 ++-------------
 modules/highgui/CMakeLists.txt | 109 +++++---------------------
 modules/ts/CMakeLists.txt      |   7 +-
 6 files changed, 127 insertions(+), 217 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 94073a165..43409620a 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -228,6 +228,7 @@ OCV_OPTION(ENABLE_SSE3                "Enable SSE3 instructions"
 OCV_OPTION(ENABLE_SSSE3               "Enable SSSE3 instructions"                                OFF  IF (CMAKE_COMPILER_IS_GNUCXX AND (X86 OR X86_64)) )
 OCV_OPTION(ENABLE_SSE41               "Enable SSE4.1 instructions"                               OFF  IF (CV_ICC OR CMAKE_COMPILER_IS_GNUCXX AND (X86 OR X86_64)) )
 OCV_OPTION(ENABLE_SSE42               "Enable SSE4.2 instructions"                               OFF  IF (CMAKE_COMPILER_IS_GNUCXX AND (X86 OR X86_64)) )
+OCV_OPTION(OPENCV_WARNINGS_ARE_ERRORS "Treat warnings as errors"                                 OFF )
 
 # ----------------------------------------------------------------------------
 #  Get actual OpenCV version number from sources
@@ -1056,21 +1057,16 @@ include_directories("."
     "${CMAKE_CURRENT_SOURCE_DIR}/include/opencv"
     )
 
-# ----------------------------------------------------------------------------
-#           Set the maximum level of warnings:
-# ----------------------------------------------------------------------------
-# Should be set to true for development
-set(OPENCV_WARNINGS_ARE_ERRORS OFF CACHE BOOL "Treat warnings as errors")
 if (WIN32 AND ${CMAKE_GENERATOR} MATCHES "(MinGW)|(MSYS)")
     set(CMAKE_CXX_FLAGS_RELEASE "-O2 -DNDEBUG" CACHE STRING "")
 endif()
 
-set(OPENCV_EXTRA_C_FLAGS)
-set(OPENCV_EXTRA_C_FLAGS_RELEASE)
-set(OPENCV_EXTRA_C_FLAGS_DEBUG)
-set(OPENCV_EXTRA_EXE_LINKER_FLAGS)
-set(OPENCV_EXTRA_EXE_LINKER_FLAGS_RELEASE)
-set(OPENCV_EXTRA_EXE_LINKER_FLAGS_DEBUG)
+set(OPENCV_EXTRA_C_FLAGS "")
+set(OPENCV_EXTRA_C_FLAGS_RELEASE "")
+set(OPENCV_EXTRA_C_FLAGS_DEBUG "")
+set(OPENCV_EXTRA_EXE_LINKER_FLAGS "")
+set(OPENCV_EXTRA_EXE_LINKER_FLAGS_RELEASE "")
+set(OPENCV_EXTRA_EXE_LINKER_FLAGS_DEBUG "")
 
 if(MSVC)
     set(OPENCV_EXTRA_C_FLAGS "${OPENCV_EXTRA_C_FLAGS} /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS")
@@ -1667,3 +1663,4 @@ status("")
 if("${CMAKE_CURRENT_SOURCE_DIR}" STREQUAL "${CMAKE_CURRENT_BINARY_DIR}")
     message(WARNING "The source directory is the same as binary directory. \"make clean\" may damage the source tree")
 endif()
+
diff --git a/cmake/OpenCVModule.cmake b/cmake/OpenCVModule.cmake
index dde6e8171..21ff49e27 100644
--- a/cmake/OpenCVModule.cmake
+++ b/cmake/OpenCVModule.cmake
@@ -1,5 +1,11 @@
-#opencv precompiled headers macro (can add pch to modules and tests)
-#this macro must be called after any "add_definitions" commands, otherwise precompiled headers will not work
+set(opencv_public_modules "" CACHE INTERNAL "List of OpenCV modules included into the build")
+# helper macro for modules management
+macro(register_opencv_module name)
+   set(opencv_public_modules ${opencv_public_modules} ${name} CACHE INTERNAL "List of OpenCV modules included into the build")
+endmacro()
+
+# opencv precompiled headers macro (can add pch to modules and tests)
+# this macro must be called after any "add_definitions" commands, otherwise precompiled headers will not work
 macro(add_opencv_precompiled_headers the_target)
     if("${the_target}" MATCHES "opencv_test_.*")
         SET(pch_name "test/test_precomp")
@@ -123,46 +129,8 @@ macro(define_opencv_test name)
     endif()
 endmacro()
 
-# this is a template for a OpenCV module
-# define_opencv_module(<module_name> <dependencies>)
-macro(define_opencv_module name)
-
-    project(opencv_${name})
-
-    include_directories("${CMAKE_CURRENT_SOURCE_DIR}/include"
-                        "${CMAKE_CURRENT_SOURCE_DIR}/src"
-                        "${CMAKE_CURRENT_BINARY_DIR}")
-
-    foreach(d ${ARGN})
-        if(d MATCHES "opencv_")
-            string(REPLACE "opencv_" "${OpenCV_SOURCE_DIR}/modules/" d_dir ${d})
-            if (EXISTS "${d_dir}/include")
-                include_directories("${d_dir}/include")
-            endif()
-        endif()
-    endforeach()
-
-    file(GLOB lib_srcs "src/*.cpp")
-    file(GLOB lib_int_hdrs "src/*.h*")
-    file(GLOB lib_hdrs "include/opencv2/${name}/*.h*")
-    file(GLOB lib_hdrs_detail "include/opencv2/${name}/detail/*.h*")
-
-    if(COMMAND get_module_external_sources)
-       get_module_external_sources(${name})
-    endif()
-
-    source_group("Src" FILES ${lib_srcs} ${lib_int_hdrs})
-    source_group("Include" FILES ${lib_hdrs})
-    source_group("Include\\detail" FILES ${lib_hdrs_detail})
-    list(APPEND lib_hdrs ${lib_hdrs_detail})
-
+macro(setup_opencv_module name)
     set(the_target "opencv_${name}")
-    if (${name} MATCHES "ts" AND MINGW)
-        add_library(${the_target} STATIC ${lib_srcs} ${lib_hdrs} ${lib_int_hdrs})
-    else()
-        add_library(${the_target} ${lib_srcs} ${lib_hdrs} ${lib_int_hdrs})
-    endif()
-
     # For dynamic link numbering convenions
     if(NOT ANDROID)
         # Android SDK build scripts can include only .so files into final .apk
@@ -195,23 +163,11 @@ macro(define_opencv_module name)
         INSTALL_NAME_DIR lib
         )
 
-    # Add the required libraries for linking:
-    target_link_libraries(${the_target} ${OPENCV_LINKER_LIBS} ${IPP_LIBS} ${ARGN})
-
     if(MSVC)
         if(CMAKE_CROSSCOMPILING)
-            set_target_properties(${the_target} PROPERTIES
-                LINK_FLAGS "/NODEFAULTLIB:secchk"
-                )
+            set_target_properties(${the_target} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:secchk")
         endif()
-        set_target_properties(${the_target} PROPERTIES
-            LINK_FLAGS "/NODEFAULTLIB:libc /DEBUG"
-            )
-    endif()
-
-    # Dependencies of this target:
-    if(ARGN)
-        add_dependencies(${the_target} ${ARGN})
+        set_target_properties(${the_target} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:libc /DEBUG")
     endif()
 
     install(TARGETS ${the_target}
@@ -219,12 +175,71 @@ macro(define_opencv_module name)
         LIBRARY DESTINATION ${OPENCV_LIB_INSTALL_PATH} COMPONENT main
         ARCHIVE DESTINATION ${OPENCV_LIB_INSTALL_PATH} COMPONENT main)
 
-    install(FILES ${lib_hdrs}
-        DESTINATION ${OPENCV_INCLUDE_PREFIX}/opencv2/${name}
-        COMPONENT main)
+    if(lib_hdrs)
+        install(FILES ${lib_hdrs}
+            DESTINATION ${OPENCV_INCLUDE_PREFIX}/opencv2/${name}
+            COMPONENT main)
+    endif()
 
     add_opencv_precompiled_headers(${the_target})
-
-    define_opencv_test(${name})
-    define_opencv_perf_test(${name})
 endmacro()
+
+# this is a template for a OpenCV module declaration
+# define_opencv_moduleEx(<module_name> [public|internal] [STATIC|SHARED|AUTO] <dependencies>)
+macro(define_opencv_moduleEx _name _visibility moduletype)
+    string(TOLOWER "${_name}" name)
+    string(TOUPPER "${_name}" mname)
+    string(TOLOWER "${_visibility}" visibility)
+    string(TOUPPER "${moduletype}" modtype)
+    if(modtype STREQUAL "AUTO" )
+        set(modtype "")
+    endif()
+
+    option(OCVMODULE_${mname} "Include ${name} module into the OpenCV build" ON)
+    if(OCVMODULE_${mname})
+        set(the_target "opencv_${name}")
+
+        if(visibility STREQUAL "public")
+            register_opencv_module(${the_target})
+        endif()
+
+        project(${the_target})
+
+        include_directories("${CMAKE_CURRENT_SOURCE_DIR}/include"
+                            "${CMAKE_CURRENT_SOURCE_DIR}/src"
+                            "${CMAKE_CURRENT_BINARY_DIR}")
+
+        include_opencv_modules(${ARGN})
+
+        file(GLOB lib_srcs "src/*.cpp")
+        file(GLOB lib_int_hdrs "src/*.h*")
+        file(GLOB lib_hdrs "include/opencv2/${name}/*.h*")
+        file(GLOB lib_hdrs_detail "include/opencv2/${name}/detail/*.h*")
+
+        if(COMMAND get_module_external_sources)
+            get_module_external_sources(${name})
+        endif()
+
+        source_group("Src" FILES ${lib_srcs} ${lib_int_hdrs})
+        source_group("Include" FILES ${lib_hdrs})
+        source_group("Include\\detail" FILES ${lib_hdrs_detail})
+
+        list(APPEND lib_hdrs ${lib_hdrs_detail})
+
+        add_library(${the_target} ${modtype} ${lib_srcs} ${lib_hdrs} ${lib_int_hdrs})
+
+        # Add the required libraries for linking:
+        target_link_libraries(${the_target} ${OPENCV_LINKER_LIBS} ${IPP_LIBS} ${ARGN})
+
+        setup_opencv_module("${name}")
+        define_opencv_test(${name})
+        define_opencv_perf_test(${name})
+    endif()
+endmacro()
+
+# this is a shorthand for a public OpenCV module declaration
+# define_opencv_module(<module_name> <dependencies>)
+macro(define_opencv_module name)
+    define_opencv_moduleEx(${name} PUBLIC AUTO ${ARGN})
+endmacro()
+
diff --git a/cmake/OpenCVUtils.cmake b/cmake/OpenCVUtils.cmake
index fbac99b7a..4b5efee83 100644
--- a/cmake/OpenCVUtils.cmake
+++ b/cmake/OpenCVUtils.cmake
@@ -116,3 +116,14 @@ macro(status text)
      endif()
 endmacro()
 
+# Setup include path for OpenCV headers for specified modules
+macro(include_opencv_modules)
+    foreach(d ${ARGN})
+        if(d MATCHES "opencv_")
+            string(REPLACE "opencv_" "${OpenCV_SOURCE_DIR}/modules/" d_dir ${d})
+            if (EXISTS "${d_dir}/include")
+                include_directories("${d_dir}/include")
+            endif()
+        endif()
+    endforeach()
+endmacro()
diff --git a/modules/gpu/CMakeLists.txt b/modules/gpu/CMakeLists.txt
index fcef9b9f6..5b71fbce8 100644
--- a/modules/gpu/CMakeLists.txt
+++ b/modules/gpu/CMakeLists.txt
@@ -1,3 +1,8 @@
+option(OCVMODULE_GPU "Include gpu module into the OpenCV build" ON)
+if(NOT OCVMODULE_GPU)
+    return()
+endif()
+
 set(name "gpu")
 
 set(the_target "opencv_${name}")
@@ -28,12 +33,7 @@ file(GLOB lib_device_hdrs_detail "src/opencv2/gpu/device/detail/*.h*")
 source_group("Device" FILES ${lib_device_hdrs})
 source_group("Device\\Detail" FILES ${lib_device_hdrs_detail})
 
-foreach(d ${DEPS_HEADER})
-	if(${d} MATCHES "opencv_")
-		string(REPLACE "opencv_" "${CMAKE_CURRENT_SOURCE_DIR}/../" d_dir ${d})
-		include_directories("${d_dir}/include")
-	endif()
-endforeach()
+include_opencv_modules(${DEPS_HEADER})
 
 if (HAVE_CUDA)
     file(GLOB_RECURSE ncv_srcs "src/nvidia/*.cpp")	
@@ -91,35 +91,6 @@ endif()
 
 add_library(${the_target} ${lib_srcs} ${lib_hdrs} ${lib_int_hdrs} ${lib_cuda} ${lib_cuda_hdrs} ${lib_device_hdrs} ${lib_device_hdrs_detail} ${ncv_srcs} ${ncv_hdrs} ${ncv_cuda} ${cuda_objs})
 
-# For dynamic link numbering convenions
-set_target_properties(${the_target} PROPERTIES
-	VERSION ${OPENCV_VERSION}
-	SOVERSION ${OPENCV_SOVERSION}
-	OUTPUT_NAME "${the_target}${OPENCV_DLLVERSION}"
-	)
-
-if(ENABLE_SOLUTION_FOLDERS)
-	set_target_properties(${the_target} PROPERTIES FOLDER "modules")
-endif()	
-	
-if (BUILD_SHARED_LIBS)
-	if (MSVC)
-		set_target_properties(${the_target} PROPERTIES DEFINE_SYMBOL CVAPI_EXPORTS)
-	else()
-		add_definitions(-DCVAPI_EXPORTS)
-	endif()
-endif()
-
-add_opencv_precompiled_headers(${the_target})
-
-# Additional target properties
-set_target_properties(${the_target} PROPERTIES
-    DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
-    ARCHIVE_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH}
-    RUNTIME_OUTPUT_DIRECTORY ${EXECUTABLE_OUTPUT_PATH}
-    INSTALL_NAME_DIR lib
-    )
-
 # Add the required libraries for linking:
 target_link_libraries(${the_target} ${OPENCV_LINKER_LIBS} ${IPP_LIBS} ${DEPS} )
 
@@ -139,25 +110,7 @@ if (HAVE_CUDA)
     endif()
 endif()
 
-if(MSVC)
-    if(CMAKE_CROSSCOMPILING)
-        set_target_properties(${the_target} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:secchk")
-    endif()
-
-    set_target_properties(${the_target} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:libc")
-endif()
-
-# Dependencies of this target:
-add_dependencies(${the_target} ${DEPS})
-
-install(TARGETS ${the_target}
-	RUNTIME DESTINATION bin COMPONENT main
-	LIBRARY DESTINATION ${OPENCV_LIB_INSTALL_PATH} COMPONENT main
-	ARCHIVE DESTINATION ${OPENCV_LIB_INSTALL_PATH} COMPONENT main)
-
-install(FILES ${lib_hdrs}
-	DESTINATION ${OPENCV_INCLUDE_PREFIX}/opencv2/${name}
-	COMPONENT main)
+setup_opencv_module(${name})
 
 install(FILES src/nvidia/NPP_staging/NPP_staging.hpp  src/nvidia/core/NCV.hpp
 	DESTINATION ${OPENCV_INCLUDE_PREFIX}/opencv2/${name}
diff --git a/modules/highgui/CMakeLists.txt b/modules/highgui/CMakeLists.txt
index 7cb2dee70..50377f5c7 100644
--- a/modules/highgui/CMakeLists.txt
+++ b/modules/highgui/CMakeLists.txt
@@ -3,7 +3,14 @@
 #   Some parts taken from version of Hartmut Seichter, HIT Lab NZ.
 #   Jose Luis Blanco, 2008
 # ----------------------------------------------------------------------------
-project(opencv_highgui)
+
+option(OCVMODULE_HIGHGUI "Include highgui module into the OpenCV build" ON)
+if(NOT OCVMODULE_HIGHGUI)
+    return()
+endif()
+
+set(the_target "opencv_highgui")
+project(${the_target})
 
 set(GRFMT_LIBS)
 
@@ -68,10 +75,6 @@ if(WITH_OPENEXR AND OPENEXR_FOUND)
     set(GRFMT_LIBS ${GRFMT_LIBS} ${OPENEXR_LIBRARIES})
 endif()
 
-if(MSVC)
-    set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /NODEFAULTLIB:libcmt.lib")
-endif()
-
 file(GLOB grfmt_hdrs src/grfmt*.hpp)
 file(GLOB grfmt_srcs src/grfmt*.cpp)
 set(grfmt_hdrs src/bitstrm.hpp ${grfmt_hdrs})
@@ -250,19 +253,15 @@ if(OPENCV_BUILD_3RDPARTY_LIBS)
     if(WIN32)
         link_directories("${CMAKE_CURRENT_SOURCE_DIR}/../../3rdparty/lib")
     endif()
-    link_directories(
-        "${CMAKE_BINARY_DIR}/3rdparty/lib"
-        "${CMAKE_BINARY_DIR}/3rdparty/lib/${ConfigurationName}"
-        )
+    #link_directories(
+     #   "${CMAKE_BINARY_DIR}/3rdparty/lib"
+      #  "${CMAKE_BINARY_DIR}/3rdparty/lib/${ConfigurationName}"
+       # )
 endif()
 
-set(lib_srcs ${highgui_srcs} ${grfmt_srcs})
-
 # ----------------------------------------------------------------------------------
 #               Define the library target:
 # ----------------------------------------------------------------------------------
-set(the_target "opencv_highgui")
-
 include_directories("${CMAKE_CURRENT_SOURCE_DIR}/include"
                     "${CMAKE_CURRENT_SOURCE_DIR}/../core/include"
                     "${CMAKE_CURRENT_SOURCE_DIR}/../imgproc/include"
@@ -273,75 +272,15 @@ if(WIN32)
     include_directories("${CMAKE_CURRENT_SOURCE_DIR}/../../3rdparty/include")
 endif()
 
-add_library(${the_target} ${lib_srcs} ${highgui_hdrs} ${grfmt_hdrs} ${highgui_ext_hdrs})
+add_library(${the_target} ${highgui_srcs} ${grfmt_srcs} ${highgui_hdrs} ${grfmt_hdrs} ${highgui_ext_hdrs})
+register_opencv_module(${the_target})
 
 if (BUILD_SHARED_LIBS)
-	add_definitions(-DHIGHGUI_EXPORTS)
-	if(MSVC)
-		set_target_properties(${the_target} PROPERTIES DEFINE_SYMBOL CVAPI_EXPORTS)
-	else()
-		add_definitions(-DCVAPI_EXPORTS)
-	endif()
+    add_definitions(-DHIGHGUI_EXPORTS)
 endif()
 
-add_opencv_precompiled_headers(${the_target})
-
-# For dynamic link numbering convenions
-if(NOT ANDROID)
-    # Android SDK build scripts can include only .so files into final .apk
-    set_target_properties(${the_target} PROPERTIES
-        VERSION ${OPENCV_VERSION}
-        SOVERSION ${OPENCV_SOVERSION}
-        )
-endif()
-
-set_target_properties(${the_target} PROPERTIES OUTPUT_NAME "${the_target}${OPENCV_DLLVERSION}" )
-
-# Additional target properties
-set_target_properties(${the_target} PROPERTIES
-    DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
-    ARCHIVE_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH}
-    RUNTIME_OUTPUT_DIRECTORY ${EXECUTABLE_OUTPUT_PATH}
-    INSTALL_NAME_DIR lib
-    LINK_INTERFACE_LIBRARIES ""
-    )
-
-if(ENABLE_SOLUTION_FOLDERS)
-    set_target_properties(${the_target} PROPERTIES FOLDER "modules")
-endif() 
-
-
 if(MSVC)
-    set_target_properties(${the_target} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:atlthunk.lib /NODEFAULTLIB:atlsd.lib /DEBUG")
-endif(MSVC)
-
-# Dependencies of this target:
-add_dependencies(${the_target} opencv_core opencv_imgproc)
-
-# Add the required libraries for linking:
-
-if(WITH_JASPER AND NOT JASPER_FOUND)
-    add_dependencies(${the_target} libjasper)
-endif()
-
-if(WITH_JPEG AND NOT JPEG_FOUND)
-    add_dependencies(${the_target} libjpeg)
-endif()
-
-if(WITH_PNG AND NOT PNG_FOUND)
-    add_dependencies(${the_target} libpng)
-endif()
-
-if(WITH_TIFF AND NOT TIFF_FOUND)
-    add_dependencies(${the_target} libtiff)
-endif()
-
-if(NOT ZLIB_FOUND)
-    add_dependencies(${the_target} zlib)
-endif()
-
-if(WITH_ANDROID_CAMERA)
-    add_dependencies(${the_target} opencv_androidcamera)
+    set_target_properties(${the_target} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:atlthunk.lib /NODEFAULTLIB:atlsd.lib /NODEFAULTLIB:libcmt.lib /DEBUG")
 endif()
 
 #message(STATUS "GRFMT: ${GRFMT_LIBS}")
@@ -368,19 +307,9 @@ if(APPLE AND NOT IOS)
 endif()
 
 if (IOS)
-	target_link_libraries(${the_target} "-lbz2 -framework QuartzCore -framework CoreFoundation -framework ImageIO -framework CoreGraphics -framework AVFoundation")
+    target_link_libraries(${the_target} "-lbz2 -framework QuartzCore -framework CoreFoundation -framework ImageIO -framework CoreGraphics -framework AVFoundation")
 endif()
 
-install(TARGETS ${the_target}
-    RUNTIME DESTINATION bin COMPONENT main
-    LIBRARY DESTINATION ${OPENCV_LIB_INSTALL_PATH} COMPONENT main
-    ARCHIVE DESTINATION ${OPENCV_LIB_INSTALL_PATH} COMPONENT main)
-
-install(FILES ${highgui_ext_hdrs}
-        DESTINATION ${OPENCV_INCLUDE_PREFIX}/opencv2/highgui
-        COMPONENT main)
-
-
-############################# highgui tests ################################
+setup_opencv_module(highgui)
 define_opencv_test(highgui)
-define_opencv_perf_test(highgui)
+define_opencv_perf_test(highgui)
\ No newline at end of file
diff --git a/modules/ts/CMakeLists.txt b/modules/ts/CMakeLists.txt
index f9735b9de..a8182faa2 100644
--- a/modules/ts/CMakeLists.txt
+++ b/modules/ts/CMakeLists.txt
@@ -6,4 +6,9 @@ if(BUILD_SHARED_LIBS)
 else()
     add_definitions(-DGTEST_CREATE_SHARED_LIBRARY=0)
 endif()
-define_opencv_module(ts opencv_core)
+
+if(MINGW)
+    define_opencv_moduleEx(ts PUBLIC STATIC opencv_core)
+else()
+    define_opencv_module(ts opencv_core)
+endif()