project(raptor2)

cmake_minimum_required(VERSION 2.8)


set(RAPTOR_VERSION_MAJOR 2)
set(RAPTOR_VERSION_MINOR 0)
set(RAPTOR_VERSION_RELEASE 8)
set(VERSION "\"${RAPTOR_VERSION_MAJOR}.${RAPTOR_VERSION_MINOR}.${RAPTOR_VERSION_RELEASE}\"")
math(EXPR RAPTOR_VERSION_DECIMAL "${RAPTOR_VERSION_MAJOR} * 10000 + ${RAPTOR_VERSION_MINOR} * 100 + ${RAPTOR_VERSION_RELEASE}")

set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake/modules)
find_package(CURL REQUIRED)
find_package(LibXml2 REQUIRED)
find_package(LibXslt REQUIRED)
find_package(Expat REQUIRED)
find_package(Yajl REQUIRED)

option(BUILD_STATIC "build static library" OFF)
option(BUILD_TOOLS "build the tools" ON)
option(BUILD_TESTS "build the tests" ON)

if(BUILD_TESTS)
    enable_testing()
endif(BUILD_TESTS)

include(ConfigureChecks.cmake)
configure_file(src/raptor2.h.in ${CMAKE_BINARY_DIR}/raptor2.h @ONLY)

if(MSVC)
    add_definitions(-D_CRT_SECURE_NO_WARNINGS -wd4996 -Dstrcasecmp=stricmp -Dstrncasecmp=strnicmp)
endif(MSVC)

set(raptor2_SRCS
    src/raptor_parse.c
    src/raptor_serialize.c
    src/raptor_rfc2396.c
    src/raptor_uri.c
    src/raptor_log.c
    src/raptor_locator.c
    src/raptor_namespace.c
    src/raptor_qname.c
    src/raptor_option.c
    src/raptor_general.c
    src/raptor_unicode.c
    src/raptor_www.c
    src/raptor_statement.c
    src/raptor_term.c
    src/raptor_sequence.c
    src/raptor_stringbuffer.c
    src/raptor_iostream.c
    src/raptor_xml.c
    src/raptor_xml_writer.c
    src/raptor_set.c
    src/turtle_common.c
    src/raptor_turtle_writer.c
    src/raptor_avltree.c
    src/snprintf.c
    src/raptor_json_writer.c
    src/raptor_memstr.c
    src/raptor_concepts.c
    src/raptor_syntax_description.c
)

    if(LIBXML2_FOUND)
        list(APPEND raptor2_SRCS
            src/raptor_libxml.c
        )
    endif(LIBXML2_FOUND)

    list(APPEND raptor2_SRCS
        src/raptor_rdfxml.c
        src/raptor_sax2.c
        src/turtle_lexer.c
        src/turtle_parser.c
        src/ntriples_parse.c
        src/raptor_rss_common.c
        src/raptor_rss.c
        src/raptor_grddl.c
        src/raptor_guess.c
        src/raptor_librdfa.c
        src/raptor_json.c
        src/raptor_serialize_rdfxml.c
        src/raptor_serialize_ntriples.c
        src/raptor_abbrev.c
        src/raptor_serialize_rdfxmla.c
        src/raptor_serialize_turtle.c
        src/raptor_serialize_rss.c
        src/raptor_serialize_dot.c
        src/raptor_serialize_html.c
        src/raptor_serialize_json.c
        src/raptor_www_libxml.c
        src/raptor_www_curl.c
        src/raptor_www.c
        src/strcasecmp.c
        src/parsedate.c
        librdfa/context.c
        librdfa/curie.c
        librdfa/iri.c
        librdfa/language.c
        librdfa/lists.c
        librdfa/namespace.c
        librdfa/rdfa.c
        librdfa/rdfa_utils.c
        librdfa/subject.c
        librdfa/triple.c
)

if(NOT HAVE_STRTOK_R)
    list(APPEND raptor2_SRCS
        librdfa/strtok_r.c
    )
endif(NOT HAVE_STRTOK_R)

set(raptor2_HDRS
    src/raptor.h
    ${CMAKE_BINARY_DIR}/raptor2.h
)

add_definitions(-DLIBRDFA_IN_RAPTOR -DRAPTOR_INTERNAL)

