Skip to content

Commit 131c11f

Browse files
codethinkicodethinkiLukas Thomann
authored
Master/add ci uncrustify enforcement (#10)
**Original Issue**: [#5] closes #5 ### Summary solves the issue by adding uncrustify as the repo wide formatter for c++ files --------- Co-authored-by: codethinki <lukas.jmy@outlook.de> Co-authored-by: Lukas Thomann <lukas.thomann@outlook.com>
1 parent e48b89a commit 131c11f

568 files changed

Lines changed: 55135 additions & 51516 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.clang-format

Lines changed: 0 additions & 94 deletions
This file was deleted.

.github/CODEOWNERS

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,10 @@
22
.github/workflows/ @codethinki
33
ci/docker/ @codethinki
44

5+
CITATION @codethinki
56
LICENSE @codethinki
6-
.clang-format @codethinki
7+
FLASHLIGHT_LICENSE @codethinki
8+
9+
.clang_tidy @codethinki
10+
uncrustify.cfg @codethinki
11+
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
name: Uncrustify Format
2+
3+
on:
4+
push: { branches: ["master", "_master/add_ci"] }
5+
pull_request: { branches: ["master"] }
6+
7+
permissions: { contents: read }
8+
9+
# ---------------------------------------------------------
10+
# CONFIG
11+
# ---------------------------------------------------------
12+
env:
13+
UNCRUSTIFY_CONFIG: "uncrustify.cfg"
14+
CHECK_PATH: "flashlight"
15+
FILE_EXTENSIONS: "c|cpp|h|hpp|cu"
16+
17+
# ---------------------------------------------------------
18+
# JOB
19+
# ---------------------------------------------------------
20+
jobs:
21+
formatting-check:
22+
name: Format check
23+
runs-on: ubuntu-latest
24+
container: archlinux:latest
25+
steps:
26+
- name: Install Git and Uncrustify
27+
run: pacman -Syu --noconfirm git uncrustify
28+
29+
- uses: actions/checkout@v4
30+
31+
- name: Run uncrustify style check
32+
run: |
33+
# Print the version to confirm you are on the absolute latest version
34+
uncrustify --version
35+
36+
find ${{ env.CHECK_PATH }} \
37+
-type f \
38+
-regextype posix-extended \
39+
-regex ".*\.(${{ env.FILE_EXTENSIONS }})$" \
40+
| uncrustify -q -c ${{ env.UNCRUSTIFY_CONFIG }} --check -F -

CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,9 @@ include(${FL_ROOT_DIR}/pkg/CMakeLists.txt)
142142

143143
# --------------------------- Cleanup ---------------------------
144144
setup_install_targets(INSTALL_TARGETS ${INSTALLABLE_TARGETS})
145+
146+
# --------------------------- Other ------------------------------
147+
include(fm_target_utilities)
148+
fm_glob_cpp(FM_CPP "flashlight/*")
149+
150+
fm_add_uncrustify_target(uncrustify-format OPTIONAL ${FM_CPP})

CONTRIBUTING.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ flashmini is still under development; we appreciate any contributions.
44
## Pull Requests
55
We actively welcome your pull requests.
66

7-
1. Fork the repo and create your branch from `master`.
7+
1. Fork the repo and create your branch from `master`. Naming style of `_master/your_feature` is preferred
88
2. If you've added code that should be tested, add tests.
99
3. If you've changed APIs, update [and build](docs/README.md) the documentation (to check correctness - don't submit built documentation).
1010
4. Ensure the test suite passes.
11-
5. Make sure your code lints and run `clang-format` given the provided configuration.
11+
5. Make sure your code lints and run `uncrustify format` given the provided configuration. Alternatively you can build the `uncrustify-format` target.
1212

1313
## Issues
1414
We use [GitHub issues](https://github.com/flashmini/flashmini/issues) to track public bugs. When filing, a bug, please make sure your description is clear and include sufficient instructions to reproduce the issue (for instance, your OS, compiler version, and selected backend).
File renamed without changes.

cmake/utils/fm_target_utilities.cmake

Lines changed: 89 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ endfunction()
8484
8585
#]]
8686
function(fm_glob_cpp OUT_VAR)
87-
fm_glob(${OUT_VAR} ${ARGN} PATTERNS "*.cpp" "*.hpp" "*.inl")
87+
fm_glob(${OUT_VAR} ${ARGN} PATTERNS "*.cpp" "*.hpp" "*.inl" "*.h" "*.cu" "*.cuh")
8888
set(${OUT_VAR} ${${OUT_VAR}} PARENT_SCOPE)
8989
endfunction()
9090

@@ -366,58 +366,121 @@ endfunction()
366366
367367
.. code-block:: cmake
368368
369-
fm_add_clang_format_target(<files...>)
369+
fm_add_clang_format_target(<target_name> [OPTIONAL] <files...>)
370370
371-
Creates a custom target named "format" that runs clang-format on specified files.
371+
Creates a custom target that runs clang-format on specified files.
372+
If OPTIONAL is specified, does not error and skips target creation if clang-format is not found.
372373
374+
:param target_name: Name of the custom target to create
375+
:param OPTIONAL: If specified, do not raise FATAL_ERROR if clang-format is not found
373376
:param files: List of source files to format
374-
:type files: list of file paths
375377
376-
:pre: clang-format executable is available in PATH
377-
:post: A custom target named "format" is created that formats the specified files in-place
378+
:post: A custom target is created if found, or configuration terminates with FATAL_ERROR if not found (unless OPTIONAL)
378379
379380
.. note::
380381
- The format target uses ``-i`` flag to format files in-place
381382
- The ``-style=file`` flag means clang-format will look for a .clang-format configuration file
382383
- Files are formatted relative to CMAKE_SOURCE_DIR
383384
384-
.. warning::
385-
This function will fail if clang-format is not found in PATH.
385+
.. seealso::
386+
- ``fm_find_clang_format(OPTIONAL)`` from fm_tool_utilities to locate clang-format optionally
387+
388+
#]]
389+
function(fm_add_clang_format_target TARGET_NAME)
390+
cmake_parse_arguments(PARSE_ARGV 1 ARG "OPTIONAL" "" "")
391+
392+
# Use a different variable name to avoid conflicts with the parsed ARG_OPTIONAL boolean
393+
if(ARG_OPTIONAL)
394+
set(FIND_OPTIONAL_ARG "OPTIONAL")
395+
else()
396+
set(FIND_OPTIONAL_ARG "")
397+
endif()
398+
399+
fm_assert_not_empty("${TARGET_NAME}" REASON "add_clang_format_target requires a target name")
400+
401+
# Use ARG_UNPARSED_ARGUMENTS instead of ARGN so "OPTIONAL" isn't treated as a file
402+
set(FILES_TO_FORMAT ${ARG_UNPARSED_ARGUMENTS})
403+
fm_assert_not_empty("${FILES_TO_FORMAT}" REASON "no files provided")
404+
405+
include(fm_tool_utilities)
386406

387-
**Example usage:**
407+
# Pass the safely stored string to the find function
408+
fm_find_clang_format(${FIND_OPTIONAL_ARG})
409+
if(NOT CLANG_FORMAT_EXECUTABLE)
410+
return()
411+
endif()
412+
413+
set(FILE_LIST_PATH "${CMAKE_CURRENT_BINARY_DIR}/${TARGET_NAME}_files.txt")
414+
string(REPLACE ";" "\n" FILES_TO_FORMAT_STR "${FILES_TO_FORMAT}")
415+
file(WRITE "${FILE_LIST_PATH}" "${FILES_TO_FORMAT_STR}\n")
416+
417+
add_custom_target(
418+
${TARGET_NAME}
419+
COMMAND ${CLANG_FORMAT_EXECUTABLE} -i -style=file --files=${FILE_LIST_PATH}
420+
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
421+
COMMENT "Formatting all source files with clang-format..."
422+
VERBATIM
423+
)
424+
endfunction()
425+
426+
#[[.rst:
427+
.. command:: fm_add_uncrustify_target
388428
389429
.. code-block:: cmake
390430
391-
# Format specific files
392-
fm_add_clang_format_target(
393-
src/main.cpp
394-
src/utils.cpp
395-
include/header.hpp
396-
)
431+
fm_add_uncrustify_target(<target_name> [OPTIONAL] <files...>)
432+
433+
Creates a custom target that runs uncrustify on specified files.
434+
If OPTIONAL is specified, does not error and skips target creation if uncrustify is not found.
397435
398-
# Then run: cmake --build . --target format
436+
:param target_name: Name of the custom target to create
437+
:param OPTIONAL: If specified, do not raise FATAL_ERROR if uncrustify is not found
438+
:param files: List of source files to format
439+
440+
:pre expects uncrustify.cfg in root directory
441+
:post: A custom target is created if found, or configuration terminates with FATAL_ERROR if not found (unless OPTIONAL)
442+
443+
.. note::
444+
- The format target uses ``--replace`` and ``--no-backup`` flags to format files in-place
445+
- The ``-F`` flag is used to pass the text file containing the list of files to format
446+
- Files are formatted relative to CMAKE_SOURCE_DIR
447+
- Uncrustify will look for an uncrustify.cfg file in the working directory or rely on the UNCRUSTIFY_CONFIG environment variable.
399448
400449
.. seealso::
401-
- ``fm_find_clang_format()`` from fm_tool_utilities to locate clang-format
402-
- Create a .clang-format file in your project root to define formatting style
450+
- ``fm_find_uncrustify(OPTIONAL)`` from fm_tool_utilities to locate uncrustify optionally
403451
404452
#]]
405-
function(fm_add_clang_format_target TARGET_NAME)
406-
fm_assert_not_empty(${TARGET_NAME} REASON "add_clang_format_target requires a target name")
453+
function(fm_add_uncrustify_target TARGET_NAME)
454+
cmake_parse_arguments(PARSE_ARGV 1 ARG "OPTIONAL" "" "")
455+
456+
# Use a different variable name to avoid being overwritten by cmake_parse_arguments
457+
if(ARG_OPTIONAL)
458+
set(FIND_OPTIONAL_ARG "OPTIONAL")
459+
else()
460+
set(FIND_OPTIONAL_ARG "")
461+
endif()
407462

408-
include(fm_tool_utilities)
409-
fm_find_clang_format()
463+
fm_assert_not_empty("${TARGET_NAME}" REASON "add_uncrustify_target requires a target name")
464+
465+
set(FILES_TO_FORMAT ${ARG_UNPARSED_ARGUMENTS})
466+
fm_assert_not_empty("${FILES_TO_FORMAT}" REASON "no files provided")
410467

411-
set(FILES_TO_FORMAT ${ARGN})
468+
include(fm_tool_utilities)
412469

413-
470+
fm_find_uncrustify(${FIND_OPTIONAL_ARG})
471+
if(NOT UNCRUSTIFY_EXECUTABLE)
472+
return()
473+
endif()
414474

475+
set(FILE_LIST_PATH "${CMAKE_CURRENT_BINARY_DIR}/${TARGET_NAME}_files.txt")
476+
string(REPLACE ";" "\n" FILES_TO_FORMAT_STR "${FILES_TO_FORMAT}")
477+
file(WRITE "${FILE_LIST_PATH}" "${FILES_TO_FORMAT_STR}\n")
415478

416479
add_custom_target(
417480
${TARGET_NAME}
418-
COMMAND ${CLANG_FORMAT_EXECUTABLE} -i -style=file ${FILES_TO_FORMAT}
481+
COMMAND ${UNCRUSTIFY_EXECUTABLE} --replace --no-backup -c uncrustify.cfg -q -F ${FILE_LIST_PATH}
419482
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
420-
COMMENT "Formatting all source files with clang-format..."
483+
COMMENT "Formatting all source files with uncrustify..."
421484
VERBATIM
422485
)
423486
endfunction()

cmake/utils/fm_tool_utilities.cmake

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,31 @@ function(fm_find_clang_format)
114114

115115
set(CLANG_FORMAT_EXECUTABLE ${CLANG_FORMAT_EXECUTABLE} PARENT_SCOPE)
116116
endfunction()
117+
118+
#[[.rst:
119+
.. command:: fm_find_uncrustify
120+
121+
.. code-block:: cmake
122+
123+
fm_find_uncrustify([OPTIONAL])
124+
125+
Locates a required uncrustify executable and exports its path to the parent scope.
126+
If OPTIONAL is specified, does not error if uncrustify is not found.
127+
128+
:post: UNCRUSTIFY_EXECUTABLE is set in PARENT_SCOPE with the full path to uncrustify, or configuration terminates with FATAL_ERROR if not found and not OPTIONAL
129+
130+
.. seealso::
131+
Use ``fm_add_uncrustify_target()`` from fm_target_utilities to create a format target.
132+
133+
#]]
134+
function(fm_find_uncrustify)
135+
cmake_parse_arguments(PARSE_ARGV 0 ARG "OPTIONAL" "" "")
136+
137+
if(ARG_OPTIONAL)
138+
fm_find_program(UNCRUSTIFY_EXECUTABLE uncrustify OPTIONAL)
139+
else()
140+
fm_find_program(UNCRUSTIFY_EXECUTABLE uncrustify)
141+
endif()
142+
143+
set(UNCRUSTIFY_EXECUTABLE ${UNCRUSTIFY_EXECUTABLE} PARENT_SCOPE)
144+
endfunction()

0 commit comments

Comments
 (0)