diff --git a/CMakeLists.txt b/CMakeLists.txt index 3033e8d9..e8cb5654 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,7 +15,6 @@ # You should have received a copy of the GNU General Public License along with # dwarfs. If not, see . # -# project(dwarfs) include(ExternalProject) @@ -272,7 +271,7 @@ endif() add_library(dwarfs ${LIBDWARFS_SRC}) if(STATIC_BUILD_DO_NOT_USE) - add_link_options(-static -static-libgcc) + add_link_options(-static -static-libgcc) endif(STATIC_BUILD_DO_NOT_USE) add_executable(mkdwarfs src/mkdwarfs.cpp) @@ -593,14 +592,10 @@ target_link_libraries( PkgConfig::LIBLZ4 PkgConfig::LIBLZMA) - if(NOT STATIC_BUILD_DO_NOT_USE) -target_link_libraries( - dwarfs - PkgConfig::LIBARCHIVE) + target_link_libraries(dwarfs PkgConfig::LIBARCHIVE) endif(NOT STATIC_BUILD_DO_NOT_USE) - if(ZSTD_FOUND AND PREFER_SYSTEM_ZSTD) target_link_libraries(dwarfs PkgConfig::ZSTD) else() @@ -632,93 +627,122 @@ foreach(tgt ${BINARY_TARGETS}) endforeach() if(STATIC_BUILD_DO_NOT_USE) -# ................................................................... -# libarchive is the module that creates the real pain here -# Its share version looks around to find other shared libraries -# which can do the real work. For example, if liblzma.so is found then -# libarchive supports lzma encoding -# Static libary is compiled against predefined set of static archive -# libraries at the time when binary distribution created. This is not -# necessarily the set of libraries which is present at the system where -# this script is executed. -# There are two options to fix it: -# 1) Build local version of libarchive.a -# 2) Use LIBARCHIVE_STATIC_LIBRARIES and LIBARCHIVE_STATIC_LDFLAGS -# that are set by pkg_check_modules(LIBARCHIVE IMPORTED_TARGET libarchive>=3.1.2) -# Method #1 is implemented here. This implementation is not pedantic - -# it uses headers from system libarchive package but libararies from local build -# Local build is based on default settings for v3.5.1 with xml and iconv support -# turned off in order to reduce that set of libraries for futher linkage -# ................................................................... + # ................................................................... + # libarchive is the module that creates the real pain here Its share version + # looks around to find other shared libraries which can do the real work. For + # example, if liblzma.so is found then libarchive supports lzma encoding + # Static libary is compiled against predefined set of static archive libraries + # at the time when binary distribution created. This is not necessarily the + # set of libraries which is present at the system where this script is + # executed. There are two options to fix it: 1) Build local version of + # libarchive.a 2) Use LIBARCHIVE_STATIC_LIBRARIES and + # LIBARCHIVE_STATIC_LDFLAGS that are set by pkg_check_modules(LIBARCHIVE + # IMPORTED_TARGET libarchive>=3.1.2) Method #1 is implemented here. This + # implementation is not pedantic - it uses headers from system libarchive + # package but libararies from local build Local build is based on default + # settings for v3.5.1 with xml and iconv support turned off in order to reduce + # that set of libraries for futher linkage + # ................................................................... + set(_LIBARCHIVE_PRJ "libarchive") + set(__LIBARCHIVE "${PROJECT_BINARY_DIR}/libarchive/lib/libarchive.a") - set(_LIBARCHIVE_PRJ "libarchive") - set(__LIBARCHIVE "${PROJECT_BINARY_DIR}/libarchive/lib/libarchive.a") + if(ZSTD_FOUND AND PREFER_SYSTEM_ZSTD) + set(_LIBARCHIVE_CMAKE_ARGS "") + else() + set(_LIBARCHIVE_CMAKE_ARGS + -DZSTD_INCLUDE_DIR=${PROJECT_SOURCE_DIR}/zstd/lib + -DCMAKE_LIBRARY_PATH=${PROJECT_BINARY_DIR}/zstd/build/cmake/lib/) + endif() - if(ZSTD_FOUND AND PREFER_SYSTEM_ZSTD) - set(_LIBARCHIVE_CMAKE_ARGS "") - else() - set(_LIBARCHIVE_CMAKE_ARGS -DZSTD_INCLUDE_DIR=${PROJECT_SOURCE_DIR}/zstd/lib -DCMAKE_LIBRARY_PATH=${PROJECT_BINARY_DIR}/zstd/build/cmake/lib/) - endif() - - -ExternalProject_Add(${_LIBARCHIVE_PRJ} - PREFIX "${PROJECT_BINARY_DIR}/libarchive" - GIT_REPOSITORY "https://github.com/libarchive/libarchive.git" - GIT_TAG "v3.5.1" - CMAKE_ARGS ${_LIBARCHIVE_CMAKE_ARGS} -DCMAKE_INSTALL_PREFIX=${PROJECT_BINARY_DIR}/libarchive -DCMAKE_BUILD_TYPE=Release -DENABLE_ICONV:BOOL=OFF -DENABLE_LIBXML2:BOOL=OFF - BUILD_BYPRODUCTS ${__LIBARCHIVE} - TMP_DIR "${PROJECT_BINARY_DIR}/libarchive/tmp" - STAMP_DIR "${PROJECT_BINARY_DIR}/libarchive/stamp" - SOURCE_DIR "${PROJECT_BINARY_DIR}/libarchive/src" - BINARY_DIR "${PROJECT_BINARY_DIR}/libarchive/build" - INSTALL_DIR "" -) + ExternalProject_Add( + ${_LIBARCHIVE_PRJ} + PREFIX "${PROJECT_BINARY_DIR}/libarchive" + GIT_REPOSITORY "https://github.com/libarchive/libarchive.git" + GIT_TAG "v3.5.1" + CMAKE_ARGS ${_LIBARCHIVE_CMAKE_ARGS} + -DCMAKE_INSTALL_PREFIX=${PROJECT_BINARY_DIR}/libarchive + -DCMAKE_BUILD_TYPE=Release + -DENABLE_ICONV:BOOL=OFF + -DENABLE_LIBXML2:BOOL=OFF + BUILD_BYPRODUCTS ${__LIBARCHIVE} + TMP_DIR "${PROJECT_BINARY_DIR}/libarchive/tmp" + STAMP_DIR "${PROJECT_BINARY_DIR}/libarchive/stamp" + SOURCE_DIR "${PROJECT_BINARY_DIR}/libarchive/src" + BINARY_DIR "${PROJECT_BINARY_DIR}/libarchive/build" + INSTALL_DIR "") if(NOT (ZSTD_FOUND AND PREFER_SYSTEM_ZSTD)) add_dependencies(${_LIBARCHIVE_PRJ} libzstd_static) endif() -add_library(_LIBARCHIVE STATIC IMPORTED) -set_target_properties(_LIBARCHIVE PROPERTIES IMPORTED_LOCATION ${__LIBARCHIVE}) -add_dependencies(_LIBARCHIVE ${_LIBARCHIVE_PRJ}) + add_library(static_libarchive STATIC IMPORTED) + set_target_properties(static_libarchive PROPERTIES IMPORTED_LOCATION + ${__LIBARCHIVE}) + add_dependencies(static_libarchive ${_LIBARCHIVE_PRJ}) -# ................................................................... -# Each library name given to the NAMES option is first considered -# as a library file name and then considered with platform-specific -# prefixes (e.g. lib) and suffixes (e.g. .so). -# ................................................................... + # ................................................................... + # Each library name given to the NAMES option is first considered as a library + # file name and then considered with platform-specific prefixes (e.g. lib) and + # suffixes (e.g. .so). + # ................................................................... - find_library(_LIBGLOG "libglog.a" REQUIRED) - find_library(_LIBFMT "libfmt.a" REQUIRED) - find_library(_LIBDC "libdouble-conversion.a" REQUIRED) - find_library(_LIBGFLAGS "libgflags.a" REQUIRED) - find_library(_LIBEVENT "libevent.a" REQUIRED) - find_library(_LIBACL "libacl.a" REQUIRED) - find_library(_LIBXML2 "libxml2.a" REQUIRED) - find_library(_LIBCRYPTO "libcrypto.a" REQUIRED) - find_library(_LIBZ "libz.a" REQUIRED) - find_library(_LIBPTHREAD "libpthread.a" REQUIRED) - find_library(_LIBDL "libdl.a" REQUIRED) - find_library(_LIBC "libc.a" REQUIRED) - find_library(_LIBM "libm.a" REQUIRED) - find_library(_LIBRT "librt.a" REQUIRED) - find_library(_LIBSSL "libssl.a" REQUIRED) - find_library(_LIBUNWIND "libunwind.a" REQUIRED) + function(IMPORT_STATIC_LIB TARGET NAME) + find_library(_TMP_LIB_LOC_${TARGET} ${NAME} NO_CACHE REQUIRED) + add_library(${TARGET} STATIC IMPORTED) + set_target_properties(${TARGET} PROPERTIES IMPORTED_LOCATION + ${_TMP_LIB_LOC_${TARGET}}) + endfunction() + + import_static_lib(static_libglog "libglog.a") + import_static_lib(static_libfmt "libfmt.a") + import_static_lib(static_libdoubleconv "libdouble-conversion.a") + import_static_lib(static_libgflags "libgflags.a") + import_static_lib(static_libevent "libevent.a") + import_static_lib(static_libacl "libacl.a") + import_static_lib(static_libxml2 "libxml2.a") + import_static_lib(static_libcrypto "libcrypto.a") + import_static_lib(static_libz "libz.a") + import_static_lib(static_libpthread "libpthread.a") + import_static_lib(static_libdl "libdl.a") + import_static_lib(static_libc "libc.a") + import_static_lib(static_libm "libm.a") + import_static_lib(static_librt "librt.a") + import_static_lib(static_libssl "libssl.a") + import_static_lib(static_libunwind "libunwind.a") + + set_target_properties(static_libunwind PROPERTIES INTERFACE_LINK_LIBRARIES + PkgConfig::LIBLZMA) + set_target_properties(static_libglog PROPERTIES INTERFACE_LINK_LIBRARIES + static_libgflags) + set_target_properties(static_librt PROPERTIES INTERFACE_LINK_LIBRARIES + static_libgflags) foreach(tgt ${BINARY_TARGETS}) -# ................................................................... -# -static-libgcc above and gcc_eh below is all together -# an ugly trick to enforce static linking -# ................................................................... - target_link_libraries(${tgt} - ${_LIBFMT} ${_LIBDC} ${_LIBGLOG} ${_LIBGFLAGS} - _LIBARCHIVE ${_LIBEVENT} ${_LIBACL} - ${_LIBSSL} ${_LIBCRYPTO} ${_LIBPTHREAD} ${_LIBDL} - ${_LIBZ} ${_LIBC} ${_LIBM} ${_LIBRT} - gcc_eh ${_LIBUNWIND} - ) + # ................................................................... + # -static-libgcc above and gcc_eh below is all together an ugly trick to + # enforce static linking + # ................................................................... + target_link_libraries( + ${tgt} + static_libfmt + static_libdoubleconv + static_libglog + static_libgflags + static_libarchive + static_libevent + static_libacl + static_libssl + static_libcrypto + static_libpthread + static_libdl + static_libz + static_libc + static_libm + static_librt + gcc_eh + static_libunwind) endforeach() endif(STATIC_BUILD_DO_NOT_USE)