119 lines
3.5 KiB
CMake
119 lines
3.5 KiB
CMake
cmake_minimum_required (VERSION 3.29)
|
|
project(VulkanEngine)
|
|
|
|
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/CMake")
|
|
|
|
include(BuildInfo)
|
|
include(FetchContent)
|
|
#LTO
|
|
include(CheckIPOSupported)
|
|
check_ipo_supported(RESULT LTO_SUPPORTED OUTPUT LTO_ERROR)
|
|
|
|
|
|
|
|
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
|
|
|
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
|
|
find_program(GCC_AR NAMES gcc-ar REQUIRED)
|
|
find_program(GCC_RANLIB NAMES gcc-ranlib REQUIRED)
|
|
set(CMAKE_AR "${GCC_AR}")
|
|
set(CMAKE_RANLIB "${GCC_RANLIB}")
|
|
endif()
|
|
|
|
# Find all package dependencies
|
|
find_package( glfw3 REQUIRED )
|
|
find_package( glm REQUIRED )
|
|
find_package( VulkanMemoryAllocator CONFIG REQUIRED )
|
|
find_package( fmt REQUIRED )
|
|
# Shader compilers
|
|
# Todo
|
|
#find_program( GLSLANG_VALIDATOR "glslangValidator" HINTS $ENV{VULKAN_SDK}/bin REQUIRED )
|
|
|
|
|
|
# fastgltf because it's not in nixpkgs
|
|
|
|
FetchContent_Declare(
|
|
fastgltf
|
|
GIT_REPOSITORY https://github.com/spnda/fastgltf.git
|
|
GIT_TAG v0.8.0
|
|
)
|
|
FetchContent_MakeAvailable(fastgltf)
|
|
|
|
# vk-bootstrap because CMAKE can't find it on NixOS
|
|
FetchContent_Declare(
|
|
vk-bootstrap
|
|
GIT_REPOSITORY https://github.com/charles-lunarg/vk-bootstrap.git
|
|
GIT_TAG v1.3.295
|
|
)
|
|
FetchContent_MakeAvailable(vk-bootstrap)
|
|
|
|
# A function to automate executables aditions
|
|
function ( addBinary BINARY_NAME )
|
|
cmake_parse_arguments ( BINARY "" "SOURCE" "SHADER;LIBS;TEXTURES;MODELS" ${ARGN} )
|
|
if (DEFINED BINARY_SOURCE)
|
|
add_executable ( ${BINARY_NAME} src/${BINARY_SOURCE}.cpp )
|
|
endif()
|
|
# Sets the output dir to have clear separation between binaries
|
|
set_target_properties ( ${BINARY_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${BINARY_NAME} )
|
|
set_target_properties ( ${BINARY_NAME} PROPERTIES CXX_STANDARD 20 ) # Uses C++20
|
|
# Sets per binary LTO
|
|
if(LTO_SUPPORTED AND CMAKE_BUILD_TYPE STREQUAL "Release")
|
|
set_target_properties(${BINARY_NAME} PROPERTIES INTERPROCEDURAL_OPTIMIZATION TRUE)
|
|
message("Enabled LTO")
|
|
endif()
|
|
set_target_properties(${BINARY_NAME} PROPERTIES CXX_SCAN_FOR_MODULES OFF)
|
|
|
|
# Define VULKAN_HPP_HANDLE_ERROR_OUT_OF_DATE_AS_SUCCESS to treat VK_ERROR_OUT_OF_DATE_KHR as a success code
|
|
target_compile_definitions (
|
|
${BINARY_NAME}
|
|
PRIVATE
|
|
"VULKAN_HPP_HANDLE_ERROR_OUT_OF_DATE_AS_SUCCESS"
|
|
$<$<CONFIG:Debug>:DEBUG> # So CMAKE expose a #define DEBUG for debug builds
|
|
# Exposes versions
|
|
BUILD_ID="${ENGINE_VERSION}"
|
|
)
|
|
|
|
|
|
|
|
if (DEFINED BINARY_LIBS)
|
|
target_link_libraries ( ${BINARY_NAME} ${BINARY_LIBS} )
|
|
endif()
|
|
|
|
if (DEFINED BINARY_MODELS) # Adds the 3D models in the binary folder
|
|
set(RESOLVED_MODELS "")
|
|
foreach(MODEL_PATTERN ${BINARY_MODELS})
|
|
file(GLOB MATCHED_MODELS CONFIGURE_DEPENDS
|
|
"${CMAKE_SOURCE_DIR}/assets/models/${MODEL_PATTERN}"
|
|
)
|
|
list(APPEND RESOLVED_MODELS ${MATCHED_MODELS})
|
|
endforeach()
|
|
|
|
if (RESOLVED_MODELS)
|
|
file(COPY ${RESOLVED_MODELS} DESTINATION ${CMAKE_BINARY_DIR}/${BINARY_NAME}/models)
|
|
endif()
|
|
endif()
|
|
|
|
if (DEFINED BINARY_TEXTURES) # Adds the textures in the binary folder
|
|
set(RESOLVED_TEXTURES "")
|
|
foreach(TEX_PATTERN ${BINARY_TEXTURES})
|
|
file(GLOB MATCHED_TEXTURES CONFIGURE_DEPENDS
|
|
"${CMAKE_SOURCE_DIR}/assets/textures/${TEX_PATTERN}"
|
|
)
|
|
list(APPEND RESOLVED_TEXTURES ${MATCHED_TEXTURES})
|
|
endforeach()
|
|
|
|
if (RESOLVED_TEXTURES)
|
|
file(COPY ${RESOLVED_TEXTURES} DESTINATION ${CMAKE_BINARY_DIR}/${BINARY_NAME}/textures)
|
|
endif()
|
|
endif ()
|
|
endfunction()
|
|
|
|
# Adds the main binary
|
|
|
|
addBinary (
|
|
engine
|
|
SOURCE main
|
|
|
|
LIBS glfw fmt vk-bootstrap vulkan
|
|
)
|