Skip to content

Commit

Permalink
Rework build system
Browse files Browse the repository at this point in the history
  • Loading branch information
yeetari committed Jun 14, 2024
1 parent d5660fe commit 617eade
Show file tree
Hide file tree
Showing 10 changed files with 261 additions and 192 deletions.
75 changes: 40 additions & 35 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,51 +9,56 @@ project(vull VERSION 0.1.0 LANGUAGES ASM CXX)
set(CMAKE_POLICY_DEFAULT_CMP0069 NEW)

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
include(CMakeDependentOption)
include(FetchContent)
include(VullAddShader)
include(VullModule)
include(VullVpak)

option(VULL_BUILD_GLTF "Build the vpak tool with glTF support" OFF)
option(VULL_BUILD_PNG "Build the vpak tool with PNG support" OFF)
option(VULL_BUILD_SANDBOX "Build sandbox" ON)
option(VULL_BUILD_TESTS "Build tests" OFF)
set(VULL_ALL_COMPONENTS "graphics;physics;script;ui;x11-window")
set(VULL_ENABLED_COMPONENTS "${VULL_ALL_COMPONENTS}" CACHE STRING "")

if(VULL_BUILD_TESTS)
enable_testing()
endif()
# Ensure enabled components are valid.
foreach(component ${VULL_ENABLED_COMPONENTS})
if(NOT "${component}" IN_LIST VULL_ALL_COMPONENTS)
message(FATAL_ERROR "Unknown vull component ${component}")
endif()
endforeach()

# Set variables for enabled components.
foreach(component ${VULL_ALL_COMPONENTS})
string(TOUPPER "${component}" upper_component)
string(REGEX REPLACE "-" "_" upper_component "${upper_component}")
if("${component}" IN_LIST VULL_ENABLED_COMPONENTS)
set(VULL_BUILD_${upper_component} TRUE)
else()
set(VULL_BUILD_${upper_component} FALSE)
endif()
endforeach()

# TODO: Make vpak tool not require graphics component.
cmake_dependent_option(VULL_BUILD_VPAK "Build the vpak tool" ON "VULL_BUILD_GRAPHICS" OFF)
cmake_dependent_option(VULL_BUILD_SANDBOX "Build the vull sandbox" ${PROJECT_IS_TOP_LEVEL}
"VULL_BUILD_GRAPHICS;VULL_BUILD_PHYSICS;VULL_BUILD_UI;VULL_BUILD_X11_WINDOW" OFF)
option(VULL_BUILD_TESTS "Build the vull tests" ${PROJECT_IS_TOP_LEVEL})

find_package(Freetype)
find_package(harfbuzz)
find_package(X11)
find_package(xxHash REQUIRED)
find_package(Zstd REQUIRED)
find_program(GLSLC glslc REQUIRED)

function(add_shader name)
set(source ${CMAKE_CURRENT_SOURCE_DIR}/${name})
set(binary ${CMAKE_CURRENT_BINARY_DIR}/${name}.spv)
cmake_path(GET source EXTENSION extension)
cmake_path(GET binary PARENT_PATH binary_dir)
file(MAKE_DIRECTORY ${binary_dir})
if(${extension} STREQUAL ".vsl")
# Vull shader.
add_custom_command(
OUTPUT ${binary}
COMMAND vslc ${source} ${binary}
DEPENDS ${source}
VERBATIM)
else()
# GLSL.
add_custom_command(
OUTPUT ${binary}
COMMAND ${GLSLC} -MD --target-env=vulkan1.3 ${source} -o ${binary}
DEPENDS ${source}
DEPFILE ${binary}.d
VERBATIM)
endif()
if(VULL_BUILD_UI)
find_package(Freetype REQUIRED)
find_package(harfbuzz REQUIRED)
endif()

if(VULL_BUILD_X11_WINDOW)
find_package(X11 REQUIRED)
endif()

# TODO: This shouldn't be needed.
set_property(TARGET vull-vulkan APPEND PROPERTY SOURCES ${binary})
endfunction()
# TODO
if(VULL_BUILD_TESTS)
enable_testing()
endif()

