Fix static linking for tests, cmake-format everything

Some tests didn't link yet due to gflags init code. Tweaking static
library dependencies a little fixed this.
This commit is contained in:
Marcus Holland-Moritz 2021-08-12 13:55:06 +02:00
parent 58af657fa7
commit 54f6f1b69e

View File

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