Qt5/Qt6 rules for Bazel
The set of rules to build GUI applications with Qt Framework. I created these rules when QtQuick3D was introduced to experiment with it. I finally found time to clean up rules and make them public, so others that both love Bazel and Qt can use them.
Rules should support all available pre-installed Qt modules. qt_local_repo (a repository rule) queries qmake
to get full info
about available Qt tools, libraries, plugins and QtQuick imports.
So, all modules should be supported (if installed). Though, I tested mostly QtWidget, QtQuick/QtQml and QtQuick3D.
- moc -- qt_cc_moc and qt_cc_moc_import
- rcc -- qt_cc_rcc and qt_qrc
- uic -- qt_cc_uic
- QtQuick/QtQml -- qt_qml_cc_module and qt_qml_import
- balsam -- qt_balsam
Besides the aforementioned set of rules, there are two convenience macros available: qt_cc_library and qt_cc_binary.
Rules are intended to be used when one needs more fine-grained Qt target composition.
Macros, usually, are the first choice to build Qt targets, because of their simplicity of use. In essence they still use the same Qt rules, but nicely hide implementation details from a user.
Tested on Ubuntu (22.04) and macOS Ventura 13.3.1 (Apple Silicon).
- deployment is not supported
- dynamic plugins development is not supported
- since rules rely on pre-installed Qt on a host, produces binaries are not hermetic
In order to use rules, Qt needs to be installed on a host and the path to qmake
needs to be provided in qt_local_repo's path
attribute.
See examples/WORKSPACE
for additional info.
Qt can be installed in several ways:
- via
brew
(macOS) - via
apt
(Ubuntu) - via official Qt Installer
- build from source using qt_http_repo and chaining it with qt_local_repo
See release
for the WORKSPACE
setup of the currect release.
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "com_github_kklochkov_rules_qt",
urls = ["<URL>"],
sha256 = "<SHA256>",
)
load("@com_github_kklochkov_rules_qt//qt:repositories.bzl", "rules_qt_dependencies", "rules_qt_toolchains")
rules_qt_dependencies()
rules_qt_toolchains()
# Qt local repo
load("@com_github_kklochkov_rules_qt//qt:qt_local_repo.bzl", "qt_local_repo")
qt_local_repo(
name = "qt",
path = "/path/to/qt/bin",
)
IMPORTANT NOTE:
qt_local_repo should be named qt
, because the repo is used to retrieve
installation paths from qtconf.bzl
which are used by qt_cc_binary.
In case a different name is used for the repository, it will not be possible to use qt_cc_binary without errors.
One will have to use cc_binary
and manually provide QT_PLUGIN_PATH
, QML2_IMPORT_PATH
and QML_IMPORT_PATH
from
@<custom_qt_repo_nam>//:qtconf.bzl
. See the reference usage.
load("@com_github_kklochkov_rules_qt//qt:defs.bzl", "qt_cc_binary")
qt_cc_binary(
name = "hello_world",
srcs = ["main.cpp"],
deps = ["@qt//:QtCore"],
)
See examples section on how to run examples.
To run examples, cd examples
and follow instructions in README.md.