build: add CMake support for libghostty-vt

Add a top-level CMakeLists.txt that wraps `zig build lib-vt` so that
CMake-based downstream projects can consume libghostty-vt without
needing to interact with the Zig build system directly. A custom
command triggers the zig build during `cmake --build`, and the
resulting shared library is exposed as an IMPORTED target.

Downstream projects can pull in the library via FetchContent, which
fetches the source and builds it as part of their own CMake build, or
via find_package after a manual install step. The package config
template in dist/cmake/ sets up the ghostty-vt::ghostty-vt target
with proper include paths and macOS rpath handling.

A c-vt-cmake example demonstrates the FetchContent workflow, creating
a terminal, writing VT sequences, and formatting the output as plain
text. CI is updated to auto-discover and build CMake-based examples
alongside the existing Zig-based ones.
This commit is contained in:
Mitchell Hashimoto
2026-03-20 10:21:31 -07:00
parent e680cf9f35
commit 3dee62f904
10 changed files with 400 additions and 7 deletions

View File

@@ -85,7 +85,8 @@ jobs:
- skip
- build-bench
- build-dist
- build-examples
- build-examples-zig
- build-examples-cmake
- build-flatpak
- build-libghostty-vt
- build-libghostty-vt-android
@@ -172,22 +173,26 @@ jobs:
needs: skip
runs-on: namespace-profile-ghostty-xsm
outputs:
dirs: ${{ steps.list.outputs.dirs }}
zig: ${{ steps.list.outputs.zig }}
cmake: ${{ steps.list.outputs.cmake }}
steps:
- name: Checkout code
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
- id: list
name: List example directories
run: |
dirs=$(ls example/*/build.zig.zon 2>/dev/null | xargs -n1 dirname | xargs -n1 basename | jq -R -s -c 'split("\n") | map(select(. != ""))')
echo "$dirs" | jq .
echo "dirs=$dirs" >> "$GITHUB_OUTPUT"
zig=$(ls example/*/build.zig.zon 2>/dev/null | xargs -n1 dirname | xargs -n1 basename | jq -R -s -c 'split("\n") | map(select(. != ""))')
echo "$zig" | jq .
echo "zig=$zig" >> "$GITHUB_OUTPUT"
cmake=$(ls example/*/CMakeLists.txt 2>/dev/null | xargs -n1 dirname | xargs -n1 basename | jq -R -s -c 'split("\n") | map(select(. != ""))')
echo "$cmake" | jq .
echo "cmake=$cmake" >> "$GITHUB_OUTPUT"
build-examples:
build-examples-zig:
strategy:
fail-fast: false
matrix:
dir: ${{ fromJSON(needs.list-examples.outputs.dirs) }}
dir: ${{ fromJSON(needs.list-examples.outputs.zig) }}
name: Example ${{ matrix.dir }}
runs-on: namespace-profile-ghostty-xsm
needs: [test, list-examples]
@@ -219,6 +224,43 @@ jobs:
cd example/${{ matrix.dir }}
nix develop -c zig build
build-examples-cmake:
strategy:
fail-fast: false
matrix:
dir: ${{ fromJSON(needs.list-examples.outputs.cmake) }}
name: Example ${{ matrix.dir }}
runs-on: namespace-profile-ghostty-xsm
needs: [test, list-examples]
env:
ZIG_LOCAL_CACHE_DIR: /zig/local-cache
ZIG_GLOBAL_CACHE_DIR: /zig/global-cache
steps:
- name: Checkout code
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
- name: Setup Cache
uses: namespacelabs/nscloud-cache-action@a90bb5d4b27522ce881c6e98eebd7d7e6d1653f9 # v1.4.2
with:
path: |
/nix
/zig
# Install Nix and use that to run our tests so our environment matches exactly.
- uses: cachix/install-nix-action@1ca7d21a94afc7c957383a2d217460d980de4934 # v31.10.1
with:
nix_path: nixpkgs=channel:nixos-unstable
- uses: cachix/cachix-action@1eb2ef646ac0255473d23a5907ad7b04ce94065c # v17
with:
name: ghostty
authToken: "${{ secrets.CACHIX_AUTH_TOKEN }}"
- name: Build Example
run: |
cd example/${{ matrix.dir }}
nix develop -c cmake -B build -DFETCHCONTENT_SOURCE_DIR_GHOSTTY=${{ github.workspace }}
nix develop -c cmake --build build
build-flatpak:
strategy:
fail-fast: false