From f9210d8f15f566458b1f634f9e398dfdfaa4cce3 Mon Sep 17 00:00:00 2001 From: Stefan Venz Date: Tue, 7 Apr 2020 09:27:18 +0200 Subject: [PATCH 1/7] makefile: Add code formatting and checking targets Signed-off-by: Stefan Venz --- .Makefile.mk | 29 +++++++++ .clang-format | 127 +++++++++++++++++++++++++++++++++++++++ docker/Makefile | 37 ++++++++++++ docker/format/Dockerfile | 14 +++++ 4 files changed, 207 insertions(+) create mode 100644 .clang-format create mode 100644 docker/format/Dockerfile diff --git a/.Makefile.mk b/.Makefile.mk index e9dc3d1..8627f21 100644 --- a/.Makefile.mk +++ b/.Makefile.mk @@ -36,6 +36,9 @@ export WORKDIR ### default target ### DEFAULT += help +# header and source files proccessed by format and check +FILES=$(shell cd ../; find -regex '.*\.\(c\|h\)' -not -path *build*) + ### menuconfig targets ### .PHONY: menuconfig @@ -107,6 +110,32 @@ CHECK += check-monitor HELP_check-monitor = check env if monitor is possible check-monitor: | check-docker check-dev +### cppcheck target + +.PHONY: check-code +TARGET += check_code +ALL += check-code +HELP_build = checks code with cppcheck +check-code: | check-docker + @make --no-print-directory -C $(DOCKERDIR) format EXEC=' cppcheck $(FILES) \ + --enable=warning,performance,portability,information \ + --std=c11 \ + --verbose \ + --suppress=missingInclude \ + --template="[{severity}][{id}] {message} {callstack} (On {file}:{line})"' + +### clang-format + +.PHONY: format-code +TARGET += format-code +ALL += format-code +HELP_build = formats code with clang-format +format-code: | check-docker + @make --no-print-directory -C $(DOCKERDIR) format EXEC="clang-format-9 \ + -style=file \ + -i \ + $(FILES)" + ### dev targets ### .PHONY: dev diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..72f8890 --- /dev/null +++ b/.clang-format @@ -0,0 +1,127 @@ +--- +Language: Cpp +# BasedOnStyle: LLVM +AccessModifierOffset: 0 +AlignAfterOpenBracket: Align +AlignConsecutiveMacros: true +AlignConsecutiveAssignments: true +AlignConsecutiveDeclarations: true +AlignEscapedNewlines: Left +AlignOperands: true +AlignTrailingComments: true +AllowAllArgumentsOnNextLine: true +AllowAllConstructorInitializersOnNextLine: true +AllowAllParametersOfDeclarationOnNextLine: true +AllowShortBlocksOnASingleLine: false +AllowShortCaseLabelsOnASingleLine: false +AllowShortFunctionsOnASingleLine: All +AllowShortLambdasOnASingleLine: All +AllowShortIfStatementsOnASingleLine: Never +AllowShortLoopsOnASingleLine: false +AlwaysBreakAfterDefinitionReturnType: All +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: false +AlwaysBreakTemplateDeclarations: MultiLine +BinPackArguments: true +BinPackParameters: true +BraceWrapping: + AfterCaseLabel: true + AfterClass: true + AfterControlStatement: true + AfterEnum: true + AfterFunction: true + AfterNamespace: false + AfterObjCDeclaration: false + AfterStruct: true + AfterUnion: true + AfterExternBlock: true + BeforeCatch: false + BeforeElse: true + IndentBraces: false + SplitEmptyFunction: true + SplitEmptyRecord: true + SplitEmptyNamespace: true +BreakBeforeBinaryOperators: None +BreakBeforeBraces: Allman +BreakBeforeInheritanceComma: false +BreakInheritanceList: BeforeColon +BreakBeforeTernaryOperators: true +BreakConstructorInitializersBeforeComma: false +BreakConstructorInitializers: BeforeColon +BreakAfterJavaFieldAnnotations: false +BreakStringLiterals: true +ColumnLimit: 80 +CommentPragmas: '^ IWYU pragma:' +CompactNamespaces: false +ConstructorInitializerAllOnOneLineOrOnePerLine: false +ConstructorInitializerIndentWidth: 4 +ContinuationIndentWidth: 4 +Cpp11BracedListStyle: true +DerivePointerAlignment: false +DisableFormat: false +ExperimentalAutoDetectBinPacking: false +FixNamespaceComments: true +ForEachMacros: + - foreach + - Q_FOREACH + - BOOST_FOREACH +IncludeBlocks: Preserve +IncludeCategories: + - Regex: '^"(llvm|llvm-c|clang|clang-c)/' + Priority: 2 + - Regex: '^(<|"(gtest|gmock|isl|json)/)' + Priority: 3 + - Regex: '.*' + Priority: 1 +IncludeIsMainRegex: '(Test)?$' +IndentCaseLabels: true +IndentPPDirectives: BeforeHash +IndentWidth: 4 +IndentWrappedFunctionNames: false +JavaScriptQuotes: Leave +JavaScriptWrapImports: true +KeepEmptyLinesAtTheStartOfBlocks: false +MacroBlockBegin: '' +MacroBlockEnd: '' +MaxEmptyLinesToKeep: 1 +NamespaceIndentation: None +ObjCBinPackProtocolList: Auto +ObjCBlockIndentWidth: 4 +ObjCSpaceAfterProperty: false +ObjCSpaceBeforeProtocolList: true +PenaltyBreakAssignment: 2 +PenaltyBreakBeforeFirstCallParameter: 19 +PenaltyBreakComment: 300 +PenaltyBreakFirstLessLess: 120 +PenaltyBreakString: 1000 +PenaltyBreakTemplateDeclaration: 10 +PenaltyExcessCharacter: 1000000 +PenaltyReturnTypeOnItsOwnLine: 60 +PointerAlignment: Right +ReflowComments: true +SortIncludes: true +SortUsingDeclarations: true +SpaceAfterCStyleCast: false +SpaceAfterLogicalNot: true +SpaceAfterTemplateKeyword: true +SpaceBeforeAssignmentOperators: true +SpaceBeforeCpp11BracedList: false +SpaceBeforeCtorInitializerColon: true +SpaceBeforeInheritanceColon: true +SpaceBeforeParens: ControlStatements +SpaceBeforeRangeBasedForLoopColon: true +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 1 +SpacesInAngles: false +SpacesInContainerLiterals: true +SpacesInCStyleCastParentheses: false +SpacesInParentheses: false +SpacesInSquareBrackets: false +Standard: Cpp11 +StatementMacros: + - Q_UNUSED + - QT_REQUIRE_VERSION +TabWidth: 4 +UseTab: Never +... + diff --git a/docker/Makefile b/docker/Makefile index a57f985..5de3cbc 100644 --- a/docker/Makefile +++ b/docker/Makefile @@ -40,6 +40,17 @@ HELP_WORKDIR = Where to enter docker container WORKDIR?=$(shell pwd)/.. ### docker build targets ### +.PHONY: format-image +ALL += format-image +DEFAULT += format-image +TARGET_format += format-image +HELP_format-image = builds docker format image +format-image: | check-docker + docker build \ + --build-arg=UID=$(UID) \ + --build-arg=GID=$(GID) \ + -t "lifesensor/format" \ + format .PHONY: idf-image ALL += idf-image @@ -77,6 +88,21 @@ vscode-image: qemu-image | check-docker vscode ### docker run targets ### +.PHONY: format +TARGET += format +HELP_idf = runs formatting container +format: | format-image check-docker + docker run \ + --rm \ + -ti \ + $(DOCKER_DEV) \ + -v format-home:/home/developer:rw \ + -v "$(shell pwd)/..:$(shell pwd)/..:rw" \ + -w "$(shell pwd)/.." \ + --hostname format \ + $(DOCKEROPTS) \ + lifesensor/format \ + /bin/bash -ic '$(EXEC)' .PHONY: idf TARGET += idf @@ -134,6 +160,11 @@ code vscode: | vscode-image check-docker /bin/bash -ic '$(EXEC)' ### docker remove volume targets ### +.PHONY: clean-format +CLEAN += clean-format +HELP_clean-format = removes format volume +clean-format: + -docker volume rm format-home .PHONY: clean-idf CLEAN += clean-idf @@ -154,6 +185,12 @@ clean-vscode: -docker volume rm vscode-home ### docker remove image targets ### +.PHONY: distclean-format +DISTCLEAN += distclean-format +HELP_distclean-format = removes format docker image +distclean-format: clean-format + -docker image remove "lifesensor/format" + docker image prune .PHONY: distclean-idf DISTCLEAN += distclean-idf diff --git a/docker/format/Dockerfile b/docker/format/Dockerfile new file mode 100644 index 0000000..9d4b8fa --- /dev/null +++ b/docker/format/Dockerfile @@ -0,0 +1,14 @@ +FROM ubuntu:18.04 + +RUN apt-get update && apt-get install -y cppcheck clang-format-9 + +ARG UID=1000 +ARG GID=1000 +RUN groupadd -g $GID -o developer +RUN useradd -m -u $UID -g $GID -s /bin/bash developer +RUN echo "developer ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers + +WORKDIR /home/developer +USER developer +ENV LC_ALL=C.UTF-8 LANG=C.UTF-8 + From ed6d9a87120689e26396c37b2e338ea3f4c6eb25 Mon Sep 17 00:00:00 2001 From: Stefan Venz Date: Tue, 7 Apr 2020 11:50:20 +0200 Subject: [PATCH 2/7] makefile: Add formatting and code analysis documentation Signed-off-by: Stefan Venz --- README.md | 8 ++++++++ lifesensor/README.md | 8 ++++++++ lifesensor_test/README.md | 8 ++++++++ 3 files changed, 24 insertions(+) diff --git a/README.md b/README.md index ab23ce1..0e2e7ee 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,14 @@ This repository features multiple idf projects for different purposes. * `make monitor` 1. creates idf docker image if needed 2. starts idf docker container to connect to ESP via specified device by `make dev` + 6. Static Code analysis + * `make check-code` + 1. creates format docker image if needed + 2. starts format docker container to run `cppcheck` on all src and header files + 7. Code formatting + * `make format-code` + 1. creates format docker image if needed + 2. starts format docker container to run `clang-format-9` on all src and header files * via local esd-idf toolchain 1. Choose flavor * i.e. `cd lifesensor` diff --git a/lifesensor/README.md b/lifesensor/README.md index e34d33c..01ebce2 100644 --- a/lifesensor/README.md +++ b/lifesensor/README.md @@ -19,6 +19,14 @@ This directory contains the main build of the project. * `make monitor` 1. creates idf docker image if needed 2. starts idf docker container to connect to ESP via specified device by `make dev` + 6. Static Code analysis + * `make check-code` + 1. creates format docker image if needed + 2. starts format docker container to run `cppcheck` on all src and header files + 7. Code formatting + * `make format-code` + 1. creates format docker image if needed + 2. starts format docker container to run `clang-format-9` on all src and header files * via local esd-idf toolchain 2. Building * `idf.py build` diff --git a/lifesensor_test/README.md b/lifesensor_test/README.md index c0f86e9..806ede8 100644 --- a/lifesensor_test/README.md +++ b/lifesensor_test/README.md @@ -19,6 +19,14 @@ This directory contains a test build of the project, running various unit tests. * `make monitor` 1. creates idf docker image if needed 2. starts idf docker container to connect to ESP via specified device by `make dev` + 6. Static Code analysis + * `make check-code` + 1. creates format docker image if needed + 2. starts format docker container to run `cppcheck` on all src and header files + 7. Code formatting + * `make format-code` + 1. creates format docker image if needed + 2. starts format docker container to run `clang-format-9` on all src and header files * via local esd-idf toolchain 2. Building * `idf.py build` From 73213c114fad3844103de270402924e6417833a6 Mon Sep 17 00:00:00 2001 From: Stefan Venz Date: Fri, 10 Apr 2020 12:54:10 +0200 Subject: [PATCH 3/7] Makefile: Apply suggestions from code review Co-Authored-By: nopeslide --- .Makefile.mk | 5 ++--- docker/Makefile | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/.Makefile.mk b/.Makefile.mk index e4eb5a8..990d46a 100644 --- a/.Makefile.mk +++ b/.Makefile.mk @@ -115,7 +115,7 @@ check-monitor: | check-docker check-dev .PHONY: check-code TARGET += check_code ALL += check-code -HELP_build = checks code with cppcheck +HELP_check-code = checks code with cppcheck check-code: | check-docker @make --no-print-directory -C $(DOCKERDIR) format EXEC=' cppcheck $(FILES) \ --enable=warning,performance,portability,information \ @@ -128,8 +128,7 @@ check-code: | check-docker .PHONY: format-code TARGET += format-code -ALL += format-code -HELP_build = formats code with clang-format +HELP_format-code = formats code with clang-format format-code: | check-docker @make --no-print-directory -C $(DOCKERDIR) format EXEC="clang-format-9 \ -style=file \ diff --git a/docker/Makefile b/docker/Makefile index 9508ff5..5c44958 100644 --- a/docker/Makefile +++ b/docker/Makefile @@ -89,7 +89,7 @@ vscode-image: qemu-image | check-docker ### docker run targets ### .PHONY: format TARGET += format -HELP_idf = runs formatting container +HELP_format = runs formatting container format: | format-image check-docker docker run \ --rm \ From 9722e88e8d806abbd1ab2ba001b3c5c02836c510 Mon Sep 17 00:00:00 2001 From: Stefan Venz Date: Wed, 15 Apr 2020 18:39:14 +0200 Subject: [PATCH 4/7] makefile: move Files variable to variable section Signed-off-by: Stefan Venz --- .Makefile.mk | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.Makefile.mk b/.Makefile.mk index 990d46a..6e81415 100644 --- a/.Makefile.mk +++ b/.Makefile.mk @@ -28,12 +28,12 @@ DOCKERDIR=../docker # assume template path TEMPLATEPATH=../.Makefile.template -### default target ### -DEFAULT += help - # header and source files proccessed by format and check FILES=$(shell cd ../; find -regex '.*\.\(c\|h\)' -not -path *build*) +### default target ### +DEFAULT += help + ### menuconfig targets ### .PHONY: menuconfig From 81b53481288b5aa6661de09d8ecb2aa15d9260a4 Mon Sep 17 00:00:00 2001 From: drechsler Date: Fri, 24 Apr 2020 11:06:38 +0200 Subject: [PATCH 5/7] makefile:refactor moved clang-format find into container --- .Makefile.mk | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/.Makefile.mk b/.Makefile.mk index 6e81415..a3bb06d 100644 --- a/.Makefile.mk +++ b/.Makefile.mk @@ -28,9 +28,6 @@ DOCKERDIR=../docker # assume template path TEMPLATEPATH=../.Makefile.template -# header and source files proccessed by format and check -FILES=$(shell cd ../; find -regex '.*\.\(c\|h\)' -not -path *build*) - ### default target ### DEFAULT += help @@ -130,10 +127,12 @@ check-code: | check-docker TARGET += format-code HELP_format-code = formats code with clang-format format-code: | check-docker - @make --no-print-directory -C $(DOCKERDIR) format EXEC="clang-format-9 \ - -style=file \ - -i \ - $(FILES)" + @make --no-print-directory -C $(DOCKERDIR) format \ + EXEC="find . \ + \( -name \"*.h\" -o -name \"*.c\" \) \ + ! -path \"*build*\" \ + -printf \"formatting %h/%f\n\" \ + -exec clang-format-9 -style=file -i \"{}\" \";\" " ### vscode ### From 3541c03ed9759ae85ebc4df48e1b84f4d46afe75 Mon Sep 17 00:00:00 2001 From: drechsler Date: Fri, 24 Apr 2020 11:36:33 +0200 Subject: [PATCH 6/7] fixup! makefile: Add code formatting and checking targets --- .Makefile.mk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.Makefile.mk b/.Makefile.mk index a3bb06d..a811269 100644 --- a/.Makefile.mk +++ b/.Makefile.mk @@ -110,7 +110,7 @@ check-monitor: | check-docker check-dev ### cppcheck target .PHONY: check-code -TARGET += check_code +TARGET += check-code ALL += check-code HELP_check-code = checks code with cppcheck check-code: | check-docker From 0fa21a54f29d962ec5de6759b2002e1183cf472e Mon Sep 17 00:00:00 2001 From: drechsler Date: Fri, 24 Apr 2020 11:37:32 +0200 Subject: [PATCH 7/7] makefile:feature added target with test if code needs formatting --- .Makefile.mk | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/.Makefile.mk b/.Makefile.mk index a811269..93ab219 100644 --- a/.Makefile.mk +++ b/.Makefile.mk @@ -134,6 +134,18 @@ format-code: | check-docker -printf \"formatting %h/%f\n\" \ -exec clang-format-9 -style=file -i \"{}\" \";\" " +.PHONY: check-format +TARGET += check-format +HELP_check-format = check if formatting code with clang-format is necessary +check-format: | check-docker + @make --no-print-directory -C $(DOCKERDIR) format \ + EXEC="find . \ + \( -name \"*.h\" -o -name \"*.c\" \) \ + ! -path \"*build*\" \ + \( -exec sh -c \" clang-format-9 -style=file {} | diff -q - {} >/dev/null \" \";\" \ + -or -printf \"format required for %h/%f\n\" \) \ + | tee /tmp/format; test ! -s /tmp/format" + ### vscode ### .PHONY: vscode