Begin to migrate framework-core and unit tests to cmake

Begin work #144
This commit is contained in:
S David 2023-12-05 13:27:33 -05:00
parent 946473f20d
commit 08bb8f364a
6 changed files with 297 additions and 93 deletions

194
CMakeLists.txt Normal file
View File

@ -0,0 +1,194 @@
cmake_minimum_required(VERSION 3.24)
# Set build conditions, prevent in-source builds, check cmake policies @{
if (CMAKE_BINARY_DIR STREQUAL CMAKE_SOURCE_DIR)
message(FATAL_ERROR "Source and build directories cannot be the same.")
endif()
if (CMAKE_VERSION VERSION_GREATER_EQUAL "3.24.0")
cmake_policy(SET CMP0135 NEW)
endif()
# When this package is included as a subproject, there's no need to
# build and run the unit-tests. setting -D ENABLE_TESTS to false could
# speed up build time for pacakge maintainers
if (NOT DEFINED PROJECT_NAME)
option(ENABLE_TESTS "Build and run unit tests" ON)
else()
option(ENABLE_TESTS "Build and run unit tests" OFF)
endif()
# @}
# @{ Define build options and build defaults
if (UNIX AND NOT(CMAKE_HOST_SYSTEM_NAME STREQUAL "Linux"))
option(USE_BOOST_STACKTRACE "Use Boost::stacktrace for stack traces" OFF)
option(USE_EXECINFO_STACKTRACE "Use BSD/UNIX execinfo for stack traces" ON)
else()
option(USE_BOOST_STACKTRACE "Use Boost::stacktrace for stack traces" ON)
option(USE_EXECINFO_STACKTRACE "Use BSD/UNIX execinfo for stack traces" OFF)
endif()
# @}
project(ElementalEngine
VERSION 0.1
LANGUAGES C CXX
# HOMEPAGE_URL
DESCRIPTION "[WIP] Simple game written in C++ and Javascript"
)
IF(NOT CMAKE_BUILD_TYPE)
SET( CMAKE_BUILD_TYPE Debug ... FORCE )
ENDIF()
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_C_STANDARD 17)
set(CMAKE_C_EXTENSIONS OFF)
set(CMAKE_CXX_EXTENSIONS OFF)
# @{ Nice-to-haves, ccache and compile_commands.json for clangd
find_program(CCACHE_PATH ccache)
if(CCACHE_PATH)
set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ${CCACHE_PATH})
set_property(GLOBAL PROPERTY RULE_LAUNCH_LINK ${CCACHE_PATH})
endif(CCACHE_PATH)
set(CMAKE_EXPORT_COMPILE_COMMANDS On)
# @}
# @{ Dependency management
include(FetchContent)
include(CheckIncludeFile)
find_package(PkgConfig REQUIRED)
pkg_check_modules(CATCH2 "catch2-with-main >= 2.13.7")
pkg_check_modules(SDL2 "sdl >= 2")
pkg_check_modules(SDL2_IMG "SDL2_image >= 0.29.0")
pkg_check_modules(SDL2_GFX "SDL2_gfx >= 1.0.2")
pkg_check_modules(NLOHMANN_JSON "nlohmann_json >= 3.11.10")
if (CATCH2_FOUND)
set(USE_SYSTEM_CATCH2 ON)
endif()
if (SDL2_FOUND)
include_directories(${SDL2_INCLUDE_DIRS})
endif()
if (SDL2_IMG_FOUND)
include_directories(${SDL2_IMG_INCLUDE_DIRS})
endif()
if (SDL2_GFX_FOUND)
include_directories(${SDL2_GFX_INCLUDE_DIRS})
endif()
if (NLOHMANN_JSON_FOUND)
include_directories(${NLOHMANN_JSON_INCLUDE_DIRS})
endif()
# @{ Download and build unit test engine only if needed
if(ENABLE_TESTS AND NOT USE_SYSTEM_CATCH2)
FetchContent_Declare(
Catch2
URL https://github.com/catchorg/Catch2/archive/refs/tags/v3.4.0.zip
URL_HASH MD5=c426e77d4ee0055410bc930182959ae5
FIND_PACKAGE_ARGS
)
FetchContent_MakeAvailable(Catch2)
if (TARGET Catch2)
set_target_properties(Catch2 PROPERTIES CXX_STANDARD 2020)
endif()
if (TARGET Catch2WithMain)
set_target_properties(Catch2WithMain PROPERTIES CXX_STANDARD 20)
endif()
elseif(ENABLE_TESTS AND USE_SYSTEM_CATCH2)
include_directories(${CATCH2_INCLUDE_DIRS})
link_directories(${CATCH2_LIBRARY_DIRS})
endif()
if (ENABLE_TESTS)
FetchContent_Declare(
FakeIt
URL https://github.com/eranpeer/FakeIt/archive/refs/tags/2.4.0.zip
URL_HASH MD5=72e4ce7f1c0de97074d2d5b517753286
FIND_PACKAGE_ARGS
)
FetchContent_MakeAvailable(FakeIt)
endif()
# @}
# @{ Boost-Specific Options
if(USE_BOOST_STACKTRACE)
set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_STATIC_RUNTIME OFF) # Do not require static C++ runtime
set(Boost_USE_MULTITHREADED ON)
find_package(Boost 1.82.0 COMPONENTS system filesystem REQUIRED)
endif()
if (Boost_FOUND)
add_definitions(-DBOOST_STACKTRACE_USE_ADDR2LINE=1)
include_directories(${Boost_INCLUDE_DIRS})
endif()
#@}
#@{ QT5 Dependency detection & tool setup
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
find_package(Qt5 COMPONENTS Widgets Gui REQUIRED)
#@}
#@}
add_custom_target(copy_assets
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_LIST_DIR}/res ${CMAKE_BINARY_DIR}/build/share/
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_LIST_DIR}/res ${CMAKE_BINARY_DIR}/tests/res
)
if (USE_EXECINFO_STACKTRACE)
CHECK_INCLUDE_FILE("execinfo.h" HAVE_EXECINFO_H)
if (HAVE_EXECINFO_H)
add_definitions(-DHAVE_EXECINFO_H=1)
endif()
find_library(LIB_EXEC_INFO
NAMES execinfo # Specify the library name without the 'lib' prefix or file extension
HINTS /usr/lib /usr/local/lib # Optional hint for the library location
)
if (LIB_EXEC_INFO)
message(STATUS "Found libexecinfo: ${LIB_EXEC_INFO}")
set(STACKTRACE_DEP_LIBS ${LIB_EXEC_INFO})
endif()
endif()
include_directories(
${CMAKE_SOURCE_DIR}/src
#${CMAKE_SOURCE_DIR}/include
#${CMAKE_SOURCE_DIR}/include/private
)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/build/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/build/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/build/bin)
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
set(custom_dbg_flags "-O0 -ggdb")
# Custom flags for Debug configuration
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} ${custom_dbg_flags}")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${custom_dbg_flags}")
elseif(CMAKE_BUILD_TYPE STREQUAL "Release")
set(custom_rel_flags "-Os")
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} ${custom_rel_flags}")
set(CMAKE_CXX_FLAGS_RELEASE
"${CMAKE_CXX_FLAGS_RELEASE} ${custom_rel_flags}"
)
endif()
add_subdirectory(src)
#add_subdirectory(apps)
# vim:set noet sts=0 sw=8 ts=8 foldmethod=marker foldmarker=@{,@}:

