diff --git a/.github/workflows/windows-clang.yml b/.github/workflows/windows-clang.yml index 183f2d6d..2c761592 100644 --- a/.github/workflows/windows-clang.yml +++ b/.github/workflows/windows-clang.yml @@ -45,9 +45,10 @@ jobs: run: | export VCPKG_DEFAULT_TRIPLET=x64-mingw-static export VCPKG_DEFAULT_HOST_TRIPLET=x64-mingw-static + export VCPKG_ROOT=./vcpkg mkdir build cd build - cmake -G "MinGW Makefiles" -DVCPKG_TARGET_TRIPLET=x64-mingw-static -DCMAKE_BUILD_TYPE=Release -DVOXELENGINE_BUILD_WINDOWS_VCPKG=ON .. + cmake -G "MinGW Makefiles" -DVCPKG_TARGET_TRIPLET=x64-mingw-static -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=./vcpkg/scripts/buildsystems/vcpkg.cmake .. cmake --build . --config Release - name: Package for Windows run: | @@ -72,4 +73,4 @@ jobs: shell: msys2 {0} working-directory: ${{ github.workspace }} run: | - packaged/vctest.exe -e packaged/VoxelCore.exe -d dev/tests -u build --output-always + packaged/vctest.exe -e packaged/VoxelCore.exe -d dev/tests -u build --output-always \ No newline at end of file diff --git a/.github/workflows/windows.yml b/.github/workflows/windows.yml index d417197c..cc95fe82 100644 --- a/.github/workflows/windows.yml +++ b/.github/workflows/windows.yml @@ -21,21 +21,20 @@ jobs: with: submodules: 'true' - - name: Set up vcpkg + - name: Bootstrap vcpkg + shell: pwsh run: | git clone https://github.com/microsoft/vcpkg.git - cd vcpkg - .\bootstrap-vcpkg.bat - .\vcpkg integrate install - cd .. + ${{ github.workspace }}/vcpkg/bootstrap-vcpkg.bat + - name: Configure and build project with CMake and vcpkg + env: + VCPKG_ROOT: ${{ github.workspace }}/vcpkg run: | - mkdir build - cd build - cmake -DCMAKE_BUILD_TYPE=Release -DVOXELENGINE_BUILD_WINDOWS_VCPKG=ON -DVOXELENGINE_BUILD_TESTS=ON .. - cmake --build . --config Release + cmake --preset default-vs-msvc-windows + cmake --build --preset default-vs-msvc-windows --config Release - name: Run tests - run: ctest --output-on-failure --test-dir build + run: ctest --preset default-vs-msvc-windows - name: Run engine tests run: | build/vctest/Release/vctest.exe -e build/Release/VoxelEngine.exe -d dev/tests -u build --output-always @@ -43,11 +42,12 @@ jobs: - name: Package for Windows run: | mkdir packaged - cp -r build/* packaged/ - cp C:/Windows/System32/msvcp140.dll packaged/Release/msvcp140.dll - mv packaged/Release/VoxelEngine.exe packaged/Release/VoxelCore.exe + cp -r build/Release/* packaged/ + cp build/vctest/Release/vctest.exe packaged/ + cp C:/Windows/System32/msvcp140.dll packaged/msvcp140.dll + mv packaged/VoxelEngine.exe packaged/VoxelCore.exe working-directory: ${{ github.workspace }} - uses: actions/upload-artifact@v4 with: name: Windows-Build - path: 'build/Release/*' + path: 'packaged/*' diff --git a/.gitignore b/.gitignore index a1ce6e33..9056cce5 100644 --- a/.gitignore +++ b/.gitignore @@ -36,10 +36,6 @@ Debug/voxel_engine AppDir appimage-build/ -# for vcpkg -/vcpkg/ -.gitmodules - # macOS folder attributes *.DS_Store diff --git a/CMakeLists.txt b/CMakeLists.txt index a2415ad9..33721bc1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,15 +1,21 @@ -option(VOXELENGINE_BUILD_WINDOWS_VCPKG ON) -if(VOXELENGINE_BUILD_WINDOWS_VCPKG AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/vcpkg/scripts/buildsystems/vcpkg.cmake") - set(CMAKE_TOOLCHAIN_FILE "${CMAKE_CURRENT_SOURCE_DIR}/vcpkg/scripts/buildsystems/vcpkg.cmake" CACHE STRING "") -endif() - cmake_minimum_required(VERSION 3.15) project(VoxelEngine) -option(VOXELENGINE_BUILD_APPDIR OFF) -option(VOXELENGINE_BUILD_TESTS OFF) +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) + +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$<$:Debug>DLL + if (VCPKG_TARGET_TRIPLET MATCHES "static") + set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>") + endif() +endif() add_subdirectory(src) add_executable(${PROJECT_NAME} src/main.cpp) @@ -24,7 +30,6 @@ if(MSVC) 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$<$:Release>") target_compile_options(${PROJECT_NAME} PRIVATE /W4 /MT /O2) else() target_compile_options(${PROJECT_NAME} PRIVATE /W4) @@ -40,35 +45,10 @@ else() target_compile_options(${PROJECT_NAME} PRIVATE -Og) endif() if (WIN32) - set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libstdc++") + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static") endif() endif() -if(VOXELENGINE_BUILD_WINDOWS_VCPKG AND WIN32) - if(NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/vcpkg/.git") - find_package(Git QUIET) - if(GIT_FOUND) - message(STATUS "Adding vcpkg as a git submodule...") - execute_process(COMMAND ${GIT_EXECUTABLE} submodule add https://github.com/microsoft/vcpkg.git vcpkg WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) - else() - message(FATAL_ERROR "Git not found, cannot add vcpkg submodule.") - endif() - endif() - - if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/vcpkg/.git") - message(STATUS "Initializing and updating vcpkg submodule...") - execute_process(COMMAND ${GIT_EXECUTABLE} submodule update --init --recursive WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) - execute_process(COMMAND ${CMAKE_COMMAND} -E chdir vcpkg ./bootstrap-vcpkg.bat WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) - endif() - - foreach(CONFIG_TYPE ${CMAKE_CONFIGURATION_TYPES}) - string(TOUPPER ${CONFIG_TYPE} CONFIG_TYPE_UPPER) - add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD - COMMAND ${CMAKE_COMMAND} -E copy_directory - ${CMAKE_SOURCE_DIR}/res ${CMAKE_BINARY_DIR}/${CONFIG_TYPE_UPPER}/res) - endforeach() -endif() - if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -no-pie -lstdc++fs") endif() @@ -79,7 +59,14 @@ endif() target_link_libraries(${PROJECT_NAME} VoxelEngineSrc ${CMAKE_DL_LIBS}) -file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/res DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) +# Deploy res to build dir +add_custom_command( + TARGET ${PROJECT_NAME} + POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy_directory_if_different + ${CMAKE_CURRENT_SOURCE_DIR}/res + $/res + ) if (VOXELENGINE_BUILD_TESTS) enable_testing() diff --git a/CMakePresets.json b/CMakePresets.json new file mode 100644 index 00000000..d4c6aa47 --- /dev/null +++ b/CMakePresets.json @@ -0,0 +1,35 @@ +{ + "version": 6, + "configurePresets": [ + { + "name": "default-vs-msvc-windows", + "condition": { + "type": "equals", + "rhs": "${hostSystemName}", + "lhs": "Windows" + }, + "generator": "Visual Studio 17 2022", + "binaryDir": "${sourceDir}/build", + "cacheVariables": { + "CMAKE_TOOLCHAIN_FILE": "$env{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake", + "VOXELENGINE_BUILD_TESTS": "ON" + } + } + ], + "buildPresets": [ + { + "name": "default-vs-msvc-windows", + "configurePreset": "default-vs-msvc-windows", + "configuration": "Debug" + } + ], + "testPresets": [ + { + "name": "default-vs-msvc-windows", + "configurePreset": "default-vs-msvc-windows", + "output": { + "outputOnFailure": true + } + } + ] +} \ No newline at end of file diff --git a/README.md b/README.md index 418d372c..e31e1436 100644 --- a/README.md +++ b/README.md @@ -108,28 +108,32 @@ cmake --build . >[!NOTE] > Requirement: > -> vcpkg, CMake +> vcpkg, CMake, Git +There are two options to use vcpkg: +1. If you have Visual Studio installed, most likely the **VCPKG_ROOT** environment variable will already exist in **Developer Command Prompt for VS** +2. If you want use **vcpkg**, install **vcpkg** from git to you system: +```PowerShell +cd C:/ +git clone https://github.com/microsoft/vcpkg.git +cd vcpkg +.\bootstrap-vcpkg.bat +``` +After installing **vcpkg**, setup env variable **VCPKG_ROOT** and add it to **PATH**: +```PowerShell +$env:VCPKG_ROOT = "C:\path\to\vcpkg" +$env:PATH = "$env:VCPKG_ROOT;$env:PATH" +``` +>[!TIP] +>For troubleshooting you can read full [documentation](https://learn.microsoft.com/ru-ru/vcpkg/get_started/get-started?pivots=shell-powershell) for **vcpkg** -```sh +After installing **vcpkg** you can build project: +```PowerShell git clone --recursive https://github.com/MihailRis/VoxelEngine-Cpp.git cd VoxelEngine-Cpp -mkdir build -cd build -cmake -DCMAKE_BUILD_TYPE=Release -DVOXELENGINE_BUILD_WINDOWS_VCPKG=ON .. -del CMakeCache.txt -rmdir /s /q CMakeFiles -cmake -DCMAKE_BUILD_TYPE=Release -DVOXELENGINE_BUILD_WINDOWS_VCPKG=ON .. -cmake --build . --config Release +cmake --preset default-vs-msvc-windows +cmake --build --preset default-vs-msvc-windows ``` -> [!TIP] -> You can use ```rm CMakeCache.txt``` and ```rm -rf CMakeFiles``` while using Git Bash - -> [!WARNING] -> If you have issues during the vcpkg integration, try navigate to ```vcpkg\downloads``` -> and extract PowerShell-[version]-win-x86 to ```vcpkg\downloads\tools``` as powershell-core-[version]-windows. -> Then rerun ```cmake -DCMAKE_BUILD_TYPE=Release -DVOXELENGINE_BUILD_WINDOWS_VCPKG=ON ..``` - ## Build using Docker ### Step 0. Install docker on your system diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 4e8ae348..6e9ee5bc 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -6,13 +6,16 @@ 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} ${SOURCES} ${HEADERS}) - -option(VOXELENGINE_BUILD_WINDOWS_VCPKG ON) +add_library(${PROJECT_NAME} STATIC ${SOURCES} ${HEADERS}) find_package(OpenGL REQUIRED) find_package(GLEW REQUIRED) -find_package(OpenAL REQUIRED) +if (CMAKE_SYSTEM_NAME STREQUAL "Windows") + # specific for vcpkg + find_package(OpenAL CONFIG REQUIRED) +else() + find_package(OpenAL REQUIRED) +endif() find_package(ZLIB REQUIRED) find_package(PNG REQUIRED) find_package(CURL REQUIRED) @@ -22,26 +25,21 @@ endif() set(LIBS "") -if (WIN32) - if(VOXELENGINE_BUILD_WINDOWS_VCPKG) - if (MSVC) - set(LUA_LIBRARIES "${CMAKE_CURRENT_SOURCE_DIR}/../vcpkg/packages/luajit_x64-windows/lib/lua51.lib") - set(LUA_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../vcpkg/packages/luajit_x64-windows/include/luajit") - else() - find_package(PkgConfig) - pkg_check_modules(OpenAL REQUIRED IMPORTED_TARGET openal) - set(LIBS ${LIBS} luajit-5.1 wsock32 ws2_32 pthread PkgConfig::OpenAL -static-libstdc++) - message(${OPENAL_LIBRARY}) - endif() - find_package(glfw3 REQUIRED) - find_package(glm REQUIRED) - find_package(vorbis REQUIRED) - set(VORBISLIB Vorbis::vorbis Vorbis::vorbisfile) +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 "$ENV{VCPKG_ROOT}/packages/luajit_${VCPKG_TARGET_TRIPLET}/include/luajit") + find_package(Lua REQUIRED) else() - find_package(Lua REQUIRED) - set(VORBISLIB vorbis vorbisfile) # not tested - add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/libs/glfw) + # 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) @@ -71,4 +69,4 @@ endif() 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 OpenGL::GL ${OPENAL_LIBRARY} GLEW::GLEW ZLIB::ZLIB PNG::PNG CURL::libcurl ${VORBISLIB} ${LUA_LIBRARIES} ${CMAKE_DL_LIBS}) +target_link_libraries(${PROJECT_NAME} ${LIBS} glfw OpenGL::GL OpenAL::OpenAL GLEW::GLEW ZLIB::ZLIB PNG::PNG CURL::libcurl ${VORBISLIB} ${LUA_LIBRARIES} ${CMAKE_DL_LIBS}) diff --git a/src/engine/Engine.cpp b/src/engine/Engine.cpp index 0770a3f6..2d73c131 100644 --- a/src/engine/Engine.cpp +++ b/src/engine/Engine.cpp @@ -1,6 +1,8 @@ #include "Engine.hpp" +#ifndef GLEW_STATIC #define GLEW_STATIC +#endif #include "debug/Logger.hpp" #include "assets/AssetsLoader.hpp" diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index cbe038a6..0597d6e4 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -15,5 +15,16 @@ target_link_libraries( 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 +add_custom_command( + TARGET ${PROJECT_NAME} + 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}) diff --git a/test/coders/vec3.cpp b/test/coders/vec3.cpp index cb699bc3..d404e92b 100644 --- a/test/coders/vec3.cpp +++ b/test/coders/vec3.cpp @@ -5,7 +5,7 @@ TEST(VEC3, Decode) { auto file = std::filesystem::u8path( - "../res/models/block.vec3" + "res/models/block.vec3" ); auto bytes = files::read_bytes_buffer(file); auto model = vec3::load(file.u8string(), bytes);