Merge pull request #3063 from akarsakov:ocl_warps_check
[profile/ivi/opencv.git] / cmake / OpenCVUtils.cmake
index 9fa94bb..a7d449f 100644 (file)
@@ -1,3 +1,22 @@
+# Debugging function
+function(ocv_cmake_dump_vars)
+  cmake_parse_arguments(DUMP "" "TOFILE" "" ${ARGN})
+  set(regex "${DUMP_UNPARSED_ARGUMENTS}")
+  get_cmake_property(_variableNames VARIABLES)
+  set(VARS "")
+  foreach(_variableName ${_variableNames})
+    if(_variableName MATCHES "${regex}")
+      set(VARS "${VARS}${_variableName}=${${_variableName}}\n")
+    endif()
+  endforeach()
+  if(DUMP_TOFILE)
+    file(WRITE ${CMAKE_BINARY_DIR}/${DUMP_TOFILE} "${VARS}")
+  else()
+    message(AUTHOR_WARNING "${VARS}")
+  endif()
+endfunction()
+
+
 # Search packages for host system instead of packages for target system
 # in case of cross compilation thess macro should be defined by toolchain file
 if(NOT COMMAND find_host_package)
@@ -23,6 +42,11 @@ macro(ocv_assert)
   endif()
 endmacro()
 
+macro(ocv_debug_message)
+#  string(REPLACE ";" " " __msg "${ARGN}")
+#  message(STATUS "${__msg}")
+endmacro()
+
 macro(ocv_check_environment_variables)
   foreach(_var ${ARGN})
     if(NOT DEFINED ${_var} AND DEFINED ENV{${_var}})
@@ -34,8 +58,18 @@ macro(ocv_check_environment_variables)
   endforeach()
 endmacro()
 
+# rename modules target to world if needed
+macro(_ocv_fix_target target_var)
+  if(BUILD_opencv_world)
+    if(OPENCV_MODULE_${${target_var}}_IS_PART_OF_WORLD)
+      set(${target_var} opencv_world)
+    endif()
+  endif()
+endmacro()
+
 # adds include directories in such way that directories from the OpenCV source tree go first
 function(ocv_include_directories)
+  ocv_debug_message("ocv_include_directories( ${ARGN} )")
   set(__add_before "")
   foreach(dir ${ARGN})
     get_filename_component(__abs_dir "${dir}" ABSOLUTE)
@@ -48,6 +82,30 @@ function(ocv_include_directories)
   include_directories(BEFORE ${__add_before})
 endfunction()
 
+# adds include directories in such way that directories from the OpenCV source tree go first
+function(ocv_target_include_directories target)
+  _ocv_fix_target(target)
+  set(__params "")
+  foreach(dir ${ARGN})
+    get_filename_component(__abs_dir "${dir}" ABSOLUTE)
+    if("${__abs_dir}" MATCHES "^${OpenCV_SOURCE_DIR}" OR "${__abs_dir}" MATCHES "^${OpenCV_BINARY_DIR}")
+      list(APPEND __params "${__abs_dir}")
+    else()
+      list(APPEND __params "${dir}")
+    endif()
+  endforeach()
+  if(CMAKE_VERSION VERSION_LESS 2.8.11)
+    include_directories(${__params})
+  else()
+    if(TARGET ${target})
+      target_include_directories(${target} PRIVATE ${__params})
+    else()
+      set(__new_inc "${OCV_TARGET_INCLUDE_DIRS_${target}};${__params}")
+      set(OCV_TARGET_INCLUDE_DIRS_${target} "${__new_inc}" CACHE INTERNAL "")
+    endif()
+  endif()
+endfunction()
+
 # clears all passed variables
 macro(ocv_clear_vars)
   foreach(_var ${ARGN})
@@ -246,16 +304,19 @@ macro(CHECK_MODULE module_name define)
       set(${define} 1)
       foreach(P "${ALIAS_INCLUDE_DIRS}")
         if(${P})
+          list(APPEND VIDEOIO_INCLUDE_DIRS ${${P}})
           list(APPEND HIGHGUI_INCLUDE_DIRS ${${P}})
         endif()
       endforeach()
 
       foreach(P "${ALIAS_LIBRARY_DIRS}")
         if(${P})
+          list(APPEND VIDEOIO_LIBRARY_DIRS ${${P}})
           list(APPEND HIGHGUI_LIBRARY_DIRS ${${P}})
         endif()
       endforeach()
 
+      list(APPEND VIDEOIO_LIBRARIES ${${ALIAS_LIBRARIES}})
       list(APPEND HIGHGUI_LIBRARIES ${${ALIAS_LIBRARIES}})
     endif()
   endif()
@@ -273,8 +334,8 @@ endfunction()
 
 macro(ocv_finalize_status)
   if(NOT OPENCV_SKIP_STATUS_FINALIZATION)
-    if(TARGET opencv_core)
-      execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different "${OPENCV_BUILD_INFO_FILE}" "${opencv_core_BINARY_DIR}/version_string.inc" OUTPUT_QUIET)
+    if(DEFINED OPENCV_MODULE_opencv_core_BINARY_DIR)
+      execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different "${OPENCV_BUILD_INFO_FILE}" "${OPENCV_MODULE_opencv_core_BINARY_DIR}/version_string.inc" OUTPUT_QUIET)
     endif()
   endif()
 endmacro()
