cmake_minimum_required(VERSION 3.22)

project(kge
        DESCRIPTION "kyle's editor"
        LANGUAGES CXX
        VERSION 0.0.1)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_VERBOSE_MAKEFILES TRUE)
set(VERBOSE YES)

string(TIMESTAMP TODAY "%Y%m%d")

####################
### BUILD CONFIG ###
####################

# in "prod", this will be ON by default; the docstring is written this
# way as I am likely to forget to change it later.
set(BUILD_GUI OFF CACHE BOOL "Disable building the graphical version.")

if (CMAKE_HOST_UNIX)
    message(STATUS "Build system is POSIX.")
else ()
    message(STATUS "Build system is NOT POSIX.")
endif ()

if (MSVC)
    add_compile_options("/W4" "$<$<CONFIG:RELEASE>:/O2>")
else ()
    add_compile_options(
            "-Wall"
            "-Wextra"
            "-Werror"
            "-static"
            "$<$<CONFIG:DEBUG>:-g>"
            "$<$<CONFIG:RELEASE>:-O2>")
    if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
        add_compile_options("-stdlib=libc++")
    else ()
        # nothing special for gcc at the moment
    endif ()
endif ()
add_compile_definitions(KGE_PLATFORM=${CMAKE_HOST_SYSTEM_NAME})
add_compile_definitions(KGE_VERSION=${PROJECT_VERSION})

message(STATUS "Build system: ${CMAKE_HOST_SYSTEM_NAME}")

if (${BUILD_GUI})
    include(cmake/imgui.cmake)
endif ()

#####################
### BUILD TARGETS ###
#####################

set(HEADER_FILES
        Defs.h
        Buffer.h
        File.h
        Cursor.h
)
set(SOURCE_FILES
        Defs.cc
        Buffer.cc
        File.cc
        Cursor.cc
)

add_executable(ke main.cc ${SOURCE_FILES} ${HEADER_FILES})

if (CMAKE_HOST_UNIX)
    add_custom_target(manpages)
    configure_file(docs/ke.md ke.1.scdoc @ONLY)
    add_custom_command(TARGET manpages COMMAND scdoc < ke.1.scdoc > ke.1
            OUTPUTS ${CMAKE_CURRENT_BINARY_DIR}/ke.1)
    add_dependencies(ke manpages)
endif ()

if (${BUILD_GUI})
    configure_file(resources/kge.desktop.in kge.desktop @ONLY)
    add_executable(kge gmain.cc ${SOURCE_FILES} ${HEADER_FILES})
    target_link_libraries(kge imgui)
    add_dependencies(kge ke)

    if (CMAKE_HOST_UNIX)
        configure_file(docs/kge.md kge.1.scdoc @ONLY)
        add_custom_command(TARGET manpages COMMAND scdoc < kge.1.scdoc > kge.1
                OUTPUTS ${CMAKE_CURRENT_BINARY_DIR}/kge.1)

        add_dependencies(kge manpages)
    endif ()
endif ()

#######################
### INSTALL TARGETS ###
#######################

install(TARGETS ke
        DESTINATION bin
        COMPONENT nox)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/ke.1
        DESTINATION share/man/man1
        COMPONENT nox)

if (${BUILD_GUI})
    install(TARGETS kge
            DESTINATION bin
            COMPONENT nox
            COMPONENT gui)
    install(FILES ${CMAKE_CURRENT_BINARY_DIR}/kge.desktop
            DESTINATION share/applications
            COMPONENT gui)
    install(FILES resources/kge.png
            DESTINATION share/${PROJECT_NAME}
            COMPONENT gui)
    install(FILES ${CMAKE_CURRENT_BINARY_DIR}/kge.1
            DESTINATION share/man/man1
            COMPONENT gui)
endif ()


include(cmake/packaging.cmake)
