Skip to content

Commit 780da33

Browse files
committed
Add support for building Z3 using Bazel.
Signed-off-by: Steffen Smolka <[email protected]>
1 parent c002c77 commit 780da33

File tree

6 files changed

+159
-3
lines changed

6 files changed

+159
-3
lines changed

.bazelrc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Use Bzlmod (`MODULE.bazel`) instead of `WORKSPACE.bazel`.
2+
common --enable_bzlmod
3+
common --noenable_workspace
4+
5+
# Use C++20.
6+
build --cxxopt=-std=c++20
7+
build --host_cxxopt=-std=c++20
8+
9+
# Use Clang.
10+
build --action_env=CC=clang
11+
build --action_env=CXX=clang++

.github/workflows/bazel-build.yml

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
name: Bazel Build
2+
3+
on:
4+
push:
5+
pull_request:
6+
7+
jobs:
8+
build:
9+
10+
strategy:
11+
matrix:
12+
# We only test on the oldest version we want to support and latest.
13+
# We trust that things also work for versions in the middle.
14+
os: [ubuntu-22.04, ubuntu-latest]
15+
# See Bazelisk README for legal values.
16+
bazel_version: [7.x, latest]
17+
# Don't abort other runs when one of them fails, to ease debugging.
18+
fail-fast: false
19+
20+
runs-on: ${{ matrix.os }}
21+
22+
env:
23+
# This tells Bazelisk (installed as `bazel`) to use specified version.
24+
# https://github.com/bazelbuild/bazelisk?tab=readme-ov-file#how-does-bazelisk-know-which-bazel-version-to-run
25+
USE_BAZEL_VERSION: ${{ matrix.bazel_version }}
26+
CACHE_KEY: bazel-${{ matrix.bazel_version }}
27+
28+
steps:
29+
- uses: actions/checkout@v4
30+
31+
- name: Mount bazel cache
32+
uses: actions/cache/restore@v4
33+
with:
34+
path: "~/.cache/bazel"
35+
key: ${{ env.CACHE_KEY }}-${{ hashFiles('**/*.bazel*', '**/*.bzl') }}
36+
restore-keys: |
37+
${{ env.CACHE_KEY }}
38+
39+
- name: Save start time
40+
uses: josStorer/get-current-time@v2
41+
id: start-time
42+
with:
43+
format: X # https://momentjs.com/docs/#/displaying/format/
44+
45+
- run: bazel build //...
46+
47+
- name: Save end time
48+
# Needed to save cache regardless of build failures.
49+
if: always()
50+
uses: josStorer/get-current-time@v2
51+
id: end-time
52+
with:
53+
format: X # https://momentjs.com/docs/#/displaying/format/
54+
55+
- name: Calculate build duration
56+
# Needed to save cache regardless of build failures.
57+
if: always()
58+
run: |
59+
START=${{ steps.start-time.outputs.formattedTime }}
60+
END=${{ steps.end-time.outputs.formattedTime }}
61+
DURATION=$(( $END - $START ))
62+
echo "duration=$DURATION" | tee "$GITHUB_ENV"
63+
64+
- name: Compress cache
65+
# Needed to save cache regardless of build failures.
66+
if: always()
67+
run: rm -rf $(bazel info repository_cache)
68+
69+
- name: Save bazel cache
70+
uses: actions/cache/save@v4
71+
# Create new cache entry if on master branch or build takes >3mins.
72+
if: always() && (github.ref_name == 'master' || env.duration > 180)
73+
with:
74+
path: "~/.cache/bazel"
75+
key: ${{ env.CACHE_KEY }}-${{ hashFiles('**/*.bazel*', '**/*.bzl') }}-${{ github.run_id }}

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,6 @@ CMakeSettings.json
109109
dbg/**
110110
*.wsp
111111
CppProperties.json
112+
# Bazel generated files
113+
bazel-*
114+
*.lock

BUILD.bazel

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
load("@rules_foreign_cc//foreign_cc:defs.bzl", "configure_make")
2+
load("@rules_license//rules:license.bzl", "license")
3+
4+
package(default_applicable_licenses = [":license"])
5+
6+
license(
7+
name = "license",
8+
license_kinds = ["@rules_license//licenses/spdx:MIT"],
9+
license_text = "LICENSE.txt",
10+
)
11+
12+
exports_files(["LICENSE.txt"])
13+
14+
filegroup(
15+
name = "all_files",
16+
srcs = glob(["**"]),
17+
)
18+
19+
configure_make(
20+
name = "z3",
21+
args = [
22+
"--directory build",
23+
# "-j8",
24+
],
25+
configure_in_place = True,
26+
env = {
27+
# See https://github.com/bazelbuild/rules_foreign_cc/issues/239.
28+
"CFLAGS": "-Dredacted=0",
29+
"CXXFLAGS": "-Dredacted=0",
30+
"PYTHON": "python3",
31+
},
32+
lib_source = ":all_files",
33+
out_binaries = ["z3"],
34+
out_shared_libs = ["libz3.so"],
35+
targets = ["install"],
36+
visibility = ["//visibility:public"],
37+
)

MODULE.bazel

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module(
2+
name = "z3",
3+
version = "4.14.0",
4+
bazel_compatibility = [">=7.0.0"],
5+
)
6+
7+
bazel_dep(name = "rules_foreign_cc", version = "0.14.0")
8+
bazel_dep(name = "rules_license", version = "1.0.0")
9+
10+
# Enables formatting all Bazel files (.bazel, .bzl) by running:
11+
# ```bash
12+
# bazel run -- @buildifier_prebuilt//:buildifier --lint=fix -r .
13+
# ```
14+
bazel_dep(
15+
name = "buildifier_prebuilt",
16+
version = "8.0.1",
17+
dev_dependency = True,
18+
)

README.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ If you are not familiar with Z3, you can start [here](https://github.com/Z3Prove
77

88
Pre-built binaries for stable and nightly releases are available [here](https://github.com/Z3Prover/z3/releases).
99

10-
Z3 can be built using [Visual Studio][1], a [Makefile][2] or using [CMake][3]. It provides
11-
[bindings for several programming languages][4].
10+
Z3 can be built using [Visual Studio][1], a [Makefile][2], using [CMake][3],
11+
using [vcpkg][4], or using [Bazel][5] on Ubuntu. It provides
12+
[bindings for several programming languages][6].
1213

1314
See the [release notes](RELEASE_NOTES.md) for notes on various stable releases of Z3.
1415

@@ -25,7 +26,9 @@ See the [release notes](RELEASE_NOTES.md) for notes on various stable releases o
2526
[1]: #building-z3-on-windows-using-visual-studio-command-prompt
2627
[2]: #building-z3-using-make-and-gccclang
2728
[3]: #building-z3-using-cmake
28-
[4]: #z3-bindings
29+
[4]: #building-z3-using-vcpkg
30+
[5]: #building-z3-using-bazel
31+
[6]: #z3-bindings
2932

3033
## Building Z3 on Windows using Visual Studio Command Prompt
3134

@@ -106,6 +109,7 @@ Z3 has a build system using CMake. Read the [README-CMake.md](README-CMake.md)
106109
file for details. It is recommended for most build tasks,
107110
except for building OCaml bindings.
108111

112+
109113
## Building Z3 using vcpkg
110114

111115
vcpkg is a full platform package manager. To install Z3 with vcpkg, execute:
@@ -117,6 +121,14 @@ git clone https://github.com/microsoft/vcpkg.git
117121
./vcpkg install z3
118122
```
119123

124+
## Building Z3 using Bazel
125+
126+
Z3 can be built using [Bazel](https://bazel.build/). This is known to work on
127+
Ubuntu with Clang (but may work elsewhere with other compilers):
128+
```
129+
bazel build //...
130+
```
131+
120132
## Dependencies
121133

122134
Z3 itself has only few dependencies. It uses C++ runtime libraries, including pthreads for multi-threading.

0 commit comments

Comments
 (0)