Skip to content

Commit 4451cbd

Browse files
[fix] Force static libraries to be linked when LINK_STATIC is enabled (#28)
* [fix] Force static libraries to be linked when LINK_STATIC is enabled ### Motivation Currently, even with `LINK_STATIC=ON` option, dynamic libraries could still be linked. For example, on macOS, the following libcurl library will be found: ``` /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/lib/libcurl.tbd ``` Then, the `libpulsar.dylib` will link to `/usr/lib/libcurl.4.dylib`, which is not a dynamic library. ### Modifications When `LINK_STATIC` is enabled, change `CMAKE_FIND_LIBRARY_SUFFIXES` to `.a` for non-MSVC compilers so that only static libraries could be found except OpenSSL, which should not be linked statically. In addition, use `find_package` as much as possible instead of raw usages of `find_path` and `find_library`. * Find curl and zlib manually * Fix Windows build
1 parent 94500a8 commit 4451cbd

File tree

2 files changed

+51
-63
lines changed

2 files changed

+51
-63
lines changed

.github/workflows/ci-pr-validation.yaml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@ jobs:
146146
-G "${{ matrix.generator }}" ${{ matrix.arch }} \
147147
-DBUILD_TESTS=OFF \
148148
-DVCPKG_TRIPLET=${{ matrix.triplet }} \
149-
-DCMAKE_BUILD_TYPE=Release \
150149
-S .
151150
fi
152151
@@ -166,7 +165,6 @@ jobs:
166165
-G "${{ matrix.generator }}" ${{ matrix.arch }} \
167166
-DBUILD_TESTS=OFF \
168167
-DVCPKG_TRIPLET=${{ matrix.triplet }} \
169-
-DCMAKE_BUILD_TYPE=Release \
170168
-DBUILD_STATIC_LIB=OFF \
171169
-S .
172170
fi
@@ -265,4 +263,4 @@ jobs:
265263
cache-to: type=gha,mode=max
266264

267265
- name: Build APK packages
268-
run: pkg/apk/docker-build-apk-x86_64.sh build-apk-x86_64:latest
266+
run: pkg/apk/docker-build-apk-x86_64.sh build-apk-x86_64:latest

CMakeLists.txt

Lines changed: 50 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ MESSAGE(STATUS "LINK_STATIC: " ${LINK_STATIC})
7575
option(USE_LOG4CXX "Build with Log4cxx support" OFF)
7676
MESSAGE(STATUS "USE_LOG4CXX: " ${USE_LOG4CXX})
7777

78-
IF (CMAKE_BUILD_TYPE STREQUAL "")
78+
IF (NOT CMAKE_BUILD_TYPE)
7979
set(CMAKE_BUILD_TYPE RelWithDebInfo)
8080
ENDIF ()
8181

@@ -124,6 +124,7 @@ add_definitions(-DLOG_CATEGORY_NAME=${LOG_CATEGORY_NAME} -DBUILDING_PULSAR -DBOO
124124

125125
set(OPENSSL_ROOT_DIR ${OPENSSL_ROOT_DIR} /usr/lib64/)
126126

127+
find_package(OpenSSL REQUIRED)
127128
### This part is to find and keep SSL dynamic libs in RECORD_OPENSSL_SSL_LIBRARY and RECORD_OPENSSL_CRYPTO_LIBRARY
128129
### After find the libs, will unset related cache, and will not affect another same call to find_package.
129130
if (APPLE)
@@ -135,6 +136,8 @@ set(OPENSSL_USE_STATIC_LIBS FALSE)
135136
find_package(OpenSSL REQUIRED)
136137
set(RECORD_OPENSSL_SSL_LIBRARY ${OPENSSL_SSL_LIBRARY})
137138
set(RECORD_OPENSSL_CRYPTO_LIBRARY ${OPENSSL_CRYPTO_LIBRARY})
139+
message("RECORD_OPENSSL_SSL_LIBRARY: " ${RECORD_OPENSSL_SSL_LIBRARY})
140+
message("RECORD_OPENSSL_CRYPTO_LIBRARY: " ${RECORD_OPENSSL_CRYPTO_LIBRARY})
138141

139142
unset(OPENSSL_FOUND CACHE)
140143
unset(OPENSSL_INCLUDE_DIR CACHE)
@@ -145,18 +148,54 @@ unset(OPENSSL_SSL_LIBRARIES CACHE)
145148
unset(OPENSSL_LIBRARIES CACHE)
146149
unset(OPENSSL_VERSION CACHE)
147150

151+
find_package(OpenSSL REQUIRED)
152+
message("OPENSSL_INCLUDE_DIR: " ${OPENSSL_INCLUDE_DIR})
153+
message("OPENSSL_SSL_LIBRARY: " ${OPENSSL_SSL_LIBRARY})
154+
message("OPENSSL_CRYPTO_LIBRARY: " ${OPENSSL_CRYPTO_LIBRARY})
155+
156+
# For dependencies other than OpenSSL, dynamic libraries are forbidden to link when LINK_STATIC is ON
157+
if (LINK_STATIC)
158+
if (NOT MSVC)
159+
set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
160+
endif()
161+
endif ()
162+
163+
find_package(Boost REQUIRED)
164+
message("Boost_INCLUDE_DIRS: " ${Boost_INCLUDE_DIRS})
165+
166+
find_package(Protobuf REQUIRED)
167+
message("Protobuf_INCLUDE_DIRS: " ${Protobuf_INCLUDE_DIRS})
168+
message("Protobuf_LIBRARIES: " ${Protobuf_LIBRARIES})
169+
170+
# NOTE: CMake might not find curl and zlib on some platforms like Ubuntu, in this case, find them manually
171+
set(CURL_NO_CURL_CMAKE ON)
172+
find_package(curl QUIET)
173+
if (NOT CURL_FOUND)
174+
find_path(CURL_INCLUDE_DIRS NAMES curl/curl.h)
175+
find_library(CURL_LIBRARIES NAMES curl curllib libcurl_imp curllib_static libcurl)
176+
endif ()
177+
message("CURL_INCLUDE_DIRS: " ${CURL_INCLUDE_DIRS})
178+
message("CURL_LIBRARIES: " ${CURL_LIBRARIES})
179+
if (NOT CURL_INCLUDE_DIRS OR NOT CURL_LIBRARIES)
180+
message(FATAL_ERROR "Could not find libcurl")
181+
endif ()
182+
183+
find_package(zlib QUIET)
184+
if (NOT ZLIB_FOUND)
185+
find_path(ZLIB_INCLUDE_DIRS NAMES zlib.h)
186+
find_library(ZLIB_LIBRARIES NAMES z zlib zdll zlib1 zlibstatic)
187+
endif ()
188+
message("ZLIB_INCLUDE_DIRS: " ${ZLIB_INCLUDE_DIRS})
189+
message("ZLIB_LIBRARIES: " ${ZLIB_LIBRARIES})
190+
if (NOT ZLIB_INCLUDE_DIRS OR NOT ZLIB_LIBRARIES)
191+
message(FATAL_ERROR "Could not find zlib")
192+
endif ()
193+
148194
if (LINK_STATIC)
149-
find_library(ZLIB_LIBRARIES REQUIRED NAMES libz.a z zlib)
150-
message(STATUS "ZLIB_LIBRARIES: ${ZLIB_LIBRARIES}")
151-
find_library(Protobuf_LIBRARIES NAMES libprotobuf.a libprotobuf)
152-
message(STATUS "Protobuf: ${Protobuf_LIBRARIES}")
153-
find_library(CURL_LIBRARIES NAMES libcurl.a curl curl_a libcurl_a)
154-
message(STATUS "CURL_LIBRARIES: ${CURL_LIBRARIES}")
155195
find_library(LIB_ZSTD NAMES libzstd.a)
156196
message(STATUS "ZStd: ${LIB_ZSTD}")
157197
find_library(LIB_SNAPPY NAMES libsnappy.a)
158198
message(STATUS "LIB_SNAPPY: ${LIB_SNAPPY}")
159-
set(COMMON_LIBS ${Protobuf_LIBRARIES} ${COMMON_LIBS})
160199

161200
if (USE_LOG4CXX)
162201
if (LOG4CXX_USE_DYNAMIC_LIBS)
@@ -180,43 +219,9 @@ if (LINK_STATIC)
180219
add_definitions(-DCURL_STATICLIB)
181220
endif()
182221

183-
if (UNIX AND NOT APPLE)
184-
set(CMAKE_FIND_LIBRARY_SUFFIXES .a)
185-
endif()
186-
187222
SET(Boost_USE_STATIC_LIBS ON)
188223
SET(OPENSSL_USE_STATIC_LIBS TRUE)
189224
else()
190-
# Link to shared libraries
191-
find_package(ZLIB REQUIRED)
192-
set(ZLIB_LIBRARIES ${ZLIB_LIBRARIES})
193-
# NOTE: The default MODULE mode may not find debug libraries so use CONFIG mode here
194-
unset(Protobuf_INCLUDE_DIRS CACHE)
195-
unset(Protobuf_LIBRARIES CACHE)
196-
find_package(Protobuf QUIET CONFIG)
197-
# NOTE: On Windows x86 platform, Protobuf_FOUND might be set false but Protobuf_INCLUDE_DIRS and
198-
# Protobuf_LIBRARIES are both found.
199-
if (Protobuf_INCLUDE_DIRS AND Protobuf_LIBRARIES AND NOT Protobuf_FOUND)
200-
set(Protobuf_FOUND TRUE)
201-
endif ()
202-
if (Protobuf_FOUND)
203-
message("Found Protobuf in config mode")
204-
message(STATUS "Protobuf_LIBRARIES: ${Protobuf_LIBRARIES}")
205-
message(STATUS "Protobuf_INCLUDE_DIRS: ${Protobuf_INCLUDE_DIRS}")
206-
else ()
207-
message("Failed to find Protobuf in config mode, try to find it from system path")
208-
find_library(Protobuf_LIBRARIES protobuf libprotobuf)
209-
find_path(Protobuf_INCLUDE_DIRS google/protobuf/stubs/common.h)
210-
message(STATUS "Protobuf_LIBRARIES: ${Protobuf_LIBRARIES}")
211-
message(STATUS "Protobuf_INCLUDE_DIRS: ${Protobuf_INCLUDE_DIRS}")
212-
endif ()
213-
214-
if (${Protobuf_FOUND} AND (${CMAKE_VERSION} VERSION_GREATER 3.8))
215-
set(COMMON_LIBS protobuf::libprotobuf ${COMMON_LIBS})
216-
else ()
217-
set(COMMON_LIBS ${Protobuf_LIBRARIES} ${COMMON_LIBS})
218-
endif ()
219-
220225
if (MSVC AND (${CMAKE_BUILD_TYPE} STREQUAL Debug))
221226
find_library(LIB_ZSTD zstdd HINTS "${VCPKG_DEBUG_ROOT}/lib")
222227
else ()
@@ -228,20 +233,12 @@ else()
228233
find_library(LIB_SNAPPY NAMES snappy libsnappy)
229234
endif ()
230235

231-
find_package(CURL REQUIRED)
232-
if (${CMAKE_VERSION} VERSION_GREATER "3.12")
233-
set(COMMON_LIBS ${COMMON_LIBS} CURL::libcurl)
234-
endif ()
235-
236236
if (USE_LOG4CXX)
237237
find_library(LOG4CXX_LIBRARY_PATH log4cxx)
238238
find_path(LOG4CXX_INCLUDE_PATH log4cxx/logger.h)
239239
endif (USE_LOG4CXX)
240240
endif (LINK_STATIC)
241241

242-
243-
find_package(Boost)
244-
245242
if (Boost_MAJOR_VERSION EQUAL 1 AND Boost_MINOR_VERSION LESS 69)
246243
# Boost System does not require linking since 1.69
247244
set(BOOST_COMPONENTS ${BOOST_COMPONENTS} system)
@@ -269,8 +266,6 @@ endif()
269266

270267
find_package(Boost REQUIRED COMPONENTS ${BOOST_COMPONENTS})
271268

272-
find_package(OpenSSL REQUIRED)
273-
274269
if (BUILD_TESTS)
275270
find_path(GTEST_INCLUDE_PATH gtest/gtest.h)
276271
find_path(GMOCK_INCLUDE_PATH gmock/gmock.h)
@@ -314,7 +309,7 @@ include_directories(
314309
${CMAKE_SOURCE_DIR}/include
315310
${CMAKE_BINARY_DIR}/include
316311
${AUTOGEN_DIR}
317-
${Boost_INCLUDE_DIR}
312+
${Boost_INCLUDE_DIRS}
318313
${OPENSSL_INCLUDE_DIR}
319314
${ZLIB_INCLUDE_DIRS}
320315
${CURL_INCLUDE_DIRS}
@@ -329,20 +324,15 @@ set(COMMON_LIBS
329324
Threads::Threads
330325
${Boost_REGEX_LIBRARY}
331326
${Boost_SYSTEM_LIBRARY}
327+
${Boost_DATE_TIME_LIBRARY}
328+
${Protobuf_LIBRARIES}
332329
${CURL_LIBRARIES}
333330
${OPENSSL_LIBRARIES}
334331
${ZLIB_LIBRARIES}
335332
${ADDITIONAL_LIBRARIES}
336333
${CMAKE_DL_LIBS}
337334
)
338335

339-
if (MSVC)
340-
set(COMMON_LIBS
341-
${COMMON_LIBS}
342-
${Boost_DATE_TIME_LIBRARY}
343-
)
344-
endif()
345-
346336
if (NOT MSVC)
347337
set(COMMON_LIBS ${COMMON_LIBS} m)
348338
else()

0 commit comments

Comments
 (0)