# ======================================================================================================================
#  fennec, a free and open source game engine
#  Copyright © 2025  Medusa Slockbower
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <https://www.gnu.org/licenses/>.
# ======================================================================================================================

cmake_minimum_required(VERSION 3.28...3.31)

project(fennec)
set(FENNEC_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})

macro(fennec_add_sources)
    list(APPEND FENNEC_SOURCES ${ARGN})
endmacro()

macro(fennec_add_definitions)
    list(APPEND FENNEC_COMPILE_DEFINITIONS ${ARGN})
endmacro()

macro(fennec_add_link_libraries)
    list(APPEND FENNEC_LINK_LIBRARIES ${ARGN})
endmacro()

macro(fennec_add_shared_libraries)
    list(APPEND FENNEC_SHARED_LIBRARIES ${ARGN})
endmacro()

macro(fennec_add_compile_options)
    list(APPEND FENNEC_PRIVATE_COMPILE_OPTIONS ${ARGN})
endmacro()

macro(fennec_add_link_options)
    list(APPEND FENNEC_PRIVATE_LINK_OPTIONS ${ARGN})
endmacro()

# External dependencies should be loaded here
add_subdirectory(external/cpptrace)

set(CMAKE_CXX_STANDARD 23)
set(CMAKE_C_STANDARD 23)

add_custom_target(fennec-dependencies
        COMMAND ${CMAKE_COMMAND} -E echo "Running dependencies."
        COMMENT "Running dependencies."
)

# include scripts
include("${FENNEC_SOURCE_DIR}/cmake/version.cmake")
include("${FENNEC_SOURCE_DIR}/cmake/platform.cmake")
include("${FENNEC_SOURCE_DIR}/cmake/build.cmake")
include("${FENNEC_SOURCE_DIR}/cmake/compiler.cmake")

# common defines
list(APPEND FENNEC_COMPILE_DEFINITIONS "NULL=0")

# find dependencies
find_package(Doxygen)
fennec_check_platform()

# any necessary include directories
include_directories(${FENNEC_SOURCE_DIR}/include)

# Metaprogramming is a dependency for generating various type info before compilation of the engine.
add_subdirectory(metaprogramming)

# Specify where to send libraries and executables
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${FENNEC_SOURCE_DIR}/lib/${FENNEC_BUILD_NAME})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${FENNEC_SOURCE_DIR}/lib/${FENNEC_BUILD_NAME})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${FENNEC_SOURCE_DIR}/bin/${FENNEC_BUILD_NAME})

