diff --git a/CMakeLists.txt b/CMakeLists.txt index 9d99d860..2f32b284 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -47,22 +47,25 @@ option(XMLWRAPP_DOC "Build and install the documentation (requires Doxygen)" ${P option(XMLWRAPP_EXAMPLES "Build examples" ${PROJECT_IS_TOP_LEVEL}) option(XMLWRAPP_TESTS "Build tests" ${PROJECT_IS_TOP_LEVEL}) -# This is always disabled but can be disabled by the super-project if it is -# not needed before calling add_subdirectory(). +# This is always enabled but can be disabled by the super-project if it is not +# needed before calling add_subdirectory(). option(XMLWRAPP_WITH_LIBXSLT "Build libxsltwrapp library" ON) +# These options are not set by default but can be used to customize the naming +# of the produced libraries. +set(XMLWRAPP_NAME_PREFIX "" CACHE STRING "Use the given prefix for the libraries") +set(XMLWRAPP_NAME_SUFFIX "" CACHE STRING "Use the given suffix for the libraries") + include(GNUInstallDirs) set(INSTALL_CMAKE_DIR ${CMAKE_INSTALL_LIBDIR}/cmake/xmlwrapp) -set(CMAKE_CXX_COMPILER_VISIBILITY_PRESET hidden) - find_package(PkgConfig) -pkg_search_module(LIBXML2 REQUIRED libxml-2.0 >= 2.4.28) +pkg_check_modules(LIBXML2 REQUIRED libxml-2.0>=2.4.28) if( XMLWRAPP_WITH_LIBXSLT ) - pkg_search_module(LIBXSLT REQUIRED libxslt >= 1.1.6) - pkg_search_module(LIBEXSLT REQUIRED libexslt) + pkg_check_modules(LIBXSLT REQUIRED libxslt>=1.1.6) + pkg_check_modules(LIBEXSLT REQUIRED libexslt) endif( XMLWRAPP_WITH_LIBXSLT ) @@ -189,7 +192,14 @@ add_custom_target(distcheck VERBATIM ) -# Give build summary at the end. +# Give build summary at the end but only if it's the first time we're running, +# as indicated by XMLWRAPP_SUMMARY not being cached yet, or if it has changed. +set(XMLWRAPP_SUMMARY_NOW + "${XMLWRAPP_VERSION}-${XMLWRAPP_LIB_TYPE}-${CMAKE_BUILD_TYPE}-${XMLWRAPP_WITH_LIBXSLT}-${XMLWRAPP_DOC}-${XMLWRAPP_EXAMPLES}-${XMLWRAPP_TESTS}" +) +if(NOT "${XMLWRAPP_SUMMARY_NOW}" STREQUAL "$CACHE{XMLWRAPP_SUMMARY}") +set(XMLWRAPP_SUMMARY ${XMLWRAPP_SUMMARY_NOW} CACHE INTERNAL "xmlwrapp internal build summary") + if (CMAKE_BUILD_TYPE) set(XMLWRAPP_CONFIG_DESCRIPTION " in ${CMAKE_BUILD_TYPE} configuration") endif() @@ -198,3 +208,5 @@ message(STATUS " Build xsltwrapp: ${XMLWRAPP_WITH_LIBXSLT}") message(STATUS " Documentation: ${XMLWRAPP_DOC}") message(STATUS " Examples: ${XMLWRAPP_EXAMPLES}") message(STATUS " Tests: ${XMLWRAPP_TESTS}") + +endif() diff --git a/build/cmake/XmlwrappConfig.cmake.in b/build/cmake/XmlwrappConfig.cmake.in index eddbbd9e..099082f1 100644 --- a/build/cmake/XmlwrappConfig.cmake.in +++ b/build/cmake/XmlwrappConfig.cmake.in @@ -31,6 +31,15 @@ endif(XMLWRAPP_XSLT_FOUND) # Our library dependencies (contains definitions form IMPORTED targets) include("${CMAKE_CURRENT_LIST_DIR}/XmlwrappTargets.cmake") +# Create alias targets if they don't exist yet (normally they should not, but +# if find_package() is somehow used twice, this might happen). +if(NOT TARGET xmlwrapp::xmlwrapp) + add_library(xmlwrapp::xmlwrapp ALIAS xmlwrapp) +endif() +if(XMLWRAPP_XSLT_FOUND AND NOT TARGET xmlwrapp::xsltwrapp) + add_library(xmlwrapp::xsltwrapp ALIAS xsltwrapp) +endif() + # These are imported targets created by XmlwrappTargets.cmake set(XMLWRAPP_XML_LIBRARIES xmlwrapp) if(XMLWRAPP_XSLT_FOUND) diff --git a/examples/cmake/add_subdirectory/CMakeLists.txt b/examples/cmake/add_subdirectory/CMakeLists.txt index f825e2c7..3aeebf8c 100644 --- a/examples/cmake/add_subdirectory/CMakeLists.txt +++ b/examples/cmake/add_subdirectory/CMakeLists.txt @@ -43,4 +43,4 @@ set(XMLWRAPP_WITH_LIBXSLT OFF) add_subdirectory(../../.. ${CMAKE_CURRENT_BINARY_DIR}/xmlwrapp) add_executable(example example.cpp) -target_link_libraries(example PRIVATE xmlwrapp) +target_link_libraries(example PRIVATE xmlwrapp::xmlwrapp) diff --git a/examples/cmake/find_package/CMakeLists.txt b/examples/cmake/find_package/CMakeLists.txt index 647548ca..dbb299d3 100644 --- a/examples/cmake/find_package/CMakeLists.txt +++ b/examples/cmake/find_package/CMakeLists.txt @@ -38,4 +38,4 @@ project(XmlwrappFindPackageExample find_package(Xmlwrapp 0.10 REQUIRED) add_executable(example example.cpp) -target_link_libraries(example PRIVATE xmlwrapp) +target_link_libraries(example PRIVATE xmlwrapp::xmlwrapp) diff --git a/include/xmlwrapp/version.h b/include/xmlwrapp/version.h index 1bac73ed..a304d50b 100644 --- a/include/xmlwrapp/version.h +++ b/include/xmlwrapp/version.h @@ -39,6 +39,8 @@ #ifndef _xmlwrapp_version_h_ #define _xmlwrapp_version_h_ +#include "xmlwrapp/export.h" + /** Compile-time major version of the library. @@ -118,7 +120,7 @@ namespace xml @since 0.10.0 */ -int get_major_version(); +XMLWRAPP_API int get_major_version(); /** Return minor runtime version of xmlwrapp library. @@ -127,7 +129,7 @@ int get_major_version(); @since 0.10.0 */ -int get_minor_version(); +XMLWRAPP_API int get_minor_version(); /** Return micro runtime version of xmlwrapp library. @@ -136,7 +138,7 @@ int get_minor_version(); @since 0.10.0 */ -int get_micro_version(); +XMLWRAPP_API int get_micro_version(); /** Return the full runtime version of xmlwrapp library. @@ -147,7 +149,7 @@ int get_micro_version(); @since 0.10.0 */ -const char* get_version_string(); +XMLWRAPP_API const char* get_version_string(); /** Check that the library version is at least the given one. @@ -159,7 +161,7 @@ const char* get_version_string(); @since 0.10.0 */ -bool check_version(int major, int minor, int micro); +XMLWRAPP_API bool check_version(int major, int minor, int micro); } // namespace xml diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 5cc0e9a5..059ac99e 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,3 +1,44 @@ +# Configure shared library with the given name and (full) ELF version. +# +# Set properties for using hidden ELF visibility, defining proper DLL-related +# symbols and use $ORIGIN in the runpath. +# +# Note that this does nothing when shared libraries are not used. +function(setup_shared_library target version) + if (XMLWRAPP_SHARED) + # Construct XMLWRAPP_USE_DLL or XSLTWRAPP_USE_DLL symbol name. + string(TOUPPER "${target}_USE_DLL" use_dll_symbol) + target_compile_definitions(${target} + PRIVATE + HAVE_VISIBILITY + INTERFACE + ${use_dll_symbol} + ) + + # Set SOVERSION to the major VERSION component. + string(REPLACE "." ";" version_as_list ${version}) + list(GET version_as_list 0 soversion) + + set(output_name "${target}") + if( XMLWRAPP_NAME_PREFIX ) + set(output_name "${XMLWRAPP_NAME_PREFIX}${output_name}") + endif() + if( XMLWRAPP_NAME_SUFFIX ) + set(output_name "${output_name}${XMLWRAPP_NAME_SUFFIX}") + endif() + + set_target_properties(${target} PROPERTIES + OUTPUT_NAME ${output_name} + BUILD_RPATH_USE_ORIGIN ON + DEFINE_SYMBOL "DLL_EXPORT" + CXX_VISIBILITY_PRESET hidden + VISIBILITY_INLINES_HIDDEN ON + VERSION ${version} + SOVERSION ${soversion} + ) + endif() +endfunction() + add_library(xmlwrapp ${XMLWRAPP_LIB_TYPE}) target_sources(xmlwrapp PRIVATE @@ -41,9 +82,11 @@ target_link_libraries(xmlwrapp PUBLIC ${LIBXML2_LIBRARIES} ) -set_target_properties(xmlwrapp PROPERTIES VERSION 6.0.0 SOVERSION 6) +setup_shared_library(xmlwrapp 6.0.0) install (TARGETS xmlwrapp EXPORT XmlwrappTargets DESTINATION ${CMAKE_INSTALL_LIBDIR}) +add_library(xmlwrapp::xmlwrapp ALIAS xmlwrapp) + if( XMLWRAPP_WITH_LIBXSLT ) add_library(xsltwrapp ${XMLWRAPP_LIB_TYPE}) target_sources(xsltwrapp @@ -71,6 +114,8 @@ if( XMLWRAPP_WITH_LIBXSLT ) ${LIBXSLT_LIBRARIES} ${LIBEXSLT_LIBRARIES} ) - set_target_properties(xsltwrapp PROPERTIES VERSION 4.0.0 SOVERSION 4) + setup_shared_library(xsltwrapp 4.0.0) install (TARGETS xsltwrapp EXPORT XmlwrappTargets DESTINATION ${CMAKE_INSTALL_LIBDIR}) + + add_library(xmlwrapp::xsltwrapp ALIAS xsltwrapp) endif( XMLWRAPP_WITH_LIBXSLT )