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 . +```