Path: blob/main/sys/contrib/openzfs/scripts/spdxcheck.pl
105240 views
#!/usr/bin/env perl12# SPDX-License-Identifier: MIT3#4# Copyright (c) 2025, Rob Norris <[email protected]>5#6# Permission is hereby granted, free of charge, to any person obtaining a copy7# of this software and associated documentation files (the "Software"), to8# deal in the Software without restriction, including without limitation the9# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or10# sell copies of the Software, and to permit persons to whom the Software is11# furnished to do so, subject to the following conditions:12#13# The above copyright notice and this permission notice shall be included in14# all copies or substantial portions of the Software.15#16# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR17# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,18# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE19# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER20# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING21# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS22# IN THE SOFTWARE.2324use 5.010;25use warnings;26use strict;2728# All files known to git are either "tagged" or "untagged". Tagged files are29# expected to have a license tag, while untagged files are expected to _not_30# have a license tag. There is no "optional" tag; all files are either "tagged"31# or "untagged".32#33# Whether or not a file is tagged or untagged is determined using the patterns34# in $tagged_patterns and $untagged_patterns and the following sequence:35#36# - if the file's full path is explicity listed in $tagged_patterns, then the37# file is tagged.38#39# - if the file's full path is explicitly listed in $untagged_patterns, then40# file is untagged.41#42# - if the filename matches a pattern in $tagged_patterns, and does not match a43# pattern in $untagged_patterns, then the file is tagged44#45# - otherwise, the file is untagged.46#47# The patterns do a simple glob-like match over the entire path relative to the48# root of the git repo (no leading /). '*' matches as anything at that point,49# across path fragments. '?' matches a single character.5051my $tagged_patterns = q(52# Compiled source files53*.c54*.h55*.S5657# Python files, eg test suite drivers, libzfs bindings58*.py59*.py.in6061# Various support scripts62*.sh63*.pl6465# Test suite66*.ksh67*.ksh.in68*.kshlib69*.kshlib.in70*.shlib7172# Test suite data files73*.run74*.cfg75*.cfg.in76*.fio77*.lua78*.zcp7980# Manpages81man/man?/*.?82man/man?/*.?.in8384# Unsuffixed programs (or generated of same)85cmd/zarcstat.in86cmd/zarcsummary87cmd/dbufstat.in88cmd/zilstat.in89cmd/zpool/zpool.d/*90etc/init.d/zfs-import.in91etc/init.d/zfs-load-key.in92etc/init.d/zfs-mount.in93etc/init.d/zfs-share.in94etc/init.d/zfs-zed.in95etc/zfs/zfs-functions.in96scripts/objtool-wrapper.in9798# Misc items that have clear licensing info but aren't easily matched,99# or are the first of a class that we aren't ready to match yet.100config/ax_code_coverage.m4101configure.ac102module/lua/README.zfs103scripts/kmodtool104tests/zfs-tests/tests/functional/inheritance/README.config105tests/zfs-tests/tests/functional/inheritance/README.state106cmd/zed/zed.d/statechange-notify.sh107);108109my $untagged_patterns = q(110# Exclude CI tooling as it's not interesting for overall project111# licensing.112.github/*113114# Everything below this has unclear licensing. Work is happening to115# identify and update them. Once one gains a tag it should be removed116# from this list.117118cmd/zed/zed.d/*.sh119cmd/zpool/zpool.d/*120121contrib/coverity/model.c122include/libzdb.h123include/os/freebsd/spl/sys/inttypes.h124include/os/freebsd/spl/sys/mode.h125include/os/freebsd/spl/sys/trace.h126include/os/freebsd/zfs/sys/trace_zfs.h127include/os/freebsd/zfs/sys/zpl.h128include/os/linux/kernel/linux/page_compat.h129lib/libspl/include/sys/string.h130lib/libzdb/libzdb.c131lib/libzpool/include/sys/trace_zfs.h132module/lua/setjmp/setjmp.S133module/lua/setjmp/setjmp_ppc.S134module/zstd/include/sparc_compat.h135module/zstd/zstd_sparc.c136tests/zfs-tests/cmd/cp_files.c137tests/zfs-tests/cmd/zed_fd_spill-zedlet.c138tests/zfs-tests/tests/functional/tmpfile/tmpfile_001_pos.c139tests/zfs-tests/tests/functional/tmpfile/tmpfile_002_pos.c140tests/zfs-tests/tests/functional/tmpfile/tmpfile_003_pos.c141tests/zfs-tests/tests/functional/tmpfile/tmpfile_test.c142143autogen.sh144contrib/bpftrace/zfs-trace.sh145contrib/pyzfs/docs/source/conf.py146contrib/pyzfs/libzfs_core/test/__init__.py147contrib/pyzfs/setup.py.in148contrib/zcp/autosnap.lua149scripts/commitcheck.sh150scripts/man-dates.sh151scripts/mancheck.sh152scripts/paxcheck.sh153scripts/zfs-helpers.sh154scripts/zfs-tests-color.sh155scripts/zfs.sh156scripts/zimport.sh157tests/zfs-tests/callbacks/zfs_failsafe.ksh158tests/zfs-tests/include/commands.cfg159tests/zfs-tests/include/tunables.cfg160tests/zfs-tests/include/zpool_script.shlib161tests/zfs-tests/tests/functional/mv_files/random_creation.ksh162);163164# For files expected to have a license tags, these are the acceptable tags by165# path. A file in one of these paths with a tag not listed here must be in the166# override list below. If the file is not in any of these paths, then167# $default_license_tags is used.168my $default_license_tags = [169'CDDL-1.0', '0BSD', 'BSD-2-Clause', 'BSD-3-Clause', 'MIT'170];171172my @path_license_tags = (173# Conventional wisdom is that the Linux SPL must be GPL2+ for174# kernel compatibility.175'module/os/linux/spl' => ['GPL-2.0-or-later'],176'include/os/linux/spl' => ['GPL-2.0-or-later'],177178# Third-party code should keep it's original license179'module/zstd/lib' => ['BSD-3-Clause OR GPL-2.0-only'],180'module/lua' => ['MIT'],181182# lua/setjmp is platform-specific code sourced from various places183'module/lua/setjmp' => $default_license_tags,184185# Some of the fletcher modules are dual-licensed186'module/zcommon/zfs_fletcher' =>187['BSD-2-Clause OR GPL-2.0-only', 'CDDL-1.0'],188189'module/icp' => ['Apache-2.0', 'CDDL-1.0'],190'contrib/icp' => ['Apache-2.0', 'CDDL-1.0'],191192# Python bindings are always Apache-2.0193'contrib/pyzfs' => ['Apache-2.0'],194);195196# This is a list of "special case" license tags that are in use in the tree,197# and the files where they occur. these exist for a variety of reasons, and198# generally should not be used for new code. If you need to bring in code that199# has a different license from the acceptable ones listed above, then you will200# also need to add it here, with rationale provided and approval given in your201# PR.202my %override_file_license_tags = (203204# SPDX have repeatedly rejected the creation of a tag for a public205# domain dedication, as not all dedications are clear and unambiguious206# in their meaning and not all jurisdictions permit relinquishing a207# copyright anyway.208#209# A reasonably common workaround appears to be to create a local210# (project-specific) identifier to convey whatever meaning the project211# wishes it to. To cover OpenZFS' use of third-party code with a212# public domain dedication, we use this custom tag.213#214# Further reading:215# https://github.com/spdx/old-wiki/blob/main/Pages/Legal%20Team/Decisions/Dealing%20with%20Public%20Domain%20within%20SPDX%20Files.md216# https://spdx.github.io/spdx-spec/v2.3/other-licensing-information-detected/217# https://cr.yp.to/spdx.html218#219'LicenseRef-OpenZFS-ThirdParty-PublicDomain' => [qw(220include/sys/skein.h221module/icp/algs/skein/skein_block.c222module/icp/algs/skein/skein.c223module/icp/algs/skein/skein_impl.h224module/icp/algs/skein/skein_iv.c225module/icp/algs/skein/skein_port.h226module/zfs/vdev_draid_rand.c227)],228229# Legacy inclusions230'Brian-Gladman-3-Clause' => [qw(231module/icp/asm-x86_64/aes/aestab.h232module/icp/asm-x86_64/aes/aesopt.h233module/icp/asm-x86_64/aes/aeskey.c234module/icp/asm-x86_64/aes/aes_amd64.S235)],236'OpenSSL-standalone' => [qw(237module/icp/asm-x86_64/aes/aes_aesni.S238)],239'LGPL-2.1-or-later' => [qw(240config/ax_code_coverage.m4241)],242243# Legacy inclusions of BSD-2-Clause files in Linux SPL.244'BSD-2-Clause' => [qw(245include/os/linux/spl/sys/debug.h246module/os/linux/spl/spl-zone.c247)],248249# Temporary overrides for things that have the wrong license for250# their path. Work is underway to understand and resolve these.251'GPL-2.0-or-later' => [qw(252include/os/freebsd/spl/sys/kstat.h253include/os/freebsd/spl/sys/sunddi.h254)],255'CDDL-1.0' => [qw(256include/os/linux/spl/sys/errno.h257include/os/linux/spl/sys/ia32/asm_linkage.h258include/os/linux/spl/sys/misc.h259include/os/linux/spl/sys/procfs_list.h260include/os/linux/spl/sys/trace.h261include/os/linux/spl/sys/trace_spl.h262include/os/linux/spl/sys/trace_taskq.h263include/os/linux/spl/sys/wmsum.h264module/os/linux/spl/spl-procfs-list.c265module/os/linux/spl/spl-trace.c266module/lua/README.zfs267)],268);269270##########271272sub setup_patterns {273my ($patterns) = @_;274275my @re;276my @files;277278for my $pat (split "\n", $patterns) {279# remove leading/trailing whitespace and comments280$pat =~ s/(:?^\s*|\s*(:?#.*)?$)//g;281# skip (now-)empty lines282next if $pat eq '';283284# if the "pattern" has no metachars, then it's a literal file285# path and gets matched a bit more strongly286unless ($pat =~ m/[?*]/) {287push @files, $pat;288next;289}290291# naive pattern to regex conversion292293# escape simple metachars294$pat =~ s/([\.\(\[])/\Q$1\E/g;295296$pat =~ s/\?/./g; # glob ? -> regex .297$pat =~ s/\*/.*/g; # glob * -> regex .*298299push @re, $pat;300}301302my $re = join '|', @re;303return (qr/^(?:$re)$/, { map { $_ => 1 } @files });304};305306my ($tagged_re, $tagged_files) = setup_patterns($tagged_patterns);307my ($untagged_re, $untagged_files) = setup_patterns($untagged_patterns);308309sub file_is_tagged {310my ($file) = @_;311312# explicitly tagged313if ($tagged_files->{$file}) {314delete $tagged_files->{$file};315return 1;316}317318# explicitly untagged319if ($untagged_files->{$file}) {320delete $untagged_files->{$file};321return 0;322}323324# must match tagged patterns and not match untagged patterns325return ($file =~ $tagged_re) && !($file =~ $untagged_re);326}327328my %override_tags = map {329my $tag = $_;330map { $_ => $tag } @{$override_file_license_tags{$_}};331} keys %override_file_license_tags;332333##########334335my $rc = 0;336337# Get a list of all files known to git. This is a crude way of avoiding any338# build artifacts that have tags embedded in them.339my @git_files = sort grep { chomp } qx(git ls-tree --name-only -r HEAD);340341# Scan all files and work out if their tags are correct.342for my $file (@git_files) {343# Ignore non-files. git can store other types of objects (submodule344# dirs, symlinks, etc) that aren't interesting for licensing.345next unless -f $file && ! -l $file;346347# Open the file, and extract its license tag. We only check the first348# 4K of each file because many of these files are large, binary, or349# both. For a typical source file that means the tag should be found350# within the first ~50 lines.351open my $fh, '<', $file or die "$0: couldn't open $file: $!\n";352my $nbytes = read $fh, my $buf, 4096;353die "$0: couldn't read $file: $!\n" if !defined $nbytes;354355my ($tag) =356$buf =~ m/\bSPDX-License-Identifier: ([A-Za-z0-9_\-\. ]+)$/smg;357358close $fh;359360# Decide if the file should have a tag at all361my $tagged = file_is_tagged($file);362363# If no license tag is wanted, there's not much left to do364if (!$tagged) {365if (defined $tag) {366# untagged file has a tag, pattern change required367say "unexpected license tag: $file";368$rc = 1;369}370next;371}372373# If a tag is required, but doesn't have one, warn and loop.374if (!defined $tag) {375say "missing license tag: $file";376$rc = 1;377next;378}379380# Determine the set of valid license tags for this file. Start with381# the defaults.382my $tags = $default_license_tags;383384if ($override_tags{$file}) {385# File has an explicit override, use it.386$tags = [delete $override_tags{$file}];387} else {388# Work through the path tag sets, taking the set with the389# most precise match. If no sets match, we fall through and390# are left with the default set.391my $matchlen = 0;392for (my $n = 0; $n < @path_license_tags; $n += 2) {393my ($path, $t) = @path_license_tags[$n,$n+1];394if (substr($file, 0, length($path)) eq $path &&395length($path) > $matchlen) {396$tags = $t;397$matchlen = length($path);398}399}400}401402# Confirm the file's tag is in the set, and warn if not.403my %tags = map { $_ => 1 } @$tags;404unless ($tags{$tag}) {405say "invalid license tag: $file";406say " (got $tag; expected: @$tags)";407$rc = 1;408next;409}410}411412##########413414# List any files explicitly listed as tagged or untagged that we didn't see.415# Likely the file was removed from the repo but not from our lists.416417for my $file (sort keys %$tagged_files) {418say "explicitly tagged file not on disk: $file";419$rc = 1;420}421for my $file (sort keys %$untagged_files) {422say "explicitly untagged file not on disk: $file";423$rc = 1;424}425for my $file (sort keys %override_tags) {426say "explicitly overridden file not on disk: $file";427$rc = 1;428}429430exit $rc;431432433