cmake_minimum_required(VERSION 3.8)
project(LotRO_dat_library)

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

SET(CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} ${GCC_COVERAGE_COMPILE_FLAGS} -O9 -pipe -fomit-frame-pointer -ffast-math -Wall -Wextra" )
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_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/lib)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)

include_directories(${CMAKE_SOURCE_DIR}/include)
include_directories(${CMAKE_SOURCE_DIR}/Third_party)

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


set(HEADER_FILES
    ${CMAKE_SOURCE_DIR}/include/BinaryData.h
    ${CMAKE_SOURCE_DIR}/include/Database.h
    ${CMAKE_SOURCE_DIR}/include/DatException.h
    ${CMAKE_SOURCE_DIR}/include/DatFile.h
    ${CMAKE_SOURCE_DIR}/include/LotroDat.h
    ${CMAKE_SOURCE_DIR}/include/SubDirectory.h
    ${CMAKE_SOURCE_DIR}/include/Subfile.h
    ${CMAKE_SOURCE_DIR}/include/SubfileData.h
)

set(SOURCE_FILES
        ${CMAKE_SOURCE_DIR}/src/DatFile.cpp
        ${CMAKE_SOURCE_DIR}/src/BinaryData.cpp
        ${CMAKE_SOURCE_DIR}/src/Database.cpp
        ${CMAKE_SOURCE_DIR}/src/Subfile.cpp
        ${CMAKE_SOURCE_DIR}/src/SubDirectory.cpp

        ${CMAKE_SOURCE_DIR}/src/Subfiles/TextSubfile.cpp
        ${CMAKE_SOURCE_DIR}/src/Subfiles/JpgSubfile.cpp
        ${CMAKE_SOURCE_DIR}/src/Subfiles/DdsSubfile.cpp
        ${CMAKE_SOURCE_DIR}/src/Subfiles/OggSubfile.cpp
        ${CMAKE_SOURCE_DIR}/src/Subfiles/FontSubfile.cpp
        ${CMAKE_SOURCE_DIR}/src/Subfiles/WavSubfile.cpp
        ${CMAKE_SOURCE_DIR}/src/Subfiles/UnknownSubfile.cpp

        ${CMAKE_SOURCE_DIR}/Third_party/SQLite/sqlite3.c
        ${CMAKE_SOURCE_DIR}/Third_party/EasyLogging++/easylogging++.cc
        )

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} ${HEADER_FILES})
# SHARED LIBRARY
add_library(LotroDat SHARED ${SOURCE_FILES} ${HEADER_FILES})
# CONSOLE EXTRACTOR TOOL
add_executable(LotRO_dat_extractor ${SOURCE_FILES} ${HEADER_FILES} ${CMAKE_SOURCE_DIR}/src/Examples/extractor_example.cpp)
# CONSOLE PATCHER TOOL
add_executable(LotRO_dat_patcher ${SOURCE_FILES} ${HEADER_FILES} ${CMAKE_SOURCE_DIR}/src/Examples/patcher_example.cpp)

foreach(TARGET LotroDat_static LotroDat LotRO_dat_extractor LotRO_dat_patcher)
    #if(MSVC)
    #    target_link_libraries(${TARGET} ${CMAKE_SOURCE_DIR}/Third_Party/Zlib/zlibstatic.lib)
    #    target_link_libraries(${TARGET} ${CMAKE_SOURCE_DIR}/Third_Party/Yaml-cpp/libyaml-cppmt.lib)
    #    target_link_libraries(${TARGET} vcruntime.lib MSVCRT.lib)
    #else(MSVC)
        target_link_libraries(${TARGET} ${CMAKE_SOURCE_DIR}/Third_Party/lib/libzlibstatic.a)
        target_link_libraries(${TARGET} ${CMAKE_SOURCE_DIR}/Third_Party/lib/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_extractor LotRO_dat_patcher)