2019-08-10 17:50:29 -04:00
# This file is copyrighted under the BSD-license for buildsystem files of KDE
# copyright 2010, Patrick Spendrin <ps_ml@gmx.de>
2019-08-17 08:49:22 -04:00
cmake_minimum_required ( VERSION 3.1.3 )
2019-08-12 14:33:16 -04:00
2019-08-12 14:41:27 -04:00
project ( expat
V E R S I O N
2 . 2 . 7
2019-08-12 14:42:06 -04:00
L A N G U A G E S
C
2019-08-12 14:41:27 -04:00
)
2019-08-10 17:50:29 -04:00
set ( PACKAGE_BUGREPORT "expat-bugs@libexpat.org" )
set ( PACKAGE_NAME "expat" )
2019-08-12 14:41:27 -04:00
set ( PACKAGE_VERSION "${PROJECT_VERSION}" )
2019-08-10 17:50:29 -04:00
set ( PACKAGE_STRING "${PACKAGE_NAME} ${PACKAGE_VERSION}" )
set ( PACKAGE_TARNAME "${PACKAGE_NAME}" )
include ( GNUInstallDirs )
2019-08-11 17:44:09 -04:00
#
# Configuration defaults
#
2019-08-10 17:50:29 -04:00
if ( WINCE )
set ( BUILD_tools_default OFF )
else ( )
set ( BUILD_tools_default ON )
endif ( )
if ( MSVC OR NOT BUILD_tools_default )
set ( BUILD_doc_default OFF )
else ( )
find_program ( DOCBOOK_TO_MAN NAMES docbook2x-man db2x_docbook2man docbook2man docbook-to-man )
if ( DOCBOOK_TO_MAN )
set ( BUILD_doc_default ON )
else ( )
set ( BUILD_doc_default OFF )
endif ( )
endif ( )
2019-08-11 17:39:22 -04:00
#
# Configuration
#
2019-08-10 17:50:29 -04:00
option ( BUILD_tools "build the xmlwf tool for expat library" ${ BUILD_tools_default } )
option ( BUILD_examples "build the examples for expat library" ON )
option ( BUILD_tests "build the tests for expat library" ON )
option ( BUILD_shared "build a shared expat library" ON )
option ( BUILD_doc "build man page for xmlwf" ${ BUILD_doc_default } )
2019-08-28 06:57:57 -04:00
option ( BUILD_fuzzers "build fuzzers for the expat library" OFF )
2019-08-10 17:50:29 -04:00
option ( USE_libbsd "utilize libbsd (for arc4random_buf)" OFF )
option ( INSTALL "install expat files in cmake install target" ON )
2019-08-11 17:39:22 -04:00
set ( XML_CONTEXT_BYTES 1024 CACHE STRING "Define to specify how much context to retain around the current parse point" )
option ( XML_DTD "Define to make parameter entity parsing functionality available" ON )
option ( XML_NS "Define to make XML Namespaces functionality available" ON )
option ( WARNINGS_AS_ERRORS "Treat all compiler warnings as errors" OFF )
if ( NOT WIN32 )
option ( XML_DEV_URANDOM "Define to include code reading entropy from `/dev/urandom'." ON )
set ( USE_GETRANDOM "AUTO" CACHE STRING
" M a k e u s e o f g e t r a n d o m function ( ON|OFF|AUTO ) [ d e f a u l t = A U T O ] " )
set ( USE_SYS_GETRANDOM "AUTO" CACHE STRING
" M a k e u s e o f s y s c a l l SYS_getrandom ( ON|OFF|AUTO ) [ d e f a u l t = A U T O ] " )
endif ( )
option ( XML_UNICODE "Use UTF-16 encoded chars (two bytes) instead of UTF-8" OFF )
option ( XML_UNICODE_WCHAR_T "Use wchar_t to represent UTF-16 instead of unsigned short" OFF )
option ( XML_ATTR_INFO "Define to allow retrieving the byte offsets for attribute names and values" OFF )
if ( MSVC )
set ( MSVC_USE_STATIC_CRT OFF CACHE BOOL "Use /MT flag (static CRT) when compiling in MSVC" )
endif ( )
2019-08-10 17:50:29 -04:00
2019-08-11 17:44:09 -04:00
#
# Environment checks
#
2019-08-10 17:50:29 -04:00
if ( USE_libbsd )
find_library ( LIB_BSD NAMES bsd )
if ( NOT LIB_BSD )
2019-08-12 14:29:43 -04:00
message ( SEND_ERROR "USE_libbsd option is enabled, but libbsd was not found" )
2019-08-10 17:50:29 -04:00
else ( )
set ( HAVE_LIBBSD TRUE )
endif ( )
endif ( )
if ( NOT WIN32 )
if ( XML_DEV_URANDOM )
set ( XML_DEV_URANDOM 1 )
else ( XML_DEV_URANDOM )
set ( XML_DEV_URANDOM 0 )
endif ( XML_DEV_URANDOM )
endif ( )
if ( XML_DTD )
set ( XML_DTD 1 )
else ( XML_DTD )
set ( XML_DTD 0 )
endif ( XML_DTD )
if ( XML_NS )
set ( XML_NS 1 )
else ( XML_NS )
set ( XML_NS 0 )
endif ( XML_NS )
if ( XML_UNICODE )
set ( XML_UNICODE 1 )
else ( XML_UNICODE )
set ( XML_UNICODE 0 )
endif ( XML_UNICODE )
if ( XML_UNICODE_WCHAR_T )
set ( XML_UNICODE_WCHAR_T 1 )
else ( XML_UNICODE_WCHAR_T )
set ( XML_UNICODE_WCHAR_T 0 )
endif ( XML_UNICODE_WCHAR_T )
if ( XML_ATTR_INFO )
set ( XML_ATTR_INFO 1 )
else ( XML_ATTR_INFO )
set ( XML_ATTR_INFO 0 )
endif ( XML_ATTR_INFO )
if ( XML_UNICODE_WCHAR_T AND NOT XML_UNICODE )
message ( SEND_ERROR "Option XML_UNICODE_WCHAR_T=ON may not be used without XML_UNICODE=ON." )
set ( XML_UNICODE 1 )
endif ( XML_UNICODE_WCHAR_T AND NOT XML_UNICODE )
if ( BUILD_tools AND ( XML_UNICODE AND NOT XML_UNICODE_WCHAR_T ) )
message ( SEND_ERROR "The xmlwf tool can not be built with option XML_UNICODE_WCHAR_T=OFF and XML_UNICODE=ON. Either set XML_UNICODE_WCHAR_T=ON or BUILD_tools=OFF." )
endif ( )
include ( ${ CMAKE_CURRENT_LIST_DIR } /ConfigureChecks.cmake )
macro ( evaluate_detection_results use_ref have_ref thing_lower thing_title )
if ( ${ use_ref } AND NOT ( ${ use_ref } STREQUAL "AUTO" ) AND NOT ${ have_ref } )
message ( SEND_ERROR
" U s e o f $ { t h i n g _ l o w e r } w a s e n f o r c e d b y $ { u s e _ r e f } = O N b u t i t c o u l d n o t b e f o u n d . " )
elseif ( NOT ${ use_ref } AND ${ have_ref } )
message ( "${thing_title} was found but it will not be used due to ${use_ref}=OFF." )
set ( ${ have_ref } 0 )
endif ( )
endmacro ( )
if ( NOT WIN32 )
evaluate_detection_results ( USE_GETRANDOM HAVE_GETRANDOM "function getrandom" "Function getrandom" )
evaluate_detection_results ( USE_SYS_GETRANDOM HAVE_SYSCALL_GETRANDOM "syscall SYS_getrandom" "Syscall SYS_getrandom" )
endif ( )
configure_file ( expat_config.h.cmake "${CMAKE_CURRENT_BINARY_DIR}/expat_config.h" )
add_definitions ( -DHAVE_EXPAT_CONFIG_H )
set ( EXTRA_COMPILE_FLAGS )
if ( FLAG_NO_STRICT_ALIASING )
set ( EXTRA_COMPILE_FLAGS "${EXTRA_COMPILE_FLAGS} -fno-strict-aliasing" )
endif ( )
if ( FLAG_VISIBILITY )
add_definitions ( -DXML_ENABLE_VISIBILITY=1 )
set ( EXTRA_COMPILE_FLAGS "${EXTRA_COMPILE_FLAGS} -fvisibility=hidden" )
endif ( FLAG_VISIBILITY )
if ( WARNINGS_AS_ERRORS )
if ( MSVC )
add_definitions ( /WX )
else ( MSVC )
set ( EXTRA_COMPILE_FLAGS "${EXTRA_COMPILE_FLAGS} -Werror" )
endif ( MSVC )
endif ( WARNINGS_AS_ERRORS )
set ( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_COMPILE_FLAGS}" )
set ( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${EXTRA_COMPILE_FLAGS}" )
if ( MSVC )
if ( MSVC_USE_STATIC_CRT )
message ( "-- Using static CRT ${MSVC_USE_STATIC_CRT}" )
foreach ( flag_var
C M A K E _ C X X _ F L A G S _ D E B U G
C M A K E _ C X X _ F L A G S _ R E L E A S E
C M A K E _ C X X _ F L A G S _ M I N S I Z E R E L
C M A K E _ C X X _ F L A G S _ R E L W I T H D E B I N F O
C M A K E _ C _ F L A G S _ D E B U G
C M A K E _ C _ F L A G S _ R E L E A S E
C M A K E _ C _ F L A G S _ M I N S I Z E R E L
C M A K E _ C _ F L A G S _ R E L W I T H D E B I N F O
)
string ( REPLACE "/MD" "/MT" ${ flag_var } "${${flag_var}}" )
endforeach ( )
endif ( )
endif ( )
include_directories ( ${ CMAKE_CURRENT_BINARY_DIR } ${ CMAKE_CURRENT_SOURCE_DIR } /lib )
if ( MSVC )
add_definitions ( -D_CRT_SECURE_NO_WARNINGS -wd4996 )
endif ( MSVC )
if ( WIN32 )
2019-08-15 16:33:22 -04:00
if ( XML_UNICODE_WCHAR_T )
set ( _POSTFIX_WIDE "w" )
endif ( )
foreach ( postfix_var
C M A K E _ D E B U G _ P O S T F I X
C M A K E _ R E L E A S E _ P O S T F I X
C M A K E _ M I N S I Z E R E L _ P O S T F I X
C M A K E _ R E L W I T H D E B I N F O _ P O S T F I X
)
if ( postfix_var STREQUAL "CMAKE_DEBUG_POSTFIX" )
set ( _POSTFIX_DEBUG "d" )
else ( )
set ( _POSTFIX_DEBUG "" )
endif ( )
2019-08-21 12:19:36 -04:00
set ( ${ postfix_var } "${_POSTFIX_WIDE}${_POSTFIX_DEBUG}" CACHE STRING "Windows binary postfix, e.g. expat<postfix=[w][d]>.dll" )
2019-08-15 16:33:22 -04:00
endforeach ( )
2019-08-10 17:50:29 -04:00
endif ( WIN32 )
2019-08-11 17:44:09 -04:00
#
# C library
#
2019-08-10 17:50:29 -04:00
set ( expat_SRCS
l i b / x m l p a r s e . c
l i b / x m l r o l e . c
l i b / x m l t o k . c
l i b / x m l t o k _ i m p l . c
l i b / x m l t o k _ n s . c
)
if ( BUILD_shared )
set ( _SHARED SHARED )
if ( MSVC )
set ( expat_SRCS ${ expat_SRCS } lib/libexpat.def )
endif ( MSVC )
else ( BUILD_shared )
set ( _SHARED STATIC )
if ( WIN32 )
add_definitions ( -DXML_STATIC )
endif ( WIN32 )
endif ( BUILD_shared )
add_library ( expat ${ _SHARED } ${ expat_SRCS } )
if ( USE_libbsd )
target_link_libraries ( expat ${ LIB_BSD } )
endif ( )
set ( LIBCURRENT 7 ) # sync
set ( LIBREVISION 9 ) # with
set ( LIBAGE 6 ) # configure.ac!
math ( EXPR LIBCURRENT_MINUS_AGE "${LIBCURRENT} - ${LIBAGE}" )
if ( NOT WIN32 )
set_property ( TARGET expat PROPERTY VERSION ${ LIBCURRENT_MINUS_AGE } . ${ LIBAGE } . ${ LIBREVISION } )
set_property ( TARGET expat PROPERTY SOVERSION ${ LIBCURRENT_MINUS_AGE } )
set_property ( TARGET expat PROPERTY NO_SONAME ${ NO_SONAME } )
endif ( NOT WIN32 )
macro ( expat_install )
if ( INSTALL )
install ( ${ ARGN } )
endif ( )
endmacro ( )
expat_install ( TARGETS expat RUNTIME DESTINATION ${ CMAKE_INSTALL_BINDIR }
L I B R A R Y D E S T I N A T I O N $ { C M A K E _ I N S T A L L _ L I B D I R }
A R C H I V E D E S T I N A T I O N $ { C M A K E _ I N S T A L L _ L I B D I R } )
2019-08-11 17:56:23 -04:00
expat_install ( FILES lib/expat.h lib/expat_external.h DESTINATION ${ CMAKE_INSTALL_INCLUDEDIR } )
2019-08-11 17:44:09 -04:00
#
# pkg-config file
#
2019-08-10 17:50:29 -04:00
set ( prefix ${ CMAKE_INSTALL_PREFIX } )
set ( exec_prefix "\${prefix}" )
set ( libdir "\${exec_prefix}/${CMAKE_INSTALL_LIBDIR}" )
set ( includedir "\${prefix}/${CMAKE_INSTALL_INCLUDEDIR}" )
configure_file ( expat.pc.in ${ CMAKE_CURRENT_BINARY_DIR } /expat.pc @ONLY )
expat_install ( FILES ${ CMAKE_CURRENT_BINARY_DIR } /expat.pc DESTINATION ${ CMAKE_INSTALL_LIBDIR } /pkgconfig )
2019-08-11 17:44:09 -04:00
#
# C command line tool xmlwf
#
2019-08-10 17:50:29 -04:00
if ( BUILD_tools )
set ( xmlwf_SRCS
x m l w f / x m l w f . c
x m l w f / x m l f i l e . c
x m l w f / c o d e p a g e . c
x m l w f / r e a d f i l e m a p . c
)
add_executable ( xmlwf ${ xmlwf_SRCS } )
set_property ( TARGET xmlwf PROPERTY RUNTIME_OUTPUT_DIRECTORY xmlwf )
target_link_libraries ( xmlwf expat )
expat_install ( TARGETS xmlwf DESTINATION ${ CMAKE_INSTALL_BINDIR } )
if ( BUILD_doc )
file ( MAKE_DIRECTORY ${ PROJECT_BINARY_DIR } /doc )
add_custom_command ( TARGET expat PRE_BUILD COMMAND "${DOCBOOK_TO_MAN}" "${PROJECT_SOURCE_DIR}/doc/xmlwf.xml" && mv "XMLWF.1" "${PROJECT_BINARY_DIR}/doc/xmlwf.1" )
expat_install ( FILES "${PROJECT_BINARY_DIR}/doc/xmlwf.1" DESTINATION ${ CMAKE_INSTALL_MANDIR } /man1 )
endif ( )
endif ( )
2019-08-11 17:44:09 -04:00
#
# C code examples
#
2019-08-10 17:50:29 -04:00
if ( BUILD_examples )
add_executable ( elements examples/elements.c )
set_property ( TARGET elements PROPERTY RUNTIME_OUTPUT_DIRECTORY examples )
target_link_libraries ( elements expat )
add_executable ( outline examples/outline.c )
set_property ( TARGET outline PROPERTY RUNTIME_OUTPUT_DIRECTORY examples )
target_link_libraries ( outline expat )
endif ( BUILD_examples )
2019-08-11 17:44:09 -04:00
#
# C/C++ test runners
#
2019-08-10 17:50:29 -04:00
if ( BUILD_tests )
## these are unittests that can be run on any platform
2019-08-12 14:42:06 -04:00
enable_language ( CXX )
2019-08-12 14:31:47 -04:00
enable_testing ( )
2019-08-11 16:46:44 -04:00
set ( test_SRCS
t e s t s / c h a r d a t a . c
t e s t s / m e m c h e c k . c
t e s t s / m i n i c h e c k . c
t e s t s / s t r u c t d a t a . c
)
add_executable ( runtests tests/runtests.c ${ test_SRCS } )
2019-08-10 17:50:29 -04:00
set_property ( TARGET runtests PROPERTY RUNTIME_OUTPUT_DIRECTORY tests )
target_link_libraries ( runtests expat )
add_test ( runtests tests/runtests )
2019-08-11 16:46:44 -04:00
add_executable ( runtestspp tests/runtestspp.cpp ${ test_SRCS } )
2019-08-10 17:50:29 -04:00
set_property ( TARGET runtestspp PROPERTY RUNTIME_OUTPUT_DIRECTORY tests )
target_link_libraries ( runtestspp expat )
add_test ( runtestspp tests/runtestspp )
endif ( BUILD_tests )
2019-08-11 17:28:39 -04:00
2019-08-28 06:57:57 -04:00
if ( BUILD_fuzzers )
if ( NOT "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" )
message ( SEND_ERROR
" B u i l d i n g f u z z t a r g e t s w i t h o u t c l a n g i s n o t s u p p o r t e d . P l e a s e s e t "
" - D C M A K E _ C _ C O M P I L E R = c l a n g . " )
endif ( )
string ( FIND "${CMAKE_EXE_LINKER_FLAGS}" "-fsanitize" sanitizer_present )
if ( ${ sanitizer_present } EQUAL "-1" )
message ( WARNING
" T h e r e w a s n o s a n i t i z e r p r e s e n t w h e n b u i l d i n g t h e f u z z t a r g e t s . "
" T h i s i s l i k e l y i n e r r o r - c o n s i d e r a d d i n g "
" - D C M A K E _ C _ F L A G S = ' - f s a n i t i z e = < s a n i t i z e r > ' a n d "
" - D C M A K E _ E X E _ L I N K E R _ F L A G S = ' - f s a n i t i z e = < s a n i t i z e r > ' t o y o u r c m a k e "
" e x e c u t i o n . " )
endif ( )
set ( encoding_types UTF-16 UTF-8 ISO-8859-1 US-ASCII UTF-16BE UTF-16LE )
set ( fuzz_targets xml_parse_fuzzer xml_parsebuffer_fuzzer )
foreach ( fuzz_target ${ fuzz_targets } )
foreach ( encoding_type ${ encoding_types } )
set ( target_name ${ fuzz_target } _ ${ encoding_type } )
add_executable ( ${ target_name } fuzz/ ${ fuzz_target } .c ${ expat_SRCS } )
target_compile_definitions ( ${ target_name }
P R I V A T E E N C O D I N G _ F O R _ F U Z Z I N G = $ { e n c o d i n g _ t y p e } )
target_compile_options ( ${ target_name } PRIVATE -fsanitize=fuzzer-no-link )
target_link_options ( ${ target_name } PRIVATE -fsanitize=fuzzer )
set_property (
T A R G E T $ { t a r g e t _ n a m e } P R O P E R T Y R U N T I M E _ O U T P U T _ D I R E C T O R Y f u z z )
endforeach ( )
endforeach ( )
endif ( BUILD_fuzzers )
2019-08-11 17:28:39 -04:00
#
# Summary
#
if ( XML_UNICODE )
if ( XML_UNICODE_WCHAR_T )
set ( _EXPAT_CHAR_TYPE "wchar_t" )
else ( )
set ( _EXPAT_CHAR_TYPE "unsigned short" )
endif ( )
else ( )
set ( _EXPAT_CHAR_TYPE "char" )
endif ( )
2019-08-15 16:33:22 -04:00
string ( TOUPPER "${CMAKE_BUILD_TYPE}" _EXPAT_BUILD_TYPE_UPPER )
2019-08-11 17:28:39 -04:00
message ( STATUS "===========================================================================" )
message ( STATUS "" )
message ( STATUS "Configuration" )
message ( STATUS " Prefix ..................... ${CMAKE_INSTALL_PREFIX}" )
message ( STATUS " Shared libraries ........... ${BUILD_shared}" )
if ( WIN32 )
message ( STATUS " Static CRT ................. ${MSVC_USE_STATIC_CRT}" )
endif ( )
message ( STATUS " Character type ............. ${_EXPAT_CHAR_TYPE}" )
2019-08-15 16:33:22 -04:00
if ( WIN32 )
message ( STATUS " Binary postfix ............. ${CMAKE_${_EXPAT_BUILD_TYPE_UPPER}_POSTFIX}" )
endif ( )
2019-08-11 17:28:39 -04:00
message ( STATUS "" )
message ( STATUS " Build documentation ........ ${BUILD_doc}" )
message ( STATUS " Build examples ............. ${BUILD_examples}" )
message ( STATUS " Build tests ................ ${BUILD_tests}" )
message ( STATUS " Build tools (xmlwf) ........ ${BUILD_tools}" )
message ( STATUS " Install files .............. ${INSTALL}" )
message ( STATUS "" )
message ( STATUS " Features" )
message ( STATUS " Attributes info .......... ${XML_ATTR_INFO}" )
message ( STATUS " Context bytes ............ ${XML_CONTEXT_BYTES}" )
message ( STATUS " DTD support .............. ${XML_DTD}" )
message ( STATUS " Namespace support ........ ${XML_NS}" )
message ( STATUS "" )
message ( STATUS " Entropy sources" )
if ( WIN32 )
message ( STATUS " rand_s ................... ON" )
else ( )
message ( STATUS " getrandom ................ ${HAVE_GETRANDOM}" )
message ( STATUS " syscall SYS_getrandom .... ${HAVE_SYSCALL_GETRANDOM}" )
message ( STATUS " libbsd ................... ${USE_libbsd}" )
message ( STATUS " /dev/random .............. ${XML_DEV_URANDOM}" )
endif ( )
message ( STATUS "" )
if ( CMAKE_GENERATOR STREQUAL "Unix Makefiles" )
message ( STATUS "Continue with" )
message ( STATUS " make" )
if ( BUILD_tests )
message ( STATUS " make test" )
endif ( )
if ( INSTALL )
message ( STATUS " sudo make install" )
endif ( )
message ( STATUS "" )
endif ( )
message ( STATUS "===========================================================================" )