From 215b326c25dc419e03e859b1534dc5b4e8ded3d2 Mon Sep 17 00:00:00 2001 From: Micah Snyder Date: Wed, 31 May 2023 15:02:46 -0700 Subject: [PATCH] CMake: Option to build static library with PIC, default ON The static library must be compiled with POSITION_INDEPENDENT_CODE=ON in order to link it into shared libraries. But some compilers cannot generate position independent code for static libraries. To solve for both use cases, this commit adds a new CMake option `ENABLE_STATIC_LIB_IS_PIC` that is ON by default. You may need to build with `-D ENABLE_STATIC_LIB_IS_PIC=OFF` if your compiler fails to generate position independent code when compiling the static library. Resolves: https://gitlab.com/bzip2/bzip2/-/issues/47 --- CMakeLists.txt | 11 ++++++++++- CMakeOptions.txt | 2 ++ COMPILING.md | 10 ++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6f488c2..c4b0b6e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -305,8 +305,10 @@ if(ENABLE_SHARED_LIB) ${CMAKE_CURRENT_SOURCE_DIR}/libbz2.def PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/bzlib_private.h INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/bzlib.h) + # Always use '-fPIC'/'-fPIE' option for shared libraries. set_property(TARGET bz2 PROPERTY POSITION_INDEPENDENT_CODE ON) + set_target_properties(bz2 PROPERTIES COMPILE_FLAGS "${WARNCFLAGS}" VERSION ${LT_VERSION} SOVERSION ${LT_SOVERSION}) @@ -321,7 +323,7 @@ if(ENABLE_SHARED_LIB) add_library(bz2_old_soname SHARED ${BZ2_RES}) target_sources(bz2_old_soname PRIVATE ${BZ2_SOURCES} - ${CMAKE_CURRENT_SOURCE_DIR}/libbz2.def + ${CMAKE_CURRENT_SOURCE_DIR}/libbz2.def PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/bzlib_private.h INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/bzlib.h ) @@ -342,6 +344,13 @@ if(ENABLE_STATIC_LIB) PRIVATE ${BZ2_SOURCES} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/bzlib_private.h INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/bzlib.h) + + # Use '-fPIC'/'-fPIE' option for static libraries by default. + # You may build with ENABLE_STATIC_LIB_IS_PIC=OFF to disable PIC for the static library. + if(ENABLE_STATIC_LIB_IS_PIC) + set_property(TARGET bz2_static PROPERTY POSITION_INDEPENDENT_CODE ON) + endif() + set_target_properties(bz2_static PROPERTIES COMPILE_FLAGS "${WARNCFLAGS}" VERSION ${LT_VERSION} diff --git a/CMakeOptions.txt b/CMakeOptions.txt index 76ad6ce..9f0cbc0 100644 --- a/CMakeOptions.txt +++ b/CMakeOptions.txt @@ -23,3 +23,5 @@ option(ENABLE_STATIC_LIB "Build libbz2 in static mode also") option(ENABLE_SHARED_LIB "Build libbz2 as a shared library" ON) option(USE_OLD_SONAME "Use libbz2.so.1.0 for compatibility with old Makefiles" OFF) + +option(ENABLE_STATIC_LIB_IS_PIC "Enable position independent code for the static library" ON) diff --git a/COMPILING.md b/COMPILING.md index 3b3fc55..fa33ce4 100644 --- a/COMPILING.md +++ b/COMPILING.md @@ -269,3 +269,13 @@ mkdir build && cd build cmake .. -DUSE_OLD_SONAME=ON cmake --build . ``` + +`ENABLE_STATIC_LIB_IS_PIC`: Default: `ON` +Disabling this option will make it so that building the static library will not +use the '-fPIC'/'-fPIE' compiler options. You may need to disable this option if +your compiler cannot generate position-independent code for your platform. E.g.: +```sh +mkdir build && cd build +cmake .. -DENABLE_STATIC_LIB_IS_PIC=OFF +cmake --build . +```