Changeset View
Changeset View
Standalone View
Standalone View
cmake/modules/AddLibraryHelper.cmake
- This file was added.
include(InstallationHelper) | |||||
# Wrapper for the add_library() function that allow to require more than a | |||||
# single library type. The type can be STATIC, SHARED or both. | |||||
# | |||||
# LINK_LIBS applies to all the types and follow the syntax of | |||||
# target_link_libraries(). | |||||
# OUTPUT_NAME allow to redefine the library output name. If not set the target | |||||
# name is used. | |||||
# OBJLIBS can be used to add sources from object targets by using the | |||||
# $<TARGET_OBJECTS:target> generator expression. | |||||
# | |||||
# This function makes 2 variables available to the caller space, named after the | |||||
# supplied name suffixed by _static_lib and _shared_lib. These variables contain | |||||
# the actual target name respectively for the static library and the shared | |||||
# library. If both are not build, the corresponding variable is not set. The | |||||
# value of these variables can be tested with `if(TARGET ${foo_static_lib}` to | |||||
# determine if the desired version has been defined and what is the target name. | |||||
function(add_library_with_type NAME) | |||||
cmake_parse_arguments(ARG | |||||
"SHARED;STATIC" | |||||
"OUTPUT_NAME" | |||||
"LINK_LIBS;OBJLIBS" | |||||
${ARGN} | |||||
) | |||||
if(NOT (ARG_STATIC OR ARG_SHARED)) | |||||
message(FATAL_ERROR "Missing library type(s) (STATIC, SHARED or both).") | |||||
endif() | |||||
set(_all_files ${ARG_OBJLIBS} ${ARG_UNPARSED_ARGUMENTS}) | |||||
# If both static and shared are requested, use an object library. | |||||
if(ARG_SHARED AND ARG_STATIC) | |||||
set(_obj_name "${NAME}-object") | |||||
add_library(${_obj_name} OBJECT ${_all_files}) | |||||
target_link_libraries(${_obj_name} ${ARG_LINK_LIBS}) | |||||
set(_all_files "$<TARGET_OBJECTS:${_obj_name}>") | |||||
if(ARG_OUTPUT_NAME) | |||||
set(_output_name OUTPUT_NAME ${ARG_OUTPUT_NAME}) | |||||
else() | |||||
set(_output_name OUTPUT_NAME ${NAME}) | |||||
endif() | |||||
set(_shared_lib_name "${NAME}-shared") | |||||
set(${NAME}_shared_target "${_shared_lib_name}" PARENT_SCOPE) | |||||
add_library_with_type(${_shared_lib_name} SHARED | |||||
OBJLIBS ${_all_files} | |||||
LINK_LIBS ${ARG_LINK_LIBS} | |||||
${_output_name} | |||||
${_shared_lib_target} | |||||
) | |||||
unset(ARG_SHARED) | |||||
endif() | |||||
if(ARG_SHARED) | |||||
add_library(${NAME} SHARED ${_all_files}) | |||||
set(${NAME}_shared_target "${NAME}" PARENT_SCOPE) | |||||
else() | |||||
add_library(${NAME} STATIC ${_all_files}) | |||||
set(${NAME}_static_target "${NAME}" PARENT_SCOPE) | |||||
endif() | |||||
target_link_libraries(${NAME} ${ARG_LINK_LIBS}) | |||||
if(${CMAKE_SYSTEM_NAME} MATCHES "Linux") | |||||
# FIXME For compatibility reason with autotools, the version is set | |||||
# to 0.0.0 (major being actually 0). This is obviously wrong and the | |||||
# version of the library should reflect the version of the release. | |||||
# On platforms other than linux, only the major version (0) is used. | |||||
# Replace the VERSION line with the statement below to set the | |||||
# correct version: | |||||
# set(_properties VERSION "${bitcoin-abc_VERSION}") | |||||
set(_properties VERSION "${bitcoin-abc_VERSION_MAJOR}.0.0") | |||||
else() | |||||
set(_properties VERSION "${bitcoin-abc_VERSION_MAJOR}") | |||||
endif() | |||||
if(ARG_OUTPUT_NAME) | |||||
# For autotools compatibility, rename the library to ${OUTPUT_NAME}-0.dll | |||||
if(${CMAKE_SYSTEM_NAME} MATCHES "Windows") | |||||
list(APPEND _properties OUTPUT_NAME "${ARG_OUTPUT_NAME}-${bitcoin-abc_VERSION_MAJOR}") | |||||
else() | |||||
list(APPEND _properties OUTPUT_NAME "${ARG_OUTPUT_NAME}") | |||||
endif() | |||||
endif() | |||||
if(ARG_SHARED) | |||||
list(APPEND _properties SOVERSION "${bitcoin-abc_VERSION_MAJOR}") | |||||
endif() | |||||
set_target_properties(${NAME} PROPERTIES ${_properties}) | |||||
endfunction() |