gen_windows.ps1 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #!powershell
  2. $ErrorActionPreference = "Stop"
  3. function init_vars {
  4. $script:patches = @("0001-Expose-callable-API-for-server.patch")
  5. $script:cmakeDefs = @("-DBUILD_SHARED_LIBS=on", "-DLLAMA_NATIVE=off", "-DLLAMA_F16C=off", "-DLLAMA_FMA=off", "-DLLAMA_AVX512=off", "-DLLAMA_AVX2=off", "-DLLAMA_AVX=on", "-DLLAMA_K_QUANTS=on", "-DLLAMA_ACCELERATE=on", "-A","x64")
  6. $script:cmakeTargets = @("ggml", "ggml_static", "llama", "build_info", "common", "ext_server_shared", "llava_static")
  7. if ($env:CGO_CFLAGS -contains "-g") {
  8. $script:cmakeDefs += @("-DCMAKE_VERBOSE_MAKEFILE=on", "-DLLAMA_SERVER_VERBOSE=on")
  9. $script:config = "RelWithDebInfo"
  10. } else {
  11. $script:cmakeDefs += @("-DLLAMA_SERVER_VERBOSE=off")
  12. $script:config = "Release"
  13. }
  14. }
  15. function git_module_setup {
  16. # TODO add flags to skip the init/patch logic to make it easier to mod llama.cpp code in-repo
  17. & git submodule init
  18. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  19. & git submodule update --force gguf
  20. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  21. }
  22. function apply_patches {
  23. # Wire up our CMakefile
  24. if (!(Select-String -Path "gguf/examples/server/CMakeLists.txt" -Pattern 'ollama.txt')) {
  25. Add-Content -Path "gguf/examples/server/CMakeLists.txt" -Value 'include (../../../ollama.txt)'
  26. }
  27. # Avoid duplicate main symbols when we link into the cgo binary
  28. $content = Get-Content -Path "./gguf/examples/server/server.cpp"
  29. $content = $content -replace 'int main\(', 'int __main('
  30. Set-Content -Path "./gguf/examples/server/server.cpp" -Value $content
  31. }
  32. function build {
  33. write-host "generating config with: cmake -S gguf -B $script:buildDir $script:cmakeDefs"
  34. & cmake --version
  35. & cmake -S gguf -B $script:buildDir $script:cmakeDefs
  36. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  37. write-host "building with: cmake --build $script:buildDir --config $script:config ($script:cmakeTargets | ForEach-Object { "--target", $_ })"
  38. & cmake --build $script:buildDir --config $script:config ($script:cmakeTargets | ForEach-Object { "--target", $_ })
  39. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  40. }
  41. function cleanup {
  42. Set-Location "gguf/examples/server"
  43. git checkout CMakeLists.txt server.cpp
  44. }
  45. init_vars
  46. git_module_setup
  47. apply_patches
  48. # first build CPU based
  49. $script:buildDir="gguf/build/wincpu"
  50. build
  51. # install
  52. md gguf/build/lib -ea 0
  53. md gguf/build/wincpu/dist/lib -ea 0
  54. cp -force gguf/build/wincpu/bin/$script:config/ext_server_shared.dll gguf/build/lib/ext_server_shared.dll
  55. cp -force gguf/build/wincpu/bin/$script:config/llama.dll gguf/build/lib/llama.dll
  56. # Nope, this barfs on lots of symbol problems
  57. #mv gguf/build/wincpu/examples/server/$script:config/ext_server_shared.dll gguf/build/wincpu/dist/lib/cpu_server.lib
  58. # Nope: this needs lots of include paths to pull in things like msvcprt.lib and other deps
  59. # & cl.exe `
  60. # gguf/build/wincpu/examples/server/$script:config/ext_server.lib `
  61. # gguf/build/wincpu/common/$script:config/common.lib `
  62. # gguf/build/wincpu/$script:config/llama.lib `
  63. # gguf/build/wincpu/$script:config/ggml_static.lib `
  64. # /link /DLL /DEF:cpu_server.def /NOENTRY /MACHINE:X64 /OUT:gguf/build/wincpu/dist/lib/cpu_server.dll
  65. # if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  66. # Then build cuda as a dynamically loaded library
  67. init_vars
  68. $script:buildDir="gguf/build/wincuda"
  69. $script:cmakeDefs += @("-DLLAMA_CUBLAS=ON", "-DBUILD_SHARED_LIBS=on")
  70. build
  71. # install
  72. cp -force gguf/build/wincuda/bin/$script:config/ext_server_shared.dll gguf/build/lib/cuda_server.dll
  73. # TODO - more to do here to create a usable dll
  74. # TODO - implement ROCm support on windows
  75. md gguf/build/winrocm/lib -ea 0
  76. echo $null >> gguf/build/winrocm/lib/.generated
  77. cleanup
  78. write-host "go generate completed"