diff --git a/.clang-format b/.clang-format index d2d504f..e9d54bc 100644 --- a/.clang-format +++ b/.clang-format @@ -1,16 +1,24 @@ +AccessModifierOffset: '-4' AllowShortFunctionsOnASingleLine: Inline +AlignConsecutiveAssignments: true +AlignConsecutiveDeclarations: true BasedOnStyle: Google BinPackArguments: false BinPackParameters: false BreakConstructorInitializers: BeforeComma ConstructorInitializerAllOnOneLineOrOnePerLine: false ColumnLimit: 120 -AccessModifierOffset: '-4' IndentWidth: '4' +IndentPPDirectives: AfterHash +IndentCaseLabels: false +IncludeBlocks: Regroup +IncludeCategories: + - Regex: '^<.*\..*>' + Priority: 2 + - Regex: '^<.*>' + Priority: 1 + - Regex: '".*"' + Priority: 3 Language: Cpp TabWidth: '4' UseTab: ForIndentation -IndentPPDirectives: AfterHash -AlignConsecutiveAssignments: true -IndentCaseLabels: false -IncludeBlocks: Preserve diff --git a/.cmake-format.yaml b/.cmake-format.yaml new file mode 100644 index 0000000..7cfa2a8 --- /dev/null +++ b/.cmake-format.yaml @@ -0,0 +1,8 @@ +bullet_char: '*' +enum_char: . +line_width: 120 +max_pargs_hwrap: 3 +separate_ctrl_name_with_space: false +separate_fn_name_with_space: false +tab_size: 4 +columnize: true \ No newline at end of file diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f318198..269d6d2 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -9,16 +9,14 @@ jobs: steps: - uses: actions/checkout@v2 - - name: Install Conan - run: pip install conan - - name: Generate - run: cmake . -B.build - + run: cmake . -B.build -Ax64 -DPROJECT_NAME_BUILD_TESTS=ON + - name: Build run: cmake --build .build/ - name: Test run: | cd .build/ - ctest . \ No newline at end of file + ctest . + cd .. \ No newline at end of file diff --git a/.gitignore b/.gitignore index d74809a..15ba08b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .build/ -.vscode/ \ No newline at end of file +.vscode/ +.deps/ \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 35a2b98..e671468 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,16 +1,23 @@ cmake_minimum_required(VERSION 3.16) -set(PROJECT_NAME "project_name") -project(${PROJECT_NAME} C CXX) + +project("project_name" C CXX) + +include("cmake/prevent_in_source_builds.cmake") +include("cmake/standard_project_setup.cmake") include("cmake/compiler_features.cmake") include("cmake/compiler_warnings.cmake") -include("cmake/conan.cmake") -set_property(GLOBAL PROPERTY CXX_STANDARD 20) -set_property(GLOBAL PROPERTY USE_FOLDERS ON) -set_property(GLOBAL PROPERTY PREDEFINED_TARGETS_FOLDER "predefined") +add_subdirectory("app") -include(GoogleTest) -enable_testing() +set(PROJECT_NAME_FOLDER + ${PROJECT_NAME} + CACHE STRING "The folder that ${PROJECT_NAME} projects will be placed in.") -add_subdirectory("app") \ No newline at end of file +option(PROJECT_NAME_BUILD_TESTS "If unit tests should be build." OFF) +if(PROJECT_NAME_BUILD_TESTS) + enable_testing() + include(GoogleTest) + include("thirdparty/googletest.cmake") + add_subdirectory("test") +endif() diff --git a/README.md b/README.md index c68b6f1..1a9c187 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # cmake-project-template -Uses CMake for generating and Conan for package management. +Uses CMake for generating. ![Build](https://github.com/Husenap/cmake-project-template/workflows/Build/badge.svg) diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index d3fa69e..20d047a 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -1,21 +1,12 @@ set(target_name "app") -set(src_files - "src/main.cpp") +set(src_files "src/main.cpp") # Project add_executable(${target_name} ${src_files}) +target_link_libraries(${target_name} compiler_features compiler_warnings) + source_group("src" FILES "src/main.cpp") set_target_properties(${target_name} PROPERTIES FOLDER ${PROJECT_NAME}) -set_target_properties(${target_name} PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY ${CMAKE_HOME_DIRECTORY}/bin) - -# Tests -add_executable(${target_name}_test "test/test.cpp") - -target_link_libraries(${target_name}_test CONAN_PKG::gtest) - -set_target_properties(${target_name}_test PROPERTIES FOLDER ${PROJECT_NAME}) - -gtest_discover_tests(${target_name}_test) \ No newline at end of file diff --git a/app/src/main.cpp b/app/src/main.cpp index e454e6d..b38001e 100644 --- a/app/src/main.cpp +++ b/app/src/main.cpp @@ -1,7 +1,7 @@ #include int main() { - std::cout << "Hello World!" << std::endl; + std::cout << "This will become something epic!" << std::endl; return 0; } \ No newline at end of file diff --git a/bin/.gitkeep b/bin/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/cmake/compiler_features.cmake b/cmake/compiler_features.cmake index 8fd0d4f..084693d 100644 --- a/cmake/compiler_features.cmake +++ b/cmake/compiler_features.cmake @@ -1,7 +1,3 @@ -if (MSVC) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /std:c++latest") -else() - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++2a") -endif() +add_library(compiler_features INTERFACE) -message("-- CMAKE_CXX_FLAGS: ${CMAKE_CXX_FLAGS}") +target_compile_features(compiler_features INTERFACE cxx_std_20) \ No newline at end of file diff --git a/cmake/compiler_warnings.cmake b/cmake/compiler_warnings.cmake index a39c5ba..7e143f7 100644 --- a/cmake/compiler_warnings.cmake +++ b/cmake/compiler_warnings.cmake @@ -1,7 +1,10 @@ -add_library(compiler-warnings INTERFACE) +add_library(compiler_warnings INTERFACE) + +# cmake-format: off if (MSVC) - target_compile_options(compiler-warnings INTERFACE + target_compile_options(compiler_warnings INTERFACE + /WX # treat warnings as errors /W4 # warning level 4 /permissive- # standards conformance https://docs.microsoft.com/en-us/cpp/build/reference/permissive-standards-conformance?view=vs-2017 /w14242 # 'identfier': conversion from 'type1' to 'type1', possible loss of data @@ -28,7 +31,7 @@ if (MSVC) /wd4201 # nonstandard extension used : nameless struct/union ) else() - target_compile_options(compiler-warnings INTERFACE + target_compile_options(compiler_warnings INTERFACE -Wall # reasonable and standard -Wextra # reasonable and standard -Wshadow # warn the user if a variable declaration shadows one from a parent context @@ -48,4 +51,6 @@ else() -Wuseless-cast # warn if you perform a cast to the same type -Wdouble-promotion # warn if float is implicit promoted to double -Wformat=2) # warn on security issues around functions that format output (ie printf) -endif() \ No newline at end of file +endif() + +# cmake-format: on diff --git a/cmake/conan.cmake b/cmake/conan.cmake deleted file mode 100644 index 0aa674f..0000000 --- a/cmake/conan.cmake +++ /dev/null @@ -1,10 +0,0 @@ -if(NOT EXISTS "${CMAKE_BINARY_DIR}/conan.cmake") - message(STATUS "Downloading conan.cmake from https://github.com/conan-io/cmake-conan") - file(DOWNLOAD "https://raw.githubusercontent.com/conan-io/cmake-conan/master/conan.cmake" - "${CMAKE_BINARY_DIR}/conan.cmake") -endif() - -include(${CMAKE_BINARY_DIR}/conan.cmake) - -conan_cmake_run(CONANFILE conanfile.txt - BASIC_SETUP CMAKE_TARGETS) \ No newline at end of file diff --git a/cmake/prevent_in_source_builds.cmake b/cmake/prevent_in_source_builds.cmake new file mode 100644 index 0000000..5486a05 --- /dev/null +++ b/cmake/prevent_in_source_builds.cmake @@ -0,0 +1,10 @@ +get_filename_component(srcdir "${CMAKE_SOURCE_DIR}" REALPATH) +get_filename_component(bindir "${CMAKE_BINARY_DIR}" REALPATH) + +if("${srcdir}" STREQUAL "${bindir}") + message("=========================================================") + message("Warning: in-source builds are disabled!") + message("Please create a separate build folder using the -B flag") + message("=========================================================") + message(FATAL_ERROR "Quitting configuration!") +endif() \ No newline at end of file diff --git a/cmake/standard_project_setup.cmake b/cmake/standard_project_setup.cmake new file mode 100644 index 0000000..4cd6fae --- /dev/null +++ b/cmake/standard_project_setup.cmake @@ -0,0 +1,2 @@ +set_property(GLOBAL PROPERTY USE_FOLDERS ON) +set_property(GLOBAL PROPERTY PREDEFINED_TARGETS_FOLDER "predefined") \ No newline at end of file diff --git a/conanfile.txt b/conanfile.txt deleted file mode 100644 index 0fd4f25..0000000 --- a/conanfile.txt +++ /dev/null @@ -1,5 +0,0 @@ -[requires] -gtest/1.10.0 - -[generators] -cmake \ No newline at end of file diff --git a/gen.bat b/gen.bat deleted file mode 100644 index 1d81454..0000000 --- a/gen.bat +++ /dev/null @@ -1 +0,0 @@ -cmake . -B.build -Ax64 \ No newline at end of file diff --git a/scripts/build.bat b/scripts/build.bat new file mode 100644 index 0000000..78a7f6c --- /dev/null +++ b/scripts/build.bat @@ -0,0 +1 @@ +cmake --build .build/ \ No newline at end of file diff --git a/scripts/gen.bat b/scripts/gen.bat new file mode 100644 index 0000000..5a4e7a6 --- /dev/null +++ b/scripts/gen.bat @@ -0,0 +1 @@ +cmake . -B.build -Ax64 -DPROJECT_NAME_BUILD_TESTS=OFF \ No newline at end of file diff --git a/scripts/open.bat b/scripts/open.bat new file mode 100644 index 0000000..aabb75c --- /dev/null +++ b/scripts/open.bat @@ -0,0 +1 @@ +cmake --open .build/ \ No newline at end of file diff --git a/scripts/test.bat b/scripts/test.bat new file mode 100644 index 0000000..90f1255 --- /dev/null +++ b/scripts/test.bat @@ -0,0 +1,7 @@ +@echo off + +cmake . -B.build -Ax64 -DPROJECT_NAME_BUILD_TESTS=ON +cmake --build .build/ +cd .build/ +ctest . +cd .. \ No newline at end of file diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 0000000..140b7b1 --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1,15 @@ +set(target_name "app_test") + +set(src_files "src/unit.cpp") + +add_executable(${target_name} ${src_files}) + +target_link_libraries( + ${target_name} + gtest + gmock + gmock_main) + +set_target_properties(${target_name} PROPERTIES FOLDER ${PROJECT_NAME}/test) + +gtest_discover_tests(${target_name}) diff --git a/app/test/test.cpp b/test/src/unit.cpp similarity index 100% rename from app/test/test.cpp rename to test/src/unit.cpp diff --git a/thirdparty/googletest.cmake b/thirdparty/googletest.cmake new file mode 100644 index 0000000..b7ac564 --- /dev/null +++ b/thirdparty/googletest.cmake @@ -0,0 +1,15 @@ +message("-- External Project: googletest") +include(FetchContent) + +FetchContent_Declare( + googletest + GIT_REPOSITORY https://github.com/google/googletest.git + GIT_TAG a4ab0ab # This includes a fix when using C++20 +) + +FetchContent_MakeAvailable(googletest) + +set_target_properties("gtest" PROPERTIES FOLDER "thirdparty/googletest") +set_target_properties("gmock" PROPERTIES FOLDER "thirdparty/googletest") +set_target_properties("gtest_main" PROPERTIES FOLDER "thirdparty/googletest") +set_target_properties("gmock_main" PROPERTIES FOLDER "thirdparty/googletest")