gen_common.sh 3.76 KB
Newer Older
John's avatar
John committed
1
# common logic across linux and darwin
2
3

init_vars() {
4
5
6
7
8
9
10
11
    case "${GOARCH}" in
    "amd64")
        ARCH="x86_64"
        ;;
    "arm64")
        ARCH="arm64"
        ;;
    *)
Daniel Hiltgen's avatar
Daniel Hiltgen committed
12
13
14
15
        echo "GOARCH must be set"
        echo "this script is meant to be run from within go generate"
        exit 1
        ;;
16
17
    esac

18
    LLAMACPP_DIR=../llama.cpp
Daniel Hiltgen's avatar
Daniel Hiltgen committed
19
    CMAKE_DEFS="-DCMAKE_SKIP_RPATH=on"
20
    CMAKE_TARGETS="--target ollama_llama_server"
21
    if echo "${CGO_CFLAGS}" | grep -- '-g' >/dev/null; then
22
        CMAKE_DEFS="-DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_VERBOSE_MAKEFILE=on -DLLAMA_GPROF=on -DLLAMA_SERVER_VERBOSE=on ${CMAKE_DEFS}"
23
24
    else
        # TODO - add additional optimization flags...
25
        CMAKE_DEFS="-DCMAKE_BUILD_TYPE=Release -DLLAMA_SERVER_VERBOSE=off ${CMAKE_DEFS}"
26
    fi
Roy Yang's avatar
Roy Yang committed
27
    case $(uname -s) in
28
29
30
31
32
    "Darwin")
        LIB_EXT="dylib"
        WHOLE_ARCHIVE="-Wl,-force_load"
        NO_WHOLE_ARCHIVE=""
        GCC_ARCH="-arch ${ARCH}"
Daniel Hiltgen's avatar
Daniel Hiltgen committed
33
        DIST_BASE=../../dist/darwin-${GOARCH}/
34
35
36
37
38
39
40
41
        ;;
    "Linux")
        LIB_EXT="so"
        WHOLE_ARCHIVE="-Wl,--whole-archive"
        NO_WHOLE_ARCHIVE="-Wl,--no-whole-archive"

        # Cross compiling not supported on linux - Use docker
        GCC_ARCH=""
Daniel Hiltgen's avatar
Daniel Hiltgen committed
42
        DIST_BASE=../../dist/linux-${GOARCH}/
43
44
45
46
        ;;
    *)
        ;;
    esac
47
    if [ -z "${CMAKE_CUDA_ARCHITECTURES}" ] ; then
48
49
        CMAKE_CUDA_ARCHITECTURES="50;52;61;70;75;80"
    fi
50
    GZIP=$(which pigz 2>/dev/null || echo "gzip")
51
52
53
}

git_module_setup() {
54
    if [ -n "${OLLAMA_SKIP_PATCHING}" ]; then
55
56
57
        echo "Skipping submodule initialization"
        return
    fi
Daniel Hiltgen's avatar
Daniel Hiltgen committed
58
59
60
61
62
    # Make sure the tree is clean after the directory moves
    if [ -d "${LLAMACPP_DIR}/gguf" ]; then
        echo "Cleaning up old submodule"
        rm -rf ${LLAMACPP_DIR}
    fi
63
    git submodule init
64
    git submodule update --force ${LLAMACPP_DIR}
65
66
67
68

}

apply_patches() {
69
    # Wire up our CMakefile
70
71
    if ! grep ollama ${LLAMACPP_DIR}/CMakeLists.txt; then
        echo 'add_subdirectory(../ext_server ext_server) # ollama' >>${LLAMACPP_DIR}/CMakeLists.txt
72
    fi
73

74
75
76
77
78
79
    if [ -n "$(ls -A ../patches/*.diff)" ]; then
        # apply temporary patches until fix is upstream
        for patch in ../patches/*.diff; do
            for file in $(grep "^+++ " ${patch} | cut -f2 -d' ' | cut -f2- -d/); do
                (cd ${LLAMACPP_DIR}; git checkout ${file})
            done
80
        done
81
82
83
84
        for patch in ../patches/*.diff; do
            (cd ${LLAMACPP_DIR} && git apply ${patch})
        done
    fi
85
86
87
}

build() {
88
89
    cmake -S ${LLAMACPP_DIR} -B ${BUILD_DIR} ${CMAKE_DEFS}
    cmake --build ${BUILD_DIR} ${CMAKE_TARGETS} -j8
90
}
91

92
compress() {
93
    echo "Compressing payloads to reduce overall binary size..."
94
95
    rm -rf ${BUILD_DIR}/bin/*.gz
    for f in ${BUILD_DIR}/bin/* ; do
96
97
        ${GZIP} -n --best -f ${f} &
        compress_pids+=" $!"
98
    done
99
100
101
    # check for lib directory
    if [ -d ${BUILD_DIR}/lib ]; then
        for f in ${BUILD_DIR}/lib/* ; do
102
103
            ${GZIP} -n --best -f ${f} &
            compress_pids+=" $!"
104
105
106
        done
    fi
    echo
107
108
109
110
}

wait_for_compress() {
    for pid in ${compress_pids}; do
111
112
113
        wait $pid
    done
    echo "Finished compression"
114
115
}

Daniel Hiltgen's avatar
Daniel Hiltgen committed
116
117
118
119
120
121
122
123
install() {
    echo "Installing libraries to bin dir ${BUILD_DIR}/bin/"
    for lib in $(find ${BUILD_DIR} -name \*.${LIB_EXT}); do
        rm -f "${BUILD_DIR}/bin/$(basename ${lib})"
        cp -af "${lib}" "${BUILD_DIR}/bin/"
    done
}

124
125
# Keep the local tree clean after we're done with the build
cleanup() {
126
    (cd ${LLAMACPP_DIR}/ && git checkout CMakeLists.txt)
127
128
129
130
131
132
133
134

    if [ -n "$(ls -A ../patches/*.diff)" ]; then
        for patch in ../patches/*.diff; do
            for file in $(grep "^+++ " ${patch} | cut -f2 -d' ' | cut -f2- -d/); do
                (cd ${LLAMACPP_DIR}; git checkout ${file})
            done
        done
    fi
135
}