gen_common.sh 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. # common logic accross linux and darwin
  2. init_vars() {
  3. case "${GOARCH}" in
  4. "amd64")
  5. ARCH="x86_64"
  6. ;;
  7. "arm64")
  8. ARCH="arm64"
  9. ;;
  10. *)
  11. ARCH=$(uname -m | sed -e "s/aarch64/arm64/g")
  12. esac
  13. LLAMACPP_DIR=../llama.cpp
  14. CMAKE_DEFS=""
  15. CMAKE_TARGETS="--target ext_server"
  16. if echo "${CGO_CFLAGS}" | grep -- '-g' >/dev/null; then
  17. CMAKE_DEFS="-DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_VERBOSE_MAKEFILE=on -DLLAMA_GPROF=on -DLLAMA_SERVER_VERBOSE=on ${CMAKE_DEFS}"
  18. else
  19. # TODO - add additional optimization flags...
  20. CMAKE_DEFS="-DCMAKE_BUILD_TYPE=Release -DLLAMA_SERVER_VERBOSE=off ${CMAKE_DEFS}"
  21. fi
  22. case $(uname -s) in
  23. "Darwin")
  24. LIB_EXT="dylib"
  25. WHOLE_ARCHIVE="-Wl,-force_load"
  26. NO_WHOLE_ARCHIVE=""
  27. GCC_ARCH="-arch ${ARCH}"
  28. ;;
  29. "Linux")
  30. LIB_EXT="so"
  31. WHOLE_ARCHIVE="-Wl,--whole-archive"
  32. NO_WHOLE_ARCHIVE="-Wl,--no-whole-archive"
  33. # Cross compiling not supported on linux - Use docker
  34. GCC_ARCH=""
  35. ;;
  36. *)
  37. ;;
  38. esac
  39. }
  40. git_module_setup() {
  41. if [ -n "${OLLAMA_SKIP_PATCHING}" ]; then
  42. echo "Skipping submodule initialization"
  43. return
  44. fi
  45. # Make sure the tree is clean after the directory moves
  46. if [ -d "${LLAMACPP_DIR}/gguf" ]; then
  47. echo "Cleaning up old submodule"
  48. rm -rf ${LLAMACPP_DIR}
  49. fi
  50. git submodule init
  51. git submodule update --force ${LLAMACPP_DIR}
  52. }
  53. apply_patches() {
  54. # Wire up our CMakefile
  55. if ! grep ollama ${LLAMACPP_DIR}/examples/server/CMakeLists.txt; then
  56. echo 'include (../../../ext_server/CMakeLists.txt) # ollama' >>${LLAMACPP_DIR}/examples/server/CMakeLists.txt
  57. fi
  58. # apply temporary patches until fix is upstream
  59. for patch in ../patches/*.diff; do
  60. for file in $(grep "^+++ " ${patch} | cut -f2 -d' ' | cut -f2- -d/); do
  61. (cd ${LLAMACPP_DIR}; git checkout ${file})
  62. done
  63. done
  64. for patch in ../patches/*.diff; do
  65. (cd ${LLAMACPP_DIR} && git apply ${patch})
  66. done
  67. # Avoid duplicate main symbols when we link into the cgo binary
  68. sed -e 's/int main(/int __main(/g' <${LLAMACPP_DIR}/examples/server/server.cpp >${LLAMACPP_DIR}/examples/server/server.cpp.tmp &&
  69. mv ${LLAMACPP_DIR}/examples/server/server.cpp.tmp ${LLAMACPP_DIR}/examples/server/server.cpp
  70. }
  71. build() {
  72. cmake -S ${LLAMACPP_DIR} -B ${BUILD_DIR} ${CMAKE_DEFS}
  73. cmake --build ${BUILD_DIR} ${CMAKE_TARGETS} -j8
  74. mkdir -p ${BUILD_DIR}/lib/
  75. g++ -fPIC -g -shared -o ${BUILD_DIR}/lib/libext_server.${LIB_EXT} \
  76. ${GCC_ARCH} \
  77. ${WHOLE_ARCHIVE} ${BUILD_DIR}/examples/server/libext_server.a ${NO_WHOLE_ARCHIVE} \
  78. ${BUILD_DIR}/common/libcommon.a \
  79. ${BUILD_DIR}/libllama.a \
  80. -Wl,-rpath,\$ORIGIN \
  81. -lpthread -ldl -lm \
  82. ${EXTRA_LIBS}
  83. }
  84. compress_libs() {
  85. echo "Compressing payloads to reduce overall binary size..."
  86. pids=""
  87. rm -rf ${BUILD_DIR}/lib/*.${LIB_EXT}*.gz
  88. for lib in ${BUILD_DIR}/lib/*.${LIB_EXT}* ; do
  89. gzip --best -f ${lib} &
  90. pids+=" $!"
  91. done
  92. echo
  93. for pid in ${pids}; do
  94. wait $pid
  95. done
  96. echo "Finished compression"
  97. }
  98. # Keep the local tree clean after we're done with the build
  99. cleanup() {
  100. (cd ${LLAMACPP_DIR}/examples/server/ && git checkout CMakeLists.txt server.cpp)
  101. }