path.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. if eval, err := filepath.EvalSymlinks(exe); err == nil {
  20. exe = eval
  21. }
  22. var libPath string
  23. switch runtime.GOOS {
  24. case "windows":
  25. libPath = filepath.Join(filepath.Dir(exe), "lib", "ollama")
  26. case "linux":
  27. libPath = filepath.Join(filepath.Dir(exe), "..", "lib", "ollama")
  28. case "darwin":
  29. libPath = filepath.Dir(exe)
  30. }
  31. cwd, err := os.Getwd()
  32. if err != nil {
  33. return ""
  34. }
  35. paths := []string{
  36. libPath,
  37. // build paths for development
  38. filepath.Join(filepath.Dir(exe), "build", "lib", "ollama"),
  39. filepath.Join(cwd, "build", "lib", "ollama"),
  40. }
  41. for _, p := range paths {
  42. if _, err := os.Stat(p); err == nil {
  43. return p
  44. }
  45. }
  46. return filepath.Dir(exe)
  47. }()