Skip to contentSkip to conversation

checkpatch: Allow passing config directory

PATCH Applied Petr Vorel 3 participants 5 messages Apr 21 – Apr 22

Subsystems (4)
lore
Share & tools
lore

Petr Vorel

author
#0 lore raw

PATCH v8 1/2 checkpatch: Allow passing config directory

checkpatch.pl searches for .checkpatch.conf in $CWD, $HOME and $CWD/.scripts. Allow passing a single directory via CHECKPATCH_CONFIG_DIR environment variable (empty value is ignored). This allows to directly use project configuration file for projects which vendored checkpatch.pl (e.g. LTP or u-boot).

Although it'd be more convenient for user to have --conf-dir option (instead of using environment variable), code would get ugly because options from the configuration file needs to be read before processing command line options with Getopt::Long.

While at it, document directories and environment variable in -h help and HTML doc.

---
Changes in v8 (sashiko):
* Remove unintentional trailing space in regexp.

Link to v7:
https://lore.kernel.org/lkml/[email protected]/

Link to v6:
https://lore.kernel.org/lkml/[email protected]/

Link to v5:
https://lore.kernel.org/lkml/[email protected]/

Link to v4:
https://lore.kernel.org/lkml/[email protected]/

Link to v3:
https://lore.kernel.org/lkml/[email protected]/

Link to v2:
https://lore.kernel.org/lkml/[email protected]/

Link to v1:
https://lore.kernel.org/lkml/[email protected]/

 Documentation/dev-tools/checkpatch.rst |  7 +++++++
 scripts/checkpatch.pl                  | 20 +++++++++++++++++---
 2 files changed, 24 insertions(+), 3 deletions(-)
Documentation/dev-tools/checkpatch.rst +7 -0
index dccede68698ca..010dfcd615afc 100644
@@ -210,6 +210,13 @@ Available options:
 
    Display the help text.
 
+Configuration file
+==================
+
+Default configuration options can be stored in ``.checkpatch.conf``, search
+path: ``.:$HOME:.scripts`` or in a directory specified by ``$CHECKPATCH_CONFIG_DIR``
+environment variable (falling back to the default search path).
+
 Message Levels
 ==============
 
scripts/checkpatch.pl +17 -3
index 0492d6afc9a1f..8b44b3a6a4dd3 100755
@@ -57,6 +57,9 @@ my %ignore_type = ();
 my @ignore = ();
 my $help = 0;
 my $configuration_file = ".checkpatch.conf";
+my $def_configuration_dirs_help = '.:$HOME:.scripts';
+(my $def_configuration_dirs = $def_configuration_dirs_help) =~ s/\$(\w+)/$ENV{$1}/g;
+my $env_config_dir = 'CHECKPATCH_CONFIG_DIR';
 my $max_line_length = 100;
 my $ignore_perl_version = 0;
 my $minimum_perl_version = 5.10.0;
@@ -146,6 +149,11 @@ Options:
   -h, --help, --version      display this help and exit
 
 When FILE is - read standard input.
+
+CONFIGURATION FILE
+Default configuration options can be stored in $configuration_file,
+search path: '$def_configuration_dirs_help' or in a directory specified by
+\$$env_config_dir environment variable (fallback to the default search path).
 EOM
 
 	exit($exitcode);
@@ -237,7 +245,7 @@ sub list_types {
 	exit($exitcode);
 }
 
