From 9822b034f796807d06b24a4bc152f6ad3b26e1d5 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Tue, 4 Apr 2023 15:48:04 +0000 Subject: [PATCH] templates: add c++-cmake --- templates/c++-cmake/.clang-format | 23 ++++ templates/c++-cmake/.envrc | 10 ++ templates/c++-cmake/.gitignore | 7 ++ templates/c++-cmake/.woodpecker/check.yml | 26 ++++ templates/c++-cmake/CMakeLists.txt | 18 +++ templates/c++-cmake/flake.nix | 112 ++++++++++++++++++ templates/c++-cmake/src/CMakeLists.txt | 4 + templates/c++-cmake/src/main.cc | 5 + templates/c++-cmake/tests/CMakeLists.txt | 1 + templates/c++-cmake/tests/unit/CMakeLists.txt | 15 +++ templates/c++-cmake/tests/unit/dummy_test.cc | 5 + templates/default.nix | 5 +- 12 files changed, 230 insertions(+), 1 deletion(-) create mode 100644 templates/c++-cmake/.clang-format create mode 100644 templates/c++-cmake/.envrc create mode 100644 templates/c++-cmake/.gitignore create mode 100644 templates/c++-cmake/.woodpecker/check.yml create mode 100644 templates/c++-cmake/CMakeLists.txt create mode 100644 templates/c++-cmake/flake.nix create mode 100644 templates/c++-cmake/src/CMakeLists.txt create mode 100644 templates/c++-cmake/src/main.cc create mode 100644 templates/c++-cmake/tests/CMakeLists.txt create mode 100644 templates/c++-cmake/tests/unit/CMakeLists.txt create mode 100644 templates/c++-cmake/tests/unit/dummy_test.cc diff --git a/templates/c++-cmake/.clang-format b/templates/c++-cmake/.clang-format new file mode 100644 index 0000000..19c58aa --- /dev/null +++ b/templates/c++-cmake/.clang-format @@ -0,0 +1,23 @@ +# vim: ft=yaml +--- +BasedOnStyle: LLVM +IndentWidth: 4 +--- +Language: Cpp +# Force pointers to the type for C++. +DerivePointerAlignment: false +PointerAlignment: Left + +# Short functions should not be on a single line, unless empty +AllowShortFunctionsOnASingleLine: Empty + +# Make them level +AccessModifierOffset: -4 + +# It makes more sense this way +BreakBeforeBinaryOperators: All +BreakBeforeTernaryOperators: true + +# Aesthetic +AlignOperands: AlignAfterOperator +--- diff --git a/templates/c++-cmake/.envrc b/templates/c++-cmake/.envrc new file mode 100644 index 0000000..95ed6fb --- /dev/null +++ b/templates/c++-cmake/.envrc @@ -0,0 +1,10 @@ +if ! has nix_direnv_version || ! nix_direnv_version 2.2.1; then + source_url "https://raw.githubusercontent.com/nix-community/nix-direnv/2.2.1/direnvrc" "sha256-zelF0vLbEl5uaqrfIzbgNzJWGmLzCmYAkInj/LNxvKs=" +fi + +use flake + +watch_file ./flake/checks.nix +watch_file ./flake/dev-shells.nix + +eval "$shellHooks" diff --git a/templates/c++-cmake/.gitignore b/templates/c++-cmake/.gitignore new file mode 100644 index 0000000..09ba440 --- /dev/null +++ b/templates/c++-cmake/.gitignore @@ -0,0 +1,7 @@ +# CMake build directories +/build +/build-* + +# Nix generated files +/.pre-commit-config.yaml +/result diff --git a/templates/c++-cmake/.woodpecker/check.yml b/templates/c++-cmake/.woodpecker/check.yml new file mode 100644 index 0000000..c3b00ef --- /dev/null +++ b/templates/c++-cmake/.woodpecker/check.yml @@ -0,0 +1,26 @@ +labels: + type: exec + +pipeline: +- name: nix flake check + image: bash + commands: + - nix flake check + +- name: notifiy + image: bash + secrets: + - source: matrix_homeserver + target: address + - source: matrix_roomid + target: room + - source: matrix_username + target: user + - source: matrix_password + target: pass + commands: + - nix run '.#matrix-notifier' + when: + status: + - failure + - success diff --git a/templates/c++-cmake/CMakeLists.txt b/templates/c++-cmake/CMakeLists.txt new file mode 100644 index 0000000..122d8b3 --- /dev/null +++ b/templates/c++-cmake/CMakeLists.txt @@ -0,0 +1,18 @@ +cmake_minimum_required(VERSION 3.10) +project(project VERSION 0.0.0 LANGUAGES CXX) +enable_testing() + +add_library(common_options INTERFACE) +target_compile_features(common_options INTERFACE + cxx_std_20 +) +target_compile_options(common_options INTERFACE + -Wall + -Wextra +) +target_include_directories(common_options INTERFACE + src +) + +add_subdirectory(src) +add_subdirectory(tests) diff --git a/templates/c++-cmake/flake.nix b/templates/c++-cmake/flake.nix new file mode 100644 index 0000000..c7e2794 --- /dev/null +++ b/templates/c++-cmake/flake.nix @@ -0,0 +1,112 @@ +{ + description = "A C++ project"; + + inputs = { + futils = { + type = "github"; + owner = "numtide"; + repo = "flake-utils"; + ref = "main"; + }; + + nixpkgs = { + type = "github"; + owner = "NixOS"; + repo = "nixpkgs"; + ref = "nixos-unstable"; + }; + + pre-commit-hooks = { + type = "github"; + owner = "cachix"; + repo = "pre-commit-hooks.nix"; + ref = "master"; + inputs = { + flake-utils.follows = "futils"; + nixpkgs.follows = "nixpkgs"; + }; + }; + }; + + outputs = { self, futils, nixpkgs, pre-commit-hooks }: + { + overlays = { + default = final: prev: { + project = with final; stdenv.mkDerivation { + pname = "project"; + version = "0.0.0"; + + src = self; + + nativeBuildInputs = with pkgs; [ + cmake + ninja + pkg-config + ]; + + checkInputs = with pkgs; [ + gtest + ]; + + doCheck = true; + + meta = with lib; { + description = "A C++ project"; + homepage = "https://gitea.belanyi.fr/ambroisie/project"; + license = licenses.mit; + maintainers = with maintainers; [ ambroisie ]; + platforms = platforms.unix; + }; + }; + }; + }; + } // futils.lib.eachDefaultSystem (system: + let + pkgs = import nixpkgs { + inherit system; + overlays = [ + self.overlays.default + ]; + }; + + pre-commit = pre-commit-hooks.lib.${system}.run { + src = self; + + hooks = { + nixpkgs-fmt = { + enable = true; + }; + + clang-format = { + enable = true; + }; + }; + }; + in + { + checks = { + inherit (self.packages.${system}) project; + + inherit pre-commit; + }; + + devShells = { + default = pkgs.mkShell { + inputsFrom = with self.packages.${system}; [ + project + ]; + + packages = with pkgs; [ + clang-tools + ]; + + inherit (pre-commit) shellHook; + }; + }; + + packages = futils.lib.flattenTree { + default = pkgs.project; + inherit (pkgs) project; + }; + }); +} diff --git a/templates/c++-cmake/src/CMakeLists.txt b/templates/c++-cmake/src/CMakeLists.txt new file mode 100644 index 0000000..d91cef1 --- /dev/null +++ b/templates/c++-cmake/src/CMakeLists.txt @@ -0,0 +1,4 @@ +add_executable(project main.cc) +target_link_libraries(project PRIVATE common_options) + +install(TARGETS project) diff --git a/templates/c++-cmake/src/main.cc b/templates/c++-cmake/src/main.cc new file mode 100644 index 0000000..5eb9e4a --- /dev/null +++ b/templates/c++-cmake/src/main.cc @@ -0,0 +1,5 @@ +#include + +int main() { + std::cout << "Hello World!\n"; +} diff --git a/templates/c++-cmake/tests/CMakeLists.txt b/templates/c++-cmake/tests/CMakeLists.txt new file mode 100644 index 0000000..269aea0 --- /dev/null +++ b/templates/c++-cmake/tests/CMakeLists.txt @@ -0,0 +1 @@ +add_subdirectory(unit) diff --git a/templates/c++-cmake/tests/unit/CMakeLists.txt b/templates/c++-cmake/tests/unit/CMakeLists.txt new file mode 100644 index 0000000..bb94448 --- /dev/null +++ b/templates/c++-cmake/tests/unit/CMakeLists.txt @@ -0,0 +1,15 @@ +find_package(GTest) + +if (${GTest_FOUND}) +include(GoogleTest) + +add_executable(dummy_test dummy_test.cc) +target_link_libraries(dummy_test PRIVATE common_options) + +target_link_libraries(dummy_test PRIVATE + GTest::gtest + GTest::gtest_main +) + +gtest_discover_tests(dummy_test) +endif (${GTest_FOUND}) diff --git a/templates/c++-cmake/tests/unit/dummy_test.cc b/templates/c++-cmake/tests/unit/dummy_test.cc new file mode 100644 index 0000000..4573678 --- /dev/null +++ b/templates/c++-cmake/tests/unit/dummy_test.cc @@ -0,0 +1,5 @@ +#include + +TEST(misc, passing) { + ASSERT_EQ(1, 1); +} diff --git a/templates/default.nix b/templates/default.nix index 679b267..60671f9 100644 --- a/templates/default.nix +++ b/templates/default.nix @@ -1,3 +1,6 @@ { - # Add templates + "c++-cmake" = { + path = ./c++-cmake; + description = "A C++ project using CMake"; + }; }