path.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package discover
  2. import (
  3. "os"
  4. "path/filepath"
  5. "runtime"
  6. )
  7. // LibPath is a path to lookup dynamic libraries
  8. // in development it's usually 'build/lib/ollama'
  9. // in distribution builds it's 'lib/ollama' on Windows
  10. // '../lib/ollama' on Linux and the executable's directory on macOS
  11. // note: distribution builds, additional GPU-specific libraries are
  12. // found in subdirectories of the returned path, such as
  13. // 'cuda_v11', 'cuda_v12', 'rocm', etc.
  14. var LibOllamaPath string = func() string {
  15. exe, err := os.Executable()
  16. if err != nil {
  17. return ""
  18. }
  19. exe, err = filepath.EvalSymlinks(exe)
  20. if err != nil {
  21. return ""
  22. }
  23. var libPath string
  24. switch runtime.GOOS {
  25. case "windows":
  26. libPath = filepath.Join(filepath.Dir(exe), "lib", "ollama")
  27. case "linux":
  28. libPath = filepath.Join(filepath.Dir(exe), "..", "lib", "ollama")
  29. case "darwin":
  30. libPath = filepath.Dir(exe)
  31. }
  32. cwd, err := os.Getwd()
  33. if err != nil {
  34. return ""
  35. }
  36. paths := []string{
  37. libPath,
  38. // build paths for development
  39. filepath.Join(filepath.Dir(exe), "build", "lib", "ollama"),
  40. filepath.Join(cwd, "build", "lib", "ollama"),
  41. }
  42. for _, p := range paths {
  43. if _, err := os.Stat(p); err == nil {
  44. return p
  45. }
  46. }
  47. return filepath.Dir(exe)
  48. }()