# Core Files
fennec_add_sources(
        # CORE =================================================================================================================
        include/fennec/core/engine.h source/core/engine.cpp
        include/fennec/core/event.h  source/core/event.cpp
        include/fennec/core/logger.h source/core/logger.cpp

        include/fennec/core/version.h
        include/fennec/core/system.h


        # SCENE ================================================================================================================
        include/fennec/scene/scene.h source/scene/scene.cpp
        include/fennec/scene/component.h
        include/fennec/scene/scene_node.h
        include/fennec/scene/node2d.h
        include/fennec/scene/forward.h



        # RENDERERS ============================================================================================================
        include/fennec/renderers/interface/forward.h

        include/fennec/renderers/interface/gfxcontext.h
        include/fennec/renderers/interface/gfxsubpass.h
        include/fennec/renderers/interface/gfxresourcepool.h


        # CONTAINERS ===========================================================================================================
        include/fennec/containers/containers.h

        include/fennec/containers/array.h
        include/fennec/containers/bintree.h
        include/fennec/containers/bitfield.h
        include/fennec/containers/deque.h
        include/fennec/containers/dynarray.h
        include/fennec/containers/generic.h
        include/fennec/containers/graph.h
        include/fennec/containers/initializer_list.h
        include/fennec/containers/list.h
        include/fennec/containers/map.h
        include/fennec/containers/object_pool.h
        include/fennec/containers/optional.h
        include/fennec/containers/pair.h
        include/fennec/containers/priority_queue.h
        include/fennec/containers/rdtree.h
        include/fennec/containers/sequence.h
        include/fennec/containers/set.h
        include/fennec/containers/traversal.h
        include/fennec/containers/tuple.h
        include/fennec/containers/variant.h


        include/fennec/containers/detail/_tuple.h


        # lang =================================================================================================================
        include/fennec/lang/lang.h
        include/fennec/lang/metaprogramming.h


        include/fennec/lang/bits.h
        include/fennec/lang/constants.h
        include/fennec/lang/conditional_types.h
        include/fennec/lang/declval.h
        include/fennec/lang/hashing.h
        include/fennec/lang/intrinsics.h
        include/fennec/lang/limits.h
        include/fennec/lang/numeric_transforms.h
        include/fennec/lang/metasequences.h
        include/fennec/lang/ranges.h
        include/fennec/lang/static_constructor.h
        include/fennec/lang/type_identity.h
        include/fennec/lang/type_operators.h
        include/fennec/lang/type_sequences.h
        include/fennec/lang/type_traits.h
        include/fennec/lang/type_transforms.h
        include/fennec/lang/types.h
        include/fennec/lang/utility.h
        include/fennec/lang/integer.h

        include/fennec/lang/assert.h source/lang/assert.cpp

        include/fennec/lang/detail/_bits.h
        include/fennec/lang/detail/_declval.h
        include/fennec/lang/detail/_int.h
        include/fennec/lang/detail/_numeric_transforms.h
        include/fennec/lang/detail/_stdlib.h
        include/fennec/lang/detail/_type_traits.h
        include/fennec/lang/detail/_type_transforms.h
        include/fennec/lang/detail/_type_sequences.h


        # RTTI =================================================================================================================
        include/fennec/rtti/typeid.h
        include/fennec/rtti/type_data.h
        include/fennec/rtti/type.h
        include/fennec/rtti/enable.h
        include/fennec/rtti/forward.h
        include/fennec/rtti/typelist.h
        include/fennec/rtti/type_registry.h
        include/fennec/rtti/singleton.h


        include/fennec/rtti/detail/_constants.h
        include/fennec/rtti/detail/_this_t.h
        include/fennec/rtti/detail/_typeid.h
        include/fennec/rtti/detail/_type_name.h


        # MEMORY ===============================================================================================================
        include/fennec/memory/new.h source/memory/new.cpp

        include/fennec/memory/allocator.h
        include/fennec/memory/bytes.h
        include/fennec/memory/common.h
        include/fennec/memory/memory.h
        include/fennec/memory/pointers.h
        include/fennec/memory/pointer_traits.h

        include/fennec/memory/detail/_ptr_traits.h

        # DEBUG ================================================================================================================
        source/debug/assert_impl.cpp


        # MATH =================================================================================================================
        include/fennec/math/math.h

        include/fennec/math/scalar.h

        include/fennec/math/vector.h
        include/fennec/math/vector_base.h
        include/fennec/math/vector_traits.h
        include/fennec/math/vector_storage.h

        include/fennec/math/swizzle.h
        include/fennec/math/swizzle_storage.h

        include/fennec/math/matrix.h

        include/fennec/math/common.h
        include/fennec/math/exponential.h
        include/fennec/math/geometric.h
        include/fennec/math/trigonometric.h
        include/fennec/math/relational.h

        include/fennec/math/ext/common.h
        include/fennec/math/ext/constants.h
        include/fennec/math/ext/primes.h
        include/fennec/math/ext/quaternion.h
        include/fennec/math/ext/rect.h
        include/fennec/math/ext/transform.h


        include/fennec/math/detail/_fwd.h
        include/fennec/math/detail/_math.h
        include/fennec/math/detail/_matrix.h
        include/fennec/math/detail/_types.h
        include/fennec/math/detail/_vector_traits.h



        # string ===============================================================================================================
        include/fennec/string/locale.h
        include/fennec/string/cstring.h
        include/fennec/string/string.h

        include/fennec/string/detail/_ctype.h



        # format ===============================================================================================================
        include/fennec/format/format.h
        include/fennec/format/format_arg.h
        include/fennec/format/formatter.h
        include/fennec/format/charconv.h

        include/fennec/format/detail/_format.h

        source/format/charconv.cpp



        # filesystem ===========================================================================================================
        include/fennec/filesystem/file.h source/filesystem/file.cpp
        include/fennec/filesystem/path.h source/filesystem/path.cpp



        # interpret ============================================================================================================
        include/fennec/interpret/tokenizer.h


        # PLATFORM =============================================================================================================

        include/fennec/platform/interface/fwd.h
        include/fennec/platform/interface/display_server.h
        include/fennec/platform/interface/platform.h source/platform/interface/platform.cpp
        include/fennec/platform/interface/window.h   source/platform/interface/window.cpp

        # GRAPHICS =============================================================================================================

        include/fennec/gfx3d/mesh_instance.h
)

