app_darwin.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. ctx, cancel := context.WithCancel(context.Background())
  39. var done chan int
  40. done, err = SpawnServer(ctx, filepath.Join(filepath.Dir(exe), "..", "Resources", "ollama"))
  41. if err != nil {
  42. slog.Error(fmt.Sprintf("Failed to spawn ollama server %s", err))
  43. done = make(chan int, 1)
  44. done <- 1
  45. }
  46. // Run the native macOS app
  47. // Note: this will block until the app is closed
  48. C.run()
  49. slog.Info("ollama macOS app closed")
  50. cancel()
  51. slog.Info("Waiting for ollama server to shutdown...")
  52. if done != nil {
  53. <-done
  54. }
  55. slog.Info("Ollama app exiting")
  56. }
  57. //export Quit
  58. func Quit() {
  59. syscall.Kill(os.Getpid(), syscall.SIGTERM)
  60. }