add_subdirectory(docs)
add_subdirectory(engine)
Expand Down
6 changes: 4 additions & 2 deletions CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
"cacheVariables": {
"CMAKE_CXX_FLAGS_DEBUG": "-ggdb3",
"CMAKE_CXX_FLAGS_RELEASE": "-O2 -DNDEBUG",
"CMAKE_EXPORT_COMPILE_COMMANDS": "On",
"VULL_BUILD_TESTS": "On",
"CMAKE_CXX_FLAGS_RELWITHDEBINFO": "-ggdb3 -O2 -DNDEBUG",
"CMAKE_EXPORT_COMPILE_COMMANDS": "ON",
"VULL_COMPILER_FLAGS": "-fno-exceptions -fno-rtti"
}
},
Expand All @@ -34,6 +34,7 @@
"hidden": true,
"inherits": "base",
"cacheVariables": {
"CMAKE_ASM_COMPILER": "gcc",
"CMAKE_C_COMPILER": "gcc",
"CMAKE_CXX_COMPILER": "g++",
"VULL_WARNING_FLAGS": "-Wall -Wextra -Wno-alloc-size-larger-than -Wno-class-memaccess -Wno-double-promotion -Wno-missing-field-initializers -Wno-non-virtual-dtor -Wno-switch -Wno-unknown-pragmas"
Expand Down Expand Up @@ -97,6 +98,7 @@
{
"name": "fuzz",
"inherits": "base",
"displayName": "Fuzz with AFLplusplus",
"cacheVariables": {
"CMAKE_AR": "llvm-ar",
"CMAKE_ASM_COMPILER": "afl-clang-lto",
Expand Down
27 changes: 27 additions & 0 deletions cmake/VullAddShader.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
function(vull_add_shader name)
set(source ${CMAKE_CURRENT_SOURCE_DIR}/${name})
set(binary ${CMAKE_CURRENT_BINARY_DIR}/${name}.spv)
cmake_path(GET source EXTENSION extension)
cmake_path(GET binary PARENT_PATH binary_dir)
file(MAKE_DIRECTORY ${binary_dir})
message(STATUS "Adding output for ${binary}")
if(${extension} STREQUAL ".vsl")
# Vull shader.
add_custom_command(
OUTPUT ${binary}
COMMAND vslc ${source} ${binary}
DEPENDS ${source}
VERBATIM)
else()
# GLSL.
add_custom_command(
OUTPUT ${binary}
COMMAND ${GLSLC} -MD --target-env=vulkan1.3 ${source} -o ${binary}
DEPENDS ${source}
DEPFILE ${binary}.d
VERBATIM)
endif()

# TODO: This shouldn't be needed.
# set_property(TARGET vull APPEND PROPERTY OBJECT_DEPENDS ${binary})
endfunction()
1 change: 1 addition & 0 deletions cmake/VullVpak.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ function(target_add_vpak target name)
COMMAND rm -f ${vpak_path}
COMMAND vpak add ${vpak_path} ${item_list}
DEPENDS "${dependency_list}")
set_property(SOURCE ${name} APPEND PROPERTY OBJECT_DEPENDS ${CMAKE_BINARY_DIR}/engine/shaders/blit_tonemap.frag.spv)
set_property(TARGET ${target} APPEND PROPERTY SOURCES ${name})
endfunction()
95 changes: 53 additions & 42 deletions engine/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,31 +1,41 @@
vull_module(core xxHash::xxHash Zstd::Zstd)
vull_module(graphics vull::core vull::vulkan)
vull_module(json vull::core)
vull_module(physics vull::core)
vull_module(script vull::core)
vull_module(shaderc vull::core)
vull_module(ui vull::core vull::vulkan PRIVATE Freetype::Freetype harfbuzz::harfbuzz)
vull_module(vulkan vull::core)
vull_module(x11-window vull::core PRIVATE X11::xcb X11::xcb_randr X11::xcb_util X11::xcb_xkb)
# Main vull library.
add_library(vull)
target_compile_features(vull PRIVATE cxx_std_20)
target_include_directories(vull PUBLIC include)
target_link_libraries(vull PUBLIC xxHash::xxHash PRIVATE Zstd::Zstd)

add_subdirectory(sources)
target_include_directories(vull-core PUBLIC include)
target_include_directories(vull-script PRIVATE sources)
if(VULL_BUILD_UI)
target_link_libraries(vull PRIVATE Freetype::Freetype harfbuzz::harfbuzz)
endif()

if(VULL_BUILD_X11_WINDOW)
target_link_libraries(vull PRIVATE X11::xcb X11::xcb_randr X11::xcb_util X11::xcb_xkb)
endif()

# Separate shaderc library. This is a separate library rather than a component since you'd likely want the shader
# compiler to be built for development, but not shipped with the final game.
add_library(vull-shaderc)
target_compile_features(vull-shaderc PRIVATE cxx_std_20)
target_link_libraries(vull-shaderc PRIVATE vull)

add_shader(shaders/blit_tonemap.frag)
add_shader(shaders/default.frag)
add_shader(shaders/default.vert)
add_shader(shaders/deferred.comp)
add_shader(shaders/depth_reduce.comp)
add_shader(shaders/draw_cull.comp)
add_shader(shaders/fst.vert)
add_shader(shaders/light_cull.comp)
add_shader(shaders/object.vsl)
add_shader(shaders/shadow.vert)
add_shader(shaders/skybox.frag)
add_shader(shaders/skybox.vert)
add_shader(shaders/ui.frag)
add_shader(shaders/ui.vert)
# Add alias targets.
add_library(vull::vull ALIAS vull)
add_library(vull::shaderc ALIAS vull-shaderc)

vull_add_shader(shaders/blit_tonemap.frag)
vull_add_shader(shaders/default.frag)
vull_add_shader(shaders/default.vert)
vull_add_shader(shaders/deferred.comp)
vull_add_shader(shaders/depth_reduce.comp)
vull_add_shader(shaders/draw_cull.comp)
vull_add_shader(shaders/fst.vert)
vull_add_shader(shaders/light_cull.comp)
vull_add_shader(shaders/object.vsl)
vull_add_shader(shaders/shadow.vert)
vull_add_shader(shaders/skybox.frag)
vull_add_shader(shaders/skybox.vert)
vull_add_shader(shaders/ui.frag)
vull_add_shader(shaders/ui.vert)

function(vull_depend_builtin target)
target_add_vpak(${target} builtin.vpak
Expand All @@ -47,7 +57,8 @@ function(vull_depend_builtin target)
${CMAKE_BINARY_DIR}/engine/shaders/ui.vert.spv /shaders/ui.vert)
endfunction()

set_target_properties(vull-vulkan PROPERTIES
add_subdirectory(sources)
set_target_properties(vull PROPERTIES
UNITY_BUILD ON
UNITY_BUILD_MODE GROUP)
set_source_files_properties(
Expand All @@ -64,18 +75,18 @@ set_source_files_properties(
sources/vulkan/swapchain.cc
PROPERTIES UNITY_GROUP "vulkan-wrappers"
)

if(VULL_BUILD_TESTS)
add_executable(vull-tests)
add_subdirectory(tests)
target_compile_features(vull-tests PRIVATE cxx_std_20)
target_link_libraries(vull-tests PRIVATE vull::core vull::json vull::script vull::shaderc)

add_custom_command(TARGET vull-tests
POST_BUILD
COMMAND "${CMAKE_COMMAND}"
-D "\"TEST_EXECUTABLE:FILEPATH=$<TARGET_FILE:vull-tests>\""
-P \"${CMAKE_SOURCE_DIR}/cmake/VullAddTests.cmake\"
BYPRODUCTS vull_tests.cmake)
set_property(DIRECTORY APPEND PROPERTY TEST_INCLUDE_FILES vull_tests.cmake)
endif()
#
#if(VULL_BUILD_TESTS)
# add_executable(vull-tests)
# add_subdirectory(tests)
# target_compile_features(vull-tests PRIVATE cxx_std_20)
# target_link_libraries(vull-tests PRIVATE vull::core vull::json vull::script vull::shaderc)
#
# add_custom_command(TARGET vull-tests
# POST_BUILD
# COMMAND "${CMAKE_COMMAND}"
# -D "\"TEST_EXECUTABLE:FILEPATH=$<TARGET_FILE:vull-tests>\""
# -P \"${CMAKE_SOURCE_DIR}/cmake/VullAddTests.cmake\"
# BYPRODUCTS vull_tests.cmake)
# set_property(DIRECTORY APPEND PROPERTY TEST_INCLUDE_FILES vull_tests.cmake)
#endif()
12 changes: 12 additions & 0 deletions engine/include/vull/test/test.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

namespace vull {

class TestDescriptor {};

} // namespace vull

#define VULL_TEST_CASE(suite, name) \
static void suite##_##name(); \
[[gnu::section("vulltest"), gnu::used]] vull::TestDescriptor s_##suite##_##name##_descriptor; \
static void suite##_##name()
Loading

0 comments on commit 617eade

Please sign in to comment.