first attempt at new build system
[cvc5.git] / contrib / update-copyright.pl
1 #!/usr/bin/perl -w
2 #
3 # This script goes through a source directory rewriting the top bits of
4 # source files to match a template (inline, below). For files with no
5 # top comment, it adds a fresh one.
6 #
7 # usage: contrib/update-copyright.pl [dirs...]
8 # if dirs... are unspecified, the script scans its own parent directory's
9 # "src" directory. Since it lives in contrib/ in the CVC4 source tree,
10 # that means src/ in the CVC4 source tree.
11 #
12 # It ignores any file/directory not starting with [a-zA-Z]
13 # (so, this includes . and .., vi swaps, .svn meta-info,
14 # .deps, etc.)
15 #
16 # It ignores any file not ending with one of:
17 # .c .cc .cpp .C .h .hh .hpp .H .y .yy .ypp .Y .l .ll .lpp .L
18 # (so, this includes emacs ~-backups, CVS detritus, etc.)
19 #
20 # It ignores any directory matching $excluded_directories
21 # (so, you should add here any sources imported but not covered under
22 # the license.)
23
24 my $excluded_directories = '^(minisat|CVS)$';
25
26 # Years of copyright for the template. E.g., the string
27 # "1985, 1987, 1992, 1997, 2008" or "2006-2009" or whatever.
28 my $years = '2009';
29
30 my $standard_template = <<EOF;
31 ** This file is part of the CVC4 prototype.
32 ** Copyright (c) $years The Analysis of Computer Systems Group (ACSys)
33 ** Courant Institute of Mathematical Sciences
34 ** New York University
35 ** See the file COPYING in the top-level source directory for licensing
36 ** information.
37 EOF
38
39 my $public_template = <<EOF;
40 ** This file is part of the CVC4 prototype.
41 ** Copyright (c) $years The Analysis of Computer Systems Group (ACSys)
42 ** Courant Institute of Mathematical Sciences
43 ** New York University
44 ** See the file COPYING in the top-level source directory for licensing
45 ** information.
46 EOF
47
48 ## end config ##
49
50 use strict;
51 use Fcntl ':mode';
52
53 my @searchdirs = ();
54 if($#ARGV == -1) {
55 my $dir = $0;
56 $dir =~ s,/[^/]+/*$,,;
57
58 (chdir($dir."/..") && -f "src/include/cvc4_expr.h") || die "can't find top-level source directory for CVC4";
59 my $pwd = `pwd`; chomp $pwd;
60
61 print <<EOF;
62 Warning: this script is dangerous. It will overwrite the header comments in your
63 source files to match the template in the script, attempting to retain file-specific
64 comments, but this isn't guaranteed. You should run this in an svn working directory
65 and run "svn diff" after to ensure everything was correctly rewritten.
66
67 The directories in which to search for and change sources is:
68 $pwd/src
69 $pwd/test
70
71 Continue? y or n:
72 EOF
73
74 $_ = <STDIN>; chomp;
75 die 'aborting operation' if !( $_ eq 'y' || $_ eq 'yes' || $_ eq 'Y' || $_ eq 'YES' );
76
77 $searchdirs[0] = 'src';
78 $searchdirs[1] = 'test';
79 } else {
80 @searchdirs = @ARGV;
81 }
82
83 print "Updating sources...\n";
84
85 recurse(shift @searchdirs) while $#searchdirs >= 0;
86
87 sub recurse {
88 my ($srcdir) = @_;
89 print "in dir $srcdir\n";
90 opendir(my $DIR, $srcdir);
91 while(my $file = readdir $DIR) {
92 next if !($file =~ /^[a-zA-Z]/);
93
94 my $mode = (stat($srcdir.'/'.$file))[2];
95 my $is_directory = S_ISDIR($mode);
96 if($is_directory) {
97 next if $file =~ /$excluded_directories/;
98 recurse($srcdir.'/'.$file);
99 } else {
100 next if !($file =~ /\.(c|cc|cpp|C|h|hh|hpp|H|y|yy|ypp|Y|l|ll|lpp|L)$/);
101 print "$srcdir/$file...";
102 my $infile = $srcdir.'/'.$file;
103 my $outfile = $srcdir.'/#'.$file.'.tmp';
104 open(my $IN, $infile) || die "error opening $infile for reading";
105 open(my $OUT, '>', $outfile) || die "error opening $outfile for writing";
106 $_ = <$IN>;
107 if(m,^(%{)?/\*\*\*\*\*,) {
108 print "updating\n";
109 if($file =~ /\.(y|yy|ypp|Y)$/) {
110 print $OUT "%{/******************* -*- C++ -*- */\n";
111 } else {
112 print $OUT "/********************* -*- C++ -*- */\n";
113 }
114 print $OUT "/** $file\n";
115 print $OUT $standard_template;
116 print $OUT " **\n";
117 while(my $line = <$IN>) {
118 last if $line =~ /^ \*\*\s*$/;
119 }
120 } else {
121 my $line = $_;
122 print "adding\n";
123 if($file =~ /\.(y|yy|ypp|Y)$/) {
124 print $OUT "%{/******************* -*- C++ -*- */\n";
125 } else {
126 print $OUT "/********************* -*- C++ -*- */\n";
127 }
128 print $OUT "/** $file\n";
129 print $OUT $standard_template;
130 print $OUT " **\n";
131 print $OUT " ** [[ Add file-specific comments here ]]\n";
132 print $OUT " **/\n\n";
133 print $OUT $line;
134 if($file =~ /\.(y|yy|ypp|Y)$/) {
135 while(my $line = <$IN>) {
136 chomp $line;
137 if($line =~ '\s*%{(.*)') {
138 print $OUT "$1\n";
139 last;
140 }
141 # just in case something's weird with the file ?
142 if(!($line =~ '\s*')) {
143 print $OUT "$line\n";
144 last;
145 }
146 }
147 }
148 }
149 while(my $line = <$IN>) {
150 print $OUT $line;
151 }
152 close $IN;
153 close $OUT;
154 rename($outfile, $infile) || die "can't rename working file \`$outfile' to \`$infile'";
155 }
156 }
157 closedir $DIR;
158 }
159
160 ### Local Variables:
161 ### perl-indent-level: 2
162 ### End: