cmake_minimum_required(VERSION 3.8)
project(LotRO_dat_library)

set(CMAKE_CXX_STANDARD 11)
set(PROJECT_BINARY_DIR bin)
set(PROJECT_VERSION 0.1.0)

SET(CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} ${GCC_COVERAGE_COMPILE_FLAGS} -O2" )
SET(CMAKE_EXE_LINKER_FLAGS  "${CMAKE_EXE_LINKER_FLAGS} ${GCC_COVERAGE_LINK_FLAGS}")

if (MSVS)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS_RELEASE} /MT /SAFESEH:NO")
endif(MSVS)

set(CMAKE_OUTPUT_DIR ${CMAKE_SOURCE_DIR}/bin)
set(CMAKE_SOURCE_DIR ${CMAKE_SOURCE_DIR}/src)

include_directories(${CMAKE_SOURCE_DIR}/Common/Yaml)

SET(EXECUTABLE_OUTPUT_PATH ${CMAKE_OUTPUT_DIR})
SET(LIBRARY_OUTPUT_PATH ${CMAKE_OUTPUT_DIR})

set(SOURCE_FILES
        ${CMAKE_SOURCE_DIR}/Common/CommonFunctions.h
        ${CMAKE_SOURCE_DIR}/Common/DatException.h
        ${CMAKE_SOURCE_DIR}/LotroDatPatcher.h

        ${CMAKE_SOURCE_DIR}/DatFile.cpp
        ${CMAKE_SOURCE_DIR}/DatFile.h

        ${CMAKE_SOURCE_DIR}/BinaryData.cpp
        ${CMAKE_SOURCE_DIR}/BinaryData.h

        ${CMAKE_SOURCE_DIR}/Database.cpp
        ${CMAKE_SOURCE_DIR}/Database.h

        ${CMAKE_SOURCE_DIR}/Subfile.cpp
        ${CMAKE_SOURCE_DIR}/Subfile.h

        ${CMAKE_SOURCE_DIR}/SubDirectory.cpp
        ${CMAKE_SOURCE_DIR}/SubDirectory.h

        ${CMAKE_SOURCE_DIR}/Common/SQLite/sqlite3.c
        ${CMAKE_SOURCE_DIR}/Common/SQLite/sqlite3.h

        ${CMAKE_SOURCE_DIR}/Subfiles/TextSubfile.cpp
        ${CMAKE_SOURCE_DIR}/Subfiles/TextSubfile.h

        ${CMAKE_SOURCE_DIR}/Subfiles/JpgSubfile.cpp
        ${CMAKE_SOURCE_DIR}/Subfiles/JpgSubfile.h

        ${CMAKE_SOURCE_DIR}/Subfiles/DdsSubfile.cpp
        ${CMAKE_SOURCE_DIR}/Subfiles/DdsSubfile.h

        ${CMAKE_SOURCE_DIR}/Subfiles/OggSubfile.cpp
        ${CMAKE_SOURCE_DIR}/Subfiles/OggSubfile.h

        ${CMAKE_SOURCE_DIR}/Subfiles/FontSubfile.cpp
        ${CMAKE_SOURCE_DIR}/Subfiles/FontSubfile.h

        ${CMAKE_SOURCE_DIR}/Subfiles/WavSubfile.cpp
        ${CMAKE_SOURCE_DIR}/Subfiles/WavSubfile.h

        ${CMAKE_SOURCE_DIR}/Subfiles/UnknownSubfile.cpp
        ${CMAKE_SOURCE_DIR}/Subfiles/UnknownSubfile.h
        src/SubfileData.h)

set(CompilerFlags
        CMAKE_CXX_FLAGS
        CMAKE_CXX_FLAGS_DEBUG
        CMAKE_CXX_FLAGS_RELEASE
        CMAKE_CXX_FLAGS_MINSIZEREL
        CMAKE_CXX_FLAGS_RELWITHDEBINFO
        CMAKE_C_FLAGS
        CMAKE_C_FLAGS_DEBUG
        CMAKE_C_FLAGS_RELEASE
        CMAKE_C_FLAGS_MINSIZEREL
        CMAKE_C_FLAGS_RELWITHDEBINFO
        )

foreach(CompilerFlag ${CompilerFlags})
    string(REPLACE "/MD" "/MT" ${CompilerFlag} "${${CompilerFlag}}")
endforeach()

# STATIC LIBRARY
add_library(LotroDat_static STATIC ${SOURCE_FILES})
# SHARED LIBRARY
add_library(LotroDat SHARED ${SOURCE_FILES})
# CONSOLE EXTRACTOR TOOL
add_executable(LotRO_dat_extract_tester ${SOURCE_FILES} ${CMAKE_SOURCE_DIR}/Examples/extractor_example.cpp)
# CONSOLE PATCHER TOOL
add_executable(LotRO_dat_pat_tester ${SOURCE_FILES} ${CMAKE_SOURCE_DIR}/Examples/patcher_example.cpp)

foreach(TARGET LotroDat_static LotroDat LotRO_dat_extract_tester LotRO_dat_pat_tester)
    if(MSVC)
        target_link_libraries(${TARGET} ${CMAKE_SOURCE_DIR}/Common/Zlib/zlibstatic.lib)
        target_link_libraries(${TARGET} ${CMAKE_SOURCE_DIR}/Common/Yaml/libyaml-cppmt.lib)
        target_link_libraries(${TARGET} vcruntime.lib MSVCRT.lib)
    else(MSVC)
        target_link_libraries(${TARGET} ${CMAKE_SOURCE_DIR}/Common/Zlib/libzlibstatic.a)
        target_link_libraries(${TARGET} ${CMAKE_SOURCE_DIR}/Common/Yaml/libyaml-cpp.a)
        target_link_libraries(${TARGET} -static-libgcc -static-libstdc++ -Wl,-Bstatic -lstdc++ -lpthread -Wl,-Bdynamic)
    endif(MSVC)
endforeach(TARGET LotroDat_static LotroDat LotRO_dat_extract_tester LotRO_dat_pat_tester)