Skip to contentSkip to conversation

checkpatch: Allow to pass config directory

PATCH In review Petr Vorel 2 participants 6 messages Apr 8 – Apr 13 Checkpatch

lore
Share & tools
lore

Petr Vorel

author
#0 lore raw

PATCH v3 1/2 checkpatch: Allow to pass config directory

checkpatch.pl searches for .checkpatch.conf in $HOME, $CWD/.scripts and $CWD. Allow to pass a single directory via CHECKPATCH_CONFIG_DIR environment variable. 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 add --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 help.

---
Changes in v3:
* CHECKPATCH_CONFIG_DIR can point to only single directory (':' would make it not finding the directory, Simon).
* Avoid join undef string (Simon).
* Don't use $ENV{$env_config_dir} twice (Joe).
* Pass new variables to which_conf() instead of using global.

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

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

 scripts/checkpatch.pl | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
scripts/checkpatch.pl +13 -3
index 0492d6afc9a1f..58f3d5a98204c 100755
@@ -57,6 +57,8 @@ my %ignore_type = ();
 my @ignore = ();
 my $help = 0;
 my $configuration_file = ".checkpatch.conf";
+my $def_configuration_dirs = ".:$ENV{HOME}:.scripts";
+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 +148,11 @@ Options:
   -h, --help, --version      display this help and exit
 
 When FILE is - read standard input.
+
+CONFIGURATION FILE
+Script searches for a configuration file $configuration_file in a directory
+specified by \$$env_config_dir environment variable, or
+(if variable not defined) in path: '$def_configuration_dirs'
 EOM
 
 	exit($exitcode);
