copyright.py 12 KB

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