Skip to content

Commit 83b4593

Browse files
committed
build(bazel): add openshell cli target
Signed-off-by: Simon Scatton <sscatton@nvidia.com>
1 parent b8cde0d commit 83b4593

14 files changed

Lines changed: 750 additions & 93 deletions

File tree

‎BUILD.bazel‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,12 @@ platform(
2020
name = "darwin_arm64",
2121
parents = ["@zig_sdk//platform:darwin_arm64"],
2222
)
23+
24+
# The Bazel-built z3 install tree (include/ + lib/) as one directory
25+
# artifact, consumed by the z3-sys crate annotation in bazel/rust.MODULE.bazel.
26+
filegroup(
27+
name = "z3_install",
28+
srcs = ["@z3//:z3_static"],
29+
output_group = "gen_dir",
30+
visibility = ["//visibility:public"],
31+
)

‎MODULE.bazel‎

Lines changed: 5 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -7,92 +7,11 @@ module(
77
)
88

99
bazel_dep(name = "platforms", version = "1.1.0")
10-
bazel_dep(name = "rules_rust", version = "0.71.3")
11-
bazel_dep(name = "hermetic_cc_toolchain", version = "4.1.0")
10+
bazel_dep(name = "rules_cc", version = "0.2.17")
1211

13-
rust = use_extension("@rules_rust//rust:extensions.bzl", "rust")
12+
# The rest of the build config lives in bazel/, split by concern.
13+
include("//bazel:rust.MODULE.bazel")
1414

15-
rust.toolchain(
16-
edition = "2024",
17-
versions = ["1.95.0"],
18-
)
19-
20-
# Musl toolchains for static release builds. Their @zig_sdk//libc:musl
21-
# constraint keeps them dormant on the (constraint-less) host platform, so
22-
# dev builds are unaffected.
23-
rust.repository_set(
24-
name = "rust_linux_amd64_musl",
25-
exec_triple = "x86_64-unknown-linux-gnu",
26-
target_triple = "x86_64-unknown-linux-musl",
27-
versions = ["1.95.0"],
28-
extra_rustc_flags = ["-Clink-self-contained=no"],
29-
target_compatible_with = [
30-
"@platforms//os:linux",
31-
"@platforms//cpu:x86_64",
32-
"@zig_sdk//libc:musl",
33-
],
34-
)
35-
36-
rust.repository_set(
37-
name = "rust_linux_arm64_musl",
38-
exec_triple = "x86_64-unknown-linux-gnu",
39-
target_triple = "aarch64-unknown-linux-musl",
40-
versions = ["1.95.0"],
41-
extra_rustc_flags = ["-Clink-self-contained=no"],
42-
target_compatible_with = [
43-
"@platforms//os:linux",
44-
"@platforms//cpu:aarch64",
45-
"@zig_sdk//libc:musl",
46-
],
47-
)
48-
49-
# Experimental macOS cross-compilation (library crates only; linking needs
50-
# Apple framework stubs that zig does not ship).
51-
rust.repository_set(
52-
name = "rust_darwin_arm64",
53-
exec_triple = "x86_64-unknown-linux-gnu",
54-
target_triple = "aarch64-apple-darwin",
55-
versions = ["1.95.0"],
56-
target_compatible_with = [
57-
"@platforms//os:macos",
58-
"@platforms//cpu:aarch64",
59-
],
60-
)
61-
62-
use_repo(rust, "rust_toolchains")
63-
64-
# The musl hub entries must be registered before //:all — the gnu entries in
65-
# the hub also match musl platforms (they declare no libc constraint), and
66-
# Bazel picks the first compatible registration.
67-
register_toolchains(
68-
"@rust_toolchains//:rust_linux_amd64_musl__x86_64-unknown-linux-musl__stable",
69-
"@rust_toolchains//:rust_linux_arm64_musl__aarch64-unknown-linux-musl__stable",
70-
)
71-
72-
register_toolchains("@rust_toolchains//:all")
73-
74-
crate = use_extension("@rules_rust//crate_universe:extensions.bzl", "crate")
75-
crate.from_cargo(
76-
name = "crates",
77-
cargo_lockfile = "//:Cargo.lock",
78-
manifests = [
79-
"//:Cargo.toml",
80-
],
81-
)
82-
use_repo(crate, "crates")
83-
84-
hermetic_toolchain = use_extension("@hermetic_cc_toolchain//toolchain:ext.bzl", "toolchains")
85-
use_repo(hermetic_toolchain, "zig_sdk")
86-
87-
# Only match platforms carrying @zig_sdk//libc:musl — dormant for dev builds,
88-
# which keep the autodetected host toolchain.
89-
register_toolchains(
90-
"@zig_sdk//libc_aware/toolchain:linux_amd64_musl",
91-
"@zig_sdk//libc_aware/toolchain:linux_arm64_musl",
92-
"@zig_sdk//toolchain:darwin_arm64",
93-
)
15+
include("//bazel:cc_toolchains.MODULE.bazel")
9416

