CMakeLists.txt 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. cmake_minimum_required(VERSION 3.10)
  2. project(pugixml)
  3. option(BUILD_SHARED_LIBS "Build shared instead of static library" OFF)
  4. option(BUILD_TESTS "Build tests" OFF)
  5. option(BUILD_PKGCONFIG "Build in PKGCONFIG mode" OFF)
  6. set(BUILD_DEFINES "" CACHE STRING "Build defines")
  7. if(MSVC)
  8. option(STATIC_CRT "Use static CRT libraries" OFF)
  9. # Rewrite command line flags to use /MT if necessary
  10. if(STATIC_CRT)
  11. foreach(flag_var
  12. CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
  13. CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
  14. if(${flag_var} MATCHES "/MD")
  15. string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
  16. endif(${flag_var} MATCHES "/MD")
  17. endforeach(flag_var)
  18. endif()
  19. endif()
  20. # Pre-defines standard install locations on *nix systems.
  21. include(GNUInstallDirs)
  22. mark_as_advanced(CLEAR CMAKE_INSTALL_LIBDIR CMAKE_INSTALL_INCLUDEDIR)
  23. set(HEADERS src/pugixml.hpp src/pugiconfig.hpp)
  24. set(SOURCES src/pugixml.cpp)
  25. if(DEFINED BUILD_DEFINES)
  26. foreach(DEFINE ${BUILD_DEFINES})
  27. add_definitions("-D" ${DEFINE})
  28. endforeach()
  29. endif()
  30. #message(pugixml" "${BUILD_SHARED_LIBS})
  31. #if(BUILD_SHARED_LIBS)
  32. # add_library(pugixml SHARED ${HEADERS} ${SOURCES})
  33. #else()
  34. add_library(pugixml STATIC ${HEADERS} ${SOURCES})
  35. #endif()
  36. # Export symbols for shared library builds
  37. if(BUILD_SHARED_LIBS AND MSVC)
  38. target_compile_definitions(pugixml PRIVATE "PUGIXML_API=__declspec(dllexport)")
  39. endif()
  40. # Enable C++11 long long for compilers that are capable of it
  41. if(NOT ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION} STRLESS 3.1 AND ";${CMAKE_CXX_COMPILE_FEATURES};" MATCHES ";cxx_long_long_type;")
  42. target_compile_features(pugixml PUBLIC cxx_long_long_type)
  43. endif()
  44. set_target_properties(pugixml PROPERTIES VERSION 1.9 SOVERSION 1)
  45. get_target_property(PUGIXML_VERSION_STRING pugixml VERSION)
  46. if(BUILD_PKGCONFIG)
  47. # Install library into its own directory under LIBDIR
  48. set(INSTALL_SUFFIX /pugixml-${PUGIXML_VERSION_STRING})
  49. endif()
  50. target_include_directories(pugixml PUBLIC
  51. $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/src>
  52. $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}${INSTALL_SUFFIX}>)
  53. install(TARGETS pugixml EXPORT pugixml-config
  54. ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}${INSTALL_SUFFIX}
  55. LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}${INSTALL_SUFFIX}
  56. RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
  57. install(FILES ${HEADERS} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}${INSTALL_SUFFIX})
  58. install(EXPORT pugixml-config DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/pugixml)
  59. if(BUILD_PKGCONFIG)
  60. configure_file(scripts/pugixml.pc.in ${PROJECT_BINARY_DIR}/pugixml.pc @ONLY)
  61. install(FILES ${PROJECT_BINARY_DIR}/pugixml.pc DESTINATION ${CMAKE_INSTALL_PREFIX}/lib/pkgconfig)
  62. endif()
  63. if(BUILD_TESTS)
  64. file(GLOB TEST_SOURCES tests/*.cpp)
  65. file(GLOB FUZZ_SOURCES tests/fuzz_*.cpp)
  66. list(REMOVE_ITEM TEST_SOURCES ${FUZZ_SOURCES})
  67. add_executable(check ${TEST_SOURCES})
  68. target_link_libraries(check pugixml)
  69. add_custom_command(TARGET check POST_BUILD COMMAND check WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
  70. endif()