# 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.0
)

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()

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

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

include( E57ExportHeader )

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

# 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 ( 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 external-xerces )

# Install
if (WIN32)
    install(
        TARGETS
            E57Format
        EXPORT
            E57Format-export
        RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}
    )
else()
    install(
        TARGETS
            E57Format
        EXPORT
            E57Format-export
        ARCHIVE DESTINATION lib
        LIBRARY DESTINATION lib
    )
endif()
