Skip to content

Commit

Permalink
允许静态链接特定依赖
Browse files Browse the repository at this point in the history
  • Loading branch information
lanthora committed Jul 1, 2024
1 parent 3cf0f49 commit 1c49533
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 31 deletions.
33 changes: 23 additions & 10 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@ option(CANDY_NOEXE "Don't build executable")
option(CANDY_DEVEL "Build development library")
option(CANDY_STATIC "Static linking dependent libraries")

set(BUILD_SHARED_LIBS OFF CACHE BOOL "" FORCE)

if (${CANDY_STATIC})
set(CMAKE_SKIP_BUILD_RPATH TRUE)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static")
set(BUILD_SHARED_LIBS OFF CACHE BOOL "" FORCE)

include(FetchContent)
include(ExternalProject)
set(CANDY_STATIC_OPENSSL 1)
set(CANDY_STATIC_FMT 1)
set(CANDY_STATIC_SPDLOG 1)
set(CANDY_STATIC_POCO 1)
endif()

# openssl
if (${CANDY_STATIC_OPENSSL})
execute_process(
COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/openssl
)
Expand Down Expand Up @@ -55,8 +58,10 @@ if (${CANDY_STATIC})
set(OPENSSL_LIB_CRYPTO ${OPENSSL_ROOT_DIR}/libcrypto.a)
set(OPENSSL_LIB_SSL ${OPENSSL_ROOT_DIR}/libssl.a)
include_directories(${OPENSSL_INCLUDE})
endif()

