Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[rust] upstreaming rust/cxx integration #2987

Merged
merged 2 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ build:limited-dbg-info --copt='-g1' --copt="-fdebug-info-for-profiling"

# Miscellaneous platform-independent options
build --@capnp-cpp//src/kj:openssl=True --@capnp-cpp//src/kj:zlib=True --@capnp-cpp//src/kj:brotli=True
build --@capnp-cpp//src/capnp:gen_rust=True
build --cxxopt="-fbracket-depth=512" --host_cxxopt="-fbracket-depth=512"
build --@capnp-cpp//src/capnp:gen_rust=True

Expand All @@ -152,13 +153,11 @@ build --@capnp-cpp//src/capnp:gen_rust=True
# improvement to build speed and Rust code size.
# Using extra_rustc_flag for non-exec flags so they can be overwritten selectively.
build --@rules_rust//:extra_rustc_flag=-Cembed-bitcode=n
build --@rules_rust//:extra_rustc_flag=-Cpanic=abort
build --@rules_rust//:extra_rustc_flag=-Cdebug-assertions=n
build --@rules_rust//:extra_exec_rustc_flags=-C,panic=abort,-C,debug-assertions=n,-C,embed-bitcode=n
build --@rules_rust//:extra_exec_rustc_flags=-C,debug-assertions=n,-C,embed-bitcode=n

# We default to not enabling debug assertions and unwinding for Rust. For debug builds this is not
# the right setting, but unfortunately we can't set that directly.
build:rust-debug --@rules_rust//:extra_rustc_flag=-Cpanic=unwind
build:rust-debug --@rules_rust//:extra_rustc_flag=-Cdebug-assertions=y
# Use the equivalent of -g/-g2 for Rust here, this is necessary to get qualified stack traces while
# -Zdebug-info-for-profiling is unavailable. Unlike gcc/clang, Rust defaults to -g3 otherwise.
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ jobs:
- name: Setup Windows
if: runner.os == 'Windows'
run: |
git config --global core.symlinks true
git config --show-scope --show-origin core.symlinks
# Set a custom output root directory to avoid long file name issues.
[System.IO.File]::WriteAllLines((Join-Path -Path $env:USERPROFILE -ChildPath '.bazelrc'), 'startup --output_user_root=C:/tmp')
- name: Configure download mirrors
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ jobs:
if: matrix.os.name == 'windows'
# Set a custom output root directory to avoid long file name issues.
run: |
git config --global core.symlinks true
git config --show-scope --show-origin core.symlinks
[System.IO.File]::WriteAllLines((Join-Path -Path $env:USERPROFILE -ChildPath '.bazelrc'), 'startup --output_user_root=C:/tmp')
- name: Configure download mirrors
shell: bash
Expand Down
2 changes: 1 addition & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ rust_register_toolchains(
# also avoids registering support for the wasm32-unknown-unknown and wasm32-wasi targets, which
# are otherwise added by default.
extra_target_triples = ["x86_64-apple-darwin"],
versions = ["1.81.0"], # LLVM 18
versions = ["1.82.0"], # LLVM 19
)

