Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions base/terminal/internal/library/embed_android_arm64.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//go:build android && arm64

package library

// Android packages place this library beside libnsgo.so in the APK's native
// library directory. It is intentionally not embedded in the Go executable.
const name = "libghostty-vt.so"

var embedded []byte
2 changes: 1 addition & 1 deletion base/terminal/internal/library/embed_other.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build !((darwin && !ios && (arm64 || amd64)) || (linux && !android && (arm64 || amd64)) || (windows && (arm64 || amd64)))
//go:build !((darwin && !ios && (arm64 || amd64)) || (linux && !android && (arm64 || amd64)) || (windows && (arm64 || amd64)) || (android && arm64))

package library

Expand Down
Binary file not shown.
2 changes: 1 addition & 1 deletion base/terminal/internal/library/library.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Package library finds libghostty-vt for the terminal: the file
// $NSGO_GHOSTTY_VT names, the one the nsgo tool put into the app, or on
// desktops the build for the platform the package embeds, written once
// into the user's cache.
// into the user's cache. Android's package places its build beside libnsgo.so.
//
// The builds in lib/ come from Ghostty's sources at the commit the terminal binds, built by `go run ./tools/libghostty`. The nsgo tool signs them into iOS apps as GhosttyVT.framework and into macOS apps' Frameworks, stages it beside Windows and Linux executables, and supplies an iOS host's companion GhosttyVT.xcframework.
package library
Expand Down
23 changes: 23 additions & 0 deletions base/terminal/internal/vt/ffi_android.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//go:build android && arm64

package vt

import "github.com/nsgo-dev/nsgo/internal/purego"

// Supported reports whether the platform can load libghostty-vt.
const Supported = true

// call calls a C function whose arguments are integers or pointers. Go
// pointers converted in the call are moved to the heap and kept alive until
// it returns (go:uintptrescapes), so stack growth cannot move them while the
// library uses them.
//
//go:uintptrescapes
func call(fn uintptr, args ...uintptr) uintptr {
r, _, _ := purego.SyscallN(fn, args...)
return r
}

func registerFunc(fptr any, addr uintptr) { purego.RegisterFunc(fptr, addr) }
func newCallback(_ string, fn any) uintptr { return purego.NewCallback(fn) }
func setupPlatform() {}
2 changes: 1 addition & 1 deletion base/terminal/internal/vt/ffi_other.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build (js && (nsgominiprogram || nsgoharmony)) || (!js && !(darwin || (linux && !android && (amd64 || arm64)) || (windows && (amd64 || arm64))))
//go:build (js && (nsgominiprogram || nsgoharmony)) || (!js && !(darwin || (linux && !android && (amd64 || arm64)) || (windows && (amd64 || arm64)) || (android && arm64)))

package vt

Expand Down
11 changes: 11 additions & 0 deletions base/terminal/internal/vt/open_android.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//go:build android && arm64

package vt

import "github.com/nsgo-dev/nsgo/internal/purego"

func open(path string) (uintptr, error) {
return purego.Dlopen(path, purego.RTLD_NOW|purego.RTLD_LOCAL)
}

func sym(h uintptr, name string) (uintptr, error) { return purego.Dlsym(h, name) }
2 changes: 1 addition & 1 deletion base/terminal/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// user's shell, or any program, in a pseudo-terminal, with Ghostty's
// terminal emulator, libghostty-vt, loaded at run time with no cgo.
//
// It runs programs on macOS, Linux and Windows, and remote sessions (Options.Conn) there, on iOS and in the browser; elsewhere New returns an error. It does what terminals do: colors (16, 256 and 24-bit) and styles, wide characters and grapheme clusters, a scrollback that reflows, the alternate screen, the keyboard as programs ask (legacy sequences, modifyOtherKeys or the Kitty keyboard protocol), bracketed paste, the title (OSC 0 and 2) and the working directory (OSC 7).
// It runs programs on macOS, Linux and Windows, and remote sessions (Options.Conn) there, on iOS, Android and in the browser; elsewhere New returns an error. It does what terminals do: colors (16, 256 and 24-bit) and styles, wide characters and grapheme clusters, a scrollback that reflows, the alternate screen, the keyboard as programs ask (legacy sequences, modifyOtherKeys or the Kitty keyboard protocol), bracketed paste, the title (OSC 0 and 2) and the working directory (OSC 7).
package terminal