# fmt
if (${CANDY_STATIC_FMT})
include(FetchContent)
FetchContent_Declare(
fmt
GIT_REPOSITORY https://github.com/fmtlib/fmt.git
Expand All @@ -67,8 +72,10 @@ if (${CANDY_STATIC})
FetchContent_Populate(fmt)
add_subdirectory(${fmt_SOURCE_DIR} ${fmt_BINARY_DIR} EXCLUDE_FROM_ALL)
endif()
endif()

# spdlog
if (${CANDY_STATIC_SPDLOG})
include(FetchContent)
FetchContent_Declare(
spdlog
GIT_REPOSITORY https://github.com/gabime/spdlog.git
Expand All @@ -79,8 +86,9 @@ if (${CANDY_STATIC})
FetchContent_Populate(spdlog)
add_subdirectory(${spdlog_SOURCE_DIR} ${spdlog_BINARY_DIR} EXCLUDE_FROM_ALL)
endif()
endif()

# poco
if (${CANDY_STATIC_POCO})
set(ENABLE_DATA OFF CACHE BOOL "" FORCE)
set(ENABLE_DATA_MYSQL OFF CACHE BOOL "" FORCE)
set(ENABLE_DATA_POSTGRESQL OFF CACHE BOOL "" FORCE)
Expand All @@ -96,10 +104,15 @@ if (${CANDY_STATIC})
set(ENABLE_ACTIVERECORD_COMPILER OFF CACHE BOOL "" FORCE)
set(ENABLE_ZIP OFF CACHE BOOL "" FORCE)
set(ENABLE_JWT OFF CACHE BOOL "" FORCE)
include(FetchContent)

set(CANDY_POCO_REPOSITORY "https://github.com/pocoproject/poco.git" CACHE STRING "")
set(CANDY_POCO_TAG "poco-1.13.3-release" CACHE STRING "")

FetchContent_Declare(
poco
GIT_REPOSITORY https://github.com/pocoproject/poco.git
GIT_TAG poco-1.13.3-release
GIT_REPOSITORY ${CANDY_POCO_REPOSITORY}
GIT_TAG ${CANDY_POCO_TAG}
)
FetchContent_GetProperties(poco)
if(NOT poco_POPULATED)
Expand Down
12 changes: 9 additions & 3 deletions src/cffi/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ target_link_libraries(${CANDY_LIBRARY_NAME} PRIVATE tun)
target_link_libraries(${CANDY_LIBRARY_NAME} PRIVATE peer)
target_link_libraries(${CANDY_LIBRARY_NAME} PRIVATE utility)

if (${CANDY_STATIC})
if (${CANDY_STATIC_SPDLOG})
target_link_libraries(${CANDY_LIBRARY_NAME} PRIVATE spdlog::spdlog)
target_link_libraries(${CANDY_LIBRARY_NAME} PRIVATE ${OPENSSL_LIB_CRYPTO} ${OPENSSL_LIB_SSL})
target_link_libraries(${CANDY_LIBRARY_NAME} PRIVATE Poco::Foundation Poco::Net Poco::NetSSL)
else()
find_package(PkgConfig REQUIRED)
pkg_check_modules(DEPS REQUIRED spdlog)
Expand All @@ -22,6 +20,14 @@ else()
target_link_libraries(${CANDY_LIBRARY_NAME} PRIVATE ${DEPS_LIBRARIES})
endif()

if (${CANDY_STATIC_OPENSSL})
target_link_libraries(${CANDY_LIBRARY_NAME} PRIVATE ${OPENSSL_LIB_CRYPTO} ${OPENSSL_LIB_SSL})
endif()

if (${CANDY_STATIC_POCO})
target_link_libraries(${CANDY_LIBRARY_NAME} PRIVATE Poco::Foundation Poco::Net Poco::NetSSL)
endif()

if (${CANDY_DEVEL})
install(FILES candy.h DESTINATION include)
install(TARGETS ${CANDY_LIBRARY_NAME} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
Expand Down
24 changes: 20 additions & 4 deletions src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,35 @@ add_library(core STATIC ${DIR_LIB_SRCS})

set_target_properties(core PROPERTIES CXX_STANDARD 20)

if (${CANDY_STATIC})
if (${CANDY_STATIC_FMT})
target_link_libraries(core PRIVATE fmt::fmt)
else()
find_package(PkgConfig REQUIRED)
pkg_check_modules(DEPS REQUIRED fmt)
add_definitions(${DEPS_CFLAGS})
include_directories(${DEPS_INCLUDEDIR})
target_link_libraries(core PRIVATE ${DEPS_LIBRARIES})
endif()

if (${CANDY_STATIC_SPDLOG})
target_link_libraries(core PRIVATE spdlog::spdlog)
target_link_libraries(core PRIVATE ${OPENSSL_LIB_CRYPTO} ${OPENSSL_LIB_SSL})
target_link_libraries(core PRIVATE Poco::Foundation Poco::Net Poco::NetSSL)
else()
find_package(PkgConfig REQUIRED)
pkg_check_modules(DEPS REQUIRED fmt spdlog)
pkg_check_modules(DEPS REQUIRED spdlog)
add_definitions(${DEPS_CFLAGS})
include_directories(${DEPS_INCLUDEDIR})
target_link_libraries(core PRIVATE ${DEPS_LIBRARIES})
endif()

if (${CANDY_STATIC_OPENSSL})
target_link_libraries(core PRIVATE ${OPENSSL_LIB_CRYPTO} ${OPENSSL_LIB_SSL})
endif()

if (${CANDY_STATIC_POCO})
target_link_libraries(core PRIVATE Poco::Foundation Poco::Net Poco::NetSSL)
endif()


target_link_libraries(core PRIVATE websocket)
target_link_libraries(core PRIVATE tun)
target_link_libraries(core PRIVATE peer)
12 changes: 9 additions & 3 deletions src/main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ add_executable(${CANDY_EXECUTE_NAME} ${DIR_SRCS})

set_target_properties(${CANDY_EXECUTE_NAME} PROPERTIES CXX_STANDARD 20)

if (${CANDY_STATIC})
if (${CANDY_STATIC_SPDLOG})
target_link_libraries(${CANDY_EXECUTE_NAME} PRIVATE spdlog::spdlog)
target_link_libraries(${CANDY_EXECUTE_NAME} PRIVATE ${OPENSSL_LIB_CRYPTO} ${OPENSSL_LIB_SSL})
target_link_libraries(${CANDY_EXECUTE_NAME} PRIVATE Poco::Foundation Poco::Net Poco::NetSSL)
else()
find_package(PkgConfig REQUIRED)
pkg_check_modules(DEPS REQUIRED spdlog)
Expand All @@ -15,6 +13,14 @@ else()
target_link_libraries(${CANDY_EXECUTE_NAME} PRIVATE ${DEPS_LIBRARIES})
endif()

if (${CANDY_STATIC_OPENSSL})
target_link_libraries(${CANDY_EXECUTE_NAME} PRIVATE ${OPENSSL_LIB_CRYPTO} ${OPENSSL_LIB_SSL})
endif()

if (${CANDY_STATIC_POCO})
target_link_libraries(${CANDY_EXECUTE_NAME} PRIVATE Poco::Foundation Poco::Net Poco::NetSSL)
endif()


set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
Expand Down
13 changes: 10 additions & 3 deletions src/peer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,24 @@ if (${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
target_link_libraries(peer PRIVATE ws2_32)
endif()

if (${CANDY_STATIC})
if (${CANDY_STATIC_SPDLOG})
target_link_libraries(peer PRIVATE spdlog::spdlog)
target_link_libraries(peer PRIVATE Poco::Net)
target_link_libraries(peer PRIVATE ${OPENSSL_LIB_CRYPTO} ${OPENSSL_LIB_SSL})
else()
find_package(PkgConfig REQUIRED)
pkg_check_modules(DEPS REQUIRED spdlog)
add_definitions(${DEPS_CFLAGS})
include_directories(${DEPS_INCLUDEDIR})
target_link_libraries(peer PRIVATE ${DEPS_LIBRARIES})
endif()

if (${CANDY_STATIC_OPENSSL})
target_link_libraries(peer PRIVATE ${OPENSSL_LIB_CRYPTO} ${OPENSSL_LIB_SSL})
endif()

if (${CANDY_STATIC_POCO})
target_link_libraries(peer PRIVATE Poco::Foundation Poco::Net Poco::NetSSL)
else()
find_package(Poco REQUIRED Net)
target_link_libraries(peer PRIVATE Poco::Net)
endif()

7 changes: 5 additions & 2 deletions src/tun/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,16 @@ if (${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
include_directories(${CMAKE_CURRENT_BINARY_DIR}/wintun/include)
endif()

if (${CANDY_STATIC})
if (${CANDY_STATIC_SPDLOG})
target_link_libraries(tun PRIVATE spdlog::spdlog)
target_link_libraries(tun PRIVATE ${OPENSSL_LIB_CRYPTO} ${OPENSSL_LIB_SSL})
else()
find_package(PkgConfig REQUIRED)
pkg_check_modules(DEPS REQUIRED spdlog)
add_definitions(${DEPS_CFLAGS})
include_directories(${DEPS_INCLUDEDIR})
target_link_libraries(tun PRIVATE ${DEPS_LIBRARIES})
endif()

if (${CANDY_STATIC_OPENSSL})
target_link_libraries(tun PRIVATE ${OPENSSL_LIB_CRYPTO} ${OPENSSL_LIB_SSL})
endif()
2 changes: 1 addition & 1 deletion src/utility/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ if (${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
target_link_libraries(utility PRIVATE ws2_32)
endif()

if (${CANDY_STATIC})
if (${CANDY_STATIC_SPDLOG})
target_link_libraries(utility PRIVATE spdlog::spdlog)
else()
find_package(PkgConfig REQUIRED)
Expand Down
17 changes: 12 additions & 5 deletions src/websocket/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,23 @@ add_library(websocket STATIC ${DIR_LIB_SRCS})

set_target_properties(websocket PROPERTIES CXX_STANDARD 20)

if (${CANDY_STATIC})
if (${CANDY_STATIC_SPDLOG})
target_link_libraries(websocket PRIVATE spdlog::spdlog)
target_link_libraries(websocket PRIVATE Poco::Foundation Poco::Net Poco::NetSSL)
else()
find_package(Poco REQUIRED COMPONENTS Foundation XML JSON Net NetSSL)
target_link_libraries(websocket PRIVATE Poco::Foundation Poco::Net Poco::NetSSL)

find_package(PkgConfig REQUIRED)
pkg_check_modules(DEPS REQUIRED spdlog)
add_definitions(${DEPS_CFLAGS})
include_directories(${DEPS_INCLUDEDIR})
target_link_libraries(websocket PRIVATE ${DEPS_LIBRARIES})
endif()

if (${CANDY_STATIC_OPENSSL})
target_link_libraries(websocket PRIVATE ${OPENSSL_LIB_CRYPTO} ${OPENSSL_LIB_SSL})
endif()

if (${CANDY_STATIC_POCO})
target_link_libraries(websocket PRIVATE Poco::Foundation Poco::Net Poco::NetSSL)
else()
find_package(Poco REQUIRED COMPONENTS Foundation XML JSON Net NetSSL)
target_link_libraries(websocket PRIVATE Poco::Foundation Poco::Net Poco::NetSSL)
endif()

0 comments on commit 1c49533

Please sign in to comment.