summaryrefslogtreecommitdiff
path: root/check-copyright
blob: bfd42fe1f2b8917377a26ff60029bfaa4b9b65e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/bin/sh
#
# Copyright (C) 2011-2023 Free Software Foundation, Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
#

# func_file_license
# Input:
# - file          A file name.
# Output:
# - file_license  The license mentioned in the file,
#                 or a token with '??' if unknown.
func_file_license ()
{
  if head -n 50 $file | grep 'the GNU LGPLv3[+] or the GNU GPLv2[+]' > /dev/null \
     || { { head -n 50 $file | grep 'under the terms of either:' > /dev/null; } \
          && { head -n 50 $file | grep 'GNU Lesser General Public' > /dev/null; } \
          && { head -n 50 $file | grep 'GNU General Public' > /dev/null; } \
          && { head -n 50 $file | grep 'either version 3' > /dev/null; } \
          && { head -n 50 $file | grep 'either version 2' > /dev/null; }; }; then
    file_license='LGPLv3+ or GPLv2+'
  else
    if head -n 50 $file | grep 'GNU General Public' > /dev/null; then
      if { head -n 50 $file | grep 'version 3 or later' > /dev/null; } \
         || { head -n 50 $file | grep 'either version 3' > /dev/null; }; then
        file_license='GPL' # an alias for GPLv3+
      else
        if { head -n 50 $file | grep 'version 2 or later' > /dev/null; } \
           || { head -n 50 $file | grep 'either version 2' > /dev/null; }; then
          file_license='GPLv2+'
        else
          file_license='GPL??'
        fi
      fi
    else
      if head -n 50 $file | grep 'Lesser General' > /dev/null; then
        if { head -n 50 $file | grep 'version 3 or later' > /dev/null; } \
           || { head -n 50 $file | grep 'either version 3' > /dev/null; }; then
          file_license='LGPL' # an alias for LGPLv3+
        else
          if { head -n 50 $file | grep 'version 2 or later' > /dev/null; } \
             || { head -n 50 $file | tr -d '\n' | grep 'version 2 of the *. *License, or' > /dev/null; } \
             || { head -n 50 $file | tr -d '\n' | grep 'version 2\.1 of the *. *License, or' > /dev/null; }; then
            file_license='LGPLv2+'
          else
            file_license='LGPL??'
          fi
        fi
      else
        if head -n 50 $file | grep 'unlimited permission to copy and/or distribute' > /dev/null; then
          file_license='unlimited'
        else
          if head -n 50 $file | grep 'This file is in the public domain' > /dev/null; then
            file_license='public domain'
          else
            file_license='??'
          fi
        fi
      fi
    fi
  fi
}

# func_module_license
# Input:
# - module          A module name.
# Output:
# - module_license  The license mentioned in the module.
func_module_license ()
{
  module_license=`./gnulib-tool --extract-license $module`
  if test "$module_license" = 'GPLv3+' || test "$module_license" = 'GPLed build tool'; then
    module_license='GPL'
  else
    if test "$module_license" = 'GPLv2+ build tool'; then
      module_license='GPLv2+'
    fi
  fi
}

# func_weaker_license license1 license2
# Determines the weaker among the licenses license1, license2.
# Input:
# - license1        A license.
# - license2        A license.
# Output:
# - weaker_license  The weaker among the licenses license1, license2.
func_weaker_license ()
{
  if test "$1" = 'public domain' || test "$2" = 'public domain'; then
    weaker_license='public domain'
  else
    if test "$1" = 'unlimited' || test "$2" = 'unlimited'; then
      weaker_license='unlimited'
    else
      if test "$1" = 'LGPLv2+' || test "$2" = 'LGPLv2+'; then
        weaker_license='LGPLv2+'
      else
        if test "$1" = 'LGPLv3+ or GPLv2+' || test "$2" = 'LGPLv3+ or GPLv2+'; then
          weaker_license='LGPLv3+ or GPLv2+'
        else
          if { test "$1" = 'LGPL' && test "$2" != 'GPLv2+'; } \
             || { test "$2" = 'LGPL' && test "$1" != 'GPLv2+'; }; then
            weaker_license='LGPL'
          else
            if { test "$1" = 'GPLv2+' && test "$2" != 'LGPL'; } \
               || { test "$2" = 'GPLv2+' && test "$1" != 'LGPL'; }; then
              weaker_license='GPLv2+'
            else
              if test "$1" = "$2"; then
                weaker_license="$1"
              else
                weaker_license='<complex>'
              fi
            fi
          fi
        fi
      fi
    fi
  fi
}

# Iterate over the modules and collect the mismatch candidates.
candidates=
for module in `./gnulib-tool --list`; do
  func_module_license
  for file in `./gnulib-tool --extract-filelist "$module" | grep '^\(lib\|build-aux\)/'`; do
    case $file in
      *.class) # These are binary files, that don't contain a license notice.
        ;;
      *)
        func_file_license
        if test "$file_license" != "$module_license"; then
          # This pair (module, file) is a mismatch candidate.
          case " $candidates " in
            *" $file "*) ;;
            *)
               candidates="$candidates $file" ;;
          esac
        fi
        ;;
    esac
  done
done

# Look at the candidates in more detail.
error=0
for file in $candidates; do
  func_file_license
  weakest_license=
  for module in `./gnulib-tool --find "$file"`; do
    func_module_license
    if test -z "$weakest_license"; then
      weakest_license="$module_license"
    else
      func_weaker_license "$weakest_license" "$module_license"
      weakest_license="$weaker_license"
    fi
  done
  if test "$file_license" != "$weakest_license"; then
    # The 'glob' module is a special case. It contains LGPLv2+ code (shared with
    # glibc) but at the same time also has a dependency to a module under GPL
    # (namely, 'fstatat'). This is not a mistake.
    if test "$weakest_license" = GPL \
       && case "$file" in lib/glob*) true ;; *) false ;; esac; then
      :
    else
      if test $error = 0; then
        echo "Module License    File License      File name"
        echo "================= ================= ====================================="
      fi
      printf '%-17s %-17s %s\n' "$weakest_license" "$file_license" "$file"
      error=1
    fi
  fi
done
exit $error