Merge pull request #2801 from ilya-lavrenov:tapi_reduction
[profile/ivi/opencv.git] / cmake / OpenCVUtils.cmake
index 818ec9b..f2a0197 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)
@@ -11,6 +30,18 @@ if(NOT COMMAND find_host_program)
   endmacro()
 endif()
 
+# assert macro
+# Note: it doesn't support lists in arguments
+# Usage samples:
+#   ocv_assert(MyLib_FOUND)
+#   ocv_assert(DEFINED MyLib_INCLUDE_DIRS)
+macro(ocv_assert)
+  if(NOT (${ARGN}))
+    string(REPLACE ";" " " __assert_msg "${ARGN}")
+    message(AUTHOR_WARNING "Assertion failed: ${__assert_msg}")
+  endif()
+endmacro()
+
 macro(ocv_check_environment_variables)
   foreach(_var ${ARGN})
     if(NOT DEFINED ${_var} AND DEFINED ENV{${_var}})
@@ -436,6 +467,20 @@ macro(ocv_convert_to_full_paths VAR)
 endmacro()
 
 
+# convert list of paths to libraries names without lib prefix
+macro(ocv_convert_to_lib_name var)
+  set(__tmp "")
+  foreach(path ${ARGN})
+    get_filename_component(__tmp_name "${path}" NAME_WE)
+    string(REGEX REPLACE "^lib" "" __tmp_name ${__tmp_name})
+    list(APPEND __tmp "${__tmp_name}")
+  endforeach()
+  set(${var} ${__tmp})
+  unset(__tmp)
+  unset(__tmp_name)
+endmacro()
+
+
 # add install command
 function(ocv_install_target)
   install(TARGETS ${ARGN})
@@ -467,9 +512,10 @@ function(ocv_install_target)
 
       set(isArchive 0)
       set(isDst 0)
+      unset(__dst)
       foreach(e ${ARGN})
         if(isDst EQUAL 1)
-          set(DST "${e}")
+          set(__dst "${e}")
           break()
         endif()
         if(isArchive EQUAL 1 AND e STREQUAL "DESTINATION")
@@ -482,18 +528,20 @@ function(ocv_install_target)
         endif()
       endforeach()
 
-#      message(STATUS "Process ${__target} dst=${DST}...")
-      if(NOT DEFINED DST)
-        set(DST "OPENCV_LIB_INSTALL_PATH")
-      endif()
-
-      get_target_property(fname ${__target} LOCATION_DEBUG)
-      string(REPLACE ".lib" ".pdb" fname "${fname}")
-      install(FILES ${fname} DESTINATION ${DST} CONFIGURATIONS Debug)
+#      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()
 
-      get_target_property(fname ${__target} LOCATION_RELEASE)
-      string(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()
+      endif()
     endif()
   endif()
 endfunction()
@@ -586,7 +634,35 @@ 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})
+  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()