gen_windows.ps1 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #!powershell
  2. $ErrorActionPreference = "Stop"
  3. function init_vars {
  4. $script:llamacppDir = "../llama.cpp"
  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", "-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 "${script:llamacppDir}"
  20. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  21. }
  22. function apply_patches {
  23. # Wire up our CMakefile
  24. if (!(Select-String -Path "${script:llamacppDir}/examples/server/CMakeLists.txt" -Pattern 'ollama')) {
  25. Add-Content -Path "${script:llamacppDir}/examples/server/CMakeLists.txt" -Value 'include (../../../ext_server/CMakeLists.txt) # ollama'
  26. }
  27. # Avoid duplicate main symbols when we link into the cgo binary
  28. $content = Get-Content -Path "${script:llamacppDir}/examples/server/server.cpp"
  29. $content = $content -replace 'int main\(', 'int __main('
  30. Set-Content -Path "${script:llamacppDir}/examples/server/server.cpp" -Value $content
  31. }
  32. function build {
  33. write-host "generating config with: cmake -S ${script:llamacppDir} -B $script:buildDir $script:cmakeDefs"
  34. & cmake --version
  35. & cmake -S "${script:llamacppDir}" -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 install {
  42. rm -ea 0 -recurse -force -path "${script:buildDir}/lib"
  43. md "${script:buildDir}/lib" -ea 0 > $null
  44. cp "${script:buildDir}/bin/${script:config}/ext_server_shared.dll" "${script:buildDir}/lib"
  45. cp "${script:buildDir}/bin/${script:config}/llama.dll" "${script:buildDir}/lib"
  46. # Display the dll dependencies in the build log
  47. dumpbin /dependents "${script:buildDir}/bin/${script:config}/ext_server_shared.dll" | select-string ".dll"
  48. }
  49. function cleanup {
  50. Set-Location "${script:llamacppDir}/examples/server"
  51. git checkout CMakeLists.txt server.cpp
  52. }
  53. init_vars
  54. git_module_setup
  55. apply_patches
  56. # first build CPU based
  57. $script:buildDir="${script:llamacppDir}/build/windows/cpu"
  58. build
  59. install
  60. # Then build cuda as a dynamically loaded library
  61. init_vars
  62. $script:buildDir="${script:llamacppDir}/build/windows/cuda"
  63. $script:cmakeDefs += @("-DLLAMA_CUBLAS=ON")
  64. build
  65. install
  66. # TODO - actually implement ROCm support on windows
  67. $script:buildDir="${script:llamacppDir}/build/windows/rocm"
  68. rm -ea 0 -recurse -force -path "${script:buildDir}/lib"
  69. md "${script:buildDir}/lib" -ea 0 > $null
  70. echo $null >> "${script:buildDir}/lib/.generated"
  71. cleanup
  72. write-host "`ngo generate completed"