Skip to content

Commit

Permalink
Improvements for v0.2.0
Browse files Browse the repository at this point in the history
- PIMPL
- CI
- clang-tidy & format
- coverage
- unit tests
  • Loading branch information
wardru committed Feb 5, 2024
1 parent 3026ff6 commit c577ec4
Show file tree
Hide file tree
Showing 105 changed files with 2,698 additions and 2,821 deletions.
19 changes: 19 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
BasedOnStyle: LLVM
IndentWidth: 8
TabWidth: 8
UseTab: Always
BreakBeforeBraces: Linux
AllowShortIfStatementsOnASingleLine: false
AllowShortFunctionsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AllowShortBlocksOnASingleLine: false
IndentCaseLabels: false
ColumnLimit: 120
AccessModifierOffset: -8
NamespaceIndentation: None
PointerAlignment: Right
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
FixNamespaceComments: false
AlignAfterOpenBracket: Align
PackConstructorInitializers: Never
11 changes: 11 additions & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
Checks:
clang-diagnostic-*,
clang-analyzer-*,
cppcoreguidelines-*,
modernize-*,
-modernize-use-trailing-return-type,
-cppcoreguidelines-non-private-member-variables-in-classes
WarningsAsErrors: '*'
HeaderFilterRegex: '.*'
---
90 changes: 90 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
name: CI Workflow

on: [push, pull_request]

jobs:
format:
runs-on: ubuntu-latest
container:
image: ghcr.io/panduza/pzacxx-build-img:latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Format
run: |
./scripts/format.sh --check
build:
runs-on: ubuntu-latest
container:
image: ghcr.io/panduza/pzacxx-build-img:latest
strategy:
fail-fast: false
matrix:
os: [linux, windows]
compiler: [gcc, clang]
build_type: [debug, release]
lib_type: [shared, static]
exclude:
- os: windows
compiler: clang
steps:
- name: Checkout
uses: actions/checkout@v4

- if: ${{ env.ACT }}
name: Hack container for local development
run: pacman -S --noconfirm nodejs

- name: "cache"
uses: actions/cache@v4
with:
path: ~/.conan2/p
key: conan-cache-${{ matrix.os }}-${{ matrix.compiler }}-${{ matrix.build_type }}-${{ matrix.lib_type }}

- id: build_type
uses: ASzc/change-string-case-action@v6
with:
string: ${{ matrix.build_type }}

