gen_common.sh 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. echo "GOARCH must be set"
  12. echo "this script is meant to be run from within go generate"
  13. exit 1
  14. ;;
  15. esac
  16. LLAMACPP_DIR=../llama.cpp
  17. CMAKE_DEFS="-DCMAKE_SKIP_RPATH=on"
  18. CMAKE_TARGETS="--target ollama_llama_server"
  19. if echo "${CGO_CFLAGS}" | grep -- '-g' >/dev/null; then
  20. CMAKE_DEFS="-DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_VERBOSE_MAKEFILE=on -DLLAMA_GPROF=on -DLLAMA_SERVER_VERBOSE=on ${CMAKE_DEFS}"
  21. else
  22. # TODO - add additional optimization flags...
  23. CMAKE_DEFS="-DCMAKE_BUILD_TYPE=Release -DLLAMA_SERVER_VERBOSE=off ${CMAKE_DEFS}"
  24. fi
  25. case $(uname -s) in
  26. "Darwin")
  27. LIB_EXT="dylib"
  28. WHOLE_ARCHIVE="-Wl,-force_load"
  29. NO_WHOLE_ARCHIVE=""
  30. GCC_ARCH="-arch ${ARCH}"
  31. DIST_BASE=../../dist/darwin-${GOARCH}/
  32. ;;
  33. "Linux")
  34. LIB_EXT="so"
  35. WHOLE_ARCHIVE="-Wl,--whole-archive"
  36. NO_WHOLE_ARCHIVE="-Wl,--no-whole-archive"
  37. # Cross compiling not supported on linux - Use docker
  38. GCC_ARCH=""
  39. DIST_BASE=../../dist/linux-${GOARCH}/
  40. ;;
  41. *)
  42. ;;
  43. esac
  44. if [ -z "${CMAKE_CUDA_ARCHITECTURES}" ] ; then
  45. CMAKE_CUDA_ARCHITECTURES="50;52;61;70;75;80"
  46. fi
  47. GZIP=$(which pigz 2>/dev/null || echo "gzip")
  48. }
  49. git_module_setup() {
  50. if [ -n "${OLLAMA_SKIP_PATCHING}" ]; then
  51. echo "Skipping submodule initialization"
  52. return
  53. fi
  54. # Make sure the tree is clean after the directory moves
  55. if [ -d "${LLAMACPP_DIR}/gguf" ]; then
  56. echo "Cleaning up old submodule"
  57. rm -rf ${LLAMACPP_DIR}
  58. fi
  59. git submodule init
  60. git submodule update --force ${LLAMACPP_DIR}
  61. }
  62. apply_patches() {
  63. # Wire up our CMakefile
  64. if ! grep ollama ${LLAMACPP_DIR}/CMakeLists.txt; then
  65. echo 'add_subdirectory(../ext_server ext_server) # ollama' >>${LLAMACPP_DIR}/CMakeLists.txt
  66. fi
  67. if [ -n "$(ls -A ../patches/*.diff)" ]; then
  68. # apply temporary patches until fix is upstream
  69. for patch in ../patches/*.diff; do
  70. for file in $(grep "^+++ " ${patch} | cut -f2 -d' ' | cut -f2- -d/); do
  71. (cd ${LLAMACPP_DIR}; git checkout ${file})
  72. done
  73. done
  74. for patch in ../patches/*.diff; do
  75. (cd ${LLAMACPP_DIR} && git apply ${patch})
  76. done
  77. fi
  78. }
  79. build() {
  80. cmake -S ${LLAMACPP_DIR} -B ${BUILD_DIR} ${CMAKE_DEFS}
  81. cmake --build ${BUILD_DIR} ${CMAKE_TARGETS} -j8
  82. }
  83. compress() {
  84. echo "Compressing payloads to reduce overall binary size..."
  85. rm -rf ${BUILD_DIR}/bin/*.gz
  86. for f in ${BUILD_DIR}/bin/* ; do
  87. ${GZIP} -n --best -f ${f} &
  88. compress_pids+=" $!"
  89. done
  90. # check for lib directory
  91. if [ -d ${BUILD_DIR}/lib ]; then
  92. for f in ${BUILD_DIR}/lib/* ; do
  93. ${GZIP} -n --best -f ${f} &
  94. compress_pids+=" $!"
  95. done
  96. fi
  97. echo
  98. }
  99. wait_for_compress() {
  100. for pid in ${compress_pids}; do
  101. wait $pid
  102. done
  103. echo "Finished compression"
  104. }
  105. install() {
  106. echo "Installing libraries to bin dir ${BUILD_DIR}/bin/"
  107. for lib in $(find ${BUILD_DIR} -name \*.${LIB_EXT}); do
  108. rm -f "${BUILD_DIR}/bin/$(basename ${lib})"
  109. cp -af "${lib}" "${BUILD_DIR}/bin/"
  110. done
  111. }
  112. # Keep the local tree clean after we're done with the build
  113. cleanup() {
  114. (cd ${LLAMACPP_DIR}/ && git checkout CMakeLists.txt)
  115. if [ -n "$(ls -A ../patches/*.diff)" ]; then
  116. for patch in ../patches/*.diff; do
  117. for file in $(grep "^+++ " ${patch} | cut -f2 -d' ' | cut -f2- -d/); do
  118. (cd ${LLAMACPP_DIR}; git checkout ${file})
  119. done
  120. done
  121. fi
  122. }