gen_windows.ps1 3.45 KB
Newer Older
1
2
3
4
5
#!powershell

$ErrorActionPreference = "Stop"

function init_vars {
6
    $script:llamacppDir = "../llama.cpp"
7
    $script:patches = @("0001-Expose-callable-API-for-server.patch")
8
    $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")
9
    $script:cmakeTargets = @("ggml", "ggml_static", "llama", "build_info", "common", "ext_server_shared", "llava_static")
10
    if ($env:CGO_CFLAGS -contains "-g") {
11
        $script:cmakeDefs += @("-DCMAKE_VERBOSE_MAKEFILE=on", "-DLLAMA_SERVER_VERBOSE=on")
Daniel Hiltgen's avatar
Daniel Hiltgen committed
12
        $script:config = "RelWithDebInfo"
13
    } else {
14
        $script:cmakeDefs += @("-DLLAMA_SERVER_VERBOSE=off")
Daniel Hiltgen's avatar
Daniel Hiltgen committed
15
        $script:config = "Release"
16
17
18
19
20
21
    }
}

function git_module_setup {
    # TODO add flags to skip the init/patch logic to make it easier to mod llama.cpp code in-repo
    & git submodule init
Daniel Hiltgen's avatar
Daniel Hiltgen committed
22
    if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
23
    & git submodule update --force "${script:llamacppDir}"
Daniel Hiltgen's avatar
Daniel Hiltgen committed
24
    if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
25
26
27
}

function apply_patches {
28
    # Wire up our CMakefile
29
30
    if (!(Select-String -Path "${script:llamacppDir}/examples/server/CMakeLists.txt" -Pattern 'ollama')) {
        Add-Content -Path "${script:llamacppDir}/examples/server/CMakeLists.txt" -Value 'include (../../../ext_server/CMakeLists.txt) # ollama'
31
    }
32
    # Avoid duplicate main symbols when we link into the cgo binary
33
    $content = Get-Content -Path "${script:llamacppDir}/examples/server/server.cpp"
34
    $content = $content -replace 'int main\(', 'int __main('
35
    Set-Content -Path "${script:llamacppDir}/examples/server/server.cpp" -Value $content
36
37
38
}

function build {
39
    write-host "generating config with: cmake -S ${script:llamacppDir} -B $script:buildDir $script:cmakeDefs"
40
    & cmake --version
41
    & cmake -S "${script:llamacppDir}" -B $script:buildDir $script:cmakeDefs
Daniel Hiltgen's avatar
Daniel Hiltgen committed
42
    if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
43
44
    write-host "building with: cmake --build $script:buildDir --config $script:config ($script:cmakeTargets | ForEach-Object { "--target", $_ })"
    & cmake --build $script:buildDir --config $script:config ($script:cmakeTargets | ForEach-Object { "--target", $_ })
Daniel Hiltgen's avatar
Daniel Hiltgen committed
45
    if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
46
47
}

48
49
50
51
52
function install {
    rm -ea 0 -recurse -force -path "${script:buildDir}/lib"
    md "${script:buildDir}/lib" -ea 0 > $null
    cp "${script:buildDir}/bin/${script:config}/ext_server_shared.dll" "${script:buildDir}/lib"
    cp "${script:buildDir}/bin/${script:config}/llama.dll" "${script:buildDir}/lib"
53
54
55

    # Display the dll dependencies in the build log
    dumpbin /dependents "${script:buildDir}/bin/${script:config}/ext_server_shared.dll" | select-string ".dll"
56
57
}

58
function cleanup {
59
    Set-Location "${script:llamacppDir}/examples/server"
60
    git checkout CMakeLists.txt server.cpp
61
62
63
64
65
}

init_vars
git_module_setup
apply_patches
Daniel Hiltgen's avatar
Daniel Hiltgen committed
66
67

# first build CPU based
68
$script:buildDir="${script:llamacppDir}/build/windows/cpu"
Daniel Hiltgen's avatar
Daniel Hiltgen committed
69
70

build
71
install
Daniel Hiltgen's avatar
Daniel Hiltgen committed
72
73
74

# Then build cuda as a dynamically loaded library
init_vars
75
$script:buildDir="${script:llamacppDir}/build/windows/cuda"
76
$script:cmakeDefs += @("-DLLAMA_CUBLAS=ON")
77
build
78
install
Daniel Hiltgen's avatar
Daniel Hiltgen committed
79

80
# TODO - actually implement ROCm support on windows
81
$script:buildDir="${script:llamacppDir}/build/windows/rocm"
82

83
84
85
rm -ea 0 -recurse -force -path "${script:buildDir}/lib"
md "${script:buildDir}/lib" -ea 0 > $null
echo $null >> "${script:buildDir}/lib/.generated"
86
87

cleanup
88
write-host "`ngo generate completed"