# add the test suite as a sub-project
add_subdirectory(test)

add_library(fennec STATIC
        ${FENNEC_SOURCES}
        include/fennec/platform/window_manager.h
        include/fennec/platform/window_manager.cpp
        include/fennec/platform/window_manager.h
        include/fennec/lang/function.h
        include/fennec/threading/thread.h
        include/fennec/threading/detail/_thread.h
        include/fennec/lang/detail/_function.h
        include/fennec/threading/atomic.h

)

add_dependencies(fennec metaprogramming fennec-dependencies)

target_compile_definitions(fennec PUBLIC ${FENNEC_COMPILE_DEFINITIONS})
target_compile_options(fennec PRIVATE ${FENNEC_PRIVATE_COMPILE_OPTIONS})

# Do not compile base fennec library with c++ stdlib
# fennec does not use the C++ stdlib because it is bloated, difficult to read, and implementation defined.
# This implementation is designed to be as readable as possible, and expose information that would otherwise be obfuscated
target_link_options(fennec PRIVATE ${FENNEC_PRIVATE_LINK_OPTIONS})



target_link_libraries(fennec PRIVATE
        ${FENNEC_LINK_LIBRARIES}
        ${FENNEC_SHARED_LIBRARIES}

        cpptrace::cpptrace
)

foreach (_LIB IN ITEMS ${FENNEC_SHARED_LIBRARIES})
    add_custom_command(TARGET fennec POST_BUILD
            COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_LINKER_FILE:${_LIB}> ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}
    )
endforeach ()


# DOXYGEN ==============================================================================================================
# https://vicrucann.github.io/tutorials/quick-cmake-doxygen/

file(COPY logo DESTINATION docs/logo)

if(DOXYGEN_FOUND)
    add_dependencies(fennec fennecdocs)
    set(DOXY_OUTPUT_DIR "${FENNEC_SOURCE_DIR}/docs")
    set(DOXY_EXAMPLES_DIR "${FENNEC_SOURCE_DIR}/examples")
    get_filename_component(DOXYGEN_PROJECT_NAME ${FENNEC_SOURCE_DIR} NAME) # Set Doxy Project name to the name of the root dir
    set(DOXYGEN_CONFIG_IN "${FENNEC_SOURCE_DIR}/doxy/Doxyfile.in") # Input config file with preprocessor arguments
    set(DOXYGEN_CONFIG_OUT "${FENNEC_SOURCE_DIR}/doxy/Doxyfile.out") # Generated config file from input

    configure_file(${DOXYGEN_CONFIG_IN} ${DOXYGEN_CONFIG_OUT} @ONLY) # Execute preprocessing step
    message("Doxygen Build Started.")


    # Target for building docs
    add_custom_target(fennecdocs ALL
            COMMAND ${DOXYGEN_EXECUTABLE} ${DOXYGEN_CONFIG_OUT}
            WORKING_DIRECTORY ${FENNEC_SOURCE_DIR}
            COMMAND ${CMAKE_COMMAND} -E copy ${FENNEC_SOURCE_DIR}/logo/raster.png
                                             ${DOXY_OUTPUT_DIR}/logo/raster.png
            COMMENT "Generating Doxygen Documentation"
            VERBATIM)

    add_dependencies(fennecdocs fennecdocs-clean)

    # Target for cleaning docs
    add_custom_target(fennecdocs-clean ALL
            COMMAND ${CMAKE_COMMAND} -E remove -f "${FENNEC_SOURCE_DIR}/docs"
            COMMENT "Cleaning Doxygen Documentation"
            VERBATIM)
else()
    message("Doxygen not found.")
endif()
