diff --git a/CMakeLists.txt b/CMakeLists.txt index 63f8999..0c897af 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,12 +1,72 @@ -cmake_minimum_required(VERSION 3.12) -project() +cmake_minimum_required(VERSION 3.26) + +# Prevent in-source builds +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 -DENABLE_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() + +option(USE_SYSTEM_CATCH2 + "Do not download & compile catch2 library for unit tests" + ON +) + +project( + VERSION 0.1 + LANGUAGES C CXX + # HOMEPAGE_URL + DESCRIPTION "Description" +) + +IF( NOT CMAKE_BUILD_TYPE ) + SET( CMAKE_BUILD_TYPE Debug ) +ENDIF() set(CMAKE_C_STANDARD 17) set(CMAKE_CXX_STANDARD 20) -# Include directories -include_directories(${CMAKE_SOURCE_DIR}/src) -include_directories(${CMAKE_SOURCE_DIR}/include) +# 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. +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) + +if (USE_SYSTEM_CATCH2) + message(CHECK_START "Detecting System Catch2 ") + find_package(Catch2 3 QUIET) + + if(TARGET Catch2::Catch2) + message(CHECK_PASS "found target Catch2::Catch2") + else() + message(CHECK_FAIL "not found") + set(USE_SYSTEM_CATCH2 OFF) + message(STATUS "USE_SYSTEM_CATCH2=OFF") + endif() +endif() + +# enable compile_commands.json generation for clangd +set(CMAKE_EXPORT_COMPILE_COMMANDS On) + +# Initialize FetchContent +include(FetchContent) # Initialize pkgconf #find_package(PkgConfig REQUIRED) @@ -22,21 +82,67 @@ include_directories(${CMAKE_SOURCE_DIR}/include) # PkgConfig::SDL2_IMAGE # PkgConfig::SDL2for_GFX # # if not using PkgConfig:: IMPORTED_TARGET syntax -# # ${SDL2_LIBRARIES} -# #${SDL2_IMG_LIBRARIES} -# # ${SDL2_GFX_LIBRARIES} +# ${SDL2_LIBRARIES} +# ${SDL2_IMG_LIBRARIES} +# ${SDL2_GFX_LIBRARIES} #) -# FetchContent for Catch2 -include(FetchContent) -FetchContent_Declare( - catch2 - GIT_REPOSITORY https://github.com/catchorg/Catch2.git - GIT_TAG v3.4.0 -) -FetchContent_MakeAvailable(catch2) +if (ENABLE_TESTS) + if (NOT USE_SYSTEM_CATCH2) + FetchContent_Declare( + Catch2 + URL https://github.com/catchorg/Catch2/archive/refs/tags/v3.4.0.zip + URL_HASH MD5=c426e77d4ee0055410bc930182959ae5 + ) + FetchContent_MakeAvailable(Catch2) + else() + include_directories(${CATCH2_INCLUDE_DIRS}) + link_directories(${CATCH2_LIBRARY_DIRS}) + endif() + 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) + + 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) +endif() + +# Set output directories for build targets +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) + +# 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(test) +add_subdirectory(app) +if(ENABLE_TESTS) + add_subdirectory(test) +endif() + + +# vim: ts=2 sw=2 noet foldmethod=indent : diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..2f2b7f3 --- /dev/null +++ b/LICENSE @@ -0,0 +1,19 @@ +Copyright (c) 2023 Saul D. Beniquez + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..ffb6c16 --- /dev/null +++ b/README.md @@ -0,0 +1,52 @@ +C++20 CMake Project Template + +This is a simple C++20 project template using CMake for building and Catch2 v3 along with FakeIt for unit testing. The project is set up to fetch Catch2 and FakeIt using CMake's FetchContent command if they are not found on the system. + +Getting Started + +Prerequisites + +- CMake (version 3.21 or higher) +- C++20-compatible compiler + +Building the Project + +1. Clone the repository: + + git clone https://github.com/your-username/your-project.git + +2. Navigate to the project directory: + + cd your-project + +3. Create a build directory: + + mkdir build && cd build + +4. Configure and build the project using CMake: + + cmake .. + make + +Running Unit Tests + +After building the project, you can run the unit tests: + +make test + +This will execute the Catch2 test suite. + +Project Structure + +- src/: Contains the source code for your application. +- test/: Contains unit tests using Catch2 and FakeIt. + +Dependencies + +- Catch2 v3: Test framework for C++ (fetched using FetchContent). +- FakeIt: Mocking framework for C++ (fetched using FetchContent). + +License + +This project is licensed under the MIT License. + diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt new file mode 100644 index 0000000..d5d389c --- /dev/null +++ b/app/CMakeLists.txt @@ -0,0 +1,15 @@ + +add_executable(app main.cpp) + +#target_include_directories(app PRIVATE +# ${CMAKE_SOURCE_DIR}/src/ +#) + +target_link_libraries(app PRIVATE + library +) + +# Manually adds a predefined target as a dependency +#add_dependencies(app copy-assets) + +# vim: ts=2 sw=2 noet foldmethod=indent : diff --git a/app/main.cpp b/app/main.cpp new file mode 100644 index 0000000..6a7efc2 --- /dev/null +++ b/app/main.cpp @@ -0,0 +1,11 @@ + +#include + +int main(int argc, char* argv[], char* envp[]) +{ + std::cout << "main(): hello world
" << std::endl; + return 0; +} + +// clang-format off +// vim: set foldmethod=marker foldmarker=#region,#endregion textwidth=80 ts=8 sts=0 sw=8 noexpandtab ft=cpp.doxygen : diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index cd2b0b3..e029451 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,15 +1,17 @@ # Define the library 'engine' -add_library(library OBJECT +add_library(libraryobj OBJECT library.cpp ) # Define build targets for 'engine', 'engine-static', and 'engine-test' -add_library(library SHARED $) -add_library(library-static STATIC $) -add_library(library-testing $) +add_library(library SHARED $) +add_library(library-static STATIC $) +add_library(library-testing $) # Link the SDL dependencies target_link_libraries(library PUBLIC ${library_dep_libs} ) target_link_libraries(library-static PUBLIC ${library_dep_libs} ) target_link_libraries(library-testing PUBLIC ${library_dep_libs}) + +# vim: ts=2 sw=2 noet foldmethod=indent : diff --git a/src/library.cpp b/src/library.cpp index 79db373..73e251a 100644 --- a/src/library.cpp +++ b/src/library.cpp @@ -1,5 +1,10 @@ -class Engine + + +class Application { - + public: + int Run(int argc, char* argv[], char* env[]) { return 0; } }; + +static Application app; diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 56db3d0..3785692 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -10,3 +10,26 @@ PRIVATE ${library_dep_libs} Catch2::Catch2WithMain ) + +# Set output directories for build targets +set_target_properties(test-runner PROPERTIES + RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/build/tests" +) +# extras: catch2 ctest integration. +# add catch2's ctest cmake module and register the tests defined by mdml-tests +# using catch_discover_tests +include(ctest) +include(catch) +catch_discover_tests(test-runner) + +# bonus: add a custom ctest target to the build system, to run tests in the +# correct directory +add_custom_target(ctest + command ctest -c $configuration --test-dir ./tests --output-on-failure +) +add_dependencies(ctest + test-runner + #copy_assets +) + +# vim: ts=2 sw=2 noet foldmethod=indent :