-
Notifications
You must be signed in to change notification settings - Fork 0
/
QtFeature.cmake
1278 lines (1103 loc) · 49.8 KB
/
QtFeature.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
# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: BSD-3-Clause
include(QtFeatureCommon)
include(CheckCXXCompilerFlag)
function(qt_feature_module_begin)
cmake_parse_arguments(PARSE_ARGV 0 arg
"NO_MODULE;ONLY_EVALUATE_FEATURES"
"LIBRARY;PRIVATE_FILE;PUBLIC_FILE"
"PUBLIC_DEPENDENCIES;PRIVATE_DEPENDENCIES")
_qt_internal_validate_all_args_are_parsed(arg)
if(NOT arg_ONLY_EVALUATE_FEATURES)
if ("${arg_LIBRARY}" STREQUAL "" AND (NOT ${arg_NO_MODULE}))
message(FATAL_ERROR
"qt_feature_begin_module needs a LIBRARY name! (or specify NO_MODULE)")
endif()
if ("${arg_PUBLIC_FILE}" STREQUAL "")
message(FATAL_ERROR "qt_feature_begin_module needs a PUBLIC_FILE name!")
endif()
if ("${arg_PRIVATE_FILE}" STREQUAL "")
message(FATAL_ERROR "qt_feature_begin_module needs a PRIVATE_FILE name!")
endif()
set(__QtFeature_only_evaluate_features OFF PARENT_SCOPE)
else()
set(__QtFeature_only_evaluate_features ON PARENT_SCOPE)
endif()
set(__QtFeature_library "${arg_LIBRARY}" PARENT_SCOPE)
set(__QtFeature_public_features "" PARENT_SCOPE)
set(__QtFeature_private_features "" PARENT_SCOPE)
set(__QtFeature_internal_features "" PARENT_SCOPE)
set(__QtFeature_private_file "${arg_PRIVATE_FILE}" PARENT_SCOPE)
set(__QtFeature_public_file "${arg_PUBLIC_FILE}" PARENT_SCOPE)
set(__QtFeature_private_extra "" PARENT_SCOPE)
set(__QtFeature_public_extra "" PARENT_SCOPE)
set(__QtFeature_config_definitions "" PARENT_SCOPE)
set(__QtFeature_define_definitions "" PARENT_SCOPE)
endfunction()
function(qt_feature feature)
set(original_name "${feature}")
qt_feature_normalize_name("${feature}" feature)
set_property(GLOBAL PROPERTY QT_FEATURE_ORIGINAL_NAME_${feature} "${original_name}")
cmake_parse_arguments(PARSE_ARGV 1 arg
"PRIVATE;PUBLIC"
"LABEL;PURPOSE;SECTION"
"AUTODETECT;CONDITION;ENABLE;DISABLE;EMIT_IF")
_qt_internal_validate_all_args_are_parsed(arg)
set(_QT_FEATURE_DEFINITION_${feature} ${ARGN} PARENT_SCOPE)
# Register feature for future use:
if (arg_PUBLIC)
list(APPEND __QtFeature_public_features "${feature}")
endif()
if (arg_PRIVATE)
list(APPEND __QtFeature_private_features "${feature}")
endif()
if (NOT arg_PUBLIC AND NOT arg_PRIVATE)
list(APPEND __QtFeature_internal_features "${feature}")
endif()
set(__QtFeature_public_features ${__QtFeature_public_features} PARENT_SCOPE)
set(__QtFeature_private_features ${__QtFeature_private_features} PARENT_SCOPE)
set(__QtFeature_internal_features ${__QtFeature_internal_features} PARENT_SCOPE)
endfunction()
function(qt_evaluate_to_boolean expressionVar)
if(${${expressionVar}})
set(${expressionVar} ON PARENT_SCOPE)
else()
set(${expressionVar} OFF PARENT_SCOPE)
endif()
endfunction()
function(qt_evaluate_config_expression resultVar)
set(result "")
set(nestingLevel 0)
set(skipNext OFF)
set(expression "${ARGN}")
list(LENGTH expression length)
math(EXPR length "${length}-1")
foreach(memberIdx RANGE ${length})
if(${skipNext})
set(skipNext OFF)
continue()
endif()
list(GET expression ${memberIdx} member)
if("${member}" STREQUAL "(")
if(${nestingLevel} GREATER 0)
list(APPEND result ${member})
endif()
math(EXPR nestingLevel "${nestingLevel} + 1")
continue()
elseif("${member}" STREQUAL ")")
math(EXPR nestingLevel "${nestingLevel} - 1")
if(nestingLevel LESS 0)
break()
endif()
if(${nestingLevel} EQUAL 0)
qt_evaluate_config_expression(result ${result})
else()
list(APPEND result ${member})
endif()
continue()
elseif(${nestingLevel} GREATER 0)
list(APPEND result ${member})
continue()
elseif("${member}" STREQUAL "NOT")
list(APPEND result ${member})
continue()
elseif("${member}" STREQUAL "AND")
qt_evaluate_to_boolean(result)
if(NOT ${result})
break()
endif()
set(result "")
elseif("${member}" STREQUAL "OR")
qt_evaluate_to_boolean(result)
if(${result})
break()
endif()
set(result "")
elseif("${member}" STREQUAL "STREQUAL" AND memberIdx LESS ${length})
# Unfortunately the semantics for STREQUAL in if() are broken when the
# RHS is an empty string and the parameters to if are coming through a variable.
# So we expect people to write the empty string with single quotes and then we
# do the comparison manually here.
list(LENGTH result lhsIndex)
math(EXPR lhsIndex "${lhsIndex}-1")
list(GET result ${lhsIndex} lhs)
list(REMOVE_AT result ${lhsIndex})
set(lhs "${${lhs}}")
math(EXPR rhsIndex "${memberIdx}+1")
set(skipNext ON)
list(GET expression ${rhsIndex} rhs)
# We can't pass through an empty string with double quotes through various
# stages of substitution, so instead it is represented using single quotes
# and resolve here.
string(REGEX REPLACE "'(.*)'" "\\1" rhs "${rhs}")
string(COMPARE EQUAL "${lhs}" "${rhs}" stringCompareResult)
list(APPEND result ${stringCompareResult})
else()
string(FIND "${member}" "QT_FEATURE_" idx)
if(idx EQUAL 0)
# Remove the QT_FEATURE_ prefix
string(SUBSTRING "${member}" 11 -1 feature)
qt_evaluate_feature(${feature})
endif()
list(APPEND result ${member})
endif()
endforeach()
# The 'TARGET Gui' case is handled by qt_evaluate_to_boolean, by passing those tokens verbatim
# to if().
if("${result}" STREQUAL "")
set(result ON)
else()
qt_evaluate_to_boolean(result)
endif()
set(${resultVar} ${result} PARENT_SCOPE)
endfunction()
function(_qt_internal_dump_expression_values expression_dump expression)
set(dump "")
set(skipNext FALSE)
set(isTargetExpression FALSE)
set(keywords "EQUAL" "LESS" "LESS_EQUAL" "GREATER" "GREATER_EQUAL" "STREQUAL" "STRLESS"
"STRLESS_EQUAL" "STRGREATER" "STRGREATER_EQUAL" "VERSION_EQUAL" "VERSION_LESS"
"VERSION_LESS_EQUAL" "VERSION_GREATER" "VERSION_GREATER_EQUAL" "MATCHES"
"EXISTS" "COMMAND" "DEFINED" "NOT" "AND" "OR" "TARGET" "EXISTS" "IN_LIST" "(" ")")
list(LENGTH expression length)
math(EXPR length "${length}-1")
if(${length} LESS 0)
return()
endif()
foreach(memberIdx RANGE ${length})
if(${skipNext})
set(skipNext FALSE)
continue()
endif()
list(GET expression ${memberIdx} member)
if(NOT "${member}" IN_LIST keywords)
string(FIND "${member}" "QT_FEATURE_" idx)
if(idx EQUAL 0)
if(NOT DEFINED ${member})
list(APPEND dump "${member} not evaluated")
else()
list(APPEND dump "${member} = \"${${member}}\"")
endif()
elseif(isTargetExpression)
set(targetExpression "TARGET;${member}")
if(${targetExpression})
list(APPEND dump "TARGET ${member} found")
else()
list(APPEND dump "TARGET ${member} not found")
endif()
else()
list(APPEND dump "${member} = \"${${member}}\"")
endif()
set(isTargetExpression FALSE)
set(skipNext FALSE)
elseif("${member}" STREQUAL "TARGET")
set(isTargetExpression TRUE)
elseif("${member}" STREQUAL "STREQUAL")
set(skipNext TRUE)
else()
set(skipNext FALSE)
set(isTargetExpression FALSE)
endif()
endforeach()
string(JOIN "\n " ${expression_dump} ${dump})
set(${expression_dump} "${${expression_dump}}" PARENT_SCOPE)
endfunction()
# Stores the user provided value to FEATURE_${feature} if provided.
# If not provided, stores ${computed} instead.
# ${computed} is also stored when reconfiguring and the condition does not align with the user
# provided value.
#
function(qt_feature_check_and_save_user_provided_value resultVar feature condition computed label)
if (DEFINED "FEATURE_${feature}")
# Revisit new user provided value
set(user_value "${FEATURE_${feature}}")
string(TOUPPER "${user_value}" result)
# If the build is marked as dirty and the user_value doesn't meet the new condition,
# reset it to the computed one.
get_property(dirty_build GLOBAL PROPERTY _qt_dirty_build)
if(NOT condition AND result AND dirty_build)
set(result "${computed}")
message(WARNING "Reset FEATURE_${feature} value to ${result}, because it doesn't \
meet its condition after reconfiguration.")
endif()
set(bool_values OFF NO FALSE N ON YES TRUE Y)
if ((result IN_LIST bool_values) OR (result GREATER_EQUAL 0))
# All good!
else()
message(FATAL_ERROR
"Sanity check failed: FEATURE_${feature} has invalid value \"${result}\"!")
endif()
# Fix-up user-provided values
set("FEATURE_${feature}" "${result}" CACHE BOOL "${label}" FORCE)
else()
# Initial setup:
set(result "${computed}")
set("FEATURE_${feature}" "${result}" CACHE BOOL "${label}")
endif()
set("${resultVar}" "${result}" PARENT_SCOPE)
endfunction()
# Saves the final user value to QT_FEATURE_${feature}, after checking that the condition is met.
macro(qt_feature_check_and_save_internal_value
feature saved_user_value condition label conditionExpression)
if(${saved_user_value})
set(result ON)
else()
set(result OFF)
endif()
if ((NOT condition) AND result)
_qt_internal_dump_expression_values(conditionDump "${conditionExpression}")
string(JOIN " " conditionString ${conditionExpression})
qt_configure_add_report_error("Feature \"${feature}\": Forcing to \"${result}\" breaks its \
condition:\n ${conditionString}\nCondition values dump:\n ${conditionDump}\n" RECORD_ON_FEATURE_EVALUATION)
endif()
if (DEFINED "QT_FEATURE_${feature}")
message(FATAL_ERROR "Feature ${feature} is already defined when evaluating configure.cmake features for ${target}.")
endif()
set(QT_FEATURE_${feature} "${result}" CACHE INTERNAL "Qt feature: ${feature}")
# Add feature to build feature collection
list(APPEND QT_KNOWN_FEATURES "${feature}")
set(QT_KNOWN_FEATURES "${QT_KNOWN_FEATURES}" CACHE INTERNAL "" FORCE)
endmacro()
# The build system stores 2 CMake cache variables for each feature, to allow detecting value changes
# during subsequent reconfigurations.
#
#
# `FEATURE_foo` stores the user provided feature value for the current configuration run.
# It can be set directly by the user, or derived from INPUT_foo (also set by the user).
#
# If a value is not provided on initial configuration, the value will be auto-computed based on the
# various conditions of the feature.
# TODO: Document the various conditions and how they relate to each other.
#
#
# `QT_FEATURE_foo` stores the value of the feature from the previous configuration run.
# Its value is updated once with the newest user provided value after some checks are performed.
#
# This variable also serves as the main source of truth throughout the build system code to check
# if the feature is enabled, e.g. if(QT_FEATURE_foo)
#
# It is not meant to be set by the user. It is only modified by the build system.
#
# Comparing the values of QT_FEATURE_foo and FEATURE_foo, the build system can detect whether
# the user changed the value for a feature and thus recompute any dependent features.
#
function(qt_evaluate_feature feature)
# If the feature was already evaluated as dependency nothing to do here.
if(DEFINED "QT_FEATURE_${feature}")
return()
endif()
if(NOT DEFINED _QT_FEATURE_DEFINITION_${feature})
qt_debug_print_variables(DEDUP MATCH "^QT_FEATURE")
message(FATAL_ERROR "Attempting to evaluate feature ${feature} but its definition is missing. Either the feature does not exist or a dependency to the module that defines it is missing")
endif()
cmake_parse_arguments(arg
"PRIVATE;PUBLIC"
"LABEL;PURPOSE;SECTION;" "AUTODETECT;CONDITION;ENABLE;DISABLE;EMIT_IF" ${_QT_FEATURE_DEFINITION_${feature}})
if("${arg_ENABLE}" STREQUAL "")
set(arg_ENABLE OFF)
endif()
if("${arg_DISABLE}" STREQUAL "")
set(arg_DISABLE OFF)
endif()
if("${arg_AUTODETECT}" STREQUAL "")
set(arg_AUTODETECT ON)
endif()
if("${arg_CONDITION}" STREQUAL "")
set(condition ON)
else()
qt_evaluate_config_expression(condition ${arg_CONDITION})
endif()
qt_evaluate_config_expression(disable_result ${arg_DISABLE})
qt_evaluate_config_expression(enable_result ${arg_ENABLE})
qt_evaluate_config_expression(auto_detect ${arg_AUTODETECT})
if(${disable_result})
set(computed OFF)
elseif((${enable_result}) OR (${auto_detect}))
set(computed ${condition})
else()
# feature not auto-detected and not explicitly enabled
set(computed OFF)
endif()
if("${arg_EMIT_IF}" STREQUAL "")
set(emit_if ON)
else()
qt_evaluate_config_expression(emit_if ${arg_EMIT_IF})
endif()
# If FEATURE_ is not defined trying to use INPUT_ variable to enable/disable feature.
if ((NOT DEFINED "FEATURE_${feature}") AND (DEFINED "INPUT_${feature}")
AND (NOT "${INPUT_${feature}}" STREQUAL "undefined")
AND (NOT "${INPUT_${feature}}" STREQUAL ""))
if(INPUT_${feature})
set(FEATURE_${feature} ON)
else()
set(FEATURE_${feature} OFF)
endif()
endif()
# Warn about a feature which is not emitted, but the user explicitly provided a value for it.
if(NOT emit_if AND DEFINED FEATURE_${feature})
set(msg "")
string(APPEND msg
"Feature ${feature} is insignificant in this configuration, "
"ignoring related command line option(s).")
qt_configure_add_report_entry(TYPE WARNING MESSAGE "${msg}")
# Remove the cache entry so that the warning is not persisted and shown on every
# reconfiguration.
unset(FEATURE_${feature} CACHE)
endif()
# Only save the user provided value if the feature was emitted.
if(emit_if)
qt_feature_check_and_save_user_provided_value(
saved_user_value "${feature}" "${condition}" "${computed}" "${arg_LABEL}")
else()
# Make sure the feature internal value is OFF if not emitted.
set(saved_user_value OFF)
endif()
qt_feature_check_and_save_internal_value(
"${feature}" "${saved_user_value}" "${condition}" "${arg_LABEL}" "${arg_CONDITION}")
# Store each feature's label for summary info.
set(QT_FEATURE_LABEL_${feature} "${arg_LABEL}" CACHE INTERNAL "")
endfunction()
function(qt_feature_config feature config_var_name)
qt_feature_normalize_name("${feature}" feature)
cmake_parse_arguments(PARSE_ARGV 2 arg
"NEGATE"
"NAME"
"")
_qt_internal_validate_all_args_are_parsed(arg)
# Store all the config related info in a unique variable key.
set(key_name "_QT_FEATURE_CONFIG_DEFINITION_${feature}_${config_var_name}")
set(${key_name} "FEATURE;${feature};CONFIG_VAR_NAME;${config_var_name};${ARGN}" PARENT_SCOPE)
# Store the key for later evaluation.
list(APPEND __QtFeature_config_definitions "${key_name}")
set(__QtFeature_config_definitions ${__QtFeature_config_definitions} PARENT_SCOPE)
endfunction()
function(qt_evaluate_qmake_config_values key)
if(NOT DEFINED ${key})
qt_debug_print_variables(DEDUP MATCH "^_QT_FEATURE_CONFIG_DEFINITION")
message(FATAL_ERROR
"Attempting to evaluate feature config ${key} but its definition is missing. ")
endif()
cmake_parse_arguments(arg
"NEGATE"
"FEATURE;NAME;CONFIG_VAR_NAME"
"" ${${key}})
# If no custom name is specified, then the config value is the same as the feature name.
if(NOT arg_NAME)
set(arg_NAME "${arg_FEATURE}")
endif()
set(expected "NOT")
if (arg_NEGATE)
set(expected "")
if(arg_NAME MATCHES "^no_(.*)")
set(arg_NAME "${CMAKE_MATCH_1}")
else()
string(PREPEND arg_NAME "no_")
endif()
endif()
# The feature condition is false, there is no need to export any config values.
if(${expected} ${QT_FEATURE_${arg_FEATURE}})
return()
endif()
if(arg_CONFIG_VAR_NAME STREQUAL "QMAKE_PUBLIC_CONFIG")
list(APPEND __QtFeature_qmake_public_config "${arg_NAME}")
set(__QtFeature_qmake_public_config "${__QtFeature_qmake_public_config}" PARENT_SCOPE)
endif()
if(arg_CONFIG_VAR_NAME STREQUAL "QMAKE_PRIVATE_CONFIG")
list(APPEND __QtFeature_qmake_private_config "${arg_NAME}")
set(__QtFeature_qmake_private_config "${__QtFeature_qmake_private_config}" PARENT_SCOPE)
endif()
if(arg_CONFIG_VAR_NAME STREQUAL "QMAKE_PUBLIC_QT_CONFIG")
list(APPEND __QtFeature_qmake_public_qt_config "${arg_NAME}")
set(__QtFeature_qmake_public_qt_config "${__QtFeature_qmake_public_qt_config}" PARENT_SCOPE)
endif()
endfunction()
function(qt_feature_definition feature name)
qt_feature_normalize_name("${feature}" feature)
cmake_parse_arguments(PARSE_ARGV 2 arg
"NEGATE"
"VALUE;PREREQUISITE"
"")
_qt_internal_validate_all_args_are_parsed(arg)
# Store all the define related info in a unique variable key.
set(key_name "_QT_FEATURE_DEFINE_DEFINITION_${feature}_${name}")
set(${key_name} "FEATURE;${feature};NAME;${name};${ARGN}" PARENT_SCOPE)
# Store the key for later evaluation and subsequent define generation:
list(APPEND __QtFeature_define_definitions "${key_name}")
set(__QtFeature_define_definitions ${__QtFeature_define_definitions} PARENT_SCOPE)
endfunction()
function(qt_evaluate_feature_definition key)
if(NOT DEFINED ${key})
qt_debug_print_variables(DEDUP MATCH "^_QT_FEATURE_DEFINE_DEFINITION")
message(FATAL_ERROR "Attempting to evaluate feature define ${key} but its definition is missing. ")
endif()
cmake_parse_arguments(arg
"NEGATE;"
"FEATURE;NAME;VALUE;PREREQUISITE" "" ${${key}})
set(expected ON)
if (arg_NEGATE)
set(expected OFF)
endif()
set(actual OFF)
if(QT_FEATURE_${arg_FEATURE})
set(actual ON)
endif()
set(msg "")
if(actual STREQUAL expected)
set(indent "")
if(arg_PREREQUISITE)
string(APPEND msg "#if ${arg_PREREQUISITE}\n")
set(indent " ")
endif()
if (arg_VALUE)
string(APPEND msg "${indent}#define ${arg_NAME} ${arg_VALUE}\n")
else()
string(APPEND msg "${indent}#define ${arg_NAME}\n")
endif()
if(arg_PREREQUISITE)
string(APPEND msg "#endif\n")
endif()
string(APPEND __QtFeature_public_extra "${msg}")
endif()
set(__QtFeature_public_extra ${__QtFeature_public_extra} PARENT_SCOPE)
endfunction()
function(qt_extra_definition name value)
cmake_parse_arguments(PARSE_ARGV 2 arg
"PUBLIC;PRIVATE"
""
"")
_qt_internal_validate_all_args_are_parsed(arg)
if (arg_PUBLIC)
string(APPEND __QtFeature_public_extra "\n#define ${name} ${value}\n")
elseif(arg_PRIVATE)
string(APPEND __QtFeature_private_extra "\n#define ${name} ${value}\n")
endif()
set(__QtFeature_public_extra ${__QtFeature_public_extra} PARENT_SCOPE)
set(__QtFeature_private_extra ${__QtFeature_private_extra} PARENT_SCOPE)
endfunction()
function(qt_internal_generate_feature_line line feature)
string(TOUPPER "${QT_FEATURE_${feature}}" value)
if (value STREQUAL "ON")
set(line "#define QT_FEATURE_${feature} 1\n\n" PARENT_SCOPE)
elseif(value STREQUAL "OFF")
set(line "#define QT_FEATURE_${feature} -1\n\n" PARENT_SCOPE)
elseif(value STREQUAL "UNSET")
set(line "#define QT_FEATURE_${feature} 0\n\n" PARENT_SCOPE)
else()
message(FATAL_ERROR "${feature} has unexpected value \"${QT_FEATURE_${feature}}\"! "
"Valid values are ON, OFF and UNSET.")
endif()
endfunction()
function(qt_internal_feature_write_file file features extra)
set(contents "")
foreach(it ${features})
qt_internal_generate_feature_line(line "${it}")
string(APPEND contents "${line}")
endforeach()
string(APPEND contents "${extra}")
file(GENERATE OUTPUT "${file}" CONTENT "${contents}")
endfunction()
# Helper function which evaluates features from a given list of configure.cmake paths
# and creates the feature cache entries.
# Should not be used directly, unless features need to be available in a directory scope before the
# associated module evaluates the features.
# E.g. qtbase/src.pro needs access to Core features before src/corelib/CMakeLists.txt is parsed.
function(qt_feature_evaluate_features list_of_paths)
qt_feature_module_begin(ONLY_EVALUATE_FEATURES)
foreach(path ${list_of_paths})
include("${path}")
endforeach()
qt_feature_module_end(ONLY_EVALUATE_FEATURES)
endfunction()
function(qt_feature_record_summary_entries list_of_paths)
# Clean up any stale state just in case.
qt_feature_unset_state_vars()
set(__QtFeature_only_record_summary_entries TRUE)
foreach(path ${list_of_paths})
include("${path}")
endforeach()
qt_feature_unset_state_vars()
endfunction()
function(qt_feature_module_end)
set(flags ONLY_EVALUATE_FEATURES)
set(options OUT_VAR_PREFIX)
set(multiopts)
cmake_parse_arguments(arg "${flags}" "${options}" "${multiopts}" ${ARGN})
set(target ${arg_UNPARSED_ARGUMENTS})
# The value of OUT_VAR_PREFIX is used as a prefix for output variables that should be
# set in the parent scope.
if(NOT arg_OUT_VAR_PREFIX)
set(arg_OUT_VAR_PREFIX "")
endif()
set(all_features ${__QtFeature_public_features} ${__QtFeature_private_features} ${__QtFeature_internal_features})
list(REMOVE_DUPLICATES all_features)
foreach(feature ${all_features})
qt_evaluate_feature(${feature})
endforeach()
# Evaluate custom cache assignments.
foreach(cache_var_name ${__QtFeature_custom_enabled_cache_variables})
set(${cache_var_name} ON CACHE BOOL "Force enabled by platform requirements." FORCE)
endforeach()
foreach(cache_var_name ${__QtFeature_custom_disabled_cache_variables})
set(${cache_var_name} OFF CACHE BOOL "Force disabled by platform requirements." FORCE)
endforeach()
set(enabled_public_features "")
set(disabled_public_features "")
set(enabled_private_features "")
set(disabled_private_features "")
foreach(feature ${__QtFeature_public_features})
if(QT_FEATURE_${feature})
list(APPEND enabled_public_features ${feature})
else()
list(APPEND disabled_public_features ${feature})
endif()
endforeach()
foreach(feature ${__QtFeature_private_features})
if(QT_FEATURE_${feature})
list(APPEND enabled_private_features ${feature})
else()
list(APPEND disabled_private_features ${feature})
endif()
endforeach()
foreach(key ${__QtFeature_config_definitions})
qt_evaluate_qmake_config_values(${key})
unset(${key} PARENT_SCOPE)
endforeach()
foreach(key ${__QtFeature_define_definitions})
qt_evaluate_feature_definition(${key})
unset(${key} PARENT_SCOPE)
endforeach()
foreach(feature ${all_features})
unset(_QT_FEATURE_DEFINITION_${feature} PARENT_SCOPE)
endforeach()
if(NOT arg_ONLY_EVALUATE_FEATURES)
qt_internal_feature_write_file("${CMAKE_CURRENT_BINARY_DIR}/${__QtFeature_private_file}"
"${__QtFeature_private_features}" "${__QtFeature_private_extra}"
)
qt_internal_feature_write_file("${CMAKE_CURRENT_BINARY_DIR}/${__QtFeature_public_file}"
"${__QtFeature_public_features}" "${__QtFeature_public_extra}"
)
endif()
if (NOT ("${target}" STREQUAL "NO_MODULE") AND NOT arg_ONLY_EVALUATE_FEATURES)
get_target_property(targetType "${target}" TYPE)
if("${targetType}" STREQUAL "INTERFACE_LIBRARY")
set(propertyPrefix "INTERFACE_")
else()
set(propertyPrefix "")
set_property(TARGET "${target}" APPEND PROPERTY EXPORT_PROPERTIES "QT_ENABLED_PUBLIC_FEATURES;QT_DISABLED_PUBLIC_FEATURES;QT_ENABLED_PRIVATE_FEATURES;QT_DISABLED_PRIVATE_FEATURES;QT_QMAKE_PUBLIC_CONFIG;QT_QMAKE_PRIVATE_CONFIG;QT_QMAKE_PUBLIC_QT_CONFIG")
endif()
foreach(visibility public private)
string(TOUPPER "${visibility}" capitalVisibility)
foreach(state enabled disabled)
string(TOUPPER "${state}" capitalState)
set_property(TARGET "${target}" PROPERTY ${propertyPrefix}QT_${capitalState}_${capitalVisibility}_FEATURES "${${state}_${visibility}_features}")
endforeach()
endforeach()
set_property(TARGET "${target}"
PROPERTY ${propertyPrefix}QT_QMAKE_PUBLIC_CONFIG
"${__QtFeature_qmake_public_config}")
set_property(TARGET "${target}"
PROPERTY ${propertyPrefix}QT_QMAKE_PRIVATE_CONFIG
"${__QtFeature_qmake_private_config}")
set_property(TARGET "${target}"
PROPERTY ${propertyPrefix}QT_QMAKE_PUBLIC_QT_CONFIG
"${__QtFeature_qmake_public_qt_config}")
# Config values were the old-school features before actual configure.json features were
# implemented. Therefore "CONFIG+=foo" values should be considered features as well,
# so that CMake can find them when building qtmultimedia for example.
if(__QtFeature_qmake_public_config)
set_property(TARGET "${target}"
APPEND PROPERTY ${propertyPrefix}QT_ENABLED_PUBLIC_FEATURES
${__QtFeature_qmake_public_config})
endif()
if(__QtFeature_qmake_private_config)
set_property(TARGET "${target}"
APPEND PROPERTY ${propertyPrefix}QT_ENABLED_PRIVATE_FEATURES
${__QtFeature_qmake_private_config})
endif()
if(__QtFeature_qmake_public_qt_config)
set_property(TARGET "${target}"
APPEND PROPERTY ${propertyPrefix}QT_ENABLED_PUBLIC_FEATURES
${__QtFeature_qmake_public_qt_config})
endif()
qt_feature_copy_global_config_features_to_core(${target})
endif()
qt_feature_unset_state_vars()
endfunction()
macro(qt_feature_unset_state_vars)
unset(__QtFeature_library PARENT_SCOPE)
unset(__QtFeature_public_features PARENT_SCOPE)
unset(__QtFeature_private_features PARENT_SCOPE)
unset(__QtFeature_internal_features PARENT_SCOPE)
unset(__QtFeature_private_file PARENT_SCOPE)
unset(__QtFeature_public_file PARENT_SCOPE)
unset(__QtFeature_private_extra PARENT_SCOPE)
unset(__QtFeature_public_extra PARENT_SCOPE)
unset(__QtFeature_define_definitions PARENT_SCOPE)
unset(__QtFeature_custom_enabled_features PARENT_SCOPE)
unset(__QtFeature_custom_disabled_features PARENT_SCOPE)
unset(__QtFeature_only_evaluate_features PARENT_SCOPE)
unset(__QtFeature_only_record_summary_entries PARENT_SCOPE)
endmacro()
function(qt_feature_copy_global_config_features_to_core target)
# CMake doesn't support setting custom properties on exported INTERFACE libraries
# See https://gitlab.kitware.com/cmake/cmake/issues/19261.
# To circumvent that, copy the properties from GlobalConfig to Core target.
# This way the global features actually get set in the generated CoreTargets.cmake file.
if(target STREQUAL Core)
foreach(visibility public private)
string(TOUPPER "${visibility}" capitalVisibility)
foreach(state enabled disabled)
string(TOUPPER "${state}" capitalState)
set(core_property_name "QT_${capitalState}_${capitalVisibility}_FEATURES")
set(global_property_name "INTERFACE_${core_property_name}")
get_property(core_values TARGET Core PROPERTY ${core_property_name})
get_property(global_values TARGET GlobalConfig PROPERTY ${global_property_name})
set(total_values ${core_values} ${global_values})
set_property(TARGET Core PROPERTY ${core_property_name} ${total_values})
endforeach()
endforeach()
set(config_property_names
QT_QMAKE_PUBLIC_CONFIG QT_QMAKE_PRIVATE_CONFIG QT_QMAKE_PUBLIC_QT_CONFIG )
foreach(property_name ${config_property_names})
set(core_property_name "${property_name}")
set(global_property_name "INTERFACE_${core_property_name}")
get_property(core_values TARGET Core PROPERTY ${core_property_name})
get_property(global_values TARGET GlobalConfig PROPERTY ${global_property_name})
set(total_values ${core_values} ${global_values})
set_property(TARGET Core PROPERTY ${core_property_name} ${total_values})
endforeach()
endif()
endfunction()
function(qt_config_compile_test name)
if(DEFINED "TEST_${name}")
return()
endif()
cmake_parse_arguments(arg "" "LABEL;PROJECT_PATH;C_STANDARD;CXX_STANDARD"
"COMPILE_OPTIONS;LIBRARIES;CODE;PACKAGES;CMAKE_FLAGS" ${ARGN})
if(arg_PROJECT_PATH)
message(STATUS "Performing Test ${arg_LABEL}")
set(flags "")
qt_get_platform_try_compile_vars(platform_try_compile_vars)
list(APPEND flags ${platform_try_compile_vars})
# If the repo has its own cmake modules, include those in the module path, so that various
# find_package calls work.
if(EXISTS "${PROJECT_SOURCE_DIR}/cmake")
set(must_append_module_path_flag TRUE)
set(flags_copy "${flags}")
set(flags)
foreach(flag IN LISTS flags_copy)
if(flag MATCHES "^-DCMAKE_MODULE_PATH:STRING=")
set(must_append_module_path_flag FALSE)
set(flag "${flag}\\;${PROJECT_SOURCE_DIR}/cmake")
endif()
list(APPEND flags "${flag}")
endforeach()
if(must_append_module_path_flag)
list(APPEND flags "-DCMAKE_MODULE_PATH:STRING=${PROJECT_SOURCE_DIR}/cmake")
endif()
endif()
# Pass which packages need to be found.
if(arg_PACKAGES)
set(packages_list "")
# Parse the package names, version, etc. An example would be:
# PACKAGE Foo 6 REQUIRED
# PACKAGE Bar 2 COMPONENTS Baz
foreach(p ${arg_PACKAGES})
if(p STREQUAL PACKAGE)
if(package_entry)
# Encode the ";" into "\;" to separate the arguments of a find_package call.
string(REPLACE ";" "\\;" package_entry_string "${package_entry}")
list(APPEND packages_list "${package_entry_string}")
endif()
set(package_entry "")
else()
list(APPEND package_entry "${p}")
endif()
endforeach()
# Parse final entry.
if(package_entry)
string(REPLACE ";" "\\;" package_entry_string "${package_entry}")
list(APPEND packages_list "${package_entry_string}")
endif()
# Encode the ";" again.
string(REPLACE ";" "\\;" packages_list "${packages_list}")
# The flags are separated by ';', the find_package entries by '\;',
# and the package parts of an entry by '\\;'.
# Example:
# WrapFoo\\;6\\;COMPONENTS\\;bar\;WrapBaz\\;5
list(APPEND flags "-DQT_CONFIG_COMPILE_TEST_PACKAGES:STRING=${packages_list}")
# Inside the project, the value of QT_CONFIG_COMPILE_TEST_PACKAGES is used in a foreach
# loop that calls find_package() for each package entry, and thus the variable expansion
# ends up calling something like find_package(WrapFoo;6;COMPONENTS;bar) aka
# find_package(WrapFoo 6 COMPONENTS bar).
endif()
# Pass which libraries need to be linked against.
if(arg_LIBRARIES)
set(link_flags "")
set(library_targets "")
# Separate targets from link flags or paths. This is to prevent configuration failures
# when the targets are not found due to missing packages.
foreach(lib ${arg_LIBRARIES})
string(FIND "${lib}" "::" is_library_target)
if(is_library_target EQUAL -1)
list(APPEND link_flags "${lib}")
else()
list(APPEND library_targets "${lib}")
endif()
endforeach()
if(link_flags)
list(APPEND flags "-DQT_CONFIG_COMPILE_TEST_LIBRARIES:STRING=${link_flags}")
endif()
if(library_targets)
list(APPEND flags
"-DQT_CONFIG_COMPILE_TEST_LIBRARY_TARGETS:STRING=${library_targets}")
endif()
endif()
# Pass override values for CMAKE_SYSTEM_{PREFIX|FRAMEWORK}_PATH.
if(DEFINED QT_CMAKE_SYSTEM_PREFIX_PATH_BACKUP)
set(path_list ${CMAKE_SYSTEM_PREFIX_PATH})
string(REPLACE ";" "\\;" path_list "${path_list}")
list(APPEND flags "-DQT_CONFIG_COMPILE_TEST_CMAKE_SYSTEM_PREFIX_PATH=${path_list}")
endif()
if(DEFINED QT_CMAKE_SYSTEM_FRAMEWORK_PATH_BACKUP)
set(path_list ${CMAKE_SYSTEM_FRAMEWORK_PATH})
string(REPLACE ";" "\\;" path_list "${path_list}")
list(APPEND flags "-DQT_CONFIG_COMPILE_TEST_CMAKE_SYSTEM_FRAMEWORK_PATH=${path_list}")
endif()
if(NOT arg_CMAKE_FLAGS)
set(arg_CMAKE_FLAGS "")
endif()
# CI passes the project dir of the Qt repository as absolute path without drive letter:
# \Users\qt\work\qt\qtbase
# Ensure that arg_PROJECT_PATH is an absolute path with drive letter:
# C:/Users/qt/work/qt/qtbase
# This works around CMake upstream issue #22534.
if(CMAKE_HOST_WIN32)
get_filename_component(arg_PROJECT_PATH "${arg_PROJECT_PATH}" REALPATH)
endif()
try_compile(HAVE_${name} "${CMAKE_BINARY_DIR}/config.tests/${name}" "${arg_PROJECT_PATH}"
"${name}" CMAKE_FLAGS ${flags} ${arg_CMAKE_FLAGS})
if(${HAVE_${name}})
set(status_label "Success")
else()
set(status_label "Failed")
endif()
message(STATUS "Performing Test ${arg_LABEL} - ${status_label}")
else()
foreach(library IN ITEMS ${arg_LIBRARIES})
if(NOT TARGET "${library}")
# If the dependency looks like a cmake target, then make this compile test
# fail instead of cmake abort later via CMAKE_REQUIRED_LIBRARIES.
string(FIND "${library}" "::" cmake_target_namespace_separator)
if(NOT cmake_target_namespace_separator EQUAL -1)
message(STATUS "Performing Test ${arg_LABEL} - Failed because ${library} not found")
set(HAVE_${name} FALSE)
break()
endif()
endif()
endforeach()
if(NOT DEFINED HAVE_${name})
set(_save_CMAKE_C_STANDARD "${CMAKE_C_STANDARD}")
set(_save_CMAKE_C_STANDARD_REQUIRED "${CMAKE_C_STANDARD_REQUIRED}")
set(_save_CMAKE_CXX_STANDARD "${CMAKE_CXX_STANDARD}")
set(_save_CMAKE_CXX_STANDARD_REQUIRED "${CMAKE_CXX_STANDARD_REQUIRED}")
set(_save_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}")
set(_save_CMAKE_TRY_COMPILE_PLATFORM_VARIABLES "${CMAKE_TRY_COMPILE_PLATFORM_VARIABLES}")
if(arg_C_STANDARD)
set(CMAKE_C_STANDARD "${arg_C_STANDARD}")
set(CMAKE_C_STANDARD_REQUIRED OFF)
endif()
if(arg_CXX_STANDARD)
if(${arg_CXX_STANDARD} LESS 23 OR ${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.20")
set(CMAKE_CXX_STANDARD "${arg_CXX_STANDARD}")
set(CMAKE_CXX_STANDARD_REQUIRED OFF)
endif()
endif()
set(CMAKE_REQUIRED_FLAGS ${arg_COMPILE_OPTIONS})
# Pass -stdlib=libc++ on if necessary
if (INPUT_stdlib_libcpp OR QT_FEATURE_stdlib_libcpp)
list(APPEND CMAKE_REQUIRED_FLAGS "-stdlib=libc++")
endif()
# For MSVC we need to explicitly pass -Zc:__cplusplus to get correct __cplusplus
# define values. According to common/msvc-version.conf the flag is supported starting
# with 1913.
# https://developercommunity.visualstudio.com/content/problem/139261/msvc-incorrectly-defines-cplusplus.html
# No support for the flag in upstream CMake as of 3.17.
# https://gitlab.kitware.com/cmake/cmake/issues/18837
if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC" AND MSVC_VERSION GREATER_EQUAL 1913)
list(APPEND CMAKE_REQUIRED_FLAGS "-Zc:__cplusplus")
endif()
# Let CMake load our custom platform modules.
if(NOT QT_AVOID_CUSTOM_PLATFORM_MODULES)
list(APPEND CMAKE_TRY_COMPILE_PLATFORM_VARIABLES CMAKE_MODULE_PATH)
endif()
set(_save_CMAKE_REQUIRED_LIBRARIES "${CMAKE_REQUIRED_LIBRARIES}")
set(CMAKE_REQUIRED_LIBRARIES "${arg_LIBRARIES}")
check_cxx_source_compiles("${arg_UNPARSED_ARGUMENTS} ${arg_CODE}" HAVE_${name})
set(CMAKE_REQUIRED_LIBRARIES "${_save_CMAKE_REQUIRED_LIBRARIES}")
set(CMAKE_C_STANDARD "${_save_CMAKE_C_STANDARD}")
set(CMAKE_C_STANDARD_REQUIRED "${_save_CMAKE_C_STANDARD_REQUIRED}")
set(CMAKE_CXX_STANDARD "${_save_CMAKE_CXX_STANDARD}")
set(CMAKE_CXX_STANDARD_REQUIRED "${_save_CMAKE_CXX_STANDARD_REQUIRED}")
set(CMAKE_REQUIRED_FLAGS "${_save_CMAKE_REQUIRED_FLAGS}")
set(CMAKE_TRY_COMPILE_PLATFORM_VARIABLES "${_save_CMAKE_TRY_COMPILE_PLATFORM_VARIABLES}")
endif()
endif()
set(TEST_${name} "${HAVE_${name}}" CACHE INTERNAL "${arg_LABEL}")
endfunction()
# This function should be used for passing required try compile platform variables to the
# project-based try_compile() call.
# out_var will be a list of -Dfoo=bar strings, suitable to pass to CMAKE_FLAGS.
function(qt_get_platform_try_compile_vars out_var)
# Use the regular variables that are used for source-based try_compile() calls.
set(flags "${CMAKE_TRY_COMPILE_PLATFORM_VARIABLES}")
# Pass custom flags.