app_darwin.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package main
  2. // #cgo CFLAGS: -x objective-c
  3. // #cgo LDFLAGS: -framework Cocoa -framework LocalAuthentication -framework ServiceManagement
  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. // Ask to move to applications directory
  25. moving := C.askToMoveToApplications()
  26. if moving {
  27. return
  28. }
  29. C.killOtherInstances()
  30. code := C.installSymlink()
  31. if code != 0 {
  32. slog.Error("Failed to install symlink")
  33. }
  34. exe, err := os.Executable()
  35. if err != nil {
  36. panic(err)
  37. }
  38. var options ServerOptions
  39. ctx, cancel := context.WithCancel(context.Background())
  40. var done chan int
  41. done, err = SpawnServer(ctx, filepath.Join(filepath.Dir(exe), "..", "Resources", "ollama"), options)
  42. if err != nil {
  43. slog.Error(fmt.Sprintf("Failed to spawn ollama server %s", err))
  44. done = make(chan int, 1)
  45. done <- 1
  46. }
  47. // Run the native macOS app
  48. // Note: this will block until the app is closed
  49. C.run()
  50. slog.Info("ollama macOS app closed")
  51. cancel()
  52. slog.Info("Waiting for ollama server to shutdown...")
  53. if done != nil {
  54. <-done
  55. }
  56. slog.Info("Ollama app exiting")
  57. }
  58. //export Quit
  59. func Quit() {
  60. syscall.Kill(os.Getpid(), syscall.SIGTERM)
  61. }