2 function(ocv_cmake_dump_vars)
3 cmake_parse_arguments(DUMP "" "TOFILE" "" ${ARGN})
4 set(regex "${DUMP_UNPARSED_ARGUMENTS}")
5 get_cmake_property(_variableNames VARIABLES)
7 foreach(_variableName ${_variableNames})
8 if(_variableName MATCHES "${regex}")
9 set(VARS "${VARS}${_variableName}=${${_variableName}}\n")
13 file(WRITE ${CMAKE_BINARY_DIR}/${DUMP_TOFILE} "${VARS}")
15 message(AUTHOR_WARNING "${VARS}")
20 # Search packages for host system instead of packages for target system
21 # in case of cross compilation thess macro should be defined by toolchain file
22 if(NOT COMMAND find_host_package)
23 macro(find_host_package)
27 if(NOT COMMAND find_host_program)
28 macro(find_host_program)
34 # Note: it doesn't support lists in arguments
36 # ocv_assert(MyLib_FOUND)
37 # ocv_assert(DEFINED MyLib_INCLUDE_DIRS)
40 string(REPLACE ";" " " __assert_msg "${ARGN}")
41 message(AUTHOR_WARNING "Assertion failed: ${__assert_msg}")
45 macro(ocv_check_environment_variables)
47 if(NOT DEFINED ${_var} AND DEFINED ENV{${_var}})
48 set(__value "$ENV{${_var}}")
49 file(TO_CMAKE_PATH "${__value}" __value) # Assume that we receive paths
50 set(${_var} "${__value}")
51 message(STATUS "Update variable ${_var} from environment: ${${_var}}")
56 # adds include directories in such way that directories from the OpenCV source tree go first
57 function(ocv_include_directories)
60 get_filename_component(__abs_dir "${dir}" ABSOLUTE)
61 if("${__abs_dir}" MATCHES "^${OpenCV_SOURCE_DIR}" OR "${__abs_dir}" MATCHES "^${OpenCV_BINARY_DIR}")
62 list(APPEND __add_before "${dir}")
64 include_directories(AFTER SYSTEM "${dir}")
67 include_directories(BEFORE ${__add_before})
70 # clears all passed variables
77 set(OCV_COMPILER_FAIL_REGEX
78 "command line option .* is valid for .* but not for C\\+\\+" # GNU
79 "command line option .* is valid for .* but not for C" # GNU
80 "unrecognized .*option" # GNU
81 "unknown .*option" # Clang
82 "ignoring unknown option" # MSVC
83 "warning D9002" # MSVC, any lang
84 "option .*not supported" # Intel
85 "[Uu]nknown option" # HP
86 "[Ww]arning: [Oo]ption" # SunPro
87 "command option .* is not recognized" # XL
88 "not supported in this configuration; ignored" # AIX
89 "File with unknown suffix passed to linker" # PGI
90 "WARNING: unknown flag:" # Open64
93 MACRO(ocv_check_compiler_flag LANG FLAG RESULT)
94 if(NOT DEFINED ${RESULT})
95 if("_${LANG}_" MATCHES "_CXX_")
96 set(_fname "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.cxx")
97 if("${CMAKE_CXX_FLAGS} ${FLAG} " MATCHES "-Werror " OR "${CMAKE_CXX_FLAGS} ${FLAG} " MATCHES "-Werror=unknown-pragmas ")
98 FILE(WRITE "${_fname}" "int main() { return 0; }\n")
100 FILE(WRITE "${_fname}" "#pragma\nint main() { return 0; }\n")
102 elseif("_${LANG}_" MATCHES "_C_")
103 set(_fname "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.c")
104 if("${CMAKE_C_FLAGS} ${FLAG} " MATCHES "-Werror " OR "${CMAKE_C_FLAGS} ${FLAG} " MATCHES "-Werror=unknown-pragmas ")
105 FILE(WRITE "${_fname}" "int main(void) { return 0; }\n")
107 FILE(WRITE "${_fname}" "#pragma\nint main(void) { return 0; }\n")
109 elseif("_${LANG}_" MATCHES "_OBJCXX_")
110 set(_fname "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.mm")
111 if("${CMAKE_CXX_FLAGS} ${FLAG} " MATCHES "-Werror " OR "${CMAKE_CXX_FLAGS} ${FLAG} " MATCHES "-Werror=unknown-pragmas ")
112 FILE(WRITE "${_fname}" "int main() { return 0; }\n")
114 FILE(WRITE "${_fname}" "#pragma\nint main() { return 0; }\n")
120 MESSAGE(STATUS "Performing Test ${RESULT}")
121 TRY_COMPILE(${RESULT}
122 "${CMAKE_BINARY_DIR}"
124 COMPILE_DEFINITIONS "${FLAG}"
125 OUTPUT_VARIABLE OUTPUT)
127 FOREACH(_regex ${OCV_COMPILER_FAIL_REGEX})
128 IF("${OUTPUT}" MATCHES "${_regex}")
135 SET(${RESULT} 1 CACHE INTERNAL "Test ${RESULT}")
136 MESSAGE(STATUS "Performing Test ${RESULT} - Success")
138 MESSAGE(STATUS "Performing Test ${RESULT} - Failed")
139 SET(${RESULT} "" CACHE INTERNAL "Test ${RESULT}")
147 macro(ocv_check_flag_support lang flag varname)
148 if("_${lang}_" MATCHES "_CXX_")
150 elseif("_${lang}_" MATCHES "_C_")
152 elseif("_${lang}_" MATCHES "_OBJCXX_")
158 string(TOUPPER "${flag}" ${varname})
159 string(REGEX REPLACE "^(/|-)" "HAVE_${_lang}_" ${varname} "${${varname}}")
160 string(REGEX REPLACE " -|-|=| |\\." "_" ${varname} "${${varname}}")
162 ocv_check_compiler_flag("${_lang}" "${ARGN} ${flag}" ${${varname}})
166 macro(ocv_warnings_disable)
167 if(NOT ENABLE_NOISY_WARNINGS)
169 set(_msvc_warnings "")
170 set(_gxx_warnings "")
172 if(arg MATCHES "^CMAKE_")
173 list(APPEND _flag_vars ${arg})
174 elseif(arg MATCHES "^/wd")
175 list(APPEND _msvc_warnings ${arg})
176 elseif(arg MATCHES "^-W")
177 list(APPEND _gxx_warnings ${arg})
180 if(MSVC AND _msvc_warnings AND _flag_vars)
181 foreach(var ${_flag_vars})
182 foreach(warning ${_msvc_warnings})
183 set(${var} "${${var}} ${warning}")
186 elseif((CMAKE_COMPILER_IS_GNUCXX OR (UNIX AND CV_ICC)) AND _gxx_warnings AND _flag_vars)
187 foreach(var ${_flag_vars})
188 foreach(warning ${_gxx_warnings})
189 if(NOT warning MATCHES "^-Wno-")
190 string(REPLACE "${warning}" "" ${var} "${${var}}")
191 string(REPLACE "-W" "-Wno-" warning "${warning}")
193 ocv_check_flag_support(${var} "${warning}" _varname)
195 set(${var} "${${var}} ${warning}")
201 unset(_msvc_warnings)
203 endif(NOT ENABLE_NOISY_WARNINGS)
206 # Provides an option that the user can optionally select.
207 # Can accept condition to control when option is available for user.
209 # option(<option_variable> "help string describing the option" <initial value or boolean expression> [IF <condition>])
210 macro(OCV_OPTION variable description value)
211 set(__value ${value})
213 set(__varname "__value")
215 if(arg STREQUAL "IF" OR arg STREQUAL "if")
216 set(__varname "__condition")
218 list(APPEND ${__varname} ${arg})
222 if("${__condition}" STREQUAL "")
223 set(__condition 2 GREATER 1)
227 if("${__value}" MATCHES ";")
229 option(${variable} "${description}" ON)
231 option(${variable} "${description}" OFF)
233 elseif(DEFINED ${__value})
235 option(${variable} "${description}" ON)
237 option(${variable} "${description}" OFF)
240 option(${variable} "${description}" ${__value})
243 unset(${variable} CACHE)
250 # Macros that checks if module have been installed.
251 # After it adds module to build and define
252 # constants passed as second arg
253 macro(CHECK_MODULE module_name define)
256 set(ALIAS ALIASOF_${module_name})
257 set(ALIAS_FOUND ${ALIAS}_FOUND)
258 set(ALIAS_INCLUDE_DIRS ${ALIAS}_INCLUDE_DIRS)
259 set(ALIAS_LIBRARY_DIRS ${ALIAS}_LIBRARY_DIRS)
260 set(ALIAS_LIBRARIES ${ALIAS}_LIBRARIES)
262 PKG_CHECK_MODULES(${ALIAS} ${module_name})
266 foreach(P "${ALIAS_INCLUDE_DIRS}")
268 list(APPEND VIDEOIO_INCLUDE_DIRS ${${P}})
269 list(APPEND HIGHGUI_INCLUDE_DIRS ${${P}})
273 foreach(P "${ALIAS_LIBRARY_DIRS}")
275 list(APPEND VIDEOIO_LIBRARY_DIRS ${${P}})
276 list(APPEND HIGHGUI_LIBRARY_DIRS ${${P}})
280 list(APPEND VIDEOIO_LIBRARIES ${${ALIAS_LIBRARIES}})
281 list(APPEND HIGHGUI_LIBRARIES ${${ALIAS_LIBRARIES}})
287 set(OPENCV_BUILD_INFO_FILE "${OpenCV_BINARY_DIR}/version_string.tmp")
288 file(REMOVE "${OPENCV_BUILD_INFO_FILE}")
289 function(ocv_output_status msg)
290 message(STATUS "${msg}")
291 string(REPLACE "\\" "\\\\" msg "${msg}")
292 string(REPLACE "\"" "\\\"" msg "${msg}")
293 file(APPEND "${OPENCV_BUILD_INFO_FILE}" "\"${msg}\\n\"\n")
296 macro(ocv_finalize_status)
297 if(NOT OPENCV_SKIP_STATUS_FINALIZATION)
298 if(TARGET opencv_core)
299 execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different "${OPENCV_BUILD_INFO_FILE}" "${opencv_core_BINARY_DIR}/version_string.inc" OUTPUT_QUIET)
305 # Status report function.
306 # Automatically align right column and selects text based on condition.
309 # status(<heading> <value1> [<value2> ...])
310 # status(<heading> <condition> THEN <text for TRUE> ELSE <text for FALSE> )
311 function(status text)
316 set(status_current_name "cond")
318 if(arg STREQUAL "THEN")
319 set(status_current_name "then")
320 elseif(arg STREQUAL "ELSE")
321 set(status_current_name "else")
323 list(APPEND status_${status_current_name} ${arg})
327 if(DEFINED status_cond)
328 set(status_placeholder_length 32)
329 string(RANDOM LENGTH ${status_placeholder_length} ALPHABET " " status_placeholder)
330 string(LENGTH "${text}" status_text_length)
331 if(status_text_length LESS status_placeholder_length)
332 string(SUBSTRING "${text}${status_placeholder}" 0 ${status_placeholder_length} status_text)
333 elseif(DEFINED status_then OR DEFINED status_else)
334 ocv_output_status("${text}")
335 set(status_text "${status_placeholder}")
337 set(status_text "${text}")
340 if(DEFINED status_then OR DEFINED status_else)
342 string(REPLACE ";" " " status_then "${status_then}")
343 string(REGEX REPLACE "^[ \t]+" "" status_then "${status_then}")
344 ocv_output_status("${status_text} ${status_then}")
346 string(REPLACE ";" " " status_else "${status_else}")
347 string(REGEX REPLACE "^[ \t]+" "" status_else "${status_else}")
348 ocv_output_status("${status_text} ${status_else}")
351 string(REPLACE ";" " " status_cond "${status_cond}")
352 string(REGEX REPLACE "^[ \t]+" "" status_cond "${status_cond}")
353 ocv_output_status("${status_text} ${status_cond}")
356 ocv_output_status("${text}")
361 # splits cmake libraries list of format "general;item1;debug;item2;release;item3" to two lists
362 macro(ocv_split_libs_list lst lstdbg lstopt)
366 foreach(word ${${lst}})
367 if(word STREQUAL "debug" OR word STREQUAL "optimized")
368 set(perv_keyword ${word})
369 elseif(word STREQUAL "general")
371 elseif(perv_keyword STREQUAL "debug")
372 list(APPEND ${lstdbg} "${word}")
374 elseif(perv_keyword STREQUAL "optimized")
375 list(APPEND ${lstopt} "${word}")
378 list(APPEND ${lstdbg} "${word}")
379 list(APPEND ${lstopt} "${word}")
386 # remove all matching elements from the list
387 macro(ocv_list_filterout lst regex)
388 foreach(item ${${lst}})
389 if(item MATCHES "${regex}")
390 list(REMOVE_ITEM ${lst} "${item}")
396 # stable & safe duplicates removal macro
397 macro(ocv_list_unique __lst)
399 list(REMOVE_DUPLICATES ${__lst})
404 # safe list reversal macro
405 macro(ocv_list_reverse __lst)
407 list(REVERSE ${__lst})
412 # safe list sorting macro
413 macro(ocv_list_sort __lst)
420 # add prefix to each item in the list
421 macro(ocv_list_add_prefix LST PREFIX)
423 foreach(item ${${LST}})
424 list(APPEND __tmp "${PREFIX}${item}")
431 # add suffix to each item in the list
432 macro(ocv_list_add_suffix LST SUFFIX)
434 foreach(item ${${LST}})
435 list(APPEND __tmp "${item}${SUFFIX}")
442 # gets and removes the first element from list
443 macro(ocv_list_pop_front LST VAR)
445 list(GET ${LST} 0 ${VAR})
446 list(REMOVE_AT ${LST} 0)
453 # simple regex escaping routine (does not cover all cases!!!)
454 macro(ocv_regex_escape var regex)
455 string(REGEX REPLACE "([+.*^$])" "\\\\1" ${var} "${regex}")
459 # convert list of paths to full paths
460 macro(ocv_convert_to_full_paths VAR)
463 foreach(path ${${VAR}})
464 get_filename_component(${VAR} "${path}" ABSOLUTE)
465 list(APPEND __tmp "${${VAR}}")
473 # convert list of paths to libraries names without lib prefix
474 macro(ocv_convert_to_lib_name var)
476 foreach(path ${ARGN})
477 get_filename_component(__tmp_name "${path}" NAME_WE)
478 string(REGEX REPLACE "^lib" "" __tmp_name ${__tmp_name})
479 list(APPEND __tmp "${__tmp_name}")
487 # add install command
488 function(ocv_install_target)
489 install(TARGETS ${ARGN})
495 if(NOT DEFINED __target)
498 if(isPackage EQUAL 1)
499 set(__package "${e}")
502 if(e STREQUAL "EXPORT")
507 if(DEFINED __package)
508 list(APPEND ${__package}_TARGETS ${__target})
509 set(${__package}_TARGETS "${${__package}_TARGETS}" CACHE INTERNAL "List of ${__package} targets")
512 if(INSTALL_CREATE_DISTRIB)
513 if(MSVC AND NOT BUILD_SHARED_LIBS)
514 set(__target "${ARGV0}")
524 if(isArchive EQUAL 1 AND e STREQUAL "DESTINATION")
527 if(e STREQUAL "ARCHIVE")
534 # message(STATUS "Process ${__target} dst=${__dst}...")
536 get_target_property(fname ${__target} LOCATION_DEBUG)
537 if(fname MATCHES "\\.lib$")
538 string(REGEX REPLACE "\\.lib$" ".pdb" fname "${fname}")
539 install(FILES ${fname} DESTINATION ${__dst} CONFIGURATIONS Debug)
542 get_target_property(fname ${__target} LOCATION_RELEASE)
543 if(fname MATCHES "\\.lib$")
544 string(REGEX REPLACE "\\.lib$" ".pdb" fname "${fname}")
545 install(FILES ${fname} DESTINATION ${__dst} CONFIGURATIONS Release)
553 # read set of version defines from the header file
554 macro(ocv_parse_header FILENAME FILE_VAR)
556 set(__parnet_scope OFF)
558 foreach(name ${ARGN})
559 if("${name}" STREQUAL "PARENT_SCOPE")
560 set(__parnet_scope ON)
561 elseif("${name}" STREQUAL "CACHE")
564 set(vars_regex "${vars_regex}|${name}")
566 set(vars_regex "${name}")
569 if(EXISTS "${FILENAME}")
570 file(STRINGS "${FILENAME}" ${FILE_VAR} REGEX "#define[ \t]+(${vars_regex})[ \t]+[0-9]+" )
574 foreach(name ${ARGN})
575 if(NOT "${name}" STREQUAL "PARENT_SCOPE" AND NOT "${name}" STREQUAL "CACHE")
577 if(${FILE_VAR} MATCHES ".+[ \t]${name}[ \t]+([0-9]+).*")
578 string(REGEX REPLACE ".+[ \t]${name}[ \t]+([0-9]+).*" "\\1" ${name} "${${FILE_VAR}}")
583 set(${name} ${${name}} CACHE INTERNAL "${name} parsed from ${FILENAME}" FORCE)
584 elseif(__parnet_scope)
585 set(${name} "${${name}}" PARENT_SCOPE)
594 # read single version define from the header file
595 macro(ocv_parse_header2 LIBNAME HDR_PATH VARNAME)
596 ocv_clear_vars(${LIBNAME}_VERSION_MAJOR
597 ${LIBNAME}_VERSION_MAJOR
598 ${LIBNAME}_VERSION_MINOR
599 ${LIBNAME}_VERSION_PATCH
600 ${LIBNAME}_VERSION_TWEAK
601 ${LIBNAME}_VERSION_STRING)
603 if(EXISTS "${HDR_PATH}")
604 file(STRINGS "${HDR_PATH}" ${LIBNAME}_H REGEX "^#define[ \t]+${VARNAME}[ \t]+\"[^\"]*\".*$" LIMIT_COUNT 1)
608 string(REGEX REPLACE "^.*[ \t]${VARNAME}[ \t]+\"([0-9]+).*$" "\\1" ${LIBNAME}_VERSION_MAJOR "${${LIBNAME}_H}")
609 string(REGEX REPLACE "^.*[ \t]${VARNAME}[ \t]+\"[0-9]+\\.([0-9]+).*$" "\\1" ${LIBNAME}_VERSION_MINOR "${${LIBNAME}_H}")
610 string(REGEX REPLACE "^.*[ \t]${VARNAME}[ \t]+\"[0-9]+\\.[0-9]+\\.([0-9]+).*$" "\\1" ${LIBNAME}_VERSION_PATCH "${${LIBNAME}_H}")
611 set(${LIBNAME}_VERSION_MAJOR ${${LIBNAME}_VERSION_MAJOR} ${ARGN})
612 set(${LIBNAME}_VERSION_MINOR ${${LIBNAME}_VERSION_MINOR} ${ARGN})
613 set(${LIBNAME}_VERSION_PATCH ${${LIBNAME}_VERSION_PATCH} ${ARGN})
614 set(${LIBNAME}_VERSION_STRING "${${LIBNAME}_VERSION_MAJOR}.${${LIBNAME}_VERSION_MINOR}.${${LIBNAME}_VERSION_PATCH}")
616 # append a TWEAK version if it exists:
617 set(${LIBNAME}_VERSION_TWEAK "")
618 if("${${LIBNAME}_H}" MATCHES "^.*[ \t]${VARNAME}[ \t]+\"[0-9]+\\.[0-9]+\\.[0-9]+\\.([0-9]+).*$")
619 set(${LIBNAME}_VERSION_TWEAK "${CMAKE_MATCH_1}" ${ARGN})
621 if(${LIBNAME}_VERSION_TWEAK)
622 set(${LIBNAME}_VERSION_STRING "${${LIBNAME}_VERSION_STRING}.${${LIBNAME}_VERSION_TWEAK}" ${ARGN})
624 set(${LIBNAME}_VERSION_STRING "${${LIBNAME}_VERSION_STRING}" ${ARGN})
629 # read single version info from the pkg file
630 macro(ocv_parse_pkg LIBNAME PKG_PATH SCOPE)
631 if(EXISTS "${PKG_PATH}/${LIBNAME}.pc")
632 file(STRINGS "${PKG_PATH}/${LIBNAME}.pc" line_to_parse REGEX "^Version:[ \t]+[0-9.]*.*$" LIMIT_COUNT 1)
633 STRING(REGEX REPLACE ".*Version: ([^ ]+).*" "\\1" ALIASOF_${LIBNAME}_VERSION "${line_to_parse}" )
637 ################################################################################################
638 # short command to setup source group
639 function(ocv_source_group group)
640 cmake_parse_arguments(SG "" "DIRBASE" "GLOB;GLOB_RECURSE;FILES" ${ARGN})
643 list(APPEND files ${SG_FILES})
646 file(GLOB srcs ${SG_GLOB})
647 list(APPEND files ${srcs})
650 file(GLOB_RECURSE srcs ${SG_GLOB_RECURSE})
651 list(APPEND files ${srcs})
655 file(RELATIVE_PATH fpart "${SG_DIRBASE}" "${f}")
656 if(fpart MATCHES "^\\.\\.")
657 message(AUTHOR_WARNING "Can't detect subpath for source_group command: Group=${group} FILE=${f} DIRBASE=${SG_DIRBASE}")
660 get_filename_component(fpart "${fpart}" PATH)
662 set(fpart "/${fpart}") # add '/'
663 string(REPLACE "/" "\\" fpart "${fpart}")
666 source_group("${group}${fpart}" FILES ${f})
669 source_group(${group} FILES ${files})