Skip to content

Cmake #76

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
Jun 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .cmake-format.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"line_width": 120,
"tab_size": 4,
"max_subgroups_hwrap": 3
}
29 changes: 18 additions & 11 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
FROM ubuntu:24.04

ENV DEBIAN_FRONTEND=noninteractive
ARG USER=ubuntu

RUN apt-get update && apt-get install -y \
# Install packages and clean up after
RUN apt-get update && apt-get install -y --no-install-recommends \
bash-completion \
build-essential \
cmake \
clang \
clangd \
clang-format \
clang-tidy \
clangd \
curl \
doxygen \
gdb \
Expand All @@ -19,19 +21,24 @@ RUN apt-get update && apt-get install -y \
lldb \
llvm \
nano \
software-properties-common \
openssh-client \
pipx \
sudo \
unzip \
wget \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
&& apt-get clean && rm -rf /var/lib/apt/lists/*

# Configure user environment and permissions
RUN passwd -d ${USER} \
&& mkdir -p /home/${USER}/.cache \
&& chown -R ${USER}:${USER} /home/${USER}/.cache \
&& echo "export PROMPT_COMMAND='history -a'" >> /home/${USER}/.bashrc \
&& echo "export HISTFILE=/home/${USER}/.cache/.bash_history" >> /home/${USER}/.bashrc

ARG USERNAME=ubuntu
RUN passwd -d ${USERNAME} \
&& mkdir -p /home/${USERNAME}/.cache && chown -R ${USERNAME}:${USERNAME} /home/${USERNAME}/.cache \
&& SNIPPET="export PROMPT_COMMAND='history -a' && export HISTFILE=/home/${USERNAME}/.cache/.bash_history" \
&& echo "$SNIPPET" >> "/home/${USERNAME}/.bashrc"
USER ${USER}

USER $USERNAME
# Install cmake-format
RUN pipx install cmake-format --include-deps \
&& pipx ensurepath

WORKDIR /workspace
4 changes: 3 additions & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
"twxs.cmake",
"fredericbonnet.cmake-test-adapter",
"llvm-vs-code-extensions.vscode-clangd",
"vadimcn.vscode-lldb"
"vadimcn.vscode-lldb",
"cheshirekow.cmake-format",
"ms-vscode.cpptools"
]
}
},
Expand Down
14 changes: 10 additions & 4 deletions .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,21 @@ jobs:
token: ${{ secrets.CODECOV_TOKEN }}
files: ${{github.workspace}}/build/coverage.info

clang-format:
name: Clang Format
format:
name: Format
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: Run Clang Format
run: clang-format --dry-run --Werror $(find include tests examples -type f -name *.*pp)
- name: C++
run: clang-format --dry-run --Werror $(find . -name *.*pp)

- name: CMake
run: |
pip install cmake-format
cmake-format -i $(find -name CMakeLists.txt)
git diff --exit-code

clang-tidy:
name: Clang Tidy
Expand Down
18 changes: 18 additions & 0 deletions .vscode/cmake-kits.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[
{
"name": "GCC",
"compilers": {
"C": "/usr/bin/gcc",
"CXX": "/usr/bin/g++"
},
"isTrusted": true
},
{
"name": "Clang",
"compilers": {
"C": "/usr/bin/clang",
"CXX": "/usr/bin/clang++"
},
"isTrusted": true
}
]
19 changes: 18 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,24 @@
"version": "0.2.0",
"configurations": [
{
"name": "Launch (LLDB)",
"name": "Launch GDB",
"type": "cppdbg",
"request": "launch",
"program": "${command:cmake.launchTargetPath}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
},
{
"name": "Launch LLDB",
"type": "lldb",
"request": "launch",
"program": "${command:cmake.launchTargetPath}",
Expand Down
39 changes: 37 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"cmake.configureArgs": [
"-DCPP_CHANNEL_BUILD_EXAMPLES=ON",
"-DCMAKE_CXX_STANDARD=11",
"-DCPP_CHANNEL_BUILD_TESTS=ON",
"-DCPP_CHANNEL_BUILD_BENCHMARKS=ON",
"-DCPP_CHANNEL_BUILD_EXAMPLES=ON",
"-DCPP_CHANNEL_COVERAGE=ON",
"-DCPP_CHANNEL_SANITIZERS=ON",
"-DCPP_CHANNEL_SANITIZE_THREADS=OFF",
"-DCMAKE_CXX_STANDARD=11",
"-DCMAKE_INSTALL_PREFIX=${workspaceFolder}/install"
],
"editor.formatOnSave": true,
Expand All @@ -17,9 +18,43 @@
"[jsonc]": {
"editor.defaultFormatter": "vscode.json-language-features"
},
"[cmake]": {
"editor.defaultFormatter": "cheshirekow.cmake-format"
},
"clangd.arguments": [
"--compile-commands-dir=${workspaceFolder}/build",
"--background-index",
"--clang-tidy"
],
"C_Cpp.intelliSenseEngine": "disabled",
"cSpell.words": [
"ARGN",
"clangd",
"cppcoreguidelines",
"cpupower",
"ctest",
"DCMAKE",
"DCPP",
"endfunction",
"ensurepath",
"fcoverage",
"fprofile",
"fsanitize",
"genhtml",
"googletest",
"graphviz",
"gtest",
"HISTFILE",
"hwrap",
"lgcov",
"lldb",
"ltsan",
"lubsan",
"MSVC",
"noninteractive",
"pipx",
"powersave",
"STREQUAL",
"TESTNAME"
]
}
11 changes: 8 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.12)
cmake_minimum_required(VERSION 3.15)
project(cpp_channel)
set(PROJECT_VERSION 1.3.0)

Expand All @@ -15,17 +15,22 @@ add_library(msd_channel INTERFACE)
target_include_directories(msd_channel INTERFACE include)

option(CPP_CHANNEL_BUILD_TESTS "Build all of cpp_channel's own tests." OFF)
option(CPP_CHANNEL_BUILD_BENCHMARKS "Build all of cpp_channel's own benchmark tests." OFF)
option(CPP_CHANNEL_BUILD_EXAMPLES "Build cpp_channel's example programs." OFF)
option(CPP_CHANNEL_COVERAGE "Generate test coverage." OFF)
option(CPP_CHANNEL_SANITIZERS "Build with sanitizers." OFF)
option(CPP_CHANNEL_SANITIZE_THREADS "Build with thread sanitizer." OFF)

if (CPP_CHANNEL_BUILD_TESTS)
if(CPP_CHANNEL_BUILD_TESTS)
enable_testing()
add_subdirectory(tests)
endif()

if (CPP_CHANNEL_BUILD_EXAMPLES)
if(CPP_CHANNEL_BUILD_BENCHMARKS)
add_subdirectory(benchmarks)
endif()

if(CPP_CHANNEL_BUILD_EXAMPLES)
add_subdirectory(examples)
endif()

Expand Down
23 changes: 18 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
PROJECT_DIRS = include tests examples
BUILD_DIR = build
BENCH_DIR = build/bench
COV_DIR = build/coverage
DOCS_DIR = docs

all:

bench:
- sudo cpupower frequency-set --governor performance

mkdir -p build/bench && cd build/bench \
&& cmake ../.. -DCMAKE_BUILD_TYPE=Release -DCPP_CHANNEL_BUILD_TESTS=ON \
rm -rf $(BENCH_DIR) && mkdir $(BENCH_DIR) && cd $(BENCH_DIR) \
&& cmake ../.. -DCMAKE_BUILD_TYPE=Release -DCPP_CHANNEL_BUILD_BENCHMARKS=ON \
&& cmake --build . --config Release --target channel_benchmark -j \
&& ./tests/channel_benchmark
&& ./benchmarks/channel_benchmark

- sudo cpupower frequency-set --governor powersave

coverage:
mkdir -p build/coverage && cd build/coverage \
rm -rf $(COV_DIR) && mkdir $(COV_DIR) && cd $(COV_DIR) \
&& cmake ../.. -DCMAKE_BUILD_TYPE=Debug -DCPP_CHANNEL_BUILD_TESTS=ON -DCPP_CHANNEL_COVERAGE=ON \
&& cmake --build . --config Debug --target channel_tests -j \
&& ctest -C Debug --verbose -L channel_tests --output-on-failure -j \
Expand All @@ -24,4 +30,11 @@ coverage:

doc:
doxygen
cd docs && python3 -m http.server 8000
cd $(DOCS_DIR) && python3 -m http.server 8000

format:
clang-format -i $(shell find $(PROJECT_DIRS) -name *.*pp)
cmake-format -i $(shell find $(PROJECT_DIRS) -name CMakeLists.txt)

clean:
rm -rf $(BUILD_DIR) $(DOCS_DIR)
9 changes: 9 additions & 0 deletions benchmarks/.clang-tidy
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
InheritParentConfig: true

Checks: >
-cppcoreguidelines-avoid-magic-numbers,
-readability-magic-numbers,
-fuchsia-default-arguments-calls

CheckOptions:
- { key: readability-identifier-length.MinimumVariableNameLength, value: '1' }
16 changes: 16 additions & 0 deletions benchmarks/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
include(FetchContent)
if(NOT benchmark_POPULATED)
set(BENCHMARK_ENABLE_TESTING OFF CACHE BOOL "" FORCE)
set(BENCHMARK_ENABLE_GTEST_TESTS OFF CACHE BOOL "" FORCE)
FetchContent_Declare(benchmark URL https://github.com/google/benchmark/archive/refs/tags/v1.9.4.zip
DOWNLOAD_EXTRACT_TIMESTAMP TRUE)
FetchContent_MakeAvailable(benchmark)
endif()

function(package_add_benchmark TESTNAME)
add_executable(${TESTNAME} ${ARGN})
set_target_warnings(${TESTNAME} PRIVATE)
target_link_libraries(${TESTNAME} benchmark)
endfunction()

package_add_benchmark(channel_benchmark channel_benchmark.cpp)
20 changes: 10 additions & 10 deletions tests/channel_benchmark.cpp → benchmarks/channel_benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
------------------------------------------------------------------------------
Benchmark Time CPU Iterations
------------------------------------------------------------------------------
bm_channel_with_queue_storage 42602 ns 42598 ns 16407
bm_channel_with_vector_storage 42724 ns 42723 ns 16288
bm_channel_with_vector_storage 51332 ns 51328 ns 11776
channel_with_queue_storage 42602 ns 42598 ns 16407
channel_with_vector_storage 42724 ns 42723 ns 16288
channel_with_vector_storage 51332 ns 51328 ns 11776
*/