95-
# Hermetic protoc for openshell-core's build script, injected via $PROTOC
96-
# instead of the autotools build that protobuf-src does under cargo. Built
97-
# from source in the exec configuration (one-time cost, then cached).
98-
bazel_dep(name = "protobuf", version = "33.4")
17+
include("//bazel:z3.MODULE.bazel")

‎MODULE.bazel.lock‎

Lines changed: 428 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎bazel/BUILD.bazel‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
# Package marker so the MODULE.bazel include() segments in this directory
5+
# resolve as labels. Build targets live in the sibling packages.

‎bazel/cc_toolchains.MODULE.bazel‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
# C/C++ toolchain providers: zig (cross-compilation), protoc, and libclang.
5+
6+
bazel_dep(name = "hermetic_cc_toolchain", version = "4.1.0")
7+
8+
hermetic_toolchain = use_extension("@hermetic_cc_toolchain//toolchain:ext.bzl", "toolchains")
9+
use_repo(hermetic_toolchain, "zig_sdk")
10+
11+
# Only match platforms carrying @zig_sdk//libc:musl — dormant for dev builds,
12+
# which keep the autodetected host toolchain.
13+
register_toolchains(
14+
"@zig_sdk//libc_aware/toolchain:linux_amd64_musl",
15+
"@zig_sdk//libc_aware/toolchain:linux_arm64_musl",
16+
"@zig_sdk//toolchain:darwin_arm64",
17+
)
18+
19+
# Hermetic protoc for openshell-core's build script, injected via $PROTOC
20+
# instead of the autotools build that protobuf-src does under cargo. Built
21+
# from source in the exec configuration (one-time cost, then cached).
22+
bazel_dep(name = "protobuf", version = "33.4")
23+
24+
# Prebuilt LLVM distribution as the source of libclang for z3-sys's bindgen
25+
# run (the same route rules_rust's bindgen_toolchain example takes; building
26+
# LLVM from source needs an older C++ toolchain than ours). Deliberately not
27+
# registered as a cc toolchain — zig and the host autodetect own that space.
28+
bazel_dep(name = "toolchains_llvm", version = "1.8.0")
29+
30+
llvm = use_extension("@toolchains_llvm//toolchain/extensions:llvm.bzl", "llvm")
31+
llvm.toolchain(
32+
name = "llvm_toolchain",
33+
llvm_version = "20.1.4",
34+
)
35+
use_repo(llvm, "llvm_toolchain_llvm")
36+
37+
# The prebuilt libclang.so dynamically links libz.so.1 and expects it from
38+
# the host, which NixOS does not serve; a Bazel-built one is bundled next to
39+
# it instead (see //bazel/clang:libclang_bundle).
40+
bazel_dep(name = "zlib", version = "1.3.1.bcr.5")

