make pugixml build static and shared via cmake (#257)

This commit is contained in:
basti171 2019-02-06 16:39:23 +01:00 committed by Arseny Kapoulkine
parent 160c1c23d9
commit 1d59e4d9d8

View file

@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 3.0)
project(pugixml VERSION 1.9)
option(BUILD_SHARED_AND_STATIC_LIBS "Build both shared and static libraries" OFF)
option(BUILD_SHARED_LIBS "Build shared instead of static library" OFF)
option(BUILD_TESTS "Build tests" OFF)
option(USE_VERSIONED_LIBDIR "Use a private subdirectory to install the headers and libs" OFF)
@ -37,41 +38,86 @@ if(DEFINED BUILD_DEFINES)
endforeach()
endif()
if(BUILD_SHARED_LIBS)
add_library(pugixml SHARED ${HEADERS} ${SOURCES})
if(BUILD_SHARED_AND_STATIC_LIBS)
add_library(pugixml-static SHARED ${HEADERS} ${SOURCES})
add_library(pugixml-shared STATIC ${HEADERS} ${SOURCES})
else()
add_library(pugixml STATIC ${HEADERS} ${SOURCES})
if(BUILD_SHARED_LIBS)
add_library(pugixml SHARED ${HEADERS} ${SOURCES})
else()
add_library(pugixml STATIC ${HEADERS} ${SOURCES})
endif()
endif()
# Export symbols for shared library builds
if(BUILD_SHARED_AND_STATIC_LIBS AND MSVC)
target_compile_definitions(pugixml-shared PRIVATE "PUGIXML_API=__declspec(dllexport)")
endif()
if(BUILD_SHARED_LIBS AND MSVC)
target_compile_definitions(pugixml PRIVATE "PUGIXML_API=__declspec(dllexport)")
endif()
# Enable C++11 long long for compilers that are capable of it
if(NOT ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION} STRLESS 3.1 AND ";${CMAKE_CXX_COMPILE_FEATURES};" MATCHES ";cxx_long_long_type;")
target_compile_features(pugixml PUBLIC cxx_long_long_type)
if(BUILD_SHARED_AND_STATIC_LIBS)
target_compile_features(pugixml-shared PUBLIC cxx_long_long_type)
target_compile_features(pugixml-static PUBLIC cxx_long_long_type)
else()
target_compile_features(pugixml PUBLIC cxx_long_long_type)
endif()
endif()
set_target_properties(pugixml PROPERTIES VERSION ${PROJECT_VERSION} SOVERSION ${PROJECT_VERSION_MAJOR})
if(BUILD_SHARED_AND_STATIC_LIBS)
set_target_properties(pugixml-static PROPERTIES VERSION ${PROJECT_VERSION} SOVERSION ${PROJECT_VERSION_MAJOR})
set_target_properties(pugixml-shared PROPERTIES VERSION ${PROJECT_VERSION} SOVERSION ${PROJECT_VERSION_MAJOR})
else()
set_target_properties(pugixml PROPERTIES VERSION ${PROJECT_VERSION} SOVERSION ${PROJECT_VERSION_MAJOR})
endif()
if(USE_VERSIONED_LIBDIR)
# Install library into its own directory under LIBDIR
set(INSTALL_SUFFIX /pugixml-${pugixml_VERSION})
endif()
target_include_directories(pugixml PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/src>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}${INSTALL_SUFFIX}>)
if(USE_POSTFIX AND CMAKE_CONFIGURATION_TYPES)
set_target_properties(pugixml PROPERTIES DEBUG_POSTFIX "_d" MINSIZEREL_POSTFIX "_m" RELWITHDEBINFO_POSTFIX "_r")
if(BUILD_SHARED_AND_STATIC_LIBS)
target_include_directories(pugixml-static PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/src>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}${INSTALL_SUFFIX}>)
target_include_directories(pugixml-shared PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/src>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}${INSTALL_SUFFIX}>)
else()
target_include_directories(pugixml PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/src>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}${INSTALL_SUFFIX}>)
endif()
if(USE_POSTFIX AND CMAKE_CONFIGURATION_TYPES)
if(BUILD_SHARED_AND_STATIC_LIBS)
set_target_properties(pugixml-static PROPERTIES DEBUG_POSTFIX "_d" MINSIZEREL_POSTFIX "_m" RELWITHDEBINFO_POSTFIX "_r")
set_target_properties(pugixml-shared PROPERTIES DEBUG_POSTFIX "_d" MINSIZEREL_POSTFIX "_m" RELWITHDEBINFO_POSTFIX "_r")
else()
set_target_properties(pugixml PROPERTIES DEBUG_POSTFIX "_d" MINSIZEREL_POSTFIX "_m" RELWITHDEBINFO_POSTFIX "_r")
endif()
endif()
if(BUILD_SHARED_AND_STATIC_LIBS)
install(TARGETS pugixml-static EXPORT pugixml-config
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}${INSTALL_SUFFIX}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}${INSTALL_SUFFIX}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
install(TARGETS pugixml-shared EXPORT pugixml-config
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}${INSTALL_SUFFIX}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}${INSTALL_SUFFIX}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
else()
install(TARGETS pugixml EXPORT pugixml-config
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}${INSTALL_SUFFIX}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}${INSTALL_SUFFIX}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
endif()
install(TARGETS pugixml EXPORT pugixml-config
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}${INSTALL_SUFFIX}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}${INSTALL_SUFFIX}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
install(FILES ${HEADERS} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}${INSTALL_SUFFIX})
install(EXPORT pugixml-config DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/pugixml)
@ -84,6 +130,11 @@ if(BUILD_TESTS)
list(REMOVE_ITEM TEST_SOURCES ${FUZZ_SOURCES})
add_executable(check ${TEST_SOURCES})
target_link_libraries(check pugixml)
if(BUILD_SHARED_AND_STATIC_LIBS)
target_link_libraries(check pugixml-shared)
target_link_libraries(check pugixml-static)
else()
target_link_libraries(check pugixml)
endif()
add_custom_command(TARGET check POST_BUILD COMMAND check WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
endif()