Dev User 1 miesiąc temu
rodzic
commit
ca14e4214d
2 zmienionych plików z 95 dodań i 16 usunięć
  1. 47 11
      CMakeLists.txt
  2. 48 5
      build.sh

+ 47 - 11
CMakeLists.txt

@@ -35,24 +35,60 @@ target_include_directories(ngs PRIVATE
 target_compile_options(ngs PRIVATE -Wall -Wextra)
 target_compile_definitions(ngs PRIVATE NGS_SOURCE_DIR="${CMAKE_SOURCE_DIR}")
 
-link_directories(/usr/local/lib /usr/lib/x86_64-linux-gnu)
+list(APPEND CMAKE_LIBRARY_PATH /usr/local/lib /usr/lib/x86_64-linux-gnu /usr/lib)
 
-target_link_libraries(ngs PRIVATE
-    ylib
-    mysqlcppconn
-    mysqlclient
-    hpsocket
-    leveldb
-    sqlite3
-    ssl
-    crypto
-    z
+# Prefer shared mysqlcppconn so we do not require linkable libmysqlclient at build time
+# (libmysqlcppconn.so already DT_NEEDED libmysqlclient.so.*).
+set(_ngs_saved_suffixes "${CMAKE_FIND_LIBRARY_SUFFIXES}")
+set(CMAKE_FIND_LIBRARY_SUFFIXES ".so")
+find_library(MYSQLCPPCONN_LIB NAMES mysqlcppconn)
+set(CMAKE_FIND_LIBRARY_SUFFIXES "${_ngs_saved_suffixes}")
+if(NOT MYSQLCPPCONN_LIB)
+    find_library(MYSQLCPPCONN_LIB NAMES mysqlcppconn REQUIRED)
+endif()
+
+find_library(MYSQLCLIENT_LIB NAMES mysqlclient)
+find_library(YLIB_LIB NAMES ylib REQUIRED)
+find_library(HPSOCKET_LIB NAMES hpsocket REQUIRED)
+find_library(LEVELDB_LIB NAMES leveldb REQUIRED)
+find_library(SQLITE3_LIB NAMES sqlite3 REQUIRED)
+find_library(SSL_LIB NAMES ssl REQUIRED)
+find_library(CRYPTO_LIB NAMES crypto REQUIRED)
+find_library(Z_LIB NAMES z REQUIRED)
+
+set(NGS_LIBS
+    ${YLIB_LIB}
+    ${MYSQLCPPCONN_LIB}
+    ${HPSOCKET_LIB}
+    ${LEVELDB_LIB}
+    ${SQLITE3_LIB}
+    ${SSL_LIB}
+    ${CRYPTO_LIB}
+    ${Z_LIB}
     pthread
     dl
     rt
     stdc++fs
 )
 
+# Needed when linking a static libmysqlcppconn; optional for the shared .so case.
+if(MYSQLCLIENT_LIB)
+    list(APPEND NGS_LIBS ${MYSQLCLIENT_LIB})
+elseif(MYSQLCPPCONN_LIB MATCHES "\\.(a)$")
+    message(FATAL_ERROR
+        "libmysqlcppconn is static but libmysqlclient was not found. "
+        "Install libmysqlclient-dev (or ./build.sh deps).")
+endif()
+
+target_link_libraries(ngs PRIVATE ${NGS_LIBS})
+
+message(STATUS "mysqlcppconn: ${MYSQLCPPCONN_LIB}")
+if(MYSQLCLIENT_LIB)
+    message(STATUS "mysqlclient: ${MYSQLCLIENT_LIB}")
+else()
+    message(STATUS "mysqlclient: (not found, using shared mysqlcppconn runtime dep)")
+endif()
+
 install(TARGETS ngs RUNTIME DESTINATION bin)
 
 add_custom_target(install-deps

+ 48 - 5
build.sh

@@ -31,12 +31,26 @@ info() { echo "==> $*"; }
 
 have_cmd() { command -v "$1" >/dev/null 2>&1; }
 
+# True only if the linker can resolve -lNAME (unversioned .so or .a).
+# Versioned runtime libs like libfoo.so.21 alone are NOT enough.
 find_lib() {
     local name="$1"
-    ldconfig -p 2>/dev/null | grep -q "lib${name}\\.so" && return 0
-    [[ -f "/usr/local/lib/lib${name}.a" || -f "/usr/local/lib/lib${name}.so" ]] && return 0
-    [[ -f "/usr/lib/x86_64-linux-gnu/lib${name}.a" || -f "/usr/lib/x86_64-linux-gnu/lib${name}.so" ]] && return 0
-    [[ -f "/usr/lib/lib${name}.a" || -f "/usr/lib/lib${name}.so" ]] && return 0
+    local dir
+    for dir in /usr/local/lib /usr/lib/x86_64-linux-gnu /usr/lib /lib/x86_64-linux-gnu /lib; do
+        [[ -e "${dir}/lib${name}.so" || -e "${dir}/lib${name}.a" ]] && return 0
+    done
+    return 1
+}
+
+# Runtime .so is enough for DT_NEEDED deps of shared mysqlcppconn.
+find_lib_runtime() {
+    local name="$1"
+    local dir
+    for dir in /usr/local/lib /usr/lib/x86_64-linux-gnu /usr/lib /lib/x86_64-linux-gnu /lib; do
+        [[ -e "${dir}/lib${name}.so" || -e "${dir}/lib${name}.a" ]] && return 0
+        compgen -G "${dir}/lib${name}.so.*" >/dev/null && return 0
+    done
+    ldconfig -p 2>/dev/null | grep -E -q "lib${name}\\.so(\\.|$)" && return 0
     return 1
 }
 
@@ -115,7 +129,36 @@ cmd_check() {
         ok=0
     fi
 
-    for lib in mysqlcppconn mysqlclient sqlite3 leveldb ssl crypto z; do
+    # Prefer shared mysqlcppconn.so so -lmysqlclient is not required at link time.
+    if [[ -e /usr/lib/x86_64-linux-gnu/libmysqlcppconn.so \
+       || -e /usr/local/lib/libmysqlcppconn.so \
+       || -e /usr/lib/libmysqlcppconn.so ]]; then
+        echo "  [OK]   library: mysqlcppconn (shared)"
+        if find_lib_runtime mysqlclient; then
+            echo "  [OK]   library: mysqlclient (runtime)"
+        else
+            echo "  [MISS] library: mysqlclient runtime (libmysqlclient.so.*)"
+            ok=0
+        fi
+        if find_lib mysqlclient; then
+            echo "  [OK]   library: mysqlclient (linkable, optional)"
+        else
+            echo "  [WARN] library: mysqlclient not linkable (ok with shared mysqlcppconn; install libmysqlclient-dev if link fails)"
+        fi
+    elif find_lib mysqlcppconn; then
+        echo "  [OK]   library: mysqlcppconn (static)"
+        if find_lib mysqlclient; then
+            echo "  [OK]   library: mysqlclient"
+        else
+            echo "  [MISS] library: mysqlclient (required for static mysqlcppconn; try: ./build.sh deps)"
+            ok=0
+        fi
+    else
+        echo "  [MISS] library: mysqlcppconn  (try: ./build.sh deps)"
+        ok=0
+    fi
+
+    for lib in sqlite3 leveldb ssl crypto z; do
         if find_lib "$lib"; then
             echo "  [OK]   library: $lib"
         else