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 HIGHGUI_INCLUDE_DIRS ${${P}})
272 foreach(P "${ALIAS_LIBRARY_DIRS}")
274 list(APPEND HIGHGUI_LIBRARY_DIRS ${${P}})
278 list(APPEND HIGHGUI_LIBRARIES ${${ALIAS_LIBRARIES}})
284 set(OPENCV_BUILD_INFO_FILE "${OpenCV_BINARY_DIR}/version_string.tmp")
285 file(REMOVE "${OPENCV_BUILD_INFO_FILE}")
286 function(ocv_output_status msg)
287 message(STATUS "${msg}")
288 string(REPLACE "\\" "\\\\" msg "${msg}")
289 string(REPLACE "\"" "\\\"" msg "${msg}")
290 file(APPEND "${OPENCV_BUILD_INFO_FILE}" "\"${msg}\\n\"\n")
293 macro(ocv_finalize_status)
294 if(NOT OPENCV_SKIP_STATUS_FINALIZATION)
295 if(TARGET opencv_core)
296 execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different "${OPENCV_BUILD_INFO_FILE}" "${opencv_core_BINARY_DIR}/version_string.inc" OUTPUT_QUIET)
302 # Status report function.
303 # Automatically align right column and selects text based on condition.
306 # status(<heading> <value1> [<value2> ...])
307 # status(<heading> <condition> THEN <text for TRUE> ELSE <text for FALSE> )
308 function(status text)
313 set(status_current_name "cond")
315 if(arg STREQUAL "THEN")
316 set(status_current_name "then")
317 elseif(arg STREQUAL "ELSE")
318 set(status_current_name "else")
320 list(APPEND status_${status_current_name} ${arg})
324 if(DEFINED status_cond)
325 set(status_placeholder_length 32)
326 string(RANDOM LENGTH ${status_placeholder_length} ALPHABET " " status_placeholder)
327 string(LENGTH "${text}" status_text_length)
328 if(status_text_length LESS status_placeholder_length)
329 string(SUBSTRING "${text}${status_placeholder}" 0 ${status_placeholder_length} status_text)
330 elseif(DEFINED status_then OR DEFINED status_else)
331 ocv_output_status("${text}")
332 set(status_text "${status_placeholder}")
334 set(status_text "${text}")
337 if(DEFINED status_then OR DEFINED status_else)
339 string(REPLACE ";" " " status_then "${status_then}")
340 string(REGEX REPLACE "^[ \t]+" "" status_then "${status_then}")
341 ocv_output_status("${status_text} ${status_then}")
343 string(REPLACE ";" " " status_else "${status_else}")
344 string(REGEX REPLACE "^[ \t]+" "" status_else "${status_else}")
345 ocv_output_status("${status_text} ${status_else}")
348 string(REPLACE ";" " " status_cond "${status_cond}")
349 string(REGEX REPLACE "^[ \t]+" "" status_cond "${status_cond}")
350 ocv_output_status("${status_text} ${status_cond}")
353 ocv_output_status("${text}")
358 # splits cmake libraries list of format "general;item1;debug;item2;release;item3" to two lists
359 macro(ocv_split_libs_list lst lstdbg lstopt)
363 foreach(word ${${lst}})
364 if(word STREQUAL "debug" OR word STREQUAL "optimized")
365 set(perv_keyword ${word})
366 elseif(word STREQUAL "general")
368 elseif(perv_keyword STREQUAL "debug")
369 list(APPEND ${lstdbg} "${word}")
371 elseif(perv_keyword STREQUAL "optimized")
372 list(APPEND ${lstopt} "${word}")
375 list(APPEND ${lstdbg} "${word}")
376 list(APPEND ${lstopt} "${word}")
383 # remove all matching elements from the list
384 macro(ocv_list_filterout lst regex)
385 foreach(item ${${lst}})
386 if(item MATCHES "${regex}")
387 list(REMOVE_ITEM ${lst} "${item}")
393 # stable & safe duplicates removal macro
394 macro(ocv_list_unique __lst)
396 list(REMOVE_DUPLICATES ${__lst})
401 # safe list reversal macro
402 macro(ocv_list_reverse __lst)
404 list(REVERSE ${__lst})
409 # safe list sorting macro
410 macro(ocv_list_sort __lst)
417 # add prefix to each item in the list
418 macro(ocv_list_add_prefix LST PREFIX)
420 foreach(item ${${LST}})
421 list(APPEND __tmp "${PREFIX}${item}")
428 # add suffix to each item in the list
429 macro(ocv_list_add_suffix LST SUFFIX)
431 foreach(item ${${LST}})
432 list(APPEND __tmp "${item}${SUFFIX}")
439 # gets and removes the first element from list
440 macro(ocv_list_pop_front LST VAR)
442 list(GET ${LST} 0 ${VAR})
443 list(REMOVE_AT ${LST} 0)
450 # simple regex escaping routine (does not cover all cases!!!)
451 macro(ocv_regex_escape var regex)
452 string(REGEX REPLACE "([+.*^$])" "\\\\1" ${var} "${regex}")
456 # convert list of paths to full paths
457 macro(ocv_convert_to_full_paths VAR)
460 foreach(path ${${VAR}})
461 get_filename_component(${VAR} "${path}" ABSOLUTE)
462 list(APPEND __tmp "${${VAR}}")
470 # add install command
471 function(ocv_install_target)
472 install(TARGETS ${ARGN})
478 if(NOT DEFINED __target)
481 if(isPackage EQUAL 1)
482 set(__package "${e}")
485 if(e STREQUAL "EXPORT")
490 if(DEFINED __package)
491 list(APPEND ${__package}_TARGETS ${__target})
492 set(${__package}_TARGETS "${${__package}_TARGETS}" CACHE INTERNAL "List of ${__package} targets")
495 if(INSTALL_CREATE_DISTRIB)
496 if(MSVC AND NOT BUILD_SHARED_LIBS)
497 set(__target "${ARGV0}")
507 if(isArchive EQUAL 1 AND e STREQUAL "DESTINATION")
510 if(e STREQUAL "ARCHIVE")
517 # message(STATUS "Process ${__target} dst=${__dst}...")
519 get_target_property(fname ${__target} LOCATION_DEBUG)
520 if(fname MATCHES "\\.lib$")
521 string(REGEX REPLACE "\\.lib$" ".pdb" fname "${fname}")
522 install(FILES ${fname} DESTINATION ${__dst} CONFIGURATIONS Debug)
525 get_target_property(fname ${__target} LOCATION_RELEASE)
526 if(fname MATCHES "\\.lib$")
527 string(REGEX REPLACE "\\.lib$" ".pdb" fname "${fname}")
528 install(FILES ${fname} DESTINATION ${__dst} CONFIGURATIONS Release)
536 # read set of version defines from the header file
537 macro(ocv_parse_header FILENAME FILE_VAR)
539 set(__parnet_scope OFF)
541 foreach(name ${ARGN})
542 if("${name}" STREQUAL "PARENT_SCOPE")
543 set(__parnet_scope ON)
544 elseif("${name}" STREQUAL "CACHE")
547 set(vars_regex "${vars_regex}|${name}")
549 set(vars_regex "${name}")
552 if(EXISTS "${FILENAME}")
553 file(STRINGS "${FILENAME}" ${FILE_VAR} REGEX "#define[ \t]+(${vars_regex})[ \t]+[0-9]+" )
557 foreach(name ${ARGN})
558 if(NOT "${name}" STREQUAL "PARENT_SCOPE" AND NOT "${name}" STREQUAL "CACHE")
560 if(${FILE_VAR} MATCHES ".+[ \t]${name}[ \t]+([0-9]+).*")
561 string(REGEX REPLACE ".+[ \t]${name}[ \t]+([0-9]+).*" "\\1" ${name} "${${FILE_VAR}}")
566 set(${name} ${${name}} CACHE INTERNAL "${name} parsed from ${FILENAME}" FORCE)
567 elseif(__parnet_scope)
568 set(${name} "${${name}}" PARENT_SCOPE)
577 # read single version define from the header file
578 macro(ocv_parse_header2 LIBNAME HDR_PATH VARNAME)
579 ocv_clear_vars(${LIBNAME}_VERSION_MAJOR
580 ${LIBNAME}_VERSION_MAJOR
581 ${LIBNAME}_VERSION_MINOR
582 ${LIBNAME}_VERSION_PATCH
583 ${LIBNAME}_VERSION_TWEAK
584 ${LIBNAME}_VERSION_STRING)
586 if(EXISTS "${HDR_PATH}")
587 file(STRINGS "${HDR_PATH}" ${LIBNAME}_H REGEX "^#define[ \t]+${VARNAME}[ \t]+\"[^\"]*\".*$" LIMIT_COUNT 1)
591 string(REGEX REPLACE "^.*[ \t]${VARNAME}[ \t]+\"([0-9]+).*$" "\\1" ${LIBNAME}_VERSION_MAJOR "${${LIBNAME}_H}")
592 string(REGEX REPLACE "^.*[ \t]${VARNAME}[ \t]+\"[0-9]+\\.([0-9]+).*$" "\\1" ${LIBNAME}_VERSION_MINOR "${${LIBNAME}_H}")
593 string(REGEX REPLACE "^.*[ \t]${VARNAME}[ \t]+\"[0-9]+\\.[0-9]+\\.([0-9]+).*$" "\\1" ${LIBNAME}_VERSION_PATCH "${${LIBNAME}_H}")
594 set(${LIBNAME}_VERSION_MAJOR ${${LIBNAME}_VERSION_MAJOR} ${ARGN})
595 set(${LIBNAME}_VERSION_MINOR ${${LIBNAME}_VERSION_MINOR} ${ARGN})
596 set(${LIBNAME}_VERSION_PATCH ${${LIBNAME}_VERSION_PATCH} ${ARGN})
597 set(${LIBNAME}_VERSION_STRING "${${LIBNAME}_VERSION_MAJOR}.${${LIBNAME}_VERSION_MINOR}.${${LIBNAME}_VERSION_PATCH}")
599 # append a TWEAK version if it exists:
600 set(${LIBNAME}_VERSION_TWEAK "")
601 if("${${LIBNAME}_H}" MATCHES "^.*[ \t]${VARNAME}[ \t]+\"[0-9]+\\.[0-9]+\\.[0-9]+\\.([0-9]+).*$")
602 set(${LIBNAME}_VERSION_TWEAK "${CMAKE_MATCH_1}" ${ARGN})
604 if(${LIBNAME}_VERSION_TWEAK)
605 set(${LIBNAME}_VERSION_STRING "${${LIBNAME}_VERSION_STRING}.${${LIBNAME}_VERSION_TWEAK}" ${ARGN})
607 set(${LIBNAME}_VERSION_STRING "${${LIBNAME}_VERSION_STRING}" ${ARGN})
612 # read single version info from the pkg file
613 macro(ocv_parse_pkg LIBNAME PKG_PATH SCOPE)
614 if(EXISTS "${PKG_PATH}/${LIBNAME}.pc")
615 file(STRINGS "${PKG_PATH}/${LIBNAME}.pc" line_to_parse REGEX "^Version:[ \t]+[0-9.]*.*$" LIMIT_COUNT 1)
616 STRING(REGEX REPLACE ".*Version: ([^ ]+).*" "\\1" ALIASOF_${LIBNAME}_VERSION "${line_to_parse}" )
620 ################################################################################################
621 # short command to setup source group
622 function(ocv_source_group group)
623 cmake_parse_arguments(SG "" "DIRBASE" "GLOB;GLOB_RECURSE;FILES" ${ARGN})
626 list(APPEND files ${SG_FILES})
629 file(GLOB srcs ${SG_GLOB})
630 list(APPEND files ${srcs})
633 file(GLOB_RECURSE srcs ${SG_GLOB_RECURSE})
634 list(APPEND files ${srcs})
638 file(RELATIVE_PATH fpart "${SG_DIRBASE}" "${f}")
639 if(fpart MATCHES "^\\.\\.")
640 message(AUTHOR_WARNING "Can't detect subpath for source_group command: Group=${group} FILE=${f} DIRBASE=${SG_DIRBASE}")
643 get_filename_component(fpart "${fpart}" PATH)
645 set(fpart "/${fpart}") # add '/'
646 string(REPLACE "/" "\\" fpart "${fpart}")
649 source_group("${group}${fpart}" FILES ${f})
652 source_group(${group} FILES ${files})