@@ -237,7 +244,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 +1538,12 @@ sub which {
 }
 
 sub which_conf {
-	my ($conf) = @_;
+	my ($conf, $env_key, $paths) = @_;
+	my $env_dir = $ENV{$env_key};
+
+	return "$env_dir/$conf" if (defined($env_dir));
 
-	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 v3 2/2 checkpatch: Add option to not force /* */ for SPDX

Add option --ignore-old-toolchain to not force /* */ for SPDX.

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

---
New in v3.

I suppose you will not want to add code which should not be used on mainline. But it would help to not force other projects which vendored checkpatch.pl to neither force /* */ nor carry extra patch.

I wonder if this rule is still relevant (are these old toolchains still being used).

Kind regards,
Petr

 scripts/checkpatch.pl | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
scripts/checkpatch.pl +6 -1
index 58f3d5a98204c..ec1e9670a0c1d 100755
@@ -61,6 +61,7 @@ my $def_configuration_dirs = ".:$ENV{HOME}:.scripts";
 my $env_config_dir = 'CHECKPATCH_CONFIG_DIR';
 my $max_line_length = 100;
 my $ignore_perl_version = 0;
+my $ignore_old_toolchain = 0;
 my $minimum_perl_version = 5.10.0;
 my $min_conf_desc_length = 4;
 my $spelling_file = "$D/spelling.txt";
@@ -137,6 +138,9 @@ Options:
                              file.  It's your fault if there's no backup or git
   --ignore-perl-version      override checking of perl version.  expect
                              runtime errors.
+  --ignore-old-toolchain     don't force C++ comments (/* */) for SPDX license
+                             (required by old toolchains). NOTE: it should *not*
+                             be used for Linux mainline.
   --codespell                Use the codespell dictionary for spelling/typos
                              (default:$codespellfile)
   --codespellfile            Use this codespell dictionary
@@ -346,6 +350,7 @@ GetOptions(
 	'fix!'		=> \$fix,
 	'fix-inplace!'	=> \$fix_inplace,
 	'ignore-perl-version!' => \$ignore_perl_version,
+	'ignore-old-toolchain!' => \$ignore_old_toolchain,
 	'debug=s'	=> \%debug,
 	'test-only=s'	=> \$tst_only,
 	'codespell!'	=> \$codespell,
@@ -3812,7 +3817,7 @@ sub process {
 			} elsif ($rawline =~ /^\+/) {
 				my $comment = "";
 				if ($realfile =~ /\.(h|s|S)$/) {
-					$comment = '/*';
+					$comment = $ignore_old_toolchain ? '//' : '/*';
 				} elsif ($realfile =~ /\.(c|rs|dts|dtsi)$/) {
 					$comment = '//';
 				} elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc|yaml)$/) {
Signature · 1 line
2.53.0
#2 lore raw

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

Hi Petr,

Quoted 65 lines · Petr Vorel

Add option --ignore-old-toolchain to not force /* */ for SPDX.

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

---
New in v3.

I suppose you will not want to add code which should not be used on mainline. But it would help to not force other projects which vendored checkpatch.pl to neither force /* */ nor carry extra patch.

I wonder if this rule is still relevant (are these old toolchains still being used).

Kind regards,
Petr

 scripts/checkpatch.pl | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
scripts/checkpatch.pl +6 -2
index 58f3d5a98204c..ec1e9670a0c1d 100755
@@ -61,6 +61,7 @@ my $def_configuration_dirs = ".:$ENV{HOME}:.scripts";
 my $env_config_dir = 'CHECKPATCH_CONFIG_DIR';
 my $max_line_length = 100;
 my $ignore_perl_version = 0;
+my $ignore_old_toolchain = 0;
 my $minimum_perl_version = 5.10.0;
 my $min_conf_desc_length = 4;
 my $spelling_file = "$D/spelling.txt";
@@ -137,6 +138,9 @@ Options:
                              file.  It's your fault if there's no backup or git
   --ignore-perl-version      override checking of perl version.  expect
                              runtime errors.
+  --ignore-old-toolchain     don't force C++ comments (/* */) for SPDX license
+                             (required by old toolchains). NOTE: it should *not*
+                             be used for Linux mainline.
   --codespell                Use the codespell dictionary for spelling/typos
                              (default:$codespellfile)
   --codespellfile            Use this codespell dictionary
@@ -346,6 +350,7 @@ GetOptions(
        'fix!'          => \$fix,
        'fix-inplace!'  => \$fix_inplace,
        'ignore-perl-version!' => \$ignore_perl_version,
+       'ignore-old-toolchain!' => \$ignore_old_toolchain,
        'debug=s'       => \%debug,
        'test-only=s'   => \$tst_only,
        'codespell!'    => \$codespell,
@@ -3812,7 +3817,7 @@ sub process {
                        } elsif ($rawline =~ /^\+/) {
                                my $comment = "";
                                if ($realfile =~ /\.(h|s|S)$/) {
-                                       $comment = '/*';
+                                       $comment = $ignore_old_toolchain ? '//' : '/*';
                                } elsif ($realfile =~ /\.(c|rs|dts|dtsi)$/) {
                                        $comment = '//';
                                } elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc|yaml)$/) {
--
Quoted 2 lines

2.53.0

Is the help text swapped? To my mind /* */ is C-style.

The option name is quite generic for something that only controls SPDX comment style - how about --spdx-cxx-comments ?

Regards,
Simon

author
#3 lore raw

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

Quoted 64 lines

Hi Petr,

Quoted 60 lines · Petr Vorel

Add option --ignore-old-toolchain to not force /* */ for SPDX.

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

---
New in v3.

I suppose you will not want to add code which should not be used on mainline. But it would help to not force other projects which vendored checkpatch.pl to neither force /* */ nor carry extra patch.

I wonder if this rule is still relevant (are these old toolchains still being used).

Kind regards,
Petr

 scripts/checkpatch.pl | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
scripts/checkpatch.pl +6 -1
index 58f3d5a98204c..ec1e9670a0c1d 100755
@@ -61,6 +61,7 @@ my $def_configuration_dirs = ".:$ENV{HOME}:.scripts";
 my $env_config_dir = 'CHECKPATCH_CONFIG_DIR';
 my $max_line_length = 100;
 my $ignore_perl_version = 0;
+my $ignore_old_toolchain = 0;
 my $minimum_perl_version = 5.10.0;
 my $min_conf_desc_length = 4;
 my $spelling_file = "$D/spelling.txt";
@@ -137,6 +138,9 @@ Options:
                              file.  It's your fault if there's no backup or git
   --ignore-perl-version      override checking of perl version.  expect
                              runtime errors.
+  --ignore-old-toolchain     don't force C++ comments (/* */) for SPDX license
+                             (required by old toolchains). NOTE: it should *not*
+                             be used for Linux mainline.
   --codespell                Use the codespell dictionary for spelling/typos
                              (default:$codespellfile)
   --codespellfile            Use this codespell dictionary
@@ -346,6 +350,7 @@ GetOptions(
        'fix!'          => \$fix,
        'fix-inplace!'  => \$fix_inplace,
        'ignore-perl-version!' => \$ignore_perl_version,
+       'ignore-old-toolchain!' => \$ignore_old_toolchain,
        'debug=s'       => \%debug,
        'test-only=s'   => \$tst_only,
        'codespell!'    => \$codespell,
@@ -3812,7 +3817,7 @@ sub process {
                        } elsif ($rawline =~ /^\+/) {
                                my $comment = "";
                                if ($realfile =~ /\.(h|s|S)$/) {
-                                       $comment = '/*';
+                                       $comment = $ignore_old_toolchain ? '//' : '/*';
Quoted 8 lines
Quoted 5 lines
scripts/checkpatch.pl +0 -1
                                } elsif ($realfile =~ /\.(c|rs|dts|dtsi)$/) {
                                        $comment = '//';
                                } elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc|yaml)$/) {
--

2.53.0

Is the help text swapped? To my mind /* */ is C-style.

+1, thanks for catching this.

Quoted 2 lines

The option name is quite generic for something that only controls SPDX comment style - how about --spdx-cxx-comments ?

Good point, I'll change it in v4.

Kind regards,
Petr

Quoted 2 lines

Regards,
Simon

Reviewed-by on v3 1/2
#4 lore raw
In reply to Petr Vorel

Hi Petr,

Quoted 81 lines · Petr Vorel

checkpatch.pl searches for .checkpatch.conf in $HOME, $CWD/.scripts and $CWD. Allow to pass a single directory via CHECKPATCH_CONFIG_DIR environment variable. 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 add --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 help.

---
Changes in v3:
* CHECKPATCH_CONFIG_DIR can point to only single directory (':' would make it not finding the directory, Simon).
* Avoid join undef string (Simon).
* Don't use $ENV{$env_config_dir} twice (Joe).
* Pass new variables to which_conf() instead of using global.

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

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

 scripts/checkpatch.pl | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
scripts/checkpatch.pl +13 -4
index 0492d6afc9a1f..58f3d5a98204c 100755
@@ -57,6 +57,8 @@ my %ignore_type = ();
 my @ignore = ();
 my $help = 0;
 my $configuration_file = ".checkpatch.conf";
+my $def_configuration_dirs = ".:$ENV{HOME}:.scripts";
+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 +148,11 @@ Options:
   -h, --help, --version      display this help and exit
 
 When FILE is - read standard input.
+
+CONFIGURATION FILE
+Script searches for a configuration file $configuration_file in a directory
+specified by \$$env_config_dir environment variable, or
+(if variable not defined) in path: '$def_configuration_dirs'
 EOM
 
        exit($exitcode);
@@ -237,7 +244,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 +1538,12 @@ sub which {
 }
 
 sub which_conf {
-       my ($conf) = @_;
+       my ($conf, $env_key, $paths) = @_;
+       my $env_dir = $ENV{$env_key};
+
+       return "$env_dir/$conf" if (defined($env_dir));
 
-       foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
+       foreach my $path (split(/:/, $paths)) {
                if (-e "$path/$conf") {
                        return "$path/$conf";
                }
--
Quoted 2 lines

2.53.0

Some nits:

When CHECKPATCH_CONFIG_DIR is set but the file doesn't exist there, the default paths are silently skipped. A warning would help catch typos.

The commit message says the search order is "$HOME, $CWD/.scripts and $CWD" but the code has ".:$HOME:.scripts" (CWD first).

'Allow to pass' -> 'Allow passing'

Regards,
Simon

author
#5 lore raw
Quoted 87 lines

Hi Petr,

Quoted 80 lines · Petr Vorel

checkpatch.pl searches for .checkpatch.conf in $HOME, $CWD/.scripts and $CWD. Allow to pass a single directory via CHECKPATCH_CONFIG_DIR environment variable. 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 add --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 help.

---
Changes in v3:
* CHECKPATCH_CONFIG_DIR can point to only single directory (':' would make it not finding the directory, Simon).
* Avoid join undef string (Simon).
* Don't use $ENV{$env_config_dir} twice (Joe).
* Pass new variables to which_conf() instead of using global.

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

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

 scripts/checkpatch.pl | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
scripts/checkpatch.pl +13 -4
index 0492d6afc9a1f..58f3d5a98204c 100755
@@ -57,6 +57,8 @@ my %ignore_type = ();
 my @ignore = ();
 my $help = 0;
 my $configuration_file = ".checkpatch.conf";
+my $def_configuration_dirs = ".:$ENV{HOME}:.scripts";
+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 +148,11 @@ Options:
   -h, --help, --version      display this help and exit
 
 When FILE is - read standard input.
+
+CONFIGURATION FILE
+Script searches for a configuration file $configuration_file in a directory
+specified by \$$env_config_dir environment variable, or
+(if variable not defined) in path: '$def_configuration_dirs'
 EOM
 
        exit($exitcode);
@@ -237,7 +244,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 +1538,12 @@ sub which {
 }
 
 sub which_conf {
-       my ($conf) = @_;
+       my ($conf, $env_key, $paths) = @_;
+       my $env_dir = $ENV{$env_key};
+
+       return "$env_dir/$conf" if (defined($env_dir));
 
-       foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
+       foreach my $path (split(/:/, $paths)) {
                if (-e "$path/$conf") {
                        return "$path/$conf";
                }
--
Quoted 1 line

2.53.0

Quoted 1 line

Thank you! I'll probably send v4, but I will not dare to add your RBT due changes I will add. Thanks for a careful review!

Quoted 5 lines

Some nits:

When CHECKPATCH_CONFIG_DIR is set but the file doesn't exist there, the default paths are silently skipped. A warning would help catch typos.

I was thinking about it as well. Probably worth to add it => v4.

Quoted 2 lines

The commit message says the search order is "$HOME, $CWD/.scripts and $CWD" but the code has ".:$HOME:.scripts" (CWD first).

+1, thanks!

Quoted 1 line

'Allow to pass' -> 'Allow passing'

+1, thanks!

Kind regards,
Petr

Quoted 2 lines

Regards,
Simon