Skip to content

Commit

Permalink
Enable consistent clang format for the project (#122)
Browse files Browse the repository at this point in the history
* Enable consistent clang format for the project

1. Add a .clang-format which is basically 2 spaces, allman braces,
   embedded namespaces.
2. Reformat the entire codebase with
   find src -iname "*.cpp" -o -iname "*.h" -o -iname "*.mm" | xargs clang-format -i
3. add a cmake code checks target which currently just checkes that
   the code remains clang formatted
4. Add a github action which runs that code check target on ubuntu on a PR
  • Loading branch information
baconpaul authored Sep 10, 2023
1 parent d491c2f commit 019afea
Show file tree
Hide file tree
Showing 30 changed files with 3,308 additions and 3,225 deletions.
36 changes: 36 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
UseTab: Never
---
Language: Cpp
AlignAfterOpenBracket: BAS_Align
ConstructorInitializerIndentWidth: 2
SpaceBeforeParens: ControlStatements
ColumnLimit: 105
BasedOnStyle: Google
IndentWidth: 2
AlignAfterOpenBracket: Align
ConstructorInitializerAllOnOneLineOrOnePerLine: true
BreakConstructorInitializers: BeforeComma
BreakBeforeBraces: Allman
ColumnLimit: 105
SortIncludes: false
NamespaceIndentation: None
ReflowComments: false
AllowShortFunctionsOnASingleLine: None
---
Language: ObjC
BasedOnStyle: Google
AlignAfterOpenBracket: BAS_Align
ConstructorInitializerIndentWidth: 2
SpaceBeforeParens: ControlStatements
IndentWidth: 2
AlignAfterOpenBracket: Align
ConstructorInitializerAllOnOneLineOrOnePerLine: true
BreakConstructorInitializers: BeforeComma
BreakBeforeBraces: Allman
ColumnLimit: 105
SortIncludes: false
NamespaceIndentation: None
ReflowComments: false
AllowShortFunctionsOnASingleLine: None
...
21 changes: 21 additions & 0 deletions .github/workflows/code-checks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Clap Wrapper Format and Code Checks
on: [pull_request]

defaults:
run:
shell: bash

jobs:
build-code-checks:
name: code-checks
runs-on: ubuntu-latest
strategy:
fail-fast: true
steps:
- uses: actions/checkout@v3
with:
submodules: recursive
- name: Run Code Checks
run: |
cmake -Bbuild -DCLAP_WRAPPER_CODE_CHECKS_ONLY=TRUE
cmake --build build --target clap-wrapper-code-checks
29 changes: 28 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ set(CMAKE_VISIBILITY_INLINES_HIDDEN ON)
# defines functions relative to this, so needs a cache
# variable with the source dir
set(CLAP_WRAPPER_CMAKE_CURRENT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR} CACHE STRING "Clap Wrapper Source Location")
include(cmake/enable_sdks.cmake)
if (DEFINED CLAP_WRAPPER_CODE_CHECKS_ONLY)
message(STATUS "clap-wrapper: code checks only; skipping enable sdks")
else()
include(cmake/enable_sdks.cmake)
endif()

# automatically build the wrapper only if this is the top level project
set(skipbuild FALSE)
Expand All @@ -64,6 +68,9 @@ if (NOT PROJECT_IS_TOP_LEVEL)
set(skipbuild TRUE)
endif()
endif()
if (DEFINED CLAP_WRAPPER_CODE_CHECKS_ONLY)
set(skipbuild ${CLAP_WRAPPER_CODE_CHECKS_ONLY})
endif()

if (${skipbuild})
message(STATUS "clap-wrapper: Skipping clap wrapper target ejection")
Expand Down Expand Up @@ -106,3 +113,23 @@ else()
endif()
endif()
endif()


add_custom_target(clap-wrapper-code-checks)

# Clang Format checks
find_program(CLANG_FORMAT_EXE NAMES clang-format)
set(CLANG_FORMAT_DIRS src)
set(CLANG_FORMAT_EXTS cpp h)
foreach(dir ${CLANG_FORMAT_DIRS})
foreach(ext ${CLANG_FORMAT_EXTS})
list(APPEND CLANG_FORMAT_GLOBS "':(glob)${dir}/**/*.${ext}'")
endforeach()
endforeach()
add_custom_command(TARGET clap-wrapper-code-checks
POST_BUILD
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
COMMAND ${CMAKE_COMMAND} -E echo About to check clang-format using ${CLANG_FORMAT_EXE}
COMMAND git ls-files -- ${CLANG_FORMAT_GLOBS} | xargs ${CLANG_FORMAT_EXE} --dry-run --Werror
)
# }}}
Loading

0 comments on commit 019afea

Please sign in to comment.