import (
Expand Down
3 changes: 3 additions & 0 deletions changelog.d/terminal-android-1452.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Added

- Run remote `base/terminal` sessions in Android arm64 apps with a cgo-free bionic loader and packaged Ghostty library (#1452 by @houko).
9 changes: 9 additions & 0 deletions cmd/nsgo/android.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ func buildAndroidPackage(ctx context.Context, p *project, dir, ext string,
return "", err
}
files = append(files, apk.File{Name: "lib/" + abi + "/libnsgo.so", Data: app})
if ghostty, err := androidGhosttyLibrary(ctx, p, goarch, abi); err != nil {
return "", err
} else if ghostty.Data != nil {
files = append(files, ghostty)
}
required, err := androidLibraries(ctx, p, goarch, app)
if err != nil {
return "", err
Expand Down Expand Up @@ -261,6 +266,10 @@ const nsgoInternal = "github.com/nsgo-dev/nsgo/internal/"

// androidGhosttyLibrary returns the prebuilt libghostty-vt for an app that links base/terminal. Android's loader searches the APK's ABI directory, so the library is a sibling of libnsgo.so rather than a Go-embedded payload.
func androidGhosttyLibrary(ctx context.Context, p *project, goarch, abi string) (apk.File, error) {
// Only arm64 has a prebuild, and only arm64 builds without cgo: listing android/amd64 with CGO_ENABLED=0 fails, as it must link externally.
if goarch != "arm64" {
return apk.File{}, nil
}
dir, err := linkedDir(ctx, p, []string{"GOOS=android", "GOARCH=" + goarch, "CGO_ENABLED=0"}, ghosttyPackage)
if err != nil {
return apk.File{}, err
Expand Down
25 changes: 25 additions & 0 deletions cmd/nsgo/android_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,31 @@ func TestFindNDKInTheSDK(t *testing.T) {
}
}

func TestAndroidGhosttyLibrary(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "lib", "android-arm64", "libghostty-vt.so")
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
t.Fatal(err)
}
want := []byte("ELF ghostty")
if err := os.WriteFile(path, want, 0o644); err != nil {
t.Fatal(err)
}
// linkedDir is deliberately bypassed here: this test exercises the APK
// path and ABI naming after dependency discovery has selected the package.
file, err := androidGhosttyFile(dir, "arm64", "arm64-v8a")
if err != nil {
t.Fatal(err)
}
if file.Name != "lib/arm64-v8a/libghostty-vt.so" || !bytes.Equal(file.Data, want) {
t.Fatalf("Android Ghostty file = %+v, want ABI path and bytes", file)
}
// x64 has no prebuild and cannot be listed without cgo, so it must not reach go list at all.
if file, err := androidGhosttyLibrary(t.Context(), nil, "amd64", "x86_64"); err != nil || file.Data != nil {
t.Fatalf("Android x64 Ghostty library = %+v, %v; want none", file, err)
}
}

// TestAndroidLibraries checks that only an app whose function table names the fold geometry's reader and whose sources select it declares the window extensions.
func TestAndroidLibraries(t *testing.T) {
if libs, err := androidLibraries(t.Context(), nil, "arm64", []byte("\x00github.com/nsgo-dev/nsgo/core.(*Env).Window\x00")); libs != nil || err != nil {
Expand Down
2 changes: 1 addition & 1 deletion docs/architecture/gpu.md
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ NSGo runs Android apps as a Go executable started by a prebuilt Java runtime (`c

Vulkan on Android is explicitly deferred to Stage 2 once workloads need compute shaders, read/write storage buffers, or lower CPU/pipe overhead than the Java GL thread provides. Two architectural prerequisites in NSGo must land first:

1. **No-cgo bionic `dlopen` on arm64 Android**: `purego` currently loads shared libraries on Android only with cgo (the same blocker recorded in the [roadmap](../roadmap.md#terminal) for `libghostty-vt`). Loading `libvulkan.so` in `CGO_ENABLED=0` arm64 builds requires a Go bionic loader.
1. **Cross-process surface sharing**: the Android backend still needs a safe Vulkan surface handoff between the Java runtime and Go before Vulkan can replace the OpenGL path. The no-cgo bionic loader is shared with `base/terminal` now.
2. **Cross-process surface sharing**: Because the Go app runs in a child process while Android Activities own `Surface`/`ANativeWindow`, presenting Vulkan swapchains directly from Go requires passing an `ANativeWindow` / `Surface` over Binder or adopting the process-lifetime changes tracked in [#12](https://github.com/nsgo-dev/nsgo/issues/12).

## Target scope (Decision 4)
Expand Down
2 changes: 1 addition & 1 deletion docs/architecture/layers.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ A line means "imports". `internal/moduletest/layering_test.go` checks the graph
- `updater` imports only `core` and its catalogs; `internal/backend` imports it, to check as the app starts where the platform's updater keeps no schedule.
- `ui` imports `base`, `core`, `mobile`, `desktop` and the icons; `base` never imports it.
- `tsx/tsxtest` is the one package that imports both `tsx` and `nsgotest`, and `tsx/ui` with the packages below it the TSX packages that import `ui`. `tsx/internal/js`, the engines' API, imports nothing of NSGo's, and on the web reaches the page through `syscall/js`.
- `base/terminal` is the one package of `base` that is not portable: it loads native libghostty-vt through purego or an embedded browser WebAssembly module, runs programs on desktops and remote sessions there, on iOS and in the browser, and elsewhere builds to report that it cannot (`unportable` in `layering_test.go`).
- `base/terminal` is the one package of `base` that is not portable: it loads native libghostty-vt through purego or an embedded browser WebAssembly module, runs programs on desktops and remote sessions there, on iOS and Android and in the browser, and elsewhere builds to report that it cannot (`unportable` in `layering_test.go`).

## Why core and backends do not meet

Expand Down
2 changes: 1 addition & 1 deletion docs/architecture/terminal.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
| `tsx/terminal` | `Terminal` in TSX's `nsgo/base`: the view over a session its props start ([TSX](tsx.md#go-components)) |
| `base/terminal/internal/library` | Provides the embedded browser module, or finds a native library: `$NSGO_GHOSTTY_VT`; the one the nsgo tool put into the app, an iOS app's `GhosttyVT.framework` or a macOS app's `Frameworks`, or one beside the executable; else on desktops the build it embeds for the platform, written once into `<cache>/nsgo/libghostty-vt/<sha256>/` |

It is a package under `base` because it links what an app without it should not, and the one there that is not portable: it reaches native libghostty-vt through purego, the browser's guest through `syscall/js`, and programs through `syscall` ([package layers](layers.md)). Desktops run local programs and remote sessions; iOS and the browser run remote sessions only (`Options.Conn`), and `New` without one returns an error there. Android, mini programs and HarmonyOS still return an unsupported error ([roadmap](../roadmap.md#terminal)). It imports `core` and `i18n`. Its only look is the terminal's palette, as `Editor`'s is its highlighting's: `Theme` and `DarkTheme`, Visual Studio Code's colors when unset; the app styles the view around the grid.
It is a package under `base` because it links what an app without it should not, and the one there that is not portable: it reaches native libghostty-vt through purego, the browser's guest through `syscall/js`, and programs through `syscall` ([package layers](layers.md)). Desktops run local programs and remote sessions; iOS, Android and the browser run remote sessions only (`Options.Conn`), and `New` without one returns an error there. Mini programs and HarmonyOS still return an unsupported error ([roadmap](../roadmap.md#terminal)). It imports `core` and `i18n`. Its only look is the terminal's palette, as `Editor`'s is its highlighting's: `Theme` and `DarkTheme`, Visual Studio Code's colors when unset; the app styles the view around the grid.

## The library

Expand Down
4 changes: 4 additions & 0 deletions internal/moduletest/layering_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,10 @@ func checkLayer(t *testing.T, p listedPackage, tags string) {
}
isPortable := matchesAny(pkg, portable) && !matchesAny(pkg, unportable)
for _, imp := range p.Imports {
// Android arm64 terminal binding uses NSGo’s cgo-free purego subset.
if pkg == "base/terminal/internal/vt" && imp == modulePath+"/internal/purego" {
continue
}
if dep, ok := inModule(imp); ok {
if ruled && !matchesAny(dep, allowed) {
t.Errorf("%s imports %s across layers", p.ImportPath, imp)
Expand Down
Loading
Loading