‎bazel/clang/BUILD.bazel‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
# libclang plumbing for z3-sys's bindgen run: the prebuilt LLVM libclang, the
5+
# libz.so.1 it expects from the host (which NixOS does not serve), and clang's
6+
# builtin headers. Kept together because the prebuilt binary's runtime
7+
# expectations are the whole problem this package exists to solve.
8+
9+
load("@rules_cc//cc:cc_shared_library.bzl", "cc_shared_library")
10+
11+
# A libz.so.1 whose exports carry the ZLIB_1.2.0 symbol version that the
12+
# prebuilt libclang references; the BCR zlib's cc_library omits it (see
13+
# zlib.map).
14+
cc_shared_library(
15+
name = "z_shared",
16+
additional_linker_inputs = ["zlib.map"],
17+
user_link_flags = ["-Wl,--version-script=$(location zlib.map)"],
18+
deps = ["@zlib//:z"],
19+
)
20+
21+
# libclang next to that libz.so.1. libclang's RUNPATH is $ORIGIN/../lib, so a
22+
# sibling libz in the same lib/ dir resolves without patching the binary.
23+
genrule(
24+
name = "libclang_bundle",
25+
srcs = [
26+
":z_shared",
27+
"@llvm_toolchain_llvm//:libclang",
28+
],
29+
outs = [
30+
"libclang/lib/libclang.so",
31+
"libclang/lib/libz.so.1",
32+
],
33+
cmd = "cp $(execpath @llvm_toolchain_llvm//:libclang) $(RULEDIR)/libclang/lib/libclang.so && " +
34+
"cp $(execpath :z_shared) $(RULEDIR)/libclang/lib/libz.so.1",
35+
visibility = ["//visibility:public"],
36+
)
37+
38+
# Clang's builtin headers (stdbool.h etc.), which libclang would normally find
39+
# relative to its binary but cannot in the stripped bundle above.
40+
alias(
41+
name = "clang_builtin_include",
42+
actual = "@llvm_toolchain_llvm//:lib/clang/20/include",
43+
visibility = ["//visibility:public"],
44+
)

‎bazel/clang/zlib.map‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/* The prebuilt libclang references zlib symbols tagged ZLIB_1.2.0, but the
2+
* BCR zlib's cc_library build exports them untagged. Re-apply the symbol
3+
* version upstream zlib's own Makefile would (see :libclang_bundle). */
4+
ZLIB_1.2.0 {
5+
global:
6+
*;
7+
};

‎bazel/rust.MODULE.bazel‎

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
# Rust toolchains and the crate_universe dependency graph.
5+
6+
bazel_dep(name = "rules_rust", version = "0.71.3")
7+
8+
rust = use_extension("@rules_rust//rust:extensions.bzl", "rust")
9+
10+
rust.toolchain(
11+
edition = "2024",
12+
versions = ["1.95.0"],
13+
)
14+
15+
# Musl toolchains for static release builds. Their @zig_sdk//libc:musl
16+
# constraint keeps them dormant on the (constraint-less) host platform, so
17+
# dev builds are unaffected.
18+
rust.repository_set(
19+
name = "rust_linux_amd64_musl",
20+
exec_triple = "x86_64-unknown-linux-gnu",
21+
target_triple = "x86_64-unknown-linux-musl",
22+
versions = ["1.95.0"],
23+
extra_rustc_flags = ["-Clink-self-contained=no"],
24+
target_compatible_with = [
25+
"@platforms//os:linux",
26+
"@platforms//cpu:x86_64",
27+
"@zig_sdk//libc:musl",
28+
],
29+
)
30+
31+
rust.repository_set(
32+
name = "rust_linux_arm64_musl",
33+
exec_triple = "x86_64-unknown-linux-gnu",
34+
target_triple = "aarch64-unknown-linux-musl",
35+
versions = ["1.95.0"],
36+
extra_rustc_flags = ["-Clink-self-contained=no"],
37+
target_compatible_with = [
38+
"@platforms//os:linux",
39+
"@platforms//cpu:aarch64",
40+
"@zig_sdk//libc:musl",
41+
],
42+
)
43+
44+
# Experimental macOS cross-compilation (library crates only; linking needs
45+
# Apple framework stubs that zig does not ship).
46+
rust.repository_set(
47+
name = "rust_darwin_arm64",
48+
exec_triple = "x86_64-unknown-linux-gnu",
49+
target_triple = "aarch64-apple-darwin",
50+
versions = ["1.95.0"],
51+
target_compatible_with = [
52+
"@platforms//os:macos",
53+
"@platforms//cpu:aarch64",
54+
],
55+
)
56+
57+
use_repo(rust, "rust_toolchains")
58+
59+
# The musl hub entries must be registered before //:all — the gnu entries in
60+
# the hub also match musl platforms (they declare no libc constraint), and
61+
# Bazel picks the first compatible registration.
62+
register_toolchains(
63+
"@rust_toolchains//:rust_linux_amd64_musl__x86_64-unknown-linux-musl__stable",
64+
"@rust_toolchains//:rust_linux_arm64_musl__aarch64-unknown-linux-musl__stable",
65+
)
66+
67+
register_toolchains("@rust_toolchains//:all")
68+
69+
crate = use_extension("@rules_rust//crate_universe:extensions.bzl", "crate")
70+
crate.from_cargo(
71+
name = "crates",
72+
cargo_lockfile = "//:Cargo.lock",
73+
manifests = [
74+
"//:Cargo.toml",
75+
],
76+
)
77+
78+
# Under cargo, z3-sys links the system libz3; here it gets the Bazel-built
79+
# one: the header for its bindgen run and the lib directory for linking.
80+
crate.annotation(
81+
crate = "z3-sys",
82+
build_script_data = [
83+
"@@//:z3_install",
84+
"@@//bazel/clang:clang_builtin_include",
85+
"@@//bazel/clang:libclang/lib/libclang.so",
86+
"@@//bazel/clang:libclang/lib/libz.so.1",
87+
],
88+
build_script_env = {
89+
"BINDGEN_EXTRA_CLANG_ARGS": "-isystem $(execpath @@//bazel/clang:clang_builtin_include)",
90+
"LIBCLANG_PATH": "$(execpath @@//bazel/clang:libclang/lib/libclang.so)",
91+
"Z3_LIBRARY_PATH_OVERRIDE": "$(execpath @@//:z3_install)/lib",
92+
"Z3_SYS_Z3_HEADER": "$(execpath @@//:z3_install)/include/z3.h",
93+
},
94+
)
95+
use_repo(crate, "crates")

