gen_common.sh 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 ollama_llama_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}/CMakeLists.txt; then
  59. echo 'add_subdirectory(../ext_server ext_server) # ollama' >>${LLAMACPP_DIR}/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. }
  73. build() {
  74. cmake -S ${LLAMACPP_DIR} -B ${BUILD_DIR} ${CMAKE_DEFS}
  75. cmake --build ${BUILD_DIR} ${CMAKE_TARGETS} -j8
  76. }
  77. compress() {
  78. echo "Compressing payloads to reduce overall binary size..."
  79. pids=""
  80. rm -rf ${BUILD_DIR}/bin/*.gz
  81. for f in ${BUILD_DIR}/bin/* ; do
  82. gzip -n --best -f ${f} &
  83. pids+=" $!"
  84. done
  85. # check for lib directory
  86. if [ -d ${BUILD_DIR}/lib ]; then
  87. for f in ${BUILD_DIR}/lib/* ; do
  88. gzip -n --best -f ${f} &
  89. pids+=" $!"
  90. done
  91. fi
  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}/ && git checkout CMakeLists.txt)
  101. if [ -n "$(ls -A ../patches/*.diff)" ]; then
  102. for patch in ../patches/*.diff; do
  103. for file in $(grep "^+++ " ${patch} | cut -f2 -d' ' | cut -f2- -d/); do
  104. (cd ${LLAMACPP_DIR}; git checkout ${file})
  105. done
  106. done
  107. fi
  108. }