static void bm_channel_with_queue_storage(benchmark::State& state)
static void channel_with_queue_storage(benchmark::State& state)
{
msd::channel<std::string, msd::queue_storage<std::string>> channel{10};

Expand All @@ -39,14 +39,14 @@ static void bm_channel_with_queue_storage(benchmark::State& state)
}
}

BENCHMARK(bm_channel_with_queue_storage);
BENCHMARK(channel_with_queue_storage);

static void bm_channel_with_vector_storage(benchmark::State& state)
static void channel_with_vector_storage(benchmark::State& state)
{
msd::channel<std::string, msd::vector_storage<std::string>> channel{10};

std::string input(1000000, 'x');
std::string out = "";
std::string out{};
out.resize(input.size());

for (auto _ : state) {
Expand All @@ -55,9 +55,9 @@ static void bm_channel_with_vector_storage(benchmark::State& state)
}
}

BENCHMARK(bm_channel_with_vector_storage);
BENCHMARK(channel_with_vector_storage);

static void bm_channel_with_array_storage(benchmark::State& state)
static void channel_with_array_storage(benchmark::State& state)
{
msd::channel<std::string, msd::array_storage<std::string, 10>> channel{};

Expand All @@ -71,6 +71,6 @@ static void bm_channel_with_array_storage(benchmark::State& state)
}
}

BENCHMARK(bm_channel_with_array_storage);
BENCHMARK(channel_with_array_storage);

BENCHMARK_MAIN();
11 changes: 4 additions & 7 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
function(add_example NAME)
add_executable(${NAME} ${ARGN})

target_compile_options(${NAME} PRIVATE -std=c++${CMAKE_CXX_STANDARD})

set_target_warnings(${NAME} PRIVATE)

if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
target_link_libraries(${NAME} -ltsan)
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
target_compile_options(${NAME} PRIVATE -fsanitize=thread)
endif()
target_link_libraries(${NAME} -ltsan)
endif()

add_dependencies(examples ${NAME})
endfunction()
Expand All @@ -18,8 +16,7 @@ function(run_example NAME)
TARGET ${NAME}
POST_BUILD
COMMAND ${NAME}
COMMENT "Running example: ${NAME}"
)
COMMENT "Running example: ${NAME}")
endfunction()

add_custom_target(examples)
Expand Down
Loading