Skip to contentSkip to conversation

checkpatch.pl: Add SPDX license tag check

PATCH Superseded Rob Herring 3 participants 5 messages Dec 20, 2017 – Dec 21, 2017 Checkpatch

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

PATCH v4 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]>

---
Thomas, if you are inclined and Joe is happy with this, can you add this on top of your series adding license-rules.rst.

v4:
- Reference license-rules.rst
- Add comment style checks based on file types
- Check .rst files

v3:
- Since we specify that the tag goes on the 1st or 2nd line, the logic can be greatly simplified compared to v2 because we can just use the line number. And now the check is improved too.

 scripts/checkpatch.pl | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
scripts/checkpatch.pl +25 -0
index 31031f10fe56..0324f845011d 100755
@@ -2866,6 +2866,31 @@ sub process {
 			}
 		}
 
+# check for using SPDX license tag at beginning of files
+		if ($rawline =~ /^\+/ && !($realline == 1 && $rawline =~ /^[\s\+]#!/)) {
+			my $ln = 1;
+			my $comment = "";
+
+			if ($realfile =~ /\.(h|s|S)$/) {
+				$comment = '/\*';
+			} elsif ($realfile =~ /\.(c|dts|dtsi)$/) {
+				$comment = '//';
+			} elsif ($realfile =~ /\.(sh|pl|py)$/) {
+				if ($prevrawline =~ /^[\s\+]#!/) {
+					$ln = 2;
+				}
+				$comment = '#';
+			} elsif ($realfile =~ /\.rst$/) {
+				$comment = '..';
+			}
+
+			if ($comment !~ /^$/ &&
+			    ($realline == $ln xor $rawline =~ m@^\+$comment 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

scripts/checkpatch.pl

Quoted 29 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]>

---
Thomas, if you are inclined and Joe is happy with this, can you add this on top of your series adding license-rules.rst.

v4:
- Reference license-rules.rst
- Add comment style checks based on file types
- Check .rst files

v3:
- Since we specify that the tag goes on the 1st or 2nd line, the logic can be greatly simplified compared to v2 because we can just use the line number. And now the check is improved too.

 scripts/checkpatch.pl | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
scripts/checkpatch.pl +0 -0

[]

scripts/checkpatch.pl sub process { · line 2870

Quoted 6 lines
scripts/checkpatch.pl +2 -0
@@ -2866,6 +2866,31 @@ sub process {
 			}
 		}
 
+# check for using SPDX license tag at beginning of files
+		if ($rawline =~ /^\+/ && !($realline == 1 && $rawline =~ /^[\s\+]#!/)) {

This test will enter this block for every added line of the patch.

Needs to be /^[ \+]/ and not [\t\+] and probably should just be ^\+

I'd probably have something like

	my $checklicenseline = 1;

at the start of sub process

and use something

		if ($realline == $checklicenseline) {
			if ($realfile =~ /\.(?:sh|pl|py)/ && $rawline =~ /\[ \+]\s*\!\#/) {
				$checklicenseline = 2;
			} elsif (etc...) {
			}
		}

scripts/checkpatch.pl sub process { · line 2884

Quoted 2 lines
scripts/checkpatch.pl +2 -0
+			} elsif ($realfile =~ /\.rst$/) {
+				$comment = '..';

\.\.

What about .txt, .json, .cocci, and .awk ?

Reviewed-by on v4 1/1
#4 lore raw
In reply to Rob Herring

Rob,

Quoted 65 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]>

---
Thomas, if you are inclined and Joe is happy with this, can you add this on top of your series adding license-rules.rst.

v4:
- Reference license-rules.rst
- Add comment style checks based on file types
- Check .rst files

v3:
- Since we specify that the tag goes on the 1st or 2nd line, the logic can be greatly simplified compared to v2 because we can just use the line number. And now the check is improved too.

 scripts/checkpatch.pl | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
scripts/checkpatch.pl +25 -1
index 31031f10fe56..0324f845011d 100755
@@ -2866,6 +2866,31 @@ sub process {
                        }
                }
 
+# check for using SPDX license tag at beginning of files
+               if ($rawline =~ /^\+/ && !($realline == 1 && $rawline =~ /^[\s\+]#!/)) {
+                       my $ln = 1;
+                       my $comment = "";
+
+                       if ($realfile =~ /\.(h|s|S)$/) {
+                               $comment = '/\*';
+                       } elsif ($realfile =~ /\.(c|dts|dtsi)$/) {
+                               $comment = '//';
+                       } elsif ($realfile =~ /\.(sh|pl|py)$/) {
+                               if ($prevrawline =~ /^[\s\+]#!/) {
+                                       $ln = 2;
+                               }
+                               $comment = '#';
+                       } elsif ($realfile =~ /\.rst$/) {
+                               $comment = '..';
+                       }
+
+                       if ($comment !~ /^$/ &&
+                           ($realline == $ln xor $rawline =~ m@^\+$comment 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)$/);
 
--
Quoted 2 lines

2.14.1

My Perl is terribly rusty. But heck this is checkpatch.pl, not checkpatch.py ;) This looks good to me though.

FWIW I maintain a comprehensive license expression parser and boolean minimizer that could be a nice addition but is likely overkill even for deeper checks.

Instead, in the future what we could add to checkpatch.pl could be some simple table lookup to ensure that the actual expression is a known one since we have a finite number of licenses in the kernel.

[1] https://github.com/nexB/license-expression/

Signature · 2 lines
Cordially
Philippe Ombredanne
#2 lore raw
In reply to Joe Perches
Quoted 47 lines · Joe Perches

scripts/checkpatch.pl

Quoted 29 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]>

---
Thomas, if you are inclined and Joe is happy with this, can you add this on top of your series adding license-rules.rst.

v4:
- Reference license-rules.rst
- Add comment style checks based on file types
- Check .rst files

v3:
- Since we specify that the tag goes on the 1st or 2nd line, the logic can be greatly simplified compared to v2 because we can just use the line number. And now the check is improved too.

 scripts/checkpatch.pl | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
scripts/checkpatch.pl +0 -0

[]

scripts/checkpatch.pl sub process { · line 2870

Quoted 6 lines
scripts/checkpatch.pl +2 -0
@@ -2866,6 +2866,31 @@ sub process {
 			}
 		}
 
+# check for using SPDX license tag at beginning of files
+		if ($rawline =~ /^\+/ && !($realline == 1 && $rawline =~ /^[\s\+]#!/)) {

This test will enter this block for every added line of the patch.

Needs to be /^[ \+]/ and not [\t\+] and probably should just be ^\+

I'd probably have something like

	my $checklicenseline = 1;

at the start of sub process

Quoted 8 lines

and use something

		if ($realline == $checklicenseline) {
			if ($realfile =~ /\.(?:sh|pl|py)/ && $rawline =~ /\[ \+]\s*\!\#/) {
				$checklicenseline = 2;
			} elsif (etc...) {
			}
		}

Okay, here's what I've ended up with:

		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 !~ m@^\+$comment SPDX-License-Identifier: @) {
					WARN("SPDX_LICENSE_TAG",
					     "Missing or malformed SPDX-License-Identifier tag in 1st (or 2nd for scripts) line\n" . 
$herecurr);
				}
			}
		}
Quoted 7 lines

scripts/checkpatch.pl sub process { · line 2884

Quoted 3 lines
scripts/checkpatch.pl +2 -0
+			} elsif ($realfile =~ /\.rst$/) {
+				$comment = '..';

\.\.

What about .txt, .json, .cocci, and .awk ?

What about them? They aren't documented in license-rules.rst and I'm just implementing what's documented as you said I should on v3.

Rob

maintainer
#3 lore raw
Quoted 17 lines · Rob Herring

Okay, here's what I've ended up with:

		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 = '\.\.';
				}
Quoted 8 lines
				if ($comment !~ /^$/ &&
				    $rawline !~ m@^\+$comment SPDX-License-Identifier: @) {
					WARN("SPDX_LICENSE_TAG",
					     "Missing or malformed SPDX-License-Identifier tag in 1st (or 2nd for scripts) line\n" . 
$herecurr);
				}
			}
		}

Seems sensible enough.

Maybe it's better to use \Q$comment\E and a consistent style on comment and rawline

Any checkpatch patch for license style requirements should not be applied until after Documentation/license-rules.rst is in -next.