1# This file is copyrighted under the BSD-license for buildsystem files of KDE
2# copyright 2010, Patrick Spendrin <ps_ml@gmx.de>
3
4project(expat)
5
6cmake_minimum_required(VERSION 2.8.10)
7set(PACKAGE_BUGREPORT "expat-bugs@libexpat.org")
8set(PACKAGE_NAME "expat")
9set(PACKAGE_VERSION "2.2.6")
10set(PACKAGE_STRING "${PACKAGE_NAME} ${PACKAGE_VERSION}")
11set(PACKAGE_TARNAME "${PACKAGE_NAME}")
12
13include(GNUInstallDirs)
14
15if(WINCE)
16    set(BUILD_tools_default OFF)
17else()
18    set(BUILD_tools_default ON)
19endif()
20if(MSVC OR NOT BUILD_tools_default)
21    set(BUILD_doc_default OFF)
22else()
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()
29endif()
30
31option(BUILD_tools "build the xmlwf tool for expat library" ${BUILD_tools_default})
32option(BUILD_examples "build the examples for expat library" ON)
33option(BUILD_tests "build the tests for expat library" ON)
34option(BUILD_shared "build a shared expat library" ON)
35option(BUILD_doc "build man page for xmlwf" ${BUILD_doc_default})
36option(USE_libbsd "utilize libbsd (for arc4random_buf)" OFF)
37option(INSTALL "install expat files in cmake install target" ON)
38
39if(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()
46endif()
47
48# configuration options
49set(XML_CONTEXT_BYTES 1024 CACHE STRING "Define to specify how much context to retain around the current parse point")
50option(XML_DTD "Define to make parameter entity parsing functionality available" ON)
51option(XML_NS "Define to make XML Namespaces functionality available" ON)
52option(WARNINGS_AS_ERRORS "Treat all compiler warnings as errors" OFF)
53if(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)
60endif()
61option(XML_UNICODE "Use UTF-16 encoded chars (two bytes) instead of UTF-8" OFF)
62option(XML_UNICODE_WCHAR_T "Use wchar_t to represent UTF-16 instead of unsigned short" OFF)
63
64if(XML_DTD)
65    set(XML_DTD 1)
66else(XML_DTD)
67    set(XML_DTD 0)
68endif(XML_DTD)
69if(XML_NS)
70    set(XML_NS 1)
71else(XML_NS)
72    set(XML_NS 0)
73endif(XML_NS)
74if(XML_UNICODE)
75    set(XML_UNICODE 1)
76else(XML_UNICODE)
77    set(XML_UNICODE 0)
78endif(XML_UNICODE)
79if(XML_UNICODE_WCHAR_T)
80    set(XML_UNICODE_WCHAR_T 1)
81else(XML_UNICODE_WCHAR_T)
82    set(XML_UNICODE_WCHAR_T 0)
83endif(XML_UNICODE_WCHAR_T)
84
85if(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)
88endif(XML_UNICODE_WCHAR_T AND NOT XML_UNICODE)
89
90if(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.")
92endif()
93
94if(BUILD_tests)
95    enable_testing()
96endif(BUILD_tests)
97
98include(${CMAKE_CURRENT_LIST_DIR}/ConfigureChecks.cmake)
99
100set(EXTRA_COMPILE_FLAGS)
101if(FLAG_NO_STRICT_ALIASING)
102    set(EXTRA_COMPILE_FLAGS "${EXTRA_COMPILE_FLAGS} -fno-strict-aliasing")
103endif()
104if (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)
110endif(WARNINGS_AS_ERRORS)
111set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_COMPILE_FLAGS}")
112set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${EXTRA_COMPILE_FLAGS}")
113
114set(MSVC_USE_STATIC_CRT OFF CACHE BOOL "Use /MT flag (static CRT) when compiling in MSVC")
115if (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()
131endif()
132
133include_directories(${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/lib)
134if(MSVC)
135    add_definitions(-D_CRT_SECURE_NO_WARNINGS -wd4996)
136endif(MSVC)
137if(WIN32)
138    set(CMAKE_DEBUG_POSTFIX "d" CACHE STRING "Add a suffix, usually d on Windows")
139endif(WIN32)
140
141set(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
150if(BUILD_shared)
151    set(_SHARED SHARED)
152    if(WIN32)
153        set(expat_SRCS ${expat_SRCS} lib/libexpat.def)
154    endif(WIN32)
155else(BUILD_shared)
156    set(_SHARED STATIC)
157    if(WIN32)
158        add_definitions(-DXML_STATIC)
159    endif(WIN32)
160endif(BUILD_shared)
161
162add_library(expat ${_SHARED} ${expat_SRCS})
163if(USE_libbsd)
164    target_link_libraries(expat ${LIB_BSD})
165endif()
166
167set(LIBCURRENT 7)   # sync
168set(LIBREVISION 8)  # with
169set(LIBAGE 6)       # configure.ac!
170math(EXPR LIBCURRENT_MINUS_AGE "${LIBCURRENT} - ${LIBAGE}")
171
172if(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})
176endif(NOT WIN32)
177
178macro(expat_install)
179    if(INSTALL)
180        install(${ARGN})
181    endif()
182endmacro()
183
184expat_install(TARGETS expat RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
185                      LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
186                      ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
187
188set(prefix ${CMAKE_INSTALL_PREFIX})
189set(exec_prefix "\${prefix}")
190set(libdir "\${exec_prefix}/lib")
191set(includedir "\${prefix}/include")
192configure_file(expat.pc.in ${CMAKE_CURRENT_BINARY_DIR}/expat.pc @ONLY)
193
194expat_install(FILES lib/expat.h lib/expat_external.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
195expat_install(FILES ${CMAKE_CURRENT_BINARY_DIR}/expat.pc DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
196
197if(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()
213endif()
214
215if(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)
223endif(BUILD_examples)
224
225if(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)
236endif(BUILD_tests)
237