Skip to content

Commit

Permalink
[contrib][linux-kernel] Generate SPDX license identifiers (facebook#3294
Browse files Browse the repository at this point in the history
)

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: facebook#3293
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>

Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
  • Loading branch information
ojeda authored Oct 18, 2022
1 parent 99d239d commit 5c1cdba
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
27 changes: 26 additions & 1 deletion contrib/freestanding_lib/freestanding.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ def __init__(
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
Expand All @@ -446,6 +446,7 @@ def __init__(
self._undefs = undefs
self._excludes = excludes
self._seds = seds
self._spdx = spdx

def _dst_lib_file_paths(self):
"""
Expand Down Expand Up @@ -640,6 +641,27 @@ def _process_seds(self):
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):
Expand All @@ -651,6 +673,7 @@ def go(self):
self._rewrite_includes()
self._replace_xxh64_prefix()
self._process_seds()
self._process_spdx()


def parse_optional_pair(defines: [str]) -> [(str, Optional[str])]:
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -743,6 +767,7 @@ def main(name, args):
args.undefs,
args.excludes,
args.seds,
args.spdx,
).go()

if __name__ == "__main__":
Expand Down
1 change: 1 addition & 0 deletions contrib/linux-kernel/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down

0 comments on commit 5c1cdba

Please sign in to comment.