16
src/CMakeLists.txt Normal file
View File

@ -0,0 +1,16 @@
add_subdirectory(framework)
if (ENABLE_TESTS)
add_subdirectory(tests)
add_custom_target(ctest
COMMAND ctest -C $CONFIGURATION --test-dir ./tests --output-on-failure
)
add_dependencies(ctest
framework-tests
copy_assets
)
endif()
#add_subdirectory(game)
#add_subdirectory(editors)

View File

@ -0,0 +1,40 @@
set(FRAMEWORK_SOURCES
core/Observable.cpp
core/components/Entity.cpp
core/components/EntityManager.cpp
core/components/PlayerBehavior.cpp
core/components/PositionAttribute.cpp
core/input/EventDispatcher.cpp)
set(FRAMEWORK_LINK_LIBS ${STACKTRACE_DEP_LIBS} )
add_library(framework-core SHARED ${FRAMEWORK_SOURCES})
add_library(framework-core_static STATIC ${FRAMEWORK_SOURCES})
add_library(framework-core_test STATIC ${FRAMEWORK_SOURCES})
target_compile_options(framework-core PRIVATE ${CMAKE_CXX_FLAGS})
target_compile_options(framework-core_static PRIVATE ${CMAKE_CXX_FLAGS})
target_compile_options(framework-core_test PRIVATE ${CMAKE_CXX_FLAGS})
# Check if the system is Linux
set_target_properties(framework-core_static PROPERTIES
CMAKE_POSITION_INDEPENDENT_CODE OFF # This forces all linkages to be static (?)
)
set_target_properties(framework-core_test PROPERTIES
CMAKE_POSITION_INDEPENDENT_CODE OFF # This forces all linkages to be static (?)
)
target_compile_definitions(framework-core_test PRIVATE -DTESTING)
# add -rdynamic link option for Boost::stacktrace on Linux
if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
# Add the -rdynamic linker option
target_link_options(framework-core PUBLIC -rdynamic)
target_link_options(framework-core_static PUBLIC -rdynamic)
target_link_options(framework-core_test PUBLIC -rdynamic)
endif()
target_link_libraries(framework-core PRIVATE ${FRAMEWORK_LINK_LIBS})
target_link_libraries(framework-core_static PRIVATE ${FRAMEWORK_LINK_LIBS})
target_link_libraries(framework-core_test PRIVATE ${FRAMEWORK_LINK_LIBS})

