Skip to content

Commit 854b750

Browse files
authored
Merge pull request #102 from cpp-best-practices/lint-task [skip ci]
2 parents a61a91b + 3ce3de4 commit 854b750

14 files changed

Lines changed: 81 additions & 51 deletions

.github/workflows/ci.yml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,12 @@ jobs:
6060
run: |
6161
task test
6262
task test_release
63-
task test_install
63+
task test_install
64+
65+
- name: Lint
66+
if: ${{ matrix.os == 'ubuntu-20.04' && matrix.compiler == 'gcc' }}
67+
run: |
68+
# TODO add to setup-cpp
69+
python3 -m pip install --user cmakelint cmake-format
70+
71+
task lint

Taskfile.yml

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,31 @@ tasks:
1313
- cd ./test/build && ctest -C Release --verbose
1414

1515
test_install:
16-
cmds:
16+
cmds:
1717
- task: test_release
1818
- cmake --install ./test/build --config Release --prefix ./install
1919
- cmake ./test_install -B ./test_install/build -DCMAKE_BUILD_TYPE:STRING=Release -G "Ninja Multi-Config" -DCMAKE_PREFIX_PATH:STRING={{.CWD}}/install;
2020
- cmake --build ./test_install/build --config Release
2121
- cd ./test_install/build && ctest -C Release --verbose
22-
vars:
23-
CWD:
24-
sh: git rev-parse --show-toplevel
22+
vars:
23+
CWD:
24+
sh: git rev-parse --show-toplevel
2525

