cmake-cpp-project/CMakeLists.txt

141 lines
3.7 KiB
CMake

cmake_minimum_required(VERSION 3.26)
# Additional paths to search for custom and third-party CMake modules
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
include(BuildProperties)
prevent_in_source_build()
disable_deprecated_features()
# When this package is included as a subproject, there's no need to
# build and run the unit-tests.
# Sets -DENABLE_TESTS to false by default if this is a third-party lib build
# This check must appear before project()
disable_tests_if_subproject()
project(<APPLICATION-NAME>
VERSION 0.1
LANGUAGES C CXX
# HOMEPAGE_URL <URL>
DESCRIPTION "Description"
)
include(CPM)
option(USE_CCACHE
[=[Use ccache compiler cache to speed up builds.
Enabled by default if ccache is found]=]
ON
)
IF( NOT CMAKE_BUILD_TYPE )
SET( CMAKE_BUILD_TYPE Debug )
ENDIF()
set(CMAKE_C_STANDARD 17)
set(CMAKE_CXX_STANDARD 20)
# Disable GNU compiler extensions
set(CMAKE_C_EXTENSIONS OFF)
set(CMAKE_CXX_EXTENSIONS OFF)
# Search for the code caching compiler wrapper, ccache and enable it
# if found. This will speed up repeated builds.
if (USE_CCACHE)
message(CHECK_START "Detecting cacche")
find_program(CCACHE_PATH ccache)
if(DEFINED CCACHE_PATH)
message(CHECK_PASS("found"))
set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ${CCACHE_PATH})
set_property(GLOBAL PROPERTY RULE_LAUNCH_LINK ${CCACHE_PATH})
endif()
list(APPEND CMAKE_MESSAGE_INDENT " ")
message(STATUS "(set -DUSE_CCACHE=Off to disable)")
list(POP_BACK CMAKE_MESSAGE_INDENT)
endif()
# enable compile_commands.json generation for clangd
set(CMAKE_EXPORT_COMPILE_COMMANDS On)
# Initialize FetchContent
include(FetchContent)
# Initialize pkgconf
#find_package(PkgConfig REQUIRED)
# Example: Find SDL2, SDL2_image, and SDL2_gfx using PkgConfig
# add IMPORTED_TARGET to enable fancy PkgConfig::SDL2 syntax
#pkg_check_modules(SDL2 REQUIRED IMPORTED_TARGET SDL2)
#pkg_check_modules(SDL2_IMAGE REQUIRED IMPORTED_TARGET SDL2_image)
#pkg_check_modules(SDL2_GFX REQUIRED IMPORTED_TARGET SDL2_gfx)
#set(engine_DEP_LIBS
# PkgConfig::SDL2
# PkgConfig::SDL2_IMAGE
# PkgConfig::SDL2for_GFX
# # if not using PkgConfig:: IMPORTED_TARGET syntax
# ${SDL2_LIBRARIES}
# ${SDL2_IMG_LIBRARIES}
# ${SDL2_GFX_LIBRARIES}
#)
if (ENABLE_TESTS)
CPMFindPackage(NAME Catch2
GITHUB_REPOSITORY catchorg/Catch2
VERSION 3.4.0
OPTIONS
"CATCH_DEVELOPMENT_BUILD OFF"
"CATCH_BUILD_TESTING OFF"
)
CPMFindPackage(NAME FakeIt
GITHUB_REPOSITORY eranpeer/FakeIt
GIT_TAG 2.4.0
OPTIONS
"ENABLE_TESTING OFF"
)
if (TARGET Catch2)
set_target_properties(Catch2 PROPERTIES
CXX_STANDARD 20
)
elseif (TARGET Catch2WithMain)
set_target_properties(Catch2WithMain PROPERTIES
CXX_STANDARD 20
)
endif()
if (Catch2_ADDED STREQUAL "YES")
list(APPEND CMAKE_MODULE_PATH ${Catch2_SOURCE_DIR}/extras)
else()
list(APPEND CMAKE_MODULE_PATH ${Catch2_DIR})
endif()
endif()
# Set output directories for build targets
set(ARTIFACT_DIR "${CMAKE_BINARY_DIR}/out")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${ARTIFACT_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${ARTIFACT_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${ARTIFACT_DIR}/bin)
# Include directories
include_directories(${CMAKE_SOURCE_DIR}/src)
include_directories(${CMAKE_SOURCE_DIR}/include)
# Example: Add a target to copy application resource files to the build dir
#add_custom_target(copy_assets
#COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_LIST_DIR}/data ${CMAKE_BINARY_DIR}/artifacts/share/data
# COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_LIST_DIR}/data ${CMAKE_BINARY_DIR}/tests/data
#)
# Add subdirectories
add_subdirectory(src)
add_subdirectory(app)
if(ENABLE_TESTS)
add_subdirectory(tests)
endif()
# vim: ts=4 sw=4 noet foldmethod=indent :