# This file is copyrighted under the BSD-license for buildsystem files of KDE
# copyright 2010, Patrick Spendrin <ps_ml@gmx.de>

project(libxml2)

cmake_minimum_required(VERSION 2.6)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules)

set(PACKAGE "libxml2")
set(VERSION "2.7.7")

find_package(ZLIB)
find_package(Libiconv REQUIRED)

include_directories(
    ${CMAKE_BINARY_DIR} 
    ${CMAKE_BINARY_DIR}/include
    ${CMAKE_SOURCE_DIR}/include 
    ${ZLIB_INCLUDE_DIR}
    ${Libiconv_INCLUDE_DIR}
    ${CMAKE_SOURCE_DIR}/win32/wince
)

if(WINCE)
    find_package(Wcecompat REQUIRED)
    include_directories(${WCECOMPAT_INCLUDE_DIR})
    set(CMAKE_REQUIRED_INCLUDES ${WCECOMPAT_INCLUDE_DIR})
    set(CMAKE_REQUIRED_LIBRARIES ${WCECOMPAT_LIBRARIES})
endif(WINCE)

option(SUPPORT_IP6 "support IPv6" ON)
option(ENABLE_THREADS "enable threading in libxml" ON)
option(ENABLE_TRIO "enable trio IO in libxml" OFF)
option(BUILD_tests "build all the test executables in libxml" ON)

if(ENABLE_TRIO)
    set(WITH_TRIO 1)
else()
    set(WITH_TRIO 0)
endif()    

if(ENABLE_THREADS)
    set(WITH_THREADS 1)
else()
    set(WITH_THREADS 0)
endif()    
set(WITH_TREE 1)
set(WITH_OUTPUT 1)
set(WITH_PUSH 1)
set(WITH_READER 1)
set(WITH_PATTERN 1)
set(WITH_WRITER 1)
set(WITH_SAX1 1)
set(WITH_FTP 1)
set(WITH_HTTP 1)
set(WITH_VALID 1)
set(WITH_HTML 1)
set(WITH_LEGACY 1)
set(WITH_C14N 1)
set(WITH_CATALOG 1)
set(WITH_DOCB 1)
set(WITH_XPATH 1)
set(WITH_XPTR 1)
set(WITH_XINCLUDE 1)
set(WITH_ICONV 1)
set(WITH_ISO8859X 1)
set(WITH_DEBUG 0) #setting to one may result into a crash in case http://mail.gnome.org/archives/xml/2011-April/msg00011.html has not been fixed
set(WITH_MEM_DEBUG 0)
set(WITH_RUN_DEBUG 0) #setting to one may result into a crash in case http://mail.gnome.org/archives/xml/2011-April/msg00011.html has not been fixed
set(WITH_REGEXPS 1)
set(WITH_SCHEMAS 1)
set(WITH_SCHEMATRON 1)
set(WITH_MODULES 1)
if(ZLIB_FOUND)
    set(WITH_ZLIB 1)
else()
    set(WITH_ZLIB 0)
endif()

set (LIBXML_VERSION_EXTRA 0)
STRING(REPLACE "." "0" LIBXML_VERSION_NUMBER ${VERSION})

include(ConfigureChecks.cmake)

######################################### Library ###############################################

set(libxml2_SRCS
    SAX.c 
    entities.c 
    encoding.c 
    error.c 
    parserInternals.c
    parser.c
    tree.c
    hash.c
    list.c
    xmlIO.c
    xmlmemory.c
    uri.c
    valid.c
    xlink.c
    HTMLparser.c
    HTMLtree.c
    debugXML.c
    xpath.c
    xpointer.c
    xinclude.c
    nanohttp.c
    nanoftp.c
    DOCBparser.c
    catalog.c
    globals.c
    threads.c
    c14n.c
    xmlstring.c
    xmlregexp.c
    xmlschemas.c
    xmlschemastypes.c
    xmlunicode.c
    xmlreader.c
    relaxng.c
    dict.c
    SAX2.c
    xmlwriter.c
    legacy.c
    chvalid.c
    pattern.c
    xmlsave.c
    xmlmodule.c
    schematron.c
)

# the trio code is yet untested, you need to change the xmlversion.h for that too
# this is currently not supported as you'd need to configure the xmlversion.h.in
if(ENABLE_TRIO)
    set(libxml2_SRCS
        ${libxml2_SRCS}
        triostr.c 
        trio.c
    )
endif(ENABLE_TRIO)

if(WINCE)
add_library(xml2 STATIC ${libxml2_SRCS})
else(WINCE)
add_library(xml2 SHARED ${libxml2_SRCS})
endif(WINCE)
target_link_libraries(xml2 ${LIBICONV_LIBRARIES})
install(TARGETS xml2    RUNTIME DESTINATION bin
                        LIBRARY DESTINATION lib
                        ARCHIVE DESTINATION lib)

