Allow user to append custom libs to linker line

Use-case is explained in the documentation. This implementation
takes care that the additional libs actually occur at the end
of the linker line despite the use of imported targets with
INTERFACE_LINK_LIBRARIES. It might still not be perfect but
sufficient for current use-cases.
This commit is contained in:
Martchus 2019-08-24 13:00:32 +02:00
parent 0cbc471743
commit ea2804f147
4 changed files with 58 additions and 0 deletions

View File

@ -31,6 +31,9 @@ if (NOT BUILTIN_TRANSLATIONS)
list(APPEND ALL_FILES ${QM_FILES})
endif ()
# add custom libraries
append_user_defined_additional_libraries()
# add target for building the application
if (ANDROID)
# create a shared library which can be loaded from the Java-side

View File

@ -518,4 +518,43 @@ endif ()
set(BIN_INSTALL_DESTINATION "${CMAKE_INSTALL_PREFIX}/bin")
set(LIB_INSTALL_DESTINATION "${CMAKE_INSTALL_PREFIX}/lib${SELECTED_LIB_SUFFIX}")
# allow user to specify additional libraries to link against (see buildvariables.md for details)
set(USER_DEFINED_ADDITIONAL_LIBRARIES
""
CACHE STRING
"specifies additional libraries to link against (added after any other libraries to the linker line)")
function (append_user_defined_additional_libraries)
if (NOT USER_DEFINED_ADDITIONAL_LIBRARIES)
return()
endif ()
# find the last library
set(LIBS PRIVATE_LIBRARIES)
list(LENGTH ${LIBS} LIB_COUNT)
if (LIB_COUNT LESS_EQUAL 0)
set(LIBS PUBLIC_LIBRARIES)
list(LENGTH ${LIBS} LIB_COUNT)
endif ()
if (LIB_COUNT LESS_EQUAL 0)
# just add the addiitional libs to PRIVATE_LIBRARIES if there are no libs yet anyways
set(PRIVATE_LIBRARIES "${USER_DEFINED_ADDITIONAL_LIBRARIES}" PARENT_SCOPE)
endif ()
math(EXPR LAST_LIB_INDEX "${LIB_COUNT} - 1")
list(GET ${LIBS} ${LAST_LIB_INDEX} LAST_LIB)
# add the additional libs as INTERFACE_LINK_LIBRARIES of the last lib if it is a target
if (TARGET "${LAST_LIB}")
# note: Otherwise the INTERFACE_LINK_LIBRARIES of the last target might still come after
# the USER_DEFINED_ADDITIONAL_LIBRARIES on the linker line.
set_property(TARGET "${LAST_LIB}"
APPEND PROPERTY
INTERFACE_LINK_LIBRARIES ${USER_DEFINED_ADDITIONAL_LIBRARIES})
return()
endif ()
# fall back to simply append the library to PRIVATE_LIBRARIES
set(PRIVATE_LIBRARIES "${USER_DEFINED_ADDITIONAL_LIBRARIES}" PARENT_SCOPE)
endfunction ()
set(BASIC_PROJECT_CONFIG_DONE YES)

View File

@ -109,6 +109,9 @@ else ()
set(META_LIBRARY_TYPE STATIC)
endif ()
# add custom libraries
append_user_defined_additional_libraries()
# add library to be created, set libs to link against, set version and C++ standard
if (META_HEADER_ONLY_LIB)
add_library(${META_TARGET_NAME} INTERFACE)

View File

@ -90,6 +90,19 @@ None of these are enabled or set by default, unless stated otherwise.
* Disabing this is required when building for MacOS and Android at the time of
writing this documentation.
* Bash completion will not be able to suggest files and directory when disabled.
* `USER_DEFINED_ADDITIONAL_LIBRARIES`: specifies additional libraries to link against
(added after any other libraries to the linker line)
* Use case: Some platforms/standard libraries/compilers require linking against
additional libraries for certain features. Adding additional libraries for
`std::filesystem` is covered by use_standard_filesystem(). However, it makes no
sense to add such a function for every specific platform/feature. For instance
it seems to be required to add "-latomic" to the linker line for
armv7-none-linux-androideabi/libc++/clang (not for aarch64-none-linux-androidabi)
but it is better to cover such details on packaging-level and only allow to pass
such flags here.
* Using `CMAKE_EXE_LINKER_FLAGS` or `CMAKE_SHARED_LINKER_FLAGS` is often not helpful
because the additional flags need to be added at the end of the linker line most
of the time.
#### Variables for specifying location of 3rd party dependencies
The build script tries to find the required dependencies at standard loctions