cmake_minimum_required(VERSION 3.25)

set(BLT_GRAPHICS_VERSION 0.9.0)
set(BLT_GRAPHICS_TEST_VERSION 0.0.1)

project(BLT_WITH_GRAPHICS VERSION ${BLT_GRAPHICS_VERSION})

option(ENABLE_ADDRSAN "Enable the address sanitizer" OFF)
option(ENABLE_UBSAN "Enable the ub sanitizer" OFF)
option(ENABLE_TSAN "Enable the thread data race sanitizer" OFF)

option(BUILD_GRAPHICS_TESTS "Build the BLT Graphics test set" OFF)

set(CMAKE_CXX_STANDARD 17)

if (EMSCRIPTEN)
    message("Using emscriptem linker and compile flags!")
    #add_definitions("-std=c++17")
    set(CMAKE_VERBOSE_MAKEFILE on)
else ()
    message("Not using emscripten! (linking GLFW)")
    set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
    set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
    set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)

    add_subdirectory(libraries/glfw-3.3.8)
    add_subdirectory(libraries/openal-soft)
endif ()

add_subdirectory(libraries/BLT)
add_subdirectory(libraries/freetype-2.13.2)

list(APPEND IMGUI_SOURCE_FILES "${CMAKE_CURRENT_SOURCE_DIR}/libraries/imgui/imgui.cpp")
list(APPEND IMGUI_SOURCE_FILES "${CMAKE_CURRENT_SOURCE_DIR}/libraries/imgui/imgui_demo.cpp")
list(APPEND IMGUI_SOURCE_FILES "${CMAKE_CURRENT_SOURCE_DIR}/libraries/imgui/imgui_draw.cpp")
list(APPEND IMGUI_SOURCE_FILES "${CMAKE_CURRENT_SOURCE_DIR}/libraries/imgui/imgui_tables.cpp")
list(APPEND IMGUI_SOURCE_FILES "${CMAKE_CURRENT_SOURCE_DIR}/libraries/imgui/imgui_widgets.cpp")
list(APPEND IMGUI_SOURCE_FILES "${CMAKE_CURRENT_SOURCE_DIR}/libraries/imgui/backends/imgui_impl_opengl3.cpp")
list(APPEND IMGUI_SOURCE_FILES "${CMAKE_CURRENT_SOURCE_DIR}/libraries/imgui/backends/imgui_impl_glfw.cpp")

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/libraries/imgui/)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/examples/)

include_directories(include/)
file(GLOB_RECURSE PROJECT_BUILD_FILES "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp")

add_library(BLT_WITH_GRAPHICS ${PROJECT_BUILD_FILES} ${IMGUI_SOURCE_FILES})

target_link_libraries(BLT_WITH_GRAPHICS PUBLIC BLT)
target_link_libraries(BLT_WITH_GRAPHICS PUBLIC freetype)
target_include_directories(BLT_WITH_GRAPHICS PUBLIC include/)
target_include_directories(BLT_WITH_GRAPHICS PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/libraries/imgui/)

if (${ENABLE_ADDRSAN} MATCHES ON)
    target_compile_options(BLT_WITH_GRAPHICS PRIVATE -fsanitize=address)
    target_link_options(BLT_WITH_GRAPHICS PRIVATE -fsanitize=address)
endif ()

if (${ENABLE_UBSAN} MATCHES ON)
    target_compile_options(BLT_WITH_GRAPHICS PRIVATE -fsanitize=undefined)
    target_link_options(BLT_WITH_GRAPHICS PRIVATE -fsanitize=undefined)
endif ()

if (${ENABLE_TSAN} MATCHES ON)
    target_compile_options(BLT_WITH_GRAPHICS PRIVATE -fsanitize=thread)
    target_link_options(BLT_WITH_GRAPHICS PRIVATE -fsanitize=thread)
endif ()

if (${BUILD_GRAPHICS_TESTS})
    message("Building graphics test")
    project(BLT_GRAPHICS_TESTS VERSION ${BLT_GRAPHICS_TEST_VERSION})

    include_directories(tests/include)

    file(GLOB_RECURSE GRAPHICS_TEST_FILES "${CMAKE_CURRENT_SOURCE_DIR}/tests/src/*.cpp")

    message("Using files ${GRAPHICS_TEST_FILES}")

    add_executable(BLT_GRAPHICS_TESTS ${GRAPHICS_TEST_FILES})

    target_link_libraries(BLT_GRAPHICS_TESTS BLT_WITH_GRAPHICS)

    target_compile_options(BLT_GRAPHICS_TESTS PRIVATE -Wall -Wpedantic -Wno-comment -Wno-strict-aliasing)
    target_link_options(BLT_GRAPHICS_TESTS PRIVATE -Wall -Wpedantic -Wno-comment -Wno-strict-aliasing -Wno-unused-function)

    if (${ENABLE_ADDRSAN} MATCHES ON)
        target_compile_options(BLT_GRAPHICS_TESTS PRIVATE -fsanitize=address)
        target_link_options(BLT_GRAPHICS_TESTS PRIVATE -fsanitize=address)
    endif ()

    if (${ENABLE_UBSAN} MATCHES ON)
        target_compile_options(BLT_GRAPHICS_TESTS PRIVATE -fsanitize=undefined)
        target_link_options(BLT_GRAPHICS_TESTS PRIVATE -fsanitize=undefined)
    endif ()

    if (${ENABLE_TSAN} MATCHES ON)
        target_compile_options(BLT_GRAPHICS_TESTS PRIVATE -fsanitize=thread)
        target_link_options(BLT_GRAPHICS_TESTS PRIVATE -fsanitize=thread)
    endif ()
    message("Built graphics tests")
endif ()

project(BLT_WITH_GRAPHICS)

if (EMSCRIPTEN)
    list(APPEND TARGETS BLT_WITH_GRAPHICS)
    if (${BUILD_GRAPHICS_TESTS})
        list(APPEND TARGETS BLT_GRAPHICS_TESTS)
    endif ()
    #set_target_properties(FinalProject PROPERTIES LINK_FLAGS "-sMAX_WEBGL_VERSION=2 -sASSERTIONS=1 -pthread -sPTHREAD_POOL_SIZE=8 -s INITIAL_MEMORY=134217728 -sUSE_GLFW=3 --preload-file 'assets'")

    set_target_properties(${TARGETS} PROPERTIES LINK_FLAGS "-sMAX_WEBGL_VERSION=2 -sASSERTIONS=1 -sUSE_GLFW=3")
    #set_target_properties(BLT_WITH_GRAPHICS PROPERTIES COMPILE_FLAGS "-pthread")

    target_compile_options(BLT_WITH_GRAPHICS PRIVATE -Wall -Wpedantic -Wno-comment -Wno-strict-aliasing)
    target_link_options(BLT_WITH_GRAPHICS PRIVATE -Wall -Wpedantic -Wno-comment -Wno-strict-aliasing)
else ()
    target_link_libraries(BLT_WITH_GRAPHICS PUBLIC glfw)
    target_link_libraries(BLT_WITH_GRAPHICS PUBLIC OpenAL)

    target_compile_options(BLT_WITH_GRAPHICS PRIVATE -Wall -Wpedantic -Wno-comment -Wno-strict-aliasing -Wno-unused-function)
    target_link_options(BLT_WITH_GRAPHICS PRIVATE -Wall -Wpedantic -Wno-comment -Wno-strict-aliasing -Wno-unused-function)
endif ()