forked from opencv/opencv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OpenCVUtils.cmake
1993 lines (1824 loc) · 67.5 KB
/
OpenCVUtils.cmake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
if(COMMAND ocv_cmake_dump_vars) # include guard
return()
endif()
include(CMakeParseArguments)
# Debugging function
function(ocv_cmake_dump_vars)
set(OPENCV_SUPPRESS_DEPRECATIONS 1) # suppress deprecation warnings from variable_watch() guards
get_cmake_property(__variableNames VARIABLES)
cmake_parse_arguments(DUMP "FORCE" "TOFILE" "" ${ARGN})
# avoid generation of excessive logs with "--trace" or "--trace-expand" parameters
# Note: `-DCMAKE_TRACE_MODE=1` should be passed to CMake through command line. It is not a CMake buildin variable for now (2020-12)
# Use `cmake . -UCMAKE_TRACE_MODE` to remove this variable from cache
if(CMAKE_TRACE_MODE AND NOT DUMP_FORCE)
if(DUMP_TOFILE)
file(WRITE ${CMAKE_BINARY_DIR}/${DUMP_TOFILE} "Skipped due to enabled CMAKE_TRACE_MODE")
else()
message(AUTHOR_WARNING "ocv_cmake_dump_vars() is skipped due to enabled CMAKE_TRACE_MODE")
endif()
return()
endif()
set(regex "${DUMP_UNPARSED_ARGUMENTS}")
string(TOLOWER "${regex}" regex_lower)
set(__VARS "")
foreach(__variableName ${__variableNames})
string(TOLOWER "${__variableName}" __variableName_lower)
if((__variableName MATCHES "${regex}" OR __variableName_lower MATCHES "${regex_lower}")
AND NOT __variableName_lower MATCHES "^__")
get_property(__value VARIABLE PROPERTY "${__variableName}")
set(__VARS "${__VARS}${__variableName}=${__value}\n")
endif()
endforeach()
if(DUMP_TOFILE)
file(WRITE ${CMAKE_BINARY_DIR}/${DUMP_TOFILE} "${__VARS}")
else()
message(AUTHOR_WARNING "${__VARS}")
endif()
endfunction()
#
# CMake script hooks support
#
option(OPENCV_DUMP_HOOKS_FLOW "Dump called OpenCV hooks" OFF)
macro(ocv_cmake_hook_append hook_name)
set(__var_name "__OPENCV_CMAKE_HOOKS_${hook_name}")
set(__value "${${__var_name}}")
message(STATUS "Registering hook '${hook_name}': ${ARGN}")
list(APPEND __value ${ARGN})
set(${__var_name} "${__value}" CACHE INTERNAL "")
endmacro()
macro(ocv_cmake_hook hook_name)
set(__var_name "__OPENCV_CMAKE_HOOKS_${hook_name}")
if(OPENCV_DUMP_HOOKS_FLOW)
message(STATUS "Hook ${hook_name} ...")
endif()
foreach(__hook ${${__var_name}})
#message(STATUS "Hook ${hook_name}: calling '${__hook}' ...")
if(COMMAND "${__hook}")
message(FATAL_ERROR "Indirect calling of CMake commands is not supported yet")
else()
include("${__hook}")
endif()
endforeach()
endmacro()
macro(ocv_cmake_reset_hooks)
get_cmake_property(__variableNames VARIABLES)
foreach(__variableName ${__variableNames})
if(__variableName MATCHES "^__OPENCV_CMAKE_HOOKS_")
unset(${__variableName})
unset(${__variableName} CACHE)
endif()
endforeach()
endmacro()
macro(ocv_cmake_hook_register_dir dir)
file(GLOB hook_files RELATIVE "${dir}" "${dir}/*.cmake")
foreach(f ${hook_files})
if(f MATCHES "^(.+)\\.cmake$")
set(hook_name "${CMAKE_MATCH_1}")
ocv_cmake_hook_append(${hook_name} "${dir}/${f}")
endif()
endforeach()
endmacro()
function(ocv_cmake_eval var_name)
if(DEFINED ${var_name})
file(WRITE "${CMAKE_BINARY_DIR}/CMakeCommand-${var_name}.cmake" ${${var_name}})
include("${CMAKE_BINARY_DIR}/CMakeCommand-${var_name}.cmake")
endif()
if(";${ARGN};" MATCHES ";ONCE;")
unset(${var_name} CACHE)
endif()
endfunction()
macro(ocv_cmake_configure file_name var_name)
file(READ "${file_name}" __config)
string(CONFIGURE "${__config}" ${var_name} ${ARGN})
endmacro()
macro(ocv_update VAR)
if(NOT DEFINED ${VAR})
if("x${ARGN}" STREQUAL "x")
set(${VAR} "")
else()
set(${VAR} ${ARGN})
endif()
else()
#ocv_debug_message("Preserve old value for ${VAR}: ${${VAR}}")
endif()
endmacro()
function(_ocv_access_removed_variable VAR ACCESS)
if(ACCESS STREQUAL "MODIFIED_ACCESS")
set(OPENCV_SUPPRESS_MESSAGE_REMOVED_VARIABLE_${VAR} 1 PARENT_SCOPE)
return()
endif()
if(ACCESS MATCHES "UNKNOWN_.*"
AND NOT OPENCV_SUPPRESS_MESSAGE_REMOVED_VARIABLE
AND NOT OPENCV_SUPPRESS_MESSAGE_REMOVED_VARIABLE_${VAR}
)
message(WARNING "OpenCV: Variable has been removed from CMake scripts: ${VAR}")
set(OPENCV_SUPPRESS_MESSAGE_REMOVED_VARIABLE_${VAR} 1 PARENT_SCOPE) # suppress similar messages
endif()
endfunction()
macro(ocv_declare_removed_variable VAR)
if(NOT DEFINED ${VAR}) # don't hit external variables
variable_watch(${VAR} _ocv_access_removed_variable)
endif()
endmacro()
macro(ocv_declare_removed_variables)
foreach(_var ${ARGN})
ocv_declare_removed_variable(${_var})
endforeach()
endmacro()
# Search packages for the host system instead of packages for the target system
# in case of cross compilation these macros should be defined by the toolchain file
if(NOT COMMAND find_host_package)
macro(find_host_package)
find_package(${ARGN})
endmacro()
endif()
if(NOT COMMAND find_host_program)
macro(find_host_program)
find_program(${ARGN})
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_debug_message)
if(OPENCV_CMAKE_DEBUG_MESSAGES)
string(REPLACE ";" " " __msg "${ARGN}")
message(STATUS "${__msg}")
endif()
endmacro()
macro(ocv_check_environment_variables)
foreach(_var ${ARGN})
if(" ${${_var}}" STREQUAL " " AND DEFINED ENV{${_var}})
set(__value "$ENV{${_var}}")
file(TO_CMAKE_PATH "${__value}" __value) # Assume that we receive paths
set(${_var} "${__value}")
message(STATUS "Update variable ${_var} from environment: ${${_var}}")
endif()
endforeach()
endmacro()
macro(ocv_path_join result_var P1 P2_)
string(REGEX REPLACE "^[/]+" "" P2 "${P2_}")
if("${P1}" STREQUAL "" OR "${P1}" STREQUAL ".")
set(${result_var} "${P2}")
elseif("${P1}" STREQUAL "/")
set(${result_var} "/${P2}")
elseif("${P2}" STREQUAL "")
set(${result_var} "${P1}")
else()
set(${result_var} "${P1}/${P2}")
endif()
string(REPLACE "\\\\" "\\" ${result_var} "${${result_var}}")
string(REPLACE "//" "/" ${result_var} "${${result_var}}")
string(REGEX REPLACE "(^|[/\\])[\\.][/\\]" "\\1" ${result_var} "${${result_var}}")
if("${${result_var}}" STREQUAL "")
set(${result_var} ".")
endif()
#message(STATUS "'${P1}' '${P2_}' => '${${result_var}}'")
endmacro()
# Used to parse Android SDK 'source.properties' files
# File lines format:
# - '<var_name>=<value>' (with possible 'space' symbols around '=')
# - '#<any comment>'
# Parsed values are saved into CMake variables:
# - '${var_prefix}_${var_name}'
# Flags:
# - 'CACHE_VAR <var1> <var2>' - put these properties into CMake internal cache
# - 'MSG_PREFIX <msg>' - prefix string for emitted messages
# - flag 'VALIDATE' - emit messages about missing values from required cached variables
# - flag 'WARNING' - emit CMake WARNING instead of STATUS messages
function(ocv_parse_properties_file file var_prefix)
cmake_parse_arguments(PARSE_PROPERTIES_PARAM "VALIDATE;WARNING" "" "CACHE_VAR;MSG_PREFIX" ${ARGN})
set(__msg_type STATUS)
if(PARSE_PROPERTIES_PARAM_WARNING)
set(__msg_type WARNING)
endif()
if(EXISTS "${file}")
set(SOURCE_PROPERTIES_REGEX "^[ ]*([^=:\n\"' ]+)[ ]*=[ ]*(.*)$")
file(STRINGS "${file}" SOURCE_PROPERTIES_LINES REGEX "^[ ]*[^#].*$")
foreach(line ${SOURCE_PROPERTIES_LINES})
if(line MATCHES "${SOURCE_PROPERTIES_REGEX}")
set(__name "${CMAKE_MATCH_1}")
set(__value "${CMAKE_MATCH_2}")
string(REGEX REPLACE "[^a-zA-Z0-9_]" "_" __name ${__name})
if(";${PARSE_PROPERTIES_PARAM_CACHE_VAR};" MATCHES ";${__name};")
set(${var_prefix}_${__name} "${__value}" CACHE INTERNAL "from ${file}")
else()
set(${var_prefix}_${__name} "${__value}" PARENT_SCOPE)
endif()
else()
message(${__msg_type} "${PARSE_PROPERTIES_PARAM_MSG_PREFIX}Can't parse source property: '${line}' (from ${file})")
endif()
endforeach()
if(PARSE_PROPERTIES_PARAM_VALIDATE)
set(__missing "")
foreach(__name ${PARSE_PROPERTIES_PARAM_CACHE_VAR})
if(NOT DEFINED ${var_prefix}_${__name})
list(APPEND __missing ${__name})
endif()
endforeach()
if(__missing)
message(${__msg_type} "${PARSE_PROPERTIES_PARAM_MSG_PREFIX}Can't read properties '${__missing}' from '${file}'")
endif()
endif()
else()
message(${__msg_type} "${PARSE_PROPERTIES_PARAM_MSG_PREFIX}Can't find file: ${file}")
endif()
endfunction()
# 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()
# check if "sub" (file or dir) is below "dir"
function(ocv_is_subdir res dir sub )
get_filename_component(dir "${dir}" ABSOLUTE)
get_filename_component(sub "${sub}" ABSOLUTE)
file(TO_CMAKE_PATH "${dir}" dir)
file(TO_CMAKE_PATH "${sub}" sub)
set(dir "${dir}/")
string(LENGTH "${dir}" len)
string(LENGTH "${sub}" len_sub)
if(NOT len GREATER len_sub)
string(SUBSTRING "${sub}" 0 ${len} prefix)
endif()
if(prefix AND prefix STREQUAL dir)
set(${res} TRUE PARENT_SCOPE)
else()
set(${res} FALSE PARENT_SCOPE)
endif()
endfunction()
function(ocv_is_opencv_directory result_var dir)
set(result FALSE)
foreach(parent ${OpenCV_SOURCE_DIR} ${OpenCV_BINARY_DIR} ${OPENCV_EXTRA_MODULES_PATH})
ocv_is_subdir(result "${parent}" "${dir}")
if(result)
break()
endif()
endforeach()
set(${result_var} ${result} PARENT_SCOPE)
endfunction()
# adds include directories in such a 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})
ocv_is_opencv_directory(__is_opencv_dir "${dir}")
if(__is_opencv_dir)
list(APPEND __add_before "${dir}")
elseif(((CV_GCC AND NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS "6.0") OR CV_CLANG) AND
dir MATCHES "/usr/include$")
# workaround for GCC 6.x bug
else()
include_directories(AFTER SYSTEM "${dir}")
endif()
endforeach()
include_directories(BEFORE ${__add_before})
endfunction()
function(ocv_append_target_property target prop)
get_target_property(val ${target} ${prop})
if(val)
set(val "${val} ${ARGN}")
set_target_properties(${target} PROPERTIES ${prop} "${val}")
else()
set_target_properties(${target} PROPERTIES ${prop} "${ARGN}")
endif()
endfunction()
if(DEFINED OPENCV_DEPENDANT_TARGETS_LIST)
foreach(v ${OPENCV_DEPENDANT_TARGETS_LIST})
unset(${v} CACHE)
endforeach()
unset(OPENCV_DEPENDANT_TARGETS_LIST CACHE)
endif()
function(ocv_append_dependant_targets target)
#ocv_debug_message("ocv_append_dependant_targets(${target} ${ARGN})")
_ocv_fix_target(target)
list(FIND OPENCV_DEPENDANT_TARGETS_LIST "OPENCV_DEPENDANT_TARGETS_${target}" __id)
if(__id EQUAL -1)
list(APPEND OPENCV_DEPENDANT_TARGETS_LIST "OPENCV_DEPENDANT_TARGETS_${target}")
list(SORT OPENCV_DEPENDANT_TARGETS_LIST)
set(OPENCV_DEPENDANT_TARGETS_LIST "${OPENCV_DEPENDANT_TARGETS_LIST}" CACHE INTERNAL "")
endif()
set(OPENCV_DEPENDANT_TARGETS_${target} "${OPENCV_DEPENDANT_TARGETS_${target}};${ARGN}" CACHE INTERNAL "" FORCE)
endfunction()
# adds include directories in such a way that directories from the OpenCV source tree go first
function(ocv_target_include_directories target)
#ocv_debug_message("ocv_target_include_directories(${target} ${ARGN})")
_ocv_fix_target(target)
set(__params "")
if(CV_GCC AND NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS "6.0" AND
";${ARGN};" MATCHES "/usr/include;")
return() # workaround for GCC 6.x bug
endif()
set(__params "")
set(__system_params "")
set(__var_name __params)
foreach(dir ${ARGN})
if("${dir}" STREQUAL "SYSTEM")
set(__var_name __system_params)
else()
get_filename_component(__abs_dir "${dir}" ABSOLUTE)
ocv_is_opencv_directory(__is_opencv_dir "${dir}")
if(__is_opencv_dir)
list(APPEND ${__var_name} "${__abs_dir}")
else()
list(APPEND ${__var_name} "${dir}")
endif()
endif()
endforeach()
if(HAVE_CUDA OR CMAKE_VERSION VERSION_LESS 2.8.11)
include_directories(${__params})
include_directories(SYSTEM ${__system_params})
else()
if(TARGET ${target})
if(__params)
target_include_directories(${target} PRIVATE ${__params})
if(OPENCV_DEPENDANT_TARGETS_${target})
foreach(t ${OPENCV_DEPENDANT_TARGETS_${target}})
target_include_directories(${t} PRIVATE ${__params})
endforeach()
endif()
endif()
if(__system_params)
target_include_directories(${target} SYSTEM PRIVATE ${__system_params})
if(OPENCV_DEPENDANT_TARGETS_${target})
foreach(t ${OPENCV_DEPENDANT_TARGETS_${target}})
target_include_directories(${t} SYSTEM PRIVATE ${__system_params})
endforeach()
endif()
endif()
else()
if(__params)
set(__new_inc ${OCV_TARGET_INCLUDE_DIRS_${target}})
list(APPEND __new_inc ${__params})
set(OCV_TARGET_INCLUDE_DIRS_${target} "${__new_inc}" CACHE INTERNAL "")
endif()
if(__system_params)
set(__new_inc ${OCV_TARGET_INCLUDE_SYSTEM_DIRS_${target}})
list(APPEND __new_inc ${__system_params})
set(OCV_TARGET_INCLUDE_SYSTEM_DIRS_${target} "${__new_inc}" CACHE INTERNAL "")
endif()
endif()
endif()
endfunction()
# clears all passed variables
macro(ocv_clear_vars)
foreach(_var ${ARGN})
unset(${_var})
unset(${_var} CACHE)
endforeach()
endmacro()
# Clears passed variables with INTERNAL type from CMake cache
macro(ocv_clear_internal_cache_vars)
foreach(_var ${ARGN})
get_property(_propertySet CACHE ${_var} PROPERTY TYPE SET)
if(_propertySet)
get_property(_type CACHE ${_var} PROPERTY TYPE)
if(_type STREQUAL "INTERNAL")
message("Cleaning INTERNAL cached variable: ${_var}")
unset(${_var} CACHE)
endif()
endif()
endforeach()
unset(_propertySet)
unset(_type)
endmacro()
set(OCV_COMPILER_FAIL_REGEX
"argument .* is not valid" # GCC 9+ (including support of unicode quotes)
"command[- ]line option .* is valid for .* but not for C\\+\\+" # GNU
"command[- ]line option .* is valid for .* but not for C" # GNU
"unrecognized .*option" # GNU
"unknown .*option" # Clang
"ignoring unknown option" # MSVC
"warning D9002" # MSVC, any lang
"option .*not supported" # Intel
"[Uu]nknown option" # HP
"[Ww]arning: [Oo]ption" # SunPro
"command option .* is not recognized" # XL
"not supported in this configuration, ignored" # AIX (';' is replaced with ',')
"File with unknown suffix passed to linker" # PGI
"WARNING: unknown flag:" # Open64
)
MACRO(ocv_check_compiler_flag LANG FLAG RESULT)
set(_fname "${ARGN}")
if(NOT DEFINED ${RESULT})
if(_fname)
# nothing
elseif("_${LANG}_" MATCHES "_CXX_")
set(_fname "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.cxx")
if("${CMAKE_CXX_FLAGS} ${FLAG} " MATCHES "-Werror " OR "${CMAKE_CXX_FLAGS} ${FLAG} " MATCHES "-Werror=unknown-pragmas ")
FILE(WRITE "${_fname}" "int main() { return 0; }\n")
else()
FILE(WRITE "${_fname}" "#pragma\nint main() { return 0; }\n")
endif()
elseif("_${LANG}_" MATCHES "_C_")
set(_fname "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.c")
if("${CMAKE_C_FLAGS} ${FLAG} " MATCHES "-Werror " OR "${CMAKE_C_FLAGS} ${FLAG} " MATCHES "-Werror=unknown-pragmas ")
FILE(WRITE "${_fname}" "int main(void) { return 0; }\n")
else()
FILE(WRITE "${_fname}" "#pragma\nint main(void) { return 0; }\n")
endif()
elseif("_${LANG}_" MATCHES "_OBJCXX_")
set(_fname "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.mm")
if("${CMAKE_CXX_FLAGS} ${FLAG} " MATCHES "-Werror " OR "${CMAKE_CXX_FLAGS} ${FLAG} " MATCHES "-Werror=unknown-pragmas ")
FILE(WRITE "${_fname}" "int main() { return 0; }\n")
else()
FILE(WRITE "${_fname}" "#pragma\nint main() { return 0; }\n")
endif()
else()
unset(_fname)
endif()
if(_fname)
if(NOT "x${ARGN}" STREQUAL "x")
file(RELATIVE_PATH __msg "${CMAKE_SOURCE_DIR}" "${ARGN}")
set(__msg " (check file: ${__msg})")
else()
set(__msg "")
endif()
if(CMAKE_REQUIRED_LIBRARIES)
set(__link_libs LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
else()
set(__link_libs)
endif()
set(__cmake_flags "")
if(CMAKE_EXE_LINKER_FLAGS) # CMP0056 do this on new CMake
list(APPEND __cmake_flags "-DCMAKE_EXE_LINKER_FLAGS=${CMAKE_EXE_LINKER_FLAGS}")
endif()
# CMP0067 do this on new CMake
if(DEFINED CMAKE_CXX_STANDARD)
list(APPEND __cmake_flags "-DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD}")
endif()
if(DEFINED CMAKE_CXX_STANDARD_REQUIRED)
list(APPEND __cmake_flags "-DCMAKE_CXX_STANDARD_REQUIRED=${CMAKE_CXX_STANDARD_REQUIRED}")
endif()
if(DEFINED CMAKE_CXX_EXTENSIONS)
list(APPEND __cmake_flags "-DCMAKE_CXX_EXTENSIONS=${CMAKE_CXX_EXTENSIONS}")
endif()
MESSAGE(STATUS "Performing Test ${RESULT}${__msg}")
TRY_COMPILE(${RESULT}
"${CMAKE_BINARY_DIR}"
"${_fname}"
CMAKE_FLAGS ${__cmake_flags}
COMPILE_DEFINITIONS "${FLAG}"
${__link_libs}
OUTPUT_VARIABLE OUTPUT)
if(${RESULT})
string(REPLACE ";" "," OUTPUT_LINES "${OUTPUT}")
string(REPLACE "\n" ";" OUTPUT_LINES "${OUTPUT_LINES}")
foreach(_regex ${OCV_COMPILER_FAIL_REGEX})
if(NOT ${RESULT})
break()
endif()
foreach(_line ${OUTPUT_LINES})
if("${_line}" MATCHES "${_regex}")
file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
"Build output check failed:\n"
" Regex: '${_regex}'\n"
" Output line: '${_line}'\n")
set(${RESULT} 0)
break()
endif()
endforeach()
endforeach()
endif()
IF(${RESULT})
SET(${RESULT} 1 CACHE INTERNAL "Test ${RESULT}")
MESSAGE(STATUS "Performing Test ${RESULT} - Success")
ELSE(${RESULT})
MESSAGE(STATUS "Performing Test ${RESULT} - Failed")
SET(${RESULT} "" CACHE INTERNAL "Test ${RESULT}")
file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
"Compilation failed:\n"
" source file: '${_fname}'\n"
" check option: '${FLAG}'\n"
"===== BUILD LOG =====\n"
"${OUTPUT}\n"
"===== END =====\n\n")
ENDIF(${RESULT})
else()
SET(${RESULT} 0)
endif()
endif()
ENDMACRO()
macro(ocv_check_flag_support lang flag varname base_options)
if(CMAKE_BUILD_TYPE)
set(CMAKE_TRY_COMPILE_CONFIGURATION ${CMAKE_BUILD_TYPE})
endif()
if("_${lang}_" MATCHES "_CXX_")
set(_lang CXX)
elseif("_${lang}_" MATCHES "_C_")
set(_lang C)
elseif("_${lang}_" MATCHES "_OBJCXX_")
if(DEFINED CMAKE_OBJCXX_COMPILER) # CMake 3.16+ and enable_language(OBJCXX) call are required
set(_lang OBJCXX)
else()
set(_lang CXX)
endif()
else()
set(_lang ${lang})
endif()
string(TOUPPER "${flag}" ${varname})
string(REGEX REPLACE "^(/|-)" "HAVE_${_lang}_" ${varname} "${${varname}}")
string(REGEX REPLACE " -|-|=| |\\.|," "_" ${varname} "${${varname}}")
if(DEFINED CMAKE_${_lang}_COMPILER)
ocv_check_compiler_flag("${_lang}" "${base_options} ${flag}" ${${varname}} ${ARGN})
endif()
endmacro()
macro(ocv_check_runtime_flag flag result)
set(_fname "${ARGN}")
if(NOT DEFINED ${result})
file(RELATIVE_PATH _rname "${CMAKE_SOURCE_DIR}" "${_fname}")
message(STATUS "Performing Runtime Test ${result} (check file: ${_rname})")
try_run(exec_return compile_result
"${CMAKE_BINARY_DIR}"
"${_fname}"
CMAKE_FLAGS "-DCMAKE_EXE_LINKER_FLAGS=${CMAKE_EXE_LINKER_FLAGS}" # CMP0056 do this on new CMake
COMPILE_DEFINITIONS "${flag}"
OUTPUT_VARIABLE OUTPUT)
if(${compile_result})
if(exec_return EQUAL 0)
set(${result} 1 CACHE INTERNAL "Runtime Test ${result}")
message(STATUS "Performing Runtime Test ${result} - Success")
else()
message(STATUS "Performing Runtime Test ${result} - Failed(${exec_return})")
set(${result} 0 CACHE INTERNAL "Runtime Test ${result}")
endif()
else()
set(${result} 0 CACHE INTERNAL "Runtime Test ${result}")
message(STATUS "Performing Runtime Test ${result} - Compiling Failed")
endif()
if(NOT ${result})
file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
"Runtime Test failed:\n"
" source file: '${_fname}'\n"
" check option: '${flag}'\n"
" exec return: ${exec_return}\n"
"===== BUILD AND RUNTIME LOG =====\n"
"${OUTPUT}\n"
"===== END =====\n\n")
endif()
endif()
endmacro()
# turns off warnings
macro(ocv_warnings_disable)
if(NOT ENABLE_NOISY_WARNINGS)
set(_flag_vars "")
set(_msvc_warnings "")
set(_gxx_warnings "")
set(_icc_warnings "")
foreach(arg ${ARGN})
if(arg MATCHES "^CMAKE_")
list(APPEND _flag_vars ${arg})
elseif(arg MATCHES "^/wd")
list(APPEND _msvc_warnings ${arg})
elseif(arg MATCHES "^-W")
list(APPEND _gxx_warnings ${arg})
elseif(arg MATCHES "^-wd" OR arg MATCHES "^-Qwd" OR arg MATCHES "^/Qwd")
list(APPEND _icc_warnings ${arg})
endif()
endforeach()
if(MSVC AND _msvc_warnings AND _flag_vars)
foreach(var ${_flag_vars})
foreach(warning ${_msvc_warnings})
set(${var} "${${var}} ${warning}")
endforeach()
endforeach()
elseif(((CV_GCC OR CV_CLANG) OR (UNIX AND CV_ICC)) AND _gxx_warnings AND _flag_vars)
foreach(var ${_flag_vars})
foreach(warning ${_gxx_warnings})
if(NOT warning MATCHES "^-Wno-")
string(REGEX REPLACE "(^|[ ]+)${warning}(=[^ ]*)?([ ]+|$)" " " ${var} "${${var}}")
string(REPLACE "-W" "-Wno-" warning "${warning}")
endif()
ocv_check_flag_support(${var} "${warning}" _varname "")
if(${_varname})
set(${var} "${${var}} ${warning}")
endif()
endforeach()
endforeach()
endif()
if(CV_ICC AND _icc_warnings AND _flag_vars)
foreach(var ${_flag_vars})
foreach(warning ${_icc_warnings})
if(UNIX)
string(REPLACE "-Qwd" "-wd" warning "${warning}")
else()
string(REPLACE "-wd" "-Qwd" warning "${warning}")
endif()
ocv_check_flag_support(${var} "${warning}" _varname "")
if(${_varname})
set(${var} "${${var}} ${warning}")
endif()
endforeach()
endforeach()
endif()
unset(_flag_vars)
unset(_msvc_warnings)
unset(_gxx_warnings)
unset(_icc_warnings)
endif(NOT ENABLE_NOISY_WARNINGS)
endmacro()
macro(ocv_append_source_file_compile_definitions source)
get_source_file_property(_value "${source}" COMPILE_DEFINITIONS)
if(_value)
set(_value ${_value} ${ARGN})
else()
set(_value ${ARGN})
endif()
set_source_files_properties("${source}" PROPERTIES COMPILE_DEFINITIONS "${_value}")
endmacro()
macro(add_apple_compiler_options the_module)
ocv_check_flag_support(OBJCXX "-fobjc-exceptions" HAVE_OBJC_EXCEPTIONS "")
if(HAVE_OBJC_EXCEPTIONS)
foreach(source ${OPENCV_MODULE_${the_module}_SOURCES})
if("${source}" MATCHES "\\.mm$")
get_source_file_property(flags "${source}" COMPILE_FLAGS)
if(flags)
set(flags "${_flags} -fobjc-exceptions")
else()
set(flags "-fobjc-exceptions")
endif()
set_source_files_properties("${source}" PROPERTIES COMPILE_FLAGS "${flags}")
endif()
endforeach()
endif()
endmacro()
# Provides an option that the user can optionally select.
# Can accept condition to control when option is available for user.
# Usage:
# option(<option_variable>
# "help string describing the option"
# <initial value or boolean expression>
# [VISIBLE_IF <condition>]
# [VERIFY <condition>])
macro(OCV_OPTION variable description value)
set(__value ${value})
set(__condition "")
set(__verification)
set(__varname "__value")
foreach(arg ${ARGN})
if(arg STREQUAL "IF" OR arg STREQUAL "if" OR arg STREQUAL "VISIBLE_IF")
set(__varname "__condition")
elseif(arg STREQUAL "VERIFY")
set(__varname "__verification")
else()
list(APPEND ${__varname} ${arg})
endif()
endforeach()
unset(__varname)
if(__condition STREQUAL "")
set(__condition 2 GREATER 1)
endif()
if(${__condition})
if(__value MATCHES ";")
if(${__value})
option(${variable} "${description}" ON)
else()
option(${variable} "${description}" OFF)
endif()
elseif(DEFINED ${__value})
if(${__value})
option(${variable} "${description}" ON)
else()
option(${variable} "${description}" OFF)
endif()
else()
option(${variable} "${description}" ${__value})
endif()
else()
if(DEFINED ${variable} AND "${${variable}}" # emit warnings about turned ON options only.
AND NOT (OPENCV_HIDE_WARNING_UNSUPPORTED_OPTION OR "$ENV{OPENCV_HIDE_WARNING_UNSUPPORTED_OPTION}")
)
message(WARNING "Unexpected option: ${variable} (=${${variable}})\nCondition: IF (${__condition})")
endif()
if(OPENCV_UNSET_UNSUPPORTED_OPTION)
unset(${variable} CACHE)
endif()
endif()
if(__verification)
set(OPENCV_VERIFY_${variable} "${__verification}") # variable containing condition to verify
list(APPEND OPENCV_VERIFICATIONS "${variable}") # list of variable names (WITH_XXX;WITH_YYY;...)
endif()
unset(__condition)
unset(__value)
endmacro()
# Check that each variable stored in OPENCV_VERIFICATIONS list
# is consistent with actual detection result (stored as condition in OPENCV_VERIFY_...) variables
function(ocv_verify_config)
set(broken_options)
foreach(var ${OPENCV_VERIFICATIONS})
set(evaluated FALSE)
if(${OPENCV_VERIFY_${var}})
set(evaluated TRUE)
endif()
status("Verifying ${var}=${${var}} => '${OPENCV_VERIFY_${var}}'=${evaluated}")
if (${var} AND NOT evaluated)
list(APPEND broken_options ${var})
message(WARNING
"Option ${var} is enabled but corresponding dependency "
"have not been found: \"${OPENCV_VERIFY_${var}}\" is FALSE")
elseif(NOT ${var} AND evaluated)
list(APPEND broken_options ${var})
message(WARNING
"Option ${var} is disabled or unset but corresponding dependency "
"have been explicitly turned on: \"${OPENCV_VERIFY_${var}}\" is TRUE")
endif()
endforeach()
if(broken_options)
string(REPLACE ";" "\n" broken_options "${broken_options}")
message(FATAL_ERROR
"Some dependencies have not been found or have been forced, "
"unset ENABLE_CONFIG_VERIFICATION option to ignore these failures "
"or change following options:\n${broken_options}")
endif()
endfunction()
# Usage: ocv_append_build_options(HIGHGUI FFMPEG)
macro(ocv_append_build_options var_prefix pkg_prefix)
foreach(suffix INCLUDE_DIRS LIBRARIES LIBRARY_DIRS LINK_LIBRARIES)
if(${pkg_prefix}_${suffix})
list(APPEND ${var_prefix}_${suffix} ${${pkg_prefix}_${suffix}})
list(REMOVE_DUPLICATES ${var_prefix}_${suffix})
endif()
endforeach()
endmacro()
function(ocv_append_source_files_cxx_compiler_options files_var)
set(__flags "${ARGN}")
ocv_check_flag_support(CXX "${__flags}" __HAVE_COMPILER_OPTIONS_VAR "")
if(${__HAVE_COMPILER_OPTIONS_VAR})
foreach(source ${${files_var}})
if("${source}" MATCHES "\\.(cpp|cc|cxx)$")
get_source_file_property(flags "${source}" COMPILE_FLAGS)
if(flags)
set(flags "${flags} ${__flags}")
else()
set(flags "${__flags}")
endif()
set_source_files_properties("${source}" PROPERTIES COMPILE_FLAGS "${flags}")
endif()
endforeach()
endif()
endfunction()
# Usage is similar to CMake 'pkg_check_modules' command
# It additionally controls HAVE_${define} and ${define}_${modname}_FOUND variables
macro(ocv_check_modules define)
unset(HAVE_${define})
foreach(m ${ARGN})
if (m MATCHES "(.*[^><])(>=|=|<=)(.*)")
set(__modname "${CMAKE_MATCH_1}")
else()
set(__modname "${m}")
endif()
unset(${define}_${__modname}_FOUND)
endforeach()
if(PKG_CONFIG_FOUND OR PkgConfig_FOUND)
pkg_check_modules(${define} ${ARGN})
endif()
if(${define}_FOUND)
set(HAVE_${define} 1)
endif()
foreach(m ${ARGN})
if (m MATCHES "(.*[^><])(>=|=|<=)(.*)")
set(__modname "${CMAKE_MATCH_1}")
else()
set(__modname "${m}")
endif()
if(NOT DEFINED ${define}_${__modname}_FOUND AND ${define}_FOUND)
set(${define}_${__modname}_FOUND 1)
endif()
endforeach()
if(${define}_FOUND AND ${define}_LIBRARIES)
if(${define}_LINK_LIBRARIES_XXXXX) # CMake 3.12+: https://gitlab.kitware.com/cmake/cmake/merge_requests/2068
set(${define}_LIBRARIES "${${define}_LINK_LIBRARIES}" CACHE INTERNAL "")
else()
unset(_libs) # absolute paths
unset(_libs_paths) # -L args
foreach(flag ${${define}_LDFLAGS})
if(flag MATCHES "^-L(.*)")
list(APPEND _libs_paths ${CMAKE_MATCH_1})
elseif(IS_ABSOLUTE "${flag}"
OR flag STREQUAL "-lstdc++"
)
list(APPEND _libs "${flag}")
elseif(flag MATCHES "^-l(.*)")
set(_lib "${CMAKE_MATCH_1}")
if(_libs_paths)
find_library(pkgcfg_lib_${define}_${_lib} NAMES ${_lib}
HINTS ${_libs_paths} NO_DEFAULT_PATH)
endif()
find_library(pkgcfg_lib_${define}_${_lib} NAMES ${_lib})
mark_as_advanced(pkgcfg_lib_${define}_${_lib})
if(pkgcfg_lib_${define}_${_lib})
list(APPEND _libs "${pkgcfg_lib_${define}_${_lib}}")
else()
message(WARNING "ocv_check_modules(${define}): can't find library '${_lib}'. Specify 'pkgcfg_lib_${define}_${_lib}' manually")
list(APPEND _libs "${_lib}")
endif()
else()
# -pthread
#message(WARNING "ocv_check_modules(${define}): unknown LDFLAG '${flag}'")
endif()
endforeach()
set(${define}_LINK_LIBRARIES "${_libs}")
set(${define}_LIBRARIES "${_libs}" CACHE INTERNAL "")
unset(_lib)
unset(_libs)
unset(_libs_paths)
endif()
endif()
endmacro()
if(NOT DEFINED CMAKE_ARGC) # Guard CMake standalone invocations
# Use this option carefully, CMake's install() will install symlinks instead of real files
# It is fine for development, but should not be used by real installations
set(__symlink_default OFF) # preprocessing is required for old CMake like 2.8.12
if(DEFINED ENV{BUILD_USE_SYMLINKS})
set(__symlink_default $ENV{BUILD_USE_SYMLINKS})
endif()
OCV_OPTION(BUILD_USE_SYMLINKS "Use symlinks instead of files copying during build (and !!INSTALL!!)" (${__symlink_default}) IF (UNIX OR DEFINED __symlink_default))
if(CMAKE_VERSION VERSION_LESS "3.2")
macro(ocv_cmake_byproducts var_name)
set(${var_name}) # nothing
endmacro()
else()
macro(ocv_cmake_byproducts var_name)
set(${var_name} BYPRODUCTS ${ARGN})
endmacro()
endif()
set(OPENCV_DEPHELPER "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/dephelper" CACHE INTERNAL "")
file(MAKE_DIRECTORY ${OPENCV_DEPHELPER})
if(BUILD_USE_SYMLINKS)
set(__file0 "${CMAKE_CURRENT_LIST_FILE}")
set(__file1 "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/symlink_test")
if(NOT IS_SYMLINK "${__file1}")
execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink "${__file0}" "${__file1}"
RESULT_VARIABLE SYMLINK_RESULT)
if(NOT SYMLINK_RESULT EQUAL 0)
file(REMOVE "${__file1}")
endif()
if(NOT IS_SYMLINK "${__file1}")
set(BUILD_USE_SYMLINKS 0 CACHE INTERNAL "")
endif()
endif()
if(NOT BUILD_USE_SYMLINKS)
message(STATUS "Build symlinks are not available (disabled)")
endif()
endif()
set(OPENCV_BUILD_INFO_STR "" CACHE INTERNAL "")
function(ocv_output_status msg)
message(STATUS "${msg}")
string(REPLACE "\\" "\\\\" msg "${msg}")
string(REPLACE "\"" "\\\"" msg "${msg}")
string(REGEX REPLACE "^\n+|\n+$" "" msg "${msg}")
if(msg MATCHES "\n")
message(WARNING "String to be inserted to version_string.inc has an unexpected line break: '${msg}'")
string(REPLACE "\n" "\\n" msg "${msg}")
endif()
set(OPENCV_BUILD_INFO_STR "${OPENCV_BUILD_INFO_STR}\"${msg}\\n\"\n" CACHE INTERNAL "")
endfunction()
macro(ocv_finalize_status)
set(OPENCV_BUILD_INFO_FILE "${CMAKE_BINARY_DIR}/version_string.tmp")
if(EXISTS "${OPENCV_BUILD_INFO_FILE}")
file(READ "${OPENCV_BUILD_INFO_FILE}" __content)
else()
set(__content "")
endif()
if("${__content}" STREQUAL "${OPENCV_BUILD_INFO_STR}")
#message(STATUS "${OPENCV_BUILD_INFO_FILE} contains the same content")
else()
file(WRITE "${OPENCV_BUILD_INFO_FILE}" "${OPENCV_BUILD_INFO_STR}")
endif()
unset(__content)
unset(OPENCV_BUILD_INFO_STR CACHE)
if(NOT OPENCV_SKIP_STATUS_FINALIZATION)
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()
if(UNIX)
install(FILES "${OpenCV_SOURCE_DIR}/platforms/scripts/valgrind.supp"
"${OpenCV_SOURCE_DIR}/platforms/scripts/valgrind_3rdparty.supp"
DESTINATION "${OPENCV_OTHER_INSTALL_PATH}" COMPONENT "dev")
endif()
endmacro()
# Status report function.
# Automatically align right column and selects text based on condition.
# Usage:
# status(<text>)
# status(<heading> <value1> [<value2> ...])
# status(<heading> <condition> THEN <text for TRUE> ELSE <text for FALSE> )
function(status text)
set(status_cond)
set(status_then)
set(status_else)
set(status_current_name "cond")
foreach(arg ${ARGN})
if(arg STREQUAL "THEN")
set(status_current_name "then")