26-
format:
27-
- clang-format -i ./test/src/*/*.cpp ./test/include/*/*.hpp ./test_install/*.cpp
28-
- cmake-format --in-place ./Index.cmake ./src/*.cmake ./test/CMakeLists.txt ./test_install/CMakeLists.txt
26+
lint:
27+
- |
28+
{{if eq OS "windows"}}
29+
powershell -c '$files=(git ls-files --exclude-standard); foreach ($file in $files) { if ((get-item $file).Extension -in ".cpp", ".hpp", ".c", ".cc", ".cxx", ".hxx", ".ixx") { clang-format -i -style=file $file } }'
30+
{{else}}
31+
git ls-files --exclude-standard | grep -E '\.(cpp|hpp|c|cc|cxx|hxx|ixx)$' | xargs clang-format -i -style=file
32+
{{end}}
33+
- |
34+
{{if eq OS "windows"}}
35+
powershell -c '$files=(git ls-files --exclude-standard); foreach ($file in $files) { $item=(get-item $file); if (($item.Name -eq "CMakeLists.txt") -or ($item.Extension -in ".cmake")) { cmake-format --in-place $file; cmake-lint $file --disabled-codes C0103 C0301 R0912 R0915 R0913 --suppress-decorations } }'
36+
{{else}}
37+
git ls-files --exclude-standard | grep -E '(CMakeLists\.txt)|(\.(cmake))$' | xargs cmake-format --in-place | xargs cmake-lint --disabled-codes C0103 C0301 R0912 R0915 R0913 --suppress-decorations
38+
{{end}}
2939
30-
lint: cmake-lint ./Index.cmake ./src/*.cmake
40+
- ~/vcpkg/vcpkg format-manifest ./test/vcpkg.json ./test_install/vcpkg.json
3141

3242
clean: |
3343
{{if eq OS "windows"}}

src/CompilerWarnings.cmake

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
# from here:
1+
# Set the compiler warnings
22
#
3+
# https://clang.llvm.org/docs/DiagnosticsReference.html
34
# https://github.com/lefticus/cppbestpractices/blob/master/02-Use_the_Tools_Available.md
4-
55
function(
66
set_project_warnings
77
project_name

src/Conan.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# Run Conan for dependency management
12
macro(run_conan)
23
# Download automatically, you can also just copy the conan.cmake file
34
if(NOT EXISTS "${CMAKE_BINARY_DIR}/conan.cmake")

src/DynamicProjectOptions.cmake

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@
44
# * ENABLE_CLANG_TIDY: ON for Ninja/Makefiles
55
# * ENABLE_SANITIZER_UNDEFINED: ON for Compilers that support it
66
# * ENABLE_CPPCHECK: ON for Ninja/Makefiles
7-
7+
#
88
# For non-developer builds
99
# -DENABLE_DEVELOPER_MODE:BOOL=OFF
1010
# Is recommended
11-
11+
#
1212
# In developer mode, all features have options that show up in the CMake GUI tools
13-
13+
#
1414
# dynamic_project_options() macro enables all recommended defaults with appropriately
1515
# applied options from the GUI which are set
16-
16+
#
1717
# Any default can be overridden
1818
# set(<feature_name>_DEFAULT <value>) - set default for both user and developer modes
1919
# set(<feature_name>_DEVELOPER_DEFAULT <value>) - set default for developer mode
2020
# set(<feature_name>_USER_DEFAULT <value>) - set default for user mode
21-
21+
#
2222
macro(dynamic_project_options)
2323
option(ENABLE_DEVELOPER_MODE "Set up defaults for a developer of the project, and let developer change options" OFF)
2424
if(NOT ${ENABLE_DEVELOPER_MODE})

src/Linker.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# TODO use function arguments instead of CMake cache variables and options
2+
3+
# Set the linker to use for the linking phase
14
macro(configure_linker project_name)
25
include(CheckCXXCompilerFlag)
36

src/PackageProject.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Uses ycm (permissive BSD-3-Clause license) and ForwardArguments (permissive MIT license)
22

3+
# A function that packages the project for external usage (e.g. from vcpkg, Conan, etc).
4+
# See the [README.md] for more details
35
function(package_project)
46
cmake_policy(SET CMP0103 NEW) # disallow multiple calls with the same NAME
57

src/Standards.cmake

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# Set the default copmiler standards if not specified
12
macro(set_standards)
23

34
# if the default CMAKE_CXX_STANDARD is not set, detect the latest CXX standard supported by the compiler and use it.
@@ -21,7 +22,7 @@ macro(set_standards)
2122
set(CMAKE_CXX_STANDARD ${CXX_LATEST_STANDARD})
2223
endif()
2324

24-
if("{CMAKE_C_STANDARD}" STREQUAL "")
25+
if("${CMAKE_C_STANDARD}" STREQUAL "")
2526
if(NOT
2627
${ProjectOptions_ENABLE_CPPCHECK} # cppcheck doesn't support C17 https://sourceforge.net/p/cppcheck/discussion/general/thread/19ea152bba/
2728
AND DEFINED CMAKE_C17_STANDARD_COMPILE_OPTION

src/StaticAnalyzers.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# Enable static analysis with Cppcheck
12
macro(enable_cppcheck CPPCHECK_OPTIONS)
23
find_program(CPPCHECK cppcheck)
34
if(CPPCHECK)
@@ -52,6 +53,7 @@ macro(enable_cppcheck CPPCHECK_OPTIONS)
5253
endif()
5354
endmacro()
5455

56+
# Enable static analysis with clang-tidy
5557
macro(enable_clang_tidy)
5658
find_program(CLANGTIDY clang-tidy)
5759
if(CLANGTIDY)
@@ -115,6 +117,7 @@ macro(enable_clang_tidy)
115117
endif()
116118
endmacro()
117119

120+
# Enable static analysis with include-what-you-use
118121
macro(enable_include_what_you_use)
119122
find_program(INCLUDE_WHAT_YOU_USE include-what-you-use)
120123
if(INCLUDE_WHAT_YOU_USE)

src/Tests.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# Enable coverage reporting for gcc/clang
12
function(enable_coverage project_name)
23
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES ".*Clang")
34
target_compile_options(${project_name} INTERFACE --coverage -O0 -g)

0 commit comments

Comments
 (0)