Skip to content

Commit

Permalink
feat: Add qtremoteobjects to the bazel build.
Browse files Browse the repository at this point in the history
This allows us to do IPC/RPC in qtox.
  • Loading branch information
iphydf committed Jan 8, 2025
1 parent 31385ff commit 52d6aac
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 5 deletions.
1 change: 1 addition & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,7 @@ build --per_file_copt='moc_.*\\.cpp$@-Wno-extra-semi-stmt'
build --per_file_copt='moc_.*\\.cpp$@-Wno-gnu-zero-variadic-macro-arguments'
build --per_file_copt='moc_.*\\.cpp$@-Wno-redundant-parens'
build --per_file_copt='moc_.*\\.cpp$@-Wno-undefined-reinterpret-cast'
build --per_file_copt='moc_.*\\.cpp$@-Wno-header-hygiene'

# TODO(iphydf): Fix these soon.
build --per_file_copt='//c-toxcore/toxcore:Messenger.c@-Wno-error=unused-but-set-variable'
Expand Down
1 change: 1 addition & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,7 @@ apple_support_dependencies()

QT_LIBS = [
"base",
"remoteobjects",
"svg",
]

Expand Down
17 changes: 17 additions & 0 deletions third_party/qt.BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@
"uic",
]]

[alias(
name = name,
actual = "@qt6.qtremoteobjects//:libexec/%s" % name,
visibility = ["//visibility:public"],
) for name in [
"repc",
]]

[alias(
name = name,
actual = "bin/%s" % name,
Expand Down Expand Up @@ -45,6 +53,14 @@ alias(
"Xml",
]]

[alias(
name = "qt_" + mod.lower(),
actual = "@qt6.qtremoteobjects//:" + mod.lower(),
visibility = ["//visibility:public"],
) for mod in [
"RemoteObjects",
]]

[alias(
name = "qt_" + mod.lower(),
actual = "@qt6.qtsvg//:" + mod.lower(),
Expand All @@ -63,6 +79,7 @@ filegroup(
":qt_network",
":qt_opengl",
":qt_printsupport",
":qt_remoteobjects",
":qt_svg",
":qt_test",
":qt_widgets",
Expand Down
72 changes: 67 additions & 5 deletions third_party/qt/build_defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ This file defines three macros:
- qt_moc, generates .cpp or .moc files for Qt MOC .h or .cpp files.
"""

# Qt UI compiler rule.
# Qt UI compiler rule (uic).
# =========================================================

load("@build_bazel_rules_apple//apple:macos.bzl", "macos_application")
Expand Down Expand Up @@ -56,7 +56,7 @@ qt_uic = rule(
implementation = _qt_uic_impl,
)

# Qt language translation compiler rule.
# Qt language translation compiler rule (lconvert).
# =========================================================

def _qt_lconvert_impl(ctx):
Expand Down Expand Up @@ -104,7 +104,7 @@ qt_lconvert = rule(
implementation = _qt_lconvert_impl,
)

# Qt resource compiler rule.
# Qt resource compiler rule (rcc).
# =========================================================

def _qt_rcc_impl(ctx):
Expand Down Expand Up @@ -162,7 +162,7 @@ qt_rcc = rule(
implementation = _qt_rcc_impl,
)

# Qt MOC compiler rule.
# Qt MOC compiler rule (moc).
# =========================================================

def _qt_moc_impl(ctx):
Expand Down Expand Up @@ -196,6 +196,9 @@ def _qt_moc_impl(ctx):
]
outs = []

# Current project's bin_dir.
bin_dir = ctx.bin_dir.path + "/" + ctx.label.package.split("/")[0] + "/"

for src in srcs:
if src.extension == "h":
out = ctx.actions.declare_file(
Expand All @@ -220,7 +223,7 @@ def _qt_moc_impl(ctx):
# If we're compiling for a .h file, we #include it in the resulting
# moc_$name.cpp.
if src.path[src.path.rindex("."):] == ".h":
arguments.append("-f" + src.path)
arguments.append("-f" + src.path.removeprefix(bin_dir))

# moc $src -o $out
arguments.extend([src.path, "-o", out.path])
Expand Down Expand Up @@ -269,6 +272,65 @@ qt_moc = rule(
implementation = _qt_moc_impl,
)

# Qt Remote Objects Compiler rule (repc).
# =========================================================

def _qt_repc_impl(ctx):
repc = ctx.executable._repc
type = ctx.attr.type

srcs = [
src
for tgt in ctx.attr.srcs
for src in tgt.files.to_list()
]
outs = []

for src in srcs:
out = ctx.actions.declare_file("rep_%s_%s.h" % (src.basename[:-4], type), sibling = src)
outs.append(out)

arguments = [
"-o",
type,
src.path,
out.path,
]

# Execute repc.
ctx.actions.run(
arguments = arguments,
executable = repc.path,
inputs = [src],
mnemonic = "CompileREPC",
outputs = [out],
progress_message = "Generating Qt Remote Objects source for " + src.basename,
tools = [repc],
)

return DefaultInfo(files = depset(outs))

qt_repc = rule(
attrs = {
"srcs": attr.label_list(
allow_files = [".rep"],
doc = "The .rep files to compile.",
),
"type": attr.string(
values = ["source", "replica"],
doc = "The type of the generated file (source/replica).",
),
"_repc": attr.label(
default = Label("@qt//:repc"),
executable = True,
cfg = "exec",
allow_single_file = True,
),
},
output_to_genfiles = True,
implementation = _qt_repc_impl,
)

# Qt binary, making sure we can `bazel run` it.
# =========================================================

Expand Down
19 changes: 19 additions & 0 deletions third_party/qt/qtremoteobjects.dev.BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
load("@rules_cc//cc:defs.bzl", "cc_library")

exports_files([
"libexec/repc",
])

[cc_library(
name = mod.lower(),
hdrs = glob(["include/Qt%s/**" % mod]),
defines = ["QT_%s_LIB" % mod.upper()],
includes = [
"include",
"include/Qt" + mod,
],
visibility = ["//visibility:public"],
deps = ["@qt6.qtremoteobjects.out//:" + mod.lower()],
) for mod in [
"RemoteObjects",
]]
9 changes: 9 additions & 0 deletions third_party/qt/qtremoteobjects.out.BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
load("@rules_cc//cc:defs.bzl", "cc_library")

[cc_library(
name = mod.lower(),
srcs = glob(["lib/libQt6%s.so*" % mod]),
visibility = ["//visibility:public"],
) for mod in [
"RemoteObjects",
]]

0 comments on commit 52d6aac

Please sign in to comment.