build.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. //go:build ignore
  2. package main
  3. import (
  4. "cmp"
  5. "errors"
  6. "flag"
  7. "log"
  8. "os"
  9. "os/exec"
  10. "path/filepath"
  11. "runtime"
  12. )
  13. // Flags
  14. var (
  15. flagForce = flag.Bool("f", false, "force re-generation of dependencies")
  16. flagSkipBuild = flag.Bool("d", false, "generate dependencies only (e.g. skip 'go build .')")
  17. // Flags to set GOARCH and GOOS explicitly for cross-platform builds,
  18. // e.g., in CI to target a different platform than the build matrix
  19. // default. These allows us to run generate without a separate build
  20. // step for building the script binary for the host ARCH and then
  21. // runing the generate script for the target ARCH. Instead, we can
  22. // just run `go run build.go -target=$GOARCH` to generate the
  23. // deps.
  24. flagGOARCH = flag.String("target", "", "sets GOARCH to use when generating dependencies and building")
  25. )
  26. func buildEnv() []string {
  27. return append(os.Environ(),
  28. "GOARCH="+cmp.Or(*flagGOARCH, runtime.GOARCH),
  29. )
  30. }
  31. func main() {
  32. log.SetFlags(0)
  33. flag.Usage = func() {
  34. log.Printf("Usage: go run build.go [flags]")
  35. log.Println()
  36. log.Println("Flags:")
  37. flag.PrintDefaults()
  38. log.Println()
  39. log.Println("This script builds the Ollama server binary and generates the llama.cpp")
  40. log.Println("bindings for the current platform. It assumes that the current working")
  41. log.Println("directory is the root directory of the Ollama project.")
  42. log.Println()
  43. log.Println("If the -d flag is provided, the script will only generate the dependencies")
  44. log.Println("and skip building the Ollama server binary.")
  45. log.Println()
  46. log.Println("If the -f flag is provided, the script will force re-generation of the")
  47. log.Println("dependencies.")
  48. log.Println()
  49. log.Println("If the -target flag is provided, the script will set GOARCH to the value")
  50. log.Println("of the flag. This is useful for cross-platform builds.")
  51. log.Println()
  52. log.Println("The script will check for the required dependencies (cmake, gcc) and")
  53. log.Println("print their version.")
  54. log.Println()
  55. log.Println("The script will also check if it is being run from the root directory of")
  56. log.Println("the Ollama project.")
  57. log.Println()
  58. os.Exit(1)
  59. }
  60. flag.Parse()
  61. log.Printf("=== Building Ollama ===")
  62. defer func() {
  63. log.Printf("=== Done building Ollama ===")
  64. log.Println()
  65. log.Println("To run the Ollama server, use:")
  66. log.Println()
  67. log.Println(" ./ollama serve")
  68. log.Println()
  69. }()
  70. if flag.NArg() > 0 {
  71. flag.Usage()
  72. }
  73. if !inRootDir() {
  74. log.Fatalf("Please run this script from the root directory of the Ollama project.")
  75. }
  76. if err := checkDependencies(); err != nil {
  77. log.Fatalf("Failed dependency check: %v", err)
  78. }
  79. if err := buildLlammaCPP(); err != nil {
  80. log.Fatalf("Failed to build llama.cpp: %v", err)
  81. }
  82. if err := goBuildOllama(); err != nil {
  83. log.Fatalf("Failed to build ollama Go binary: %v", err)
  84. }
  85. }
  86. // checkDependencies does a quick check to see if the required dependencies are
  87. // installed on the system and functioning enough to print their version.
  88. //
  89. // TODO(bmizerany): Check the actual version of the dependencies? Seems a
  90. // little daunting given diff versions might print diff things. This should
  91. // be good enough for now.
  92. func checkDependencies() error {
  93. var err error
  94. check := func(name string, args ...string) {
  95. log.Printf("=== Checking for %s ===", name)
  96. defer log.Printf("=== Done checking for %s ===\n\n", name)
  97. cmd := exec.Command(name, args...)
  98. cmd.Stdout = os.Stdout
  99. cmd.Stderr = os.Stderr
  100. err = errors.Join(err, cmd.Run())
  101. }
  102. check("cmake", "--version")
  103. check("gcc", "--version")
  104. return err
  105. }
  106. func goBuildOllama() error {
  107. log.Println("=== Building Ollama binary ===")
  108. defer log.Printf("=== Done building Ollama binary ===\n\n")
  109. if *flagSkipBuild {
  110. log.Println("Skipping 'go build -o ollama .'")
  111. return nil
  112. }
  113. cmd := exec.Command("go", "build", "-o", "ollama", ".")
  114. cmd.Stdout = os.Stdout
  115. cmd.Stderr = os.Stderr
  116. cmd.Env = buildEnv()
  117. return cmd.Run()
  118. }
  119. // buildLlammaCPP generates the llama.cpp bindings for the current platform.
  120. //
  121. // It assumes that the current working directory is the root directory of the
  122. // Ollama project.
  123. func buildLlammaCPP() error {
  124. log.Println("=== Generating dependencies ===")
  125. defer log.Printf("=== Done generating dependencies ===\n\n")
  126. if *flagForce {
  127. if err := os.RemoveAll(filepath.Join("llm", "build")); err != nil {
  128. return err
  129. }
  130. }
  131. if isDirectory(filepath.Join("llm", "build")) {
  132. log.Println("llm/build already exists; skipping. Use -f to force re-generate.")
  133. return nil
  134. }
  135. scriptDir, err := filepath.Abs(filepath.Join("llm", "generate"))
  136. if err != nil {
  137. return err
  138. }
  139. var cmd *exec.Cmd
  140. switch runtime.GOOS {
  141. case "windows":
  142. script := filepath.Join(scriptDir, "gen_windows.ps1")
  143. cmd = exec.Command("powershell", "-ExecutionPolicy", "Bypass", "-File", script)
  144. case "linux":
  145. script := filepath.Join(scriptDir, "gen_linux.sh")
  146. cmd = exec.Command("bash", script)
  147. case "darwin":
  148. script := filepath.Join(scriptDir, "gen_darwin.sh")
  149. cmd = exec.Command("bash", script)
  150. default:
  151. log.Fatalf("Unsupported OS: %s", runtime.GOOS)
  152. }
  153. cmd.Dir = filepath.Join("llm", "generate")
  154. cmd.Stdout = os.Stdout
  155. cmd.Stderr = os.Stderr
  156. cmd.Env = buildEnv()
  157. log.Printf("Running GOOS=%s GOARCH=%s %s", runtime.GOOS, runtime.GOARCH, cmd.Args)
  158. return cmd.Run()
  159. }
  160. func isDirectory(path string) bool {
  161. info, err := os.Stat(path)
  162. if err != nil {
  163. return false
  164. }
  165. return info.IsDir()
  166. }
  167. // inRootDir returns true if the current working directory is the root
  168. // directory of the Ollama project. It looks for a file named "go.mod".
  169. func inRootDir() bool {
  170. _, err := os.Stat("go.mod")
  171. return err == nil
  172. }