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
qtqml-qml-script-compiler.qdoc
Go to the documentation of this file.
1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4/*!
5\page qtqml-qml-script-compiler.html
6\title QML script compiler
7\brief A tool to compile functions and expressions in QML.
8\keyword qmlsc
9\ingroup qtqml-tooling
10\ingroup qtqml-tooling-internal
11
12The QML script compiler compiles functions and expressions in QML and JavaScript
13files to a byte code that can be interpreted or Just-in-time compiled by the
14QML engine.
15
16In addition, it compiles some functions and expressions in QML files into C++
17code, within limitations set by the nature of JavaScript. It generates C++ code
18for functions that can be exhaustively analyzed. The following flow chart
19explains the compilation workflow.
20
21\image qmlsc-compilation-scheme.png {Flow chart shows the compilation schemes}
22
23QML script compiler is available in two versions. One is \e qmlcachegen, which
24is a part of the \l{Qt Quick Compiler}. Another one is \e qmlsc, which is a part
25of the commercial-only add-on \e{Qt Quick Compiler Extensions}.
26
27\section1 qmlcachegen
28\e qmlcachegen uses the meta-object system and generates lookups and stores them in a
29central place, a compilation unit. The compilation unit contains a representation of
30document structure, compact byte code representation for each function and expression,
31and native code for functions and bindings that compiler fully understands.
32The byte code in a compilation unit can be used by the QML engine to avoid re-compilation
33and to speed up execution.
34
35\section1 qmlsc
36\e qmlsc, on the flip side, extends the base functionality of qmlcachegen by providing
37two extra modes.
38
39\list
40\li \l {static mode}
41\li \l {direct mode}
42\endlist
43
44\section2 static mode
45In static mode, qmlsc assumes that no properties of any types exposed to C++ can be
46shadowed by derived types. It eliminates the shadow checking mechanism and allows more
47JavaScript code to be compiled to C++ and eventually generates faster code.
48
49To enable static mode in qmlsc, you should pass \c{--static} via \c{QT_QMLCACHEGEN_ARGUMENTS} to \l{qt_add_qml_module}.
50\badcode
51 qt_add_qml_module(someTarget
52 ...
53 )
54
55 set_target_properties(someTarget PROPERTIES
56 QT_QMLCACHEGEN_ARGUMENTS "--static"
57 )
58\endcode
59
60\warning qmlsc static-mode generates invalid code if any properties are shadowed in
61the QML document.
62
63\section2 direct mode
64In direct mode, qmlsc assumes that all C++ types used in your QML code are available
65and can be included as C++ headers to the generated code. Then the generated code
66accesses or modifies properties by directly calling getters, setters and invokable
67functions in those headers which makes the execution even faster. This means you have to
68link to private Qt APIs in CMake.
69
70\warning Private Qt APIs change often. You will need to recompile Qt for each new version.
71
72\warning If a type is only defined in a plugin or has no header, you can’t use it in direct mode.
73
74To enable direct mode, you should consider the followings:
75
76\list
77 \li you should pass \c{--direct-calls} via \c{QT_QMLCACHEGEN_ARGUMENTS} to \l{qt_add_qml_module}.
78
79\badcode
80 qt_add_qml_module(someTarget
81 ...
82 )
83
84 set_target_properties(someTarget PROPERTIES
85 QT_QMLCACHEGEN_ARGUMENTS "--direct-calls"
86 )
87\endcode
88
89 \li Link all the relavant private Qt modules instead of their public counterparts.
90\badcode
91 find_package(Qt6 COMPONENTS QmlPrivate QuickPrivate)
92
93 qt_add_qml_module(someTarget
94 ...
95 )
96
97 target_link_libraries(someTarget PRIVATE
98 Qt::QmlPrivate
99 Qt::QuickPrivate
100 ...
101 )
102\endcode
103
104 \li Do not set the \c{PLUGIN_TARGET} to be the same as the backing library target.
105\badcode
106 # direct mode will not function in this setup.
107 qt_add_qml_module(someTarget
108 PLUGIN_TARGET someTarget
109 ...
110 )
111\endcode
112\endlist
113
114\section1 Limitations when compiling JavaScript to C++
115
116Many JavaScript constructs cannot be efficiently represented in C++. The QML
117script compiler skips the C++ code generation for functions that contain such
118constructs and only generates byte code to be interpreted or run through the
119Just-in-time compiler. Most common QML expressions are rather simple: value
120lookups on QObjects, arithmetics, simple if/else or loop constructs. Those can
121easily be expressed in C++, and doing so makes your application run faster.
122
123\section1 Obtaining statistics about the compilation of functions and bindings
124
125The QML Script Compiler records statistics when compiling QML to C++. These can
126give insight into the compilation of functions and bindings and whether it was
127successful or which error has caused it to fail. In addition to
128showing these results per binding or function, the statistics are also
129summarized per file, per module, and at the project level. This can give a
130quick overview of how well a project is being compiled.
131
132To show statistics, invoke the \e{all_aotstats} cmake target.
133
134The collection of compilation statistics is enabled by default. In order to
135disable it, set the \l{QT_QML_GENERATE_AOTSTATS} CMake variable to OFF before
136the first call to \l{qt_add_qml_module}.
137
138\note These statistics are only available for modules registered through the
139\l{qt_add_qml_module} cmake API
140
141*/