gen_common.sh 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. # common logic across 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. if [ -z "${CMAKE_CUDA_ARCHITECTURES}" ] ; then
  40. CMAKE_CUDA_ARCHITECTURES="50;52;61;70;75;80"
  41. fi
  42. }
  43. git_module_setup() {
  44. if [ -n "${OLLAMA_SKIP_PATCHING}" ]; then
  45. echo "Skipping submodule initialization"
  46. return
  47. fi
  48. # Make sure the tree is clean after the directory moves
  49. if [ -d "${LLAMACPP_DIR}/gguf" ]; then
  50. echo "Cleaning up old submodule"
  51. rm -rf ${LLAMACPP_DIR}
  52. fi
  53. git submodule init
  54. git submodule update --force ${LLAMACPP_DIR}
  55. }
  56. apply_patches() {
  57. # Wire up our CMakefile
  58. if ! grep ollama ${LLAMACPP_DIR}/examples/server/CMakeLists.txt; then
  59. echo 'include (../../../ext_server/CMakeLists.txt) # ollama' >>${LLAMACPP_DIR}/examples/server/CMakeLists.txt
  60. fi
  61. if [ -n "$(ls -A ../patches/*.diff)" ]; then
  62. # apply temporary patches until fix is upstream
  63. for patch in ../patches/*.diff; do
  64. for file in $(grep "^+++ " ${patch} | cut -f2 -d' ' | cut -f2- -d/); do
  65. (cd ${LLAMACPP_DIR}; git checkout ${file})
  66. done
  67. done
  68. for patch in ../patches/*.diff; do
  69. (cd ${LLAMACPP_DIR} && git apply ${patch})
  70. done
  71. fi
  72. # Avoid duplicate main symbols when we link into the cgo binary
  73. sed -e 's/int main(/int __main(/g' <${LLAMACPP_DIR}/examples/server/server.cpp >${LLAMACPP_DIR}/examples/server/server.cpp.tmp &&
  74. mv ${LLAMACPP_DIR}/examples/server/server.cpp.tmp ${LLAMACPP_DIR}/examples/server/server.cpp
  75. }
  76. build() {
  77. cmake -S ${LLAMACPP_DIR} -B ${BUILD_DIR} ${CMAKE_DEFS}
  78. cmake --build ${BUILD_DIR} ${CMAKE_TARGETS} -j8
  79. mkdir -p ${BUILD_DIR}/lib/
  80. g++ -fPIC -g -shared -o ${BUILD_DIR}/lib/libext_server.${LIB_EXT} \
  81. ${GCC_ARCH} \
  82. ${WHOLE_ARCHIVE} ${BUILD_DIR}/examples/server/libext_server.a ${NO_WHOLE_ARCHIVE} \
  83. ${BUILD_DIR}/common/libcommon.a \
  84. ${BUILD_DIR}/libllama.a \
  85. -Wl,-rpath,\$ORIGIN \
  86. -lpthread -ldl -lm \
  87. ${EXTRA_LIBS}
  88. }
  89. compress_libs() {
  90. echo "Compressing payloads to reduce overall binary size..."
  91. pids=""
  92. rm -rf ${BUILD_DIR}/lib/*.${LIB_EXT}*.gz
  93. for lib in ${BUILD_DIR}/lib/*.${LIB_EXT}* ; do
  94. gzip -n --best -f ${lib} &
  95. pids+=" $!"
  96. done
  97. echo
  98. for pid in ${pids}; do
  99. wait $pid
  100. done
  101. echo "Finished compression"
  102. }
  103. # Keep the local tree clean after we're done with the build
  104. cleanup() {
  105. (cd ${LLAMACPP_DIR}/examples/server/ && git checkout CMakeLists.txt server.cpp)
  106. if [ -n "$(ls -A ../patches/*.diff)" ]; then
  107. for patch in ../patches/*.diff; do
  108. for file in $(grep "^+++ " ${patch} | cut -f2 -d' ' | cut -f2- -d/); do
  109. (cd ${LLAMACPP_DIR}; git checkout ${file})
  110. done
  111. done
  112. fi
  113. }