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

init_vars() {
4
5
6
7
8
9
10
11
12
13
14
    case "${GOARCH}" in
    "amd64")
        ARCH="x86_64"
        ;;
    "arm64")
        ARCH="arm64"
        ;;
    *)
        ARCH=$(uname -m | sed -e "s/aarch64/arm64/g")
    esac

15
    LLAMACPP_DIR=../llama.cpp
16
    CMAKE_DEFS=""
17
    CMAKE_TARGETS="--target ext_server"
18
    if echo "${CGO_CFLAGS}" | grep -- '-g' >/dev/null; then
19
        CMAKE_DEFS="-DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_VERBOSE_MAKEFILE=on -DLLAMA_GPROF=on -DLLAMA_SERVER_VERBOSE=on ${CMAKE_DEFS}"
20
21
    else
        # TODO - add additional optimization flags...
22
        CMAKE_DEFS="-DCMAKE_BUILD_TYPE=Release -DLLAMA_SERVER_VERBOSE=off ${CMAKE_DEFS}"
23
    fi
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
    case $(uname -s) in 
    "Darwin")
        LIB_EXT="dylib"
        WHOLE_ARCHIVE="-Wl,-force_load"
        NO_WHOLE_ARCHIVE=""
        GCC_ARCH="-arch ${ARCH}"
        ;;
    "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=""
        ;;
    *)
        ;;
    esac
42
43
44
}

git_module_setup() {
45
    if [ -n "${OLLAMA_SKIP_PATCHING}" ]; then
46
47
48
        echo "Skipping submodule initialization"
        return
    fi
Daniel Hiltgen's avatar
Daniel Hiltgen committed
49
50
51
52
53
    # 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
54
    git submodule init
55
    git submodule update --force ${LLAMACPP_DIR}
56
57
58
59

}

apply_patches() {
60
    # Wire up our CMakefile
61
62
    if ! grep ollama ${LLAMACPP_DIR}/examples/server/CMakeLists.txt; then
        echo 'include (../../../ext_server/CMakeLists.txt) # ollama' >>${LLAMACPP_DIR}/examples/server/CMakeLists.txt
63
    fi
64
    # Avoid duplicate main symbols when we link into the cgo binary
65
66
    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
67
68
69
}

build() {
70
71
    cmake -S ${LLAMACPP_DIR} -B ${BUILD_DIR} ${CMAKE_DEFS}
    cmake --build ${BUILD_DIR} ${CMAKE_TARGETS} -j8
72
73
74
75
76
77
78
79
80
    mkdir -p ${BUILD_DIR}/lib/
    g++ -fPIC -g -shared -o ${BUILD_DIR}/lib/libext_server.${LIB_EXT} \
        ${GCC_ARCH} \
        ${WHOLE_ARCHIVE} ${BUILD_DIR}/examples/server/libext_server.a ${NO_WHOLE_ARCHIVE} \
        ${BUILD_DIR}/common/libcommon.a \
        ${BUILD_DIR}/libllama.a \
        -Wl,-rpath,\$ORIGIN \
        -lpthread -ldl -lm \
        ${EXTRA_LIBS}
81
}
82

83
84
85
86
87
88
89
90
91
92
93
94
compress_libs() {
    echo "Compressing payloads to reduce overall binary size..."
    pids=""
    for lib in ${BUILD_DIR}/lib/*.${LIB_EXT}* ; do
        bzip2 -v9 ${lib} &
        pids+=" $!"
    done
    echo 
    for pid in ${pids}; do
        wait $pid
    done
    echo "Finished compression"
95
96
}

97
98
# Keep the local tree clean after we're done with the build
cleanup() {
99
    (cd ${LLAMACPP_DIR}/examples/server/ && git checkout CMakeLists.txt server.cpp)
100
}