Skip to content
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

Clang-format support #117

Merged
merged 19 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from 18 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
24 changes: 24 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
BasedOnStyle: Google
AccessModifierOffset: -1
ColumnLimit: 120
BreakBeforeBraces: Custom
BraceWrapping:
AfterCaseLabel: false
AfterClass: false
AfterControlStatement: false
AfterEnum: true
AfterFunction: true
AfterNamespace: false
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
AfterExternBlock: false
BeforeCatch: false
BeforeElse: false
IndentBraces: false
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
DerivePointerAlignment: false
SortIncludes: false
PointerAlignment: Left
7 changes: 7 additions & 0 deletions .github/actions/apply-style/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM ghcr.io/llnl/radiuss:clang-14-ubuntu-22.04

COPY entrypoint.sh /entrypoint.sh

USER root

ENTRYPOINT ["/entrypoint.sh"]
69 changes: 69 additions & 0 deletions .github/actions/apply-style/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/bin/bash

# This is a bare minimum of options needed to create the `style` build target
# This does not create a working build.
CMAKE_ARGS=-DCMAKE_CXX_COMPILER=clang++
CMAKE_ARGS="$CMAKE_ARGS -DENABLE_CLANGFORMAT=ON"
CMAKE_ARGS="$CMAKE_ARGS -DCLANGFORMAT_EXECUTABLE=/usr/bin/clang-format"
CMAKE_ARGS="$CMAKE_ARGS -DTRIBOL_STYLE_CI_ONLY=ON"

# Avoid error "fatal: detected dubious ownership in repository at '/github/workspace'"
REPO_PATH=/github/workspace
git config --global --add safe.directory "$REPO_PATH"
find "$REPO_PATH" -type d | while read -r dir; do
git config --global --add safe.directory "$dir"
done

git fetch

###
# Attempt to find the branch of the PR from the detached head state
##

echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
echo "Attempting to find branch that matches commit..."

# Get the current commit SHA
current_commit_sha=$(git rev-parse HEAD)
# List all branches containing the current commit SHA
branches=$(git branch -r --contains $current_commit_sha)

