app_darwin.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package main
  2. // #cgo CFLAGS: -x objective-c -Wno-deprecated-declarations
  3. // #cgo LDFLAGS: -framework Cocoa -framework LocalAuthentication
  4. // #include "app_darwin.h"
  5. import "C"
  6. import (
  7. "context"
  8. "fmt"
  9. "log/slog"
  10. "os"
  11. "path/filepath"
  12. "syscall"
  13. )
  14. func init() {
  15. home, err := os.UserHomeDir()
  16. if err != nil {
  17. panic(err)
  18. }
  19. ServerLogFile = filepath.Join(home, ".ollama", "logs", "server.log")
  20. }
  21. func run() {
  22. initLogging()
  23. slog.Info("ollama macOS app started")
  24. C.killOtherInstances()
  25. exe, err := os.Executable()
  26. if err != nil {
  27. panic(err)
  28. }
  29. ctx, cancel := context.WithCancel(context.Background())
  30. var done chan int
  31. done, err = SpawnServer(ctx, filepath.Join(filepath.Dir(exe), "..", "Resources", "ollama"))
  32. if err != nil {
  33. slog.Error(fmt.Sprintf("Failed to spawn ollama server %s", err))
  34. done = make(chan int, 1)
  35. done <- 1
  36. }
  37. // Run the native macOS app
  38. // Note: this will block until the app is closed
  39. C.run()
  40. slog.Info("ollama macOS app closed")
  41. cancel()
  42. slog.Info("Waiting for ollama server to shutdown...")
  43. if done != nil {
  44. <-done
  45. }
  46. slog.Info("Ollama app exiting")
  47. }
  48. //export Quit
  49. func Quit() {
  50. syscall.Kill(os.Getpid(), syscall.SIGTERM)
  51. }