Adapt our build for imported server.cpp

This commit is contained in:
Daniel Hiltgen 2024-03-12 13:51:44 -07:00
parent 9ac6440da3
commit 85129d3a32
4 changed files with 12 additions and 24 deletions

View file

@ -1,15 +1,11 @@
# Ollama specific CMakefile to include in llama.cpp/examples/server
set(TARGET ext_server) set(TARGET ext_server)
option(LLAMA_SERVER_VERBOSE "Build verbose logging option for Server" ON) option(LLAMA_SERVER_VERBOSE "Build verbose logging option for Server" ON)
if (WIN32) if (WIN32)
add_library(${TARGET} SHARED ../../../ext_server/ext_server.cpp ../../llama.cpp) add_library(${TARGET} SHARED ext_server.cpp ../llama.cpp/llama.cpp)
else() else()
add_library(${TARGET} STATIC ../../../ext_server/ext_server.cpp ../../llama.cpp) add_library(${TARGET} STATIC ext_server.cpp ../llama.cpp/llama.cpp)
endif() endif()
target_include_directories(${TARGET} PRIVATE ../../common)
target_include_directories(${TARGET} PRIVATE ../..)
target_include_directories(${TARGET} PRIVATE ../../..)
target_compile_features(${TARGET} PRIVATE cxx_std_11) target_compile_features(${TARGET} PRIVATE cxx_std_11)
target_compile_definitions(${TARGET} PUBLIC LLAMA_SERVER_LIBRARY=1) target_compile_definitions(${TARGET} PUBLIC LLAMA_SERVER_LIBRARY=1)
target_link_libraries(${TARGET} PRIVATE ggml llava common ) target_link_libraries(${TARGET} PRIVATE ggml llava common )

View file

@ -2770,7 +2770,7 @@ inline void signal_handler(int signal) {
shutdown_handler(signal); shutdown_handler(signal);
} }
int main(int argc, char **argv) int _main(int argc, char **argv)
{ {
#if SERVER_VERBOSE != 1 #if SERVER_VERBOSE != 1
log_disable(); log_disable();

View file

@ -61,8 +61,8 @@ git_module_setup() {
apply_patches() { apply_patches() {
# Wire up our CMakefile # Wire up our CMakefile
if ! grep ollama ${LLAMACPP_DIR}/examples/server/CMakeLists.txt; then if ! grep ollama ${LLAMACPP_DIR}/CMakeLists.txt; then
echo 'include (../../../ext_server/CMakeLists.txt) # ollama' >>${LLAMACPP_DIR}/examples/server/CMakeLists.txt echo 'add_subdirectory(../ext_server ext_server) # ollama' >>${LLAMACPP_DIR}/CMakeLists.txt
fi fi
if [ -n "$(ls -A ../patches/*.diff)" ]; then if [ -n "$(ls -A ../patches/*.diff)" ]; then
@ -76,19 +76,16 @@ apply_patches() {
(cd ${LLAMACPP_DIR} && git apply ${patch}) (cd ${LLAMACPP_DIR} && git apply ${patch})
done done
fi fi
# Avoid duplicate main symbols when we link into the cgo binary
sed -e 's/int main(/int __main(/g' <${LLAMACPP_DIR}/examples/server/server.cpp >${LLAMACPP_DIR}/examples/server/server.cpp.tmp &&
mv ${LLAMACPP_DIR}/examples/server/server.cpp.tmp ${LLAMACPP_DIR}/examples/server/server.cpp
} }
build() { build() {
cmake -S ${LLAMACPP_DIR} -B ${BUILD_DIR} ${CMAKE_DEFS} cmake -S ${LLAMACPP_DIR} -B ${BUILD_DIR} ${CMAKE_DEFS}
cmake --build ${BUILD_DIR} ${CMAKE_TARGETS} -j8 cmake --build ${BUILD_DIR} ${CMAKE_TARGETS} -j8
mkdir -p ${BUILD_DIR}/lib/ mkdir -p ${BUILD_DIR}/lib/
ls ${BUILD_DIR}
g++ -fPIC -g -shared -o ${BUILD_DIR}/lib/libext_server.${LIB_EXT} \ g++ -fPIC -g -shared -o ${BUILD_DIR}/lib/libext_server.${LIB_EXT} \
${GCC_ARCH} \ ${GCC_ARCH} \
${WHOLE_ARCHIVE} ${BUILD_DIR}/examples/server/libext_server.a ${NO_WHOLE_ARCHIVE} \ ${WHOLE_ARCHIVE} ${BUILD_DIR}/ext_server/libext_server.a ${NO_WHOLE_ARCHIVE} \
${BUILD_DIR}/common/libcommon.a \ ${BUILD_DIR}/common/libcommon.a \
${BUILD_DIR}/libllama.a \ ${BUILD_DIR}/libllama.a \
-Wl,-rpath,\$ORIGIN \ -Wl,-rpath,\$ORIGIN \
@ -113,7 +110,7 @@ compress_libs() {
# Keep the local tree clean after we're done with the build # Keep the local tree clean after we're done with the build
cleanup() { cleanup() {
(cd ${LLAMACPP_DIR}/examples/server/ && git checkout CMakeLists.txt server.cpp) (cd ${LLAMACPP_DIR}/ && git checkout CMakeLists.txt)
if [ -n "$(ls -A ../patches/*.diff)" ]; then if [ -n "$(ls -A ../patches/*.diff)" ]; then
for patch in ../patches/*.diff; do for patch in ../patches/*.diff; do

View file

@ -82,8 +82,8 @@ function git_module_setup {
function apply_patches { function apply_patches {
# Wire up our CMakefile # Wire up our CMakefile
if (!(Select-String -Path "${script:llamacppDir}/examples/server/CMakeLists.txt" -Pattern 'ollama')) { if (!(Select-String -Path "${script:llamacppDir}/CMakeLists.txt" -Pattern 'ollama')) {
Add-Content -Path "${script:llamacppDir}/examples/server/CMakeLists.txt" -Value 'include (../../../ext_server/CMakeLists.txt) # ollama' Add-Content -Path "${script:llamacppDir}/CMakeLists.txt" -Value 'add_subdirectory(../ext_server ext_server) # ollama'
} }
# Apply temporary patches until fix is upstream # Apply temporary patches until fix is upstream
@ -107,11 +107,6 @@ function apply_patches {
Set-Location -Path ${script:llamacppDir} Set-Location -Path ${script:llamacppDir}
git apply $patch.FullName git apply $patch.FullName
} }
# Avoid duplicate main symbols when we link into the cgo binary
$content = Get-Content -Path "${script:llamacppDir}/examples/server/server.cpp"
$content = $content -replace 'int main\(', 'int __main('
Set-Content -Path "${script:llamacppDir}/examples/server/server.cpp" -Value $content
} }
function build { function build {
@ -173,8 +168,8 @@ function cleanup {
git checkout $file git checkout $file
} }
} }
Set-Location "${script:llamacppDir}/examples/server" Set-Location "${script:llamacppDir}/"
git checkout CMakeLists.txt server.cpp git checkout CMakeLists.txt
} }