# This is a rewrite over time of the CMake file from the libe57 reference implementation
#   https://en.wikipedia.org/wiki/Ship_of_Theseus
#
# Use git blame to see all the changes and who has contributed.
#
# Original work Copyright 2010-2012 Roland Schwarz, Riegl LMS GmbH
# Modified work Copyright 2018-2020 Andy Maloney <asmaloney@gmail.com>
#
# 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 3.10.0 )

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

project( E57Format
	DESCRIPTION
	    "E57Format is a library to read and write E57 files"
	LANGUAGES
	    CXX
	VERSION
	    2.2.1
)

include( Tags )

# Check if we are building ourself or being included and use this to set some defaults
if ( ${PROJECT_NAME} STREQUAL ${CMAKE_PROJECT_NAME} )
    set( E57_BUILDING_SELF ON )
endif()

# propose a default installation directory
if ( E57_BUILDING_SELF )
    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_VERSION_MAJOR}.${PROJECT_VERSION_MINOR} )
        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()
endif()

find_package( Threads REQUIRED )
find_package( XercesC REQUIRED )

option( E57_BUILD_SHARED
	"Compile E57Format as a shared library"
	OFF
)

if( BUILD_SHARED_LIBS )
	set( E57_BUILD_SHARED ON )
endif()

#########################################################################################

# Various levels of cross checking and verification in the code.
# The extra code does not change the file contents.
# Recommend that E57_DEBUG remain defined even for production versions.

option( E57_DEBUG      "Compile library with minimal debug checking" ON )
option( E57_MAX_DEBUG  "Compile library with maximum debug checking" OFF )

# Various levels of printing to the console of what is going on in the code.
# Optional

option( E57_VERBOSE      "Compile library with verbose logging" OFF )
option( E57_MAX_VERBOSE  "Compile library with maximum verbose logging" OFF )

# Enable writing packets that are correct but will stress the reader.

option( E57_WRITE_CRAZY_PACKET_MODE "Compile library to enable reader-stressing packets" OFF )

#########################################################################################

set( revision_id "${PROJECT_NAME}-${PROJECT_VERSION}-${${PROJECT_NAME}_BUILD_TAG}" )
message( STATUS "[E57] Revison ID: ${revision_id}" )

# Need to explicitly set the source files to add_library()
if(${CMAKE_VERSION} VERSION_LESS "3.11.0")
	file(GLOB E57Format_SOURCES src/[^.]*.cpp)
endif()

# Target
if ( E57_BUILD_SHARED )
	message( STATUS "[E57] Building shared library" )
	add_library( E57Format SHARED ${E57Format_SOURCES})
else()
	message( STATUS "[E57] Building static library" )
	add_library( E57Format STATIC ${E57Format_SOURCES})
endif()

include( E57ExportHeader )

# Main sources and includes
add_subdirectory( extern/CRCpp )
add_subdirectory( include )
add_subdirectory( src )

include( ClangFormat )

# Target properties
set_target_properties( E57Format
	PROPERTIES
	    CXX_STANDARD 11
		CXX_STANDARD_REQUIRED YES
		CXX_EXTENSIONS NO
		DEBUG_POSTFIX "-d"
		POSITION_INDEPENDENT_CODE ON
)

# Target definitions
target_compile_definitions( E57Format
    PRIVATE
        CRCPP_USE_CPP11
        CRCPP_BRANCHLESS
        REVISION_ID="${revision_id}"
)

if ( E57_DEBUG )
    target_compile_definitions( E57Format PRIVATE E57_DEBUG )
endif()

if ( E57_MAX_DEBUG )
    target_compile_definitions( E57Format PRIVATE E57_MAX_DEBUG )
endif()

if ( E57_VERBOSE )
    target_compile_definitions( E57Format PRIVATE E57_VERBOSE )
endif()

if ( E57_MAX_VERBOSE )
    target_compile_definitions( E57Format PRIVATE E57_MAX_VERBOSE )
endif()

if ( E57_WRITE_CRAZY_PACKET_MODE )
    target_compile_definitions( E57Format PRIVATE E57_WRITE_CRAZY_PACKET_MODE )
endif()

if ( WIN32 )
    option( USING_STATIC_XERCES "Turn on if you are linking with Xerces as a static lib" OFF )
    if ( USING_STATIC_XERCES )
        target_compile_definitions( E57Format
            PUBLIC
                XERCES_STATIC_LIBRARY
        )
    endif()
endif()

# Target Libraries
target_link_libraries( E57Format PRIVATE XercesC::XercesC )

# Install
install(
    TARGETS
        E57Format
    EXPORT
        E57Format-export
    ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)

# CMake package files
#install(
#    EXPORT
#        E57Format-export
#    DESTINATION lib/cmake/E57Format
#)

#install(
#    FILES
#        ${CMAKE_CURRENT_SOURCE_DIR}/cmake/e57format-config.cmake
#    DESTINATION
#        lib/cmake/E57Format
#)
