4 # Morgan Deters <mdeters@cs.nyu.edu> for CVC4
5 # Copyright (c) 2009-2014 The CVC4 Project
7 # usage: update-copyright [-m] [files/directories...]
8 # update-copyright [-h | --help]
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.
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.
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
23 # It ignores any file/directory not starting with [a-zA-Z]
24 # (so, this includes . and .., vi swaps, .git meta-info,
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.)
32 # It ignores any directory matching $excluded_directories
33 # (so, you should add here any sources imported but not covered under
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)$';
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';
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
58 $dir =~ s
,/[^/]+/*$,,;
60 if($#ARGV >= 0 && ($ARGV[0] eq '-h' || $ARGV[0] eq '--help')) {
61 open(my $SELF, $0) || die "error opening $0 for reading";
70 # whether we ONLY process files with git status "M"
73 if($#ARGV >= 0 && $ARGV[0] eq '-m') {
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;
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.
89 The directories in which to search for and change sources is:
98 die 'aborting operation' if !( $_ eq 'y' || $_ eq 'yes' || $_ eq 'Y' || $_ eq 'YES' );
100 $searchdirs[0] = 'src';
101 $searchdirs[1] = 'examples';
102 $searchdirs[2] = 'test';
107 print "Updating sources...\n";
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);
116 if($dir =~ m
,^(.*)\
/([^/]*)$,) {
117 my($dir, $file) = ($1, $2);
121 handleFile
($dir, $file);
123 handleFile
(".", $dir);
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;
142 if(m
,^(%{)?
/\
*(\
*| )\
*\
*\
*,) {
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";
152 print $OUT "/********************* */\n";
153 print $OUT "/*! \\file $file\n";
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
164 last if $line =~ /^ \*\*\s*$/;
165 if($line =~ /\*\//) {
166 $comment_stub = " ** [[ Add lengthier description here ]]\n\
167 ** \\todo document this file\n\
172 print $OUT $standard_template;
175 print $OUT $comment_stub;
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";
188 print $OUT "/********************* */\n";
189 print $OUT "/*! \\file $file\n";
191 print $OUT " ** \\verbatim\n";
192 print $OUT " ** Top authors (to current version): $authors\n";
193 print $OUT $standard_template;
195 print $OUT " ** \\brief [[ Add one-line brief description here ]]\n";
197 print $OUT " ** [[ Add lengthier description here ]]\n";
198 print $OUT " ** \\todo document this file\n";
199 print $OUT " **/\n\n";
201 if($file =~ /\.(y|yy|ypp|Y)$/) {
202 while(my $line = <$IN>) {
204 if($line =~ '\s*%{(.*)') {
208 # just in case something's weird with the file ?
209 if(!($line =~ '\s*')) {
210 print $OUT "$line\n";
216 while(my $line = <$IN>) {
221 rename($outfile, $infile) || die "can't rename working file \`$outfile' to \`$infile'";
226 print "in dir $srcdir\n";
227 opendir(my $DIR, $srcdir);
228 while(my $file = readdir $DIR) {
229 next if !($file =~ /^[a-zA-Z]/);
231 my $mode = (stat($srcdir.'/'.$file))[2];
232 my $is_directory = S_ISDIR
($mode);
234 next if $file =~ /$excluded_directories/;
235 recurse
($srcdir.'/'.$file);
237 handleFile
($srcdir, $file);
244 ### perl-indent-level: 2