Merge pull request #435 from Ygrik2003/Refactoring-cmake

Refactoring cmake
This commit is contained in:
Ygrik2003 2025-03-20 20:59:19 +03:00 committed by GitHub
parent a8eda192af
commit 6c3d2d0907
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
13 changed files with 210 additions and 185 deletions

View File

@ -17,12 +17,7 @@ jobs:
- name: Install dependencies from brew
run: |
brew install glfw3 glew libpng openal-soft luajit libvorbis skypjack/entt/entt googletest
- name: Install specific version of GLM
run: |
curl -O https://raw.githubusercontent.com/Homebrew/homebrew-core/5c7655a866646aa4b857c002b8ae5465b9d26f65/Formula/g/glm.rb
brew install --formula glm.rb
brew install glfw3 glew libpng openal-soft luajit libvorbis skypjack/entt/entt googletest glm
- name: Configure
run: cmake -S . -B build -DCMAKE_BUILD_TYPE=Release -DVOXELENGINE_BUILD_TESTS=ON -DVOXELENGINE_BUILD_APPDIR=1

View File

@ -29,8 +29,6 @@ jobs:
install: >-
mingw-w64-clang-x86_64-toolchain
mingw-w64-clang-x86_64-cmake
mingw-w64-clang-x86_64-make
mingw-w64-clang-x86_64-luajit
git
- name: Set up vcpkg
shell: msys2 {0}
@ -41,11 +39,12 @@ jobs:
./vcpkg integrate install
cd ..
- name: Configure project with CMake and vcpkg
env:
VCPKG_DEFAULT_TRIPLET: x64-mingw-static
VCPKG_DEFAULT_HOST_TRIPLET: x64-mingw-static
shell: msys2 {0}
run: |
export VCPKG_DEFAULT_TRIPLET=x64-mingw-static
export VCPKG_DEFAULT_HOST_TRIPLET=x64-mingw-static
export VCPKG_ROOT=./vcpkg
export VCPKG_ROOT=$(pwd)/vcpkg
mkdir build
cd build
cmake -G "MinGW Makefiles" -DVCPKG_TARGET_TRIPLET=x64-mingw-static -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=./vcpkg/scripts/buildsystems/vcpkg.cmake ..

View File

@ -1,84 +1,47 @@
cmake_minimum_required(VERSION 3.26)
project(VoxelEngine)
option(VOXELENGINE_BUILD_APPDIR "" OFF)
option(VOXELENGINE_BUILD_TESTS "" OFF)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
execute_process(COMMAND ${CMAKE_COMMAND} --version)
option(VOXELENGINE_BUILD_APPDIR "Pack linux build" OFF)
option(VOXELENGINE_BUILD_TESTS "Build tests" OFF)
# Need for static compilation on Windows with MSVC clang TODO: Make single build
# on Windows to avoid dependence on combinations of platforms and compilers and
# make it independent
if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
# We use two types linking: for clang build is static (vcpkg triplet
# x64-windows-static) and for msvc build is dynamic linking (vcpkg triplet
# x64-windows) By default CMAKE_MSVC_RUNTIME_LIBRARY set by
# MultiThreaded$<$<CONFIG:Debug>:Debug>DLL
if(VCPKG_TARGET_TRIPLET MATCHES "static")
# Need for MSVC clang
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
endif()
endif()
add_subdirectory(src)
add_executable(${PROJECT_NAME} src/main.cpp)
target_include_directories(${PROJECT_NAME}
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src)
add_executable(VoxelEngine src/main.cpp)
if(VOXELENGINE_BUILD_APPDIR)
include(${CMAKE_CURRENT_SOURCE_DIR}/dev/cmake/BuildAppdir.cmake)
endif()
if(MSVC)
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE
Release
CACHE STRING "Build type" FORCE)
endif()
if((CMAKE_BUILD_TYPE EQUAL "Release") OR (CMAKE_BUILD_TYPE EQUAL
"RelWithDebInfo"))
target_compile_options(${PROJECT_NAME} PRIVATE /W4 /MT /O2)
else()
target_compile_options(${PROJECT_NAME} PRIVATE /W4)
endif()
set(CMAKE_CXX_FLAGS
"${CMAKE_CXX_FLAGS} /source-charset:UTF-8 /D_DISABLE_CONSTEXPR_MUTEX_CONSTRUCTOR"
)
else()
target_compile_options(
${PROJECT_NAME}
PRIVATE -Wall
-Wextra
# additional warnings
-Wformat-nonliteral
-Wcast-align
-Wpointer-arith
-Wundef
-Wwrite-strings
-Wno-unused-parameter)
if(CMAKE_BUILD_TYPE MATCHES "Debug")
target_compile_options(${PROJECT_NAME} PRIVATE -Og)
endif()
if(WIN32)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static")
endif()
endif()
target_link_libraries(VoxelEngine PRIVATE VoxelEngineSrc
$<$<PLATFORM_ID:Windows>:winmm>)
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -no-pie")
endif()
if(WIN32)
target_link_libraries(${PROJECT_NAME} VoxelEngineSrc winmm)
endif()
target_link_libraries(${PROJECT_NAME} VoxelEngineSrc ${CMAKE_DL_LIBS})
target_link_options(VoxelEngine PRIVATE $<$<CXX_COMPILER_ID:GNU>:-no-pie>)
# Deploy res to build dir
add_custom_command(
TARGET ${PROJECT_NAME}
TARGET VoxelEngine
POST_BUILD
COMMAND
${CMAKE_COMMAND} -E copy_directory_if_different
${CMAKE_CURRENT_SOURCE_DIR}/res $<TARGET_FILE_DIR:${PROJECT_NAME}>/res)
COMMAND ${CMAKE_COMMAND} -E copy_directory_if_different
${CMAKE_CURRENT_SOURCE_DIR}/res $<TARGET_FILE_DIR:VoxelEngine>/res)
if(VOXELENGINE_BUILD_TESTS)
enable_testing()

