Skip to content

Commit 185ce46

Browse files
committed
Cross-compilation
1 parent 10fa073 commit 185ce46

4 files changed

Lines changed: 132 additions & 1 deletion

File tree

.github/workflows/test.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,38 @@ jobs:
8888
- name: Run example
8989
run: nix develop -c go run ./examples/${{ matrix.example }}/
9090

91+
cross:
92+
strategy:
93+
fail-fast: false
94+
matrix:
95+
target:
96+
- linux-amd64
97+
- linux-arm64
98+
- macos-amd64
99+
- macos-arm64
100+
- windows-amd64
101+
- windows-arm64
102+
name: Cross ${{ matrix.target }}
103+
runs-on: namespace-profile-ghostty-sm
104+
env:
105+
ZIG_LOCAL_CACHE_DIR: /zig/local-cache
106+
ZIG_GLOBAL_CACHE_DIR: /zig/global-cache
107+
steps:
108+
- name: Checkout code
109+
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
110+
- name: Setup Cache
111+
uses: namespacelabs/nscloud-cache-action@a90bb5d4b27522ce881c6e98eebd7d7e6d1653f9 # v1.4.2
112+
with:
113+
path: |
114+
/nix
115+
/zig
116+
- name: Setup Nix
117+
uses: cachix/install-nix-action@616559265b40713947b9c190a8ff4b507b5df49b # v31.10.4
118+
with:
119+
nix_path: nixpkgs=channel:nixos-unstable
120+
- name: Cross-compile
121+
run: nix develop -c make cross-${{ matrix.target }}
122+
91123
build-shared:
92124
runs-on: namespace-profile-ghostty-sm
93125
env:

CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,13 @@ FetchContent_Declare(ghostty
77
GIT_TAG 2ed382a15566b267c32fae440b065f7844b15bfb
88
)
99
FetchContent_MakeAvailable(ghostty)
10+
11+
# Cross-compilation targets for CI and multi-platform builds.
12+
# Each call produces ghostty-vt-static-<NAME> and ghostty-vt-<NAME>
13+
# IMPORTED targets whose output lands under build/ghostty-<NAME>/.
14+
ghostty_vt_add_target(NAME linux-amd64 ZIG_TARGET x86_64-linux-gnu)
15+
ghostty_vt_add_target(NAME linux-arm64 ZIG_TARGET aarch64-linux-gnu)
16+
ghostty_vt_add_target(NAME macos-amd64 ZIG_TARGET x86_64-macos)
17+
ghostty_vt_add_target(NAME macos-arm64 ZIG_TARGET aarch64-macos)
18+
ghostty_vt_add_target(NAME windows-amd64 ZIG_TARGET x86_64-windows-gnu)
19+
ghostty_vt_add_target(NAME windows-arm64 ZIG_TARGET aarch64-windows-gnu)

Makefile

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,36 @@ LD_LIBRARY_PATH := $(GHOSTTY_ZIG_OUT)/lib
99
# Stamp file to track whether the cmake build has run.
1010
STAMP := $(BUILD_DIR)/.ghostty-built
1111

12-
.PHONY: build test clean
12+
# Cross-compilation target definitions.
13+
# Each entry maps a make target suffix to GOOS, GOARCH, zig target triple,
14+
# and the CC/CXX target flag for zig cc.
15+
CROSS_TARGETS := linux-amd64 linux-arm64 macos-amd64 macos-arm64 windows-amd64 windows-arm64
16+
17+
linux-amd64_GOOS := linux
18+
linux-amd64_GOARCH := amd64
19+
linux-amd64_ZIG := x86_64-linux-gnu
20+
21+
linux-arm64_GOOS := linux
22+
linux-arm64_GOARCH := arm64
23+
linux-arm64_ZIG := aarch64-linux-gnu
24+
25+
macos-amd64_GOOS := darwin
26+
macos-amd64_GOARCH := amd64
27+
macos-amd64_ZIG := x86_64-macos
28+
29+
macos-arm64_GOOS := darwin
30+
macos-arm64_GOARCH := arm64
31+
macos-arm64_ZIG := aarch64-macos
32+
33+
windows-amd64_GOOS := windows
34+
windows-amd64_GOARCH := amd64
35+
windows-amd64_ZIG := x86_64-windows-gnu
36+
37+
windows-arm64_GOOS := windows
38+
windows-arm64_GOARCH := arm64
39+
windows-arm64_ZIG := aarch64-windows-gnu
40+
41+
.PHONY: build test clean cross $(addprefix cross-,$(CROSS_TARGETS))
1342

