# $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-2012 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.
#
# Requirements:
#     Xerces library: http://xerces.apache.org/
# If you find any errors or have suggestion to improve the build script:
# patches are most welcome! Please send them to the development mailing list.


cmake_minimum_required(VERSION 3.1.0 FATAL_ERROR)
set(CMAKE_CXX_STANDARD 11)

# 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_CURRENT_SOURCE_DIR}/cmake/Modules/")

project(E57Format)

# developer adjustable version numbers
set(${PROJECT_NAME}_MAJOR_VERSION 2)
set(${PROJECT_NAME}_MINOR_VERSION 0)
set(${PROJECT_NAME}_PATCH_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_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 )

# Xerces-c
find_package( XercesC )

if ( NOT XercesC_FOUND )
    set( XercesC_INCLUDE_DIR "" CACHE PATH "Xerces include directory" )
endif()

if ( WIN32 )
    option( USING_STATIC_XERCES "Turn on if you are linking with Xerces as a static lib" OFF )

    if ( USING_STATIC_XERCES )
        add_definitions( -DXERCES_STATIC_LIBRARY )
    endif()
endif()

set( XML_INCLUDE_DIRS ${XercesC_INCLUDE_DIR} )

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

add_definitions(-DREVISION_ID="${PROJECT_NAME}-${${PROJECT_NAME}_MAJOR_VERSION}.${${PROJECT_NAME}_MINOR_VERSION}.${${PROJECT_NAME}_PATCH_VERSION}-${${PROJECT_NAME}_BUILD_TAG}")

include_directories(
    ${PROJECT_BINARY_DIR}/include
    include
    src/
    ${XML_INCLUDE_DIRS}
)

# CRCpp from here: https://github.com/d-bahr/CRCpp
add_definitions( -DCRCPP_USE_CPP11 )
add_definitions( -DCRCPP_BRANCHLESS )
include_directories( contrib/CRCpp/inc )

#
# The reference implementation
#

add_library( E57Format STATIC
    src/CheckedFile.h
    src/CheckedFile.cpp
    src/Common.h
    src/Decoder.h
    src/Decoder.cpp
    src/Encoder.h
    src/Encoder.cpp
    src/NodeImpl.h
    src/NodeImpl.cpp
    src/Packet.h
    src/Packet.cpp
    src/ImageFileImpl.cpp
    src/ImageFileImpl.h
    src/SourceDestBufferImpl.h
    src/SourceDestBufferImpl.cpp
    src/StructureNodeImpl.h
    src/StructureNodeImpl.cpp
    src/E57Exception.cpp
    src/E57Format.cpp
    src/E57FormatImpl.cpp
    src/E57FormatImpl.h
    src/E57Version.h
    src/E57XmlParser.h
    src/E57XmlParser.cpp
    include/E57Exception.h
    include/E57Format.h
    contrib/CRCpp/inc/CRC.h
)
set_target_properties( E57Format PROPERTIES
    DEBUG_POSTFIX "-d"
    POSITION_INDEPENDENT_CODE ON
)
