gen_common.sh 1.94 KB
Newer Older
1
2
3
# common logic accross linux and darwin

init_vars() {
4
    LLAMACPP_DIR=../llama.cpp
5
    PATCHES="0001-Expose-callable-API-for-server.patch"
6
    CMAKE_DEFS=""
7
    CMAKE_TARGETS="--target ggml --target ggml_static --target llama --target build_info --target common --target ext_server --target llava_static"
8
    if echo "${CGO_CFLAGS}" | grep -- '-g' >/dev/null; then
9
        CMAKE_DEFS="-DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_VERBOSE_MAKEFILE=on -DLLAMA_GPROF=on -DLLAMA_SERVER_VERBOSE=on"
10
11
    else
        # TODO - add additional optimization flags...
12
        CMAKE_DEFS="-DCMAKE_BUILD_TYPE=Release -DLLAMA_SERVER_VERBOSE=off"
13
14
15
16
    fi
}

git_module_setup() {
17
    if [ -n "${OLLAMA_SKIP_PATCHING}" ]; then
18
19
20
        echo "Skipping submodule initialization"
        return
    fi
21
    git submodule init
22
    git submodule update --force ${LLAMACPP_DIR}
23
24
25
26

}

apply_patches() {
27
    # Wire up our CMakefile
28
29
    if ! grep ollama ${LLAMACPP_DIR}/examples/server/CMakeLists.txt; then
        echo 'include (../../../ext_server/CMakeLists.txt) # ollama' >>${LLAMACPP_DIR}/examples/server/CMakeLists.txt
30
    fi
31
    # Avoid duplicate main symbols when we link into the cgo binary
32
33
    sed -e 's/int main(/int __main(/g' <${LLAMACPP_DIR}/examples/server/server.cpp >${LLAMACPP_DIR}/examples/server/server.cpp.tmp &&
        mv ${LLAMACPP_DIR}/examples/server/server.cpp.tmp ${LLAMACPP_DIR}/examples/server/server.cpp
34
35
36
}

build() {
37
38
    cmake -S ${LLAMACPP_DIR} -B ${BUILD_DIR} ${CMAKE_DEFS}
    cmake --build ${BUILD_DIR} ${CMAKE_TARGETS} -j8
39
}
40

41
42
43
44
45
46
47
48
49
install() {
    rm -rf ${BUILD_DIR}/lib
    mkdir -p ${BUILD_DIR}/lib
    cp ${BUILD_DIR}/examples/server/libext_server.a ${BUILD_DIR}/lib
    cp ${BUILD_DIR}/common/libcommon.a ${BUILD_DIR}/lib
    cp ${BUILD_DIR}/libllama.a ${BUILD_DIR}/lib
    cp ${BUILD_DIR}/libggml_static.a ${BUILD_DIR}/lib
}

50
51
# Keep the local tree clean after we're done with the build
cleanup() {
52
    (cd ${LLAMACPP_DIR}/examples/server/ && git checkout CMakeLists.txt server.cpp)
53
}