comparison dep/pugixml/CMakeLists.txt @ 367:8d45d892be88 default tip

*: instead of pugixml, use Qt XML features this means we have one extra Qt dependency though...
author Paper <paper@tflc.us>
date Sun, 17 Nov 2024 22:55:47 -0500
parents 886f66775f31
children
comparison
equal deleted inserted replaced
366:886f66775f31 367:8d45d892be88
1 cmake_minimum_required(VERSION 3.5)
2
3 # Policy configuration; this *MUST* be specified before project is defined
4 if(POLICY CMP0091)
5 cmake_policy(SET CMP0091 NEW) # Enables use of MSVC_RUNTIME_LIBRARY
6 endif()
7
8 project(pugixml VERSION 1.14 LANGUAGES CXX)
9
10 include(CMakePackageConfigHelpers)
11 include(CMakeDependentOption)
12 include(GNUInstallDirs)
13
14 cmake_dependent_option(PUGIXML_USE_VERSIONED_LIBDIR
15 "Use a private subdirectory to install the headers and libraries" OFF
16 "CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR" OFF)
17
18 cmake_dependent_option(PUGIXML_USE_POSTFIX
19 "Use separate postfix for each configuration to make sure you can install multiple build outputs" OFF
20 "CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR" OFF)
21
22 cmake_dependent_option(PUGIXML_STATIC_CRT
23 "Use static MSVC RT libraries" OFF
24 "MSVC" OFF)
25
26 cmake_dependent_option(PUGIXML_BUILD_TESTS
27 "Build pugixml tests" OFF
28 "CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR" OFF)
29
30 # Custom build defines
31 set(PUGIXML_BUILD_DEFINES CACHE STRING "Build defines for custom options")
32 separate_arguments(PUGIXML_BUILD_DEFINES)
33
34 # Technically not needed for this file. This is builtin CMAKE global variable.
35 option(BUILD_SHARED_LIBS "Build shared instead of static library" OFF)
36
37 # Expose option to build PUGIXML as static as well when the global BUILD_SHARED_LIBS variable is set
38 cmake_dependent_option(PUGIXML_BUILD_SHARED_AND_STATIC_LIBS
39 "Build both shared and static libraries" OFF
40 "BUILD_SHARED_LIBS" OFF)
41
42 # Expose options from the pugiconfig.hpp
43 option(PUGIXML_WCHAR_MODE "Enable wchar_t mode" OFF)
44 option(PUGIXML_COMPACT "Enable compact mode" OFF)
45 option(PUGIXML_INSTALL "Enable installation rules" ON)
46
47 # Advanced options from pugiconfig.hpp
48 option(PUGIXML_NO_XPATH "Disable XPath" OFF)
49 option(PUGIXML_NO_STL "Disable STL" OFF)
50 option(PUGIXML_NO_EXCEPTIONS "Disable Exceptions" OFF)
51 mark_as_advanced(PUGIXML_NO_XPATH PUGIXML_NO_STL PUGIXML_NO_EXCEPTIONS)
52
53 set(PUGIXML_PUBLIC_DEFINITIONS
54 $<$<BOOL:${PUGIXML_WCHAR_MODE}>:PUGIXML_WCHAR_MODE>
55 $<$<BOOL:${PUGIXML_COMPACT}>:PUGIXML_COMPACT>
56 $<$<BOOL:${PUGIXML_NO_XPATH}>:PUGIXML_NO_XPATH>
57 $<$<BOOL:${PUGIXML_NO_STL}>:PUGIXML_NO_STL>
58 $<$<BOOL:${PUGIXML_NO_EXCEPTIONS}>:PUGIXML_NO_EXCEPTIONS>)
59
60 # This is used to backport a CMake 3.15 feature, but is also forwards compatible
61 if (NOT DEFINED CMAKE_MSVC_RUNTIME_LIBRARY)
62 set(CMAKE_MSVC_RUNTIME_LIBRARY
63 MultiThreaded$<$<CONFIG:Debug>:Debug>$<$<NOT:$<BOOL:${PUGIXML_STATIC_CRT}>>:DLL>)
64 endif()
65
66 if (NOT DEFINED CMAKE_CXX_STANDARD_REQUIRED)
67 set(CMAKE_CXX_STANDARD_REQUIRED ON)
68 endif()
69
70 if (NOT DEFINED CMAKE_CXX_STANDARD)
71 set(CMAKE_CXX_STANDARD 11)
72 endif()
73
74 if (PUGIXML_USE_POSTFIX)
75 set(CMAKE_RELWITHDEBINFO_POSTFIX _r)
76 set(CMAKE_MINSIZEREL_POSTFIX _m)
77 set(CMAKE_DEBUG_POSTFIX _d)
78 endif()
79
80 if (CMAKE_VERSION VERSION_LESS 3.15)
81 set(msvc-rt $<TARGET_PROPERTY:MSVC_RUNTIME_LIBRARY>)
82
83 set(msvc-rt-mtd-shared $<STREQUAL:${msvc-rt},MultiThreadedDebugDLL>)
84 set(msvc-rt-mtd-static $<STREQUAL:${msvc-rt},MultiThreadedDebug>)
85 set(msvc-rt-mt-shared $<STREQUAL:${msvc-rt},MultiThreadedDLL>)
86 set(msvc-rt-mt-static $<STREQUAL:${msvc-rt},MultiThreaded>)
87 unset(msvc-rt)
88
89 set(msvc-rt-mtd-shared $<${msvc-rt-mtd-shared}:-MDd>)
90 set(msvc-rt-mtd-static $<${msvc-rt-mtd-static}:-MTd>)
91 set(msvc-rt-mt-shared $<${msvc-rt-mt-shared}:-MD>)
92 set(msvc-rt-mt-static $<${msvc-rt-mt-static}:-MT>)
93 endif()
94
95 set(versioned-dir $<$<BOOL:${PUGIXML_USE_VERSIONED_LIBDIR}>:/pugixml-${PROJECT_VERSION}>)
96
97 set(libs)
98
99 if (BUILD_SHARED_LIBS)
100 add_library(pugixml-shared SHARED
101 ${PROJECT_SOURCE_DIR}/scripts/pugixml_dll.rc
102 ${PROJECT_SOURCE_DIR}/src/pugixml.cpp)
103 add_library(pugixml::shared ALIAS pugixml-shared)
104 list(APPEND libs pugixml-shared)
105 string(CONCAT pugixml.msvc $<OR:
106 $<STREQUAL:${CMAKE_CXX_COMPILER_FRONTEND_VARIANT},MSVC>,
107 $<CXX_COMPILER_ID:MSVC>
108 >)
109
110 set_property(TARGET pugixml-shared PROPERTY EXPORT_NAME shared)
111 target_include_directories(pugixml-shared
112 PUBLIC
113 $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/src>)
114 target_compile_definitions(pugixml-shared
115 PUBLIC
116 ${PUGIXML_BUILD_DEFINES}
117 ${PUGIXML_PUBLIC_DEFINITIONS}
118 PRIVATE
119 PUGIXML_API=$<IF:${pugixml.msvc},__declspec\(dllexport\),__attribute__\(\(visibility\("default"\)\)\)>
120 )
121 target_compile_options(pugixml-shared
122 PRIVATE
123 ${msvc-rt-mtd-shared}
124 ${msvc-rt-mtd-static}
125 ${msvc-rt-mt-shared}
126 ${msvc-rt-mt-static})
127 endif()
128
129 if (NOT BUILD_SHARED_LIBS OR PUGIXML_BUILD_SHARED_AND_STATIC_LIBS)
130 add_library(pugixml-static STATIC
131 ${PROJECT_SOURCE_DIR}/src/pugixml.cpp)
132 add_library(pugixml::static ALIAS pugixml-static)
133 list(APPEND libs pugixml-static)
134
135 set_property(TARGET pugixml-static PROPERTY EXPORT_NAME static)
136 target_include_directories(pugixml-static
137 PUBLIC
138 $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/src>)
139 target_compile_definitions(pugixml-static
140 PUBLIC
141 ${PUGIXML_BUILD_DEFINES}
142 ${PUGIXML_PUBLIC_DEFINITIONS})
143 target_compile_options(pugixml-static
144 PRIVATE
145 ${msvc-rt-mtd-shared}
146 ${msvc-rt-mtd-static}
147 ${msvc-rt-mt-shared}
148 ${msvc-rt-mt-static})
149 endif()
150
151 if (BUILD_SHARED_LIBS)
152 set(pugixml-alias pugixml-shared)
153 else()
154 set(pugixml-alias pugixml-static)
155 endif()
156 add_library(pugixml INTERFACE)
157 target_link_libraries(pugixml INTERFACE ${pugixml-alias})
158 add_library(pugixml::pugixml ALIAS pugixml)
159
160 set_target_properties(${libs}
161 PROPERTIES
162 MSVC_RUNTIME_LIBRARY ${CMAKE_MSVC_RUNTIME_LIBRARY}
163 EXCLUDE_FROM_ALL ON
164 POSITION_INDEPENDENT_CODE ON
165 SOVERSION ${PROJECT_VERSION_MAJOR}
166 VERSION ${PROJECT_VERSION}
167 OUTPUT_NAME pugixml)
168
169 set_target_properties(${libs}
170 PROPERTIES
171 EXCLUDE_FROM_ALL OFF)
172 set(install-targets pugixml ${libs})
173
174 configure_package_config_file(
175 "${PROJECT_SOURCE_DIR}/scripts/pugixml-config.cmake.in"
176 "${PROJECT_BINARY_DIR}/pugixml-config.cmake"
177 INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}
178 NO_CHECK_REQUIRED_COMPONENTS_MACRO
179 NO_SET_AND_CHECK_MACRO)
180
181 write_basic_package_version_file(
182 "${PROJECT_BINARY_DIR}/pugixml-config-version.cmake"
183 COMPATIBILITY SameMajorVersion)
184
185 if (PUGIXML_USE_POSTFIX)
186 if(CMAKE_BUILD_TYPE MATCHES RelWithDebInfo)
187 set(LIB_POSTFIX ${CMAKE_RELWITHDEBINFO_POSTFIX})
188 elseif(CMAKE_BUILD_TYPE MATCHES MinSizeRel)
189 set(LIB_POSTFIX ${CMAKE_MINSIZEREL_POSTFIX})
190 elseif(CMAKE_BUILD_TYPE MATCHES Debug)
191 set(LIB_POSTFIX ${CMAKE_DEBUG_POSTFIX})
192 endif()
193 endif()
194
195 # Handle both relative and absolute paths (e.g. NixOS) for a relocatable package
196 if(IS_ABSOLUTE "${CMAKE_INSTALL_INCLUDEDIR}")
197 set(PUGIXML_PC_INCLUDEDIR "${CMAKE_INSTALL_INCLUDEDIR}")
198 else()
199 set(PUGIXML_PC_INCLUDEDIR "\${prefix}/${CMAKE_INSTALL_INCLUDEDIR}")
200 endif()
201 if(IS_ABSOLUTE "${CMAKE_INSTALL_LIBDIR}")
202 set(PUGIXML_PC_LIBDIR "${CMAKE_INSTALL_LIBDIR}")
203 else()
204 set(PUGIXML_PC_LIBDIR "\${exec_prefix}/${CMAKE_INSTALL_LIBDIR}")
205 endif()
206 configure_file(scripts/pugixml.pc.in pugixml.pc @ONLY)
207
208 export(TARGETS ${install-targets}
209 NAMESPACE pugixml::
210 FILE pugixml-targets.cmake)
211
212 if(PUGIXML_INSTALL)
213 if (NOT DEFINED PUGIXML_RUNTIME_COMPONENT)
214 set(PUGIXML_RUNTIME_COMPONENT Runtime)
215 endif()
216
217 if (NOT DEFINED PUGIXML_LIBRARY_COMPONENT)
218 set(PUGIXML_LIBRARY_COMPONENT Library)
219 endif()
220
221 if (NOT DEFINED PUGIXML_DEVELOPMENT_COMPONENT)
222 set(PUGIXML_DEVELOPMENT_COMPONENT Development)
223 endif()
224
225 set(namelink-component)
226 if (NOT CMAKE_VERSION VERSION_LESS 3.12)
227 set(namelink-component NAMELINK_COMPONENT ${PUGIXML_DEVELOPMENT_COMPONENT})
228 endif()
229 install(TARGETS ${install-targets}
230 EXPORT pugixml-targets
231 RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT ${PUGIXML_RUNTIME_COMPONENT}
232 LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT ${PUGIXML_LIBRARY_COMPONENT} ${namelink-component}
233 ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT ${PUGIXML_DEVELOPMENT_COMPONENT}
234 INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}${versioned-dir})
235
236 install(EXPORT pugixml-targets
237 NAMESPACE pugixml::
238 DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/pugixml COMPONENT ${PUGIXML_DEVELOPMENT_COMPONENT})
239
240 install(FILES
241 "${PROJECT_BINARY_DIR}/pugixml-config-version.cmake"
242 "${PROJECT_BINARY_DIR}/pugixml-config.cmake"
243 DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/pugixml COMPONENT ${PUGIXML_DEVELOPMENT_COMPONENT})
244
245 install(FILES ${PROJECT_BINARY_DIR}/pugixml.pc
246 DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig COMPONENT ${PUGIXML_DEVELOPMENT_COMPONENT})
247
248 install(
249 FILES
250 "${PROJECT_SOURCE_DIR}/src/pugiconfig.hpp"
251 "${PROJECT_SOURCE_DIR}/src/pugixml.hpp"
252 DESTINATION
253 ${CMAKE_INSTALL_INCLUDEDIR}${versioned-dir} COMPONENT ${PUGIXML_DEVELOPMENT_COMPONENT})
254 endif()
255
256 if (PUGIXML_BUILD_TESTS)
257 include(CTest)
258 set(fuzz-pattern "tests/fuzz_*.cpp")
259 set(test-pattern "tests/*.cpp")
260 if (CMAKE_VERSION VERSION_GREATER 3.11)
261 list(INSERT fuzz-pattern 0 CONFIGURE_DEPENDS)
262 list(INSERT test-pattern 0 CONFIGURE_DEPENDS)
263 endif()
264 file(GLOB test-sources ${test-pattern})
265 file(GLOB fuzz-sources ${fuzz-pattern})
266 list(REMOVE_ITEM test-sources ${fuzz-sources})
267
268 add_custom_target(check
269 COMMAND ${CMAKE_CTEST_COMMAND} --output-on-failure)
270
271 add_executable(pugixml-check ${test-sources})
272 add_test(NAME pugixml::test
273 COMMAND pugixml-check
274 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR})
275 add_dependencies(check pugixml-check)
276 target_link_libraries(pugixml-check
277 PRIVATE
278 pugixml::pugixml)
279 endif()