‎bazel/z3.MODULE.bazel‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
# Bazel-built libz3 for openshell-prover, injected into z3-sys instead of the
5+
# system library that plain cargo links. The BCR module is a year stale, so
6+
# override with the upstream release, which carries its own MODULE.bazel.
7+
bazel_dep(name = "z3", version = "4.16.0")
8+
9+
archive_override(
10+
module_name = "z3",
11+
# z3's BUILD.bazel collects install outputs from lib/, but CMake's
12+
# GNUInstallDirs host-sniffs the layout and picks lib64/ on non-Debian
13+
# Linux. Pin the libdir right after each generator declaration.
14+
patch_cmds = [
15+
"""sed -i '/-G Ninja/a\\ "-D CMAKE_INSTALL_LIBDIR=lib",' BUILD.bazel""",
16+
],
17+
integrity = "sha256-xow+XkgQsWEmuMtMR+7oXBrD4kqBkUyONxtA3p3TOsc=",
18+
strip_prefix = "z3-z3-4.16.0",
19+
urls = ["https://github.com/Z3Prover/z3/archive/refs/tags/z3-4.16.0.tar.gz"],
20+
)

‎crates/openshell-cli/BUILD.bazel‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
load("@crates//:defs.bzl", "all_crate_deps")
5+
load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_library", "rust_test")
6+
7+
WORKSPACE_DEPS = [
8+
"//crates/openshell-bootstrap:openshell_bootstrap",
9+
"//crates/openshell-core:openshell_core",
10+
"//crates/openshell-policy:openshell_policy",
11+
"//crates/openshell-providers:openshell_providers",
12+
"//crates/openshell-prover:openshell_prover",
13+
"//crates/openshell-tui:openshell_tui",
14+
]
15+
16+
rust_library(
17+
name = "openshell_cli",
18+
srcs = glob(
19+
["src/**/*.rs"],
20+
exclude = ["src/main.rs"],
21+
),
22+
edition = "2024",
23+
visibility = ["//visibility:public"],
24+
deps = all_crate_deps(normal = True) + WORKSPACE_DEPS,
25+
proc_macro_deps = all_crate_deps(proc_macro = True),
26+
)
27+
28+
rust_binary(
29+
name = "openshell",
30+
srcs = ["src/main.rs"],
31+
edition = "2024",
32+
visibility = ["//visibility:public"],
33+
deps = all_crate_deps(normal = True) + WORKSPACE_DEPS + [
34+
":openshell_cli",
35+
],
36+
proc_macro_deps = all_crate_deps(proc_macro = True),
37+
)
38+
39+
rust_test(
40+
name = "openshell_cli_test",
41+
crate = ":openshell_cli",
42+
deps = all_crate_deps(normal_dev = True),
43+
)

0 commit comments

Comments
 (0)