function(bigfoot_create_logger)
    set(LOGGER_FILENAME ${PROJECT_NAME}Logger)
    string(TOUPPER ${PROJECT_NAME}_Logger LOGGER_NAME)
    string(TOUPPER ${LOGGER_FILENAME} LOGGER_FILENAME_UPPER)
    configure_file( ${CMAKE_SOURCE_DIR}/Bigfoot/Sources/System/Include/System/Log/TargetLogger_generated.hpp.in
                    ${CMAKE_CURRENT_SOURCE_DIR}/Include/${PROJECT_NAME}/${LOGGER_FILENAME}_generated.hpp
                    @ONLY)
endfunction()

function(bigfoot_create_bigfile ParentFolder)
    set(OUTPUT_PATH_BIGFILE "${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${PROJECT_NAME}-bigfile.db")
    get_filename_component(OUTPUT_PATH_BIGFILE_ABSOLUTE ${OUTPUT_PATH_BIGFILE} ABSOLUTE)
    get_filename_component(OUTPUT_PATH_BIGFILE_DIRECTORY ${OUTPUT_PATH_BIGFILE_ABSOLUTE} DIRECTORY)

    add_custom_command(
        OUTPUT ${OUTPUT_PATH_BIGFILE_ABSOLUTE}.bftimestamp ${OUTPUT_PATH_BIGFILE_ABSOLUTE}
        DEPENDS ${CMAKE_SOURCE_DIR}/Bigfoot/Sources/Engine/BigFile/BigFileSchema.sql
        COMMAND ${CMAKE_COMMAND} -E make_directory ${OUTPUT_PATH_BIGFILE_DIRECTORY}
        COMMAND ${SQLITE3_EXECUTABLE} ${OUTPUT_PATH_BIGFILE_ABSOLUTE} < ${CMAKE_SOURCE_DIR}/Bigfoot/Sources/Engine/BigFile/BigFileSchema.sql
        COMMAND ${CMAKE_COMMAND} -E touch ${OUTPUT_PATH_BIGFILE_ABSOLUTE}.bftimestamp
        COMMENT "Creating Bigfile ${OUTPUT_PATH_BIGFILE_ABSOLUTE}"
    )

    add_custom_target(${PROJECT_NAME}BigFile ALL
        DEPENDS ${OUTPUT_PATH_BIGFILE_ABSOLUTE}.bftimestamp
    )

    set_target_properties(${PROJECT_NAME}BigFile PROPERTIES FOLDER UtilityTargets/${ParentFolder})

    add_dependencies(${PROJECT_NAME} ${PROJECT_NAME}BigFile)

    string(TOUPPER ${PROJECT_NAME} BIGFILE_NAME)
    set(BIGFILE_LOCATION "./${PROJECT_NAME}-bigfile.db")
    configure_file(
        ${CMAKE_SOURCE_DIR}/Bigfoot/Sources/Engine/Include/Engine/BigFile/BigFileInfo_generated.hpp.in
        ${CMAKE_CURRENT_SOURCE_DIR}/Include/${PROJECT_NAME}/BigFileInfo_generated.hpp
        @ONLY
    )
endfunction()

function(bigfoot_compile_flatbuffers BigfootDependencies)
    set(IncludeFolders "")

    list(APPEND IncludeFolders ${CMAKE_CURRENT_SOURCE_DIR}/Include)

    foreach(Dependency IN LISTS BigfootDependencies)
        if(TARGET ${Dependency})
            get_target_property(DependencyIncludeDirs ${Dependency} INTERFACE_INCLUDE_DIRECTORIES)
            if(DependencyIncludeDirs)
                foreach(Dir IN LISTS DependencyIncludeDirs)
                    # Strip $<BUILD_INTERFACE:...> generator expression
                    if(Dir MATCHES "^\\$<BUILD_INTERFACE:(.+)>$")
                        list(APPEND IncludeFolders "${CMAKE_MATCH_1}")
                    elseif(NOT Dir MATCHES "^\\$<")
                        # Pass through plain paths, skip other generator expressions
                        list(APPEND IncludeFolders "${Dir}")
                    endif()
                endforeach()
            endif()
        endif()
    endforeach()

    set(IncludeFlags "")
    foreach(folder IN LISTS IncludeFolders)
        list(APPEND IncludeFlags -I "${folder}")
    endforeach()

    file(GLOB_RECURSE SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/Include/*.fbs")
    foreach(SOURCE_FILE IN LISTS SOURCES)
        get_filename_component(SOURCE_DIRECTORY ${SOURCE_FILE} DIRECTORY)
        set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${SOURCE_FILE})

        execute_process(
            COMMAND ${FLATBUFFERS_FLATC_EXECUTABLE}
                    --cpp
                    ${IncludeFlags}
                    --keep-prefix
                    --filename-ext "hpp"
                    --cpp-std c++17
                    --reflect-names
                    --reflect-types
                    --gen-name-strings
                    --gen-object-api
                    --cpp-str-flex-ctor
                    --no-cpp-direct-copy
                    --force-empty
                    --cpp-ptr-type "eastl::unique_ptr"
                    --cpp-str-type "eastl::string"
                    --cpp-vec-type "eastl::vector"
                    --cpp-include "EASTL/unique_ptr.h"
                    --cpp-include "EASTL/string.h"
                    --cpp-include "EASTL/vector.h"
                    -o "${SOURCE_DIRECTORY}"
                    --schema "${SOURCE_FILE}"
        )
    endforeach()
endfunction()

macro(bigfoot_remove_default_exception_flags)
    if(MSVC)
        string(REPLACE "/EHsc" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
        string(REPLACE "/EHs" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
    endif()
endmacro()