Skip to content

Commit

Permalink
Check C/C++ code formatting in CI. (#1732)
Browse files Browse the repository at this point in the history
* Add C/C++ formatting check.

Fix [check-]clang-format targets in clean builds.

* Do not format nrnmpidec.h.

* clang-format-12.0.1: apply
  • Loading branch information
olupton authored Mar 29, 2022
1 parent cd7274c commit 013b69a
Show file tree
Hide file tree
Showing 356 changed files with 108,809 additions and 103,697 deletions.
49 changes: 45 additions & 4 deletions .github/workflows/formatting.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ jobs:
- uses: actions/checkout@v3

- name: Check Python formatting
shell: bash
working-directory: ${{runner.workspace}}/nrn
run: |
git ls-tree -z -r HEAD --name-only \
Expand Down Expand Up @@ -63,14 +62,56 @@ jobs:
- uses: actions/checkout@v3

- name: Configure
shell: bash
working-directory: ${{runner.workspace}}/nrn
run: |
mkdir build
cd build/
# Need to configure the build to trigger the hpc-coding-conventions YAML magic
cmake -G Ninja .. -DNRN_CMAKE_FORMAT=ON -DNRN_ENABLE_RX3D=OFF -DNRN_ENABLE_PYTHON=OFF -DNRN_ENABLE_INTERVIEWS=OFF -DNRN_ENABLE_MPI=OFF
# Need to configure the build to trigger the hpc-coding-conventions
# YAML magic
cmake -G Ninja .. \
-DNRN_CMAKE_FORMAT=ON \
-DNRN_ENABLE_RX3D=OFF \
-DNRN_ENABLE_PYTHON=OFF \
-DNRN_ENABLE_INTERVIEWS=OFF \
-DNRN_ENABLE_MPI=OFF
- name: Check CMake formatting
working-directory: ${{runner.workspace}}/nrn/build
run: ninja check-cmake-format

cpp:
name: C/C++
runs-on: ubuntu-20.04
timeout-minutes: 5
steps:
- name: Install dependencies
run: |
sudo apt-get update
sudo apt-get install cython3 libopenmpi-dev ninja-build python3-dev \
python3-numpy python3-pytest python3-pytest-cov
# The last .1 refers to ssciwr/clang-format-wheel#23.
sudo pip install clang-format==12.0.1.1
command -v clang-format
clang-format --version
- uses: actions/checkout@v3

- name: Configure
working-directory: ${{runner.workspace}}/nrn
run: |
mkdir build
cd build/
# Need to configure the build to trigger the hpc-coding-conventions
# YAML magic. Unlike the CMake formatting check, the C/C++ formatting
# check is based on the compilation dictionary generated by CMake, so
# we need to configure the build to compile as many source files as
# possible.
cmake -G Ninja .. \
-DClangFormat_EXECUTABLE=$(command -v clang-format) \
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
-DNRN_CLANG_FORMAT=ON \
-DNRN_ENABLE_INTERVIEWS=OFF
- name: Check C/C++ formatting
working-directory: ${{runner.workspace}}/nrn/build
run: ninja check-clang-format
14 changes: 14 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,13 @@ endif()
# =============================================================================
set(CODING_CONV_PREFIX "NRN")
set(${CODING_CONV_PREFIX}_3RDPARTY_DIR "external")
set(${CODING_CONV_PREFIX}_ClangFormat_REQUIRED_VERSION 12.0.1)
set(${CODING_CONV_PREFIX}_ClangFormat_EXCLUDES_RE
# Exclude translated MOD files that are put in the source tree. This can be
# dropped once https://github.com/neuronsimulator/nrn/pull/1673 is merged.
".*?/src/nrnoc/\\(apcount\\|exp2syn\\|expsyn\\|feature\\|hh\\|intfire1\\|intfire2\\|intfire4\\|netstim\\|oclmp\\|passive\\|pattern\\|ppmark\\|stim\\|svclmp\\|syn\\|vclmp\\)\\.c$"
".*?/src/nrnoc/nrnversion\\.h$"
CACHE STRING "")
set(CODING_CONV_CMAKE "${PROJECT_SOURCE_DIR}/${NRN_3RDPARTY_DIR}/coding-conventions/cpp/cmake")
if(NOT EXISTS "${CODING_CONV_CMAKE}/3rdparty.cmake")
nrn_initialize_submodule(external/coding-conventions)
Expand All @@ -629,6 +636,13 @@ include("${CODING_CONV_CMAKE}/3rdparty.cmake")
# =============================================================================
if(NRN_CMAKE_FORMAT OR NRN_CLANG_FORMAT)
add_subdirectory(external/coding-conventions/cpp)
if(NRN_CLANG_FORMAT)
# The clang-format and check-clang-format targets fail out of the box in clean builds because
# the headers generated by Bison and Flex are not available. The generated_source_files target
# makes sure all such files exist, without forcing a full build of NEURON.
add_dependencies(clang-format generated_source_files)
add_dependencies(check-clang-format generated_source_files)
endif()
endif()

# =============================================================================
Expand Down
Loading

0 comments on commit 013b69a

Please sign in to comment.