windows_clear_solib memory leak
[deliverable/binutils-gdb.git] / gdb / copyright.py
CommitLineData
5f4def5c 1#! /usr/bin/env python3
e9bdf92c 2
b811d2c2 3# Copyright (C) 2011-2020 Free Software Foundation, Inc.
8ba098ad
JB
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
e9bdf92c
JB
20"""copyright.py
21
8ba098ad
JB
22This script updates the list of years in the copyright notices in
23most files maintained by the GDB project.
24
25Usage: cd src/gdb && python copyright.py
e9bdf92c 26
8ba098ad
JB
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.
e9bdf92c
JB
31"""
32
33import datetime
5f4def5c 34import locale
e9bdf92c
JB
35import os
36import os.path
8ba098ad 37import subprocess
5fb651f2 38import sys
8ba098ad 39
8ba098ad
JB
40
41def get_update_list():
42 """Return the list of files to update.
43
44 Assumes that the current working directory when called is the root
45 of the GDB source tree (NOT the gdb/ subdirectory!). The names of
46 the files are relative to that root directory.
e9bdf92c 47 """
8ba098ad 48 result = []
51fd4002 49 for gdb_dir in ('gdb', 'gnulib', 'sim', 'include/gdb'):
8ba098ad
JB
50 for root, dirs, files in os.walk(gdb_dir, topdown=True):
51 for dirname in dirs:
52 reldirname = "%s/%s" % (root, dirname)
53 if (dirname in EXCLUDE_ALL_LIST
54 or reldirname in EXCLUDE_LIST
55 or reldirname in NOT_FSF_LIST
56 or reldirname in BY_HAND):
57 # Prune this directory from our search list.
58 dirs.remove(dirname)
59 for filename in files:
60 relpath = "%s/%s" % (root, filename)
61 if (filename in EXCLUDE_ALL_LIST
62 or relpath in EXCLUDE_LIST
63 or relpath in NOT_FSF_LIST
64 or relpath in BY_HAND):
65 # Ignore this file.
66 pass
67 else:
68 result.append(relpath)
69 return result
70
71
72def update_files(update_list):
73 """Update the copyright header of the files in the given list.
74
75 We use gnulib's update-copyright script for that.
76 """
8ba85d85
JB
77 # We want to use year intervals in the copyright notices, and
78 # all years should be collapsed to one single year interval,
79 # even if there are "holes" in the list of years found in the
80 # original copyright notice (OK'ed by the FSF, case [gnu.org #719834]).
81 os.environ['UPDATE_COPYRIGHT_USE_INTERVALS'] = '2'
8ba098ad
JB
82
83 # Perform the update, and save the output in a string.
51fd4002 84 update_cmd = ['bash', 'gnulib/import/extra/update-copyright']
399501a5
JB
85 update_cmd += update_list
86
8ba098ad 87 p = subprocess.Popen(update_cmd, stdout=subprocess.PIPE,
5f4def5c
JB
88 stderr=subprocess.STDOUT,
89 encoding=locale.getpreferredencoding())
8ba098ad
JB
90 update_out = p.communicate()[0]
91
92 # Process the output. Typically, a lot of files do not have
93 # a copyright notice :-(. The update-copyright script prints
94 # a well defined warning when it did not find the copyright notice.
95 # For each of those, do a sanity check and see if they may in fact
96 # have one. For the files that are found not to have one, we filter
97 # the line out from the output, since there is nothing more to do,
98 # short of looking at each file and seeing which notice is appropriate.
99 # Too much work! (~4,000 files listed as of 2012-01-03).
5f4def5c 100 update_out = update_out.splitlines(keepends=False)
8ba098ad
JB
101 warning_string = ': warning: copyright statement not found'
102 warning_len = len(warning_string)
103
104 for line in update_out:
8ba098ad
JB
105 if line.endswith(warning_string):
106 filename = line[:-warning_len]
107 if may_have_copyright_notice(filename):
5f4def5c 108 print(line)
8ba098ad
JB
109 else:
110 # Unrecognized file format. !?!
5f4def5c 111 print("*** " + line)
8ba098ad
JB
112
113
114def may_have_copyright_notice(filename):
115 """Check that the given file does not seem to have a copyright notice.
116
117 The filename is relative to the root directory.
118 This function assumes that the current working directory is that root
119 directory.
e9bdf92c 120
8ba098ad
JB
121 The algorigthm is fairly crude, meaning that it might return
122 some false positives. I do not think it will return any false
123 negatives... We might improve this function to handle more
124 complex cases later...
125 """
126 # For now, it may have a copyright notice if we find the word
127 # "Copyright" at the (reasonable) start of the given file, say
128 # 50 lines...
129 MAX_LINES = 50
130
5f4def5c
JB
131 # We don't really know what encoding each file might be following,
132 # so just open the file as a byte stream. We only need to search
133 # for a pattern that should be the same regardless of encoding,
134 # so that should be good enough.
135 fd = open(filename, 'rb')
8ba098ad
JB
136
137 lineno = 1
138 for line in fd:
5f4def5c 139 if b'Copyright' in line:
8ba098ad
JB
140 return True
141 lineno += 1
142 if lineno > 50:
143 return False
144 return False
145
146
147def main ():
148 """The main subprogram."""
8ba098ad
JB
149 root_dir = os.path.dirname(os.getcwd())
150 os.chdir(root_dir)
151
51fd4002
JB
152 if not (os.path.isdir('gdb') and
153 os.path.isfile("gnulib/import/extra/update-copyright")):
5f4def5c 154 print("Error: This script must be called from the gdb directory.")
51fd4002
JB
155 sys.exit(1)
156
8ba098ad
JB
157 update_list = get_update_list()
158 update_files (update_list)
159
160 # Remind the user that some files need to be updated by HAND...
0f0c98a8
JB
161
162 if MULTIPLE_COPYRIGHT_HEADERS:
5f4def5c 163 print()
0f0c98a8
JB
164 print("\033[31m"
165 "REMINDER: Multiple copyright headers must be updated by hand:"
166 "\033[0m")
167 for filename in MULTIPLE_COPYRIGHT_HEADERS:
5f4def5c 168 print(" ", filename)
0f0c98a8 169
8ba098ad 170 if BY_HAND:
5f4def5c
JB
171 print()
172 print("\033[31mREMINDER: The following files must be updated by hand." \
173 "\033[0m")
0f0c98a8 174 for filename in BY_HAND:
5f4def5c 175 print(" ", filename)
8ba098ad
JB
176
177############################################################################
178#
179# Some constants, placed at the end because they take up a lot of room.
180# The actual value of these constants is not significant to the understanding
181# of the script.
182#
183############################################################################
e9bdf92c 184
8ba098ad
JB
185# Files which should not be modified, either because they are
186# generated, non-FSF, or otherwise special (e.g. license text,
187# or test cases which must be sensitive to line numbering).
188#
189# Filenames are relative to the root directory.
190EXCLUDE_LIST = (
125f8a3d 191 'gdb/nat/glibc_thread_db.h',
e23d4a9c 192 'gdb/CONTRIBUTE',
51fd4002 193 'gnulib/import'
8ba098ad 194)
e9bdf92c
JB
195
196# Files which should not be modified, either because they are
197# generated, non-FSF, or otherwise special (e.g. license text,
198# or test cases which must be sensitive to line numbering).
8ba098ad
JB
199#
200# Matches any file or directory name anywhere. Use with caution.
201# This is mostly for files that can be found in multiple directories.
202# Eg: We want all files named COPYING to be left untouched.
203
204EXCLUDE_ALL_LIST = (
205 "COPYING", "COPYING.LIB", "CVS", "configure", "copying.c",
206 "fdl.texi", "gpl.texi", "aclocal.m4",
207)
208
209# The list of files to update by hand.
210BY_HAND = (
1690bb24 211 # Nothing at the moment :-).
8ba098ad
JB
212)
213
3770a159
JB
214# Files containing multiple copyright headers. This script is only
215# fixing the first one it finds, so we need to finish the update
216# by hand.
217MULTIPLE_COPYRIGHT_HEADERS = (
218 "gdb/doc/gdb.texinfo",
219 "gdb/doc/refcard.tex",
bf6be9db 220 "gdb/gdbarch.sh",
3770a159
JB
221)
222
8ba098ad
JB
223# The list of file which have a copyright, but not head by the FSF.
224# Filenames are relative to the root directory.
225NOT_FSF_LIST = (
226 "gdb/exc_request.defs",
8ba098ad
JB
227 "gdb/gdbtk",
228 "gdb/testsuite/gdb.gdbtk/",
229 "sim/arm/armemu.h", "sim/arm/armos.c", "sim/arm/gdbhost.c",
230 "sim/arm/dbg_hif.h", "sim/arm/dbg_conf.h", "sim/arm/communicate.h",
231 "sim/arm/armos.h", "sim/arm/armcopro.c", "sim/arm/armemu.c",
232 "sim/arm/kid.c", "sim/arm/thumbemu.c", "sim/arm/armdefs.h",
233 "sim/arm/armopts.h", "sim/arm/dbg_cp.h", "sim/arm/dbg_rdi.h",
234 "sim/arm/parent.c", "sim/arm/armsupp.c", "sim/arm/armrdi.c",
235 "sim/arm/bag.c", "sim/arm/armvirt.c", "sim/arm/main.c", "sim/arm/bag.h",
236 "sim/arm/communicate.c", "sim/arm/gdbhost.h", "sim/arm/armfpe.h",
237 "sim/arm/arminit.c",
ab39020b
JB
238 "sim/common/cgen-fpu.c", "sim/common/cgen-fpu.h",
239 "sim/common/cgen-accfp.c",
8ba098ad 240 "sim/mips/m16run.c", "sim/mips/sim-main.c",
8ba098ad
JB
241 "sim/moxie/moxie-gdb.dts",
242 # Not a single file in sim/ppc/ appears to be copyright FSF :-(.
243 "sim/ppc/filter.h", "sim/ppc/gen-support.h", "sim/ppc/ld-insn.h",
244 "sim/ppc/hw_sem.c", "sim/ppc/hw_disk.c", "sim/ppc/idecode_branch.h",
245 "sim/ppc/sim-endian.h", "sim/ppc/table.c", "sim/ppc/hw_core.c",
246 "sim/ppc/gen-support.c", "sim/ppc/gen-semantics.h", "sim/ppc/cpu.h",
247 "sim/ppc/sim_callbacks.h", "sim/ppc/RUN", "sim/ppc/Makefile.in",
248 "sim/ppc/emul_chirp.c", "sim/ppc/hw_nvram.c", "sim/ppc/dc-test.01",
249 "sim/ppc/hw_phb.c", "sim/ppc/hw_eeprom.c", "sim/ppc/bits.h",
250 "sim/ppc/hw_vm.c", "sim/ppc/cap.h", "sim/ppc/os_emul.h",
251 "sim/ppc/options.h", "sim/ppc/gen-idecode.c", "sim/ppc/filter.c",
252 "sim/ppc/corefile-n.h", "sim/ppc/std-config.h", "sim/ppc/ld-decode.h",
253 "sim/ppc/filter_filename.h", "sim/ppc/hw_shm.c",
254 "sim/ppc/pk_disklabel.c", "sim/ppc/dc-simple", "sim/ppc/misc.h",
255 "sim/ppc/device_table.h", "sim/ppc/ld-insn.c", "sim/ppc/inline.c",
256 "sim/ppc/emul_bugapi.h", "sim/ppc/hw_cpu.h", "sim/ppc/debug.h",
257 "sim/ppc/hw_ide.c", "sim/ppc/debug.c", "sim/ppc/gen-itable.h",
258 "sim/ppc/interrupts.c", "sim/ppc/hw_glue.c", "sim/ppc/emul_unix.c",
259 "sim/ppc/sim_calls.c", "sim/ppc/dc-complex", "sim/ppc/ld-cache.c",
260 "sim/ppc/registers.h", "sim/ppc/dc-test.02", "sim/ppc/options.c",
261 "sim/ppc/igen.h", "sim/ppc/registers.c", "sim/ppc/device.h",
262 "sim/ppc/emul_chirp.h", "sim/ppc/hw_register.c", "sim/ppc/hw_init.c",
263 "sim/ppc/sim-endian-n.h", "sim/ppc/filter_filename.c",
264 "sim/ppc/bits.c", "sim/ppc/idecode_fields.h", "sim/ppc/hw_memory.c",
265 "sim/ppc/misc.c", "sim/ppc/double.c", "sim/ppc/psim.h",
266 "sim/ppc/hw_trace.c", "sim/ppc/emul_netbsd.h", "sim/ppc/psim.c",
267 "sim/ppc/ppc-instructions", "sim/ppc/tree.h", "sim/ppc/README",
268 "sim/ppc/gen-icache.h", "sim/ppc/gen-model.h", "sim/ppc/ld-cache.h",
269 "sim/ppc/mon.c", "sim/ppc/corefile.h", "sim/ppc/vm.c",
270 "sim/ppc/INSTALL", "sim/ppc/gen-model.c", "sim/ppc/hw_cpu.c",
271 "sim/ppc/corefile.c", "sim/ppc/hw_opic.c", "sim/ppc/gen-icache.c",
272 "sim/ppc/events.h", "sim/ppc/os_emul.c", "sim/ppc/emul_generic.c",
273 "sim/ppc/main.c", "sim/ppc/hw_com.c", "sim/ppc/gen-semantics.c",
274 "sim/ppc/emul_bugapi.c", "sim/ppc/device.c", "sim/ppc/emul_generic.h",
275 "sim/ppc/tree.c", "sim/ppc/mon.h", "sim/ppc/interrupts.h",
276 "sim/ppc/cap.c", "sim/ppc/cpu.c", "sim/ppc/hw_phb.h",
277 "sim/ppc/device_table.c", "sim/ppc/lf.c", "sim/ppc/lf.c",
278 "sim/ppc/dc-stupid", "sim/ppc/hw_pal.c", "sim/ppc/ppc-spr-table",
279 "sim/ppc/emul_unix.h", "sim/ppc/words.h", "sim/ppc/basics.h",
280 "sim/ppc/hw_htab.c", "sim/ppc/lf.h", "sim/ppc/ld-decode.c",
281 "sim/ppc/sim-endian.c", "sim/ppc/gen-itable.c",
282 "sim/ppc/idecode_expression.h", "sim/ppc/table.h", "sim/ppc/dgen.c",
283 "sim/ppc/events.c", "sim/ppc/gen-idecode.h", "sim/ppc/emul_netbsd.c",
284 "sim/ppc/igen.c", "sim/ppc/vm_n.h", "sim/ppc/vm.h",
285 "sim/ppc/hw_iobus.c", "sim/ppc/inline.h",
286 "sim/testsuite/sim/bfin/s21.s", "sim/testsuite/sim/mips/mips32-dsp2.s",
287)
e9bdf92c
JB
288
289if __name__ == "__main__":
8ba098ad 290 main()
e9bdf92c 291
This page took 0.714872 seconds and 4 git commands to generate.