-my $conf = which_conf($configuration_file);
+my $conf = which_conf($configuration_file, $env_config_dir, $def_configuration_dirs);
 if (-f $conf) {
 	my @conf_args;
 	open(my $conffile, '<', "$conf")
@@ -1531,9 +1539,15 @@ sub which {
 }
 
 sub which_conf {
-	my ($conf) = @_;
+	my ($conf, $env_key, $paths) = @_;
+	my $env_dir = $ENV{$env_key};
+
+	if (defined($env_dir) && $env_dir ne "") {
+		return "$env_dir/$conf" if (-e "$env_dir/$conf");
+		warn "$P: Can't find a readable $conf in '$env_dir', falling back to default search paths\n";
+	}
 
-	foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
+	foreach my $path (split(/:/, $paths)) {
 		if (-e "$path/$conf") {
 			return "$path/$conf";
 		}
Signature · 1 line
2.53.0
author
#1 lore raw

PATCH v8 2/2 checkpatch: Add option to not force /* */ for SPDX

Add option --spdx-cxx-comments to not force C comments (/* */) for SPDX, but allow also C++ comments (//).

As documented in aa19a176df95d6, this is required for some old toolchains still have older assembler tools which cannot handle C++ style comments. This avoids forcing this for projects which vendored checkpatch.pl (e.g. LTP or u-boot).

---
The same as in v6 and v7.

Link to v7:
https://lore.kernel.org/lkml/[email protected]/

Link to v6:
https://lore.kernel.org/lkml/[email protected]/

Link to v5:
https://lore.kernel.org/lkml/[email protected]/

Link to v4:
https://lore.kernel.org/lkml/[email protected]/

Link to v3:
https://lore.kernel.org/lkml/[email protected]/

 Documentation/dev-tools/checkpatch.rst |  7 +++++++
 scripts/checkpatch.pl                  | 23 ++++++++++++++++++-----
 2 files changed, 25 insertions(+), 5 deletions(-)
Documentation/dev-tools/checkpatch.rst +7 -0
index 010dfcd615afc..6139a08c34cd8 100644
@@ -184,6 +184,13 @@ Available options:
    Override checking of perl version.  Runtime errors may be encountered after
    enabling this flag if the perl version does not meet the minimum specified.
 
+ - --spdx-cxx-comments
+
+   Don't force C comments ``/* */`` for SPDX license (required by old
+   toolchains), allow also C++ comments ``//``.
+
+   NOTE: it should *not* be used for Linux mainline.
+
  - --codespell
 
    Use the codespell dictionary for checking spelling errors.
scripts/checkpatch.pl +18 -5
index 8b44b3a6a4dd3..0d18771f1b013 100755
@@ -62,6 +62,7 @@ my $def_configuration_dirs_help = '.:$HOME:.scripts';
 my $env_config_dir = 'CHECKPATCH_CONFIG_DIR';
 my $max_line_length = 100;
 my $ignore_perl_version = 0;
+my $spdx_cxx_comments = 0;
 my $minimum_perl_version = 5.10.0;
 my $min_conf_desc_length = 4;
 my $spelling_file = "$D/spelling.txt";
@@ -138,6 +139,10 @@ Options:
                              file.  It's your fault if there's no backup or git
   --ignore-perl-version      override checking of perl version.  expect
                              runtime errors.
+  --spdx-cxx-comments        don't force C comments (/* */) for SPDX license
+                             (required by old toolchains), allow also C++
+                             comments (//).
+                             NOTE: it should *not* be used for Linux mainline.
   --codespell                Use the codespell dictionary for spelling/typos
                              (default:$codespellfile)
   --codespellfile            Use this codespell dictionary
@@ -347,6 +352,7 @@ GetOptions(
 	'fix!'		=> \$fix,
 	'fix-inplace!'	=> \$fix_inplace,
 	'ignore-perl-version!' => \$ignore_perl_version,
+	'spdx-cxx-comments!' => \$spdx_cxx_comments,
 	'debug=s'	=> \%debug,
 	'test-only=s'	=> \$tst_only,
 	'codespell!'	=> \$codespell,
@@ -3815,26 +3821,33 @@ sub process {
 				$checklicenseline = 2;
 			} elsif ($rawline =~ /^\+/) {
 				my $comment = "";
-				if ($realfile =~ /\.(h|s|S)$/) {
-					$comment = '/*';
-				} elsif ($realfile =~ /\.(c|rs|dts|dtsi)$/) {
+				if ($realfile =~ /\.(c|rs|dts|dtsi)$/) {
 					$comment = '//';
 				} elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc|yaml)$/) {
 					$comment = '#';
 				} elsif ($realfile =~ /\.rst$/) {
 					$comment = '..';
 				}
+				my $pattern = qr{\Q$comment\E};
+				if ($realfile =~ /\.(h|s|S)$/) {
+					$comment = '/*';
+					$pattern = qr{/\*};
+					if ($spdx_cxx_comments) {
+						$comment = '// or /*';
+						$pattern = qr{//|/\*};
+					}
+				}
 
 # check SPDX comment style for .[chsS] files
 				if ($realfile =~ /\.[chsS]$/ &&
 				    $rawline =~ /SPDX-License-Identifier:/ &&
-				    $rawline !~ m@^\+\s*\Q$comment\E\s*@) {
+				    $rawline !~ m@^\+\s*$pattern\s*@) {
 					WARN("SPDX_LICENSE_TAG",
 					     "Improper SPDX comment style for '$realfile', please use '$comment' instead\n" . $herecurr);
 				}
 
 				if ($comment !~ /^$/ &&
-				    $rawline !~ m@^\+\Q$comment\E SPDX-License-Identifier: @) {
+				    $rawline !~ m@^\+$pattern SPDX-License-Identifier: @) {
 					WARN("SPDX_LICENSE_TAG",
 					     "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr);
 				} elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) {
Signature · 1 line
2.53.0
maintainer Acked-by on v8 2/2
#2 lore raw

Re: [PATCH v8 2/2] checkpatch: Add option to not force /* */ for SPDX

Quoted 10 lines · Petr Vorel

Add option --spdx-cxx-comments to not force C comments (/* */) for SPDX, but allow also C++ comments (//).

As documented in aa19a176df95d6, this is required for some old toolchains still have older assembler tools which cannot handle C++ style comments. This avoids forcing this for projects which vendored checkpatch.pl (e.g. LTP or u-boot).

Quoted 110 lines

---
The same as in v6 and v7.

Link to v7:
[https://lore.kernel.org/lkml/[email protected]/](https://lore.kernel.org/lkml/[email protected]/)

Link to v6:
[https://lore.kernel.org/lkml/[email protected]/](https://lore.kernel.org/lkml/[email protected]/)

Link to v5:
[https://lore.kernel.org/lkml/[email protected]/](https://lore.kernel.org/lkml/[email protected]/)

Link to v4:
[https://lore.kernel.org/lkml/[email protected]/](https://lore.kernel.org/lkml/[email protected]/)

Link to v3:
[https://lore.kernel.org/lkml/[email protected]/](https://lore.kernel.org/lkml/[email protected]/)

 Documentation/dev-tools/checkpatch.rst |  7 +++++++
 scripts/checkpatch.pl                  | 23 ++++++++++++++++++-----
 2 files changed, 25 insertions(+), 5 deletions(-)
Documentation/dev-tools/checkpatch.rst +7 -0
index 010dfcd615afc..6139a08c34cd8 100644
@@ -184,6 +184,13 @@ Available options:
    Override checking of perl version.  Runtime errors may be encountered after
    enabling this flag if the perl version does not meet the minimum specified.
 
+ - --spdx-cxx-comments
+
+   Don't force C comments ``/* */`` for SPDX license (required by old
+   toolchains), allow also C++ comments ``//``.
+
+   NOTE: it should *not* be used for Linux mainline.
+
  - --codespell
 
    Use the codespell dictionary for checking spelling errors.
scripts/checkpatch.pl +18 -5
index 8b44b3a6a4dd3..0d18771f1b013 100755
@@ -62,6 +62,7 @@ my $def_configuration_dirs_help = '.:$HOME:.scripts';
 my $env_config_dir = 'CHECKPATCH_CONFIG_DIR';
 my $max_line_length = 100;
 my $ignore_perl_version = 0;
+my $spdx_cxx_comments = 0;
 my $minimum_perl_version = 5.10.0;
 my $min_conf_desc_length = 4;
 my $spelling_file = "$D/spelling.txt";
@@ -138,6 +139,10 @@ Options:
                              file.  It's your fault if there's no backup or git
   --ignore-perl-version      override checking of perl version.  expect
                              runtime errors.
+  --spdx-cxx-comments        don't force C comments (/* */) for SPDX license
+                             (required by old toolchains), allow also C++
+                             comments (//).
+                             NOTE: it should *not* be used for Linux mainline.
   --codespell                Use the codespell dictionary for spelling/typos
                              (default:$codespellfile)
   --codespellfile            Use this codespell dictionary
@@ -347,6 +352,7 @@ GetOptions(
 	'fix!'		=> \$fix,
 	'fix-inplace!'	=> \$fix_inplace,
 	'ignore-perl-version!' => \$ignore_perl_version,
+	'spdx-cxx-comments!' => \$spdx_cxx_comments,
 	'debug=s'	=> \%debug,
 	'test-only=s'	=> \$tst_only,
 	'codespell!'	=> \$codespell,
@@ -3815,26 +3821,33 @@ sub process {
 				$checklicenseline = 2;
 			} elsif ($rawline =~ /^\+/) {
 				my $comment = "";
-				if ($realfile =~ /\.(h|s|S)$/) {
-					$comment = '/*';
-				} elsif ($realfile =~ /\.(c|rs|dts|dtsi)$/) {
+				if ($realfile =~ /\.(c|rs|dts|dtsi)$/) {
 					$comment = '//';
 				} elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc|yaml)$/) {
 					$comment = '#';
 				} elsif ($realfile =~ /\.rst$/) {
 					$comment = '..';
 				}
+				my $pattern = qr{\Q$comment\E};
+				if ($realfile =~ /\.(h|s|S)$/) {
+					$comment = '/*';
+					$pattern = qr{/\*};
+					if ($spdx_cxx_comments) {
+						$comment = '// or /*';
+						$pattern = qr{//|/\*};
+					}
+				}
 
 # check SPDX comment style for .[chsS] files
 				if ($realfile =~ /\.[chsS]$/ &&
 				    $rawline =~ /SPDX-License-Identifier:/ &&
-				    $rawline !~ m@^\+\s*\Q$comment\E\s*@) {
+				    $rawline !~ m@^\+\s*$pattern\s*@) {
 					WARN("SPDX_LICENSE_TAG",
 					     "Improper SPDX comment style for '$realfile', please use '$comment' instead\n" . $herecurr);
 				}
 
 				if ($comment !~ /^$/ &&
-				    $rawline !~ m@^\+\Q$comment\E SPDX-License-Identifier: @) {
+				    $rawline !~ m@^\+$pattern SPDX-License-Identifier: @) {
 					WARN("SPDX_LICENSE_TAG",
 					     "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr);
 				} elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) {
Reviewed-by on v8 1/2
#3 lore raw
In reply to Petr Vorel
Quoted 38 lines · Petr Vorel

checkpatch.pl searches for .checkpatch.conf in $CWD, $HOME and $CWD/.scripts. Allow passing a single directory via CHECKPATCH_CONFIG_DIR environment variable (empty value is ignored). This allows to directly use project configuration file for projects which vendored checkpatch.pl (e.g. LTP or u-boot).

Although it'd be more convenient for user to have --conf-dir option (instead of using environment variable), code would get ugly because options from the configuration file needs to be read before processing command line options with Getopt::Long.

While at it, document directories and environment variable in -h help and HTML doc.

---
Changes in v8 (sashiko):
* Remove unintentional trailing space in regexp.

Link to v7:
https://lore.kernel.org/lkml/[email protected]/

Link to v6:
https://lore.kernel.org/lkml/[email protected]/

Link to v5:
https://lore.kernel.org/lkml/[email protected]/

Link to v4:
https://lore.kernel.org/lkml/[email protected]/

Link to v3:
https://lore.kernel.org/lkml/[email protected]/

Link to v2:
https://lore.kernel.org/lkml/[email protected]/

Quoted 7 lines

Link to v1:
https://lore.kernel.org/lkml/[email protected]/

 Documentation/dev-tools/checkpatch.rst |  7 +++++++
 scripts/checkpatch.pl                  | 20 +++++++++++++++++---
 2 files changed, 24 insertions(+), 3 deletions(-)
maintainer Acked-by on v8 1/2
#4 lore raw
In reply to Petr Vorel
Quoted 11 lines · Petr Vorel

checkpatch.pl searches for .checkpatch.conf in $CWD, $HOME and $CWD/.scripts. Allow passing a single directory via CHECKPATCH_CONFIG_DIR environment variable (empty value is ignored). This allows to directly use project configuration file for projects which vendored checkpatch.pl (e.g. LTP or u-boot).

Although it'd be more convenient for user to have --conf-dir option (instead of using environment variable), code would get ugly because options from the configuration file needs to be read before processing command line options with Getopt::Long.

Quoted 4 lines

While at it, document directories and environment variable in -h help and HTML doc.

Quoted 101 lines

---
Changes in v8 (sashiko):
* Remove unintentional trailing space in regexp.

Link to v7:
[https://lore.kernel.org/lkml/[email protected]/](https://lore.kernel.org/lkml/[email protected]/)

Link to v6:
[https://lore.kernel.org/lkml/[email protected]/](https://lore.kernel.org/lkml/[email protected]/)

Link to v5:
[https://lore.kernel.org/lkml/[email protected]/](https://lore.kernel.org/lkml/[email protected]/)

Link to v4:
[https://lore.kernel.org/lkml/[email protected]/](https://lore.kernel.org/lkml/[email protected]/)

Link to v3:
[https://lore.kernel.org/lkml/[email protected]/](https://lore.kernel.org/lkml/[email protected]/)

Link to v2:
[https://lore.kernel.org/lkml/[email protected]/](https://lore.kernel.org/lkml/[email protected]/)

Link to v1:
[https://lore.kernel.org/lkml/[email protected]/](https://lore.kernel.org/lkml/[email protected]/)

 Documentation/dev-tools/checkpatch.rst |  7 +++++++
 scripts/checkpatch.pl                  | 20 +++++++++++++++++---
 2 files changed, 24 insertions(+), 3 deletions(-)
Documentation/dev-tools/checkpatch.rst +7 -0
index dccede68698ca..010dfcd615afc 100644
@@ -210,6 +210,13 @@ Available options:
 
    Display the help text.
 
+Configuration file
+==================
+
+Default configuration options can be stored in ``.checkpatch.conf``, search
+path: ``.:$HOME:.scripts`` or in a directory specified by ``$CHECKPATCH_CONFIG_DIR``
+environment variable (falling back to the default search path).
+
 Message Levels
 ==============
 
scripts/checkpatch.pl +17 -3
index 0492d6afc9a1f..8b44b3a6a4dd3 100755
@@ -57,6 +57,9 @@ my %ignore_type = ();
 my @ignore = ();
 my $help = 0;
 my $configuration_file = ".checkpatch.conf";
+my $def_configuration_dirs_help = '.:$HOME:.scripts';
+(my $def_configuration_dirs = $def_configuration_dirs_help) =~ s/\$(\w+)/$ENV{$1}/g;
+my $env_config_dir = 'CHECKPATCH_CONFIG_DIR';
 my $max_line_length = 100;
 my $ignore_perl_version = 0;
 my $minimum_perl_version = 5.10.0;
@@ -146,6 +149,11 @@ Options:
   -h, --help, --version      display this help and exit
 
 When FILE is - read standard input.
+
+CONFIGURATION FILE
+Default configuration options can be stored in $configuration_file,
+search path: '$def_configuration_dirs_help' or in a directory specified by
+\$$env_config_dir environment variable (fallback to the default search path).
 EOM
 
 	exit($exitcode);
@@ -237,7 +245,7 @@ sub list_types {
 	exit($exitcode);
 }
 
-my $conf = which_conf($configuration_file);
+my $conf = which_conf($configuration_file, $env_config_dir, $def_configuration_dirs);
 if (-f $conf) {
 	my @conf_args;
 	open(my $conffile, '<', "$conf")
@@ -1531,9 +1539,15 @@ sub which {
 }
 
 sub which_conf {
-	my ($conf) = @_;
+	my ($conf, $env_key, $paths) = @_;
+	my $env_dir = $ENV{$env_key};
+
+	if (defined($env_dir) && $env_dir ne "") {
+		return "$env_dir/$conf" if (-e "$env_dir/$conf");
+		warn "$P: Can't find a readable $conf in '$env_dir', falling back to default search paths\n";
+	}
 
-	foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
+	foreach my $path (split(/:/, $paths)) {
 		if (-e "$path/$conf") {
 			return "$path/$conf";
 		}