├── .gitignore ├── tests ├── qt_resource │ ├── file1.txt │ ├── subdir │ │ └── file2.txt │ ├── BUILD │ └── main.cc ├── qt_ui_library │ ├── BUILD │ └── mainwindow.ui └── qt_qml │ ├── main.qml │ ├── BUILD │ └── main.cc ├── BUILD.local_qt.tpl ├── BUILD ├── WORKSPACE ├── .bazelci └── presubmit.yml ├── .appveyor.yml ├── .circleci └── config.yml ├── tools ├── qt_toolchain.bzl └── BUILD ├── qt_configure.bzl ├── README.md ├── qt.BUILD ├── qt.bzl └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | bazel-* 2 | -------------------------------------------------------------------------------- /tests/qt_resource/file1.txt: -------------------------------------------------------------------------------- 1 | file1 -------------------------------------------------------------------------------- /tests/qt_resource/subdir/file2.txt: -------------------------------------------------------------------------------- 1 | file2 -------------------------------------------------------------------------------- /BUILD.local_qt.tpl: -------------------------------------------------------------------------------- 1 | def local_qt_path(): 2 | return "%{path}" 3 | -------------------------------------------------------------------------------- /BUILD: -------------------------------------------------------------------------------- 1 | # empty BUILD file so that bazel sees this as a valid package directory 2 | -------------------------------------------------------------------------------- /tests/qt_ui_library/BUILD: -------------------------------------------------------------------------------- 1 | load("//:qt.bzl", "qt_ui_library") 2 | 3 | qt_ui_library( 4 | name = "main_window_ui", 5 | ui = "mainwindow.ui", 6 | deps = ["@qt//:qt_widgets"], 7 | ) 8 | -------------------------------------------------------------------------------- /tests/qt_qml/main.qml: -------------------------------------------------------------------------------- 1 | import QtQuick 2.6 2 | import QtQuick.Window 2.1 3 | import QtQuick.Controls 1.2 4 | 5 | ApplicationWindow { 6 | title: "bazel rules_qt qml demo" 7 | 8 | Label { 9 | text: "Hello world" 10 | font.pixelSize: 24 11 | anchors.fill: parent 12 | horizontalAlignment: Text.AlignHCenter 13 | verticalAlignment: Text.AlignVCenter 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /tests/qt_qml/BUILD: -------------------------------------------------------------------------------- 1 | load("//:qt.bzl", "qt_resource") 2 | 3 | # simple demo binary that shows how to use qml 4 | cc_binary( 5 | name = "main", 6 | srcs = ["main.cc"], 7 | deps = [ 8 | ":main_qrc", 9 | "@qt//:qt_qml", 10 | "@qt//:qt_quick", 11 | "@qt//:qt_widgets", 12 | ], 13 | ) 14 | 15 | qt_resource( 16 | name = "main_qrc", 17 | files = [ 18 | "main.qml", 19 | ], 20 | ) 21 | -------------------------------------------------------------------------------- /tests/qt_resource/BUILD: -------------------------------------------------------------------------------- 1 | load("//:qt.bzl", "qt_resource") 2 | 3 | qt_resource( 4 | name = "resources", 5 | files = [ 6 | "file1.txt", 7 | "subdir/file2.txt", 8 | ], 9 | ) 10 | 11 | # TODO(justbuchanan): refactor this into a cc_test 12 | # For now, test with: 13 | # bazel run //tests/qt_resource:main 14 | cc_binary( 15 | name = "main", 16 | srcs = ["main.cc"], 17 | deps = [ 18 | ":resources", 19 | "@qt//:qt_core", 20 | ], 21 | ) 22 | -------------------------------------------------------------------------------- /WORKSPACE: -------------------------------------------------------------------------------- 1 | workspace(name = "com_justbuchanan_rules_qt") 2 | 3 | load("@com_justbuchanan_rules_qt//:qt_configure.bzl", "qt_configure") 4 | load("@com_justbuchanan_rules_qt//tools:qt_toolchain.bzl", "register_qt_toolchains") 5 | 6 | qt_configure() 7 | 8 | load("@local_config_qt//:local_qt.bzl", "local_qt_path") 9 | 10 | new_local_repository( 11 | name = "qt", 12 | build_file = "@com_justbuchanan_rules_qt//:qt.BUILD", 13 | path = local_qt_path(), 14 | ) 15 | 16 | register_qt_toolchains() 17 | -------------------------------------------------------------------------------- /tests/qt_resource/main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(int argc, char** argv) { 5 | QString path = ":tests/qt_resource/file1.txt"; 6 | QFile file(path); 7 | 8 | if (!file.open(QIODevice::ReadOnly)) { 9 | std::cerr << "failed to open resource file" << std::endl; 10 | exit(1); 11 | } 12 | 13 | std::cerr << "opened resource file" << std::endl; 14 | QString data = file.readAll(); 15 | file.close(); 16 | 17 | std::cout << data.toStdString() << std::endl; 18 | } -------------------------------------------------------------------------------- /.bazelci/presubmit.yml: -------------------------------------------------------------------------------- 1 | --- 2 | tasks: 3 | ubuntu2004: 4 | platform: ubuntu2004 5 | shell_commands: 6 | - "sudo apt update && sudo apt -y install qt5-default qtdeclarative5-dev" 7 | build_targets: 8 | - "//..." 9 | # Uncommented since `brew install qt@5` does currently not work 10 | # see https://github.com/bazelbuild/continuous-integration/issues/1330 11 | # TODO: wait until a solution is provided 12 | #macos: 13 | # shell_commands: 14 | # - "brew install qt@5" 15 | # build_targets: 16 | # - "//..." 17 | -------------------------------------------------------------------------------- /.appveyor.yml: -------------------------------------------------------------------------------- 1 | environment: 2 | APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2019 3 | # Qt is not available compiled with Visual Studio 2019 but it is binary compatible 4 | QT_VERSION: 5.9.9\msvc2017_64 5 | VCVARSALLPATH: C:\"Program Files (x86)\Microsoft Visual Studio"\2019\Community\VC\Auxiliary\Build\vcvarsall.bat 6 | VCVARSALL: x64 7 | nodejs_version: "12.1.0" 8 | npm_version: "6.14.4" 9 | GYP_MSVS_VERSION: 2019 10 | platform: "x64" 11 | 12 | init: 13 | - call %VCVARSALLPATH% %VCVARSALL% 14 | - set PATH=C:\Qt\%QT_VERSION%\bin;C:\Qt\Tools\QtCreator\bin;%PATH%; 15 | 16 | install: 17 | - cmd: appveyor-retry powershell Install-Product node $env:nodejs_version x64 18 | 19 | build_script: 20 | - npx @bazel/bazelisk version 21 | - npx @bazel/bazelisk build //... 22 | -------------------------------------------------------------------------------- /.circleci/config.yml: -------------------------------------------------------------------------------- 1 | # Docs: https://circleci.com/docs/2.0/configuration-reference 2 | version: 2.1 3 | 4 | executors: 5 | main: 6 | docker: 7 | - image: ubuntu:focal-20201106 8 | 9 | jobs: 10 | build: 11 | executor: main 12 | environment: 13 | DEBIAN_FRONTEND: noninteractive 14 | steps: 15 | # setup bazel 16 | - run: echo 'export GOPATH="/go"' >> $BASH_ENV 17 | - run: apt-get update && apt-get install -y golang git 18 | - run: go get github.com/bazelbuild/bazelisk 19 | - run: ln -sf /go/bin/bazelisk /usr/bin/bazel 20 | 21 | - checkout 22 | 23 | - run: apt-get install -y qt5-default qtdeclarative5-dev 24 | - run: bazel build //... 25 | 26 | 27 | # Orchestrate or schedule a set of jobs 28 | workflows: 29 | build: 30 | jobs: 31 | - build 32 | -------------------------------------------------------------------------------- /tests/qt_qml/main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | int main(int argc, char *argv[]) { 8 | QApplication app(argc, argv); 9 | app.setOrganizationName("justbuchanan"); 10 | app.setApplicationName("bazel rules_qt qml demo"); 11 | 12 | QQmlApplicationEngine engine(nullptr); 13 | engine.rootContext()->setContextProperty("main", &engine); 14 | 15 | // load main.qml from qt resources, which are baked into the binary. Path is 16 | // relative to the bazel workspace root. 17 | engine.load((QUrl("qrc:///tests/qt_qml/main.qml"))); 18 | 19 | // get reference to main window from qml 20 | auto win = static_cast(engine.rootObjects()[0]); 21 | if (!win) { 22 | std::cerr << "Failed to load qml" << std::endl; 23 | exit(1); 24 | } 25 | 26 | win->show(); 27 | 28 | return app.exec(); 29 | } 30 | -------------------------------------------------------------------------------- /tools/qt_toolchain.bzl: -------------------------------------------------------------------------------- 1 | QtToolchainInfo = provider( 2 | doc = "Information about how to invoke qt tools.", 3 | fields = ["rcc_path", "uic_path", "moc_path"], 4 | ) 5 | 6 | def _qt_toolchain_impl(ctx): 7 | toolchain_info = platform_common.ToolchainInfo( 8 | qtinfo = QtToolchainInfo( 9 | rcc_path = ctx.attr.rcc_path, 10 | uic_path = ctx.attr.uic_path, 11 | moc_path = ctx.attr.moc_path, 12 | ), 13 | ) 14 | return [toolchain_info] 15 | 16 | qt_toolchain = rule( 17 | implementation = _qt_toolchain_impl, 18 | attrs = { 19 | "rcc_path": attr.string(), 20 | "uic_path": attr.string(), 21 | "moc_path": attr.string(), 22 | }, 23 | ) 24 | 25 | def register_qt_toolchains(): 26 | native.register_toolchains( 27 | "@com_justbuchanan_rules_qt//tools:qt_linux_toolchain", 28 | "@com_justbuchanan_rules_qt//tools:qt_windows_toolchain", 29 | "@com_justbuchanan_rules_qt//tools:qt_osx_toolchain", 30 | ) 31 | -------------------------------------------------------------------------------- /tests/qt_ui_library/mainwindow.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | MainWindow 4 | 5 | 6 | 7 | 0 8 | 0 9 | 324 10 | 234 11 | 12 | 13 | 14 | MainWindow 15 | 16 | 17 | 18 | 19 | 20 | 21 | PushButton 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 0 31 | 0 32 | 324 33 | 21 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /tools/BUILD: -------------------------------------------------------------------------------- 1 | package(default_visibility = ["//visibility:public"]) 2 | 3 | load(":qt_toolchain.bzl", "qt_toolchain") 4 | 5 | toolchain_type(name = "toolchain_type") 6 | 7 | qt_toolchain( 8 | name = "qt_linux", 9 | # TODO: determine paths in qt_configure.bzl 10 | moc_path = "/usr/bin/moc", 11 | rcc_path = "/usr/bin/rcc", 12 | uic_path = "/usr/bin/uic", 13 | ) 14 | 15 | qt_toolchain( 16 | name = "qt_windows", 17 | # TODO: determine paths in qt_configure.bzl 18 | moc_path = "moc", 19 | rcc_path = "rcc", 20 | uic_path = "uic", 21 | ) 22 | 23 | qt_toolchain( 24 | name = "qt_osx", 25 | # TODO: determine paths in qt_configure.bzl 26 | moc_path = "/usr/local/opt/qt5/bin/moc", 27 | rcc_path = "/usr/local/opt/qt5/bin/rcc", 28 | uic_path = "/usr/local/opt/qt5/bin/uic", 29 | ) 30 | 31 | toolchain( 32 | name = "qt_linux_toolchain", 33 | exec_compatible_with = [ 34 | "@platforms//os:linux", 35 | ], 36 | target_compatible_with = [ 37 | "@platforms//os:linux", 38 | "@platforms//cpu:x86_64", 39 | ], 40 | toolchain = ":qt_linux", 41 | toolchain_type = "@com_justbuchanan_rules_qt//tools:toolchain_type", 42 | ) 43 | 44 | toolchain( 45 | name = "qt_windows_toolchain", 46 | exec_compatible_with = [ 47 | "@platforms//os:windows", 48 | ], 49 | target_compatible_with = [ 50 | "@platforms//os:windows", 51 | "@platforms//cpu:x86_64", 52 | ], 53 | toolchain = ":qt_windows", 54 | toolchain_type = "@com_justbuchanan_rules_qt//tools:toolchain_type", 55 | ) 56 | 57 | toolchain( 58 | name = "qt_osx_toolchain", 59 | exec_compatible_with = [ 60 | "@platforms//os:osx", 61 | ], 62 | target_compatible_with = [ 63 | "@platforms//os:osx", 64 | "@platforms//cpu:x86_64", 65 | ], 66 | toolchain = ":qt_osx", 67 | toolchain_type = "@com_justbuchanan_rules_qt//tools:toolchain_type", 68 | ) 69 | -------------------------------------------------------------------------------- /qt_configure.bzl: -------------------------------------------------------------------------------- 1 | def _get_env_var(repository_ctx, name, default = None): 2 | """Returns a value from an environment variable.""" 3 | for key, value in repository_ctx.os.environ.items(): 4 | if name == key: 5 | return value 6 | return default 7 | 8 | def qt_autoconf_impl(repository_ctx): 9 | """ 10 | Generate BUILD file with 'local_qt_path' function to get the Qt local path. 11 | 12 | Args: 13 | repository_ctx: repository context 14 | """ 15 | os_name = repository_ctx.os.name.lower() 16 | is_linux_machine = False 17 | is_windows_machine = False 18 | if os_name.find("windows") != -1: 19 | is_windows_machine = True 20 | # Inside this folder, in Windows you can find include, lib and bin folder 21 | default_qt_path = "C:\\\\Qt\\\\5.9.9\\\\msvc2017_64\\\\" 22 | # If predefined path does not exist search for an alternative e.g. "C:\\\\Qt\\\\5.12.10\\\\msvc2017_64\\\\" 23 | if not repository_ctx.path(default_qt_path).exists: 24 | win_path_env = _get_env_var(repository_ctx, "PATH") 25 | if win_path_env.find("C:\\Qt\\5.") != -1: 26 | start_index = win_path_env.index("C:\\Qt\\5.") 27 | end_index = win_path_env.index("msvc2017_64\\", start_index) + len("msvc2017_64") 28 | default_qt_path = win_path_env[start_index:end_index+1] 29 | default_qt_path = default_qt_path.replace('\\', "\\\\") 30 | elif os_name.find("linux") != -1: 31 | is_linux_machine = True 32 | 33 | # In Linux, this is the equivalent to the include folder, the binaries are located in 34 | # /usr/bin/ 35 | # This would be the path if it has been installed using a package manager 36 | default_qt_path = "/usr/include/x86_64-linux-gnu/qt5" 37 | if not repository_ctx.path(default_qt_path).exists: 38 | default_qt_path = "/usr/include/qt" 39 | elif os_name.find("mac") != -1: 40 | # assume Qt was installed using `brew install qt@5` 41 | default_qt_path = "/usr/local/opt/qt5" 42 | else: 43 | fail("Unsupported OS: %s" % os_name) 44 | 45 | if repository_ctx.path(default_qt_path).exists: 46 | print("Installation available on the default path: ", default_qt_path) 47 | 48 | qt_path = _get_env_var(repository_ctx, "BAZEL_RULES_QT_DIR", default_qt_path) 49 | if qt_path != default_qt_path: 50 | print("However BAZEL_RULES_QT_DIR is defined and will be used: ", qt_path) 51 | 52 | # On Windows, replace "\" by "\\" 53 | if is_windows_machine: 54 | qt_path = qt_path.replace('\\', "\\\\") 55 | 56 | # In Linux in case that we have a standalone installation, we need to provide the path inside the include folder 57 | qt_path_with_include = qt_path + "/include" 58 | if is_linux_machine and repository_ctx.path(qt_path_with_include).exists: 59 | qt_path = qt_path_with_include 60 | 61 | repository_ctx.file("BUILD", "# empty BUILD file so that bazel sees this as a valid package directory") 62 | repository_ctx.template( 63 | "local_qt.bzl", 64 | repository_ctx.path(Label("//:BUILD.local_qt.tpl")), 65 | {"%{path}": qt_path}, 66 | ) 67 | 68 | qt_autoconf = repository_rule( 69 | implementation = qt_autoconf_impl, 70 | configure = True, 71 | ) 72 | 73 | def qt_configure(): 74 | qt_autoconf(name = "local_config_qt") 75 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | | Circle CI (Linux) | AppVeyor (Windows) | Bazel CI | 2 | | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------: | :--------------------------------------------------------------------------------------------------------------------------------------: | 3 | | [![ci status](https://circleci.com/gh/justbuchanan/bazel_rules_qt.png?circle-token=9077bf6ecc5554e3ddbdc4d3947784460eb1df72)](https://app.circleci.com/pipelines/github/justbuchanan/bazel_rules_qt?branch=master) | [![ci status](https://ci.appveyor.com/api/projects/status/3klljux2otuk69u2/branch/master?svg=true)](https://ci.appveyor.com/project/justbuchanan/bazel-rules-qt/branch/master) | [![ci status](https://badge.buildkite.com/a1033836f9522e389316105837b79c67e5a749c23ba62cdc20.svg?branch=master)](https://buildkite.com/bazel/rules-qt) | 4 | 5 | # Bazel rules for Qt 6 | 7 | These Bazel rules and BUILD targets make it easy to use Qt from C++ projects built with Bazel. 8 | 9 | Note that unlike many libraries used through Bazel, it requires Qt to be installed when building the application. 10 | Also keep in mind that these rules only allow to link Qt dynamically. 11 | In addition in the case of Linux it also requires Qt to be installed on the system when running it. 12 | For Windows, it is not needed to have Qt installed in the system to run your program. The needed DLL files are copied to the output folder to make it self-contained. However, Qt is still dynamically linked. 13 | 14 | ## Platform support 15 | 16 | This project currently works on Linux, Windows, and macOS. 17 | 18 | ## Usage 19 | 20 | You can either copy the `qt.BUILD` and `qt.bzl` files into your project, add this project as a submodule if you're using git, or use a `git_repository` rule to fetch the rules. 21 | 22 | Configure your `WORKSPACE` to include the Qt libraries: 23 | 24 | ```python 25 | # WORKSPACE 26 | 27 | load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository") 28 | 29 | git_repository( 30 | name = "com_justbuchanan_rules_qt", 31 | remote = "https://github.com/justbuchanan/bazel_rules_qt.git", 32 | branch = "master", 33 | ) 34 | 35 | load("@com_justbuchanan_rules_qt//:qt_configure.bzl", "qt_configure") 36 | 37 | qt_configure() 38 | 39 | load("@local_config_qt//:local_qt.bzl", "local_qt_path") 40 | 41 | new_local_repository( 42 | name = "qt", 43 | build_file = "@com_justbuchanan_rules_qt//:qt.BUILD", 44 | path = local_qt_path(), 45 | ) 46 | 47 | load("@com_justbuchanan_rules_qt//tools:qt_toolchain.bzl", "register_qt_toolchains") 48 | register_qt_toolchains() 49 | ``` 50 | 51 | Use the build rules provided by `qt.bzl` to build your project. See `qt.bzl` for which rules are available. 52 | 53 | ```python 54 | # BUILD 55 | 56 | load("@com_justbuchanan_rules_qt//:qt.bzl", "qt_cc_library", "qt_ui_library") 57 | 58 | qt_cc_library( 59 | name = "MyWidget", 60 | srcs = ["MyWidget.cc"], 61 | hdrs = ["MyWidget.h"], 62 | deps = ["@qt//:qt_widgets"], 63 | ) 64 | 65 | qt_ui_library( 66 | name = "mainwindow", 67 | ui = "mainwindow.ui", 68 | deps = ["@qt//:qt_widgets"], 69 | ) 70 | 71 | cc_binary( 72 | name = "main", 73 | srcs = ["main.cpp"], 74 | copts = ["-fpic"], 75 | deps = [ 76 | "@qt//:qt_widgets", 77 | ":MyWidget", 78 | ":mainwindow", 79 | ], 80 | ) 81 | ``` 82 | 83 | ## Credits 84 | 85 | This is a fork of https://github.com/bbreslauer/qt-bazel-example with many modifications. 86 | -------------------------------------------------------------------------------- /qt.BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_import", "cc_library") 2 | 3 | QT_LIBRARIES = [ 4 | ("core", "QtCore", "Qt5Core", []), 5 | ("network", "QtNetwork", "Qt5Network", []), 6 | ("widgets", "QtWidgets", "Qt5Widgets", [":qt_core", ":qt_gui"]), 7 | ("quick", "QtQuick", "Qt5Quick", [":qt_gui", ":qt_qml"]), 8 | ("qml", "QtQml", "Qt5Qml", [":qt_core", ":qt_network"]), 9 | ("qml_models", "QtQmlModels", "Qt5QmlModels", []), 10 | ("gui", "QtGui", "Qt5Gui", [":qt_core"]), 11 | ("opengl", "QtOpenGL", "Qt5OpenGL", []), 12 | ] 13 | 14 | [ 15 | cc_library( 16 | name = "qt_%s_linux" % name, 17 | # When being on Windows this glob will be empty 18 | hdrs = glob(["%s/**" % include_folder], allow_empty = True), 19 | includes = ["."], 20 | linkopts = ["-l%s" % library_name], 21 | # Available from Bazel 4.0.0 22 | # target_compatible_with = ["@platforms//os:linux"], 23 | ) 24 | for name, include_folder, library_name, _ in QT_LIBRARIES 25 | ] 26 | 27 | [ 28 | cc_import( 29 | name = "qt_%s_windows_import" % name, 30 | # When being on Linux or macOS this glob will be empty 31 | hdrs = glob(["include/%s/**" % include_folder], allow_empty = True), 32 | interface_library = "lib/%s.lib" % library_name, 33 | shared_library = "bin/%s.dll" % library_name, 34 | # Not available in cc_import (See: https://github.com/bazelbuild/bazel/issues/12745) 35 | # target_compatible_with = ["@platforms//os:windows"], 36 | ) 37 | for name, include_folder, library_name, _ in QT_LIBRARIES 38 | ] 39 | 40 | [ 41 | cc_library( 42 | name = "qt_%s_windows" % name, 43 | # When being on Linux or macOS this glob will be empty 44 | hdrs = glob(["include/%s/**" % include_folder], allow_empty = True), 45 | includes = ["include"], 46 | # Available from Bazel 4.0.0 47 | # target_compatible_with = ["@platforms//os:windows"], 48 | deps = [":qt_%s_windows_import" % name], 49 | ) 50 | for name, include_folder, _, _ in QT_LIBRARIES 51 | ] 52 | 53 | [ 54 | cc_library( 55 | name = "qt_%s_osx" % name, 56 | # When being on Windows or Linux this glob will be empty 57 | hdrs = glob(["%s/**" % include_folder], allow_empty = True), 58 | includes = ["."], 59 | linkopts = ["-F/usr/local/opt/qt5/lib"] + [ 60 | "-framework %s" % library_name.replace("5", "") # macOS qt libs do not contain a 5 - e.g. instead of Qt5Core the lib is called QtCore 61 | ], 62 | # Available from Bazel 4.0.0 63 | # target_compatible_with = ["@platforms//os:osx"], 64 | ) 65 | for name, include_folder, library_name, _ in QT_LIBRARIES 66 | ] 67 | 68 | [ 69 | cc_library( 70 | name = "qt_%s" % name, 71 | visibility = ["//visibility:public"], 72 | deps = dependencies + select({ 73 | "@platforms//os:linux": [":qt_%s_linux" % name], 74 | "@platforms//os:windows": [":qt_%s_windows" % name], 75 | "@platforms//os:osx": [":qt_%s_osx" % name], 76 | }), 77 | ) 78 | for name, _, _, dependencies in QT_LIBRARIES 79 | ] 80 | 81 | # TODO: Make available also for Windows 82 | cc_library( 83 | name = "qt_3d", 84 | # When being on Windows this glob will be empty 85 | hdrs = glob([ 86 | "Qt3DAnimation/**", 87 | "Qt3DCore/**", 88 | "Qt3DExtras/**", 89 | "Qt3DInput/**", 90 | "Qt3DLogic/**", 91 | "Qt3DQuick/**", 92 | "Qt3DQuickAnimation/**", 93 | "Qt3DQuickExtras/**", 94 | "Qt3DQuickInput/**", 95 | "Qt3DQuickRender/**", 96 | "Qt3DQuickScene2D/**", 97 | "Qt3DRender/**", 98 | ], allow_empty = True), 99 | includes = ["."], 100 | linkopts = [ 101 | "-lQt53DAnimation", 102 | "-lQt53DCore", 103 | "-lQt53DExtras", 104 | "-lQt53DInput", 105 | "-lQt53DLogic", 106 | "-lQt53DQuick", 107 | "-lQt53DQuickAnimation", 108 | "-lQt53DQuickExtras", 109 | "-lQt53DQuickInput", 110 | "-lQt53DQuickRender", 111 | "-lQt53DQuickScene2D", 112 | "-lQt53DRender", 113 | ], 114 | # Available from Bazel 4.0.0 115 | # target_compatible_with = ["@platforms//os:linux"], 116 | ) 117 | 118 | # TODO: remove in favor of toolchain-defined binary 119 | filegroup( 120 | name = "uic", 121 | srcs = ["bin/uic.exe"], 122 | visibility = ["//visibility:public"], 123 | ) 124 | 125 | # TODO: remove in favor of toolchain-defined binary 126 | filegroup( 127 | name = "moc", 128 | srcs = ["bin/moc.exe"], 129 | visibility = ["//visibility:public"], 130 | ) 131 | -------------------------------------------------------------------------------- /qt.bzl: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2020 Justin Buchanan 3 | # Copyright 2016 Ben Breslauer 4 | # 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | # 17 | 18 | load("@rules_cc//cc:defs.bzl", "cc_library") 19 | 20 | def _gen_ui_header(ctx): 21 | info = ctx.toolchains["@com_justbuchanan_rules_qt//tools:toolchain_type"].qtinfo 22 | args = [ctx.file.ui_file.path, "-o", ctx.outputs.ui_header.path] 23 | ctx.actions.run( 24 | inputs = [ctx.file.ui_file], 25 | outputs = [ctx.outputs.ui_header], 26 | arguments = args, 27 | executable = info.uic_path, 28 | ) 29 | return [OutputGroupInfo(ui_header = depset([ctx.outputs.ui_header]))] 30 | 31 | gen_ui_header = rule( 32 | implementation = _gen_ui_header, 33 | attrs = { 34 | "ui_file": attr.label(allow_single_file = True, mandatory = True), 35 | "ui_header": attr.output(), 36 | }, 37 | toolchains = ["@com_justbuchanan_rules_qt//tools:toolchain_type"], 38 | ) 39 | 40 | def qt_ui_library(name, ui, deps, **kwargs): 41 | """Compiles a QT UI file and makes a library for it. 42 | 43 | Args: 44 | name: A name for the rule. 45 | src: The ui file to compile. 46 | deps: cc_library dependencies for the library. 47 | """ 48 | gen_ui_header( 49 | name = "%s_uic" % name, 50 | ui_file = ui, 51 | ui_header = "ui_%s.h" % ui.split(".")[0], 52 | ) 53 | cc_library( 54 | name = name, 55 | hdrs = [":%s_uic" % name], 56 | deps = deps, 57 | **kwargs 58 | ) 59 | 60 | def _gencpp(ctx): 61 | info = ctx.toolchains["@com_justbuchanan_rules_qt//tools:toolchain_type"].qtinfo 62 | 63 | resource_files = [(f, ctx.actions.declare_file(f.path)) for f in ctx.files.files] 64 | for target_file, output in resource_files: 65 | ctx.actions.symlink( 66 | output = output, 67 | target_file = target_file, 68 | ) 69 | 70 | args = ["--name", ctx.attr.resource_name, "--output", ctx.outputs.cpp.path, ctx.file.qrc.path] 71 | ctx.actions.run( 72 | inputs = [resource for _, resource in resource_files] + [ctx.file.qrc], 73 | outputs = [ctx.outputs.cpp], 74 | arguments = args, 75 | executable = info.rcc_path, 76 | ) 77 | return [OutputGroupInfo(cpp = depset([ctx.outputs.cpp]))] 78 | 79 | gencpp = rule( 80 | implementation = _gencpp, 81 | attrs = { 82 | "resource_name": attr.string(), 83 | "files": attr.label_list(allow_files = True, mandatory = False), 84 | "qrc": attr.label(allow_single_file = True, mandatory = True), 85 | "cpp": attr.output(), 86 | }, 87 | toolchains = ["@com_justbuchanan_rules_qt//tools:toolchain_type"], 88 | ) 89 | 90 | # generate a qrc file that lists each of the input files. 91 | def _genqrc(ctx): 92 | qrc_output = ctx.outputs.qrc 93 | qrc_content = "\n " 94 | for f in ctx.files.files: 95 | qrc_content += "\n %s" % f.path 96 | qrc_content += "\n \n" 97 | cmd = ["echo", "\"%s\"" % qrc_content, ">", qrc_output.path] 98 | ctx.actions.run_shell( 99 | command = " ".join(cmd), 100 | outputs = [qrc_output], 101 | ) 102 | return [OutputGroupInfo(qrc = depset([qrc_output]))] 103 | 104 | genqrc = rule( 105 | implementation = _genqrc, 106 | attrs = { 107 | "files": attr.label_list(allow_files = True, mandatory = True), 108 | "qrc": attr.output(), 109 | }, 110 | ) 111 | 112 | def qt_resource(name, files, **kwargs): 113 | """Creates a cc_library containing the contents of all input files using qt's `rcc` tool. 114 | 115 | Args: 116 | name: rule name 117 | files: a list of files to be included in the resource bundle 118 | kwargs: extra args to pass to the cc_library 119 | """ 120 | qrc_file = name + "_qrc.qrc" 121 | genqrc(name = name + "_qrc", files = files, qrc = qrc_file) 122 | 123 | # every resource cc_library that is linked into the same binary needs a 124 | # unique 'name'. 125 | rsrc_name = native.package_name().replace("/", "_") + "_" + name 126 | 127 | outfile = name + "_gen.cpp" 128 | gencpp( 129 | name = name + "_gen", 130 | resource_name = rsrc_name, 131 | files = files, 132 | qrc = qrc_file, 133 | cpp = outfile, 134 | ) 135 | cc_library( 136 | name = name, 137 | srcs = [outfile], 138 | alwayslink = 1, 139 | **kwargs 140 | ) 141 | 142 | def qt_cc_library(name, srcs, hdrs, normal_hdrs = [], deps = None, **kwargs): 143 | """Compiles a QT library and generates the MOC for it. 144 | 145 | Args: 146 | name: A name for the rule. 147 | srcs: The cpp files to compile. 148 | hdrs: The header files that the MOC compiles to src. 149 | normal_hdrs: Headers which are not sources for generated code. 150 | deps: cc_library dependencies for the library. 151 | kwargs: Any additional arguments are passed to the cc_library rule. 152 | """ 153 | _moc_srcs = [] 154 | for hdr in hdrs: 155 | header_path = "%s/%s" % (native.package_name(), hdr) if len(native.package_name()) > 0 else hdr 156 | moc_name = "%s_moc" % hdr.replace(".", "_") 157 | native.genrule( 158 | name = moc_name, 159 | srcs = [hdr], 160 | outs = [moc_name + ".cc"], 161 | cmd = select({ 162 | "@platforms//os:linux": "moc $(location %s) -o $@ -f'%s'" % (hdr, header_path), 163 | "@platforms//os:windows": "$(location @qt//:moc) $(locations %s) -o $@ -f'%s'" % (hdr, header_path), 164 | "@platforms//os:osx": "moc $(location %s) -o $@ -f'%s'" % (hdr, header_path), 165 | }), 166 | tools = select({ 167 | "@platforms//os:linux": [], 168 | "@platforms//os:windows": ["@qt//:moc"], 169 | "@platforms//os:osx": [], 170 | }), 171 | ) 172 | _moc_srcs.append(":" + moc_name) 173 | cc_library( 174 | name = name, 175 | srcs = srcs + _moc_srcs, 176 | hdrs = hdrs + normal_hdrs, 177 | deps = deps, 178 | **kwargs 179 | ) 180 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | --------------------------------------------------------------------------------