cmake_minimum_required(VERSION 3.20)
project(AngelscriptDebugger)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

option(WINDOWS "Whether the build target is Windows or not." OFF)
option(SHARED "Whether we should build a shared library, instead of a static one." ON)
option(STATICC "Whether gcc and stdc++ should be linked statically to the library." OFF)

include(CMakeLists.txt.angelscript.in)
include_angelscript()


if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
    add_compile_options(-fconcepts)
endif ()

if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
    add_link_options(-fuse-ld=lld)
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 (CMAKE_BUILD_TYPE MATCHES Release AND NOT WINDOWS)
    # Include debug symbols in all linux builds
    message("Including debug symbols")
    add_compile_options(-g -gline-tables-only)
endif ()

# Set whether we want a static or shared library.
set(LIBTYPE STATIC)
if (SHARED)
    set(LIBTYPE SHARED)
endif (SHARED)

file(GLOB_RECURSE SRC_FILES "src/*.cpp" "src/*.hpp")
add_library(AngelscriptDebugger ${LIBTYPE} ${SRC_FILES})
target_compile_options(AngelscriptDebugger PRIVATE -Wall -Wextra -Werror)
target_include_directories(AngelscriptDebugger PRIVATE extern/asio-1.18.2/include)

SET(_LINKS angelscript)
if (WINDOWS)
    MESSAGE(WARNING, "Using Windows Build.")
    # Add a definition for the compiler, so we can use it in C++ as well.
    ADD_DEFINITIONS(-D WINDOWS=1)
    # -m64: Build a 64 bit library
    add_compile_options(-m64)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wl,-allow-multiple-definition")
    if (SHARED)
        set_target_properties(AngelscriptDebugger PROPERTIES SUFFIX ".dll")
    endif(SHARED)
    set(_LINKS ${_LINKS} -Wl,-Bstatic -lws2_32 -Wl,-Bdynamic)
endif (WINDOWS)

if (STATICC)
    set (CMAKE_SHARED_LINKER_FLAGS "-Wl,--as-needed")
    message(STATUS "Linking C library statically")
    set(_LINKS ${_LINKS} -static-libgcc -static-libstdc++ -Wl,-Bstatic -lm -lstdc++ -lpthread -Wl,-Bdynamic)
    SET(_TESTLINKS ${_TESTLINKS} -static-libgcc -static-libstdc++ -Wl,-Bstatic -lstdc++ -lpthread -Wl,-Bdynamic)
else()
    SET(_LINKS ${_LINKS} -Wl,--whole-archive -lpthread -Wl,--no-whole-archive)
endif()

target_link_libraries(AngelscriptDebugger PUBLIC ${_LINKS})

file(GLOB_RECURSE RUNNER_SRC_FILES "TestRunner/*.cpp" "TestRunner/*.hpp")
add_executable(AngelscriptDebuggerRunner ${RUNNER_SRC_FILES})
target_include_directories(AngelscriptDebuggerRunner PRIVATE extern/asio-1.18.2/include)
target_link_libraries(AngelscriptDebuggerRunner AngelscriptDebugger)