aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/Qt6QmlBuildInternals.cmake
blob: 161c90644d8aa5870edfa993dcef74e019f77a88 (plain)
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
#
# QtDeclarative Specific extensions
#

include_guard(GLOBAL)

# This function creates a CMake target for qml modules. It will also make
# sure that if no C++ source are present, that qml files show up in the project
# in an IDE. Finally, it will also create a custom ${target}_qmltypes which
# can be used to generate the respective plugins.qmltypes file.
#
#  URI: Module's uri.
#  TARGET_PATH: Expected installation path for the Qml Module. Equivalent
#  to the module's URI where '.' is replaced with '/'. Use this to override the
#  default substitution pattern.
#  VERSION: Version of the qml module
#  SKIP_TYPE_REGISTRATION: All qml files are expected to be registered by the
#  c++ plugin code.
#  PLUGIN_OPTIONAL: Any plugins are optional
#
function(qt_internal_add_qml_module target)

    set(qml_module_optional_args
        GENERATE_QMLTYPES
        INSTALL_QMLTYPES
        DESIGNER_SUPPORTED
        DO_NOT_INSTALL
        SKIP_TYPE_REGISTRATION
        PLUGIN_OPTIONAL
    )

    set(qml_module_single_args
        URI
        TARGET_PATH
        VERSION
        CLASSNAME
    )

    set(qml_module_multi_args
        IMPORTS
        OPTIONAL_IMPORTS
        TYPEINFO
        DEPENDENCIES
        PAST_MAJOR_VERSIONS
    )

    qt_parse_all_arguments(arg "qt_add_qml_module"
        "${__qt_add_plugin_optional_args};${qml_module_optional_args}"
        "${__qt_add_plugin_single_args};${qml_module_single_args}"
        "${__qt_add_plugin_multi_args};${qml_module_multi_args}" ${ARGN})

    if (NOT arg_URI)
        message(FATAL_ERROR "qt_add_qml_module called without specifying the module's uri. Please specify one using the URI parameter.")
    endif()

    set(target_path ${arg_TARGET_PATH})

    if (NOT arg_VERSION)
        message(FATAL_ERROR "qt_add_qml_module called without specifying the module's import version. Please specify one using the VERSION parameter.")
    endif()

    if (NOT arg_TARGET_PATH)
        string(REPLACE "." "/" arg_TARGET_PATH ${arg_URI})
    endif()

    qt_remove_args(plugin_args
        ARGS_TO_REMOVE
            ${target}
            ${qml_module_multi_args}
            ${qml_module_single_args}
        ALL_ARGS
            ${__qt_add_plugin_optional_args}
            ${__qt_add_plugin_single_args}
            ${qml_module_single_args}
            ${__qt_add_plugin_multi_args}
            ${qml_module_multi_args}
        ARGS
            ${ARGV}
    )

    qt_internal_add_plugin(${target}
        TYPE
            qml_plugin
        QML_TARGET_PATH
            "${arg_TARGET_PATH}"
        ${plugin_args}
    )

    set(no_create_option DO_NOT_CREATE_TARGET)

    if (arg_CLASSNAME)
        set(classname_arg CLASSNAME ${arg_CLASSNAME})
    endif()

    if (arg_DESIGNER_SUPPORTED)
        set(designer_supported_arg DESIGNER_SUPPORTED)
    endif()

    if (arg_SKIP_TYPE_REGISTRATION)
        set(skip_registration_arg SKIP_TYPE_REGISTRATION)
    endif()

    if (arg_PLUGIN_OPTIONAL)
        set(plugin_optional_arg PLUGIN_OPTIONAL)
    endif()

    if (arg_GENERATE_QMLTYPES)
        set(generate_qmltypes_arg GENERATE_QMLTYPES)
    endif()

    if (arg_INSTALL_QMLTYPES)
        set(install_qmltypes_arg INSTALL_QMLTYPES)
    endif()


    # Because qt_internal_add_qml_module does not propagate its SOURCES option to
    # qt6_add_qml_module, but only to qt_internal_add_plugin, we need a way to tell
    # qt6_add_qml_module if it should generate a dummy plugin cpp file. Otherwise we'd generate
    # a dummy plugin.cpp file twice and thus cause duplicate symbol issues.
    if (NOT arg_SOURCES)
        set(pure_qml_module "PURE_MODULE")
    endif()

    qt_path_join(qml_module_install_dir ${QT_INSTALL_DIR} "${INSTALL_QMLDIR}/${arg_TARGET_PATH}")

    if (arg_SOURCES AND NOT arg_TYPEINFO)
        set(arg_TYPEINFO "plugins.qmltypes")
    endif()

    qt6_add_qml_module(${target}
        ${designer_supported_arg}
        ${no_create_option}
        ${skip_registration_arg}
        ${plugin_optional_arg}
        ${classname_arg}
        ${generate_qmltypes_arg}
        ${install_qmltypes_arg}
        ${pure_qml_module}
        RESOURCE_PREFIX "/qt-project.org/imports"
        TARGET_PATH ${arg_TARGET_PATH}
        URI ${arg_URI}
        VERSION ${arg_VERSION}
        PAST_MAJOR_VERSIONS ${arg_PAST_MAJOR_VERSIONS}
        QML_FILES ${arg_QML_FILES}
        IMPORTS "${arg_IMPORTS}"
        OPTIONAL_IMPORTS "${arg_OPTIONAL_IMPORTS}"
        TYPEINFO "${arg_TYPEINFO}"
        DO_NOT_INSTALL_METADATA
        INSTALL_LOCATION "${qml_module_install_dir}"
        DEPENDENCIES ${arg_DEPENDENCIES}
        RESOURCE_EXPORT "${INSTALL_CMAKE_NAMESPACE}${target}Targets"
    )

    get_target_property(qmldir_file ${target} QT_QML_MODULE_QMLDIR_FILE)
    get_target_property(plugin_types ${target} QT_QML_MODULE_PLUGIN_TYPES_FILE)
    set(files_to_install)
    if (EXISTS ${plugin_types})
        list(APPEND files_to_install ${plugin_types})
        qt_copy_or_install(FILES ${plugin_types}
            DESTINATION "${qml_module_install_dir}"
        )

        if(QT_WILL_INSTALL)
            # plugin.qmltypes when present should also be copied to the
            # cmake binary dir when doing prefix builds
            file(COPY ${plugin_types}
                DESTINATION "${QT_BUILD_DIR}/${INSTALL_QMLDIR}/${arg_TARGET_PATH}"
            )
        endif()
    endif()

    list(APPEND files_to_install ${qmldir_file})
    if (QT_WILL_INSTALL)
        install(FILES ${files_to_install} DESTINATION ${qml_module_install_dir})
    endif()

    set(copy_destination "${QT_BUILD_DIR}/${INSTALL_QMLDIR}/${arg_TARGET_PATH}")
    foreach(file IN LISTS files_to_install)
        get_filename_component(file_name "${file}" NAME)
        add_custom_command(TARGET ${target} POST_BUILD
            COMMAND ${CMAKE_COMMAND} -E copy_if_different
                "${file}"
                "${copy_destination}/${file_name}"
            COMMENT "Copying ${file} to ${copy_destination}"
        )
    endforeach()
endfunction()

if(NOT QT_NO_INTERNAL_COMPATIBILITY_FUNCTIONS)
    # Compatibility functions that should be removed once all their usages are removed.
    function(add_qml_module)
        qt_add_qml_module(${ARGV})
    endfunction()

    function(qt_add_qml_module)
        qt_internal_add_qml_module(${ARGV})
    endfunction()
endif()