]> git.ipfire.org Git - thirdparty/zstd.git/commitdiff
[contrib][linux-kernel] Generate SPDX license identifiers (#3294)
authorMiguel Ojeda <ojeda@users.noreply.github.com>
Tue, 18 Oct 2022 23:35:44 +0000 (01:35 +0200)
committerGitHub <noreply@github.com>
Tue, 18 Oct 2022 23:35:44 +0000 (16:35 -0700)
Add a `--spdx` option to the freestanding script to prefix
files with a line like (for `.c` files):

    // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause

or (for `.h` and `.S` files):

    /* SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause */

Given the style of the line to be used depends on the extension,
a simple `sed` insert command would not work.

It also skips the file if an existing SPDX line is there,
as well as raising an error if an unexpected SPDX line appears
anywhere else in the file, as well as for unexpected
file extensions.

I double-checked that all currently generated files appear
to be license as expected with:

    grep -LRF 'This source code is licensed under both the BSD-style license (found in the'  linux/lib/zstd
    grep -LRF 'LICENSE file in the root directory of this source tree) and the GPLv2 (found' linux/lib/zstd

but somebody knowledgable on the licensing of the project should
double-check this is the intended case.

Fixes: https://github.com/facebook/zstd/issues/3293
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
contrib/freestanding_lib/freestanding.py
contrib/linux-kernel/Makefile

index 4a02dea147be4c5b6335236c13f0a6d14ffb9af4..4e0a944f1441a4216658cf9d461da3db3f0d69e0 100755 (executable)
@@ -431,7 +431,7 @@ class Freestanding(object):
             external_xxhash: bool, xxh64_state: Optional[str],
             xxh64_prefix: Optional[str], rewritten_includes: [(str, str)],
             defs: [(str, Optional[str])], replaces: [(str, str)],
-            undefs: [str], excludes: [str], seds: [str],
+            undefs: [str], excludes: [str], seds: [str], spdx: bool,
     ):
         self._zstd_deps = zstd_deps
         self._mem = mem
@@ -446,6 +446,7 @@ class Freestanding(object):
         self._undefs = undefs
         self._excludes = excludes
         self._seds = seds
+        self._spdx = spdx
 
     def _dst_lib_file_paths(self):
         """
@@ -640,6 +641,27 @@ class Freestanding(object):
         for sed in self._seds:
             self._process_sed(sed)
 
+    def _process_spdx(self):
+        if not self._spdx:
+            return
+        self._log("Processing spdx")
+        SPDX_C = "// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause\n"
+        SPDX_H_S = "/* SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause */\n"
+        for filepath in self._dst_lib_file_paths():
+            file = FileLines(filepath)
+            if file.lines[0] == SPDX_C or file.lines[0] == SPDX_H_S:
+                continue
+            for line in file.lines:
+                if "SPDX-License-Identifier" in line:
+                    raise RuntimeError(f"Unexpected SPDX license identifier: {file.filename} {repr(line)}")
+            if file.filename.endswith(".c"):
+                file.lines.insert(0, SPDX_C)
+            elif file.filename.endswith(".h") or file.filename.endswith(".S"):
+                file.lines.insert(0, SPDX_H_S)
+            else:
+                raise RuntimeError(f"Unexpected file extension: {file.filename}")
+            file.write()
+
 
 
     def go(self):
@@ -651,6 +673,7 @@ class Freestanding(object):
         self._rewrite_includes()
         self._replace_xxh64_prefix()
         self._process_seds()
+        self._process_spdx()
 
 
 def parse_optional_pair(defines: [str]) -> [(str, Optional[str])]:
@@ -689,6 +712,7 @@ def main(name, args):
     parser.add_argument("--xxh64-prefix", default=None, help="Alternate XXH64 function prefix (excluding _) e.g. --xxh64-prefix=xxh64")
     parser.add_argument("--rewrite-include", default=[], dest="rewritten_includes", action="append", help="Rewrite an include REGEX=NEW (e.g. '<stddef\\.h>=<linux/types.h>')")
     parser.add_argument("--sed", default=[], dest="seds", action="append", help="Apply a sed replacement. Format: `s/REGEX/FORMAT/[g]`. REGEX is a Python regex. FORMAT is a Python format string formatted by the regex dict.")
+    parser.add_argument("--spdx", action="store_true", help="Add SPDX License Identifiers")
     parser.add_argument("-D", "--define", default=[], dest="defs", action="append", help="Pre-define this macro (can be passed multiple times)")
     parser.add_argument("-U", "--undefine", default=[], dest="undefs", action="append", help="Pre-undefine this macro (can be passed multiple times)")
     parser.add_argument("-R", "--replace", default=[], dest="replaces", action="append", help="Pre-define this macro and replace the first ifndef block with its definition")
@@ -743,6 +767,7 @@ def main(name, args):
         args.undefs,
         args.excludes,
         args.seds,
+        args.spdx,
     ).go()
 
 if __name__ == "__main__":
index 47a4317404bde66d9abef58357db13e2bfd88ab6..38aa6f4f4a0296a89b980e17b227127dd82a3463 100644 (file)
@@ -26,6 +26,7 @@ libzstd:
                --rewrite-include '"(\.\./)?zstd_errors.h"=<linux/zstd_errors.h>' \
                --sed 's,/\*\*\*,/* *,g' \
                --sed 's,/\*\*,/*,g' \
+               --spdx \
                -DZSTD_NO_INTRINSICS \
                -DZSTD_NO_UNUSED_FUNCTIONS \
                -DZSTD_LEGACY_SUPPORT=0 \