Go bindings for libghostty-vt.
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Document and test cross-compilation

One of the common stigmas of cgo in Go projects is that it makes
cross-compilation overly difficult. Its more difficult than pure Go projects,
to be sure, but with the right shape of libraries and build scripts, it
can be made to work well. libghostty is a good example of this.

libghostty only depends on libc and the Zig compiler (tool, not language)
as a drop-in replacement for c/c++ compilation means we can easily
cross-compile!

This commit adds documentation, tests, and examples on how to do this.

+132 -1
+32
.github/workflows/test.yml
··· 88 88 - name: Run example 89 89 run: nix develop -c go run ./examples/${{ matrix.example }}/ 90 90 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 + 91 123 build-shared: 92 124 runs-on: namespace-profile-ghostty-sm 93 125 env:
+10
CMakeLists.txt
··· 7 7 GIT_TAG 2ed382a15566b267c32fae440b065f7844b15bfb 8 8 ) 9 9 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)
+49 -1
Makefile
··· 9 9 # Stamp file to track whether the cmake build has run. 10 10 STAMP := $(BUILD_DIR)/.ghostty-built 11 11 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)) 13 42 14 43 $(STAMP): 15 44 cmake -B $(BUILD_DIR) -DCMAKE_BUILD_TYPE=Release ··· 21 50 22 51 test: $(STAMP) 23 52 PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) DYLD_LIBRARY_PATH=$(DYLD_LIBRARY_PATH) LD_LIBRARY_PATH=$(LD_LIBRARY_PATH) go test ./... 53 + 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)))) 24 72 25 73 clean: 26 74 rm -rf $(BUILD_DIR)
+41
README.md
··· 79 79 See the [Ghostty docs](https://ghostty.org/docs/install/build) for 80 80 building `libghostty-vt` from source. 81 81 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 + 82 123 ## Development 83 124 84 125 CMake fetches and builds `libghostty-vt` automatically. CMake is only