quartz-warriors/CMakeLists.txt
S David 4eac146a8e
All checks were successful
buildbot/quartz-warriors-linux-podman-builder Build done.
buildbot/quartz-warriors-darwin-macos-builder Build done.
buildbot/quartz-warriors-freebsd-jail-builder Build done.
Fix system Catch2 Detection
2023-12-09 17:06:07 -05:00

211 lines
5.9 KiB
CMake

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 )
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(Threads REQUIRED)
find_package(PkgConfig REQUIRED)
#pkg_check_modules(CATCH2 "catch2-with-main >= 2.13.7")
pkg_check_modules(SDL2 "sdl2 >= 2" REQUIRED)
pkg_check_modules(SDL2_IMG "SDL2_image >= 0.29.0" REQUIRED)
pkg_check_modules(SDL2_GFX "SDL2_gfx >= 1.0.2" REQUIRED)
pkg_check_modules(NLOHMANN_JSON "nlohmann_json >= 3.11.2" REQUIRED)
message(CHECK_START "Detecting system Catch2 installation")
find_package(Catch2 3 QUIET)
if(TARGET Catch2::Catch2)
message(CHECK_PASS "found")
set(USE_SYSTEM_CATCH2 ON)
else()
message(CHECK_FAIL "not found")
endif()
if (SDL2_FOUND)
include_directories(${SDL2_INCLUDE_DIRS})
link_directories(AFTER ${SDL2_IMG_LIBRARY_DIRS})
endif()
if (SDL2_IMG_FOUND)
include_directories(${SDL2_IMG_INCLUDE_DIRS})
link_directories(AFTER ${SDL2_LIBRARY_DIRS})
endif()
if (SDL2_GFX_FOUND)
include_directories(${SDL2_GFX_INCLUDE_DIRS})
link_directories(AFTER ${SDL2_GFX_LIBRARY_DIRS})
endif()
if (NLOHMANN_JSON_FOUND)
include_directories(${NLOHMANN_JSON_INCLUDE_DIRS})
#add_link_options(${NKLOHMANN_JSON_LDFLAGS})
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 20)
endif()
if (TARGET Catch2WithMain)
set_target_properties(Catch2WithMain PROPERTIES CXX_STANDARD 20)
endif()
list(APPEND CMAKE_MODULE_PATH ${Catch2_SOURCE_DIR}/extras)
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)
if (Boost_FOUND)
add_definitions(-DUSE_BOOST_STACKTRACER=1)
add_definitions(-DBOOST_STACKTRACE_USE_ADDR2LINE=1)
include_directories(${Boost_INCLUDE_DIRS})
endif()
endif()
#@}
#@{ QT5 Dependency detection & tool setup
find_package(Qt6
COMPONENTS
Core REQUIRED
Widgets REQUIRED
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}
${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=@{,@}: