cmlib/CMLibCommonLibraryTarget.cmake

49 lines
1.9 KiB
CMake

# Создание статической и динамической библиотеки из одинаковых объектных файлов
function(add_common_library)
set(options)
set(oneValueArgs TARGET OUTPUT_NAME)
set(multiValueArgs SOURCES)
cmake_parse_arguments(_LOCAL "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
set(LIBNAME ${_LOCAL_TARGET})
set(SOURCES ${_LOCAL_SOURCES})
if(_LOCAL_OUTPUT_NAME)
set(OUTNAME ${_LOCAL_OUTPUT_NAME})
else()
set(OUTNAME ${LIBNAME})
endif()
add_library(${LIBNAME} OBJECT ${SOURCES})
target_include_directories(
${LIBNAME}
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<BUILD_INTERFACE:${CMAKE_BINARY_DIR}/include>
$<INSTALL_INTERFACE:include/${LIBNAME}>)
set_target_properties(${LIBNAME} PROPERTIES POSITION_INDEPENDENT_CODE ON)
if(BUILD_SHARED_LIBS)
add_library(${LIBNAME}_shared SHARED $<TARGET_OBJECTS:${LIBNAME}>)
set_target_properties(
${LIBNAME}_shared
PROPERTIES
VERSION ${PROJECT_VERSION} SOVERSION ${PROJECT_VERSION_MAJOR} OUTPUT_NAME
${OUTNAME}
LIBRARY_OUTPUT_DIRECTORY ${CMAKE_INSTALL_LIBDIR})
install(TARGETS ${LIBNAME}_shared LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
endif()
if(NOT SKIP_STATIC_LIBS)
add_library(${LIBNAME}_static STATIC $<TARGET_OBJECTS:${LIBNAME}>)
set_target_properties(
${LIBNAME}_static PROPERTIES OUTPUT_NAME ${OUTNAME} ARCHIVE_OUTPUT_DIRECTORY
${CMAKE_INSTALL_LIBDIR})
install(TARGETS ${LIBNAME}_static ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
endif()
configure_file(${CMLIB_MODULE_DIR}/pc/lib.pc.in ${CMAKE_BINARY_DIR}/${LIBNAME}.pc)
install(FILES ${CMAKE_BINARY_DIR}/${LIBNAME}.pc
DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
endfunction()