cmake_minimum_required(VERSION 3.13)

# Make warnings trigger errors.
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror")

project(pkmnLib)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

if (NOT SCRIPT_PROVIDER)
    message(WARNING "Script provider was not set, using angelscript as default.")
    set(SCRIPT_PROVIDER "angelscript")
    ADD_DEFINITIONS(-D ANGELSCRIPT=1)
endif()

if (WINDOWS)
    SET(CMAKE_SYSTEM_NAME Windows)
    ADD_DEFINITIONS(-D WINDOWS=1)
endif(WINDOWS)

message(STATUS "Using:
\t C ${CMAKE_C_COMPILER}
\t C++ ${CMAKE_CXX_COMPILER}
\t CXX ABI ${CMAKE_CXX_COMPILER_ABI}
\t C++ Version ${CMAKE_CXX_STANDARD}")


if (NOT EXISTS ${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
    message(WARNING "The file conanbuildinfo.cmake doesn't exist, running conan install.")
    string(REPLACE "." ";" VERSION_LIST "${CMAKE_C_COMPILER_VERSION}")
    list(GET VERSION_LIST 0 VERSION)
    list(GET VERSION_LIST 1 MINOR)
    if (NOT MINOR MATCHES 0)
        SET(VERSION ${VERSION}.${MINOR})
    endif ()
    set(CONAN_STATIC_C False)
    if (STATICC)
        set(CONAN_STATIC_C True)
    endif (STATICC)

    if (NOT WINDOWS)
        execute_process(COMMAND conan install ${CMAKE_SOURCE_DIR} --install-folder=${CMAKE_BINARY_DIR} --build outdated
                -s compiler=clang -s compiler.libcxx=libstdc++11 -s compiler.version=${VERSION} -o *:staticC=${CONAN_STATIC_C} -o AngelScript:link_std_statically=True)
    else ()
        execute_process(COMMAND conan install ${CMAKE_SOURCE_DIR} --install-folder=${CMAKE_BINARY_DIR} --build outdated
                -s compiler=gcc -s compiler.libcxx=libstdc++11 -s compiler.version=${VERSION} -s os=Windows -o *:staticC=${CONAN_STATIC_C} -o AngelScript:link_std_statically=True)
    endif ()
endif ()
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()

message(STATUS "Using Conan Libs:")
foreach (_conanLib ${CONAN_LIBS})
    message(STATUS "\t ${_conanLib}")
endforeach()

# Create Core library with files in src/Core
SET(FILE_SOURCE
        "src/Battling/*.cpp"
        "src/Battling/*.hpp"
        "src/Library/*.cpp"
        "src/Library/*.hpp"
        "CInterface/Core.*"
        "CInterface/Library/*.cpp"
        "CInterface/Library/*.hpp"
        "CInterface/Battling/*.cpp"
        "CInterface/Battling/*.hpp"
        )
if (SCRIPT_PROVIDER STREQUAL "angelscript")
    SET(FILE_SOURCE ${FILE_SOURCE}
            "src/ScriptResolving/AngelScript/*.cpp"
            "src/ScriptResolving/AngelScript/*.hpp"
            "extern/angelscript_addons/*.cpp"
            "extern/angelscript_addons/*.h"
            "CInterface/AngelScript/*.cpp"
            "CInterface/AngelScript/*.hpp"
        )
    ADD_DEFINITIONS(-D AS_USE_ACCESSORS=1)
endif()
message(STATUS "${FILE_SOURCE}")
file(GLOB_RECURSE CORE_SRC_FILES ${FILE_SOURCE})

add_library(pkmnLib SHARED ${CORE_SRC_FILES})

foreach (_variableName ${CONAN_LIBS})
    message(STATUS "Lib: ${_variableName}")
endforeach()

SET(_LINKS CreatureLibLibrary CreatureLibBattling Arbutils)
SET(_TESTLINKS pkmnLib CreatureLibLibrary CreatureLibBattling Arbutils)

if (SCRIPT_PROVIDER STREQUAL "angelscript")
    SET(_LINKS  angelscript ${_LINKS} )
    SET(_TESTLINKS angelscript ${_TESTLINKS} )
endif()

if (WINDOWS)
    message(STATUS "Using Windows build.")
    set(CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} -L ${CMAKE_BINARY_DIR}/bin")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m64 -Wa,-mbig-obj -Wl,-allow-multiple-definition")
endif (WINDOWS)

if (STATICC)
    message(STATUS "Linking C library statically")
    SET(_LINKS ${_LINKS} -static-libgcc -static-libstdc++)
    if (NOT DEFINED CONAN_EXPORTED)
        SET(_TESTLINKS ${_TESTLINKS} -static-libgcc -static-libstdc++)
    endif()
endif()

target_link_libraries(pkmnLib PUBLIC ${_LINKS})

if (NOT DEFINED CONAN_EXPORTED)
    # Create Test executable
    file(GLOB_RECURSE TEST_FILES "tests/*.cpp" "tests/*.hpp")
    add_executable(pkmnLibTests  ${TEST_FILES} extern/catch.hpp)
    message(STATUS "${_TESTLINKS}")
    target_link_libraries(pkmnLibTests PUBLIC ${_TESTLINKS})

    # Add a definition for the test library
    target_compile_definitions(pkmnLibTests PRIVATE TESTS_BUILD)
endif ()