load("@rules_rust//crate_universe:repositories.bzl", "crate_universe_dependencies")
Expand Down
5 changes: 5 additions & 0 deletions build/deps/build_deps.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@
"file_type": "executable",
"downloaded_file_path": "downloaded.exe"
},
{
"name": "cxxbridge-cmd",
"type": "crate",
"build_file": "//deps/rust:BUILD.cxxbridge-cmd"
},
{
"name": "aspect_rules_esbuild",
"type": "github_release",
Expand Down
2 changes: 2 additions & 0 deletions build/deps/gen/build_deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ load("@//build/deps:gen/dep_cargo_bazel_linux_x64.bzl", "dep_cargo_bazel_linux_x
load("@//build/deps:gen/dep_cargo_bazel_macos_arm64.bzl", "dep_cargo_bazel_macos_arm64")
load("@//build/deps:gen/dep_cargo_bazel_macos_x64.bzl", "dep_cargo_bazel_macos_x64")
load("@//build/deps:gen/dep_cargo_bazel_win_x64.bzl", "dep_cargo_bazel_win_x64")
load("@//build/deps:gen/dep_cxxbridge_cmd.bzl", "dep_cxxbridge_cmd")
load("@//build/deps:gen/dep_rules_pkg.bzl", "dep_rules_pkg")
load("@//build/deps:gen/dep_rules_python.bzl", "dep_rules_python")
load("@//build/deps:gen/dep_rules_rust.bzl", "dep_rules_rust")
Expand All @@ -25,6 +26,7 @@ def deps_gen():
dep_cargo_bazel_macos_x64()
dep_cargo_bazel_macos_arm64()
dep_cargo_bazel_win_x64()
dep_cxxbridge_cmd()
dep_aspect_rules_esbuild()
dep_rules_pkg()
dep_aspect_bazel_lib()
Expand Down
8 changes: 4 additions & 4 deletions build/deps/gen/dep_capnp_cpp.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

load("@//:build/http.bzl", "http_archive")

URL = "https://github.com/capnproto/capnproto/tarball/3c849db4caa127f75fefcfdc9e7105c8a62cbed0"
STRIP_PREFIX = "capnproto-capnproto-3c849db/c++"
SHA256 = "acc108b7cb8d3ef6fd37da19529b5a97940e49c75676bb3c8749e32478628265"
URL = "https://github.com/capnproto/capnproto/tarball/0fb157fb00f8f2cacf23751f56d12bf6c8459f6a"
STRIP_PREFIX = "capnproto-capnproto-0fb157f/c++"
SHA256 = "15bc213e1869c84d2b02ad4cde61e01c0ee264c77a2af055a41581091c851b9c"
TYPE = "tgz"
COMMIT = "3c849db4caa127f75fefcfdc9e7105c8a62cbed0"
COMMIT = "0fb157fb00f8f2cacf23751f56d12bf6c8459f6a"

def dep_capnp_cpp():
http_archive(
Expand Down
19 changes: 19 additions & 0 deletions build/deps/gen/dep_cxxbridge_cmd.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# WARNING: THIS FILE IS AUTOGENERATED BY update-deps.py DO NOT EDIT

load("@//:build/http.bzl", "http_archive")

URL = "https://crates.io/api/v1/crates/cxxbridge-cmd/1.0.129/download"
STRIP_PREFIX = "cxxbridge-cmd-1.0.129"
SHA256 = "c089cc7133ae878b2b6993a85f006b5d13e6031a72da721d7dce98a17cb72b72"
TYPE = "tgz"
VERSION = "1.0.129"

def dep_cxxbridge_cmd():
http_archive(
name = "cxxbridge-cmd",
url = URL,
strip_prefix = STRIP_PREFIX,
type = TYPE,
sha256 = SHA256,
build_file = "//deps/rust:BUILD.cxxbridge-cmd",
)
126 changes: 126 additions & 0 deletions build/wd_rust_crate.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test")

def rust_cxx_include(name, visibility = [], include_prefix = None):
native.genrule(
name = "%s/generated" % name,
outs = ["cxx.h"],
cmd = "$(location @cxxbridge-cmd//:cxxbridge-cmd) --header > \"$@\"",
tools = ["@cxxbridge-cmd//:cxxbridge-cmd"],
)

native.cc_library(
name = name,
hdrs = ["cxx.h"],
include_prefix = include_prefix,
visibility = visibility,
)

def _rust_cxx_bridge(
name,
src,
hdrs = [],
deps = [],
visibility = [],
strip_include_prefix = None,
include_prefix = None):
native.genrule(
name = "%s/generated" % name,
srcs = [src],
outs = [
src + ".h",
src + ".cc",
],
cmd = "$(location @cxxbridge-cmd//:cxxbridge-cmd) $(location %s) -o $(location %s.h) -o $(location %s.cc)" % (src, src, src),
tools = ["@cxxbridge-cmd//:cxxbridge-cmd"],
)

native.cc_library(
name = name,
srcs = [src + ".cc"],
hdrs = [src + ".h"] + hdrs,
strip_include_prefix = strip_include_prefix,
include_prefix = include_prefix,
linkstatic = True,
deps = deps,
visibility = visibility,
)

def wd_rust_crate(
name,
cxx_bridge_src = None,
deps = [],
proc_macro_deps = [],
data = [],
test_env = {},
test_tags = [],
test_deps = [],
test_proc_macro_deps = [],
cxx_bridge_deps = [],
visibility = None):
"""Define rust crate.

Args:
name: crate name.
cxx_bridge_src: (optional) .rs source file with cxx ffi bridge definition. The rule will
generation additional<name>@cxx c++ library with cxx bindings if this is set.
deps: crate dependencies: rust crates or c/c++ libraries.
visibility: crate visibility.
data: additional data files.
proc_macro_deps: proc_macro dependencies.
rustc_env: rustc environment variables,
test_env: additional test environment variable.
test_tags: additional test tags.
test_deps: test-only dependencies.
test_proc_macro_deps: test-only proc_macro dependencies.
"""
srcs = native.glob(["**/*.rs"])
crate_name = name.replace("-", "_")

if cxx_bridge_src:
hdrs = native.glob(["**/*.h"], allow_empty = True)

_rust_cxx_bridge(
name = name + "@cxx",
src = cxx_bridge_src,
hdrs = hdrs,
include_prefix = "rust/" + name,
strip_include_prefix = "",
# Not applying visibility here – if you import the cxxbridge header, you will likely
# also need the rust library itself to avoid linker errors.
deps = cxx_bridge_deps + [
"@crates_vendor//:cxx",
"//src/rust/cxx-integration:cxx-include",
],
)

deps.append("@crates_vendor//:cxx")
deps.append(name + "@cxx")

crate_features = []

rust_library(
name = name,
crate_name = crate_name,
srcs = srcs,
deps = deps,
visibility = visibility,
data = data,
proc_macro_deps = proc_macro_deps,
crate_features = crate_features,
)

rust_test(
name = name + "_test",
crate = ":" + name,
env = {
"RUST_BACKTRACE": "1",
# rust test runner captures stderr by default, which makes debugging tests very hard
"RUST_TEST_NOCAPTURE": "1",
# our tests are usually very heavy and do not support concurrent invocation
"RUST_TEST_THREADS": "1",
} | test_env,
tags = test_tags,
crate_features = crate_features,
deps = test_deps,
proc_macro_deps = test_proc_macro_deps,
)
4 changes: 3 additions & 1 deletion deps/rust/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ crates_vendor(
cc_library(
name = "runtime",
linkopts = select({
"@platforms//os:windows": ["ntdll.lib"],
"@platforms//os:windows": [
"ntdll.lib",
],
"//conditions:default": [],
}),
visibility = ["//visibility:public"],
Expand Down
19 changes: 19 additions & 0 deletions deps/rust/BUILD.cxxbridge-cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
load("@rules_rust//rust:defs.bzl", "rust_binary")

# Build file for cxxbridge-cmd tool.

rust_binary(
name = "cxxbridge-cmd",
srcs = glob(["src/**/*.rs"]),
data = ["src/gen/include/cxx.h"],
edition = "2021",
visibility = ["//visibility:public"],
deps = [
"@crates_vendor//:clap",
"@crates_vendor//:codespan-reporting",
"@crates_vendor//:cxx",
"@crates_vendor//:proc-macro2",
"@crates_vendor//:quote",
"@crates_vendor//:syn",
],
)
Loading