| 1 |
cmake_minimum_required(VERSION 3.17.0 FATAL_ERROR) |
| 2 |
|
| 3 |
# prevent multiple inclusion |
| 4 |
if (DEFINED THIRD_PARTY_MODULE_LOADED) |
| 5 |
return() |
| 6 |
endif () |
| 7 |
set(THIRD_PARTY_MODULE_LOADED YES) |
| 8 |
|
| 9 |
include(3rdPartyFunctions) |
| 10 |
|
| 11 |
# skip subsequent configuration if only the function includes are wanted |
| 12 |
if (META_NO_3RDPARTY_CONFIG) |
| 13 |
return() |
| 14 |
endif () |
| 15 |
|
| 16 |
# add options for deciding whether to build/use static or shared libraries |
| 17 |
if (("${META_PROJECT_TYPE}" STREQUAL "library") |
| 18 |
OR ("${META_PROJECT_TYPE}" STREQUAL "plugin") |
| 19 |
OR ("${META_PROJECT_TYPE}" STREQUAL "qtplugin") |
| 20 |
OR ("${META_PROJECT_TYPE}" STREQUAL "")) |
| 21 |
set(META_PROJECT_IS_LIBRARY YES) |
| 22 |
elseif ("${META_PROJECT_TYPE}" STREQUAL "application") |
| 23 |
set(META_PROJECT_IS_APPLICATION YES) |
| 24 |
endif () |
| 25 |
if (META_PROJECT_IS_LIBRARY) |
| 26 |
# when development defaults are enabled, build shared libs by default when targeting GNU/Linux |
| 27 |
set(BUILD_SHARED_LIBS_BY_DEFAULT "${BUILD_SHARED_LIBS}") |
| 28 |
if (ENABLE_DEVEL_DEFAULTS AND CMAKE_SYSTEM_NAME STREQUAL "Linux") |
| 29 |
set(BUILD_SHARED_LIBS_BY_DEFAULT ON) |
| 30 |
endif () |
| 31 |
if (DEFINED CACHE{${META_PROJECT_VARNAME}_BUILD_SHARED_LIBS}) |
| 32 |
# allow overriding BUILD_SHARED_LIBS via a project-specific cache variable |
| 33 |
set(BUILD_SHARED_LIBS "${${META_PROJECT_VARNAME}_BUILD_SHARED_LIBS}") |
| 34 |
else () |
| 35 |
# make BUILD_SHARED_LIBS an overridable cache variable |
| 36 |
option(BUILD_SHARED_LIBS "whether to build shared or static libraries" "${BUILD_SHARED_LIBS_BY_DEFAULT}") |
| 37 |
endif () |
| 38 |
option( |
| 39 |
STATIC_LIBRARY_LINKAGE |
| 40 |
"prefer linking against dependencies statically; adds additional flags for static linkage; only applies when building shared libraries" |
| 41 |
OFF) |
| 42 |
elseif (META_PROJECT_IS_APPLICATION) |
| 43 |
option( |
| 44 |
STATIC_LINKAGE |
| 45 |
"prefer linking against dependencies statically; adds additional flags for static linkage; only applies when building applications" |
| 46 |
OFF) |
| 47 |
endif () |
| 48 |
|
| 49 |
# configure "static linkage" |
| 50 |
if ((STATIC_LINKAGE AND META_PROJECT_IS_APPLICATION) OR (STATIC_LIBRARY_LINKAGE AND META_PROJECT_IS_LIBRARY)) |
| 51 |
set(STATIC_LINKAGE_CONFIGURED ON) |
| 52 |
|
| 53 |
# add options to opt out from linking statically against certain core libraries as it might not work under all platforms |
| 54 |
# (see https://github.com/Martchus/syncthingtray/issues/64) |
| 55 |
if (APPLE OR UNIX) |
| 56 |
set(DEFAUT_NO_STATIC_ENFORCE ON) |
| 57 |
# note: The -static flag is considered completely unsupported under Apple platforms (see |
| 58 |
# https://stackoverflow.com/questions/844819/how-to-static-link-on-os-x) and generally not a good idea to use under |
| 59 |
# GNU/Linux as it leads to linking statically against glibc. |
| 60 |
else () |
| 61 |
set(DEFAULT_NO_STATIC_ENFORCE OFF) |
| 62 |
endif () |
| 63 |
option(NO_STATIC_ENFORCE "prevent enforcing static linkage despite generally aiming for a statically linked build" |
| 64 |
"${DEFAUT_NO_STATIC_ENFORCE}") |
| 65 |
option(NO_STATIC_LIBGCC |
| 66 |
"prevent linking statically against libgcc despite aiming otherwise for a statically linked build" OFF) |
| 67 |
option(NO_STATIC_LIBSTDCXX |
| 68 |
"prevent linking statically against libstdc++ despite aiming otherwise for a statically linked build" OFF) |
| 69 |
|
| 70 |
# add additional linker flags to achieve a fully statically linked build |
| 71 |
set(STATIC_LINKAGE_LINKER_FLAGS) |
| 72 |
if (NOT NO_STATIC_ENFORCE) |
| 73 |
list(APPEND STATIC_LINKAGE_LINKER_FLAGS -static) |
| 74 |
endif () |
| 75 |
if (CMAKE_CXX_COMPILER_ID STREQUAL Clang AND (NOT NO_STATIC_LIBGCC OR NOT NO_STATIC_LIBSTDCXX)) |
| 76 |
list(APPEND STATIC_LINKAGE_LINKER_FLAGS --start-no-unused-arguments) |
| 77 |
endif () |
| 78 |
if (NOT NO_STATIC_LIBGCC) |
| 79 |
list(APPEND STATIC_LINKAGE_LINKER_FLAGS -static-libgcc) |
| 80 |
endif () |
| 81 |
if (NOT NO_STATIC_LIBSTDCXX) |
| 82 |
list(APPEND STATIC_LINKAGE_LINKER_FLAGS -static-libstdc++) |
| 83 |
endif () |
| 84 |
if (CMAKE_CXX_COMPILER_ID STREQUAL Clang AND (NOT NO_STATIC_LIBGCC OR NOT NO_STATIC_LIBSTDCXX)) |
| 85 |
list(APPEND STATIC_LINKAGE_LINKER_FLAGS --end-no-unused-arguments) |
| 86 |
endif () |
| 87 |
if (META_PROJECT_IS_APPLICATION) |
| 88 |
list(APPEND META_ADDITIONAL_LINK_FLAGS ${STATIC_LINKAGE_LINKER_FLAGS}) |
| 89 |
endif () |
| 90 |
list(APPEND META_ADDITIONAL_LINK_FLAGS_TEST_TARGET ${STATIC_LINKAGE_LINKER_FLAGS}) |
| 91 |
|
| 92 |
prefer_static_libraries() |
| 93 |
else () |
| 94 |
set(STATIC_LINKAGE_CONFIGURED OFF) |
| 95 |
endif () |