name: ghostty
base: core24
summary: A terminal emulator
description: |
  Ghostty is a fast, feature-rich, and cross-platform terminal emulator that
  uses platform-native UI and GPU acceleration.
grade: stable
confinement: classic
contact: https://github.com/ghostty-org/ghostty/discussions
issues: https://github.com/ghostty-org/ghostty/issues
website: https://ghostty.org
license: MIT
icon: images/icons/icon_512.png
adopt-info: ghostty

platforms:
  amd64:
  arm64:

apps:
  ghostty:
    command: bin/ghostty
    command-chain: [app/launcher]
    completer: share/bash-completion/completions/ghostty.bash
    desktop: share/applications/com.mitchellh.ghostty.desktop
    #refresh-mode: ignore-running # Store rejects this, needs fix in review-tools
    environment:
      PATH: /snap/ghostty/current/bin:/snap/ghostty/current/usr/bin:$PATH
      LC_ALL: C.UTF-8
      GHOSTTY_RESOURCES_DIR: /snap/ghostty/current/share/ghostty

parts:
  launcher:
    plugin: dump
    source: snap/local
    source-type: local
    organize:
      launcher: app/

  zig:
    plugin: nil
    build-packages:
      - curl
    override-pull: |
      set -ex
      case "$CRAFT_ARCH_BUILD_FOR" in
        amd64)  arch=x86_64    ;;
        arm64)  arch=aarch64  ;;
        *)      arch=""     ;;
      esac

      rm -rf $CRAFT_PART_SRC/*

      if [[ -n $arch ]]; then
        curl -LO --retry-connrefused --retry 10 https://ziglang.org/download/0.15.2/zig-$arch-linux-0.15.2.tar.xz
      else
        echo "Unsupported arch"
        exit 1
      fi

      tar xf zig-$arch-lin*xz
      rm -f *xz
      mv zig-$arch-linux*/* .
    prime:
      - -*

  ghostty:
    source: .
    after: [zig]
    plugin: nil
    build-attributes: [enable-patchelf]
    build-packages:
      - libgtk-4-dev
      - libadwaita-1-dev
      - libxml2-utils
      - git
      - patchelf
      - gettext
    # TODO: Remove -fno-sys=gtk4-layer-shell when we upgrade to a version that packages it Ubuntu 24.10+
    override-build: |
      craftctl set version=$(cat VERSION)
      $CRAFT_PART_SRC/../../zig/src/zig build \
        -Dsnap \
        -Dpatch-rpath=\$ORIGIN/../usr/lib/$CRAFT_ARCH_TRIPLET_BUILD_FOR:/snap/core24/current/lib/$CRAFT_ARCH_TRIPLET_BUILD_FOR \
        -Doptimize=ReleaseFast \
        -Dcpu=baseline \
        -fno-sys=gtk4-layer-shell
      cp -rp zig-out/* $CRAFT_PART_INSTALL/
      sed -i 's|Icon=com.mitchellh.ghostty|Icon=${SNAP}/share/icons/hicolor/512x512/apps/com.mitchellh.ghostty.png|g' $CRAFT_PART_INSTALL/share/applications/com.mitchellh.ghostty.desktop

  libs:
    plugin: nil
    build-attributes: [enable-patchelf]
    stage-packages:
      - libadwaita-1-0
      - libglib2.0-0t64
      - libgtk-4-1
      - libgtk-4-media-gstreamer
      - ibus-gtk4
      - libpciaccess0
      - libtinfo6
      - libedit2
      - libelf1t64
      - libsensors5
      - libllvm17
      - libunistring5
      - librsvg2-2
      - librsvg2-common
      - libgdk-pixbuf-2.0-0
      - on amd64:
          [
            i965-va-driver,
            libdrm-intel1,
            libdrm-nouveau2,
            libdrm-amdgpu1,
            libdrm-radeon1,
          ]
    stage:
      # The libraries in dri need no-patchelf, so they come from the mesa-unpatched part
      - -usr/lib/*/dri

  mesa:
    plugin: nil
    build-attributes: [enable-patchelf]
    stage-packages:
      - libglu1-mesa
      - libgl1-mesa-dri
      - libegl-mesa0
      - libegl1
      - libglx-mesa0
      - mesa-libgallium
    stage:
      # The libraries in dri need no-patchelf, so they come from the mesa-unpatched part
      - usr/lib/*/*.so*
      - usr/lib/*/dri/libdril_dri.so
      - -usr/lib/*/libxml2.so.*
      - -usr/lib/*/libgallium*so
      - -usr/lib/*/dri

  mesa-gl1-dri:
    plugin: nil
    stage-packages:
      - libgl1-mesa-dri
    build-attributes: [no-patchelf]
    stage:
      # Only the libraries in dri need to not be patched, the rest come from the mesa part
      # Otherwise snapcraft may strip the build ID and cause the driver to crash
      - usr/lib/*/libgallium*so
      - -usr/lib/*/dri/libdril_dri.so
      - usr/lib/*/dri
