cmake_minimum_required(VERSION 3.21)

project(Gitree VERSION 0.1.0 LANGUAGES C CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(BUILD_SHARED_LIBS OFF CACHE BOOL "Build all dependencies as static libraries" FORCE)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")

if(NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/vendor/imgui/imgui.cpp")
    message(FATAL_ERROR "Vendor dependencies are missing. Run: git submodule update --init --recursive")
endif()

# 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)
set(GLFW_INSTALL OFF CACHE BOOL "" FORCE)
add_subdirectory(vendor/glfw EXCLUDE_FROM_ALL)

# Dear ImGui does not ship a CMake target, so keep its static-library recipe here.
add_library(imgui STATIC
    vendor/imgui/imgui.cpp
    vendor/imgui/imgui_draw.cpp
    vendor/imgui/imgui_tables.cpp
    vendor/imgui/imgui_widgets.cpp
    vendor/imgui/backends/imgui_impl_glfw.cpp
    vendor/imgui/backends/imgui_impl_opengl3.cpp
)
target_include_directories(imgui PUBLIC vendor/imgui vendor/imgui/backends)
target_link_libraries(imgui PUBLIC glfw)

# libgit2
set(BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(BUILD_CLI OFF CACHE BOOL "" FORCE)
set(BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
set(BUILD_FUZZERS OFF CACHE BOOL "" FORCE)
set(USE_SSH OFF CACHE STRING "" FORCE)
if(WIN32)
    set(USE_HTTPS WinHTTP CACHE STRING "" FORCE)
endif()
add_subdirectory(vendor/libgit2 EXCLUDE_FROM_ALL)

# Native platform file and folder dialogs.
set(IZO_BUILD_EXAMPLE OFF CACHE BOOL "" FORCE)
add_subdirectory(vendor/iZo EXCLUDE_FROM_ALL)

# Persistent application settings and session data.
set(IKV_BUILD_DEMOS OFF CACHE BOOL "" FORCE)
set(IKV_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(IKV_INSTALL OFF CACHE BOOL "" FORCE)
add_subdirectory(vendor/iKv EXCLUDE_FROM_ALL)

find_package(OpenGL REQUIRED)

add_executable(gitree WIN32
    src/main.cpp
    src/ui/gitree_ui.cpp
    src/ui/gitree_ui.h
    src/ui/graph_renderer.cpp
    src/ui/graph_renderer.h
    src/ui/diff_viewer.cpp
    src/ui/diff_viewer.h
    src/managers/git_manager.cpp
    src/managers/git_manager.h
    src/managers/window_manager.cpp
    src/managers/window_manager.h
    src/managers/user_data.cpp
    src/managers/user_data.h
    src/managers/avatar_cache.cpp
    src/managers/avatar_cache.h
    src/managers/application_manager.cpp
    src/managers/application_manager.h
    src/managers/application_icon_cache.cpp
    src/managers/application_icon_cache.h
    src/models/repository.h
)
target_include_directories(gitree PRIVATE src vendor/libgit2/include vendor/icons)
target_link_libraries(gitree PRIVATE imgui libgit2package iZo::izo ikv::ikv OpenGL::GL)
target_compile_definitions(gitree PRIVATE
    GITREE_VERSION="${PROJECT_VERSION}"
    GITREE_ASSET_DIR="${CMAKE_CURRENT_SOURCE_DIR}/vendor/fonts"
    GITREE_IMAGE_ASSET_DIR="${CMAKE_CURRENT_SOURCE_DIR}/assets"
    $<$<PLATFORM_ID:Windows>:NOMINMAX;WIN32_LEAN_AND_MEAN>
)

if(WIN32)
    target_link_libraries(gitree PRIVATE dwmapi urlmon windowscodecs bcrypt crypt32)
endif()

if(MSVC)
    target_compile_options(gitree PRIVATE /W4 /permissive-)
else()
    target_compile_options(gitree PRIVATE -Wall -Wextra -Wpedantic)
endif()
