From 5453f173cabf6bca00d38a5c74ff78440f1d45dc Mon Sep 17 00:00:00 2001 From: Tobias Schmidl Date: Sun, 13 Aug 2023 08:34:22 +0200 Subject: [PATCH] Added first compiling version --- CMakeLists.txt | 80 ++++++++++++++++++++++++++++++++ cmake/version-from-git.cmake | 35 ++++++++++++++ documentation/CMakeLists.txt | 38 +++++++++++++++ documentation/pages/about.dox | 8 ++++ include/glimpses/glimpses.hpp | 25 ++++++++++ source/glimpses.cpp | 9 ++++ standalone/CMakeLists.txt | 33 +++++++++++++ standalone/source/main.cpp | 16 +++++++ test/CMakeLists.txt | 87 +++++++++++++++++++++++++++++++++++ test/source/glimpses.cpp | 25 ++++++++++ test/source/main.cpp | 14 ++++++ 11 files changed, 370 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 cmake/version-from-git.cmake create mode 100644 documentation/CMakeLists.txt create mode 100644 documentation/pages/about.dox create mode 100644 include/glimpses/glimpses.hpp create mode 100644 source/glimpses.cpp create mode 100644 standalone/CMakeLists.txt create mode 100644 standalone/source/main.cpp create mode 100644 test/CMakeLists.txt create mode 100644 test/source/glimpses.cpp create mode 100644 test/source/main.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..cac0369 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,80 @@ +# SPDX-FileCopyrightText: 2023 Tobias Schmidl +# +# SPDX-License-Identifier: AGPL-3.0-or-later + +cmake_minimum_required(VERSION 3.14...3.22) + +include(cmake/version-from-git.cmake) + +# ---- Project ---- + +project( + Glimpses + VERSION ${VERSION_STRING} + LANGUAGES CXX +) + +# ---- Include guards ---- +if(PROJECT_SOURCE_DIR STREQUAL PROJECT_BINARY_DIR) + message( + FATAL_ERROR + "In-source builds not allowed. Please make a new directory (called a build directory) and run CMake from there." + ) +endif() + +# ---- Add dependencies via CPM ---- +# see https://github.com/TheLartians/CPM.cmake for more info + +include(cmake/CPM.cmake) + +# PackageProject.cmake will be used to make our target installable +CPMAddPackage("gh:TheLartians/PackageProject.cmake@1.8.0") + +# CPMAddPackage( NAME fmt GIT_TAG 9.1.0 GITHUB_REPOSITORY fmtlib/fmt OPTIONS "FMT_INSTALL YES" # create an installable +# target ) + +# ---- Add source files ---- +file( + GLOB_RECURSE + headers + CONFIGURE_DEPENDS + "${CMAKE_CURRENT_SOURCE_DIR}/include/*.h" +) +file( + GLOB_RECURSE + sources + CONFIGURE_DEPENDS + "${CMAKE_CURRENT_SOURCE_DIR}/source/*.cpp" +) + +# ---- Create library ---- + +add_library(${PROJECT_NAME} ${headers} ${sources}) +set_target_properties(${PROJECT_NAME} PROPERTIES CXX_STANDARD 17) + +# being a cross-platform target, we enforce standards conformance on MSVC +target_compile_options(${PROJECT_NAME} PUBLIC "$<$:/permissive->") + +# Link dependencies +target_link_libraries(${PROJECT_NAME} PRIVATE # fmt::fmt +) + +target_include_directories( + ${PROJECT_NAME} PUBLIC $ + $ +) + +# the location where the project's version header will be placed should match the project's regular header paths +string(TOLOWER ${PROJECT_NAME}/version.h VERSION_HEADER_LOCATION) + +packageProject( + NAME ${PROJECT_NAME} + VERSION ${PROJECT_VERSION} + NAMESPACE ${PROJECT_NAME} + BINARY_DIR ${PROJECT_BINARY_DIR} + INCLUDE_DIR ${PROJECT_SOURCE_DIR}/include + INCLUDE_DESTINATION include/${PROJECT_NAME}-${PROJECT_VERSION} + VERSION_HEADER "${VERSION_HEADER_LOCATION}" + COMPATIBILITY SameMajorVersion + DEPENDENCIES "" +) diff --git a/cmake/version-from-git.cmake b/cmake/version-from-git.cmake new file mode 100644 index 0000000..e25c9ba --- /dev/null +++ b/cmake/version-from-git.cmake @@ -0,0 +1,35 @@ +# SPDX-FileCopyrightText: 2023 Tobias Schmidl +# +# SPDX-License-Identifier: AGPL-3.0-or-later + +# ---- retrieve version information from git ---- +set(GIT_DEFAULT_VERSION "0.0.0") +execute_process( + COMMAND git describe --tags --dirty --always + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} + OUTPUT_VARIABLE GIT_DESCRIBE_VERSION_STRING + RESULT_VARIABLE GIT_DESCRIBE_ERROR_CODE + OUTPUT_STRIP_TRAILING_WHITESPACE +) +if(GIT_DESCRIBE_ERROR_CODE EQUAL 0) + string( + REGEX MATCH + "^v?([0-9]+)(\\.([0-9]+))?(\\.([0-9]+))?(\\.([0-9]+))?.*$" + _ + "${GIT_DESCRIBE_VERSION_STRING}" + ) + if(DEFINED CMAKE_MATCH_1) + set(VERSION_STRING "${CMAKE_MATCH_1}") + if(DEFINED CMAKE_MATCH_3) + string(APPEND VERSION_STRING ".${CMAKE_MATCH_3}") + if(DEFINED CMAKE_MATCH_5) + string(APPEND VERSION_STRING ".${CMAKE_MATCH_5}") + if(DEFINED CMAKE_MATCH_7) + string(APPEND VERSION_STRING ".${CMAKE_MATCH_7}") + endif(DEFINED CMAKE_MATCH_7) + endif(DEFINED CMAKE_MATCH_5) + endif(DEFINED CMAKE_MATCH_3) + else(DEFINED CMAKE_MATCH_1) + set(VERSION_STRING "${GIT_DEFAULT_VERSION}") + endif(DEFINED CMAKE_MATCH_1) +endif() diff --git a/documentation/CMakeLists.txt b/documentation/CMakeLists.txt new file mode 100644 index 0000000..4a8b34b --- /dev/null +++ b/documentation/CMakeLists.txt @@ -0,0 +1,38 @@ +# SPDX-FileCopyrightText: 2023 github.com/TheLartians +# +# SPDX-License-Identifier: CC0-1.0 + +cmake_minimum_required(VERSION 3.14...3.22) + +project(GlimpsesDocs) + +# ---- Dependencies ---- + +include(../cmake/CPM.cmake) + +CPMAddPackage("gh:mosra/m.css#a0d292ec311b97fefd21e93cdefb60f88d19ede6") +CPMAddPackage(NAME Glimpses SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/..) + +# ---- Doxygen variables ---- + +# set Doxyfile variables +set(DOXYGEN_PROJECT_NAME Glimpses) +set(DOXYGEN_PROJECT_VERSION ${Glimpses_VERSION}) +set(DOXYGEN_PROJECT_ROOT "${CMAKE_CURRENT_LIST_DIR}/..") +set(DOXYGEN_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/doxygen") + +configure_file(${CMAKE_CURRENT_LIST_DIR}/Doxyfile ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile) + +configure_file(${CMAKE_CURRENT_LIST_DIR}/conf.py ${CMAKE_CURRENT_BINARY_DIR}/conf.py) + +# cmake-lint: disable=C0113 +add_custom_target( + GenerateDocs + ${CMAKE_COMMAND} + -E + make_directory + "${DOXYGEN_OUTPUT_DIRECTORY}" + COMMAND "${m.css_SOURCE_DIR}/documentation/doxygen.py" "${CMAKE_CURRENT_BINARY_DIR}/conf.py" + COMMAND echo "Docs written to: ${DOXYGEN_OUTPUT_DIRECTORY}" + WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}" +) diff --git a/documentation/pages/about.dox b/documentation/pages/about.dox new file mode 100644 index 0000000..4442c6e --- /dev/null +++ b/documentation/pages/about.dox @@ -0,0 +1,8 @@ +// SPDX-FileCopyrightText: 2023 Tobias Schmidl +// +// SPDX-License-Identifier: AGPL-3.0-or-later + +/** @page about About + @section doc Glimpses Documentation + This is the auto-generated documentation for the Glimpses project. +*/ diff --git a/include/glimpses/glimpses.hpp b/include/glimpses/glimpses.hpp new file mode 100644 index 0000000..7d8fc02 --- /dev/null +++ b/include/glimpses/glimpses.hpp @@ -0,0 +1,25 @@ +// SPDX-FileCopyrightText: 2023 Tobias Schmidl +// +// SPDX-License-Identifier: AGPL-3.0-or-later + +#pragma once + +#include + +namespace glimpses { + +/** + * @brief A class for saying hello in multiple languages + */ +class Glimpses +{ + + public: + /** + * @brief Creates a new glimpses + * @param name the name to greet + */ + Glimpses(); +}; + +} // namespace glimpses diff --git a/source/glimpses.cpp b/source/glimpses.cpp new file mode 100644 index 0000000..80aed49 --- /dev/null +++ b/source/glimpses.cpp @@ -0,0 +1,9 @@ +// SPDX-FileCopyrightText: 2023 Tobias Schmidl +// +// SPDX-License-Identifier: AGPL-3.0-or-later + +#include + +using namespace glimpses; + +Glimpses::Glimpses() {} diff --git a/standalone/CMakeLists.txt b/standalone/CMakeLists.txt new file mode 100644 index 0000000..b6142bc --- /dev/null +++ b/standalone/CMakeLists.txt @@ -0,0 +1,33 @@ +# SPDX-FileCopyrightText: 2023 Tobias Schmidl +# +# SPDX-License-Identifier: AGPL-3.0-or-later + +cmake_minimum_required(VERSION 3.14...3.22) + +project(GlimpsesStandalone LANGUAGES CXX) + +# --- Import tools ---- + +include(../cmake/tools.cmake) + +# ---- Dependencies ---- + +include(../cmake/CPM.cmake) + +CPMAddPackage(NAME Glimpses SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/..) + +# ---- Create standalone executable ---- + +file( + GLOB + sources + CONFIGURE_DEPENDS + ${CMAKE_CURRENT_SOURCE_DIR}/source/*.cpp +) + +add_executable(${PROJECT_NAME} ${sources}) + +set_target_properties(${PROJECT_NAME} PROPERTIES CXX_STANDARD 17 OUTPUT_NAME "Glimpses") + +target_link_libraries(${PROJECT_NAME} Glimpses::Glimpses # Boost::System +) diff --git a/standalone/source/main.cpp b/standalone/source/main.cpp new file mode 100644 index 0000000..2e7cfde --- /dev/null +++ b/standalone/source/main.cpp @@ -0,0 +1,16 @@ +// SPDX-FileCopyrightText: 2023 Tobias Schmidl +// +// SPDX-License-Identifier: AGPL-3.0-or-later + +#include +#include +#include + +int +main(int, char**) +{ + glimpses::Glimpses glimpses; + std::cout << "Hello, World." << std::endl; + + return 0; +} diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 0000000..06da936 --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1,87 @@ +# SPDX-FileCopyrightText: 2023 Tobias Schmidl +# +# SPDX-License-Identifier: AGPL-3.0-or-later + +cmake_minimum_required(VERSION 3.14...3.22) + +project(GlimpsesTests LANGUAGES CXX) + +# ---- Options ---- + +option(TEST_INSTALLED_VERSION "Test the version found by find_package" OFF) + +# --- Import tools ---- + +include(../cmake/tools.cmake) + +# ---- Dependencies ---- + +include(../cmake/CPM.cmake) + +# CPMAddPackage("gh:doctest/doctest@2.4.9") +CPMAddPackage("gh:TheLartians/Format.cmake@1.7.3") + +if(TEST_INSTALLED_VERSION) + find_package(Glimpses REQUIRED) +else() + CPMAddPackage(NAME Glimpses SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/..) +endif() + +# ---- Create binary ---- + +file( + GLOB + sources + CONFIGURE_DEPENDS + ${CMAKE_CURRENT_SOURCE_DIR}/source/*.cpp +) +add_executable(${PROJECT_NAME} ${sources}) +target_link_libraries( + ${PROJECT_NAME} + # doctest::doctest + Glimpses::Glimpses +) +set_target_properties(${PROJECT_NAME} PROPERTIES CXX_STANDARD 17) + +# enable compiler warnings +if(NOT TEST_INSTALLED_VERSION) + if(CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU") + target_compile_options( + Glimpses + PUBLIC -Wall + -Wpedantic + -Wextra + -Werror + ) + elseif(MSVC) + target_compile_options(Glimpses PUBLIC /W4 /WX) + target_compile_definitions(${PROJECT_NAME} PUBLIC DOCTEST_CONFIG_USE_STD_HEADERS) + endif() +endif() + +# ---- Add GlimpsesTests ---- + +enable_testing() + +# Note: doctest and similar testing frameworks can automatically configure CMake tests. For other testing frameworks add +# the tests target instead: add_test(NAME ${PROJECT_NAME} COMMAND ${PROJECT_NAME}) + +# include(${doctest_SOURCE_DIR}/scripts/cmake/doctest.cmake) doctest_discover_tests(${PROJECT_NAME}) + +# ---- code coverage ---- + +if(ENABLE_TEST_COVERAGE) + target_compile_options( + Glimpses + PUBLIC -O0 + -g + -fprofile-arcs + -ftest-coverage + ) + target_link_options( + Glimpses + PUBLIC + -fprofile-arcs + -ftest-coverage + ) +endif() diff --git a/test/source/glimpses.cpp b/test/source/glimpses.cpp new file mode 100644 index 0000000..82e9090 --- /dev/null +++ b/test/source/glimpses.cpp @@ -0,0 +1,25 @@ +// SPDX-FileCopyrightText: 2023 Tobias Schmidl +// +// SPDX-License-Identifier: AGPL-3.0-or-later + +#include +#include + +#include +/* +TEST_CASE("Glimpses") { + using namespace greeter; + + Glimpses greeter("Tests"); + + CHECK(greeter.greet(LanguageCode::EN) == "Hello, Tests!"); + CHECK(greeter.greet(LanguageCode::DE) == "Hallo Tests!"); + CHECK(greeter.greet(LanguageCode::ES) == "¡Hola Tests!"); + CHECK(greeter.greet(LanguageCode::FR) == "Bonjour Tests!"); +} + +TEST_CASE("Glimpses version") { + static_assert(std::string_view(GREETER_VERSION) == std::string_view("1.0")); + CHECK(std::string(GREETER_VERSION) == std::string("1.0")); +} +*/ diff --git a/test/source/main.cpp b/test/source/main.cpp new file mode 100644 index 0000000..ef34b77 --- /dev/null +++ b/test/source/main.cpp @@ -0,0 +1,14 @@ +// SPDX-FileCopyrightText: 2023 Tobias Schmidl +// +// SPDX-License-Identifier: AGPL-3.0-or-later +#if 0 +#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN + +#include +#endif + +int +main() +{ + return 0; +}