1 #! /usr/bin/env python3
3 # Copyright (C) 2011-2024 Free Software Foundation, Inc.
5 # This file is part of GDB.
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.
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.
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/>.
22 This script updates the list of years in the copyright notices in
23 most files maintained by the GDB project.
25 Usage: cd src/gdb && ./copyright.py
27 Always review the output of this script before committing it!
28 A useful command to review the output is:
29 % filterdiff -x \*.c -x \*.cc -x \*.h -x \*.exp updates.diff
30 This removes the bulk of the changes which are most likely to be correct.
40 from typing
import List
, Optional
43 def get_update_list():
44 """Return the list of files to update.
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.
59 for root
, dirs
, files
in os
.walk(gdb_dir
, topdown
=True):
61 reldirname
= "%s/%s" % (root
, dirname
)
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
68 # Prune this directory from our search list.
70 for filename
in files
:
71 relpath
= "%s/%s" % (root
, filename
)
73 filename
in EXCLUDE_ALL_LIST
74 or relpath
in EXCLUDE_LIST
75 or relpath
in NOT_FSF_LIST
81 result
.append(relpath
)
85 def update_files(update_list
):
86 """Update the copyright header of the files in the given list.
88 We use gnulib's update-copyright script for that.
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"
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
100 p
= subprocess
.Popen(
102 stdout
=subprocess
.PIPE
,
103 stderr
=subprocess
.STDOUT
,
104 encoding
=locale
.getpreferredencoding(),
106 update_out
= p
.communicate()[0]
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
)
120 for line
in update_out
:
121 if line
.endswith(warning_string
):
122 filename
= line
[:-warning_len
]
123 if may_have_copyright_notice(filename
):
126 # Unrecognized file format. !?!
130 def may_have_copyright_notice(filename
):
131 """Check that the given file does not seem to have a copyright notice.
133 The filename is relative to the root directory.
134 This function assumes that the current working directory is that root
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...
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
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
:
155 if lineno
> MAX_LINES
:
160 def get_parser() -> argparse
.ArgumentParser
:
161 """Get a command line parser."""
162 parser
= argparse
.ArgumentParser(
163 description
=__doc__
, formatter_class
=argparse
.RawDescriptionHelpFormatter
168 def 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())
176 os
.path
.isdir("gdb") and os
.path
.isfile("gnulib/import/extra/update-copyright")
178 sys
.exit("Error: This script must be called from the gdb directory.")
180 update_list
= get_update_list()
181 update_files(update_list
)
183 # Remind the user that some files need to be updated by HAND...
185 if MULTIPLE_COPYRIGHT_HEADERS
:
189 "REMINDER: Multiple copyright headers must be updated by hand:"
192 for filename
in MULTIPLE_COPYRIGHT_HEADERS
:
198 "\033[31mREMINDER: The following files must be updated by hand." "\033[0m"
200 for filename
in BY_HAND
:
204 ############################################################################
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
210 ############################################################################
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).
216 # Filenames are relative to the root directory.
218 "gdb/nat/glibc_thread_db.h",
220 "gdbsupport/Makefile.in",
221 "gnulib/doc/gendocs_template",
222 "gnulib/doc/gendocs_template_min",
225 "gnulib/Makefile.in",
228 # Files which should not be modified, either because they are
229 # generated, non-FSF, or otherwise special (e.g. license text,
230 # or test cases which must be sensitive to line numbering).
232 # Matches any file or directory name anywhere. Use with caution.
233 # This is mostly for files that can be found in multiple directories.
234 # Eg: We want all files named COPYING to be left untouched.
247 # The list of files to update by hand.
249 # Nothing at the moment :-).
252 # Files containing multiple copyright headers. This script is only
253 # fixing the first one it finds, so we need to finish the update
255 MULTIPLE_COPYRIGHT_HEADERS
= (
256 "gdb/doc/gdb.texinfo",
257 "gdb/doc/refcard.tex",
258 "gdb/syscalls/update-netbsd.sh",
261 # The list of file which have a copyright, but not held by the FSF.
262 # Filenames are relative to the root directory.
264 "gdb/exc_request.defs",
266 "gdb/testsuite/gdb.gdbtk/",
271 "sim/arm/dbg_conf.h",
272 "sim/arm/communicate.h",
274 "sim/arm/armcopro.c",
277 "sim/arm/thumbemu.c",
289 "sim/arm/communicate.c",
293 "sim/common/cgen-fpu.c",
294 "sim/common/cgen-fpu.h",
295 "sim/common/cgen-accfp.c",
297 "sim/mips/sim-main.c",
298 "sim/moxie/moxie-gdb.dts",
299 # Not a single file in sim/ppc/ appears to be copyright FSF :-(.
301 "sim/ppc/gen-support.h",
305 "sim/ppc/idecode_branch.h",
306 "sim/ppc/sim-endian.h",
309 "sim/ppc/gen-support.c",
310 "sim/ppc/gen-semantics.h",
312 "sim/ppc/sim_callbacks.h",
314 "sim/ppc/Makefile.in",
315 "sim/ppc/emul_chirp.c",
316 "sim/ppc/hw_nvram.c",
317 "sim/ppc/dc-test.01",
319 "sim/ppc/hw_eeprom.c",
325 "sim/ppc/gen-idecode.c",
327 "sim/ppc/corefile-n.h",
328 "sim/ppc/std-config.h",
329 "sim/ppc/ld-decode.h",
330 "sim/ppc/filter_filename.h",
332 "sim/ppc/pk_disklabel.c",
335 "sim/ppc/device_table.h",
338 "sim/ppc/emul_bugapi.h",
343 "sim/ppc/gen-itable.h",
344 "sim/ppc/interrupts.c",
346 "sim/ppc/emul_unix.c",
347 "sim/ppc/sim_calls.c",
348 "sim/ppc/dc-complex",
349 "sim/ppc/ld-cache.c",
350 "sim/ppc/registers.h",
351 "sim/ppc/dc-test.02",
354 "sim/ppc/registers.c",
356 "sim/ppc/emul_chirp.h",
357 "sim/ppc/hw_register.c",
359 "sim/ppc/sim-endian-n.h",
360 "sim/ppc/filter_filename.c",
362 "sim/ppc/idecode_fields.h",
363 "sim/ppc/hw_memory.c",
367 "sim/ppc/hw_trace.c",
368 "sim/ppc/emul_netbsd.h",
370 "sim/ppc/powerpc.igen",
373 "sim/ppc/gen-icache.h",
374 "sim/ppc/gen-model.h",
375 "sim/ppc/ld-cache.h",
377 "sim/ppc/corefile.h",
380 "sim/ppc/gen-model.c",
382 "sim/ppc/corefile.c",
384 "sim/ppc/gen-icache.c",
387 "sim/ppc/emul_generic.c",
390 "sim/ppc/gen-semantics.c",
391 "sim/ppc/emul_bugapi.c",
393 "sim/ppc/emul_generic.h",
396 "sim/ppc/interrupts.h",
400 "sim/ppc/device_table.c",
405 "sim/ppc/ppc-spr-table",
406 "sim/ppc/emul_unix.h",
411 "sim/ppc/ld-decode.c",
412 "sim/ppc/sim-endian.c",
413 "sim/ppc/gen-itable.c",
414 "sim/ppc/idecode_expression.h",
418 "sim/ppc/gen-idecode.h",
419 "sim/ppc/emul_netbsd.c",
423 "sim/ppc/hw_iobus.c",
425 "sim/testsuite/mips/mips32-dsp2.s",
428 if __name__
== "__main__":
429 sys
.exit(main(sys
.argv
[1:]))