Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qtp0005.qdoc
Go to the documentation of this file.
1// Copyright (C) 2023 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4/*!
5\page qt-cmake-policy-qtp0005.html
6\ingroup qt-cmake-policies
7
8\title QTP0005
9\keyword qt_cmake_policy_qtp0005
10
11\summary {qt_add_qml_module dependency keywords accept CMake targets}
12
13This policy was introduced in Qt 6.8. It allows passing CMake targets to
14\l{qt_add_qml_module}{qt_add_qml_module()} via the \c DEPENDENCIES, \c IMPORTS,
15\c OPTIONAL_IMPORTS, and \c DEFAULT_IMPORTS keywords.
16
17Enabling this policy means that arguments passed to those keywords can be prefixed
18with \c TARGET, and are then treated as a CMake target name. The QML module URI
19and import path are derived automatically from the target.
20
21The \c OLD behavior of this policy is that the token sequence \c{TARGET name} is
22treated as two separate URIs, \c{TARGET} and \c{name}.
23
24The \c NEW behavior of this policy is that \c TARGET is a keyword. The URI is
25extracted from the QML module target that follows. It is a hard error if the name
26following \c TARGET does not refer to an existing target, or if that target does
27not correspond to a QML module.
28
29In both the \c NEW and the \c OLD behavior it is possible to specify a module
30version by appending a slash and the version. See
31\l{Declaring module dependencies} for more details.
32
33\section1 Background
34
35Before this policy, QML module dependencies had to be declared by their string
36URI, for example \c{QtQuick} or \c{com.example.mymodule}. This meant that the
37URI and, when needed, the import path of the dependency had to be kept consistent
38with the CMake target that provides the module.
39
40With the \c NEW behavior, you can refer to a dependency by its CMake target
41name instead. The build system then determines the correct URI and import path
42from the target automatically. This removes the need to manually specify
43\l{qt_add_qml_module}{IMPORT_PATH} entries for dependencies outside the default
44QML import path, reduces redundancy, and eliminates a class of copy-paste
45mistakes.
46
47\section1 Example
48
49Consider a module that depends on another module defined in the same project.
50With the \c OLD behavior (or before Qt 6.8), the URI must be spelled out
51explicitly:
52
53\badcode
54qt_add_qml_module(my_module
55 URI MyModule
56 VERSION 1.0
57 DEPENDENCIES
58 OtherModule
59)
60\endcode
61
62With the \c NEW behavior (policy QTP0005 set to \c NEW), you can refer to the
63dependency by its CMake target name:
64
65\badcode
66qt_policy(SET QTP0005 NEW)
67
68qt_add_qml_module(other_module
69 URI OtherModule
70 VERSION 1.0
71)
72
73qt_add_qml_module(my_module
74 URI MyModule
75 VERSION 1.0
76 DEPENDENCIES
77 TARGET other_module
78)
79\endcode
80
81The same \c TARGET syntax is available for \c IMPORTS, \c OPTIONAL_IMPORTS,
82and \c DEFAULT_IMPORTS.
83
84As shown above, you can set the policy explicitly with
85\c{qt_policy(SET QTP0005 NEW)} before calling \c{qt_add_qml_module()}.
86Alternatively, call
87\l{qt6_standard_project_setup}{qt_standard_project_setup()} with a
88\c REQUIRES version of 6.8 or later to enable all policies up to that version.
89
90Qt 6.8 issues a deprecation warning if you use \c TARGET as a token in
91\c DEPENDENCIES, \c IMPORTS, \c OPTIONAL_IMPORTS, or \c DEFAULT_IMPORTS while
92the policy is set to \c OLD.
93Use the \l qt_policy command to suppress the warning by explicitly setting
94the policy to \c OLD or \c NEW.
95
96\qtpolicydeprecatedbehavior
97
98\sa qt_policy, {qt6_standard_project_setup}{qt_standard_project_setup()},
99 qt_cmake_policies, qt_add_qml_module
100
101*/