gen_common.sh 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. }
  48. git_module_setup() {
  49. if [ -n "${OLLAMA_SKIP_PATCHING}" ]; then
  50. echo "Skipping submodule initialization"
  51. return
  52. fi
  53. # Make sure the tree is clean after the directory moves
  54. if [ -d "${LLAMACPP_DIR}/gguf" ]; then
  55. echo "Cleaning up old submodule"
  56. rm -rf ${LLAMACPP_DIR}
  57. fi
  58. git submodule init
  59. git submodule update --force ${LLAMACPP_DIR}
  60. }
  61. apply_patches() {
  62. # Wire up our CMakefile
  63. if ! grep ollama ${LLAMACPP_DIR}/CMakeLists.txt; then
  64. echo 'add_subdirectory(../ext_server ext_server) # ollama' >>${LLAMACPP_DIR}/CMakeLists.txt
  65. fi
  66. if [ -n "$(ls -A ../patches/*.diff)" ]; then
  67. # apply temporary patches until fix is upstream
  68. for patch in ../patches/*.diff; do
  69. for file in $(grep "^+++ " ${patch} | cut -f2 -d' ' | cut -f2- -d/); do
  70. (cd ${LLAMACPP_DIR}; git checkout ${file})
  71. done
  72. done
  73. for patch in ../patches/*.diff; do
  74. (cd ${LLAMACPP_DIR} && git apply ${patch})
  75. done
  76. fi
  77. }
  78. build() {
  79. cmake -S ${LLAMACPP_DIR} -B ${BUILD_DIR} ${CMAKE_DEFS}
  80. cmake --build ${BUILD_DIR} ${CMAKE_TARGETS} -j8
  81. }
  82. compress() {
  83. echo "Compressing payloads to reduce overall binary size..."
  84. pids=""
  85. rm -rf ${BUILD_DIR}/bin/*.gz
  86. for f in ${BUILD_DIR}/bin/* ; do
  87. gzip -n --best -f ${f} &
  88. 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. pids+=" $!"
  95. done
  96. fi
  97. echo
  98. for pid in ${pids}; do
  99. wait $pid
  100. done
  101. echo "Finished compression"
  102. }
  103. install() {
  104. echo "Installing libraries to bin dir ${BUILD_DIR}/bin/"
  105. for lib in $(find ${BUILD_DIR} -name \*.${LIB_EXT}); do
  106. rm -f "${BUILD_DIR}/bin/$(basename ${lib})"
  107. cp -af "${lib}" "${BUILD_DIR}/bin/"
  108. done
  109. }
  110. # Keep the local tree clean after we're done with the build
  111. cleanup() {
  112. (cd ${LLAMACPP_DIR}/ && git checkout CMakeLists.txt)
  113. if [ -n "$(ls -A ../patches/*.diff)" ]; then
  114. for patch in ../patches/*.diff; do
  115. for file in $(grep "^+++ " ${patch} | cut -f2 -d' ' | cut -f2- -d/); do
  116. (cd ${LLAMACPP_DIR}; git checkout ${file})
  117. done
  118. done
  119. fi
  120. }