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-qtquick-compiler-tech.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-qtquick-compiler-tech.html
6\ingroup overviews
7\title Qt Quick Compiler
8\brief Overview of Qt Quick Compiler components
9
10Qt Quick Compiler lets you process QML and JavaScript code at compile
11time, rather than at run time. This allows for:
12
13\list
14 \li Faster application startup
15 \li Faster evaluation of bindings and functions
16\endlist
17
18The Qt Quick Compiler consist of two components:
19\list
20 \li \l {QML type compiler}
21 \li \l {QML script compiler}
22\endlist
23
24\note \e qmltc, \e qmlsc and \l qmlcachegen are internal build tools. If you
25need to care about their invocation, you are either writing a build system, or
26you are doing something wrong.
27
28\section1 The QML type compiler
29
30The \l{QML type compiler}, \e(qmltc) compiles QML types to C++ classes. These C++
31classes are then added to your application and can be instantiated from other
32C++ code. This way you can avoid much of the overhead of using \l{QQmlComponent}
33to create instances of your QML types. In order to benefit from \l qmltc, you
34need to adapt your C++ code and make use of the new classes.
35
36\l qmltc can only compile a QML document if it completely understands its
37structure. It will fail if an unsupported language feature is encountered.
38It does not have to understand the JavaScript code in bindings and functions,
39though.
40
41\section1 The QML script compiler
42
43The \l{QML script compiler}, (\e qmlsc and \e qmlcachegen) compiles bindings and
44functions to both, an efficient byte code and C++ functions. This process
45automatically happens behind the scenes if you are using \l{qt_add_qml_module}
46to specify your QML modules. For more information about available options to
47control different aspects of QML compilation, see
48\l {Caching compiled QML sources}.
49
50At compile time, for each QML or JavaScript
51document a corresponding C++ file is created and built into the application. The
52C++ file then contains a \e{QML compilation unit}, which consists of:
53
54\list
55 \li An efficient representation of the document structure
56 \li Byte code for all functions and bindings in the document
57 \li C++ code for functions and bindings the compiler fully understands
58\endlist
59
60The QML engine then refrains from compiling the QML or JavaScript source code
61at run time and instead uses the pre-built compilation unit to load the QML
62component and its functions and bindings more quickly. The functions and
63bindings that were compiled to C++ can also be executed faster. Other bindings
64and functions are either interpreted directly from the byte code, or compiled
65to machine code via a JIT compilation step at run time. At compile time, more
66involved type analysis can be performed. Therefore, the generated C++ code
67is generally more efficient than the result of the JIT compilation.
68
69There are limitations on what JavaScript constructs can be compiled to C++.
70For more information on these limitations, see
71\l {Limitations when compiling JavaScript to C++}. For statistics on how well
72a QML project can be compiled, see
73\l {Obtaining statistics about the compilation of functions and bindings}.
74
75
76\e{qmlsc} will be used instead of \e{qmlcachegen} if the Qt Quick Compiler
77Extensions are installed. It has the following additional features over
78\e{qmlcachegen}:
79
80\list
81 \li It can compile documents in Direct Mode. In that case, the C++ headers
82 of the types underpinning other QML components are directly included
83 and the methods of those types are directly called. Conversely, in
84 Indirect Mode, \e qmlcachegen or \e qmlsc call methods through the
85 lookup mechanism which is also used in the interpreter and JIT.
86 \li It can compile documents in Static Mode. In that case, \e qmlsc assumes
87 that no properties of any types exposed to C++ can be shadowed by
88 derived types. This allows for more bindings and functions to be
89 compiled, but generates invalid code if any properties are shadowed.
90\endlist
91
92Instead of producing C++ as output, \l {qmlsc} and \l {qmlcachegen} can also
93generate .qmlc, .jsc and .mjsc "cache files". These still contain a QML
94compilation unit each, and can be loaded by the QML engine to avoid
95re-compilation. They can only contain document structure and byte code, though.
96Compilation of bindings and functions to C++ is omitted if cache files are
97produced. Neither the CMake nor the qmake build system offered by Qt expose this
98functionality.
99
100\section1 Summary
101
102The following table summarizes the differences between \l{qmltc},
103\l{qmlcachegen} and \l{qmlsc}:
104
105\table
106 \header
107 \li \e qmltc
108 \li \e qmlcachegen
109 \li \e qmlsc
110 \row
111 \li Compiles QML types to C++ classes
112 \li Compiles QML documents to QML compilation units
113 \li Compiles QML documents to QML compilation units
114 \row
115 \li Generated output acts as faster alternative to
116 \l{QQmlComponent}-based object creation.
117 \li Generated output is used internally by the QML engine to avoid
118 re-compilation, and to speed up execution.
119 \li Generated output is used internally by the QML engine to avoid
120 re-compilation, and to speed up execution. Direct Mode and Static
121 Mode can further accelerate your application.
122 \row
123 \li Available for all versions of Qt
124 \li Available for all versions of Qt
125 \li Available for commercial customers
126\endtable
127*/