View File

@ -1,18 +1,4 @@
/* requirements.h
* Copyright © 2019-2022 Saul D. Beniquez
* License: Mozilla Public License v. 2.0
*
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v.2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at https://mozilla.org/MPL/2.0/.
*/
#pragma once
#include <config.h>
#if __cplusplus < 202002L
#error This framework requires C++20, at least
#endif
// clang-format off
// vim: set foldmethod=marker foldmarker=#region,#endregion textwidth=80 ts=8 sts=0 sw=8 noexpandtab ft=cpp.doxygen :
/Users/sdavid/Developer/Personal/C++/quartz-warriors/.clang-format:15:1: error: duplicated mapping key 'ConstructorInitializerIndentWidth'
ConstructorInitializerIndentWidth: 4
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Error reading /Users/sdavid/Developer/Personal/C++/quartz-warriors/.clang-format: Invalid argument

View File

@ -1,75 +1,4 @@
/* debuginfo.h
* Copyright © 2020-2023 Saul D. Beniquez
* License: Mozilla Public License v. 2.0
*
* This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#pragma once
#include "config.h"
#include "framework/core/types.hpp"
#ifdef HAVE_EXECINFO_H
#include <execinfo.h>
#else
#include <boost/stacktrace.hpp>
#endif
#include <iostream>
#include <sstream>
using namespace QW::Core::Types;
#ifdef QW_DEBUG // #region
#include <iostream>
#define debugprint(msg) std::cout << "#*!* " << msg << std::endl
#else
#define debugprint(msg) ;
#endif // #endregion
inline std::string
generate_stacktrace()
{
std::stringstream buffer;
#ifdef HAVE_EXECINFO_H
void* callstack[128];
int i, frames = backtrace(callstack, 128);
char** strs = backtrace_symbols(callstack, frames);
for (i = 0; i < frames; ++i) {
buffer << strs[i] << std::endl;
}
free(strs);
#else
buffer << boost::stacktrace::stacktrace() << std::flush;
#endif
return buffer.str();
}
inline void
print_backtrace()
{
std::cout << generate_stacktrace() << std::endl;
}
inline void
print_backtrace(std::exception& ex)
{
std::cout << "caught: " << ex.what() << std::endl;
print_backtrace();
}
inline void
print_cmdline(int argc, const c_string argv[])
{
int i;
std::cout << "Command-line received" << std::endl;
for (i = 0; i < argc; ++i)
std::cout << argv[i] << " ";
std::cout << std::endl;
}
// clang-format off
// vim: set foldmethod=marker foldmarker=#region,#endregion textwidth=80 ts=8 sts=0 sw=8 noexpandtab ft=cpp.doxygen :
/Users/sdavid/Developer/Personal/C++/quartz-warriors/.clang-format:15:1: error: duplicated mapping key 'ConstructorInitializerIndentWidth'
ConstructorInitializerIndentWidth: 4
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Error reading /Users/sdavid/Developer/Personal/C++/quartz-warriors/.clang-format: Invalid argument

39
src/tests/CMakeLists.txt Normal file
View File

@ -0,0 +1,39 @@
# Add the test executable
set(FRAMEWORK_TEST_SOURCE_LIST
framework/types/adapters/Sequential.test.cpp
framework/core/input/EventDispatcher.test.cpp
framework/core/input/InputMapper.test.cpp
framework/core/components/Entity.test.cpp
framework/core/components/EntityManager.test.cpp
framework/core/components/PositionAttribute.test.cpp
)
add_executable(framework-tests
${FRAMEWORK_TEST_SOURCE_LIST}
)
set_target_properties(framework-tests PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/artifacts/tests"
)
target_compile_definitions(framework-tests PUBLIC -DTESTING)
target_include_directories(framework-tests PRIVATE ${Catch2_INCLUDE_DIRS})
# Link the test executable with libframework_static source code and Catch2
target_link_libraries(framework-tests PRIVATE
framework-core_test
catch2-with-main
FakeIt::FakeIt-catch
)
# Extras: Catch2 Ctest integration. @{
# add catch2's ctest CMake module and register the tests defined by framework-tests
# using catch_discover_tests
list(APPEND CMAKE_MODULE_PATH ${Catch2_SOURCE_DIR}/extras)
include(CTest)
include(Catch)
catch_discover_tests(framework-tests)
# @}
# vim: ft=cmake sw=4 ts=4 noet sts=4 foldmethod=marker foldmarker=@{,@}