1 # This file is copyrighted under the BSD-license for buildsystem files of KDE 2 # copyright 2010, Patrick Spendrin <ps_ml (a] gmx.de> 3 4 project(expat) 5 6 cmake_minimum_required(VERSION 2.8.10) 7 set(PACKAGE_BUGREPORT "expat-bugs (a] libexpat.org") 8 set(PACKAGE_NAME "expat") 9 set(PACKAGE_VERSION "2.2.6") 10 set(PACKAGE_STRING "${PACKAGE_NAME} ${PACKAGE_VERSION}") 11 set(PACKAGE_TARNAME "${PACKAGE_NAME}") 12 13 include(GNUInstallDirs) 14 15 if(WINCE) 16 set(BUILD_tools_default OFF) 17 else() 18 set(BUILD_tools_default ON) 19 endif() 20 if(MSVC OR NOT BUILD_tools_default) 21 set(BUILD_doc_default OFF) 22 else() 23 find_program(DOCBOOK_TO_MAN NAMES docbook2x-man db2x_docbook2man docbook2man docbook-to-man) 24 if(DOCBOOK_TO_MAN) 25 set(BUILD_doc_default ON) 26 else() 27 set(BUILD_doc_default OFF) 28 endif() 29 endif() 30 31 option(BUILD_tools "build the xmlwf tool for expat library" ${BUILD_tools_default}) 32 option(BUILD_examples "build the examples for expat library" ON) 33 option(BUILD_tests "build the tests for expat library" ON) 34 option(BUILD_shared "build a shared expat library" ON) 35 option(BUILD_doc "build man page for xmlwf" ${BUILD_doc_default}) 36 option(USE_libbsd "utilize libbsd (for arc4random_buf)" OFF) 37 option(INSTALL "install expat files in cmake install target" ON) 38 39 if(USE_libbsd) 40 find_library(LIB_BSD NAMES bsd) 41 if(NOT LIB_BSD) 42 message(FATAL_ERROR "USE_libbsd option is enabled, but libbsd was not found") 43 else() 44 set(HAVE_LIBBSD TRUE) 45 endif() 46 endif() 47 48 # configuration options 49 set(XML_CONTEXT_BYTES 1024 CACHE STRING "Define to specify how much context to retain around the current parse point") 50 option(XML_DTD "Define to make parameter entity parsing functionality available" ON) 51 option(XML_NS "Define to make XML Namespaces functionality available" ON) 52 option(WARNINGS_AS_ERRORS "Treat all compiler warnings as errors" OFF) 53 if(NOT WIN32) 54 option(XML_DEV_URANDOM "Define to include code reading entropy from `/dev/urandom'." ON) 55 if(XML_DEV_URANDOM) 56 set(XML_DEV_URANDOM 1) 57 else(XML_DEV_URANDOM) 58 set(XML_DEV_URANDOM 0) 59 endif(XML_DEV_URANDOM) 60 endif() 61 option(XML_UNICODE "Use UTF-16 encoded chars (two bytes) instead of UTF-8" OFF) 62 option(XML_UNICODE_WCHAR_T "Use wchar_t to represent UTF-16 instead of unsigned short" OFF) 63 64 if(XML_DTD) 65 set(XML_DTD 1) 66 else(XML_DTD) 67 set(XML_DTD 0) 68 endif(XML_DTD) 69 if(XML_NS) 70 set(XML_NS 1) 71 else(XML_NS) 72 set(XML_NS 0) 73 endif(XML_NS) 74 if(XML_UNICODE) 75 set(XML_UNICODE 1) 76 else(XML_UNICODE) 77 set(XML_UNICODE 0) 78 endif(XML_UNICODE) 79 if(XML_UNICODE_WCHAR_T) 80 set(XML_UNICODE_WCHAR_T 1) 81 else(XML_UNICODE_WCHAR_T) 82 set(XML_UNICODE_WCHAR_T 0) 83 endif(XML_UNICODE_WCHAR_T) 84 85 if(XML_UNICODE_WCHAR_T AND NOT XML_UNICODE) 86 message(SEND_ERROR "Option XML_UNICODE_WCHAR_T=ON may not be used without XML_UNICODE=ON.") 87 set(XML_UNICODE 1) 88 endif(XML_UNICODE_WCHAR_T AND NOT XML_UNICODE) 89 90 if(BUILD_tools AND (XML_UNICODE AND NOT XML_UNICODE_WCHAR_T)) 91 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.") 92 endif() 93 94 if(BUILD_tests) 95 enable_testing() 96 endif(BUILD_tests) 97 98 include(${CMAKE_CURRENT_LIST_DIR}/ConfigureChecks.cmake) 99 100 set(EXTRA_COMPILE_FLAGS) 101 if(FLAG_NO_STRICT_ALIASING) 102 set(EXTRA_COMPILE_FLAGS "${EXTRA_COMPILE_FLAGS} -fno-strict-aliasing") 103 endif() 104 if (WARNINGS_AS_ERRORS) 105 if(MSVC) 106 add_definitions(/WX) 107 else(MSVC) 108 set(EXTRA_COMPILE_FLAGS "${EXTRA_COMPILE_FLAGS} -Werror") 109 endif(MSVC) 110 endif(WARNINGS_AS_ERRORS) 111 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_COMPILE_FLAGS}") 112 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${EXTRA_COMPILE_FLAGS}") 113 114 set(MSVC_USE_STATIC_CRT OFF CACHE BOOL "Use /MT flag (static CRT) when compiling in MSVC") 115 if (MSVC) 116 if (MSVC_USE_STATIC_CRT) 117 message("-- Using static CRT ${MSVC_USE_STATIC_CRT}") 118 foreach(flag_var 119 CMAKE_CXX_FLAGS_DEBUG 120 CMAKE_CXX_FLAGS_RELEASE 121 CMAKE_CXX_FLAGS_MINSIZEREL 122 CMAKE_CXX_FLAGS_RELWITHDEBINFO 123 CMAKE_C_FLAGS_DEBUG 124 CMAKE_C_FLAGS_RELEASE 125 CMAKE_C_FLAGS_MINSIZEREL 126 CMAKE_C_FLAGS_RELWITHDEBINFO 127 ) 128 string(REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}") 129 endforeach() 130 endif() 131 endif() 132 133 include_directories(${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/lib) 134 if(MSVC) 135 add_definitions(-D_CRT_SECURE_NO_WARNINGS -wd4996) 136 endif(MSVC) 137 if(WIN32) 138 set(CMAKE_DEBUG_POSTFIX "d" CACHE STRING "Add a suffix, usually d on Windows") 139 endif(WIN32) 140 141 set(expat_SRCS 142 lib/loadlibrary.c 143 lib/xmlparse.c 144 lib/xmlrole.c 145 lib/xmltok.c 146 lib/xmltok_impl.c 147 lib/xmltok_ns.c 148 ) 149 150 if(BUILD_shared) 151 set(_SHARED SHARED) 152 if(WIN32) 153 set(expat_SRCS ${expat_SRCS} lib/libexpat.def) 154 endif(WIN32) 155 else(BUILD_shared) 156 set(_SHARED STATIC) 157 if(WIN32) 158 add_definitions(-DXML_STATIC) 159 endif(WIN32) 160 endif(BUILD_shared) 161 162 add_library(expat ${_SHARED} ${expat_SRCS}) 163 if(USE_libbsd) 164 target_link_libraries(expat ${LIB_BSD}) 165 endif() 166 167 set(LIBCURRENT 7) # sync 168 set(LIBREVISION 8) # with 169 set(LIBAGE 6) # configure.ac! 170 math(EXPR LIBCURRENT_MINUS_AGE "${LIBCURRENT} - ${LIBAGE}") 171 172 if(NOT WIN32) 173 set_property(TARGET expat PROPERTY VERSION ${LIBCURRENT_MINUS_AGE}.${LIBAGE}.${LIBREVISION}) 174 set_property(TARGET expat PROPERTY SOVERSION ${LIBCURRENT_MINUS_AGE}) 175 set_property(TARGET expat PROPERTY NO_SONAME ${NO_SONAME}) 176 endif(NOT WIN32) 177 178 macro(expat_install) 179 if(INSTALL) 180 install(${ARGN}) 181 endif() 182 endmacro() 183 184 expat_install(TARGETS expat RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} 185 LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} 186 ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}) 187 188 set(prefix ${CMAKE_INSTALL_PREFIX}) 189 set(exec_prefix "\${prefix}") 190 set(libdir "\${exec_prefix}/lib") 191 set(includedir "\${prefix}/include") 192 configure_file(expat.pc.in ${CMAKE_CURRENT_BINARY_DIR}/expat.pc @ONLY) 193 194 expat_install(FILES lib/expat.h lib/expat_external.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) 195 expat_install(FILES ${CMAKE_CURRENT_BINARY_DIR}/expat.pc DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig) 196 197 if(BUILD_tools) 198 set(xmlwf_SRCS 199 xmlwf/xmlwf.c 200 xmlwf/xmlfile.c 201 xmlwf/codepage.c 202 xmlwf/readfilemap.c 203 ) 204 205 add_executable(xmlwf ${xmlwf_SRCS}) 206 set_property(TARGET xmlwf PROPERTY RUNTIME_OUTPUT_DIRECTORY xmlwf) 207 target_link_libraries(xmlwf expat) 208 expat_install(TARGETS xmlwf DESTINATION ${CMAKE_INSTALL_BINDIR}) 209 if(BUILD_doc) 210 add_custom_command(TARGET expat PRE_BUILD COMMAND "${DOCBOOK_TO_MAN}" "${PROJECT_SOURCE_DIR}/doc/xmlwf.xml" && mv "XMLWF.1" "${PROJECT_SOURCE_DIR}/doc/xmlwf.1") 211 expat_install(FILES "${PROJECT_SOURCE_DIR}/doc/xmlwf.1" DESTINATION ${CMAKE_INSTALL_MANDIR}/man1) 212 endif() 213 endif() 214 215 if(BUILD_examples) 216 add_executable(elements examples/elements.c) 217 set_property(TARGET elements PROPERTY RUNTIME_OUTPUT_DIRECTORY examples) 218 target_link_libraries(elements expat) 219 220 add_executable(outline examples/outline.c) 221 set_property(TARGET outline PROPERTY RUNTIME_OUTPUT_DIRECTORY examples) 222 target_link_libraries(outline expat) 223 endif(BUILD_examples) 224 225 if(BUILD_tests) 226 ## these are unittests that can be run on any platform 227 add_executable(runtests tests/runtests.c tests/chardata.c tests/structdata.c tests/minicheck.c tests/memcheck.c) 228 set_property(TARGET runtests PROPERTY RUNTIME_OUTPUT_DIRECTORY tests) 229 target_link_libraries(runtests expat) 230 add_test(runtests tests/runtests) 231 232 add_executable(runtestspp tests/runtestspp.cpp tests/chardata.c tests/structdata.c tests/minicheck.c tests/memcheck.c) 233 set_property(TARGET runtestspp PROPERTY RUNTIME_OUTPUT_DIRECTORY tests) 234 target_link_libraries(runtestspp expat) 235 add_test(runtestspp tests/runtestspp) 236 endif(BUILD_tests) 237