Better configuration for QF_NRA
[cvc5.git] / contrib / update-copyright.pl
1 #!/usr/bin/perl -w
2 #
3 # update-copyright.pl
4 # Morgan Deters <mdeters@cs.nyu.edu> for CVC4
5 # Copyright (c) 2009-2014 The CVC4 Project
6 #
7 # usage: update-copyright [-m] [files/directories...]
8 # update-copyright [-h | --help]
9 #
10 # This script goes through a source directory rewriting the top bits of
11 # source files to match a template (inline, below). For files with no
12 # top comment, it adds a fresh one.
13 #
14 # if no files/directories are unspecified, the script scans its own
15 # parent directory's "src" directory. Since it lives in contrib/ in
16 # the CVC4 source tree, that means src/ in the CVC4 source tree.
17 #
18 # If -m is specified as the first argument, all files and directories
19 # are scanned, but only ones modified in the index or working tree
20 # are modified (i.e., those that have at least one status M in
21 # "git status -s").
22 #
23 # It ignores any file/directory not starting with [a-zA-Z]
24 # (so, this includes . and .., vi swaps, .git meta-info,
25 # .deps, etc.)
26 #
27 # It ignores any file not ending with one of:
28 # .c .cc .cpp .C .h .hh .hpp .H .y .yy .ypp .Y .l .ll .lpp .L .g
29 # [ or those with ".in" also suffixed, e.g., .cpp.in ]
30 # (so, this includes emacs ~-backups, CVS detritus, etc.)
31 #
32 # It ignores any directory matching $excluded_directories
33 # (so, you should add here any sources imported but not covered under
34 # the license.)
35 #
36
37 my $excluded_directories = '^(minisat|bvminisat|CVS|generated)$';
38 my $excluded_paths = '^(src/parser/antlr_input_imports.cpp|src/bindings/compat/.*|src/util/channel.h)$';
39
40 # Years of copyright for the template. E.g., the string
41 # "1985, 1987, 1992, 1997, 2008" or "2006-2009" or whatever.
42 my $years = '2009-2016';
43
44 my $standard_template = <<EOF;
45 ** This file is part of the CVC4 project.
46 ** Copyright (c) $years by the authors listed in the file AUTHORS
47 ** in the top-level source directory) and their institutional affiliations.
48 ** All rights reserved. See the file COPYING in the top-level source
49 ** directory for licensing information.\\endverbatim
50 EOF
51
52 ## end config ##
53
54 use strict;
55 use Fcntl ':mode';
56
57 my $dir = $0;
58 $dir =~ s,/[^/]+/*$,,;
59
60 if($#ARGV >= 0 && ($ARGV[0] eq '-h' || $ARGV[0] eq '--help')) {
61 open(my $SELF, $0) || die "error opening $0 for reading";
62 while($_ = <$SELF>) {
63 last if !/^#/;
64 print;
65 }
66 close $SELF;
67 exit;
68 }
69
70 # whether we ONLY process files with git status "M"
71 my $modonly = 0;
72
73 if($#ARGV >= 0 && $ARGV[0] eq '-m') {
74 $modonly = 1;
75 shift;
76 }
77
78 my @searchdirs = ();
79 if($#ARGV == -1) {
80 (chdir($dir."/..") && -f "src/include/cvc4_public.h") || die "can't find top-level source directory for CVC4";
81 my $pwd = `pwd`; chomp $pwd;
82
83 print <<EOF;
84 Warning: this script is dangerous. It will overwrite the header comments in your
85 source files to match the template in the script, attempting to retain file-specific
86 comments, but this isn't guaranteed. You should run this in a git working tree
87 and run "git diff" after to ensure everything was correctly rewritten.
88
89 The directories in which to search for and change sources is:
90 $pwd/src
91 $pwd/examples
92 $pwd/test
93
94 Continue? y or n:
95 EOF
96
97 $_ = <STDIN>; chomp;
98 die 'aborting operation' if !( $_ eq 'y' || $_ eq 'yes' || $_ eq 'Y' || $_ eq 'YES' );
99
100 $searchdirs[0] = 'src';
101 $searchdirs[1] = 'examples';
102 $searchdirs[2] = 'test';
103 } else {
104 @searchdirs = @ARGV;
105 }
106
107 print "Updating sources...\n";
108
109 while($#searchdirs >= 0) {
110 my $dir = shift @searchdirs;
111 my $mode = (stat($dir))[2] || warn "file or directory \`$dir' does not exist!";
112 my $is_directory = S_ISDIR($mode);
113 if($is_directory) {
114 recurse($dir);
115 } else {
116 if($dir =~ m,^(.*)\/([^/]*)$,) {
117 my($dir, $file) = ($1, $2);
118 if($dir eq "") {
119 $dir = "/";
120 }
121 handleFile($dir, $file);
122 } else {
123 handleFile(".", $dir);
124 }
125 }
126 }
127
128 sub handleFile {
129 my ($srcdir, $file) = @_;
130 return if !($file =~ /\.(c|cc|cpp|C|h|hh|hpp|H|y|yy|ypp|Y|l|ll|lpp|L|g|java)(\.in)?$/);
131 return if ($srcdir.'/'.$file) =~ /$excluded_paths/;
132 return if $modonly && `git status -s "$srcdir/$file" 2>/dev/null` !~ /^(M|.M)/;
133 print "$srcdir/$file...";
134 my $infile = $srcdir.'/'.$file;
135 my $outfile = $srcdir.'/#'.$file.'.tmp';
136 open(my $IN, $infile) || die "error opening $infile for reading";
137 open(my $OUT, '>', $outfile) || die "error opening $outfile for writing";
138 open(my $AUTHOR, "$dir/get-authors " . $infile . '|');
139 my $authors = <$AUTHOR>; chomp $authors;
140 close $AUTHOR;
141 $_ = <$IN>;
142 if(m,^(%{)?/\*(\*| )\*\*\*,) {
143 print "updating\n";
144 if($file =~ /\.(y|yy|ypp|Y)$/) {
145 print $OUT "%{/******************* */\n";
146 print $OUT "/** $file\n";
147 } elsif($file =~ /\.g$/) {
148 # avoid javadoc-style comment here; antlr complains
149 print $OUT "/* ******************* */\n";
150 print $OUT "/*! \\file $file\n";
151 } else {
152 print $OUT "/********************* */\n";
153 print $OUT "/*! \\file $file\n";
154 }
155 print $OUT " ** \\verbatim\n";
156 print $OUT " ** Top contributors (to current version):\n";
157 print $OUT " ** $authors\n";
158 my $comment_stub = "";
159 while(my $line = <$IN>) {
160 if($line =~ /\b[Cc]opyright\b/ && $line !~ /\bby the authors listed in the file AUTHORS\b/) {
161 # someone else holds this copyright
162 print $OUT $line;
163 }
164 last if $line =~ /^ \*\*\s*$/;
165 if($line =~ /\*\//) {
166 $comment_stub = " ** [[ Add lengthier description here ]]\n\
167 ** \\todo document this file\n\
168 $line";
169 last;
170 }
171 }
172 print $OUT $standard_template;
173 print $OUT " **\n";
174 if($comment_stub) {
175 print $OUT $comment_stub;
176 }
177 } else {
178 my $line = $_;
179 print "adding\n";
180 if($file =~ /\.(y|yy|ypp|Y)$/) {
181 print $OUT "%{/******************* */\n";
182 print $OUT "/*! \\file $file\n";
183 } elsif($file =~ /\.g$/) {
184 # avoid javadoc-style comment here; antlr complains
185 print $OUT "/* ******************* */\n";
186 print $OUT "/*! \\file $file\n";
187 } else {
188 print $OUT "/********************* */\n";
189 print $OUT "/*! \\file $file\n";
190 }
191 print $OUT " ** \\verbatim\n";
192 print $OUT " ** Top authors (to current version): $authors\n";
193 print $OUT $standard_template;
194 print $OUT " **\n";
195 print $OUT " ** \\brief [[ Add one-line brief description here ]]\n";
196 print $OUT " **\n";
197 print $OUT " ** [[ Add lengthier description here ]]\n";
198 print $OUT " ** \\todo document this file\n";
199 print $OUT " **/\n\n";
200 print $OUT $line;
201 if($file =~ /\.(y|yy|ypp|Y)$/) {
202 while(my $line = <$IN>) {
203 chomp $line;
204 if($line =~ '\s*%{(.*)') {
205 print $OUT "$1\n";
206 last;
207 }
208 # just in case something's weird with the file ?
209 if(!($line =~ '\s*')) {
210 print $OUT "$line\n";
211 last;
212 }
213 }
214 }
215 }
216 while(my $line = <$IN>) {
217 print $OUT $line;
218 }
219 close $IN;
220 close $OUT;
221 rename($outfile, $infile) || die "can't rename working file \`$outfile' to \`$infile'";
222 }
223
224 sub recurse {
225 my ($srcdir) = @_;
226 print "in dir $srcdir\n";
227 opendir(my $DIR, $srcdir);
228 while(my $file = readdir $DIR) {
229 next if !($file =~ /^[a-zA-Z]/);
230
231 my $mode = (stat($srcdir.'/'.$file))[2];
232 my $is_directory = S_ISDIR($mode);
233 if($is_directory) {
234 next if $file =~ /$excluded_directories/;
235 recurse($srcdir.'/'.$file);
236 } else {
237 handleFile($srcdir, $file);
238 }
239 }
240 closedir $DIR;
241 }
242
243 ### Local Variables:
244 ### perl-indent-level: 2
245 ### End: