@@ -167,7 +167,42 @@ if(LLVM_USE_STATIC_ZSTD AND NOT TARGET zstd::libzstd_static)
167167 endif ()
168168set (LLVM_ENABLE_ZSTD OFF )
169169else ()
170- set (LLVM_ENABLE_ZSTD ${zstd_FOUND} )
170+ # Check if the zstd static library is usable when BUILD_SHARED_LIBS is ON
171+ # Test linking zstd::libzstd_static with a shared library. This is to ensure
172+ # that the static library is built with -fPIC flag.
173+ if (zstd_FOUND AND LLVM_USE_STATIC_ZSTD AND TARGET zstd::libzstd_static AND BUILD_SHARED_LIBS )
174+ cmake_push_check_state()
175+
176+ set (CMAKE_REQUIRED_FLAGS "-fPIC -shared" )
177+ set (CMAKE_REQUIRED_INCLUDES ${ZSTD_INCLUDE_DIRS} )
178+ set (CMAKE_REQUIRED_LIBRARIES zstd::libzstd_static)
179+ check_c_source_compiles("
180+ #include <zstd.h>
181+ int main() {
182+ ZSTD_CCtx* cctx = ZSTD_createCCtx();
183+ ZSTD_freeCCtx(cctx);
184+ return 0;
185+ }"
186+ IS_ZSTD_STATIC_LIB_USABLE_WITH_SHARED_LIBRARIES
187+ OUTPUT_VARIABLE CMAKE_ERROR_MESSAGE)
188+
189+ cmake_pop_check_state()
190+
191+ # If the test program failed to compile, disable zstd or fail if LLVM_ENABLE_ZSTD is FORCE_ON.
192+ if (NOT IS_ZSTD_STATIC_LIB_USABLE_WITH_SHARED_LIBRARIES)
193+ message (WARNING "ZSTD static library is not usable. Error compiling the test program." )
194+ message (WARNING "Build log:\n ${CMAKE_ERROR_MESSAGE} " )
195+
196+ # Fail if LLVM_ENABLE_ZSTD is FORCE_ON.
197+ if (LLVM_ENABLE_ZSTD STREQUAL FORCE_ON)
198+ message (FATAL_ERROR "zstd static library is not usable, but LLVM_USE_STATIC_ZSTD=ON and LLVM_ENABLE_ZSTD=FORCE_ON." )
199+ endif ()
200+ endif ()
201+
202+ set (LLVM_ENABLE_ZSTD IS_ZSTD_STATIC_LIB_USABLE_WITH_SHARED_LIBRARIES)
203+ else ()
204+ set (LLVM_ENABLE_ZSTD ${zstd_FOUND} )
205+ endif ()
171206endif ()
172207
173208if (LLVM_ENABLE_LIBXML2)
0 commit comments