windows-once: Improve comments.
[gnulib.git] / check-copyright
blob2ff803a55249eee18149d08eb5ed8ec913bc4614
1 #!/bin/sh
3 # Copyright (C) 2011-2024 Free Software Foundation, Inc.
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <https://www.gnu.org/licenses/>.
19 # func_file_license
20 # Input:
21 # - file A file name.
22 # Output:
23 # - file_license The license mentioned in the file,
24 # or a token with '??' if unknown.
25 func_file_license ()
27 if head -n 50 $file | grep 'the GNU LGPLv3[+] or the GNU GPLv2[+]' > /dev/null \
28 || { { head -n 50 $file | grep 'under the terms of either:' > /dev/null; } \
29 && { head -n 50 $file | grep 'GNU Lesser General Public' > /dev/null; } \
30 && { head -n 50 $file | grep 'GNU General Public' > /dev/null; } \
31 && { head -n 50 $file | grep 'either version 3' > /dev/null; } \
32 && { head -n 50 $file | grep 'either version 2' > /dev/null; }; }; then
33 file_license='LGPLv3+ or GPLv2+'
34 else
35 if head -n 50 $file | grep 'GNU General Public' > /dev/null; then
36 if { head -n 50 $file | grep 'version 3 or later' > /dev/null; } \
37 || { head -n 50 $file | grep 'either version 3' > /dev/null; }; then
38 file_license='GPL' # an alias for GPLv3+
39 else
40 if { head -n 50 $file | grep 'version 2 or later' > /dev/null; } \
41 || { head -n 50 $file | grep 'either version 2' > /dev/null; }; then
42 file_license='GPLv2+'
43 else
44 file_license='GPL??'
47 else
48 if head -n 50 $file | grep 'Lesser General' > /dev/null; then
49 if { head -n 50 $file | grep 'version 3 or later' > /dev/null; } \
50 || { head -n 50 $file | grep 'either version 3' > /dev/null; }; then
51 file_license='LGPL' # an alias for LGPLv3+
52 else
53 if { head -n 50 $file | grep 'version 2 or later' > /dev/null; } \
54 || { head -n 50 $file | tr -d '\n' | grep 'version 2 of the *. *License, or' > /dev/null; } \
55 || { head -n 50 $file | tr -d '\n' | grep 'version 2\.1 of the *. *License, or' > /dev/null; }; then
56 file_license='LGPLv2+'
57 else
58 file_license='LGPL??'
61 else
62 if head -n 50 $file | grep 'unlimited permission to copy and/or distribute' > /dev/null; then
63 file_license='unlimited'
64 else
65 if head -n 50 $file | grep 'This file is in the public domain' > /dev/null; then
66 file_license='public domain'
67 else
68 file_license='??'
76 # func_module_license
77 # Input:
78 # - module A module name.
79 # Output:
80 # - module_license The license mentioned in the module.
81 func_module_license ()
83 module_license=`./gnulib-tool --extract-license $module`
84 if test "$module_license" = 'GPLv3+' || test "$module_license" = 'GPLed build tool'; then
85 module_license='GPL'
86 else
87 if test "$module_license" = 'GPLv2+ build tool'; then
88 module_license='GPLv2+'
93 # func_weaker_license license1 license2
94 # Determines the weaker among the licenses license1, license2.
95 # Input:
96 # - license1 A license.
97 # - license2 A license.
98 # Output:
99 # - weaker_license The weaker among the licenses license1, license2.
100 func_weaker_license ()
102 if test "$1" = 'public domain' || test "$2" = 'public domain'; then
103 weaker_license='public domain'
104 else
105 if test "$1" = 'unlimited' || test "$2" = 'unlimited'; then
106 weaker_license='unlimited'
107 else
108 if test "$1" = 'LGPLv2+' || test "$2" = 'LGPLv2+'; then
109 weaker_license='LGPLv2+'
110 else
111 if test "$1" = 'LGPLv3+ or GPLv2+' || test "$2" = 'LGPLv3+ or GPLv2+'; then
112 weaker_license='LGPLv3+ or GPLv2+'
113 else
114 if { test "$1" = 'LGPL' && test "$2" != 'GPLv2+'; } \
115 || { test "$2" = 'LGPL' && test "$1" != 'GPLv2+'; }; then
116 weaker_license='LGPL'
117 else
118 if { test "$1" = 'GPLv2+' && test "$2" != 'LGPL'; } \
119 || { test "$2" = 'GPLv2+' && test "$1" != 'LGPL'; }; then
120 weaker_license='GPLv2+'
121 else
122 if test "$1" = "$2"; then
123 weaker_license="$1"
124 else
125 weaker_license='<complex>'
135 # Iterate over the modules and collect the mismatch candidates.
136 candidates=
137 for module in `./gnulib-tool --list`; do
138 func_module_license
139 for file in `./gnulib-tool --extract-filelist "$module" | grep '^\(lib\|build-aux\)/'`; do
140 case $file in
141 *.class) # These are binary files, that don't contain a license notice.
144 func_file_license
145 if test "$file_license" != "$module_license"; then
146 # This pair (module, file) is a mismatch candidate.
147 case " $candidates " in
148 *" $file "*) ;;
150 candidates="$candidates $file" ;;
151 esac
154 esac
155 done
156 done
158 # Look at the candidates in more detail.
159 error=0
160 for file in $candidates; do
161 func_file_license
162 weakest_license=
163 for module in `./gnulib-tool --find "$file"`; do
164 func_module_license
165 if test -z "$weakest_license"; then
166 weakest_license="$module_license"
167 else
168 func_weaker_license "$weakest_license" "$module_license"
169 weakest_license="$weaker_license"
171 done
172 if test "$file_license" != "$weakest_license"; then
173 # The 'glob' module is a special case. It contains LGPLv2+ code (shared with
174 # glibc) but at the same time also has a dependency to a module under GPL
175 # (namely, 'fstatat'). This is not a mistake.
176 if test "$weakest_license" = GPL \
177 && case "$file" in lib/glob*) true ;; *) false ;; esac; then
179 else
180 if test $error = 0; then
181 echo "Module License File License File name"
182 echo "================= ================= ====================================="
184 printf '%-17s %-17s %s\n' "$weakest_license" "$file_license" "$file"
185 error=1
188 done
189 exit $error