Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LLVM] Check if zstd static libraries are built with -fPIC flag #15970

Open
wants to merge 1 commit into
base: sycl
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion llvm/cmake/config-ix.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,42 @@ if(LLVM_USE_STATIC_ZSTD AND NOT TARGET zstd::libzstd_static)
endif()
set(LLVM_ENABLE_ZSTD OFF)
else()
set(LLVM_ENABLE_ZSTD ${zstd_FOUND})
# Check if the zstd static library is usable when BUILD_SHARED_LIBS is ON
# Test linking zstd::libzstd_static with a shared library. This is to ensure
# that the static library is built with -fPIC flag.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i havent seen this kind of issue before, is that because almost everything is built with -fPIC so it just works?

are there any other examples in llvm or other open source projects of solving it this way?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i havent seen this kind of issue before, is that because almost everything is built with -fPIC so it just works?

This issue was observed when DPC++ is built in shared lib configuration on Ubuntu 24.04. Apparently, the zstd static library that comes with Ubuntu 24.04 was built without the -fPIC flag and thus it gives error when linking to dynamic libraries. I've filed a bug report about the zstd package in Ubuntu 24.04 here: https://bugs.launchpad.net/ubuntu/+source/libzstd/+bug/2086543

We did not observe this error before because the zstd package that we use in CI (Ubuntu 22.04) was built correctly with fPIC flag.

are there any other examples in llvm or other open source projects of solving it this way?

I'm not aware of any. Let me dig around.

Copy link
Contributor

@sarnex sarnex Nov 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah my overalll question is i've never seen this kind of issue before so i don't know what the fix should look like so i can't give a good review :)

if(zstd_FOUND AND LLVM_USE_STATIC_ZSTD AND TARGET zstd::libzstd_static AND BUILD_SHARED_LIBS)
cmake_push_check_state()

set(CMAKE_REQUIRED_FLAGS "-fPIC -shared")
set(CMAKE_REQUIRED_INCLUDES ${ZSTD_INCLUDE_DIRS})
set(CMAKE_REQUIRED_LIBRARIES zstd::libzstd_static)
check_c_source_compiles("
#include <zstd.h>
int main() {
ZSTD_CCtx* cctx = ZSTD_createCCtx();
ZSTD_freeCCtx(cctx);
return 0;
}"
IS_ZSTD_STATIC_LIB_USABLE_WITH_SHARED_LIBRARIES
OUTPUT_VARIABLE CMAKE_ERROR_MESSAGE)

cmake_pop_check_state()

# If the test program failed to compile, disable zstd or fail if LLVM_ENABLE_ZSTD is FORCE_ON.
if(NOT IS_ZSTD_STATIC_LIB_USABLE_WITH_SHARED_LIBRARIES)
message(WARNING "ZSTD static library is not usable. Error compiling the test program.")
message(WARNING "Build log:\n ${CMAKE_ERROR_MESSAGE}")

# Fail if LLVM_ENABLE_ZSTD is FORCE_ON.
if(LLVM_ENABLE_ZSTD STREQUAL FORCE_ON)
message(FATAL_ERROR "zstd static library is not usable, but LLVM_USE_STATIC_ZSTD=ON and LLVM_ENABLE_ZSTD=FORCE_ON.")
endif()
endif()

set(LLVM_ENABLE_ZSTD IS_ZSTD_STATIC_LIB_USABLE_WITH_SHARED_LIBRARIES)
else()
set(LLVM_ENABLE_ZSTD ${zstd_FOUND})
endif()
endif()

if(LLVM_ENABLE_LIBXML2)
Expand Down
Loading