include_directories(
    ${CMAKE_BINARY_DIR}
    librdfa
    src
    utils
    ${CURL_INCLUDE_DIR}
    ${LIBXML2_INCLUDE_DIR}
    ${YAJL_INCLUDE_DIR}
    ${EXPAT_INCLUDE_DIR}
)

if(BUILD_STATIC)
    add_definitions(-DRAPTOR_STATIC)
    set(_SHARED STATIC)
else(BUILD_STATIC)
    set(_SHARED SHARED)
endif(BUILD_STATIC)

add_library(raptor2 ${_SHARED} ${raptor2_SRCS})
set_target_properties(raptor2 PROPERTIES DEFINE_SYMBOL RAPTOR_EXPORTS)
set(raptor2_LIBS ${CURL_LIBRARIES} ${LIBXML2_LIBRARIES} ${LIBXSLT_LIBRARIES} ${EXPAT_LIBRARIES} ${YAJL_LIBRARIES})
target_link_libraries(raptor2 ${raptor2_LIBS})

set(_targets
    raptor2
)

###############################################################################
if(BUILD_TESTS OR BUILD_TOOLS)
    add_library(raptor2_static STATIC ${raptor2_SRCS})
    set_target_properties(raptor2_static PROPERTIES COMPILE_FLAGS "-DRAPTOR_STATIC")
endif(BUILD_TESTS OR BUILD_TOOLS)
###############################################################################
if(BUILD_TESTS)
    set(raptor_TESTS
        raptor_parse
        raptor_rfc2396
        raptor_uri
        raptor_namespace
        strcasecmp
        raptor_www
        raptor_sequence
        raptor_stringbuffer
        raptor_uri_win32
        raptor_iostream
        raptor_xml_writer
        raptor_turtle_writer
        raptor_avltree
        raptor_term
        raptor_set
        raptor_xml
    )

    set(raptor_parse_SRCS src/raptor_general.c src/raptor_parse.c)
    set(raptor_uri_win32_SRCS src/raptor_uri.c)
    set(raptor_www_SRCS src/raptor_www_test.c)
    set(raptor_nfc_SRCS src/raptor_nfc_test.c)
    foreach(_test ${raptor_TESTS})
        if(NOT DEFINED ${_test}_SRCS)
            set(${_test}_SRCS src/${_test}.c)
        endif(NOT DEFINED ${_test}_SRCS)
        add_executable(${_test} ${${_test}_SRCS})
        set_target_properties(${_test} PROPERTIES COMPILE_FLAGS "-DSTANDALONE -DRAPTOR_STATIC")
        target_link_libraries(${_test} raptor2_static ${raptor2_LIBS})
        add_test(${_test} ${_test})
    endforeach(_test ${raptor_TESTS})
    set_target_properties(raptor_uri_win32 PROPERTIES COMPILE_FLAGS "-DSTANDALONE -DRAPTOR_STATIC -DWIN32_URI_TEST")
endif(BUILD_TESTS)
###############################################################################
if(BUILD_TOOLS)
    set(rapper_SRCS
        utils/rapper.c
    )
    set(rdfdiff_SRCS
        utils/rdfdiff.c
    )
    if(MSVC)
        set(rapper_SRCS utils/getopt.c ${rapper_SRCS})
        set(rdfdiff_SRCS utils/getopt.c ${rdfdiff_SRCS})
    endif(MSVC)
    add_executable(rapper ${rapper_SRCS})
    set_target_properties(rapper PROPERTIES COMPILE_FLAGS "-DRAPTOR_STATIC")
    target_link_libraries(rapper raptor2_static ${raptor2_LIBS})
    add_executable(rdfdiff ${rdfdiff_SRCS})
    set_target_properties(rdfdiff PROPERTIES COMPILE_FLAGS "-DRAPTOR_STATIC")
    target_link_libraries(rdfdiff raptor2_static ${raptor2_LIBS})
    list(APPEND _targets rapper rdfdiff)
endif(BUILD_TOOLS)

###############################################################################
install(TARGETS ${_targets} RUNTIME DESTINATION bin
                            LIBRARY DESTINATION lib
                            ARCHIVE DESTINATION lib)

install(FILES ${raptor2_HDRS} DESTINATION include/raptor2)