if(WIN32)
    if(MSVC)
        set_target_properties(xml2 PROPERTIES OUTPUT_NAME libxml2)
    endif(MSVC)
    if(WINCE)
        target_link_libraries(xml2 ws2)
    else(WINCE)
        target_link_libraries(xml2 ws2_32)
    endif(WINCE)
endif(WIN32)

if(ZLIB_FOUND)
    target_link_libraries(xml2 ${ZLIB_LIBRARIES})
endif(ZLIB_FOUND)


######################################### Utilities ###############################################

# For WINCE the utils are not compiled
if(NOT WINCE)
add_executable(xmllint xmllint.c)
target_link_libraries(xmllint xml2)
install(TARGETS xmllint RUNTIME DESTINATION bin
                        LIBRARY DESTINATION lib
                        ARCHIVE DESTINATION lib)

add_executable(xmlcatalog xmlcatalog.c)
target_link_libraries(xmlcatalog xml2)
install(TARGETS xmlcatalog  RUNTIME DESTINATION bin
                            LIBRARY DESTINATION lib
                            ARCHIVE DESTINATION lib)
                            
endif(NOT WINCE)

######################################### headers & docs ###############################################
set(libxml2_HDRS
include/libxml/SAX.h
include/libxml/entities.h
include/libxml/encoding.h
include/libxml/parser.h
include/libxml/parserInternals.h
include/libxml/xmlerror.h
include/libxml/HTMLparser.h
include/libxml/HTMLtree.h
include/libxml/debugXML.h
include/libxml/tree.h
include/libxml/list.h
include/libxml/hash.h
include/libxml/xpath.h
include/libxml/xpathInternals.h
include/libxml/xpointer.h
include/libxml/xinclude.h
include/libxml/xmlIO.h
include/libxml/xmlmemory.h
include/libxml/nanohttp.h
include/libxml/nanoftp.h
include/libxml/uri.h
include/libxml/valid.h
include/libxml/xlink.h
${CMAKE_BINARY_DIR}/include/libxml/xmlversion.h
include/libxml/DOCBparser.h
include/libxml/catalog.h
include/libxml/threads.h
include/libxml/globals.h
include/libxml/c14n.h
include/libxml/xmlautomata.h
include/libxml/xmlregexp.h
include/libxml/xmlmodule.h
include/libxml/xmlschemas.h
include/libxml/schemasInternals.h
include/libxml/xmlschemastypes.h
include/libxml/xmlstring.h
include/libxml/xmlunicode.h
include/libxml/xmlreader.h
include/libxml/relaxng.h
include/libxml/dict.h
include/libxml/SAX2.h
include/libxml/xmlexports.h
include/libxml/xmlwriter.h
include/libxml/chvalid.h
include/libxml/pattern.h
include/libxml/xmlsave.h
include/libxml/schematron.h)

if(WINCE)
set(libxml2_HDRS
    ${libxml2_HDRS}
    win32/wince/wincecompat.h)
endif(WINCE)

install(FILES ${libxml2_HDRS} DESTINATION include/libxml)

set(_html_DOCS 
doc/xml.html
doc/encoding.html
doc/FAQ.html
doc/structure.gif
doc/DOM.gif
doc/smallfootonly.gif
doc/redhat.gif
doc/libxml.gif
doc/w3c.png
doc/Libxml2-Logo-180x168.gif
doc/Libxml2-Logo-90x34.gif
)

file(GLOB html_DOCS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "${CMAKE_CURRENT_SOURCE_DIR}/doc/html/*.html")
file(GLOB png_DOCS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "${CMAKE_CURRENT_SOURCE_DIR}/doc/html/*.png")
install(FILES ${html_DOCS} ${png_DOCS} DESTINATION doc/libxml/html)

file(GLOB tut_DOCS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "${CMAKE_CURRENT_SOURCE_DIR}/doc/tutorial/*.*")
install(FILES ${tut_DOCS} DESTINATION doc/libxml/html/tutorial)


######################################### Tests ###############################################
if(BUILD_tests)
macro(buildTests)
    foreach(target ${ARGN})
        add_executable(${target} ${target}.c)
        target_link_libraries(${target} xml2)
        if(WINCE)
            target_link_libraries(${target} ${WCECOMPAT_LIBRARIES})
        endif(WINCE)
    endforeach(target ${ARGN})
endmacro(buildTests)

buildTests(
    testSchemas 
    testRelax 
    testSAX 
    testHTML 
    testXPath 
    testURI
    testC14N
    testAutomata
    testRegexp
    testReader
    testapi
    testModule
    runtest
    runsuite
    testchar
    testdict
    runxmlconf
    testrecurse
)
if(WIN32)
    buildTests(
        testThreadsWin32
    )
else(WIN32)
    buildTests(
        testThreads
    )
endif(WIN32)
endif(BUILD_tests)
