-
Notifications
You must be signed in to change notification settings - Fork 177
/
Qt6QmlMacros.cmake
4800 lines (4272 loc) · 201 KB
/
Qt6QmlMacros.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
#
# Q6QmlMacros
#
set(__qt_qml_macros_module_base_dir "${CMAKE_CURRENT_LIST_DIR}" CACHE INTERNAL "")
# Install support uses the CMAKE_INSTALL_xxxDIR variables. Include this here
# so that it is more likely to get pulled in earlier at a higher level, and also
# to avoid re-including it many times later
include(GNUInstallDirs)
_qt_internal_add_deploy_support("${CMAKE_CURRENT_LIST_DIR}/Qt6QmlDeploySupport.cmake")
# This function is used to parse DEPENDENCY and IMPORT entries passed to qt_add_qml_module
# It takes the entry as a mandatory argument, and then sets the following
# user provided properties in the callers scope
# OUTPUT_URI <property>: the URI of the module; mandatory argument
# OUTPUT_VERSION <property>: the requested version of the module; will potentially be empty; mandatory
# OUTPUT_MODULE_LOCATION <property>: the folder in which the module is located; optional; can be used to extract potential import path
# OUTPUT_MODULE_TARGET <property>: the target corresponding to the module
function(_qt_internal_parse_qml_module_dependency dependency was_marked_as_target)
set(args_option "")
set(args_single OUTPUT_URI OUTPUT_VERSION OUTPUT_MODULE_LOCATION OUTPUT_MODULE_TARGET)
set(args_multi QML_FILES IMPORT_PATHS)
cmake_parse_arguments(PARSE_ARGV 2 arg
"${args_option}" "${args_single}" "${args_multi}"
)
if(arg_UNPARSED_ARGUMENTS)
message(FATAL_ERROR "Unknown/unexpected arguments: ${arg_UNPARSED_ARGUMENTS}")
endif()
if(NOT arg_OUTPUT_URI)
message(FATAL_ERROR "Missing output URI variable")
endif()
if(NOT arg_OUTPUT_VERSION)
message(FATAL_ERROR "Missing output version variable")
endif()
set(dep_version "")
set(dep_target_or_uri "")
string(FIND "${dependency}" "/" slash_position REVERSE)
if(slash_position EQUAL -1)
set(dep_target_or_uri "${dependency}")
else()
string(SUBSTRING "${dependency}" 0 ${slash_position} dep_module)
math(EXPR slash_position "${slash_position} + 1")
string(SUBSTRING "${dependency}" ${slash_position} -1 dep_version)
if(NOT dep_version MATCHES "^([0-9]+(\\.[0-9]+)?|auto)$")
message(FATAL_ERROR
"Invalid module dependency version number. "
"Expected 'VersionMajor', 'VersionMajor.VersionMinor' or 'auto'."
)
endif()
set(dep_target_or_uri "${dep_module}")
endif()
if("${was_marked_as_target}" AND NOT TARGET ${dep_target_or_uri})
message(FATAL_ERROR "Argument ${dep_target_or_uri} is not a target!")
endif()
if("${was_marked_as_target}")
qt6_query_qml_module(${dep_target_or_uri}
URI dependency_uri
QMLDIR qmldir_location
)
set(dep_module ${dependency_uri})
else()
set(dep_module "${dep_target_or_uri}")
endif()
set(${arg_OUTPUT_URI} ${dep_module} PARENT_SCOPE)
set(${arg_OUTPUT_VERSION} ${dep_version} PARENT_SCOPE)
if(arg_OUTPUT_MODULE_LOCATION)
if(was_marked_as_target)
if(NOT qmldir_location)
message(FATAL_ERROR "module has no qmldir! Given target was ${dep_target_or_uri}")
endif()
set(module_location "${qmldir_location}")
string(REGEX MATCHALL "\\." matches "${dependency_uri}")
list(LENGTH matches go_up_count)
# inclusive, which is what we want here: go up once for the qmldir,
# and then once per separated component
foreach(i RANGE ${go_up_count})
get_filename_component(module_location "${module_location}" DIRECTORY)
endforeach()
set(${arg_OUTPUT_MODULE_LOCATION} "${module_location}" PARENT_SCOPE)
else()
set(${arg_OUTPUT_MODULE_LOCATION} "NOTFOUND" PARENT_SCOPE)
endif()
endif()
if (arg_OUTPUT_MODULE_TARGET AND was_marked_as_target)
set(${arg_OUTPUT_MODULE_TARGET} "${dep_target_or_uri}" PARENT_SCOPE)
else()
set(${arg_OUTPUT_MODULE_TARGET} "" PARENT_SCOPE)
endif()
endfunction()
function(_qt_internal_write_qmldir_part target)
set(effective_outdir $<TARGET_FILE_DIR:${target}>)
set(qtconf_file "${effective_outdir}/${target}_qt.part.conf")
get_target_property(dependency_targets "${target}" QT_QML_DEPENDENT_QML_MODULE_TARGETS)
get_directory_property(counter
DIRECTORY ${PROJECT_SOURCE_DIR}
QT_QMLDIR_DEFERRED_WRITEOUT_COUNTER
)
math(EXPR counter "${counter} - 1")
set_property(
DIRECTORY ${PROJECT_SOURCE_DIR}
PROPERTY QT_QMLDIR_DEFERRED_WRITEOUT_COUNTER "${counter}"
)
if(dependency_targets)
foreach(dep_target ${dependency_targets})
qt6_query_qml_module(${dep_target}
QMLDIR qmldir_location
)
get_filename_component(module_location "${qmldir_location}" DIRECTORY)
get_filename_component(module_import_path "${module_location}" DIRECTORY)
list(APPEND qt_all_qml_output_dirs ${module_import_path})
endforeach()
if (qt_all_qml_output_dirs)
list(REMOVE_DUPLICATES qt_all_qml_output_dirs)
# TODO: this will break for paths containing a newline character..
list(JOIN qt_all_qml_output_dirs "\n" qt_all_qml_output_dirs)
file(GENERATE
OUTPUT "${qtconf_file}"
CONTENT "${qt_all_qml_output_dirs}\n"
)
set_property(
DIRECTORY ${PROJECT_SOURCE_DIR}
APPEND
PROPERTY QT_QMLDIR_ALL_PARTS "${qtconf_file}"
)
set_property(
DIRECTORY ${PROJECT_SOURCE_DIR}
APPEND
PROPERTY QT_QMLDIR_DEFERRED_WRITEOUT_ALL_TARGETS "${target}")
endif()
endif()
if (counter EQUAL 0)
# counter reached zero, all relevant finalizers are done
get_directory_property(all_parts DIRECTORY ${PROJECT_SOURCE_DIR} QT_QMLDIR_ALL_PARTS)
if (NOT all_parts)
return()
endif()
list(JOIN all_parts "\n" all_parts_string)
set(command_args_location "${PROJECT_BINARY_DIR}/.qt/qtconf_list_$<CONFIG>")
file(GENERATE
OUTPUT "${command_args_location}"
CONTENT "${all_parts_string}\n"
)
_qt_internal_get_tool_wrapper_script_path(tool_wrapper)
add_custom_command(
OUTPUT "${command_args_location}.done"
COMMAND
${tool_wrapper}
$<TARGET_FILE:${QT_CMAKE_EXPORT_NAMESPACE}::qmltyperegistrar>
--merge-qt-conf "${command_args_location}"
COMMENT "Generating qt.conf file"
DEPENDS
${all_parts}
${QT_CMAKE_EXPORT_NAMESPACE}::qmltyperegistrar
)
# actually not specific to $target, but we need a unique identifier
set(customtargetname "generate_finalqtconf_${target}")
add_custom_target("${customtargetname}" DEPENDS "${command_args_location}.done")
get_directory_property(all_targets
DIRECTORY ${PROJECT_SOURCE_DIR}
QT_QMLDIR_DEFERRED_WRITEOUT_ALL_TARGETS
)
foreach (moduleTarget ${all_targets})
add_dependencies(${moduleTarget} "${customtargetname}")
endforeach()
endif()
endfunction()
function(_qt_internal_writebuilddir_qtconf_nondeferred property_folder writeout_folder)
set(qt_all_qml_output_dirs "")
get_directory_property(targets
DIRECTORY "${property_folder}"
QT_QML_TARGETS_FOR_DEFERRED_QTCONF_WRITEOUT
)
set(qtconf_file "${writeout_folder}/qt.conf")
foreach(target IN LISTS targets)
get_target_property(dependency_targets "${target}" QT_QML_DEPENDENT_QML_MODULE_TARGETS)
if(NOT dependency_targets)
continue()
endif()
foreach(dep_target ${dependency_targets})
qt6_query_qml_module(${dep_target}
QMLDIR qmldir_location
)
get_filename_component(module_location "${qmldir_location}" DIRECTORY)
get_filename_component(module_import_path "${module_location}" DIRECTORY)
list(APPEND qt_all_qml_output_dirs ${module_import_path})
endforeach()
endforeach()
if (NOT qt_all_qml_output_dirs)
return()
endif()
list(REMOVE_DUPLICATES qt_all_qml_output_dirs)
# add quotes to deal with whitespace
list(TRANSFORM qt_all_qml_output_dirs APPEND "\"")
list(TRANSFORM qt_all_qml_output_dirs PREPEND "\"")
list(JOIN qt_all_qml_output_dirs "," qt_all_qml_output_dirs)
configure_file(
${__qt_qml_macros_module_base_dir}/Qt6qt.conf.in ${qtconf_file}
@ONLY
)
endfunction()
function(qt6_add_qml_module target)
set(args_option
STATIC
SHARED
DESIGNER_SUPPORTED
FOLLOW_FOREIGN_VERSIONING
AUTO_RESOURCE_PREFIX
NO_PLUGIN
NO_PLUGIN_OPTIONAL
NO_CREATE_PLUGIN_TARGET
NO_GENERATE_PLUGIN_SOURCE
NO_GENERATE_QMLTYPES
NO_GENERATE_QMLDIR
NO_GENERATE_EXTRA_QMLDIRS
NO_LINT
NO_CACHEGEN
NO_RESOURCE_TARGET_PATH
NO_IMPORT_SCAN
ENABLE_TYPE_COMPILER
# Used to mark modules as having static side effects (i.e. if they install an image provider)
__QT_INTERNAL_STATIC_MODULE
# Used to mark modules as being a system module that provides all builtins
__QT_INTERNAL_SYSTEM_MODULE
# Give the resource for the qmldir a unique name; TODO: Remove once we can
__QT_INTERNAL_DISAMBIGUATE_QMLDIR_RESOURCE
)
set(args_single
PLUGIN_TARGET
INSTALLED_PLUGIN_TARGET # Internal option only, it may be removed
OUTPUT_TARGETS
RESOURCE_PREFIX
URI
TARGET_PATH # Internal option only, it may be removed
VERSION
OUTPUT_DIRECTORY
CLASS_NAME
TYPEINFO
NAMESPACE
# TODO: We don't handle installation, warn if callers used these with the old
# API and eventually remove them once we have updated all other repos
RESOURCE_EXPORT
INSTALL_DIRECTORY
INSTALL_LOCATION
TYPE_COMPILER_NAMESPACE
QMLTC_EXPORT_DIRECTIVE
QMLTC_EXPORT_FILE_NAME
)
set(args_multi
SOURCES
QML_FILES
RESOURCES
IMPORTS
IMPORT_PATH
OPTIONAL_IMPORTS
DEFAULT_IMPORTS
DEPENDENCIES
PAST_MAJOR_VERSIONS
)
cmake_parse_arguments(PARSE_ARGV 1 arg
"${args_option}"
"${args_single}"
"${args_multi}"
)
if(arg_UNPARSED_ARGUMENTS)
message(FATAL_ERROR "Unknown/unexpected arguments: ${arg_UNPARSED_ARGUMENTS}")
endif()
# Warn about options we no longer need/use (these were used by the internal
# targets and examples, but the logic has been shifted to
# qt_internal_add_qml_module() or left as a responsibility of the caller).
if(DEFINED arg_RESOURCE_EXPORT)
message(AUTHOR_WARNING
"RESOURCE_EXPORT will be ignored. This function does not handle "
"installation, which is what RESOURCE_EXPORT was previously used "
"for. Please update your project to install the target directly."
)
endif()
if(DEFINED arg_INSTALL_DIRECTORY)
message(AUTHOR_WARNING
"INSTALL_DIRECTORY will be ignored. This function does not handle "
"installation, please update your project to install the target "
"directly."
)
endif()
if(DEFINED arg_INSTALL_LOCATION)
message(AUTHOR_WARNING
"INSTALL_LOCATION will be ignored. This function does not handle "
"installation, please update your project to install the target "
"directly."
)
endif()
# Mandatory arguments
if (NOT arg_URI)
message(FATAL_ERROR
"Called without a module URI. Please specify one using the URI argument."
)
endif()
_qt_internal_require_qml_uri_valid("${arg_URI}")
if (NOT arg_VERSION)
set(arg_VERSION "254.254")
elseif ("${arg_VERSION}" MATCHES "^([0-9]+\\.[0-9]+)\\.[0-9]+$")
set(arg_VERSION "${CMAKE_MATCH_1}")
elseif (NOT "${arg_VERSION}" MATCHES "^[0-9]+\\.[0-9]+$")
message(FATAL_ERROR
"Called with an invalid version argument: '${arg_VERSION}'. "
"Expected version in the form: VersionMajor.VersionMinor."
)
endif()
# Other arguments and checking for invalid combinations
if (NOT arg_TARGET_PATH)
# NOTE: This will always be used for copying things to the build
# directory, but it will not be used for resource paths if
# NO_RESOURCE_TARGET_PATH was given.
string(REPLACE "." "/" arg_TARGET_PATH ${arg_URI})
endif()
if(arg_NO_PLUGIN AND DEFINED arg_PLUGIN_TARGET)
message(FATAL_ERROR
"NO_PLUGIN was specified, but PLUGIN_TARGET was also given. "
"At most one of these can be present."
)
endif()
if (NOT arg_ENABLE_TYPE_COMPILER)
if (DEFINED arg_TYPE_COMPILER_NAMESPACE)
message(WARNING
"TYPE_COMPILER_NAMESPACE is set, but ENABLE_TYPE_COMPILER is not specified. "
"The TYPE_COMPILER_NAMESPACE value will be ignored."
)
endif()
if (DEFINED arg_QMLTC_EXPORT_DIRECTIVE)
message(WARNING
"QMLTC_EXPORT_DIRECTIVE is set, but ENABLE_TYPE_COMPILER is not specified. "
"The QMLTC_EXPORT_DIRECTIVE value will be ignored."
)
endif()
if (DEFINED arg_QMLTC_EXPORT_FILE_NAME)
message(WARNING
"QMLTC_EXPORT_FILE_NAME is set, but ENABLE_TYPE_COMPILER is not specified. "
"The QMLTC_EXPORT_FILE_NAME will be ignored."
)
endif()
else()
if ((DEFINED arg_QMLTC_EXPORT_FILE_NAME) AND (NOT (DEFINED arg_QMLTC_EXPORT_DIRECTIVE)))
message(FATAL_ERROR
"Specifying a value for QMLTC_EXPORT_FILE_NAME also requires one for QMLTC_EXPORT_DIRECTIVE."
)
endif()
endif()
set(is_executable FALSE)
if(TARGET ${target})
if(arg_STATIC OR arg_SHARED)
message(FATAL_ERROR
"Cannot use STATIC or SHARED keyword when passed an existing target (${target})"
)
endif()
# With CMake 3.17 and earlier, a source file's generated property isn't
# visible outside of the directory scope in which it is set. That can
# lead to build errors for things like type registration due to CMake
# thinking nothing will create a missing file on the first run. With
# CMake 3.18 or later, we can force that visibility. Policy CMP0118
# added in CMake 3.20 should have made this unnecessary, but we can't
# rely on that because the user project controls what it is set to at
# the point where it matters, which is the end of the target's
# directory scope (hence we can't even test for it here).
get_target_property(source_dir ${target} SOURCE_DIR)
if(NOT source_dir STREQUAL CMAKE_CURRENT_SOURCE_DIR AND
CMAKE_VERSION VERSION_LESS "3.18")
message(WARNING
"qt6_add_qml_module() is being called in a different "
"directory scope to the one in which the target \"${target}\" "
"was created. CMake 3.18 or later is required to generate a "
"project robustly for this scenario, but you are using "
"CMake ${CMAKE_VERSION}. Ideally, qt6_add_qml_module() should "
"only be called from the same scope as the one the target was "
"created in to avoid dependency and visibility problems."
)
endif()
get_target_property(backing_target_type ${target} TYPE)
get_target_property(is_android_executable "${target}" _qt_is_android_executable)
if (backing_target_type STREQUAL "EXECUTABLE" OR is_android_executable)
if(DEFINED arg_PLUGIN_TARGET)
message(FATAL_ERROR
"A QML module with an executable as its backing target "
"cannot have a plugin."
)
endif()
if(arg_NO_CREATE_PLUGIN_TARGET)
message(WARNING
"A QML module with an executable as its backing target "
"cannot have a plugin. The NO_CREATE_PLUGIN_TARGET option "
"has no effect and should be removed."
)
endif()
set(arg_NO_PLUGIN TRUE)
set(lib_type "")
set(is_executable TRUE)
elseif(arg_NO_RESOURCE_TARGET_PATH)
message(FATAL_ERROR
"NO_RESOURCE_TARGET_PATH cannot be used for a backing target "
"that is not an executable"
)
elseif(backing_target_type STREQUAL "STATIC_LIBRARY")
set(lib_type STATIC)
elseif(backing_target_type MATCHES "(SHARED|MODULE)_LIBRARY")
set(lib_type SHARED)
else()
message(FATAL_ERROR "Unsupported backing target type: ${backing_target_type}")
endif()
else()
if(arg_STATIC AND arg_SHARED)
message(FATAL_ERROR
"Both STATIC and SHARED specified, at most one can be given"
)
endif()
if(arg_NO_RESOURCE_TARGET_PATH)
message(FATAL_ERROR
"NO_RESOURCE_TARGET_PATH can only be provided when an existing "
"executable target is passed in as the backing target"
)
endif()
# Explicit arguments take precedence, otherwise default to using the same
# staticality as what Qt was built with. This follows the already
# established default behavior for building ordinary Qt plugins.
# We don't allow the standard CMake BUILD_SHARED_LIBS variable to control
# the default because that can lead to different defaults depending on
# whether you build with a separate backing target or not.
if(arg_STATIC)
set(lib_type STATIC)
elseif(arg_SHARED)
set(lib_type SHARED)
elseif(QT6_IS_SHARED_LIBS_BUILD)
set(lib_type SHARED)
else()
set(lib_type STATIC)
endif()
endif()
if(arg_NO_PLUGIN)
# Simplifies things a bit further below
set(arg_PLUGIN_TARGET "")
elseif(NOT DEFINED arg_PLUGIN_TARGET)
if(arg_NO_CREATE_PLUGIN_TARGET)
# We technically could allow this and rely on the project using the
# default plugin target name, but not doing so gives us the
# flexibility to potentially change that default later if needed.
message(FATAL_ERROR
"PLUGIN_TARGET must also be provided when NO_CREATE_PLUGIN_TARGET "
"is used. If you want to disable creating a plugin altogether, "
"use the NO_PLUGIN option instead."
)
endif()
set(arg_PLUGIN_TARGET ${target}plugin)
endif()
if(arg_NO_CREATE_PLUGIN_TARGET AND arg_PLUGIN_TARGET STREQUAL target AND NOT TARGET ${target})
message(FATAL_ERROR
"PLUGIN_TARGET is the same as the backing target, which is allowed, "
"but NO_CREATE_PLUGIN_TARGET was also given and the target does not "
"exist. Either ensure the target is already created or do not "
"specify NO_CREATE_PLUGIN_TARGET."
)
endif()
if(NOT arg_INSTALLED_PLUGIN_TARGET)
set(arg_INSTALLED_PLUGIN_TARGET ${arg_PLUGIN_TARGET})
endif()
set(no_gen_source)
if(arg_NO_GENERATE_PLUGIN_SOURCE)
set(no_gen_source NO_GENERATE_PLUGIN_SOURCE)
endif()
if(arg_OUTPUT_DIRECTORY)
get_filename_component(arg_OUTPUT_DIRECTORY "${arg_OUTPUT_DIRECTORY}"
ABSOLUTE BASE_DIR "${CMAKE_CURRENT_BINARY_DIR}"
)
else()
if("${QT_QML_OUTPUT_DIRECTORY}" STREQUAL "")
set(arg_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
# For libraries, we assume/require that the source directory
# structure is consistent with the target path. For executables,
# the source directory will usually not reflect the target path
# and the project will often expect to be able to use resource
# paths that don't include the target path (they need the
# NO_RESOURCE_TARGET_PATH option if they do that). Tooling always
# needs the target path in the file system though, so the output
# directory should always have it. Handle the special case for
# executables to ensure this is what we get.
if(is_executable)
string(APPEND arg_OUTPUT_DIRECTORY "/${arg_TARGET_PATH}")
endif()
else()
if(NOT IS_ABSOLUTE "${QT_QML_OUTPUT_DIRECTORY}")
message(FATAL_ERROR
"QT_QML_OUTPUT_DIRECTORY must be an absolute path, but given: "
"${QT_QML_OUTPUT_DIRECTORY}"
)
endif()
# This inherently does what we want for libraries and executables
set(arg_OUTPUT_DIRECTORY ${QT_QML_OUTPUT_DIRECTORY}/${arg_TARGET_PATH})
endif()
endif()
# Sanity check that we are not trying to have two different QML modules use
# the same output directory.
_qt_internal_find_qml_module(other_target BY_OUTPUT_DIR "${arg_OUTPUT_DIRECTORY}")
if(other_target)
message(FATAL_ERROR
"Output directory for target \"${target}\" is already used by "
"another QML module (target \"${other_target}\"). "
"Output directory is:\n ${arg_OUTPUT_DIRECTORY}\n"
)
endif()
set_property(GLOBAL APPEND PROPERTY _qt_all_qml_uris ${arg_URI})
set_property(GLOBAL APPEND PROPERTY _qt_all_qml_output_dirs ${arg_OUTPUT_DIRECTORY})
set_property(GLOBAL APPEND PROPERTY _qt_all_qml_targets ${target})
if(NOT arg_CLASS_NAME AND TARGET "${arg_PLUGIN_TARGET}")
get_target_property(class_name ${arg_PLUGIN_TARGET} QT_PLUGIN_CLASS_NAME)
if(class_name)
set(arg_CLASS_NAME)
endif()
endif()
if(NOT arg_CLASS_NAME)
_qt_internal_compute_qml_plugin_class_name_from_uri("${arg_URI}" arg_CLASS_NAME)
endif()
if(TARGET ${target})
if(arg_PLUGIN_TARGET STREQUAL target)
# Insert the plugin's URI into its meta data to enable usage
# of static plugins in QtDeclarative (like in mkspecs/features/qml_plugin.prf).
set_property(TARGET ${target} APPEND PROPERTY
AUTOMOC_MOC_OPTIONS "-Muri=${arg_URI}"
)
endif()
else()
if(arg_PLUGIN_TARGET STREQUAL target)
set(conditional_args ${no_gen_source})
if(arg_NAMESPACE)
list(APPEND conditional_args NAMESPACE ${arg_NAMESPACE})
endif()
qt6_add_qml_plugin(${target}
${lib_type}
OUTPUT_DIRECTORY ${arg_OUTPUT_DIRECTORY}
URI ${arg_URI}
CLASS_NAME ${arg_CLASS_NAME}
${conditional_args}
)
else()
qt6_add_library(${target} ${lib_type})
endif()
endif()
if(NOT target STREQUAL Qml)
target_link_libraries(${target} PRIVATE ${QT_CMAKE_EXPORT_NAMESPACE}::Qml)
endif()
if(NOT arg_TYPEINFO AND NOT arg_NO_GENERATE_QMLTYPES)
set(arg_TYPEINFO ${target}.qmltypes)
endif()
set(all_qml_import_paths "${arg_IMPORT_PATH}")
set(all_dependency_targets)
set(original_no_show_policy_value "${QT_NO_SHOW_OLD_POLICY_WARNINGS}")
# silent by default, we only warn if someone uses TARGET as a URI
set(QT_NO_SHOW_OLD_POLICY_WARNINGS TRUE)
__qt_internal_setup_policy(QTP0005 "6.8.0"
"" # intentionally empty as we silence the warning anyway
)
qt6_policy(GET QTP0005 allow_targets_for_dependencies_policy)
set(QT_NO_SHOW_OLD_POLICY_WARNINGS "${original_no_show_policy_value}")
string(COMPARE EQUAL "${allow_targets_for_dependencies_policy}" "NEW" target_is_keyword)
set(target_keyword_was_set FALSE)
foreach(import_set IN ITEMS IMPORTS OPTIONAL_IMPORTS DEFAULT_IMPORTS)
foreach(import IN LISTS arg_${import_set})
if (import STREQUAL "TARGET")
if (target_is_keyword)
set(target_keyword_was_set TRUE)
continue()
else()
message(AUTHOR_WARNING "TARGET is treated as a URI because QTP0005 is set to OLD. This is deprecated behavior. Check https://doc.qt.io/qt-6/qt-cmake-policy-qtp0005.html for policy details.")
set(target_keyword_was_set FALSE)
endif()
endif()
_qt_internal_parse_qml_module_dependency(${import} ${target_keyword_was_set}
OUTPUT_URI import_uri
OUTPUT_VERSION import_version
OUTPUT_MODULE_LOCATION module_location
OUTPUT_MODULE_TARGET dependency_target
)
get_filename_component(module_import_path "${module_location}" DIRECTORY)
list(APPEND all_qml_import_paths "${module_import_path}")
if (NOT "${import_version}" STREQUAL "")
set_property(TARGET ${target} APPEND PROPERTY
QT_QML_MODULE_${import_set} "${import_uri} ${import_version}"
)
else()
set_property(TARGET ${target} APPEND PROPERTY
QT_QML_MODULE_${import_set} "${import_uri}"
)
endif()
if(TARGET "${dependency_target}")
list(APPEND all_dependency_targets "${dependency_target}")
endif()
set(target_keyword_was_set FALSE)
endforeach()
endforeach()
foreach(dependency IN LISTS arg_DEPENDENCIES)
if (dependency STREQUAL "TARGET")
if (target_is_keyword)
set(target_keyword_was_set TRUE)
continue()
else()
message(AUTHOR_WARNING "TARGET is treated as a URI because QTP0005 is set to OLD. This is deprecated behavior. Check https://doc.qt.io/qt-6/qt-cmake-policy-qtp0005.html for policy details.")
set(target_keyword_was_set FALSE)
endif()
endif()
_qt_internal_parse_qml_module_dependency(${dependency} "${target_keyword_was_set}"
OUTPUT_URI dep_uri
OUTPUT_VERSION dep_version
OUTPUT_MODULE_LOCATION module_location
OUTPUT_MODULE_TARGET dependency_target
)
get_filename_component(module_import_path "${module_location}" DIRECTORY)
list(APPEND all_qml_import_paths "${module_import_path}")
if (NOT "${dep_version}" STREQUAL "")
set_property(TARGET ${target} APPEND PROPERTY
QT_QML_MODULE_DEPENDENCIES "${dep_uri} ${dep_version}"
)
else()
set_property(TARGET ${target} APPEND PROPERTY
QT_QML_MODULE_DEPENDENCIES "${dep_uri}"
)
endif()
set(target_keyword_was_set FALSE)
if(TARGET "${dependency_target}")
list(APPEND all_dependency_targets "${dependency_target}")
endif()
endforeach()
### TODO: add support for transitive dependencies, too
list(REMOVE_DUPLICATES all_dependency_targets)
set_property(TARGET ${target} PROPERTY QT_QML_DEPENDENT_QML_MODULE_TARGETS "${all_dependency_targets}")
_qt_internal_collect_qml_module_dependencies(${target})
if(arg_AUTO_RESOURCE_PREFIX)
if(arg_RESOURCE_PREFIX)
message(FATAL_ERROR
"Both RESOURCE_PREFIX and AUTO_RESOURCE_PREFIX are specified for ${target}. "
"You can only have one."
)
else()
set(arg_RESOURCE_PREFIX "/qt/qml")
message(DEPRECATION "AUTO_RESOURCE_PREFIX is deprecated. "
"Please use the qt_policy(SET) command to set the QTP0001 policy, "
"or use the qt_standard_project_setup() command to set your preferred "
"REQUIRES to get the preferred behavior. "
"Check https://doc.qt.io/qt-6/qt-cmake-policy-qtp0001.html for policy details.")
endif()
elseif(arg_RESOURCE_PREFIX)
_qt_internal_canonicalize_resource_path("${arg_RESOURCE_PREFIX}" arg_RESOURCE_PREFIX)
elseif(arg_NO_RESOURCE_TARGET_PATH)
# Suppress the warning if NO_RESOURCE_TARGET_PATH is given.
# In that case, we assume the user knows what they want.
set(arg_RESOURCE_PREFIX "/")
else()
__qt_internal_setup_policy(QTP0001 "6.5.0"
"':/qt/qml/' is the default resource prefix for QML modules. \
Check https://doc.qt.io/qt-6/qt-cmake-policy-qtp0001.html for policy details."
)
qt6_policy(GET QTP0001 use_auto_prefix_policy)
if ("${use_auto_prefix_policy}" STREQUAL "NEW")
set(arg_RESOURCE_PREFIX "/qt/qml")
else()
set(arg_RESOURCE_PREFIX "/")
endif()
endif()
if(arg_NO_RESOURCE_TARGET_PATH)
set(qt_qml_module_resource_prefix "${arg_RESOURCE_PREFIX}")
else()
if(arg_RESOURCE_PREFIX STREQUAL "/") # Checked so we prevent double-slash
set(qt_qml_module_resource_prefix "/${arg_TARGET_PATH}")
else()
set(qt_qml_module_resource_prefix "${arg_RESOURCE_PREFIX}/${arg_TARGET_PATH}")
endif()
endif()
list(REMOVE_DUPLICATES all_qml_import_paths)
set_target_properties(${target} PROPERTIES
QT_QML_MODULE_NO_LINT "${arg_NO_LINT}"
QT_QML_MODULE_NO_CACHEGEN "${arg_NO_CACHEGEN}"
QT_QML_MODULE_NO_GENERATE_QMLDIR "${arg_NO_GENERATE_QMLDIR}"
QT_QML_MODULE_NO_GENERATE_EXTRA_QMLDIRS "${arg_NO_GENERATE_EXTRA_QMLDIRS}"
QT_QML_MODULE_NO_PLUGIN "${arg_NO_PLUGIN}"
QT_QML_MODULE_NO_PLUGIN_OPTIONAL "${arg_NO_PLUGIN_OPTIONAL}"
QT_QML_MODULE_NO_IMPORT_SCAN "${arg_NO_IMPORT_SCAN}"
_qt_qml_module_follow_foreign_versioning "${arg_FOLLOW_FOREIGN_VERSIONING}"
QT_QML_MODULE_URI "${arg_URI}"
QT_QML_MODULE_TARGET_PATH "${arg_TARGET_PATH}"
QT_QML_MODULE_VERSION "${arg_VERSION}"
QT_QML_MODULE_CLASS_NAME "${arg_CLASS_NAME}"
QT_QML_MODULE_PLUGIN_TARGET "${arg_PLUGIN_TARGET}"
QT_QML_MODULE_INSTALLED_PLUGIN_TARGET "${arg_INSTALLED_PLUGIN_TARGET}"
# Also Save the PLUGIN_TARGET values in a separate property to circumvent
# https://gitlab.kitware.com/cmake/cmake/-/issues/21484 when exporting the properties
_qt_qml_module_plugin_target "${arg_PLUGIN_TARGET}"
_qt_qml_module_installed_plugin_target "${arg_INSTALLED_PLUGIN_TARGET}"
QT_QML_MODULE_DESIGNER_SUPPORTED "${arg_DESIGNER_SUPPORTED}"
QT_QML_MODULE_IS_STATIC "${arg___QT_INTERNAL_STATIC_MODULE}"
QT_QML_MODULE_IS_SYSTEM "${arg___QT_INTERNAL_SYSTEM_MODULE}"
QT_QML_MODULE_OUTPUT_DIRECTORY "${arg_OUTPUT_DIRECTORY}"
QT_QML_MODULE_RESOURCE_PREFIX "${qt_qml_module_resource_prefix}"
QT_QML_MODULE_PAST_MAJOR_VERSIONS "${arg_PAST_MAJOR_VERSIONS}"
# TODO: Check how this is used by qt6_android_generate_deployment_settings()
QT_QML_IMPORT_PATH "${all_qml_import_paths}"
)
if(arg_TYPEINFO)
set_target_properties(${target} PROPERTIES
QT_QML_MODULE_TYPEINFO "${arg_TYPEINFO}"
)
endif()
# Executables don't have a plugin target, so no need to export the properties.
if(NOT backing_target_type STREQUAL "EXECUTABLE" AND NOT is_android_executable)
set_property(TARGET ${target} APPEND PROPERTY
EXPORT_PROPERTIES _qt_qml_module_plugin_target _qt_qml_module_installed_plugin_target
)
endif()
if(NOT arg_NO_GENERATE_QMLTYPES)
set(type_registration_extra_args "")
if(arg_NAMESPACE)
list(APPEND type_registration_extra_args NAMESPACE ${arg_NAMESPACE})
endif()
set_target_properties(${target} PROPERTIES _qt_internal_has_qmltypes TRUE)
_qt_internal_qml_type_registration(${target} ${type_registration_extra_args})
else()
set_target_properties(${target} PROPERTIES _qt_internal_has_qmltypes FALSE)
endif()
set(output_targets)
if(NOT arg_NO_GENERATE_QMLDIR)
_qt_internal_target_generate_qmldir(${target})
set_source_files_properties(${arg_OUTPUT_DIRECTORY}/qmldir
PROPERTIES GENERATED TRUE
)
if(${arg___QT_INTERNAL_DISAMBIGUATE_QMLDIR_RESOURCE})
# TODO: Make this the default and remove the option
string(REPLACE "/" "_" qmldir_resource_name "${target}_qmldir_${arg_TARGET_PATH}")
else()
# Embed qmldir in qrc. The following comments relate mostly to Qt5->6 transition.
# The requirement to keep the same resource name might no longer apply, but it doesn't
# currently appear to cause any hinderance to keep it.
# The qmldir resource name needs to match the one generated by qmake's qml_module.prf,
# to ensure that all Q_INIT_RESOURCE(resource_name) calls in Qt code don't lead to
# undefined symbol errors when linking an application project.
# The Q_INIT_RESOURCE() calls are not strictly necessary anymore because the CMake Qt
# build passes around the compiled resources as object files.
# These object files have global initiliazers that don't get discared when linked into
# an application (as opposed to when the resource libraries were embedded into the
# static libraries when Qt was built with qmake).
# The reason to match the naming is to ensure that applications link successfully
# regardless if Qt was built with CMake or qmake, while the build system transition
# phase is still happening.
string(REPLACE "/" "_" qmldir_resource_name "qmake_${arg_TARGET_PATH}")
endif()
# The qmldir file ALWAYS has to be under the target path, even in the
# resources. If it isn't, an explicit import can't find it. We need a
# second copy NOT under the target path if NO_RESOURCE_TARGET_PATH is
# given so that the implicit import will work.
set(prefixes "${qt_qml_module_resource_prefix}")
if(arg_NO_RESOURCE_TARGET_PATH)
# The above prefixes item won't include the target path, so add a
# second one that does.
if(qt_qml_module_resource_prefix STREQUAL "/")
list(APPEND prefixes "/${arg_TARGET_PATH}")
else()
list(APPEND prefixes "${qt_qml_module_resource_prefix}/${arg_TARGET_PATH}")
endif()
endif()
set_source_files_properties(${arg_OUTPUT_DIRECTORY}/qmldir
PROPERTIES QT_RESOURCE_ALIAS "qmldir"
)
foreach(prefix IN LISTS prefixes)
set(resource_targets)
qt6_add_resources(${target} ${qmldir_resource_name}
FILES ${arg_OUTPUT_DIRECTORY}/qmldir
PREFIX "${prefix}"
OUTPUT_TARGETS resource_targets
)
# Save the resource name in a property so we can reference it later in a qml plugin
# constructor, to avoid discarding the resource if it's in a static library.
__qt_internal_sanitize_resource_name(
sanitized_qmldir_resource_name "${qmldir_resource_name}")
set_property(TARGET ${target} APPEND PROPERTY
_qt_qml_module_sanitized_resource_names "${sanitized_qmldir_resource_name}")
list(APPEND output_targets ${resource_targets})
# If we are adding the same file twice, we need a different resource
# name for the second one. It has the same QT_RESOURCE_ALIAS but a
# different prefix, so we can't put it in the same resource.
string(APPEND qmldir_resource_name "_copy")
endforeach()
endif()
if(NOT arg_NO_PLUGIN AND NOT arg_NO_CREATE_PLUGIN_TARGET)
# This also handles the case where ${arg_PLUGIN_TARGET} already exists,
# including where it is the same as ${target}. If ${arg_PLUGIN_TARGET}
# already exists, it will update the necessary things that are specific
# to qml plugins.
if(TARGET ${arg_PLUGIN_TARGET})
set(plugin_args "")
else()
set(plugin_args ${lib_type})
endif()
list(APPEND plugin_args ${no_gen_source})
if(arg_NAMESPACE)
list(APPEND plugin_args NAMESPACE ${arg_NAMESPACE})
endif()
qt6_add_qml_plugin(${arg_PLUGIN_TARGET}
${plugin_args}
OUTPUT_DIRECTORY ${arg_OUTPUT_DIRECTORY}
BACKING_TARGET ${target}
CLASS_NAME ${arg_CLASS_NAME}
)
endif()
if(TARGET "${arg_PLUGIN_TARGET}" AND NOT arg_PLUGIN_TARGET STREQUAL target)
target_link_libraries(${arg_PLUGIN_TARGET} PRIVATE ${target})
endif()
target_sources(${target} PRIVATE ${arg_SOURCES})
# QML tooling might need to map build dir paths to source dir paths. Create
# a mapping file before qt6_target_qml_sources() to be able to use it
_qt_internal_qml_map_build_files(${target} ${qt_qml_module_resource_prefix} dir_map_qrc)
# use different property from _qt_generated_qrc_files since this qrc is
# special (and is not a real resource file)
set_property(TARGET ${target} APPEND PROPERTY _qt_qml_meta_qrc_files "${dir_map_qrc}")
set(cache_target)
qt6_target_qml_sources(${target}
__QT_INTERNAL_FORCE_DEFER_QMLDIR
QML_FILES ${arg_QML_FILES}
RESOURCES ${arg_RESOURCES}
OUTPUT_TARGETS cache_target
PREFIX "${qt_qml_module_resource_prefix}"
)
list(APPEND output_targets ${cache_target})
# Build an init object library for static plugins and propagate it along with the plugin
# target.
# TODO: Figure out if we can move this code block into qt_add_qml_plugin. Need to consider
# various corner cases.
# QTBUG-96937
if(TARGET "${arg_PLUGIN_TARGET}")
get_target_property(plugin_lib_type ${arg_PLUGIN_TARGET} TYPE)
if(plugin_lib_type STREQUAL "STATIC_LIBRARY")
__qt_internal_add_static_plugin_init_object_library(
"${arg_PLUGIN_TARGET}" plugin_init_target)
list(APPEND output_targets ${plugin_init_target})
__qt_internal_propagate_object_library("${arg_PLUGIN_TARGET}" "${plugin_init_target}")
endif()
endif()
if(NOT arg_NO_GENERATE_QMLDIR)
if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.19.0")
# Defer the write to allow more qml files to be added later by calls to
# qt6_target_qml_sources(). We wrap the deferred call with EVAL CODE
# so that ${target} is evaluated now rather than the end of the scope.
# We also delay target finalization until after our deferred write
# because the qmldir file must be written before any finalizer
# might call qt_import_qml_plugins().
cmake_language(EVAL CODE
"cmake_language(DEFER ID_VAR write_id CALL _qt_internal_write_deferred_qmldir_file ${target})"
)
_qt_internal_delay_finalization_until_after(${write_id})
else()
# Can't defer the write, have to do it now
_qt_internal_write_deferred_qmldir_file(${target})
endif()
endif()
if (arg_ENABLE_TYPE_COMPILER)
if (DEFINED arg_TYPE_COMPILER_NAMESPACE AND NOT $<STREQUAL:"${arg_TYPE_COMPILER_NAMESPACE}","">)
set(qmltc_namespace ${arg_TYPE_COMPILER_NAMESPACE})
else()
string(REPLACE "." "::" qmltc_namespace "${arg_URI}")
endif()
_qt_internal_target_enable_qmltc(${target}
QML_FILES ${arg_QML_FILES}
IMPORT_PATHS ${arg_IMPORT_PATH}
NAMESPACE ${qmltc_namespace}
EXPORT_MACRO_NAME ${arg_QMLTC_EXPORT_DIRECTIVE}
EXPORT_FILE_NAME ${arg_QMLTC_EXPORT_FILE_NAME}
MODULE ${arg_URI}
)
endif()
if(arg_OUTPUT_TARGETS)
set(${arg_OUTPUT_TARGETS} ${output_targets} PARENT_SCOPE)
endif()
set(ensure_set_properties
QT_QML_MODULE_PLUGIN_TYPES_FILE
QT_QML_MODULE_QML_FILES
QT_QML_MODULE_RESOURCES # Original files as provided by the project (absolute)
QT_QML_MODULE_RESOURCE_PATHS # By qmlcachegen (resource paths)
QT_QMLCACHEGEN_DIRECT_CALLS
QT_QMLCACHEGEN_EXECUTABLE
QT_QMLCACHEGEN_ARGUMENTS
)
foreach(prop IN LISTS ensure_set_properties)
get_target_property(val ${target} ${prop})
if("${val}" MATCHES "-NOTFOUND$")
set_target_properties(${target} PROPERTIES ${prop} "")
endif()
endforeach()
if(${QT_QML_GENERATE_QMLLS_INI})
if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.19.0")
# collect all build dirs obtained from all the qt_add_qml_module calls and
# write the .qmlls.ini file in a deferred call
if(NOT "${arg_OUTPUT_DIRECTORY}" STREQUAL "")
set(output_folder "${arg_OUTPUT_DIRECTORY}")
else()
set(output_folder "${CMAKE_CURRENT_BINARY_DIR}")
endif()
string(REPLACE "." ";" uri_bits "${arg_URI}")
set(build_folder "${output_folder}")
foreach(bit IN LISTS uri_bits)
get_filename_component(build_folder "${build_folder}" DIRECTORY)
endforeach()
get_directory_property(_qmlls_ini_build_folders _qmlls_ini_build_folders)
list(APPEND _qmlls_ini_build_folders "${build_folder}")
set_directory_properties(PROPERTIES _qmlls_ini_build_folders "${_qmlls_ini_build_folders}")
# if no call with id 'qmlls_ini_generation_id' was deferred for this directory, do it now
cmake_language(DEFER GET_CALL qmlls_ini_generation_id call)
if("${call}" STREQUAL "")
cmake_language(EVAL CODE
"cmake_language(DEFER ID qmlls_ini_generation_id CALL _qt_internal_write_deferred_qmlls_ini_file ${target})"
)
endif()
else()
get_property(__qt_internal_generate_qmlls_ini_warning
GLOBAL
PROPERTY __qt_internal_generate_qmlls_ini_warning