# Loop over the string split by whitespace
branch=""
num_branches_found=0
for _possible_branch in $branches; do
# Skip items that start with "pull/"
if [[ $_possible_branch == pull/* ]]; then
continue
fi
if [[ $_possible_branch == origin/* ]]; then
_possible_branch=$(echo "$_possible_branch" | sed 's/origin\///')
fi
echo "Possible Branch: $_possible_branch"
branch=$_possible_branch
num_branches_found=$((num_branches_found+1))
done

if [ "$num_branches_found" -ne 1 ]; then
echo "Error: Unable to find a single branch that matched git sha $current_commit_sha"
exit 1
fi

echo "Found branch: $branch"
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"

git checkout $branch

git submodule update --init --recursive

mkdir build && cd build
cmake $CMAKE_ARGS ..
make style
cd ..

git config user.name "Agent Style"
git config user.email "[email protected]"
if [ -n "$(git status --porcelain)" ]; then
git commit -am 'Apply style updates'
git push
fi
28 changes: 28 additions & 0 deletions .github/workflows/apply-style.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Apply Style

on:
issue_comment:
types: [created]

jobs:
apply-style:
if: startsWith(github.event.comment.body, '/style')
name: Apply Style to Source
runs-on: ubuntu-latest

steps:
# Checkout the GitHub created reference for the PR.
# The only way to do this is by using the "issue" number
# but that is really the PR number in this context.
# This is due to using an `issue_comment` event which
# is due to the GitHub Actions API that does not have
# a `pull_request_comment` event or something similar.
# This leaves us in a detached head state which is corrected
# in `apply-style/entrypoint.sh`
- name: Checkout pull request
uses: actions/checkout@v3
with:
ref: refs/pull/${{ github.event.issue.number }}/head

- name: Apply style updates
uses: ./.github/actions/apply-style
58 changes: 55 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,35 @@ if ("${PROJECT_SOURCE_DIR}" STREQUAL "${CMAKE_SOURCE_DIR}")
set(BLT_CXX_STD "c++14" CACHE STRING "")
endif()

set(ENABLE_ASTYLE OFF CACHE BOOL "")
set(ENABLE_CLANGFORMAT OFF CACHE BOOL "")
set(ENABLE_UNCRUSTIFY OFF CACHE BOOL "")
# These BLT tools are not used in Tribol, turn them off
set(_unused_blt_tools
CLANGQUERY
VALGRIND
ASTYLE
CMAKEFORMAT
UNCRUSTIFY
YAPF)
foreach(_tool ${_unused_blt_tools})
set(ENABLE_${_tool} OFF CACHE BOOL "")
endforeach()

# These BLT tools are only used by Tribol developers, so turn them off
# unless an explicit executable path is given
set(_used_blt_tools
CLANGFORMAT
CLANGTIDY
CPPCHECK
DOXYGEN
SPHINX)
foreach(_tool ${_used_blt_tools})
if(NOT ${_tool}_EXECUTABLE)
set(ENABLE_${_tool} OFF CACHE BOOL "")
else()
set(ENABLE_${_tool} ON CACHE BOOL "")
endif()
endforeach()

set(BLT_REQUIRED_CLANGFORMAT_VERSION "14" CACHE STRING "")
endif()

include(${BLT_SOURCE_DIR}/SetupBLT.cmake)
Expand All @@ -75,6 +101,32 @@ include(cmake/TribolMacros.cmake)
include(cmake/Options.cmake)
include(cmake/TribolCompilerFlags.cmake)

#------------------------------------------------------------------------------
# Add Code Checks
#------------------------------------------------------------------------------

message(STATUS "Tribol Code Checks: ${TRIBOL_ENABLE_CODE_CHECKS}")

# Add extra file extensions for code styling
# Note: Add them also to tribol_add_code_checks in cmake/TribolMacros.cmake

# CUDA
list(APPEND BLT_C_FILE_EXTS ".cuh")
# Configured C++ files
list(APPEND BLT_C_FILE_EXTS ".cpp.in" ".hpp.in")

if (TRIBOL_STYLE_CI_ONLY OR TRIBOL_ENABLE_CODE_CHECKS)
tribol_add_code_checks(PREFIX tribol)
endif()

if (TRIBOL_STYLE_CI_ONLY)
# Exit processing the rest of the build in style only build to avoid any possible
# CMake configuration issues outside of just enabling `make style`. This build,
# is not capable of building Tribol and should not be advertised. It is for CI
# purposes only.
return()
endif()

#------------------------------------------------------------------------------
# Add TPLs and source directories
#------------------------------------------------------------------------------
Expand Down
83 changes: 44 additions & 39 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,42 +67,47 @@ jobs:
testRunTitle: '$(TEST_TARGET) Tests'
failTaskOnFailedTests: true

#TODO: Enable if you want code checks
#- job: Check_Code
# variables:
# VM_ImageName: 'ubuntu-22.04'
# Compiler_ImageName: $(Clang_14_ImageName)
# TEST_TARGET: 'linux_clang14'
# HOST_CONFIG: '[email protected]'
#
# strategy:
# matrix:
#TODO: Enable if you want codecov support
# coverage:
# DO_COVERAGE_CHECK: 'yes'
# DO_DOCS_CHECK: 'no'
# DO_STYLE_CHECK: 'no'
#TODO: Enable if you want to ensure documentation does not have warnings
# docs:
# DO_COVERAGE_CHECK: 'no'
# DO_DOCS_CHECK: 'yes'
# DO_STYLE_CHECK: 'no'
#TODO: Enable if you want to enforce style checks
# style:
# DO_COVERAGE_CHECK: 'no'
# DO_DOCS_CHECK: 'no'
# DO_STYLE_CHECK: 'yes'
#
# pool:
# vmImage: $(VM_ImageName)
#
# steps:
# - checkout: self
# clean: true
# submodules: recursive
# # https://docs.codecov.io/docs/testing-with-docker
# - script: |
# ci_env=`bash <(curl -s https://codecov.io/env)`
# echo " -e DO_COVERAGE_CHECK -e DO_DOCS_CHECK -e DO_STYLE_CHECK -e HOST_CONFIG ./tribol/scripts/azure-pipelines/linux-check.sh"
# docker run --rm -v `pwd`:$(HOME_DIR)/tribol $ci_env -e DO_COVERAGE_CHECK -e DO_DOCS_CHECK -e DO_STYLE_CHECK -e HOST_CONFIG $(Compiler_ImageName) ./tribol/scripts/azure-pipelines/linux-check.sh
# displayName: '$(TEST_TARGET) Check'
- job: Check_Code
variables:
VM_ImageName: 'ubuntu-22.04'
Compiler_ImageName: $(Clang_14_ImageName)
TEST_TARGET: 'linux_clang14'
HOST_CONFIG: '[email protected]'

strategy:
matrix:
coverage:
DO_COVERAGE_CHECK: 'yes'
DO_DOCS_CHECK: 'no'
DO_STYLE_CHECK: 'no'
DO_HEADER_CHECK: 'no'
# TODO: enable to check doxygen for warnings
# docs:
# DO_COVERAGE_CHECK: 'no'
# DO_DOCS_CHECK: 'yes'
# DO_STYLE_CHECK: 'no'
# DO_HEADER_CHECK: 'no'
style:
DO_COVERAGE_CHECK: 'no'
DO_DOCS_CHECK: 'no'
DO_STYLE_CHECK: 'yes'
DO_HEADER_CHECK: 'no'
header:
DO_COVERAGE_CHECK: 'no'
DO_DOCS_CHECK: 'no'
DO_STYLE_CHECK: 'no'
DO_HEADER_CHECK: 'yes'

pool:
vmImage: $(VM_ImageName)

steps:
- checkout: self
clean: true
submodules: recursive
# https://docs.codecov.io/docs/testing-with-docker
- script: |
ci_env=`bash <(curl -s https://codecov.io/env)`
echo " -e DO_COVERAGE_CHECK -e DO_DOCS_CHECK -e DO_STYLE_CHECK -e HOST_CONFIG ./tribol/scripts/azure-pipelines/linux-check.sh"
docker run --rm -v `pwd`:$(HOME_DIR)/tribol $ci_env -e DO_COVERAGE_CHECK -e DO_DOCS_CHECK -e DO_STYLE_CHECK -e HOST_CONFIG $(Compiler_ImageName) ./tribol/scripts/azure-pipelines/linux-check.sh
displayName: '$(TEST_TARGET) Check'
78 changes: 78 additions & 0 deletions cmake/TribolMacros.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,84 @@
#
# SPDX-License-Identifier: (MIT)


#------------------------------------------------------------------------------
# tribol_add_code_checks( PREFIX [prefix] )
#
# Adds code checks for all cpp/hpp files recursively under the current directory
# that regex match INCLUDES and excludes any files that regex match EXCLUDES
#
# This creates the following parent build targets:
# check - Runs a non file changing style check and CppCheck
# style - In-place code formatting
#
# Creates various child build targets that follow this pattern:
# tribol_<check|style>
# tribol_<cppcheck|clangformat>_<check|style>
#
# This also creates targets for running clang-tidy on the src/ and test/
# directories, with a more permissive set of checks for the tests,
# called tribol_guidelines_check and tribol_guidelines_check_tests, respectively
#------------------------------------------------------------------------------
macro(tribol_add_code_checks)

set(options)
set(singleValueArgs PREFIX)
set(multiValueArgs)

# Parse the arguments to the macro
cmake_parse_arguments(arg
"${options}" "${singleValueArgs}" "${multiValueArgs}" ${ARGN})

# Create file globbing expressions that only include directories that contain source
set(_base_dirs "src")
# Note: any extensions added here should also be added to BLT's lists in CMakeLists.txt
set(_ext_expressions "*.cpp" "*.hpp" "*.inl" "*.cuh" "*.cu" "*.cpp.in" "*.hpp.in")

set(_glob_expressions)
foreach(_exp ${_ext_expressions})
foreach(_base_dir ${_base_dirs})
list(APPEND _glob_expressions "${PROJECT_SOURCE_DIR}/${_base_dir}/${_exp}")
endforeach()
endforeach()

# Glob for list of files to run code checks on
set(_sources)
file(GLOB_RECURSE _sources ${_glob_expressions})

blt_add_code_checks(PREFIX ${arg_PREFIX}
SOURCES ${_sources}
CLANGFORMAT_CFG_FILE ${PROJECT_SOURCE_DIR}/.clang-format
CPPCHECK_FLAGS --enable=all --inconclusive)


set(_src_sources)
file(GLOB_RECURSE _src_sources "src/*.cpp" "src/*.hpp" "src/*.inl")
list(FILTER _src_sources EXCLUDE REGEX ".*/tests/.*pp")

blt_add_clang_tidy_target(NAME ${arg_PREFIX}_guidelines_check
CHECKS "clang-analyzer-*,clang-analyzer-cplusplus*,cppcoreguidelines-*"
SRC_FILES ${_src_sources})

# Create list of recursive test directory glob expressions
# NOTE: GLOB operator ** did not appear to be supported by cmake and did not recursively find test subdirectories
# NOTE: Do not include all directories at root (for example: blt)

file(GLOB_RECURSE _test_sources "${PROJECT_SOURCE_DIR}/src/*.cpp" "${PROJECT_SOURCE_DIR}/tests/*.cpp")
list(FILTER _test_sources INCLUDE REGEX ".*/tests/.*pp")

blt_add_clang_tidy_target(NAME ${arg_PREFIX}_guidelines_check_tests
CHECKS "clang-analyzer-*,clang-analyzer-cplusplus*,cppcoreguidelines-*,-cppcoreguidelines-avoid-magic-numbers"
SRC_FILES ${_test_sources})

if (ENABLE_COVERAGE)
blt_add_code_coverage_target(NAME ${arg_PREFIX}_coverage
RUNNER ${CMAKE_MAKE_PROGRAM} test
SOURCE_DIRECTORIES ${PROJECT_SOURCE_DIR}/src )
endif()

endmacro(tribol_add_code_checks)

##------------------------------------------------------------------------------
## tribol_assert_path_exists( path )
##
Expand Down
6 changes: 6 additions & 0 deletions host-configs/docker/[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ set(ENABLE_MPI ON CACHE BOOL "")

set(ENABLE_OPENMP ON CACHE BOOL "")

#------------------------------------------------------------------------------
# Options
#------------------------------------------------------------------------------

set(BUILD_REDECOMP ON CACHE BOOL "")

#------------------------------------------------------------------------------
# TPLs
#------------------------------------------------------------------------------
Expand Down
Loading