gen_common.sh 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. # Avoid duplicate main symbols when we link into the cgo binary
  59. sed -e 's/int main(/int __main(/g' <${LLAMACPP_DIR}/examples/server/server.cpp >${LLAMACPP_DIR}/examples/server/server.cpp.tmp &&
  60. mv ${LLAMACPP_DIR}/examples/server/server.cpp.tmp ${LLAMACPP_DIR}/examples/server/server.cpp
  61. }
  62. build() {
  63. cmake -S ${LLAMACPP_DIR} -B ${BUILD_DIR} ${CMAKE_DEFS}
  64. cmake --build ${BUILD_DIR} ${CMAKE_TARGETS} -j8
  65. mkdir -p ${BUILD_DIR}/lib/
  66. g++ -fPIC -g -shared -o ${BUILD_DIR}/lib/libext_server.${LIB_EXT} \
  67. ${GCC_ARCH} \
  68. ${WHOLE_ARCHIVE} ${BUILD_DIR}/examples/server/libext_server.a ${NO_WHOLE_ARCHIVE} \
  69. ${BUILD_DIR}/common/libcommon.a \
  70. ${BUILD_DIR}/libllama.a \
  71. -Wl,-rpath,\$ORIGIN \
  72. -lpthread -ldl -lm \
  73. ${EXTRA_LIBS}
  74. }
  75. compress_libs() {
  76. echo "Compressing payloads to reduce overall binary size..."
  77. pids=""
  78. for lib in ${BUILD_DIR}/lib/*.${LIB_EXT}* ; do
  79. bzip2 -v9 ${lib} &
  80. pids+=" $!"
  81. done
  82. echo
  83. for pid in ${pids}; do
  84. wait $pid
  85. done
  86. echo "Finished compression"
  87. }
  88. # Keep the local tree clean after we're done with the build
  89. cleanup() {
  90. (cd ${LLAMACPP_DIR}/examples/server/ && git checkout CMakeLists.txt server.cpp)
  91. }