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
Binary file not shown.
Binary file not shown.
Binary file not shown.
34 changes: 21 additions & 13 deletions base/terminal/internal/library/library.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
// desktops the build for the platform the package embeds, written once
// into the user's cache.
//
// 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.
// 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

import (
Expand All @@ -31,15 +28,8 @@ func Find() (string, error) {
return p, nil
}
if exe, err := os.Executable(); err == nil {
dir := filepath.Dir(exe)
paths := []string{filepath.Join(dir, "Frameworks", "GhosttyVT.framework", "GhosttyVT")}
if name != "" {
paths = append(paths, filepath.Join(dir, "..", "Frameworks", name), filepath.Join(dir, name))
}
for _, p := range paths {
if _, err := os.Stat(p); err == nil {
return p, nil
}
if path := findInApp(exe); path != "" {
return path, nil
}
}
if embedded == nil {
Expand Down Expand Up @@ -87,3 +77,21 @@ func extract() (string, error) {
}
return path, nil
}

// findInApp searches the host bundle as well as a standalone executable's directory. Catalyst uses a macOS bundle layout even though its terminal takes the iOS path.
func findInApp(executable string) string {
dir := filepath.Dir(executable)
paths := []string{
filepath.Join(dir, "Frameworks", "GhosttyVT.framework", "GhosttyVT"),
filepath.Join(dir, "..", "Frameworks", "GhosttyVT.framework", "GhosttyVT"),
}
if name != "" {
paths = append(paths, filepath.Join(dir, "..", "Frameworks", name), filepath.Join(dir, name))
}
for _, path := range paths {
if info, err := os.Stat(path); err == nil && info.Mode().IsRegular() {
return path
}
}
return ""
}
30 changes: 30 additions & 0 deletions base/terminal/internal/library/library_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//go:build !js

package library

import (
"os"
"path/filepath"
"testing"
)

func TestFindInEmbeddedHostBundle(t *testing.T) {
for _, layout := range []struct{ name, executable, library string }{
{"ios", "Host.app/Host", "Host.app/Frameworks/GhosttyVT.framework/GhosttyVT"},
{"catalyst", "Host.app/Contents/MacOS/Host", "Host.app/Contents/Frameworks/GhosttyVT.framework/GhosttyVT"},
} {
t.Run(layout.name, func(t *testing.T) {
root := t.TempDir()
library := filepath.Join(root, layout.library)
if err := os.MkdirAll(filepath.Dir(library), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(library, []byte("fixture"), 0o755); err != nil {
t.Fatal(err)
}
if got := findInApp(filepath.Join(root, layout.executable)); got != library {
t.Errorf("findInApp = %q, want %q", got, library)
}
})
}
}
6 changes: 1 addition & 5 deletions base/terminal/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,7 @@ func Load() error {
return load.err
}

// LibraryPath returns the libghostty-vt the terminal loads, the first of:
// the file $NSGO_GHOSTTY_VT names; the one beside the executable, or in
// its macOS bundle's Frameworks or Resources; the build the package
// embeds for the platform, written once into the user's cache
// (<cache>/nsgo/libghostty-vt/<sha256>/).
// LibraryPath returns the libghostty-vt the terminal loads, the first of: the file $NSGO_GHOSTTY_VT names; the one beside the executable, or in its iOS or Catalyst GhosttyVT.framework or macOS Frameworks; the build the package embeds for the platform, written once into the user's cache (<cache>/nsgo/libghostty-vt/<sha256>/).
func LibraryPath() (string, error) { return library.Find() }

// New makes a terminal running opts.Command, or connected to opts.Conn.
Expand Down
3 changes: 3 additions & 0 deletions changelog.d/terminal-package-libraries-1454.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Fixed

- Include libghostty-vt beside Windows and Linux executables in arm64 AARs and as a companion XCFramework for embedded iOS and Mac Catalyst apps (#1454 by @houko).
26 changes: 26 additions & 0 deletions cmd/nsgo/android.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"go/parser"
"go/token"
"io"
"io/fs"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -227,6 +228,31 @@ const geometryReader = "github.com/nsgo-dev/nsgo/core.(*Env).Geometry"
// nsgoInternal is the import path prefix of NSGo's internal packages.
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) {
dir, err := linkedDir(ctx, p, []string{"GOOS=android", "GOARCH=" + goarch, "CGO_ENABLED=0"}, ghosttyPackage)
if err != nil {
return apk.File{}, err
}
if dir == "" {
return apk.File{}, nil
}
return androidGhosttyFile(dir, goarch, abi)
}

func androidGhosttyFile(dir, goarch, abi string) (apk.File, error) {
path := filepath.Join(dir, "lib", "android-"+goarch, "libghostty-vt.so")
data, err := os.ReadFile(path)
if errors.Is(err, fs.ErrNotExist) {
// Until #1452 commits an Android prebuild the package has none for this ABI; the AAR builds without it, as before, rather than failing every embedded app that links the terminal.
return apk.File{}, nil
}
if err != nil {
return apk.File{}, fmt.Errorf("base/terminal: read Android libghostty-vt: %w", err)
}
return apk.File{Name: "lib/" + abi + "/libghostty-vt.so", Data: data}, nil
}

// androidLibraries are the device's libraries app, the app's linked executable for goarch, uses where the device has them: the window extensions for an app that reads its fold geometry, so no other app declares them. The name in the function table alone does not tell, as reflection may have kept it, so the app's sources must also select a Geometry.
func androidLibraries(ctx context.Context, p *project, goarch string, app []byte) ([]string, error) {
if !bytes.Contains(app, []byte(geometryReader)) {
Expand Down
4 changes: 2 additions & 2 deletions cmd/nsgo/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -1017,7 +1017,7 @@ func buildWindows(ctx context.Context, p *project, dir, arch string) error {
if err := addWinSparkle(ctx, p, dir, arch); err != nil {
return err
}
return p.addNativeLibraries(ctx, "windows_"+goArchName(arch), dir)
return addDesktopLibraries(ctx, p, dir, "windows", arch)
}

// buildLinux writes <dir>/<name> with the resources and icon beside it.
Expand All @@ -1033,7 +1033,7 @@ func buildLinux(ctx context.Context, p *project, dir, arch string) error {
if err := writeLinuxIcon(p, dir); err != nil {
return err
}
return p.addNativeLibraries(ctx, "linux_"+goArchName(arch), dir)
return addDesktopLibraries(ctx, p, dir, "linux", arch)
}

// buildFPS is -fps: the site starts its app with --nsgo-fps, which a page
Expand Down
56 changes: 45 additions & 11 deletions cmd/nsgo/embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,10 @@ func buildIOSFramework(ctx context.Context, p *project, dir string) error {
if err := os.MkdirAll(resources, 0o755); err != nil {
return err
}
return copyResources(p, resources)
if err := copyResources(p, resources); err != nil {
return err
}
return addIOSEmbeddedLibraries(ctx, p, dir, tmp, minimum)
}

// iosLinks are a static library's module map's link lines: what Go's
Expand Down Expand Up @@ -354,20 +357,17 @@ func writeIOSHeaders(dir, header, modulemap string) error {
// its binary left to the caller: iOS's flat, Mac Catalyst's in macOS's
// versioned layout. It returns the bundle's path.
func writeIOSFramework(slice, module, id, header, minimum string, catalyst bool) (string, error) {
return writeFrameworkBundle(slice, module, id, header, minimum, catalyst)
}

// writeFrameworkBundle writes a dynamic framework bundle; an empty header makes a runtime-only framework, loaded through purego.
func writeFrameworkBundle(slice, module, id, header, minimum string, catalyst bool) (string, error) {
fw := filepath.Join(slice, module+".framework")
root := fw
if catalyst {
root = filepath.Join(fw, "Versions/A")
}
modulemap := fmt.Sprintf("framework module %s {\n\tumbrella header \"NSGo.h\"\n\texport *\n}\n", module)
if err := writeIOSHeaders(filepath.Join(root, "Headers"), header, ""); err != nil {
return "", err
}
os.Remove(filepath.Join(root, "Headers", "module.modulemap"))
if err := os.MkdirAll(filepath.Join(root, "Modules"), 0o755); err != nil {
return "", err
}
if err := os.WriteFile(filepath.Join(root, "Modules", "module.modulemap"), []byte(modulemap), 0o644); err != nil {
if err := writeIOSFrameworkHeaders(root, module, header); err != nil {
return "", err
}
plistDir := root
Expand Down Expand Up @@ -396,15 +396,40 @@ func writeIOSFramework(slice, module, id, header, minimum string, catalyst bool)
}
if catalyst {
for link, to := range map[string]string{"Versions/Current": "A", module: "Versions/Current/" + module,
"Headers": "Versions/Current/Headers", "Modules": "Versions/Current/Modules", "Resources": "Versions/Current/Resources"} {
"Resources": "Versions/Current/Resources"} {
if err := os.Symlink(to, filepath.Join(fw, link)); err != nil {
return "", err
}
}
if header != "" {
for _, name := range []string{"Headers", "Modules"} {
if err := os.Symlink("Versions/Current/"+name, filepath.Join(fw, name)); err != nil {
return "", err
}
}
}
}
return fw, nil
}

func writeIOSFrameworkHeaders(root, module, header string) error {
if header == "" {
return nil
}
modulemap := fmt.Sprintf("framework module %s {\n\tumbrella header \"NSGo.h\"\n\texport *\n}\n", module)
if err := writeIOSHeaders(filepath.Join(root, "Headers"), header, ""); err != nil {
return err
}
os.Remove(filepath.Join(root, "Headers", "module.modulemap"))
if err := os.MkdirAll(filepath.Join(root, "Modules"), 0o755); err != nil {
return err
}
if err := os.WriteFile(filepath.Join(root, "Modules", "module.modulemap"), []byte(modulemap), 0o644); err != nil {
return err
}
return nil
}

// aarManifest is the library's manifest, which a host's build merges into its own: the runtime starts the app from the native library directory, so native libraries must be extracted, the provider of the files the app hands other apps is named after the host's package, the receiver of scheduled notifications' alarms hears the boot to set them again, and the device's libraries the app uses (androidLibraries) are declared without being required. An app that shows overlays over other apps (androidOverlay) asks for SYSTEM_ALERT_WINDOW and declares the overlay's foreground service with its permissions, which no other library does.
func aarManifest(overlay bool, libraries []string) string {
perms, services := "", ""
Expand Down Expand Up @@ -478,6 +503,10 @@ func buildAndroidAAR(ctx context.Context, p *project, dir, goarch string) error
if err != nil {
return err
}
ghostty, err := androidGhosttyLibrary(ctx, p, goarch, abi)
if err != nil {
return err
}
stage(ctx, "packaging the AAR")
f, err := os.Create(filepath.Join(dir, p.Name+".aar"))
if err != nil {
Expand Down Expand Up @@ -505,6 +534,11 @@ func buildAndroidAAR(ctx context.Context, p *project, dir, goarch string) error
return err
}
}
if ghostty.Name != "" {
if err := add("jni/"+strings.TrimPrefix(ghostty.Name, "lib/"), ghostty.Data); err != nil {
return err
}
}
err = walkResources(p, func(rel string, data []byte) error { return add("assets/"+rel, data) })
if err != nil {
return err
Expand Down
86 changes: 86 additions & 0 deletions cmd/nsgo/natives.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,89 @@ func copyLibrary(src, dst string) error {
}
return os.WriteFile(dst, data, 0o755)
}

// addDesktopLibraries places terminal and package-declared libraries beside the executable before signing and installer assembly.
func addDesktopLibraries(ctx context.Context, p *project, dir, system, arch string) error {
env := []string{"GOOS=" + system, "GOARCH=" + arch, "CGO_ENABLED=0"}
source, err := linkedDir(ctx, p, env, ghosttyPackage)
if err != nil {
return err
}
target := system + "_" + goArchName(arch)
if source != "" {
name := "libghostty-vt.so"
if system == "windows" {
name = "ghostty-vt.dll"
}
source = filepath.Join(source, "lib", system+"-"+arch, name)
if err := validateNativeBinary(source, target); err != nil {
return fmt.Errorf("libghostty-vt: %w", err)
}
if err := copyLibrary(source, filepath.Join(dir, name)); err != nil {
return err
}
}
return p.addNativeLibraries(ctx, target, dir)
}

// addIOSEmbeddedLibraries supplies the host with a companion XCFramework whenever its app links the terminal, for static and dynamic embedding alike.
func addIOSEmbeddedLibraries(ctx context.Context, p *project, dir, tmp, minimum string) error {
args := []string{"-create-xcframework"}
for _, sl := range iosSlices {
var libraries []string
for _, arch := range sl.archs {
env := []string{"GOOS=" + sl.goos, "GOARCH=" + arch, "CGO_ENABLED=1"}
tags := []string{"nsgoembed"}
if sl.catalyst {
tags = append(tags, "ios")
}
source, err := linkedDir(ctx, p, env, ghosttyPackage, tags...)
if err != nil {
return err
}
if source == "" {
continue
}
platform := "ios"
if sl.catalyst {
platform = "ioscatalyst"
} else if sl.sdk == "iphonesimulator" {
platform = "iossimulator"
}
libraries = append(libraries, filepath.Join(source, "lib", platform+"-"+arch, "libghostty-vt.dylib"))
}
if len(libraries) == 0 {
continue
}
fw, err := writeFrameworkBundle(filepath.Join(tmp, "ghostty", sl.sdk), "GhosttyVT", p.ID+".ghosttyvt", "", frameworkMinimum(minimum, sl.catalyst), sl.catalyst)
if err != nil {
return err
}
binary, install := filepath.Join(fw, "GhosttyVT"), "@rpath/"+ghosttyFramework+"/GhosttyVT"
if sl.catalyst {
binary = filepath.Join(fw, "Versions/A/GhosttyVT")
install = "@rpath/" + ghosttyFramework + "/Versions/A/GhosttyVT"
}
if err := run(ctx, "lipo", append([]string{"-create", "-output", binary}, libraries...)...); err != nil {
return err
}
if err := run(ctx, "install_name_tool", "-id", install, binary); err != nil {
return err
}
if err := run(ctx, "codesign", "--force", "--sign", "-", fw); err != nil {
return err
}
args = append(args, "-framework", fw)
}
if len(args) > 1 && embedModule(p) == "GhosttyVT" {
return fmt.Errorf("the app framework name GhosttyVT conflicts with the terminal companion: choose another app name")
}
framework := filepath.Join(dir, "GhosttyVT.xcframework")
if err := os.RemoveAll(framework); err != nil {
return err
}
if len(args) == 1 {
return nil
}
return run(ctx, "xcodebuild", append(args, "-output", framework)...)
}
Loading
Loading