• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# This file is copyrighted under the BSD-license for buildsystem files of KDE
2# copyright 2010, Patrick Spendrin <ps_ml@gmx.de>
3
4cmake_minimum_required(VERSION 3.1.3)
5
6# This allows controlling documented build time switches
7# when Expat is pulled in using the add_subdirectory function, e.g.
8#
9#   set(EXPAT_BUILD_DOCS OFF)
10#   set(EXPAT_BUILD_TOOLS OFF)
11#   add_subdirectory(${expat_SOURCE_DIR}/expat ${expat_BINARY_DIR})
12#
13# would disable compilation of the xmlwf CLI and its man page.
14# Without activating behaviour NEW for policy CMP0077 here,
15# a user with -Wdev enabled would see warning
16#
17#   Policy CMP0077 is not set: option() honors normal variables.  Run "cmake
18#   --help-policy CMP0077" for policy details.  Use the cmake_policy command to
19#   set the policy and suppress this warning.
20#
21#   For compatibility with older versions of CMake, option is clearing the
22#   normal variable 'EXPAT_BUILD_DOCS'.
23#
24# and effectively not be able to adjust option EXPAT_BUILD_DOCS.
25#
26# For more details please see:
27# - https://cmake.org/cmake/help/latest/policy/CMP0077.html
28# - https://github.com/libexpat/libexpat/pull/419
29#
30if(POLICY CMP0077)
31    cmake_policy(SET CMP0077 NEW)
32endif()
33
34project(expat
35    VERSION
36        2.3.0
37    LANGUAGES
38        C
39)
40
41set(PACKAGE_BUGREPORT "expat-bugs@libexpat.org")
42set(PACKAGE_NAME "expat")
43set(PACKAGE_VERSION "${PROJECT_VERSION}")
44set(PACKAGE_STRING "${PACKAGE_NAME} ${PACKAGE_VERSION}")
45set(PACKAGE_TARNAME "${PACKAGE_NAME}")
46
47include(CMakePackageConfigHelpers)
48include(GNUInstallDirs)
49
50#
51# Configuration defaults
52#
53if(WINCE)
54    set(_EXPAT_BUILD_TOOLS_DEFAULT OFF)
55else()
56    set(_EXPAT_BUILD_TOOLS_DEFAULT ON)
57endif()
58if(MSVC OR NOT _EXPAT_BUILD_TOOLS_DEFAULT)
59    set(_EXPAT_BUILD_DOCS_DEFAULT OFF)
60else()
61    find_program(DOCBOOK_TO_MAN NAMES docbook2x-man db2x_docbook2man docbook2man docbook-to-man)
62    if(DOCBOOK_TO_MAN)
63        set(_EXPAT_BUILD_DOCS_DEFAULT ON)
64    else()
65        set(_EXPAT_BUILD_DOCS_DEFAULT OFF)
66    endif()
67endif()
68if(MSVC)
69    set(_EXPAT_BUILD_PKGCONFIG_DEFAULT OFF)
70else()
71    set(_EXPAT_BUILD_PKGCONFIG_DEFAULT ON)
72endif()
73
74#
75# Configuration
76#
77option(EXPAT_BUILD_TOOLS "build the xmlwf tool for expat library" ${_EXPAT_BUILD_TOOLS_DEFAULT})
78option(EXPAT_BUILD_EXAMPLES "build the examples for expat library" ON)
79option(EXPAT_BUILD_TESTS "build the tests for expat library" ON)
80option(EXPAT_SHARED_LIBS "build a shared expat library" ON)
81option(EXPAT_BUILD_DOCS "build man page for xmlwf" ${_EXPAT_BUILD_DOCS_DEFAULT})
82option(EXPAT_BUILD_FUZZERS "build fuzzers for the expat library" OFF)
83option(EXPAT_BUILD_PKGCONFIG "build pkg-config file" ${_EXPAT_BUILD_PKGCONFIG_DEFAULT})
84option(EXPAT_OSSFUZZ_BUILD "build fuzzers via ossfuzz for the expat library" OFF)
85if(UNIX OR _EXPAT_HELP)
86    option(EXPAT_WITH_LIBBSD "utilize libbsd (for arc4random_buf)" OFF)
87endif()
88option(EXPAT_ENABLE_INSTALL "install expat files in cmake install target" ON)
89set(EXPAT_CONTEXT_BYTES 1024 CACHE STRING "Define to specify how much context to retain around the current parse point")
90mark_as_advanced(EXPAT_CONTEXT_BYTES)
91option(EXPAT_DTD "Define to make parameter entity parsing functionality available" ON)
92mark_as_advanced(EXPAT_DTD)
93option(EXPAT_NS "Define to make XML Namespaces functionality available" ON)
94mark_as_advanced(EXPAT_NS)
95option(EXPAT_WARNINGS_AS_ERRORS "Treat all compiler warnings as errors" OFF)
96if(UNIX OR _EXPAT_HELP)
97    option(EXPAT_DEV_URANDOM "Define to include code reading entropy from `/dev/urandom'." ON)
98    set(EXPAT_WITH_GETRANDOM "AUTO" CACHE STRING
99            "Make use of getrandom function (ON|OFF|AUTO) [default=AUTO]")
100    set(EXPAT_WITH_SYS_GETRANDOM "AUTO" CACHE STRING
101            "Make use of syscall SYS_getrandom (ON|OFF|AUTO) [default=AUTO]")
102    mark_as_advanced(EXPAT_DEV_URANDOM)
103endif()
104set(EXPAT_CHAR_TYPE "char" CACHE STRING "Character type to use (char|ushort|wchar_t) [default=char]")
105option(EXPAT_ATTR_INFO "Define to allow retrieving the byte offsets for attribute names and values" OFF)
106mark_as_advanced(EXPAT_ATTR_INFO)
107option(EXPAT_LARGE_SIZE "Make XML_GetCurrent* functions return <(unsigned) long long> rather than <(unsigned) long>" OFF)
108mark_as_advanced(EXPAT_LARGE_SIZE)
109option(EXPAT_MIN_SIZE "Get a smaller (but slower) parser (in particular avoid multiple copies of the tokenizer)" OFF)
110mark_as_advanced(EXPAT_MIN_SIZE)
111if(MSVC OR _EXPAT_HELP)
112    set(EXPAT_MSVC_STATIC_CRT OFF CACHE BOOL "Use /MT flag (static CRT) when compiling in MSVC")
113endif()
114
115if(EXPAT_BUILD_TESTS)
116    # We have to call enable_language() before modifying any CMAKE_CXX_* variables
117    enable_language(CXX)
118endif()
119
120#
121# Environment checks
122#
123if(EXPAT_WITH_LIBBSD)
124    find_library(LIB_BSD NAMES bsd)
125    if(NOT LIB_BSD)
126        message(SEND_ERROR "EXPAT_WITH_LIBBSD option is enabled, but libbsd was not found")
127    else()
128        set(HAVE_LIBBSD TRUE)
129    endif()
130endif()
131
132if(MSVC)
133    # Minimum supported MSVC version is 1910 = Visual Studio 15.0/2017
134    # See also https://cmake.org/cmake/help/latest/variable/MSVC_VERSION.html
135    if(MSVC_VERSION VERSION_LESS 1910)
136        message(SEND_ERROR "MSVC_VERSION ${MSVC_VERSION} is not a supported Visual Studio compiler version. Please use Visual Studio 15.0/2017 or any later version.")
137    endif()
138endif()
139
140macro(_expat_copy_bool_int source_ref dest_ref)
141    if(${source_ref})
142        set(${dest_ref} 1)
143    else()
144        set(${dest_ref} 0)
145    endif()
146endmacro()
147
148if(EXPAT_LARGE_SIZE)
149    add_definitions(-DXML_LARGE_SIZE)
150endif()
151
152if(EXPAT_MIN_SIZE)
153    add_definitions(-DXML_MIN_SIZE)
154endif()
155
156if(EXPAT_CHAR_TYPE STREQUAL "char")
157    set(_EXPAT_UNICODE OFF)
158    set(_EXPAT_UNICODE_WCHAR_T OFF)
159elseif(EXPAT_CHAR_TYPE STREQUAL "ushort")
160    set(_EXPAT_UNICODE ON)
161    set(_EXPAT_UNICODE_WCHAR_T OFF)
162    if(EXPAT_BUILD_EXAMPLES)
163        message(SEND_ERROR "Examples can not be built with option -DEXPAT_CHAR_TYPE=ushort. Please pass -DEXPAT_CHAR_TYPE=(char|wchar_t) or -DEXPAT_BUILD_EXAMPLES=OFF.")
164    endif()
165    if(EXPAT_BUILD_TESTS)
166        message(SEND_ERROR "The testsuite can not be built with option -DEXPAT_CHAR_TYPE=ushort. Please pass -DEXPAT_CHAR_TYPE=(char|wchar_t) or -DEXPAT_BUILD_TESTS=OFF.")
167    endif()
168    if(EXPAT_BUILD_TOOLS)
169        message(SEND_ERROR "The xmlwf tool can not be built with option -DEXPAT_CHAR_TYPE=ushort. Please pass -DEXPAT_CHAR_TYPE=(char|wchar_t) or -DEXPAT_BUILD_TOOLS=OFF.")
170    endif()
171elseif(EXPAT_CHAR_TYPE STREQUAL "wchar_t")
172    set(_EXPAT_UNICODE ON)
173    set(_EXPAT_UNICODE_WCHAR_T ON)
174    if(NOT WIN32)
175        string(FIND "${CMAKE_C_FLAGS}" "-fshort-wchar" _expat_short_wchar_found)
176        if(${_expat_short_wchar_found} EQUAL "-1")
177            message(SEND_ERROR "Configuration -DEXPAT_CHAR_TYPE=wchar_t requires -DCMAKE_{C,CXX}_FLAGS=-fshort-wchar (which was not found) and libc compiled with -fshort-wchar, too.")
178        endif()
179        if (EXPAT_BUILD_TOOLS)
180            message(SEND_ERROR "The xmlwf tool can not be built with option -DEXPAT_CHAR_TYPE=wchar_t outside of Windows. Please pass -DEXPAT_CHAR_TYPE=char or -DEXPAT_BUILD_TOOLS=OFF.")
181        endif()
182    endif()
183else()
184    message(SEND_ERROR "Option -DEXPAT_CHAR_TYPE=(char|ushort|wchar_t) cannot be \"${EXPAT_CHAR_TYPE}\".")
185endif()
186
187if(_EXPAT_UNICODE)
188    add_definitions(-DXML_UNICODE)              # for unsigned short
189    if(_EXPAT_UNICODE_WCHAR_T)
190        add_definitions(-DXML_UNICODE_WCHAR_T)  # for wchar_t
191    endif()
192endif()
193
194include(${CMAKE_CURRENT_LIST_DIR}/ConfigureChecks.cmake)
195
196macro(evaluate_detection_results use_ref have_ref thing_lower thing_title)
197    if(${use_ref} AND NOT (${use_ref} STREQUAL "AUTO") AND NOT ${have_ref})
198        message(SEND_ERROR
199                "Use of ${thing_lower} was enforced by ${use_ref}=ON but it could not be found.")
200    elseif(NOT ${use_ref} AND ${have_ref})
201        message("${thing_title} was found but it will not be used due to ${use_ref}=OFF.")
202        set(${have_ref} 0)
203    endif()
204endmacro()
205
206if(NOT WIN32)
207    evaluate_detection_results(EXPAT_WITH_GETRANDOM HAVE_GETRANDOM "function getrandom" "Function getrandom")
208    evaluate_detection_results(EXPAT_WITH_SYS_GETRANDOM HAVE_SYSCALL_GETRANDOM "syscall SYS_getrandom" "Syscall SYS_getrandom")
209endif()
210
211_expat_copy_bool_int(EXPAT_ATTR_INFO        XML_ATTR_INFO)
212_expat_copy_bool_int(EXPAT_DTD              XML_DTD)
213_expat_copy_bool_int(EXPAT_LARGE_SIZE       XML_LARGE_SIZE)
214_expat_copy_bool_int(EXPAT_MIN_SIZE         XML_MIN_SIZE)
215_expat_copy_bool_int(EXPAT_NS               XML_NS)
216if(NOT WIN32)
217    _expat_copy_bool_int(EXPAT_DEV_URANDOM  XML_DEV_URANDOM)
218endif()
219set(XML_CONTEXT_BYTES ${EXPAT_CONTEXT_BYTES})
220
221macro(expat_install)
222    if(EXPAT_ENABLE_INSTALL)
223        install(${ARGN})
224    endif()
225endmacro()
226
227configure_file(expat_config.h.cmake "${CMAKE_CURRENT_BINARY_DIR}/expat_config.h")
228add_definitions(-DHAVE_EXPAT_CONFIG_H)
229expat_install(FILES "${CMAKE_CURRENT_BINARY_DIR}/expat_config.h" DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
230
231
232set(EXTRA_COMPILE_FLAGS)
233if(FLAG_NO_STRICT_ALIASING)
234    set(EXTRA_COMPILE_FLAGS "${EXTRA_COMPILE_FLAGS} -fno-strict-aliasing")
235endif()
236if(FLAG_VISIBILITY)
237  add_definitions(-DXML_ENABLE_VISIBILITY=1)
238  set(EXTRA_COMPILE_FLAGS "${EXTRA_COMPILE_FLAGS} -fvisibility=hidden")
239endif()
240if (EXPAT_WARNINGS_AS_ERRORS)
241    if(MSVC)
242        add_definitions(/WX)
243    else()
244        set(EXTRA_COMPILE_FLAGS "${EXTRA_COMPILE_FLAGS} -Werror")
245    endif()
246endif()
247set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_COMPILE_FLAGS}")
248set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${EXTRA_COMPILE_FLAGS}")
249
250if (MSVC)
251    if (EXPAT_MSVC_STATIC_CRT)
252        message("-- Using static CRT ${EXPAT_MSVC_STATIC_CRT}")
253        foreach(flag_var
254                CMAKE_CXX_FLAGS_DEBUG
255                CMAKE_CXX_FLAGS_RELEASE
256                CMAKE_CXX_FLAGS_MINSIZEREL
257                CMAKE_CXX_FLAGS_RELWITHDEBINFO
258                CMAKE_C_FLAGS_DEBUG
259                CMAKE_C_FLAGS_RELEASE
260                CMAKE_C_FLAGS_MINSIZEREL
261                CMAKE_C_FLAGS_RELWITHDEBINFO
262                )
263            string(REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
264        endforeach()
265    endif()
266endif()
267
268include_directories(${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/lib)
269if(MSVC)
270    add_definitions(-D_CRT_SECURE_NO_WARNINGS -wd4996)
271endif()
272if(WIN32)
273    if(_EXPAT_UNICODE_WCHAR_T)
274        set(_POSTFIX_WIDE "w")
275    endif()
276
277    if(MSVC AND NOT EXPAT_SHARED_LIBS)
278        if(EXPAT_MSVC_STATIC_CRT)
279            set(_POSTFIX_CRT "MT")
280        else()
281            set(_POSTFIX_CRT "MD")
282        endif()
283    endif()
284
285    foreach(postfix_var
286            CMAKE_DEBUG_POSTFIX
287            CMAKE_RELEASE_POSTFIX
288            CMAKE_MINSIZEREL_POSTFIX
289            CMAKE_RELWITHDEBINFO_POSTFIX
290            )
291        if(postfix_var STREQUAL "CMAKE_DEBUG_POSTFIX")
292            set(_POSTFIX_DEBUG "d")
293        else()
294            set(_POSTFIX_DEBUG "")
295        endif()
296
297        set(${postfix_var} "${_POSTFIX_WIDE}${_POSTFIX_DEBUG}${_POSTFIX_CRT}" CACHE STRING "Windows binary postfix, e.g. libexpat<postfix=[w][d][MD|MT]>.lib")
298    endforeach()
299endif()
300
301#
302# C library
303#
304set(expat_SRCS
305    lib/xmlparse.c
306    lib/xmlrole.c
307    lib/xmltok.c
308# NOTE: ISO C forbids an empty translation unit
309#   lib/xmltok_impl.c
310#   lib/xmltok_ns.c
311)
312
313if(EXPAT_SHARED_LIBS)
314    set(_SHARED SHARED)
315    if(MSVC)
316        set(expat_SRCS ${expat_SRCS} lib/libexpat.def)
317    endif()
318else()
319    set(_SHARED STATIC)
320endif()
321
322# Avoid colliding with Expat.dll of Perl's XML::Parser::Expat
323if(WIN32 AND NOT MINGW)
324    set(_EXPAT_OUTPUT_NAME libexpat)  # CMAKE_*_POSTFIX applies, see above
325else()
326    if(_EXPAT_UNICODE)
327        set(_EXPAT_OUTPUT_NAME expatw)
328    else()
329        set(_EXPAT_OUTPUT_NAME expat)
330    endif()
331endif()
332
333add_library(expat ${_SHARED} ${expat_SRCS})
334if(EXPAT_WITH_LIBBSD)
335    target_link_libraries(expat ${LIB_BSD})
336endif()
337
338set(LIBCURRENT 8)   # sync
339set(LIBREVISION 0)  # with
340set(LIBAGE 7)       # configure.ac!
341math(EXPR LIBCURRENT_MINUS_AGE "${LIBCURRENT} - ${LIBAGE}")
342
343set_property(TARGET expat PROPERTY OUTPUT_NAME "${_EXPAT_OUTPUT_NAME}")
344if(NOT WIN32)
345    set_property(TARGET expat PROPERTY VERSION ${LIBCURRENT_MINUS_AGE}.${LIBAGE}.${LIBREVISION})
346    set_property(TARGET expat PROPERTY SOVERSION ${LIBCURRENT_MINUS_AGE})
347    set_property(TARGET expat PROPERTY NO_SONAME ${NO_SONAME})
348endif()
349
350target_include_directories(expat
351    INTERFACE
352        $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/lib>
353        $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
354        $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
355)
356
357if(NOT EXPAT_SHARED_LIBS AND WIN32)
358    target_compile_definitions(expat PUBLIC -DXML_STATIC)
359endif()
360
361expat_install(TARGETS expat EXPORT expat
362                      RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
363                      LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
364                      ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
365
366expat_install(FILES lib/expat.h lib/expat_external.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
367
368#
369# pkg-config file
370#
371if(EXPAT_BUILD_PKGCONFIG)
372    set(prefix ${CMAKE_INSTALL_PREFIX})
373    set(exec_prefix "\${prefix}")
374    set(libdir "\${exec_prefix}/${CMAKE_INSTALL_LIBDIR}")
375    set(includedir "\${prefix}/${CMAKE_INSTALL_INCLUDEDIR}")
376    configure_file(expat.pc.in ${CMAKE_CURRENT_BINARY_DIR}/expat.pc @ONLY)
377    expat_install(FILES ${CMAKE_CURRENT_BINARY_DIR}/expat.pc DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
378endif()
379
380#
381# C command line tool xmlwf
382#
383if(EXPAT_BUILD_TOOLS)
384    set(xmlwf_SRCS
385        xmlwf/xmlwf.c
386        xmlwf/xmlfile.c
387        xmlwf/codepage.c
388        xmlwf/readfilemap.c
389    )
390
391    add_executable(xmlwf ${xmlwf_SRCS})
392    set_property(TARGET xmlwf PROPERTY RUNTIME_OUTPUT_DIRECTORY xmlwf)
393    target_link_libraries(xmlwf expat)
394    expat_install(TARGETS xmlwf DESTINATION ${CMAKE_INSTALL_BINDIR})
395
396    if(MINGW AND _EXPAT_UNICODE_WCHAR_T)
397        # https://gcc.gnu.org/onlinedocs/gcc/x86-Windows-Options.html
398        set_target_properties(xmlwf PROPERTIES LINK_FLAGS -municode)
399    endif()
400
401    if(EXPAT_BUILD_DOCS)
402        file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/doc)
403        add_custom_target(
404            xmlwf-manpage
405            COMMAND
406                "${DOCBOOK_TO_MAN}" "${PROJECT_SOURCE_DIR}/doc/xmlwf.xml" && mv "XMLWF.1" "${PROJECT_BINARY_DIR}/doc/xmlwf.1"
407            BYPRODUCTS
408                doc/xmlwf.1)
409        add_dependencies(expat xmlwf-manpage)
410        expat_install(FILES "${PROJECT_BINARY_DIR}/doc/xmlwf.1" DESTINATION ${CMAKE_INSTALL_MANDIR}/man1)
411    elseif(EXISTS ${PROJECT_SOURCE_DIR}/doc/xmlwf.1)
412        expat_install(FILES "${PROJECT_SOURCE_DIR}/doc/xmlwf.1" DESTINATION ${CMAKE_INSTALL_MANDIR}/man1)
413    endif()
414endif()
415
416#
417# C code examples
418#
419if(EXPAT_BUILD_EXAMPLES)
420    add_executable(elements examples/elements.c)
421    set_property(TARGET elements PROPERTY RUNTIME_OUTPUT_DIRECTORY examples)
422    target_link_libraries(elements expat)
423
424    add_executable(outline examples/outline.c)
425    set_property(TARGET outline PROPERTY RUNTIME_OUTPUT_DIRECTORY examples)
426    target_link_libraries(outline expat)
427endif()
428
429#
430# C/C++ test runners
431#
432if(EXPAT_BUILD_TESTS)
433    ## these are unittests that can be run on any platform
434    enable_testing()
435
436    set(test_SRCS
437        tests/chardata.c
438        tests/memcheck.c
439        tests/minicheck.c
440        tests/structdata.c
441    )
442
443    if(NOT MSVC)
444        if(MINGW)
445            set(host whatever-mingw32)  # for nothing but run.sh
446        endif()
447        configure_file(${CMAKE_CURRENT_SOURCE_DIR}/run.sh.in run.sh @ONLY)
448    endif()
449
450    function(expat_add_test _name _file)
451        if(MSVC)
452            add_test(NAME ${_name} COMMAND ${_file})
453        else()
454            add_test(NAME ${_name} COMMAND bash run.sh ${_file})
455        endif()
456    endfunction()
457
458    add_executable(runtests tests/runtests.c ${test_SRCS})
459    set_property(TARGET runtests PROPERTY RUNTIME_OUTPUT_DIRECTORY tests)
460    target_link_libraries(runtests expat)
461    expat_add_test(runtests $<TARGET_FILE:runtests>)
462
463    add_executable(runtestspp tests/runtestspp.cpp ${test_SRCS})
464    set_property(TARGET runtestspp PROPERTY RUNTIME_OUTPUT_DIRECTORY tests)
465    target_link_libraries(runtestspp expat)
466    expat_add_test(runtestspp $<TARGET_FILE:runtestspp>)
467endif()
468
469if(EXPAT_BUILD_FUZZERS)
470    if(NOT "${CMAKE_C_COMPILER_ID}" STREQUAL "Clang")
471        message(SEND_ERROR
472            "Building fuzz targets without Clang (but ${CMAKE_C_COMPILER_ID}) "
473            "is not supported. Please set "
474            "-DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++.")
475    endif()
476
477    string(FIND "${CMAKE_C_FLAGS}" "-fsanitize" sanitizer_present)
478    if(${sanitizer_present} EQUAL "-1")
479        message(WARNING
480            "There was no sanitizer present when building the fuzz targets. "
481            "This is likely in error - consider adding "
482            "-DCMAKE_C_FLAGS='-fsanitize=<sanitizer>' and "
483            "-DCMAKE_CXX_FLAGS='-fsanitize=<sanitizer>' and "
484            "-DCMAKE_EXE_LINKER_FLAGS='-fsanitize=<sanitizer>' and "
485            "-DCMAKE_MODULE_LINKER_FLAGS='-fsanitize=<sanitizer>' and "
486            "-DCMAKE_SHARED_LINKER_FLAGS='-fsanitize=<sanitizer>' to your cmake "
487            "execution.")
488    endif()
489
490    if(EXPAT_OSSFUZZ_BUILD AND NOT DEFINED ENV{LIB_FUZZING_ENGINE})
491        message(SEND_ERROR
492            "OSS-Fuzz builds require the environment variable "
493            "LIB_FUZZING_ENGINE to be set. If you are seeing this "
494            "warning, it points to a deeper problem in the ossfuzz "
495            "build setup.")
496    endif()
497
498    set(encoding_types UTF-16 UTF-8 ISO-8859-1 US-ASCII UTF-16BE UTF-16LE)
499    set(fuzz_targets xml_parse_fuzzer xml_parsebuffer_fuzzer)
500
501    add_library(fuzzpat STATIC ${expat_SRCS})
502    if(NOT EXPAT_OSSFUZZ_BUILD)
503        target_compile_options(fuzzpat PRIVATE -fsanitize=fuzzer-no-link)
504    endif()
505
506    foreach(fuzz_target ${fuzz_targets})
507        foreach(encoding_type ${encoding_types})
508            set(target_name ${fuzz_target}_${encoding_type})
509            add_executable(${target_name} fuzz/${fuzz_target}.c)
510            target_link_libraries(${target_name} fuzzpat)
511            target_compile_definitions(${target_name}
512                PRIVATE ENCODING_FOR_FUZZING=${encoding_type})
513            if(NOT EXPAT_OSSFUZZ_BUILD)
514                target_compile_options(${target_name} PRIVATE -fsanitize=fuzzer-no-link)
515            endif()
516            # NOTE: Avoiding target_link_options here only because it needs CMake >=3.13
517            if(EXPAT_OSSFUZZ_BUILD)
518                set_target_properties(${target_name} PROPERTIES LINK_FLAGS $ENV{LIB_FUZZING_ENGINE})
519                set_target_properties(${target_name} PROPERTIES LINKER_LANGUAGE "CXX")
520            else()
521                set_target_properties(${target_name} PROPERTIES LINK_FLAGS -fsanitize=fuzzer)
522            endif()
523            set_property(
524                TARGET ${target_name} PROPERTY RUNTIME_OUTPUT_DIRECTORY fuzz)
525        endforeach()
526    endforeach()
527else()
528    if(EXPAT_OSSFUZZ_BUILD)
529        message(SEND_ERROR
530                "Attempting to perform an ossfuzz build without turning on the fuzzer build. "
531                "This is likely in error - consider adding "
532                "-DEXPAT_BUILD_FUZZERS=ON to your cmake execution.")
533    endif()
534endif()
535
536#
537# Custom target "run-xmltest"
538#
539if(EXPAT_BUILD_TOOLS AND NOT MSVC)
540    add_custom_target(
541        xmlts-zip-downloaded
542        COMMAND
543            sh -c 'test -f xmlts.zip || wget --output-document=xmlts.zip https://www.w3.org/XML/Test/xmlts20080827.zip'
544        BYPRODUCTS
545            tests/xmlts.zip
546        WORKING_DIRECTORY
547            tests/)
548
549    add_custom_target(
550        xmlts-zip-extracted
551        COMMAND
552            sh -c 'test -d xmlconf || unzip -q xmlts.zip'
553        BYPRODUCTS
554            tests/xmlconf
555        WORKING_DIRECTORY
556            tests/)
557    add_dependencies(xmlts-zip-extracted xmlts-zip-downloaded)
558
559    add_custom_target(
560        xmltest-sh-been-run
561        COMMAND
562            sh -c '${CMAKE_CURRENT_SOURCE_DIR}/tests/xmltest.sh "bash ${CMAKE_CURRENT_BINARY_DIR}/run.sh $<TARGET_FILE:xmlwf>" 2>&1 | tee tests/xmltest.log'
563        BYPRODUCTS
564            tests/xmltest.log)
565    add_dependencies(xmltest-sh-been-run xmlts-zip-extracted xmlwf)
566
567    add_custom_target(
568        xmltest-log-fixed
569        COMMAND
570            ${CMAKE_CURRENT_SOURCE_DIR}/fix-xmltest-log.sh tests/xmltest.log
571        DEPENDS
572            tests/xmltest.log)
573    add_dependencies(xmltest-log-fixed xmltest-sh-been-run)
574
575    add_custom_target(
576        xmltest-log-verified
577        COMMAND
578            diff -u ${CMAKE_CURRENT_SOURCE_DIR}/tests/xmltest.log.expected tests/xmltest.log)
579    add_dependencies(xmltest-log-verified xmltest-log-fixed)
580
581    add_custom_target(run-xmltest)
582    add_dependencies(run-xmltest xmltest-log-verified)
583endif()
584
585#
586# Documentation
587#
588configure_file(Changes changelog COPYONLY)
589expat_install(
590    FILES
591        AUTHORS
592        ${CMAKE_CURRENT_BINARY_DIR}/changelog
593    DESTINATION
594        ${CMAKE_INSTALL_DOCDIR})
595
596#
597# CMake files for find_package(expat [..] CONFIG [..])
598#
599configure_package_config_file(
600        cmake/expat-config.cmake.in
601        cmake/expat-config.cmake
602    INSTALL_DESTINATION
603        ${CMAKE_INSTALL_LIBDIR}/cmake/expat-${PROJECT_VERSION}/
604)
605write_basic_package_version_file(
606    cmake/expat-config-version.cmake
607    COMPATIBILITY SameMajorVersion  # i.e. semver
608)
609export(
610    TARGETS
611        expat
612    FILE
613        cmake/expat-targets.cmake  # not going to be installed
614)
615expat_install(
616    FILES
617        ${CMAKE_CURRENT_BINARY_DIR}/cmake/expat-config.cmake
618        ${CMAKE_CURRENT_BINARY_DIR}/cmake/expat-config-version.cmake
619    DESTINATION
620        ${CMAKE_INSTALL_LIBDIR}/cmake/expat-${PROJECT_VERSION}/
621)
622expat_install(
623    EXPORT
624        expat
625    DESTINATION
626        ${CMAKE_INSTALL_LIBDIR}/cmake/expat-${PROJECT_VERSION}/
627    NAMESPACE
628        expat::
629)
630
631#
632# CPack
633#
634
635# This effectively disables target "package_source".
636# That is done due to CPack's unfortunate choice of an exclusion list
637# rather than inclusion list.  An exclusion list does not protect against
638# unwanted files ending up in the resulting archive in a way that's
639# safe to run from an Expat developer's machine.
640set(CPACK_SOURCE_GENERATOR '')
641
642if(WIN32)
643    set(CPACK_GENERATOR ZIP)
644else()
645    set(CPACK_GENERATOR TGZ)
646endif()
647
648include(CPack)
649
650#
651# Summary
652#
653if(EXPAT_CHAR_TYPE STREQUAL "char")
654    set(_EXPAT_CHAR_TYPE_SUMMARY "char (UTF-8)")
655elseif(EXPAT_CHAR_TYPE STREQUAL "ushort")
656    set(_EXPAT_CHAR_TYPE_SUMMARY "ushort (unsigned short, UTF-16)")
657elseif(EXPAT_CHAR_TYPE STREQUAL "wchar_t")
658    if(WIN32)
659        set(_EXPAT_CHAR_TYPE_SUMMARY "wchar_t (UTF-16)")
660    else()
661        set(_EXPAT_CHAR_TYPE_SUMMARY "wchar_t (UTF-32)  // not implemented")
662    endif()
663else()
664    set(_EXPAT_CHAR_TYPE_SUMMARY "ERROR")
665endif()
666string(TOUPPER "${CMAKE_BUILD_TYPE}" _EXPAT_BUILD_TYPE_UPPER)
667
668message(STATUS "===========================================================================")
669message(STATUS "")
670message(STATUS "Configuration")
671message(STATUS "  Prefix ..................... ${CMAKE_INSTALL_PREFIX}")
672message(STATUS "  Build type ................. ${CMAKE_BUILD_TYPE}")
673message(STATUS "  Shared libraries ........... ${EXPAT_SHARED_LIBS}")
674if(MSVC)
675    message(STATUS "  Static CRT ................. ${EXPAT_MSVC_STATIC_CRT}")
676endif()
677message(STATUS "  Character type ............. ${_EXPAT_CHAR_TYPE_SUMMARY}")
678if(WIN32)
679    message(STATUS "  Binary postfix ............. ${CMAKE_${_EXPAT_BUILD_TYPE_UPPER}_POSTFIX}")
680endif()
681message(STATUS "")
682message(STATUS "  Build documentation ........ ${EXPAT_BUILD_DOCS}")
683message(STATUS "  Build examples ............. ${EXPAT_BUILD_EXAMPLES}")
684message(STATUS "  Build fuzzers .............. ${EXPAT_BUILD_FUZZERS}")
685message(STATUS "  Build tests ................ ${EXPAT_BUILD_TESTS}")
686message(STATUS "  Build tools (xmlwf) ........ ${EXPAT_BUILD_TOOLS}")
687message(STATUS "  Build pkg-config file ...... ${EXPAT_BUILD_PKGCONFIG}")
688message(STATUS "  Install files .............. ${EXPAT_ENABLE_INSTALL}")
689message(STATUS "")
690message(STATUS "  Features")
691message(STATUS "    // Advanced options, changes not advised")
692message(STATUS "    Attributes info .......... ${EXPAT_ATTR_INFO}")
693message(STATUS "    Context bytes ............ ${EXPAT_CONTEXT_BYTES}")
694message(STATUS "    DTD support .............. ${EXPAT_DTD}")
695message(STATUS "    Large size ............... ${EXPAT_LARGE_SIZE}")
696message(STATUS "    Minimum size ............. ${EXPAT_MIN_SIZE}")
697message(STATUS "    Namespace support ........ ${EXPAT_NS}")
698message(STATUS "")
699message(STATUS "  Entropy sources")
700if(WIN32)
701    message(STATUS "    rand_s ................... ON")
702else()
703    message(STATUS "    getrandom ................ ${HAVE_GETRANDOM}")
704    message(STATUS "    syscall SYS_getrandom .... ${HAVE_SYSCALL_GETRANDOM}")
705    message(STATUS "    libbsd ................... ${EXPAT_WITH_LIBBSD}")
706    message(STATUS "    /dev/random .............. ${EXPAT_DEV_URANDOM}")
707endif()
708message(STATUS "")
709if(CMAKE_GENERATOR STREQUAL "Unix Makefiles")
710    message(STATUS "Continue with")
711    message(STATUS "  make")
712    if(EXPAT_BUILD_TESTS)
713        message(STATUS "  make test")
714    endif()
715    if(EXPAT_ENABLE_INSTALL)
716        message(STATUS "  sudo make install")
717    endif()
718    message(STATUS "")
719endif()
720message(STATUS "===========================================================================")
721