xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/copyright.py (revision 8b657b0747480f8989760d71343d6dd33f8d4cf9)
1#! /usr/bin/env python3
2
3# Copyright (C) 2011-2023 Free Software Foundation, Inc.
4#
5# This file is part of GDB.
6#
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 3 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20"""copyright.py
21
22This script updates the list of years in the copyright notices in
23most files maintained by the GDB project.
24
25Usage: cd src/gdb && ./copyright.py
26
27Always review the output of this script before committing it!
28A useful command to review the output is:
29    % filterdiff -x \*.c -x \*.cc -x \*.h -x \*.exp updates.diff
30This removes the bulk of the changes which are most likely to be correct.
31"""
32
33import argparse
34import datetime
35import locale
36import os
37import os.path
38import subprocess
39import sys
40from typing import List, Optional
41
42
43def get_update_list():
44    """Return the list of files to update.
45
46    Assumes that the current working directory when called is the root
47    of the GDB source tree (NOT the gdb/ subdirectory!).  The names of
48    the files are relative to that root directory.
49    """
50    result = []
51    for gdb_dir in (
52        "gdb",
53        "gdbserver",
54        "gdbsupport",
55        "gnulib",
56        "sim",
57        "include/gdb",
58    ):
59        for root, dirs, files in os.walk(gdb_dir, topdown=True):
60            for dirname in dirs:
61                reldirname = "%s/%s" % (root, dirname)
62                if (
63                    dirname in EXCLUDE_ALL_LIST
64                    or reldirname in EXCLUDE_LIST
65                    or reldirname in NOT_FSF_LIST
66                    or reldirname in BY_HAND
67                ):
68                    # Prune this directory from our search list.
69                    dirs.remove(dirname)
70            for filename in files:
71                relpath = "%s/%s" % (root, filename)
72                if (
73                    filename in EXCLUDE_ALL_LIST
74                    or relpath in EXCLUDE_LIST
75                    or relpath in NOT_FSF_LIST
76                    or relpath in BY_HAND
77                ):
78                    # Ignore this file.
79                    pass
80                else:
81                    result.append(relpath)
82    return result
83
84
85def update_files(update_list):
86    """Update the copyright header of the files in the given list.
87
88    We use gnulib's update-copyright script for that.
89    """
90    # We want to use year intervals in the copyright notices, and
91    # all years should be collapsed to one single year interval,
92    # even if there are "holes" in the list of years found in the
93    # original copyright notice (OK'ed by the FSF, case [gnu.org #719834]).
94    os.environ["UPDATE_COPYRIGHT_USE_INTERVALS"] = "2"
95
96    # Perform the update, and save the output in a string.
97    update_cmd = ["bash", "gnulib/import/extra/update-copyright"]
98    update_cmd += update_list
99
100    p = subprocess.Popen(
101        update_cmd,
102        stdout=subprocess.PIPE,
103        stderr=subprocess.STDOUT,
104        encoding=locale.getpreferredencoding(),
105    )
106    update_out = p.communicate()[0]
107
108    # Process the output.  Typically, a lot of files do not have
109    # a copyright notice :-(.  The update-copyright script prints
110    # a well defined warning when it did not find the copyright notice.
111    # For each of those, do a sanity check and see if they may in fact
112    # have one.  For the files that are found not to have one, we filter
113    # the line out from the output, since there is nothing more to do,
114    # short of looking at each file and seeing which notice is appropriate.
115    # Too much work! (~4,000 files listed as of 2012-01-03).
116    update_out = update_out.splitlines(keepends=False)
117    warning_string = ": warning: copyright statement not found"
118    warning_len = len(warning_string)
119
120    for line in update_out:
121        if line.endswith(warning_string):
122            filename = line[:-warning_len]
123            if may_have_copyright_notice(filename):
124                print(line)
125        else:
126            # Unrecognized file format. !?!
127            print("*** " + line)
128
129
130def may_have_copyright_notice(filename):
131    """Check that the given file does not seem to have a copyright notice.
132
133    The filename is relative to the root directory.
134    This function assumes that the current working directory is that root
135    directory.
136
137    The algorithm is fairly crude, meaning that it might return
138    some false positives.  I do not think it will return any false
139    negatives...  We might improve this function to handle more
140    complex cases later...
141    """
142    # For now, it may have a copyright notice if we find the word
143    # "Copyright" at the (reasonable) start of the given file, say
144    # 50 lines...
145    MAX_LINES = 50
146
147    # We don't really know what encoding each file might be following,
148    # so just open the file as a byte stream. We only need to search
149    # for a pattern that should be the same regardless of encoding,
150    # so that should be good enough.
151    with open(filename, "rb") as fd:
152        for lineno, line in enumerate(fd, start=1):
153            if b"Copyright" in line:
154                return True
155            if lineno > MAX_LINES:
156                break
157    return False
158
159
160def get_parser() -> argparse.ArgumentParser:
161    """Get a command line parser."""
162    parser = argparse.ArgumentParser(
163        description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
164    )
165    return parser
166
167
168def main(argv: List[str]) -> Optional[int]:
169    """The main subprogram."""
170    parser = get_parser()
171    _ = parser.parse_args(argv)
172    root_dir = os.path.dirname(os.getcwd())
173    os.chdir(root_dir)
174
175    if not (
176        os.path.isdir("gdb") and os.path.isfile("gnulib/import/extra/update-copyright")
177    ):
178        sys.exit("Error: This script must be called from the gdb directory.")
179
180    update_list = get_update_list()
181    update_files(update_list)
182
183    # Remind the user that some files need to be updated by HAND...
184
185    if MULTIPLE_COPYRIGHT_HEADERS:
186        print()
187        print(
188            "\033[31m"
189            "REMINDER: Multiple copyright headers must be updated by hand:"
190            "\033[0m"
191        )
192        for filename in MULTIPLE_COPYRIGHT_HEADERS:
193            print("  ", filename)
194
195    if BY_HAND:
196        print()
197        print(
198            "\033[31mREMINDER: The following files must be updated by hand." "\033[0m"
199        )
200        for filename in BY_HAND:
201            print("  ", filename)
202
203
204############################################################################
205#
206# Some constants, placed at the end because they take up a lot of room.
207# The actual value of these constants is not significant to the understanding
208# of the script.
209#
210############################################################################
211
212# Files which should not be modified, either because they are
213# generated, non-FSF, or otherwise special (e.g. license text,
214# or test cases which must be sensitive to line numbering).
215#
216# Filenames are relative to the root directory.
217EXCLUDE_LIST = (
218    "gdb/nat/glibc_thread_db.h",
219    "gdb/CONTRIBUTE",
220    "gdbsupport/Makefile.in",
221    "gnulib/import",
222    "gnulib/config.in",
223    "gnulib/Makefile.in",
224)
225
226# Files which should not be modified, either because they are
227# generated, non-FSF, or otherwise special (e.g. license text,
228# or test cases which must be sensitive to line numbering).
229#
230# Matches any file or directory name anywhere.  Use with caution.
231# This is mostly for files that can be found in multiple directories.
232# Eg: We want all files named COPYING to be left untouched.
233
234EXCLUDE_ALL_LIST = (
235    "COPYING",
236    "COPYING.LIB",
237    "CVS",
238    "configure",
239    "copying.c",
240    "fdl.texi",
241    "gpl.texi",
242    "aclocal.m4",
243)
244
245# The list of files to update by hand.
246BY_HAND = (
247    # Nothing at the moment :-).
248)
249
250# Files containing multiple copyright headers.  This script is only
251# fixing the first one it finds, so we need to finish the update
252# by hand.
253MULTIPLE_COPYRIGHT_HEADERS = (
254    "gdb/doc/gdb.texinfo",
255    "gdb/doc/refcard.tex",
256    "gdb/syscalls/update-netbsd.sh",
257)
258
259# The list of file which have a copyright, but not held by the FSF.
260# Filenames are relative to the root directory.
261NOT_FSF_LIST = (
262    "gdb/exc_request.defs",
263    "gdb/gdbtk",
264    "gdb/testsuite/gdb.gdbtk/",
265    "sim/arm/armemu.h",
266    "sim/arm/armos.c",
267    "sim/arm/gdbhost.c",
268    "sim/arm/dbg_hif.h",
269    "sim/arm/dbg_conf.h",
270    "sim/arm/communicate.h",
271    "sim/arm/armos.h",
272    "sim/arm/armcopro.c",
273    "sim/arm/armemu.c",
274    "sim/arm/kid.c",
275    "sim/arm/thumbemu.c",
276    "sim/arm/armdefs.h",
277    "sim/arm/armopts.h",
278    "sim/arm/dbg_cp.h",
279    "sim/arm/dbg_rdi.h",
280    "sim/arm/parent.c",
281    "sim/arm/armsupp.c",
282    "sim/arm/armrdi.c",
283    "sim/arm/bag.c",
284    "sim/arm/armvirt.c",
285    "sim/arm/main.c",
286    "sim/arm/bag.h",
287    "sim/arm/communicate.c",
288    "sim/arm/gdbhost.h",
289    "sim/arm/armfpe.h",
290    "sim/arm/arminit.c",
291    "sim/common/cgen-fpu.c",
292    "sim/common/cgen-fpu.h",
293    "sim/common/cgen-accfp.c",
294    "sim/mips/m16run.c",
295    "sim/mips/sim-main.c",
296    "sim/moxie/moxie-gdb.dts",
297    # Not a single file in sim/ppc/ appears to be copyright FSF :-(.
298    "sim/ppc/filter.h",
299    "sim/ppc/gen-support.h",
300    "sim/ppc/ld-insn.h",
301    "sim/ppc/hw_sem.c",
302    "sim/ppc/hw_disk.c",
303    "sim/ppc/idecode_branch.h",
304    "sim/ppc/sim-endian.h",
305    "sim/ppc/table.c",
306    "sim/ppc/hw_core.c",
307    "sim/ppc/gen-support.c",
308    "sim/ppc/gen-semantics.h",
309    "sim/ppc/cpu.h",
310    "sim/ppc/sim_callbacks.h",
311    "sim/ppc/RUN",
312    "sim/ppc/Makefile.in",
313    "sim/ppc/emul_chirp.c",
314    "sim/ppc/hw_nvram.c",
315    "sim/ppc/dc-test.01",
316    "sim/ppc/hw_phb.c",
317    "sim/ppc/hw_eeprom.c",
318    "sim/ppc/bits.h",
319    "sim/ppc/hw_vm.c",
320    "sim/ppc/cap.h",
321    "sim/ppc/os_emul.h",
322    "sim/ppc/options.h",
323    "sim/ppc/gen-idecode.c",
324    "sim/ppc/filter.c",
325    "sim/ppc/corefile-n.h",
326    "sim/ppc/std-config.h",
327    "sim/ppc/ld-decode.h",
328    "sim/ppc/filter_filename.h",
329    "sim/ppc/hw_shm.c",
330    "sim/ppc/pk_disklabel.c",
331    "sim/ppc/dc-simple",
332    "sim/ppc/misc.h",
333    "sim/ppc/device_table.h",
334    "sim/ppc/ld-insn.c",
335    "sim/ppc/inline.c",
336    "sim/ppc/emul_bugapi.h",
337    "sim/ppc/hw_cpu.h",
338    "sim/ppc/debug.h",
339    "sim/ppc/hw_ide.c",
340    "sim/ppc/debug.c",
341    "sim/ppc/gen-itable.h",
342    "sim/ppc/interrupts.c",
343    "sim/ppc/hw_glue.c",
344    "sim/ppc/emul_unix.c",
345    "sim/ppc/sim_calls.c",
346    "sim/ppc/dc-complex",
347    "sim/ppc/ld-cache.c",
348    "sim/ppc/registers.h",
349    "sim/ppc/dc-test.02",
350    "sim/ppc/options.c",
351    "sim/ppc/igen.h",
352    "sim/ppc/registers.c",
353    "sim/ppc/device.h",
354    "sim/ppc/emul_chirp.h",
355    "sim/ppc/hw_register.c",
356    "sim/ppc/hw_init.c",
357    "sim/ppc/sim-endian-n.h",
358    "sim/ppc/filter_filename.c",
359    "sim/ppc/bits.c",
360    "sim/ppc/idecode_fields.h",
361    "sim/ppc/hw_memory.c",
362    "sim/ppc/misc.c",
363    "sim/ppc/double.c",
364    "sim/ppc/psim.h",
365    "sim/ppc/hw_trace.c",
366    "sim/ppc/emul_netbsd.h",
367    "sim/ppc/psim.c",
368    "sim/ppc/powerpc.igen",
369    "sim/ppc/tree.h",
370    "sim/ppc/README",
371    "sim/ppc/gen-icache.h",
372    "sim/ppc/gen-model.h",
373    "sim/ppc/ld-cache.h",
374    "sim/ppc/mon.c",
375    "sim/ppc/corefile.h",
376    "sim/ppc/vm.c",
377    "sim/ppc/INSTALL",
378    "sim/ppc/gen-model.c",
379    "sim/ppc/hw_cpu.c",
380    "sim/ppc/corefile.c",
381    "sim/ppc/hw_opic.c",
382    "sim/ppc/gen-icache.c",
383    "sim/ppc/events.h",
384    "sim/ppc/os_emul.c",
385    "sim/ppc/emul_generic.c",
386    "sim/ppc/main.c",
387    "sim/ppc/hw_com.c",
388    "sim/ppc/gen-semantics.c",
389    "sim/ppc/emul_bugapi.c",
390    "sim/ppc/device.c",
391    "sim/ppc/emul_generic.h",
392    "sim/ppc/tree.c",
393    "sim/ppc/mon.h",
394    "sim/ppc/interrupts.h",
395    "sim/ppc/cap.c",
396    "sim/ppc/cpu.c",
397    "sim/ppc/hw_phb.h",
398    "sim/ppc/device_table.c",
399    "sim/ppc/lf.c",
400    "sim/ppc/lf.c",
401    "sim/ppc/dc-stupid",
402    "sim/ppc/hw_pal.c",
403    "sim/ppc/ppc-spr-table",
404    "sim/ppc/emul_unix.h",
405    "sim/ppc/words.h",
406    "sim/ppc/basics.h",
407    "sim/ppc/hw_htab.c",
408    "sim/ppc/lf.h",
409    "sim/ppc/ld-decode.c",
410    "sim/ppc/sim-endian.c",
411    "sim/ppc/gen-itable.c",
412    "sim/ppc/idecode_expression.h",
413    "sim/ppc/table.h",
414    "sim/ppc/dgen.c",
415    "sim/ppc/events.c",
416    "sim/ppc/gen-idecode.h",
417    "sim/ppc/emul_netbsd.c",
418    "sim/ppc/igen.c",
419    "sim/ppc/vm_n.h",
420    "sim/ppc/vm.h",
421    "sim/ppc/hw_iobus.c",
422    "sim/ppc/inline.h",
423    "sim/testsuite/mips/mips32-dsp2.s",
424)
425
426if __name__ == "__main__":
427    sys.exit(main(sys.argv[1:]))
428