# $Id: CMakeLists.txt 332 2013-04-03 14:24:08Z roland_schwarz $
# This is the CMake project file for the libe57 reference implementation
#
# Copyright 2010 Roland Schwarz, Riegl LMS GmbH
#
# Permission is hereby granted, free of charge, to any person or organization
# obtaining a copy of the software and accompanying documentation covered by
# this license (the "Software") to use, reproduce, display, distribute,
# execute, and transmit the Software, and to prepare derivative works of the
# Software, and to permit third-parties to whom the Software is furnished to
# do so, all subject to the following:
#
# The copyright notices in the Software and this entire statement, including
# the above license grant, this restriction and the following disclaimer,
# must be included in all copies of the Software, in whole or in part, and
# all derivative works of the Software, unless such copies or derivative
# works are solely in the form of machine-executable object code generated by
# a source language processor.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
# SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
# FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.

cmake_minimum_required(VERSION 2.8.2)

# Override flags to enable prepare for linking to static runtime
set(CMAKE_USER_MAKE_RULES_OVERRIDE ${CMAKE_CURRENT_SOURCE_DIR}/../../cmake/c_flag_overrides.cmake)
set(CMAKE_USER_MAKE_RULES_OVERRIDE_CXX ${CMAKE_CURRENT_SOURCE_DIR}/../../cmake/cxx_flag_overrides.cmake)

# Set a private module find path
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/../../cmake/Modules/")

include(CMakeGenerateDocumentation)

project(E57RefImpl_doc)
# developer adjustable version numbers
set(${PROJECT_NAME}_MAJOR_VERSION 1)
set(${PROJECT_NAME}_MINOR_VERSION 1)

include(Tags)

# propose a default installation directory
if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
    string(REGEX REPLACE "/${PROJECT_NAME}" "" CMAKE_INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX})
    set(T_ ${PROJECT_NAME})
    set(T_ ${T_}-${${PROJECT_NAME}_MAJOR_VERSION})
    set(T_ ${T_}.${${PROJECT_NAME}_MINOR_VERSION})
    set(T_ ${T_}.${${PROJECT_NAME}_BUILD_VERSION})
    # set(T_ ${T_}-${${PROJECT_NAME}_BUILD_TAG})
    set(CMAKE_INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX}/${T_}
        CACHE PATH
        "Install path prefix, prepended onto install directories."
        FORCE
    )
endif(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)

find_package(Threads REQUIRED)

# Find the Boost and Xerces libraries

set(Boost_USE_STATIC_LIBS   ON)
set(Boost_USE_STATIC_RUNTIME ON)
set(Boost_USE_MULTITHREADED ON)
find_package(Boost
    COMPONENTS
        program_options
        system
        thread
        filesystem
    QUIET
)
if (NOT Boost_FOUND)
    set(BOOST_ROOT CACHE PATH  "Location of the boost root directory" )
    message(FATAL_ERROR
"Unable to find boost library.
Please set the BOOST_ROOT to point to the boost distribution files."
)
endif(NOT Boost_FOUND)

set(Xerces_USE_STATIC_LIBS On)
find_package(Xerces QUIET)
if (NOT Xerces_FOUND)
    set(XERCES_ROOT CACHE PATH "Location of the xerces library")
    message(FATAL_ERROR
"Unable to find xerces library.
Please set the the XERCES_ROOT to point to the root of the xerces directory."
)
endif (NOT Xerces_FOUND)

set (XML_LIBRARIES ${Xerces_LIBRARY})
set (XML_INCLUDE_DIRS ${Xerces_INCLUDE_DIR})

if (${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
    add_definitions(-DLINUX)
    find_package(ICU REQUIRED)
    set (XML_LIBRARIES ${XML_LIBRARIES} ${ICU_LIBRARIES})
    set (XML_INCLUDE_DIRS ${XML_INCLUDE_DIRS} ${ICU_INCLUDE_DIRS})
elseif(${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
    add_definitions(-DWINDOWS)
endif()

add_definitions(${Boost_LIB_DIAGNOSTIC_DEFINITIONS})
add_definitions(-DBOOST_ALL_NO_LIB -DXERCES_STATIC_LIBRARY)

configure_file (
  "${PROJECT_SOURCE_DIR}/../../include/config.h.in"
  "${PROJECT_BINARY_DIR}/include/config.h"
)

include_directories(
    ${PROJECT_BINARY_DIR}/include
    ../../include
    ../../src/refimpl
    ${XML_INCLUDE_DIRS}
    ${Boost_INCLUDE_DIR}
)

link_directories(
    ${Boost_LIBRARY_DIRS}
)

#
# The reference implementation
#

add_library( E57RefImpl STATIC
    ../../src/refimpl/E57Foundation.cpp
    ../../src/refimpl/E57FoundationImpl.cpp
    ../../src/refimpl/E57FoundationImpl.h
    ../../include/E57Foundation.h
)
set_target_properties( E57RefImpl
    PROPERTIES DEBUG_POSTFIX "-d"
)

#
# E57-4 Tools needed for doc building
#

add_executable( e57xmldump
    ../../src/tools/e57xmldump.cpp
)
target_link_libraries( e57xmldump
    E57RefImpl
    ${XML_LIBRARIES}
    ${CMAKE_THREAD_LIBS_INIT}
)

#
# Documentation
#

set(DOC_EXAMPLES
    examples/HelloWorld.cpp
    examples/Cancel.cpp
    examples/Extensions.cpp
    examples/NameParse.cpp
    examples/ImageFileDump.cpp
    examples/NodeFunctions.cpp
    examples/StructureCreate.cpp
    examples/VectorCreate.cpp
    examples/VectorFunctions.cpp
    examples/IntegerCreate.cpp
    examples/ScaledIntegerCreate.cpp
    examples/FloatCreate.cpp
    examples/StringCreate.cpp
    examples/BlobCreate.cpp
    examples/CompressedVectorCreate.cpp
    examples/SourceDestBufferNumericCreate.cpp
    examples/SourceDestBufferStringCreate.cpp
    examples/E57ExceptionFunctions.cpp
    examples/RawXML.cpp
    examples/Versions.cpp
    examples/SourceDestBufferFunctions.cpp
    examples/CheckInvariant.cpp
)

file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/examples)

generate_documentation(doc
    Doxyfile
    DOXYVAR_INPUT
        ../../src/refimpl/E57Foundation.cpp
        ../../include/E57Foundation.h
        ${DOC_EXAMPLES}
    DOXYVAR_OUTPUT_DIRECTORY
        ${PROJECT_BINARY_DIR}
    DOXYVAR_EXAMPLE_PATH
        examples
        ${PROJECT_BINARY_DIR}/examples
        ../../src/refimpl
)

foreach(X ${DOC_EXAMPLES})
    get_filename_component(F ${X} NAME_WE)
    add_executable(${F} ${X})
    target_link_libraries(${F}
        E57RefImpl
        ${XML_LIBRARIES}
        ${CMAKE_THREAD_LIBS_INIT}
    )
    add_custom_command(TARGET ${F} POST_BUILD
        COMMAND ${F} > examples/${F}.out
        COMMAND e57xmldump temp._e57 > examples/${F}.xml
    )
    add_dependencies(doc ${F})
endforeach()

install(
    DIRECTORY
        ${PROJECT_BINARY_DIR}/html
    DESTINATION
        .
)

install(
    FILES
        ../libE57ReleaseNotes.doc
        ../las2e57.doc
        ../e57xmldump.doc
        ../e57fields.doc
    DESTINATION
        .
)