View File

@ -14,6 +14,34 @@
"CMAKE_TOOLCHAIN_FILE": "$env{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake",
"VOXELENGINE_BUILD_TESTS": "ON"
}
},
{
"name": "default-ninja-gnu-linux",
"condition": {
"type": "equals",
"rhs": "${hostSystemName}",
"lhs": "Linux"
},
"generator": "Ninja Multi-Config",
"binaryDir": "${sourceDir}/build",
"cacheVariables": {
"VOXELENGINE_BUILD_TESTS": "ON",
"VOXELENGINE_BUILD_APPDIR": "ON"
}
},
{
"name": "default-ninja-clang-macos",
"condition": {
"type": "equals",
"rhs": "${hostSystemName}",
"lhs": "Darwin"
},
"generator": "Ninja",
"binaryDir": "${sourceDir}/build",
"cacheVariables": {
"VOXELENGINE_BUILD_TESTS": "ON",
"VOXELENGINE_BUILD_APPDIR": "ON"
}
}
],
"buildPresets": [
@ -21,6 +49,16 @@
"name": "default-vs-msvc-windows",
"configurePreset": "default-vs-msvc-windows",
"configuration": "Debug"
},
{
"name": "default-ninja-gnu-linux",
"configurePreset": "default-ninja-gnu-linux",
"configuration": "Debug"
},
{
"name": "default-ninja-clang-macos",
"configurePreset": "default-ninja-clang-macos",
"configuration": "Debug"
}
],
"testPresets": [
@ -30,6 +68,20 @@
"output": {
"outputOnFailure": true
}
},
{
"name": "default-ninja-gnu-linux",
"configurePreset": "default-ninja-gnu-linux",
"output": {
"outputOnFailure": true
}
},
{
"name": "default-ninja-clang-macos",
"configurePreset": "default-ninja-clang-macos",
"output": {
"outputOnFailure": true
}
}
]
}

View File

