gen_windows.ps1 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. if ($env:CGO_CFLAGS -contains "-g") {
  7. $script:cmakeDefs += @("-DCMAKE_VERBOSE_MAKEFILE=on", "-DLLAMA_SERVER_VERBOSE=on")
  8. $script:config = "RelWithDebInfo"
  9. } else {
  10. $script:cmakeDefs += @("-DLLAMA_SERVER_VERBOSE=off")
  11. $script:config = "Release"
  12. }
  13. }
  14. function git_module_setup {
  15. # TODO add flags to skip the init/patch logic to make it easier to mod llama.cpp code in-repo
  16. & git submodule init
  17. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  18. & git submodule update --force gguf
  19. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  20. }
  21. function apply_patches {
  22. rm -erroraction ignore -path "gguf/examples/server/server.h"
  23. foreach ($patch in $script:patches) {
  24. write-host "Applying patch $patch"
  25. & git -C gguf apply ../patches/$patch
  26. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  27. }
  28. }
  29. function build {
  30. write-host "generating config with: cmake -S gguf -B $script:buildDir $script:cmakeDefs"
  31. & cmake --version
  32. & cmake -S gguf -B $script:buildDir $script:cmakeDefs
  33. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  34. write-host "building with: cmake --build $script:buildDir --config $script:config"
  35. & cmake --build $script:buildDir --config $script:config
  36. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  37. }
  38. function install {
  39. rm -erroraction ignore -recurse -force -path $script:installDir
  40. & cmake --install $script:buildDir --prefix $script:installDir --config $script:config
  41. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  42. }
  43. init_vars
  44. git_module_setup
  45. apply_patches
  46. # first build CPU based
  47. $script:buildDir="gguf/build/wincpu"
  48. $script:installDir="gguf/build/wincpu/dist"
  49. build
  50. # install
  51. md gguf/build/lib -ea 0
  52. md gguf/build/wincpu/dist/lib -ea 0
  53. mv gguf/build/wincpu/bin/$script:config/ext_server_shared.dll gguf/build/wincpu/dist/lib/cpu_server.dll
  54. # Nope, this barfs on lots of symbol problems
  55. #mv gguf/build/wincpu/examples/server/$script:config/ext_server_shared.dll gguf/build/wincpu/dist/lib/cpu_server.lib
  56. # Nope: this needs lots of include paths to pull in things like msvcprt.lib and other deps
  57. # & cl.exe `
  58. # gguf/build/wincpu/examples/server/$script:config/ext_server.lib `
  59. # gguf/build/wincpu/common/$script:config/common.lib `
  60. # gguf/build/wincpu/$script:config/llama.lib `
  61. # gguf/build/wincpu/$script:config/ggml_static.lib `
  62. # /link /DLL /DEF:cpu_server.def /NOENTRY /MACHINE:X64 /OUT:gguf/build/wincpu/dist/lib/cpu_server.dll
  63. # if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  64. # Then build cuda as a dynamically loaded library
  65. init_vars
  66. $script:buildDir="gguf/build/wincuda"
  67. $script:installDir="gguf/build/wincuda/dist"
  68. $script:cmakeDefs += @("-DLLAMA_CUBLAS=ON", "-DBUILD_SHARED_LIBS=on")
  69. build
  70. install
  71. cp gguf/build/wincuda/dist/bin/ext_server_shared.dll gguf/build/lib/cuda_server.dll
  72. # TODO - more to do here to create a usable dll
  73. # TODO - implement ROCm support on windows
  74. md gguf/build/winrocm/lib -ea 0
  75. echo $null >> gguf/build/winrocm/lib/.generated