@@ -511,16 +572,20 @@ function(ocv_install_target)
 
 #      message(STATUS "Process ${__target} dst=${__dst}...")
       if(DEFINED __dst)
-        get_target_property(fname ${__target} LOCATION_DEBUG)
-        if(fname MATCHES "\\.lib$")
-          string(REGEX REPLACE "\\.lib$" ".pdb" fname "${fname}")
-          install(FILES ${fname} DESTINATION ${__dst} CONFIGURATIONS Debug)
-        endif()
+        if(CMAKE_VERSION VERSION_LESS 2.8.12)
+          get_target_property(fname ${__target} LOCATION_DEBUG)
+          if(fname MATCHES "\\.lib$")
+            string(REGEX REPLACE "\\.lib$" ".pdb" fname "${fname}")
+            install(FILES ${fname} DESTINATION ${__dst} CONFIGURATIONS Debug)
+          endif()
 
-        get_target_property(fname ${__target} LOCATION_RELEASE)
-        if(fname MATCHES "\\.lib$")
-          string(REGEX REPLACE "\\.lib$" ".pdb" fname "${fname}")
-          install(FILES ${fname} DESTINATION ${__dst} CONFIGURATIONS Release)
+          get_target_property(fname ${__target} LOCATION_RELEASE)
+          if(fname MATCHES "\\.lib$")
+            string(REGEX REPLACE "\\.lib$" ".pdb" fname "${fname}")
+            install(FILES ${fname} DESTINATION ${__dst} CONFIGURATIONS Release)
+          endif()
+        else()
+          # CMake 2.8.12 brokes PDB support in STATIC libraries for MSVS
         endif()
       endif()
     endif()
@@ -615,7 +680,74 @@ endmacro()
 ################################################################################################
 # short command to setup source group
 function(ocv_source_group group)
-  cmake_parse_arguments(OCV_SOURCE_GROUP "" "" "GLOB" ${ARGN})
-  file(GLOB srcs ${OCV_SOURCE_GROUP_GLOB})
-  source_group(${group} FILES ${srcs})
+  if(BUILD_opencv_world AND OPENCV_MODULE_${the_module}_IS_PART_OF_WORLD)
+    set(group "${the_module}\\${group}")
+  endif()
+  cmake_parse_arguments(SG "" "DIRBASE" "GLOB;GLOB_RECURSE;FILES" ${ARGN})
+  set(files "")
+  if(SG_FILES)
+    list(APPEND files ${SG_FILES})
+  endif()
+  if(SG_GLOB)
+    file(GLOB srcs ${SG_GLOB})
+    list(APPEND files ${srcs})
+  endif()
+  if(SG_GLOB_RECURSE)
+    file(GLOB_RECURSE srcs ${SG_GLOB_RECURSE})
+    list(APPEND files ${srcs})
+  endif()
+  if(SG_DIRBASE)
+    foreach(f ${files})
+      file(RELATIVE_PATH fpart "${SG_DIRBASE}" "${f}")
+      if(fpart MATCHES "^\\.\\.")
+        message(AUTHOR_WARNING "Can't detect subpath for source_group command: Group=${group} FILE=${f} DIRBASE=${SG_DIRBASE}")
+        set(fpart "")
+      else()
+        get_filename_component(fpart "${fpart}" PATH)
+        if(fpart)
+          set(fpart "/${fpart}") # add '/'
+          string(REPLACE "/" "\\" fpart "${fpart}")
+        endif()
+      endif()
+      source_group("${group}${fpart}" FILES ${f})
+    endforeach()
+  else()
+    source_group(${group} FILES ${files})
+  endif()
 endfunction()
+
+function(ocv_target_link_libraries target)
+  _ocv_fix_target(target)
+  set(LINK_DEPS ${ARGN})
+  # process world
+  if(BUILD_opencv_world)
+    foreach(m ${OPENCV_MODULES_BUILD})
+      if(OPENCV_MODULE_${m}_IS_PART_OF_WORLD)
+        if(";${LINK_DEPS};" MATCHES ";${m};")
+          list(REMOVE_ITEM LINK_DEPS ${m})
+          if(NOT (";${LINK_DEPS};" MATCHES ";opencv_world;"))
+            list(APPEND LINK_DEPS opencv_world)
+          endif()
+        endif()
+      endif()
+    endforeach()
+  endif()
+  target_link_libraries(${target} ${LINK_DEPS})
+endfunction()
+
+function(_ocv_append_target_includes target)
+  if(DEFINED OCV_TARGET_INCLUDE_DIRS_${target})
+    target_include_directories(${target} PRIVATE ${OCV_TARGET_INCLUDE_DIRS_${target}})
+    unset(OCV_TARGET_INCLUDE_DIRS_${target} CACHE)
+  endif()
+endfunction()
+
+function(ocv_add_executable target)
+  add_executable(${target} ${ARGN})
+  _ocv_append_target_includes(${target})
+endfunction()
+
+function(ocv_add_library target)
+  add_library(${target} ${ARGN})
+  _ocv_append_target_includes(${target})
+endfunction()
\ No newline at end of file