aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel P. Berrangé <berrange@redhat.com>2024-10-07 15:40:28 +0100
committerDaniel P. Berrangé <berrange@redhat.com>2025-02-28 15:37:36 +0000
commit2b96c1a4931e3b4e0294761c16759bcbb2652df4 (patch)
tree68ca0c5412ed34031df16c5985fe90d823a23d96
parentfa4d79c64dae03ffa269e42e21822453856618b7 (diff)
downloadqemu-2b96c1a4931e3b4e0294761c16759bcbb2652df4.zip
qemu-2b96c1a4931e3b4e0294761c16759bcbb2652df4.tar.gz
qemu-2b96c1a4931e3b4e0294761c16759bcbb2652df4.tar.bz2
scripts: validate SPDX license choices
We expect all new code to be contributed with the "GPL-2.0-or-later" license tag. Divergence is permitted if the new file is derived from pre-existing code under a different license, whether from elsewhere in QEMU codebase, or outside. Issue a warning if the declared license is not "GPL-2.0-or-later", and an error if the license is not one of the handful of the expected licenses to prevent unintended proliferation. The warning asks users to explain their unusual choice of license in the commit message. Reviewed-by: Peter Maydell <peter.maydell@linaro.org> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
-rwxr-xr-xscripts/checkpatch.pl69
1 files changed, 69 insertions, 0 deletions
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 01f25aa..8995d2c3 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -1353,6 +1353,70 @@ sub checkfilename {
}
}
+sub checkspdx {
+ my ($file, $expr) = @_;
+
+ # Imported Linux headers probably have SPDX tags, but if they
+ # don't we're not requiring contributors to fix this, as these
+ # files are not expected to be modified locally in QEMU.
+ # Also don't accidentally detect own checking code.
+ if ($file =~ m,include/standard-headers, ||
+ $file =~ m,linux-headers, ||
+ $file =~ m,checkpatch.pl,) {
+ return;
+ }
+
+ my $origexpr = $expr;
+
+ # Flatten sub-expressions
+ $expr =~ s/\(|\)/ /g;
+ $expr =~ s/OR|AND/ /g;
+
+ # Merge WITH exceptions to the license
+ $expr =~ s/\s+WITH\s+/-WITH-/g;
+
+ # Cull more leading/trailing whitespace
+ $expr =~ s/^\s*//g;
+ $expr =~ s/\s*$//g;
+
+ my @bits = split / +/, $expr;
+
+ my $prefer = "GPL-2.0-or-later";
+ my @valid = qw(
+ GPL-2.0-only
+ LGPL-2.1-only
+ LGPL-2.1-or-later
+ BSD-2-Clause
+ BSD-3-Clause
+ MIT
+ );
+
+ my $nonpreferred = 0;
+ my @unknown = ();
+ foreach my $bit (@bits) {
+ if ($bit eq $prefer) {
+ next;
+ }
+ if (grep /^$bit$/, @valid) {
+ $nonpreferred = 1;
+ } else {
+ push @unknown, $bit;
+ }
+ }
+ if (@unknown) {
+ ERROR("Saw unacceptable licenses '" . join(',', @unknown) .
+ "', valid choices for QEMU are:\n" . join("\n", $prefer, @valid));
+ }
+
+ if ($nonpreferred) {
+ WARN("Saw acceptable license '$origexpr' but note '$prefer' is " .
+ "preferred for new files unless the code is derived from a " .
+ "source file with an existing declared license that must be " .
+ "retained. Please explain the license choice in the commit " .
+ "message.");
+ }
+}
+
sub process {
my $filename = shift;
@@ -1645,6 +1709,11 @@ sub process {
}
}
+# Check SPDX-License-Identifier references a permitted license
+ if ($rawline =~ m,SPDX-License-Identifier: (.*?)(\*/)?\s*$,) {
+ &checkspdx($realfile, $1);
+ }
+
# Check for wrappage within a valid hunk of the file
if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
ERROR("patch seems to be corrupt (line wrapped?)\n" .