gen_common.sh 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # common logic accross linux and darwin
  2. init_vars() {
  3. LLAMACPP_DIR=../llama.cpp
  4. CMAKE_DEFS=""
  5. CMAKE_TARGETS="--target ggml --target ggml_static --target llama --target build_info --target common --target ext_server --target llava_static"
  6. if echo "${CGO_CFLAGS}" | grep -- '-g' >/dev/null; then
  7. CMAKE_DEFS="-DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_VERBOSE_MAKEFILE=on -DLLAMA_GPROF=on -DLLAMA_SERVER_VERBOSE=on"
  8. else
  9. # TODO - add additional optimization flags...
  10. CMAKE_DEFS="-DCMAKE_BUILD_TYPE=Release -DLLAMA_SERVER_VERBOSE=off"
  11. fi
  12. }
  13. git_module_setup() {
  14. if [ -n "${OLLAMA_SKIP_PATCHING}" ]; then
  15. echo "Skipping submodule initialization"
  16. return
  17. fi
  18. # Make sure the tree is clean after the directory moves
  19. if [ -d "${LLAMACPP_DIR}/gguf" ]; then
  20. echo "Cleaning up old submodule"
  21. rm -rf ${LLAMACPP_DIR}
  22. fi
  23. git submodule init
  24. git submodule update --force ${LLAMACPP_DIR}
  25. }
  26. apply_patches() {
  27. # Wire up our CMakefile
  28. if ! grep ollama ${LLAMACPP_DIR}/examples/server/CMakeLists.txt; then
  29. echo 'include (../../../ext_server/CMakeLists.txt) # ollama' >>${LLAMACPP_DIR}/examples/server/CMakeLists.txt
  30. fi
  31. # Avoid duplicate main symbols when we link into the cgo binary
  32. sed -e 's/int main(/int __main(/g' <${LLAMACPP_DIR}/examples/server/server.cpp >${LLAMACPP_DIR}/examples/server/server.cpp.tmp &&
  33. mv ${LLAMACPP_DIR}/examples/server/server.cpp.tmp ${LLAMACPP_DIR}/examples/server/server.cpp
  34. }
  35. build() {
  36. cmake -S ${LLAMACPP_DIR} -B ${BUILD_DIR} ${CMAKE_DEFS}
  37. cmake --build ${BUILD_DIR} ${CMAKE_TARGETS} -j8
  38. }
  39. install() {
  40. rm -rf ${BUILD_DIR}/lib
  41. mkdir -p ${BUILD_DIR}/lib
  42. cp ${BUILD_DIR}/examples/server/libext_server.a ${BUILD_DIR}/lib
  43. cp ${BUILD_DIR}/common/libcommon.a ${BUILD_DIR}/lib
  44. cp ${BUILD_DIR}/libllama.a ${BUILD_DIR}/lib
  45. cp ${BUILD_DIR}/libggml_static.a ${BUILD_DIR}/lib
  46. }
  47. # Keep the local tree clean after we're done with the build
  48. cleanup() {
  49. (cd ${LLAMACPP_DIR}/examples/server/ && git checkout CMakeLists.txt server.cpp)
  50. }