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 17 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
33 changes: 33 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,42 @@
"[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",
"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"
]
}
6 changes: 3 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 @@ -20,12 +20,12 @@ 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_EXAMPLES)
add_subdirectory(examples)
endif()

Expand Down
19 changes: 16 additions & 3 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 \
mkdir -p $(BENCH_DIR) && cd $(BENCH_DIR) \
&& cmake ../.. -DCMAKE_BUILD_TYPE=Release -DCPP_CHANNEL_BUILD_TESTS=ON \
&& cmake --build . --config Release --target channel_benchmark -j \
&& ./tests/channel_benchmark

- sudo cpupower frequency-set --governor powersave

coverage:
mkdir -p build/coverage && cd build/coverage \
rm -rf $(COV_DIR) && mkdir -p $(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)
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
13 changes: 6 additions & 7 deletions examples/cmake-project/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.16)
cmake_minimum_required(VERSION 3.15)
project(cmake_project)
set(PROJECT_VERSION 0.1.0)

Expand All @@ -10,11 +10,10 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wpedantic -Werror --cover
add_executable(cmake_project src/main.cpp)

include(FetchContent)
if (NOT channel_POPULATED)
FetchContent_Declare(channel URL https://github.com/andreiavrammsd/cpp-channel/archive/v1.3.0.zip DOWNLOAD_EXTRACT_TIMESTAMP TRUE)
if(NOT channel_POPULATED)
FetchContent_Declare(channel URL https://github.com/andreiavrammsd/cpp-channel/archive/v1.3.0.zip
DOWNLOAD_EXTRACT_TIMESTAMP TRUE)
FetchContent_Populate(channel)
include_directories(${channel_SOURCE_DIR}/include)
# OR
# add_subdirectory(${channel_SOURCE_DIR}/)
# target_link_libraries(cmake_project msd_channel)
endif ()
# OR add_subdirectory(${channel_SOURCE_DIR}/) target_link_libraries(cmake_project msd_channel)
endif()
20 changes: 10 additions & 10 deletions examples/move.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,31 @@
#include <iostream>

class Data final {
int i_{};
int value_{};

public:
Data() = default;
explicit Data(int i) : i_{i} {}
explicit Data(int value) : value_{value} {}

int getI() const { return i_; }
int get_value() const { return value_; }

Data(const Data& other) noexcept : i_{other.i_} { std::cout << "copy " << i_ << '\n'; }
Data(const Data& other) noexcept : value_{other.value_} { std::cout << "copy " << value_ << '\n'; }
Data& operator=(const Data& other)
{
if (this != &other) {
i_ = other.i_;
value_ = other.value_;
}
std::cout << "copy " << i_ << '\n';
std::cout << "copy " << value_ << '\n';

return *this;
}

Data(Data&& other) noexcept : i_{other.i_} { std::cout << "move " << i_ << '\n'; }
Data(Data&& other) noexcept : value_{other.value_} { std::cout << "move " << value_ << '\n'; }
Data& operator=(Data&& other) noexcept
{
if (this != &other) {
i_ = other.i_;
std::cout << "move " << i_ << '\n';
value_ = other.value_;
std::cout << "move " << value_ << '\n';
}

return *this;
Expand All @@ -49,7 +49,7 @@ int main()
chan << std::move(in3);

for (const auto& out : chan) {
std::cout << out.getI() << '\n';
std::cout << out.get_value() << '\n';

if (chan.empty()) {
break;
Expand Down
Loading
Loading