@ -1,87 +1,122 @@
project(VoxelEngineSrc)
set(CMAKE_CXX_STANDARD 17)
file(GLOB_RECURSE headers ${CMAKE_CURRENT_SOURCE_DIR}/*.hpp)
file(GLOB_RECURSE sources ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)
list(REMOVE_ITEM sources ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp)
file(GLOB_RECURSE HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/*.hpp)
file(GLOB_RECURSE SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)
list(REMOVE_ITEM SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp)
add_library(${PROJECT_NAME} STATIC ${SOURCES} ${HEADERS})
add_library(VoxelEngineSrc STATIC ${sources} ${headers})
find_package(OpenGL REQUIRED)
find_package(GLEW REQUIRED)
find_package(glm REQUIRED)
if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
# specific for vcpkg
find_package(OpenAL CONFIG REQUIRED)
set(OPENAL_LIBRARY OpenAL::OpenAL)
else()
find_package(OpenAL REQUIRED)
endif()
find_package(ZLIB REQUIRED)
find_package(PNG REQUIRED)
find_package(CURL REQUIRED)
find_package(glfw3 REQUIRED)
if(NOT APPLE)
find_package(EnTT REQUIRED)
endif()
set(LIBS "")
if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
# Use directly linking to lib instead PkgConfig (because pkg-config dont
# install on windows as default) TODO: Do it with findLua.
if(MSVC)
set(LUA_INCLUDE_DIR
find_package(vorbis REQUIRED)
if(VCPKG_TARGET_TRIPLET MATCHES "static")
add_library(luajit STATIC IMPORTED)
set_target_properties(
luajit
PROPERTIES
IMPORTED_LOCATION
"$ENV{VCPKG_ROOT}/packages/luajit_${VCPKG_TARGET_TRIPLET}/lib/libluajit-5.1.a"
INTERFACE_INCLUDE_DIRECTORIES
"$ENV{VCPKG_ROOT}/packages/luajit_${VCPKG_TARGET_TRIPLET}/include/"
)
else()
add_library(luajit SHARED IMPORTED)
set_target_properties(
luajit
PROPERTIES
IMPORTED_LOCATION
"$ENV{VCPKG_ROOT}/packages/luajit_${VCPKG_TARGET_TRIPLET}/bin/lua51.dll"
IMPORTED_IMPLIB
"$ENV{VCPKG_ROOT}/packages/luajit_${VCPKG_TARGET_TRIPLET}/lib/lua51.lib"
INTERFACE_INCLUDE_DIRECTORIES
"$ENV{VCPKG_ROOT}/packages/luajit_${VCPKG_TARGET_TRIPLET}/include/luajit"
)
find_package(Lua REQUIRED)
else()
# Used for mingw-clang cross compiling from msys2
set(LIBS ${LIBS} luajit-5.1)
endif()
find_package(glfw3 REQUIRED)
find_package(glm REQUIRED)
find_package(vorbis REQUIRED)
set(VORBISLIB Vorbis::vorbis Vorbis::vorbisfile)
elseif(APPLE)
find_package(PkgConfig)
pkg_check_modules(LUAJIT REQUIRED luajit)
pkg_check_modules(VORBIS REQUIRED vorbis vorbisfile)
set(LUA_INCLUDE_DIR "/opt/homebrew/include/luajit-2.1")
set(LUA_LIBRARIES "/opt/homebrew/lib/libluajit-5.1.a")
message(STATUS "LUA Libraries: ${LUA_LIBRARIES}")
message(STATUS "LUA Include Dir: ${LUA_INCLUDE_DIR}")
set(VORBISLIB ${VORBIS_LDFLAGS})
message(STATUS "Vorbis Lib: ${VORBIS_LDFLAGS}")
add_library(luajit::luajit ALIAS luajit)
else()
find_package(PkgConfig)
pkg_check_modules(LUAJIT REQUIRED luajit)
pkg_check_modules(VORBIS REQUIRED vorbis vorbisfile)
set(LUA_LIBRARIES ${LUAJIT_LIBRARIES})
set(LUA_INCLUDE_DIR ${LUAJIT_INCLUDE_DIRS})
set(VORBISLIB ${VORBIS_LDFLAGS})
pkg_check_modules(luajit REQUIRED IMPORTED_TARGET luajit)
pkg_check_modules(vorbis REQUIRED IMPORTED_TARGET vorbis)
pkg_check_modules(vorbisfile REQUIRED IMPORTED_TARGET vorbisfile)
add_library(Vorbis::vorbis ALIAS PkgConfig::vorbis)
add_library(Vorbis::vorbisfile ALIAS PkgConfig::vorbisfile)
add_library(luajit::luajit ALIAS PkgConfig::luajit)
endif()
if(UNIX)
find_package(glfw3 3.3 REQUIRED)
find_package(Threads REQUIRED)
set(LIBS ${LIBS} Threads::Threads)
endif()
target_include_directories(VoxelEngineSrc PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
include_directories(${LUA_INCLUDE_DIR})
include_directories(${CURL_INCLUDE_DIR})
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(
${PROJECT_NAME}
${LIBS}
glfw
VoxelEngineSrc
PRIVATE glfw
OpenGL::GL
${OPENAL_LIBRARY}
GLEW::GLEW
ZLIB::ZLIB
PNG::PNG
CURL::libcurl
${VORBISLIB}
${LUA_LIBRARIES}
${CMAKE_DL_LIBS})
OpenAL::OpenAL
Vorbis::vorbis
Vorbis::vorbisfile
luajit::luajit
PUBLIC glm::glm # Need public for src/delegates.hpp, which including to
# main.cpp
)
target_compile_options(
VoxelEngineSrc
PUBLIC $<$<CXX_COMPILER_ID:MSVC>:
/utf-8
/MP
/D_DISABLE_CONSTEXPR_MUTEX_CONSTRUCTOR
/W4
/wd4244 # conversion from 'a' to 'T', possible loss of data
/wd4267 # conversion from 'size_t' to 'int', possible loss of data
/wd4245 # conversion from 'int' to 'const size_t', signed/unsigned
# mismatch
/wd4100 # unreferenced formal parameter
/wd4458 # declaration of 'var' hides class member
/wd4101 # 'var': unreferenced local variable
/wd4388 # 'token' : signed/unsigned mismatch
/wd4018 # '>': signed/unsigned mismatch
>
$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:
-Wall
-Wextra
# additional warnings
-Wformat-nonliteral
-Wcast-align
-Wpointer-arith
-Wundef
-Wwrite-strings
-Wno-unused-parameter
-Wno-sign-compare
$<$<CONFIG:Debug>:-Og>
>)
target_link_options(
VoxelEngineSrc
PUBLIC
$<$<CXX_COMPILER_ID:GNU>:
-no-pie
>
# Need for static compilation on Windows with clang TODO: Make single build
# on Windows to avoid dependence on combinations of platforms and compilers
# and make it independent
$<$<PLATFORM_ID:Windows>:$<$<CXX_COMPILER_ID:Clang>:-static>>)

View File

@ -7,6 +7,7 @@
#include "io/io.hpp"
#include "io/engine_paths.hpp"
#include "logic/scripting/scripting.hpp"
#include "util/stringutil.hpp"
#include "world/generator/GeneratorDef.hpp"
#include "world/generator/VoxelFragment.hpp"
#include "debug/Logger.hpp"

View File

@ -1,12 +1,13 @@
#include "Label.hpp"
#include <utility>
#include "assets/Assets.hpp"
#include "graphics/core/DrawContext.hpp"
#include "graphics/core/Batch2D.hpp"
#include "graphics/core/Font.hpp"
#include "assets/Assets.hpp"
#include "graphics/ui/markdown.hpp"
#include "util/stringutil.hpp"
#include "../markdown.hpp"
using namespace gui;

View File

@ -1,15 +1,16 @@
#include "TextBox.hpp"
#include <algorithm>
#include <sstream>
#include <utility>
#include <algorithm>
#include "Label.hpp"
#include "devtools/syntax_highlighting.hpp"
#include "graphics/core/DrawContext.hpp"
#include "graphics/core/Batch2D.hpp"
#include "graphics/core/Font.hpp"
#include "assets/Assets.hpp"
#include "devtools/syntax_highlighting.hpp"
#include "graphics/core/Batch2D.hpp"
#include "graphics/core/DrawContext.hpp"
#include "graphics/core/Font.hpp"
#include "graphics/ui/markdown.hpp"
#include "util/stringutil.hpp"
#include "window/Events.hpp"
#include "window/Window.hpp"

View File

@ -1,15 +1,15 @@
#pragma once
#include <mutex>
#include <vector>
#include <algorithm>
#include <mutex>
#include <unordered_map>
#include <vector>
#include "typedefs.hpp"
#include "delegates.hpp"
#include "typedefs.hpp"
namespace util {
template<class...Types>
template <class... Types>
class HandlersList {
int nextid = 1;
std::unordered_map<int, std::function<bool(Types...)>> handlers;
@ -45,7 +45,7 @@ namespace util {
});
}
void notify(Types...args) {
void notify(Types... args) {
std::vector<int> orderCopy;
decltype(handlers) handlersCopy;
{

View File

@ -13,6 +13,7 @@
#include "content/Content.hpp"
#include "maths/voxmaths.hpp"
#include <algorithm>
#include <set>
#include <algorithm>
#include <stdint.h>

View File

@ -1,26 +1,22 @@
project(VoxelEngineTest)
set(CMAKE_CXX_STANDARD 17)
file(GLOB_RECURSE SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)
file(GLOB_RECURSE sources ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)
find_package(GTest)
add_executable(${PROJECT_NAME} ${SOURCES})
add_executable(VoxelEngineTest ${sources})
target_include_directories(${PROJECT_NAME}
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../src)
target_link_libraries(${PROJECT_NAME} VoxelEngineSrc GTest::gtest_main)
target_link_libraries(VoxelEngineTest PRIVATE VoxelEngineSrc GTest::gtest_main)
# HACK: copy res to test/ folder for fixing problem compatibility MultiConfig
# and non MultiConfig builds. Delete in future and use only root res folder Also
# this resolve problem with ctests, because it set cwd to
# CMAKE_CURRENT_BINARY_DIR
# ${CMAKE_CURRENT_BINARY_DIR}
add_custom_command(
TARGET ${PROJECT_NAME}
TARGET VoxelEngineTest
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory_if_different
${CMAKE_SOURCE_DIR}/res ${CMAKE_CURRENT_BINARY_DIR}/res)
include(GoogleTest)
gtest_discover_tests(${PROJECT_NAME})
gtest_discover_tests(VoxelEngineTest)

View File

@ -1,7 +1,8 @@
#include "coders/lua_parsing.hpp"
#include <gtest/gtest.h>
#include "coders/commons.hpp"
#include "coders/lua_parsing.hpp"
#include "io/io.hpp"
#include "io/devices/StdfsDevice.hpp"
#include "util/stringutil.hpp"

View File

@ -1,42 +1,22 @@
project(vctest)
set(CMAKE_CXX_STANDARD 17)
add_executable(vctest ${CMAKE_CURRENT_LIST_DIR}/main.cpp)
file(GLOB_RECURSE SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)
# Needed for header-only source util/ArgsReader.hpp
target_include_directories(vctest PRIVATE ${CMAKE_SOURCE_DIR}/src)
add_executable(${PROJECT_NAME} ${SOURCES})
if(MSVC)
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE
Release
CACHE STRING "Build type" FORCE)
endif()
if((CMAKE_BUILD_TYPE EQUAL "Release") OR (CMAKE_BUILD_TYPE EQUAL
"RelWithDebInfo"))
set(CMAKE_MSVC_RUNTIME_LIBRARY
"MultiThreaded$<$<CONFIG:Release>:Release>")
target_compile_options(${PROJECT_NAME} PRIVATE /W4 /MT /O2)
else()
target_compile_options(${PROJECT_NAME} PRIVATE /W4)
endif()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
else()
target_compile_options(
${PROJECT_NAME}
PRIVATE -Wall
target_compile_options(
vctest
PRIVATE $<$<CXX_COMPILER_ID:MSVC>:/W4>
$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:
-Wall
-Wextra
-Wformat-nonliteral
-Wcast-align
-Wpointer-arith
-Wundef
-Wwrite-strings
-Wno-unused-parameter)
endif()
-Wno-unused-parameter
>)
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -no-pie")
endif()
target_include_directories(
${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../src ${CMAKE_DL_LIBS})
target_link_options(vctest PRIVATE $<$<CXX_COMPILER_ID:GNU>:-no-pie>)