3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2025-04-23 09:05:31 +00:00

WIP: Migrating OCaml binding to CMake (#7254)

* Update doc for `mk_context`.

* Migrating to cmake.

* Migrating to cmake. It builds both internal or external libz3.

* Start to work on platform-specific problem.

* Messy notes.

* debug.

* Cleanup a bit.

* Fixing shared lib extension.

* Minor.

* Resume working on this PR.

* Remove including `AddOCaml`.

* Keep `z3.ml` and `z3.mli` in the src but specify the generated file in the bin.

* Keep `ml_example.ml` in the src.

* Try github action for ocaml.

* Add workflow using matrix.

* Fix mac linking once more.

* Bypass @rpath in building sanity check.
This commit is contained in:
Shiwei Weng 翁士伟 2025-04-19 16:41:27 -04:00 committed by GitHub
parent ab9f3307d6
commit f7aec02503
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 1104 additions and 3 deletions

125
.github/workflows/ocaml-all.yaml vendored Normal file
View file

@ -0,0 +1,125 @@
name: OCaml Binding CI (Ubuntu + macOS)
on:
push:
branches: [ "**" ]
pull_request:
branches: [ "**" ]
jobs:
build-test:
strategy:
matrix:
os: [ ubuntu-latest, macos-latest]
ocaml-version: ["5"]
fail-fast: false
runs-on: ${{ matrix.os }}
steps:
- name: Checkout code
uses: actions/checkout@v4
# Cache ccache (shared across runs)
- name: Cache ccache
uses: actions/cache@v4
with:
path: ~/.ccache
key: ${{ runner.os }}-ccache-${{ github.sha }}
restore-keys: |
${{ runner.os }}-ccache-
# Cache opam (compiler + packages)
- name: Cache opam
uses: actions/cache@v4
with:
path: ~/.opam
key: ${{ runner.os }}-opam-${{ matrix.ocaml-version }}-${{ github.sha }}
restore-keys: |
${{ runner.os }}-opam-${{ matrix.ocaml-version }}-
# Setup OCaml via action
- uses: ocaml/setup-ocaml@v3
with:
ocaml-compiler: ${{ matrix.ocaml-version }}
opam-disable-sandboxing: true
# Platform-specific dependencies
- name: Install system dependencies (Ubuntu)
if: matrix.os == 'ubuntu-latest'
run: |
sudo apt-get update
sudo apt-get install -y \
bubblewrap m4 libgmp-dev pkg-config ninja-build ccache
- name: Install system dependencies (macOS)
if: matrix.os == 'macos-latest'
run: |
brew install gmp pkg-config ninja ccache
- name: Install required opam packages
run: opam install -y ocamlfind zarith
# Configure
- name: Configure with CMake
env:
RUNNER_OS: ${{ runner.os }}
CC: ${{ matrix.os == 'macos-latest' && 'ccache clang' || 'ccache gcc' }}
CXX: ${{ matrix.os == 'macos-latest' && 'ccache clang++' || 'ccache g++' }}
run: |
mkdir -p build
cd build
eval $(opam env)
echo "CC: $CC"
echo "CXX: $CXX"
echo "OCAMLFIND: $(which ocamlfind)"
echo "OCAMLC: $(which ocamlc)"
echo "OCAMLOPT: $(which ocamlopt)"
echo "OCAML_VERSION: $(ocamlc -version)"
echo "OCAMLLIB: $OCAMLLIB"
env | grep OCAML
cmake .. \
-G Ninja \
-DZ3_BUILD_LIBZ3_SHARED=ON \
-DZ3_BUILD_OCAML_BINDINGS=ON \
-DZ3_BUILD_JAVA_BINDINGS=OFF \
-DZ3_BUILD_PYTHON_BINDINGS=OFF \
-DZ3_BUILD_CLI=OFF \
-DZ3_BUILD_TEST_EXECUTABLES=OFF \
-DCMAKE_VERBOSE_MAKEFILE=TRUE
- name: Build Z3 and OCaml bindings
run: |
ccache -z || true
eval $(opam env)
cd build
ninja build_z3_ocaml_bindings
ccache -s || true
- name: Compile ml_example.byte
run: |
eval $(opam env)
ocamlfind ocamlc -o ml_example.byte \
-package zarith \
-linkpkg \
-I build/src/api/ml \
-dllpath build/src/api/ml \
build/src/api/ml/z3ml.cma \
examples/ml/ml_example.ml
- name: Run ml_example.byte
run: |
eval $(opam env)
ocamlrun ./ml_example.byte
- name: Compile ml_example (native)
run: |
eval $(opam env)
ocamlfind ocamlopt -o ml_example \
-package zarith \
-linkpkg \
-I build/src/api/ml \
build/src/api/ml/z3ml.cmxa \
examples/ml/ml_example.ml
- name: Run ml_example (native)
run: ./ml_example

99
.github/workflows/ocaml.yaml vendored Normal file
View file

@ -0,0 +1,99 @@
name: OCaml Binding CI (Ubuntu)
on:
push:
branches: [ "**" ]
pull_request:
branches: [ "**" ]
jobs:
build-test-ocaml:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Cache ccache
uses: actions/cache@v4
with:
path: ~/.ccache
key: ${{ runner.os }}-ccache-${{ github.ref }}
restore-keys: |
${{ runner.os }}-ccache-
- name: Install system dependencies
run: |
sudo apt-get update
sudo apt-get install -y \
opam bubblewrap m4 \
libgmp-dev pkg-config \
ninja-build ccache
- name: Init opam (no sandbox, no default switch)
run: |
opam init --bare --no-setup --disable-sandboxing
opam switch create 5.3.0
eval $(opam env)
opam install -y ocamlfind zarith
eval $(opam env)
- name: Configure with CMake
run: |
eval $(opam env)
export CC="ccache gcc"
export CXX="ccache g++"
mkdir -p build
cd build
cmake .. \
-G Ninja \
-DZ3_BUILD_LIBZ3_SHARED=ON \
-DZ3_BUILD_OCAML_BINDINGS=ON \
-DZ3_BUILD_JAVA_BINDINGS=OFF \
-DZ3_BUILD_PYTHON_BINDINGS=OFF \
-DZ3_BUILD_CLI=OFF \
-DZ3_BUILD_TEST_EXECUTABLES=OFF \
-DCMAKE_VERBOSE_MAKEFILE=TRUE
- name: Build Z3 and OCaml bindings
run: |
eval $(opam env)
export CC="ccache gcc"
export CXX="ccache g++"
ocamlc -version
ccache -z # reset stats
cd build
ninja build_z3_ocaml_bindings
ccache -s # show stats
- name: Compile ml_example.byte
run: |
eval $(opam env)
ocamlc -version
ocamlfind ocamlc -o ml_example.byte \
-package zarith \
-linkpkg \
-I build/src/api/ml \
-dllpath build/src/api/ml \
build/src/api/ml/z3ml.cma \
examples/ml/ml_example.ml
- name: Run ml_example.byte
run: |
eval $(opam env)
ocamlrun ./ml_example.byte
- name: Compile ml_example (native)
run: |
eval $(opam env)
ocamlopt -version
ocamlfind ocamlopt -o ml_example \
-package zarith \
-linkpkg \
-I build/src/api/ml \
build/src/api/ml/z3ml.cmxa \
examples/ml/ml_example.ml
- name: Run ml_example (native)
run: |
./ml_example