From f54f23efc2081bafc0a8b11070ff0a0bd5c14f27 Mon Sep 17 00:00:00 2001 From: Volodymyr Kolesnykov Date: Sat, 16 Nov 2024 23:35:44 +0200 Subject: [PATCH] ci: add more checks (#40) * ci: add more checks * ci: add clang-tidy workflow * style: fix clang-tidy warnings --- .github/workflows/ci.yml | 75 ++++++++++++++++--- .github/workflows/clang-tidy.yml | 43 +++++++++++ CMakeLists.txt | 34 +++++++++ CMakePresets.json | 120 +++++++++++++++++++++++++++++++ src/CMakeLists.txt | 4 ++ src/log_record_exporter.cpp | 1 + src/log_record_exporter.h | 1 + src/recordable.h | 2 +- test/test_logger.cpp | 2 +- 9 files changed, 271 insertions(+), 11 deletions(-) create mode 100644 .github/workflows/clang-tidy.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3ff59d2..080968c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -11,9 +11,70 @@ permissions: contents: read jobs: - build: - name: Build and Test + prepare: + name: Prepare list of configurations runs-on: ubuntu-latest + permissions: + contents: read + outputs: + presets: ${{ steps.set-matrix.outputs.presets }} + steps: + - name: Harden Runner + uses: step-security/harden-runner@91182cccc01eb5e619899d80e4e971d6181294a7 # v2.10.1 + with: + disable-sudo: true + egress-policy: block + allowed-endpoints: > + github.com:443 + + - name: Check out the source code + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + + - name: Set matrix + id: set-matrix + run: echo presets="$(jq '.configurePresets[] | select(.hidden == false) | {name, description}' CMakePresets.json | jq --slurp -c .)" >> "${GITHUB_OUTPUT}" + + build: + needs: prepare + name: Build and Test (${{ matrix.preset.description }}) + runs-on: ubuntu-24.04 + strategy: + fail-fast: false + matrix: + preset: ${{ fromJson(needs.prepare.outputs.presets) }} + permissions: + contents: read + steps: + - name: Harden Runner + uses: step-security/harden-runner@91182cccc01eb5e619899d80e4e971d6181294a7 # v2.10.1 + with: + egress-policy: block + allowed-endpoints: > + api.github.com:443 + azure.archive.ubuntu.com:80 + esm.ubuntu.com:443 + github.com:443 + motd.ubuntu.com:443 + objects.githubusercontent.com:443 + packages.microsoft.com:443 + + - name: Check out code + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + submodules: true + + - name: Install dependencies + uses: ./.github/actions/install-dependencies + + - name: Build and test + run: | + cmake --preset ${{ matrix.preset.name }} + cmake --build --preset ${{ matrix.preset.name }} -j $(nproc) + ctest --preset ${{ matrix.preset.name }} + + smoke: + name: Smoke test + runs-on: ubuntu-24.04 permissions: contents: read steps: @@ -27,6 +88,7 @@ jobs: esm.ubuntu.com:443 github.com:443 motd.ubuntu.com:443 + objects.githubusercontent.com:443 packages.microsoft.com:443 - name: Check out code @@ -35,16 +97,11 @@ jobs: - name: Install dependencies uses: ./.github/actions/install-dependencies - - name: Build + - name: Build and install run: | cmake -B build cmake --build build - - - name: Test - run: ctest --test-dir build - - - name: Install - run: sudo cmake --install build + sudo cmake --install build - name: Smoke test run: | diff --git a/.github/workflows/clang-tidy.yml b/.github/workflows/clang-tidy.yml new file mode 100644 index 0000000..97fb24e --- /dev/null +++ b/.github/workflows/clang-tidy.yml @@ -0,0 +1,43 @@ +name: clang-tidy + +on: + push: + branches: + - master + pull_request: + workflow_dispatch: + +permissions: + contents: read + +jobs: + clang-tidy: + name: Run clang-tidy + runs-on: ubuntu-latest + permissions: + contents: read + steps: + - name: Harden Runner + uses: step-security/harden-runner@91182cccc01eb5e619899d80e4e971d6181294a7 # v2.10.1 + with: + egress-policy: block + allowed-endpoints: > + api.github.com:443 + azure.archive.ubuntu.com:80 + esm.ubuntu.com:443 + github.com:443 + motd.ubuntu.com:443 + objects.githubusercontent.com:443 + packages.microsoft.com:443 + + - name: Check out the source code + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + + - name: Install dependencies + uses: ./.github/actions/install-dependencies + + - name: Configure + run: cmake -B build -DCMAKE_EXPORT_COMPILE_COMMANDS=ON + + - name: Run clang-tidy + run: clang-tidy -p build $(jq -r '.[].file' build/compile_commands.json) --warnings-as-errors='*' diff --git a/CMakeLists.txt b/CMakeLists.txt index 05175e7..1ec763c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,8 +11,42 @@ project( ) set(CMAKE_VERBOSE_MAKEFILE ON) +option(BUILD_SHARED_LIBS "Build shared libraries" OFF) option(WITH_TESTING "Whether to enable tests" ON) option(INSTALL_SYSLOG_EXPORTER "Whether to install the syslog exporter" ON) +option(ENABLE_MAINTAINER_MODE "Enable maintainer mode" OFF) + +if(CMAKE_CONFIGURATION_TYPES) + list(APPEND CMAKE_CONFIGURATION_TYPES "Coverage" "ASAN" "LSAN" "TSAN" "UBSAN") +endif() + +string(TOLOWER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE_LOWER) +string(TOLOWER "${CMAKE_CONFIGURATION_TYPES}" CMAKE_CONFIGURATION_TYPES_LOWER) + +string(REGEX MATCH "Clang" CMAKE_COMPILER_IS_CLANG "${CMAKE_CXX_COMPILER_ID}") +string(REGEX MATCH "GNU" CMAKE_COMPILER_IS_GNU "${CMAKE_CXX_COMPILER_ID}") + +if(CMAKE_COMPILER_IS_GNU OR CMAKE_COMPILER_IS_CLANG) + set(CMAKE_CXX_FLAGS_ASAN "-O1 -g -fsanitize=address -fno-omit-frame-pointer -fno-optimize-sibling-calls") + set(CMAKE_CXX_FLAGS_TSAN "-O1 -g -fsanitize=thread -fno-omit-frame-pointer") + set(CMAKE_CXX_FLAGS_LSAN "-O1 -g -fsanitize=leak -fno-omit-frame-pointer -fno-optimize-sibling-calls") + + if(CMAKE_COMPILER_IS_GNU) + set(CMAKE_CXX_FLAGS_COVERAGE "-Og -g --coverage -fprofile-abs-path") + set(CMAKE_CXX_FLAGS_UBSAN "-O1 -g -fsanitize=undefined -fsanitize=float-divide-by-zero -fno-omit-frame-pointer") + elseif(CMAKE_COMPILER_IS_CLANG) + set(CMAKE_CXX_FLAGS_COVERAGE "-O1 -g --coverage") + set(CMAKE_CXX_FLAGS_UBSAN "-O1 -g -fsanitize=undefined -fsanitize=float-divide-by-zero -fsanitize=integer -fsanitize=implicit-conversion -fsanitize=local-bounds -fsanitize=nullability -fno-omit-frame-pointer") + endif() +endif() + +if(ENABLE_MAINTAINER_MODE) + if(CMAKE_COMPILER_IS_CLANG) + set(CMAKE_CXX_FLAGS_MM -Weverything -Werror -Wno-c++98-compat -Wno-c++98-compat-pedantic -Wno-missing-prototypes -Wno-padded -pedantic -Wno-global-constructors -Wno-exit-time-destructors) + elseif(CMAKE_COMPILER_IS_GNU) + set(CMAKE_CXX_FLAGS_MM -Wall -Wextra -Werror -pedantic) + endif() +endif() include(FetchContent) diff --git a/CMakePresets.json b/CMakePresets.json index 3612559..44ee57d 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -44,11 +44,61 @@ "CMAKE_BUILD_TYPE": "Release" } }, + { + "name": "mm-clang", + "description": "Maintainer mode build with clang", + "inherits": "base", + "hidden": false, + "cacheVariables": { + "ENABLE_MAINTAINER_MODE": "ON", + "CMAKE_CXX_COMPILER": "clang++" + } + }, + { + "name": "mm-gcc", + "description": "Maintainer mode build with gcc", + "inherits": "base", + "hidden": false, + "cacheVariables": { + "ENABLE_MAINTAINER_MODE": "ON", + "CMAKE_CXX_COMPILER": "g++" + } + }, { "name": "debug-vcpkg", "description": "Debug + vcpkg", "inherits": "base-vcpkg", "hidden": false + }, + { + "name": "asan", + "description": "AddressSanitizer build", + "inherits": "base", + "hidden": false, + "cacheVariables": { + "CMAKE_CXX_COMPILER": "clang++", + "CMAKE_BUILD_TYPE": "ASAN" + } + }, + { + "name": "tsan", + "description": "ThreadSanitizer build", + "inherits": "base", + "hidden": false, + "cacheVariables": { + "CMAKE_CXX_COMPILER": "clang++", + "CMAKE_BUILD_TYPE": "TSAN" + } + }, + { + "name": "ubsan", + "description": "UndefinedBehaviorSanitizer build", + "inherits": "base", + "hidden": false, + "cacheVariables": { + "CMAKE_CXX_COMPILER": "clang++", + "CMAKE_BUILD_TYPE": "UBSAN" + } } ], "buildPresets": [ @@ -78,12 +128,47 @@ "hidden": false, "configurePreset": "release" }, + { + "name": "mm-clang", + "description": "Maintainer mode build with clang", + "inherits": "base", + "hidden": false, + "configurePreset": "mm-clang" + }, + { + "name": "mm-gcc", + "description": "Maintainer mode build with gcc", + "inherits": "base", + "hidden": false, + "configurePreset": "mm-gcc" + }, { "name": "debug-vcpkg", "description": "Debug + vcpkg", "inherits": "base", "hidden": false, "configurePreset": "debug-vcpkg" + }, + { + "name": "asan", + "description": "AddressSanitizer build", + "inherits": "base", + "hidden": false, + "configurePreset": "asan" + }, + { + "name": "tsan", + "description": "ThreadSanitizer build", + "inherits": "base", + "hidden": false, + "configurePreset": "tsan" + }, + { + "name": "ubsan", + "description": "UndefinedBehaviorSanitizer build", + "inherits": "base", + "hidden": false, + "configurePreset": "ubsan" } ], "testPresets": [ @@ -116,12 +201,47 @@ "hidden": false, "configurePreset": "release" }, + { + "name": "mm-clang", + "description": "Maintainer mode build with clang", + "inherits": "base", + "hidden": false, + "configurePreset": "mm-clang" + }, + { + "name": "mm-gcc", + "description": "Maintainer mode build with gcc", + "inherits": "base", + "hidden": false, + "configurePreset": "mm-gcc" + }, { "name": "debug-vcpkg", "description": "Debug + vcpkg", "inherits": "base", "hidden": false, "configurePreset": "debug-vcpkg" + }, + { + "name": "asan", + "description": "AddressSanitizer build", + "inherits": "base", + "hidden": false, + "configurePreset": "asan" + }, + { + "name": "tsan", + "description": "ThreadSanitizer build", + "inherits": "base", + "hidden": false, + "configurePreset": "tsan" + }, + { + "name": "ubsan", + "description": "UndefinedBehaviorSanitizer build", + "inherits": "base", + "hidden": false, + "configurePreset": "ubsan" } ] } diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 577dfa1..b41409c 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -37,6 +37,10 @@ if(NOT BUILD_SHARED_LIBS) target_compile_definitions(${PROJECT_NAME} PUBLIC OPENTELEMETRY_EXPORTER_SYSLOG_LOGS_STATIC_DEFINE) endif() +if(ENABLE_MAINTAINER_MODE) + target_compile_options(${PROJECT_NAME} PRIVATE ${CMAKE_CXX_FLAGS_MM}) +endif() + if(INSTALL_SYSLOG_EXPORTER) include(GNUInstallDirs) include(CMakePackageConfigHelpers) diff --git a/src/log_record_exporter.cpp b/src/log_record_exporter.cpp index b49f24d..a4814df 100644 --- a/src/log_record_exporter.cpp +++ b/src/log_record_exporter.cpp @@ -11,6 +11,7 @@ namespace wwa::opentelemetry::exporter::logs { +// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) std::shared_ptr SyslogLogRecordExporter::syslog{nullptr}; SyslogLogRecordExporter::SyslogLogRecordExporter( diff --git a/src/log_record_exporter.h b/src/log_record_exporter.h index 6612db2..333ff5f 100644 --- a/src/log_record_exporter.h +++ b/src/log_record_exporter.h @@ -41,6 +41,7 @@ class SyslogLogRecordExporter final : public ::opentelemetry::sdk::logs::LogReco private: std::atomic is_shutdown{false}; + // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) static std::shared_ptr syslog; static void process_record(const Recordable* record); diff --git a/src/recordable.h b/src/recordable.h index 18e776e..bc057d5 100644 --- a/src/recordable.h +++ b/src/recordable.h @@ -80,7 +80,7 @@ class Recordable final : public ::opentelemetry::sdk::logs::Recordable { /** * Set Resource of this log - * @param Resource the resource to set + * @param resource the resource to set */ void SetResource(const ::opentelemetry::sdk::resource::Resource& resource) noexcept override; diff --git a/test/test_logger.cpp b/test/test_logger.cpp index 29b8970..2071ae0 100644 --- a/test/test_logger.cpp +++ b/test/test_logger.cpp @@ -90,7 +90,7 @@ TEST_F(SyslogExporterTest, AdvancedTest) const opentelemetry::nostd::string_view name = "somelib"; const opentelemetry::nostd::string_view version = "1.0"; const std::vector message{"Part 1", "Part 2"}; - std::int64_t event_id = 12345; + const std::int64_t event_id = 12345; const opentelemetry::nostd::string_view event_name = "event_name"; std::array trace_id_raw = {