1443
$(STAMP):
1544
cmake -B $(BUILD_DIR) -DCMAKE_BUILD_TYPE=Release
@@ -22,5 +51,24 @@ build: $(STAMP)
2251
test: $(STAMP)
2352
PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) DYLD_LIBRARY_PATH=$(DYLD_LIBRARY_PATH) LD_LIBRARY_PATH=$(LD_LIBRARY_PATH) go test ./...
2453

54+
# cross builds all cross-compilation targets.
55+
cross: $(addprefix cross-,$(CROSS_TARGETS))
56+
57+
# cross-<target> cross-compiles the Go package for the given target using
58+
# zig cc and the libghostty-vt static library built by CMake.
59+
define CROSS_RULE
60+
cross-$(1): $(STAMP)
61+
CGO_ENABLED=1 \
62+
CC="zig cc -target $$($(1)_ZIG)" \
63+
CXX="zig c++ -target $$($(1)_ZIG)" \
64+
GOOS=$$($(1)_GOOS) \
65+
GOARCH=$$($(1)_GOARCH) \
66+
CGO_CFLAGS="-I$(CURDIR)/$(BUILD_DIR)/ghostty-$(1)/include -DGHOSTTY_STATIC" \
67+
CGO_LDFLAGS="-L$(CURDIR)/$(BUILD_DIR)/ghostty-$(1)/lib -lghostty-vt" \
68+
go build . ./sys/...
69+
endef
70+
71+
$(foreach t,$(CROSS_TARGETS),$(eval $(call CROSS_RULE,$(t))))
72+
2573
clean:
2674
rm -rf $(BUILD_DIR)

README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,47 @@ go build -tags dynamic
7979
See the [Ghostty docs](https://ghostty.org/docs/install/build) for
8080
building `libghostty-vt` from source.
8181

82+
### Cross-Compilation
83+
84+
Because `libghostty-vt` only depends on libc, cross-compilation is
85+
straightforward using [Zig](https://ziglang.org/) as the C compiler.
86+
Zig is already required to build `libghostty-vt`, so no extra tooling
87+
is needed. You don't need to write any Zig code, we're just using
88+
Zig as a C/C++ compiler.
89+
90+
First, build `libghostty-vt` for your target (from the ghostty source tree):
91+
92+
```shell
93+
zig build -Demit-lib-vt -Dtarget=x86_64-linux-gnu --prefix /tmp/ghostty-linux-amd64
94+
```
95+
96+
Then cross-compile your Go project with `zig cc`:
97+
98+
```shell
99+
CGO_ENABLED=1 \
100+
GOOS=linux GOARCH=amd64 \
101+
CC="zig cc -target x86_64-linux-gnu" \
102+
CXX="zig c++ -target x86_64-linux-gnu" \
103+
CGO_CFLAGS="-I/tmp/ghostty-linux-amd64/include -DGHOSTTY_STATIC" \
104+
CGO_LDFLAGS="-L/tmp/ghostty-linux-amd64/lib -lghostty-vt" \
105+
go build ./...
106+
```
107+
108+
Supported targets include `x86_64-linux-gnu`, `aarch64-linux-gnu`,
109+
`x86_64-macos`, `aarch64-macos`, `x86_64-windows-gnu`, and
110+
`aarch64-windows-gnu`.
111+
112+
If you are using ghostty's CMake integration via `FetchContent`, the
113+
`ghostty_vt_add_target()` function handles the zig build for you:
114+
115+
```cmake
116+
FetchContent_MakeAvailable(ghostty)
117+
ghostty_vt_add_target(NAME linux-amd64 ZIG_TARGET x86_64-linux-gnu)
118+
```
119+
120+
See the [ghostty CMakeLists.txt](https://github.com/ghostty-org/ghostty/blob/main/CMakeLists.txt)
121+
for full documentation of `ghostty_vt_add_target()`.
122+
82123
## Development
83124

84125
CMake fetches and builds `libghostty-vt` automatically. CMake is only

0 commit comments

Comments
 (0)