From df11965d09b7879b080a00f22fee0d18254fb586 Mon Sep 17 00:00:00 2001 From: Matt Shin Date: Thu, 4 Jun 2026 16:19:44 +0100 Subject: [PATCH] Improve cmake targets for code linters Modify cpplint target to use `cpplint` from the environment. ~Remove cpplint.py from lfric-jedi's source tree.~ cpplint 2.0.2 happiness - Update filter categories. - Fix new violations, `build/include_what_you_use`. Add cmakelint to CI and fix violations. --- .cmakelintrc | 1 + .github/workflows/ci.yml | 12 +++++ CMakeLists.txt | 7 +-- CPPLINT.cfg | 3 +- src/monio/AtlasReader.cc | 4 ++ src/monio/AtlasWriter.cc | 4 ++ src/monio/AttributeBase.cc | 2 + src/monio/AttributeDouble.cc | 2 + src/monio/AttributeInt.cc | 2 + src/monio/AttributeString.cc | 2 + src/monio/Data.cc | 3 ++ src/monio/DataContainerBase.cc | 3 ++ src/monio/DataContainerDouble.cc | 2 + src/monio/DataContainerFloat.cc | 2 + src/monio/DataContainerInt.cc | 2 + src/monio/File.cc | 2 + src/monio/FileData.cc | 2 + src/monio/Metadata.cc | 2 + src/monio/Monio.cc | 3 ++ src/monio/Reader.cc | 3 ++ src/monio/Utils.cc | 3 ++ src/monio/UtilsAtlas.cc | 3 ++ src/monio/Variable.cc | 4 ++ src/monio/Writer.cc | 2 + test/CMakeLists.txt | 75 ++++++++++++++++++-------------- 25 files changed, 113 insertions(+), 37 deletions(-) create mode 100644 .cmakelintrc diff --git a/.cmakelintrc b/.cmakelintrc new file mode 100644 index 0000000..41265bb --- /dev/null +++ b/.cmakelintrc @@ -0,0 +1 @@ +linelength=100 diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7914d1b..6f297b7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -17,6 +17,18 @@ env: jobs: + checks: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v5 + - name: Install checkers/linters + run: | + python3 -m pip install cmakelint + - name: Run cmakelint + run: | + mapfile -t names < <(find -name 'CMakeLists.txt' -o -name '*.cmake') + cmakelint "${names[@]}" + build1: if: | ( github.repository_owner == 'MetOffice' && github.event.pull_request.draft == false ) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4a73ed2..21e2426 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -36,9 +36,10 @@ find_package(atlas 0.20.2 REQUIRED) find_package(oops 1.0.0 REQUIRED) ## Export package info -set(MONIO_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/src - ${CMAKE_CURRENT_BINARY_DIR}/src - ${oops_SOURCE_DIR}/src) +set(MONIO_INCLUDE_DIRS + ${CMAKE_CURRENT_SOURCE_DIR}/src + ${CMAKE_CURRENT_BINARY_DIR}/src + ${oops_SOURCE_DIR}/src) set(MONIO_LIBRARIES monio) get_directory_property(MONIO_DEFINITIONS COMPILE_DEFINITIONS) diff --git a/CPPLINT.cfg b/CPPLINT.cfg index 900d63f..69e13b4 100644 --- a/CPPLINT.cfg +++ b/CPPLINT.cfg @@ -1,4 +1,3 @@ set noparent linelength=100 -filter=+build,-build/c++14,+legal,+readability,+runtime,+whitespace,-runtime/references,-runtime/printf - +filter=+build,-build/include,+legal,+readability,+runtime,-runtime/references,+whitespace,-whitespace/indent_namespace diff --git a/src/monio/AtlasReader.cc b/src/monio/AtlasReader.cc index 99d0740..a33c767 100644 --- a/src/monio/AtlasReader.cc +++ b/src/monio/AtlasReader.cc @@ -8,6 +8,10 @@ ******************************************************************************/ #include "AtlasReader.h" +#include +#include +#include + #include "oops/util/Logger.h" #include "Utils.h" diff --git a/src/monio/AtlasWriter.cc b/src/monio/AtlasWriter.cc index 087429a..17a8b91 100644 --- a/src/monio/AtlasWriter.cc +++ b/src/monio/AtlasWriter.cc @@ -9,6 +9,10 @@ ******************************************************************************/ #include "AtlasWriter.h" +#include +#include +#include + #include "atlas/grid/Iterator.h" #include "oops/util/Logger.h" diff --git a/src/monio/AttributeBase.cc b/src/monio/AttributeBase.cc index 81b9f5b..c1582b9 100644 --- a/src/monio/AttributeBase.cc +++ b/src/monio/AttributeBase.cc @@ -8,6 +8,8 @@ ******************************************************************************/ #include "AttributeBase.h" +#include + monio::AttributeBase::AttributeBase( const std::string& name, const int type): diff --git a/src/monio/AttributeDouble.cc b/src/monio/AttributeDouble.cc index fcad5e9..49a1049 100644 --- a/src/monio/AttributeDouble.cc +++ b/src/monio/AttributeDouble.cc @@ -8,6 +8,8 @@ ******************************************************************************/ #include "AttributeDouble.h" +#include + #include "Constants.h" monio::AttributeDouble::AttributeDouble(const std::string& name, const double value) : diff --git a/src/monio/AttributeInt.cc b/src/monio/AttributeInt.cc index e559f2a..dc90a9f 100644 --- a/src/monio/AttributeInt.cc +++ b/src/monio/AttributeInt.cc @@ -8,6 +8,8 @@ ******************************************************************************/ #include "AttributeInt.h" +#include + #include "Constants.h" monio::AttributeInt::AttributeInt(const std::string& name, const int value) : diff --git a/src/monio/AttributeString.cc b/src/monio/AttributeString.cc index 62b3a14..712e57e 100644 --- a/src/monio/AttributeString.cc +++ b/src/monio/AttributeString.cc @@ -8,6 +8,8 @@ ******************************************************************************/ #include "AttributeString.h" +#include + #include "Constants.h" monio::AttributeString::AttributeString(const std::string& name, diff --git a/src/monio/Data.cc b/src/monio/Data.cc index b177506..b5febb2 100644 --- a/src/monio/Data.cc +++ b/src/monio/Data.cc @@ -8,7 +8,10 @@ ******************************************************************************/ #include "Data.h" +#include +#include #include +#include #include #include "oops/util/Logger.h" diff --git a/src/monio/DataContainerBase.cc b/src/monio/DataContainerBase.cc index 0bab586..84e146a 100644 --- a/src/monio/DataContainerBase.cc +++ b/src/monio/DataContainerBase.cc @@ -8,6 +8,9 @@ ******************************************************************************/ #include "DataContainerBase.h" +#include +#include + monio::DataContainerBase::DataContainerBase( const std::string& name, const int type) : diff --git a/src/monio/DataContainerDouble.cc b/src/monio/DataContainerDouble.cc index 439919e..ec815be 100644 --- a/src/monio/DataContainerDouble.cc +++ b/src/monio/DataContainerDouble.cc @@ -9,6 +9,8 @@ #include "DataContainerDouble.h" #include +#include +#include #include "Constants.h" #include "Monio.h" diff --git a/src/monio/DataContainerFloat.cc b/src/monio/DataContainerFloat.cc index 7f0417c..161b369 100644 --- a/src/monio/DataContainerFloat.cc +++ b/src/monio/DataContainerFloat.cc @@ -9,6 +9,8 @@ #include "DataContainerFloat.h" #include +#include +#include #include "Constants.h" #include "Monio.h" diff --git a/src/monio/DataContainerInt.cc b/src/monio/DataContainerInt.cc index 374054d..7645d98 100644 --- a/src/monio/DataContainerInt.cc +++ b/src/monio/DataContainerInt.cc @@ -9,6 +9,8 @@ #include "DataContainerInt.h" #include +#include +#include #include "Constants.h" #include "Monio.h" diff --git a/src/monio/File.cc b/src/monio/File.cc index 0c8872b..c0064cb 100644 --- a/src/monio/File.cc +++ b/src/monio/File.cc @@ -12,6 +12,8 @@ #include #include #include +#include +#include #include "oops/util/Logger.h" diff --git a/src/monio/FileData.cc b/src/monio/FileData.cc index dc29255..6f0867d 100644 --- a/src/monio/FileData.cc +++ b/src/monio/FileData.cc @@ -8,6 +8,8 @@ ******************************************************************************/ #include "FileData.h" +#include + monio::FileData::FileData() : data_(), metadata_() {} diff --git a/src/monio/Metadata.cc b/src/monio/Metadata.cc index 0421ff1..29e2f29 100644 --- a/src/monio/Metadata.cc +++ b/src/monio/Metadata.cc @@ -10,6 +10,8 @@ #include #include +#include +#include #include #include #include diff --git a/src/monio/Monio.cc b/src/monio/Monio.cc index 8d3729a..89108de 100644 --- a/src/monio/Monio.cc +++ b/src/monio/Monio.cc @@ -8,7 +8,10 @@ ******************************************************************************/ #include "Monio.h" +#include #include +#include +#include #include #include "atlas/parallel/mpi/mpi.h" diff --git a/src/monio/Reader.cc b/src/monio/Reader.cc index 115ee46..9a7a6f6 100644 --- a/src/monio/Reader.cc +++ b/src/monio/Reader.cc @@ -10,9 +10,12 @@ #include #include +#include #include #include +#include #include +#include #include "Constants.h" #include "DataContainerDouble.h" diff --git a/src/monio/Utils.cc b/src/monio/Utils.cc index 1668eaa..74cafcc 100644 --- a/src/monio/Utils.cc +++ b/src/monio/Utils.cc @@ -13,7 +13,10 @@ #include #include #include +#include #include +#include +#include #include "AttributeBase.h" #include "DataContainerBase.h" diff --git a/src/monio/UtilsAtlas.cc b/src/monio/UtilsAtlas.cc index 2a784de..5010094 100644 --- a/src/monio/UtilsAtlas.cc +++ b/src/monio/UtilsAtlas.cc @@ -9,7 +9,10 @@ #include "UtilsAtlas.h" #include +#include #include +#include +#include #include "atlas/functionspace.h" #include "atlas/grid/Iterator.h" diff --git a/src/monio/Variable.cc b/src/monio/Variable.cc index 7299cba..c55523d 100644 --- a/src/monio/Variable.cc +++ b/src/monio/Variable.cc @@ -9,8 +9,12 @@ #include "Variable.h" #include +#include +#include #include +#include #include +#include #include "AttributeInt.h" #include "AttributeString.h" diff --git a/src/monio/Writer.cc b/src/monio/Writer.cc index 1f42cf2..ecf69d7 100644 --- a/src/monio/Writer.cc +++ b/src/monio/Writer.cc @@ -10,7 +10,9 @@ #include #include +#include #include +#include #include "Constants.h" #include "DataContainerDouble.h" diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 45546bb..0861e16 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -12,13 +12,7 @@ message(STATUS "PROJECT_BINARY_DIR: ${PROJECT_BINARY_DIR}") message(STATUS "CMAKE_CURRENT_SOURCE_DIR: ${CMAKE_CURRENT_SOURCE_DIR}") message(STATUS "CMAKE_CURRENT_BINARY_DIR: ${CMAKE_CURRENT_BINARY_DIR}") -execute_process(COMMAND ${CMAKE_COMMAND} -E copy - ${PROJECT_SOURCE_DIR}/test/utils/cpplint.py - ${CMAKE_BINARY_DIR}/bin/monio_cpplint.py) - -ecbuild_add_resources(TARGET monio_test_scripts - SOURCES_PACK ${monio_test_input} - ${PROJECT_SOURCE_DIR}/test/utils/cpplint.py) +ecbuild_add_resources(TARGET monio_test_scripts SOURCES_PACK ${monio_test_input}) if(NOT DEFINED MONIO_TESTFILES_DIR) if(DEFINED ENV{MONIO_TESTFILES_DIR}) @@ -62,17 +56,31 @@ list(APPEND monio_testinput ) foreach(FILENAME ${monio_testinput}) - execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink - ${CMAKE_CURRENT_SOURCE_DIR}/${FILENAME} - ${CMAKE_CURRENT_BINARY_DIR}/${FILENAME}) -endforeach(FILENAME) + file( + CREATE_LINK + ${CMAKE_CURRENT_SOURCE_DIR}/${FILENAME} + ${CMAKE_CURRENT_BINARY_DIR}/${FILENAME} + SYMBOLIC + ) +endforeach() -ecbuild_add_test(TARGET monio_coding_norms - TYPE SCRIPT - COMMAND ${CMAKE_BINARY_DIR}/bin/monio_cpplint.py - ARGS --quiet --recursive - ${PROJECT_SOURCE_DIR}/src - ${PROJECT_SOURCE_DIR}/test) +# We can make use of CMake's native support of cpplint in future. If we set +# `CMAKE__CPPLINT` (where `` is either `C` or `CXX`) to point to +# the `cpplint` executable, it will run `cpplint` as part of the build. +find_program( + CPPLINT_EXECUTABLE + NAMES cpplint + DOC "Path to the cpplint executable" +) +if(CPPLINT_EXECUTABLE) + ecbuild_add_test( + TARGET "${PROJECT_NAME}_coding_norms" + LABELS lint + TYPE SCRIPT + COMMAND "${CPPLINT_EXECUTABLE}" + ARGS --quiet --recursive "${PROJECT_SOURCE_DIR}" + ) +endif() if(NOT IS_DIRECTORY "${MONIO_TESTFILES_DIR}") message(WARNING @@ -81,20 +89,23 @@ if(NOT IS_DIRECTORY "${MONIO_TESTFILES_DIR}") return() endif() -ecbuild_add_test(TARGET test_monio_fieldset_write - SOURCES mains/TestFieldSetWrite.cc - ARGS "testinput/fieldset_write.yaml" - LIBS monio - MPI 4) +ecbuild_add_test( + TARGET test_monio_fieldset_write + SOURCES mains/TestFieldSetWrite.cc + ARGS "testinput/fieldset_write.yaml" + LIBS monio + MPI 4) -ecbuild_add_test(TARGET test_monio_state_basic - SOURCES mains/TestStateBasic.cc - ARGS "testinput/state_basic.yaml" - LIBS monio - MPI 4) +ecbuild_add_test( + TARGET test_monio_state_basic + SOURCES mains/TestStateBasic.cc + ARGS "testinput/state_basic.yaml" + LIBS monio + MPI 4) -ecbuild_add_test(TARGET test_monio_state_full - SOURCES mains/TestStateFull.cc - ARGS "testinput/state_full.yaml" - LIBS monio - MPI 4) +ecbuild_add_test( + TARGET test_monio_state_full + SOURCES mains/TestStateFull.cc + ARGS "testinput/state_full.yaml" + LIBS monio + MPI 4)