Skip to contentSkip to conversation

checkpatch.pl: Add SPDX license tag check

PATCH In review Rob Herring 4 participants 4 messages Feb 1, 2018 – Feb 8, 2018 Checkpatch

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 checkpatch.pl: Add SPDX license tag check

Add SPDX license tag check based on the rules defined in Documentation/process/license-rules.rst. To summarize, SPDX license tags should be on the 1st line (or 2nd line in scripts) using the appropriate comment style for the file type.

Cc Andy Whitcroft <[email protected]>
Cc Joe Perches <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Philippe Ombredanne <[email protected]>
Cc Andrew Morton <[email protected]>

---
I didn't get around to resending once license-rules.rst landed in -next. Hopefully, this can be picked up for 4.16 so folks can start using it. SPDX tags have already become a frequent review comment.

Rob

 scripts/checkpatch.pl | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)
scripts/checkpatch.pl +27 -0
index ba03f17ff662..cf1b5a90b20a 100755
@@ -2225,6 +2225,8 @@ sub process {
 
 	my $camelcase_file_seeded = 0;
 
+	my $checklicenseline = 1;
+
 	sanitise_line_reset();
 	my $line;
 	foreach my $rawline (@rawlines) {
@@ -2416,6 +2418,7 @@ sub process {
 			} else {
 				$check = $check_orig;
 			}
+			$checklicenseline = 1;
 			next;
 		}
 
@@ -2866,6 +2869,30 @@ sub process {
 			}
 		}
 
+# check for using SPDX license tag at beginning of files
+		if ($realline == $checklicenseline) {
+			if ($realfile =~ /\.(?:sh|pl|py)/ && $rawline =~ /\[ \+]\s*\!\#/) {
+				$checklicenseline = 2;
+			} elsif ($rawline =~ /^\+/) {
+				my $comment = "";
+				if ($realfile =~ /\.(h|s|S)$/) {
+					$comment = '/*';
+				} elsif ($realfile =~ /\.(c|dts|dtsi)$/) {
+					$comment = '//';
+				} elsif ($realfile =~ /\.(sh|pl|py)$/) {
+					$comment = '#';
+				} elsif ($realfile =~ /\.rst$/) {
+					$comment = '..';
+				}
+
+				if ($comment !~ /^$/ &&
+				    $rawline !~ /^\+\Q$comment\E SPDX-License-Identifier: /) {
+					WARN("SPDX_LICENSE_TAG",
+					     "Missing or malformed SPDX-License-Identifier tag in 1st (or 2nd for scripts) line\n" . $herecurr);
+				}
+			}
+		}
+
 # check we are in a valid source file if not then ignore this hunk
 		next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
 
Signature · 1 line
2.14.1
maintainer
#1 lore raw
Quoted 12 lines · Rob Herring

Add SPDX license tag check based on the rules defined in Documentation/process/license-rules.rst. To summarize, SPDX license tags should be on the 1st line (or 2nd line in scripts) using the appropriate comment style for the file type.

Cc Andy Whitcroft <[email protected]>
Cc Joe Perches <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Philippe Ombredanne <[email protected]>
Cc Andrew Morton <[email protected]>
Quoted 4 lines

---
I didn't get around to resending once license-rules.rst landed in -next. Hopefully, this can be picked up for 4.16 so folks can start using it. SPDX tags have already become a frequent review comment.

Seems sensible enough now.
Here are some other suggestions.

scripts/checkpatch.pl

Quoted 1 line
scripts/checkpatch.pl +0 -0

[]

scripts/checkpatch.pl sub process { · line 2874

Quoted 7 lines
scripts/checkpatch.pl +3 -0
@@ -2866,6 +2869,30 @@ sub process {
 			}
 		}
 
+# check for using SPDX license tag at beginning of files
+		if ($realline == $checklicenseline) {
+			if ($realfile =~ /\.(?:sh|pl|py)/ && $rawline =~ /\[ \+]\s*\!\#/) {

There are many files with a #! shebang that do not use these filename types.

$ git grep -P --name-only '^\s*\#\!\s*/(?:bin|usr)' | \
  grep -vP
"(?:txt|rst|py|sh|pl)$" | wc -l
158

i.e.: .tc and .awk files and ~100 files without extensions

So I would add awk and tc to the $realfile test and perhaps extend this check to test if the file is not binary and executable.

Quoted 7 lines
scripts/checkpatch.pl +7 -0
+				$checklicenseline = 2;
+			} elsif ($rawline =~ /^\+/) {
+				my $comment = "";
+				if ($realfile =~ /\.(h|s|S)$/) {
+					$comment = '/*';
+				} elsif ($realfile =~ /\.(c|dts|dtsi)$/) {
+					$comment = '//';

scripts/checkpatch.pl sub process { · line 2891

Quoted 10 lines
scripts/checkpatch.pl +10 -0
+				} elsif ($realfile =~ /\.(sh|pl|py)$/) {
+					$comment = '#';
+				} elsif ($realfile =~ /\.rst$/) {
+					$comment = '..';
+				}
+
+				if ($comment !~ /^$/ &&
+				    $rawline !~ /^\+\Q$comment\E SPDX-License-Identifier: /) {
+					WARN("SPDX_LICENSE_TAG",
+					     "Missing or malformed SPDX-License-Identifier tag in 1st (or 2nd for scripts) line\n" . $herecurr);

Perhaps 'Missing ... in line $checklicense\n"

Acked-by on v1 1/1
#2 lore raw
In reply to Rob Herring
Quoted 5 lines · Rob Herring

Add SPDX license tag check based on the rules defined in Documentation/process/license-rules.rst. To summarize, SPDX license tags should be on the 1st line (or 2nd line in scripts) using the appropriate comment style for the file type.

Quoted 7 lines
Cc Andy Whitcroft <[email protected]>
Cc Joe Perches <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Philippe Ombredanne <[email protected]>
Cc Andrew Morton <[email protected]>
Acked-by on v1 1/1
#3 lore raw
In reply to Rob Herring
Quoted 5 lines · Rob Herring

Add SPDX license tag check based on the rules defined in Documentation/process/license-rules.rst. To summarize, SPDX license tags should be on the 1st line (or 2nd line in scripts) using the appropriate comment style for the file type.

Quoted 7 lines
Cc Andy Whitcroft <[email protected]>
Cc Joe Perches <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Philippe Ombredanne <[email protected]>
Cc Andrew Morton <[email protected]>

(Sorry for the late reply but I was busy with FOSDEM)