- name: "Install dependencies"
run: |
if [ "${{ matrix.lib_type }}" = "static" ]; then
shared="False"
else
shared="True"
fi
conan install -pr:b ./conan_profiles/linux_gcc -pr:h ./conan_profiles/${{ matrix.os }}_${{ matrix.compiler }} --build=missing -s build_type=${{ steps.build_type.outputs.capitalized }} -o shared=$shared .
- name: "Build for ${{ matrix.os }}-${{ matrix.compiler }}-${{ matrix.lib_type }}-${{ matrix.build_type }}"
run: |
if [ "${{ matrix.compiler }}" = "clang" ] && [ "${{ matrix.build_type }}" = "debug" ] && [ "${{ matrix.lib_type }}" = "shared" ]; then
tidy="True"
else
tidy="False"
fi
if [ "${{ matrix.os }}" = "windows" ]; then
cmake --preset ${{ matrix.os }}-${{ matrix.compiler }}-${{ matrix.lib_type }} -DWITH_TIDY=$tidy -DWITH_TESTS=False
else
cmake --preset ${{ matrix.os }}-${{ matrix.compiler }}-${{ matrix.lib_type }} -DWITH_TIDY=$tidy -DWITH_TESTS=True -DWITH_COVERAGE=True
fi
cmake --build --preset ${{ matrix.os }}-${{ matrix.compiler }}-${{ matrix.lib_type }}-${{ matrix.build_type }}
- name: "Test for ${{ matrix.os }}-${{ matrix.compiler }}-${{ matrix.lib_type }}-${{ matrix.build_type }}"
if: ${{ matrix.os == 'Linux' }}
run: |
ctest --preset ${{ matrix.os }}-${{ matrix.compiler }}-${{ matrix.lib_type }}-${{ matrix.build_type }}
- name: "Coverage"
if: ${{ matrix.os == 'linux' && matrix.compiler == 'clang' && matrix.lib_type == 'shared' && matrix.build_type == 'debug' }}
run: |
grcov . -s ./ -t html --llvm -o ./build/coverage/ --binary-path ./build/${{ matrix.os }}-${{ matrix.compiler }}-${{ matrix.lib_type }} --ignore '*test*' --ignore '*examples*' --ignore '*include*' --llvm-path=/usr/bin
- name: "Upload coverage artifact"
if: ${{ matrix.os == 'linux' && matrix.compiler == 'clang' && matrix.lib_type == 'shared' && matrix.build_type == 'debug' }}
uses: actions/upload-artifact@v4
with:
name: coverage-report
path: ./build/coverage/
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
build*
build/*
examples/build*
examples/CMakeUserPresets.json
CMakeUserPresets.json
ConanPresets.json
.vscode
Testing
conan_imports_manifest.txt
*.swp
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# [0.2.0]
## Added
## Changed
## Fixed
## Removed

# [0.1.0]
## Added
- Initial release
- Supports Bench Power Supply (BPS) devices
63 changes: 13 additions & 50 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,71 +1,34 @@
cmake_minimum_required(VERSION 3.25)

project(PZACXX VERSION 0.1.0)
set(LIBRARY_NAME pza-cxx)
include(cmake/version.cmake)
get_version_from_changelog()
project(PZACXX_LIBRARY VERSION ${PZACXX_VERSION} LANGUAGES CXX)

set(LIBRARY_NAME pzacxx)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

include(cmake/cppcheck.cmake)

add_compile_options(-Wall -Wextra)
include(cmake/compiler.cmake)
include(cmake/lib.cmake)

set(SPDLOG_FMT_EXTERNAL 1)
find_package(spdlog REQUIRED)
find_package(nlohmann_json REQUIRED)
find_package(PahoMqttCpp REQUIRED)

if (CMAKE_SYSTEM_NAME MATCHES "Linux")
find_package(GTest REQUIRED)
find_package(cppcheck REQUIRED)
endif()

if (CMAKE_SYSTEM_NAME MATCHES "Windows")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--allow-multiple-definition")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--allow-multiple-definition")
elseif (CMAKE_SYSTEM_NAME MATCHES "Linux" AND NOT BUILD_SHARED_LIBS)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--allow-multiple-definition")
endif()

if (CMAKE_SYSTEM_NAME MATCHES "Windows" AND NOT BUILD_SHARED_LIBS)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static")
endif()

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
add_library(${LIBRARY_NAME})

add_subdirectory(source)
#add_subdirectory(test)

option(BUILD_EXAMPLES "Build examples" OFF)
if(BUILD_EXAMPLES)
option(WITH_EXAMPLES "Build examples" OFF)
if (WITH_EXAMPLES)
add_subdirectory(examples)
endif()

target_include_directories(${LIBRARY_NAME} PUBLIC
${CMAKE_CURRENT_LIST_DIR}
${CMAKE_BINARY_DIR}
)

target_link_libraries(${LIBRARY_NAME}
$<$<BOOL:${BUILD_SHARED_LIBS}>:PahoMqttCpp::paho-mqttpp3>
$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:PahoMqttCpp::paho-mqttpp3-static>
spdlog::spdlog
nlohmann_json::nlohmann_json
)

if (CMAKE_SYSTEM_NAME MATCHES "Windows" AND BUILD_SHARED_LIBS)
message(STATUS "Copying DLLs from ${CMAKE_BINARY_DIR}/${CMAKE_BUILD_TYPE}/bin to ${CMAKE_BINARY_DIR}/bin")
add_custom_command(TARGET ${LIBRARY_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${CMAKE_BINARY_DIR}/${CMAKE_BUILD_TYPE}/bin/*.dll
$<TARGET_FILE_DIR:${LIBRARY_NAME}>
)
option(WITH_TESTS "Build tests" OFF)
if (WITH_TESTS)
enable_testing()
add_subdirectory(test)
endif()

set_target_properties(${LIBRARY_NAME} PROPERTIES
VERSION ${PROJECT_VERSION}
SOVERSION ${PROJECT_VERSION_MAJOR}
)
4 changes: 4 additions & 0 deletions CMakePresets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"version": 6,
"include": ["./ConanPresets.json"]
}
25 changes: 25 additions & 0 deletions cmake/compiler.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
include_guard()

if (CMAKE_SYSTEM_NAME MATCHES "Windows")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--allow-multiple-definition")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--allow-multiple-definition")
elseif (CMAKE_SYSTEM_NAME MATCHES "Linux" AND NOT BUILD_SHARED_LIBS)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--allow-multiple-definition")
endif()

if (CMAKE_SYSTEM_NAME MATCHES "Windows" AND NOT BUILD_SHARED_LIBS)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static")
endif()

option(WITH_TIDY "Enable clang-tidy" OFF)
if (WITH_TIDY)
find_program(CLANG_TIDY_EXECUTABLE clang-tidy REQUIRED)
set(CMAKE_CXX_CLANG_TIDY ${CLANG_TIDY_EXECUTABLE})
set(CMAKE_C_CLANG_TIDY ${CLANG_TIDY_EXECUTABLE})
endif()

option(WITH_COVERAGE "Enable code coverage" OFF)
if (WITH_COVERAGE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --coverage")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --coverage")
endif()
14 changes: 0 additions & 14 deletions cmake/cppcheck.cmake

This file was deleted.

36 changes: 30 additions & 6 deletions cmake/lib.cmake
Original file line number Diff line number Diff line change
@@ -1,7 +1,31 @@
set(CMAKE_DEBUG_POSTFIX -debug)
set_target_properties(${LIBRARY_NAME}
PROPERTIES
VERSION "${LIBRARY_VERSION}"
SOVERSION "${LIBRARY_VERSION_MAJOR}"
DEBUG_POSTFIX ${CMAKE_DEBUG_POSTFIX}
include_guard()

add_library(${LIBRARY_NAME})

target_link_libraries(${LIBRARY_NAME}
$<$<BOOL:${BUILD_SHARED_LIBS}>:PahoMqttCpp::paho-mqttpp3>
$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:PahoMqttCpp::paho-mqttpp3-static>
spdlog::spdlog
nlohmann_json::nlohmann_json
)

target_include_directories(${LIBRARY_NAME} PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)

set_target_properties(${LIBRARY_NAME} PROPERTIES
VERSION ${PROJECT_VERSION}
SOVERSION ${PROJECT_VERSION_MAJOR}
)

set_target_properties(${LIBRARY_NAME} PROPERTIES DEBUG_POSTFIX d)

install(TARGETS ${LIBRARY_NAME}
EXPORT ${PROJECT_NAME}Targets
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
RUNTIME DESTINATION bin
)

install(DIRECTORY include/ DESTINATION include)
27 changes: 27 additions & 0 deletions cmake/version.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
include_guard()

function(get_version_from_changelog)
file(STRINGS CHANGELOG.md FIRST_LINE LIMIT_COUNT 1)

string(REGEX REPLACE "# \\[([0-9]+)\\.([0-9]+)\\.([0-9]+)\\].*" "\\1;\\2;\\3" VERSION_NUMBERS "${FIRST_LINE}")
list(GET VERSION_NUMBERS 0 MAJOR)
list(GET VERSION_NUMBERS 1 MINOR)
list(GET VERSION_NUMBERS 2 PATCH)

set(PZACXX_VERSION_MAJOR ${MAJOR} PARENT_SCOPE)
set(PZACXX_VERSION_MINOR ${MINOR} PARENT_SCOPE)
set(PZACXX_VERSION_PATCH ${PATCH} PARENT_SCOPE)
set(PZACXX_VERSION "${MAJOR}.${MINOR}.${PATCH}" PARENT_SCOPE)

find_package(Git REQUIRED)
execute_process(
COMMAND ${GIT_EXECUTABLE} rev-parse HEAD
RESULT_VARIABLE result
OUTPUT_VARIABLE GITHASH
OUTPUT_STRIP_TRAILING_WHITESPACE
)
if (result)
message(FATAL_ERROR "Failed to get git hash: ${result}")
endif()
set(PZACXX_VERSION_GITHASH ${GITHASH} PARENT_SCOPE)
endfunction()
10 changes: 10 additions & 0 deletions conan_profiles/linux_clang
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[settings]
os=Linux
arch=x86_64
compiler=clang
compiler.version=16
compiler.libcxx=libstdc++11
compiler.cppstd=gnu17

[conf]
tools.build:compiler_executables={'c': 'clang', 'cpp': 'clang++' }
4 changes: 4 additions & 0 deletions conan_profiles/x86_64_Linux → conan_profiles/linux_gcc
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ arch=x86_64
compiler=gcc
compiler.version=13
compiler.libcxx=libstdc++11
compiler.cppstd=gnu17

[conf]
tools.build:compiler_executables={'c': 'gcc', 'cpp': 'g++' }
19 changes: 19 additions & 0 deletions conan_profiles/windows_gcc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[buildenv]
AR=x86_64-w64-mingw32-ar
AS=x86_64-w64-mingw32-as
RANLIB=x86_64-w64-mingw32-ranlib
CC=x86_64-w64-mingw32-gcc
CXX=x86_64-w64-mingw32-g++
STRIP=x86_64-w64-mingw32-strip
RC=x86_64-w64-mingw32-windres

[settings]
os=Windows
arch=x86_64
compiler=gcc
compiler.version=12
compiler.libcxx=libstdc++11
compiler.cppstd=17

[conf]
tools.build:compiler_executables={"c": "x86_64-w64-mingw32-gcc", "cpp": "x86_64-w64-mingw32-g++"}
Loading

0 comments on commit c577ec4

Please sign in to comment.