]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - make/run-cc.pl
...because every now and again, i have to do a massive commit.
[user/henk/code/inspircd.git] / make / run-cc.pl
1 #!/usr/bin/perl
2
3 #       +------------------------------------+
4 #       | Inspire Internet Relay Chat Daemon |
5 #       +------------------------------------+
6 #
7 #  InspIRCd: (C) 2002-2010 InspIRCd Development Team
8 # See: http://wiki.inspircd.org/Credits
9 #
10 # This program is free but copyrighted software; see
11 #      the file COPYING for details.
12 #
13 # ---------------------------------------------------
14
15 ### THIS IS DESIGNED TO BE RUN BY MAKE! DO NOT RUN FROM THE SHELL (because it MIGHT sigterm the shell)! ###
16
17 use strict;
18 use warnings FATAL => qw(all);
19
20 use POSIX ();
21
22 # Runs the compiler, passing it the given arguments.
23 # Filters select output from the compiler's standard error channel and
24 # can take different actions as a result.
25
26 # NOTE: this is *NOT* a hash (sadly: a hash would stringize all the regexes and thus render them useless, plus you can't index a hash based on regexes anyway)
27 # even though we use the => in it.
28
29 # The subs are passed the message, and anything the regex captured.
30
31 my $cc = shift(@ARGV);
32
33 my $showncmdline = 0;
34
35 # GCC's "location of error stuff", which accumulates the "In file included from" include stack
36 my $location = "";
37
38 my @msgfilters = (
39         [ qr/^(.*) warning: cannot pass objects of non-POD type `(.*)' through `\.\.\.'; call will abort at runtime/ => sub {
40                 my ($msg, $where, $type) = @_;
41                 my $errstr = $location . "$where error: cannot pass objects of non-POD type `$type' through `...'\n";
42                 $location = "";
43                 if ($type =~ m/::(basic_)?string/) {
44                         $errstr .= "$where (Did you forget to call c_str()?)\n";
45                 }
46                 die $errstr;
47         } ],
48
49         # Start of an include stack.
50         [ qr/^In file included from .*[,:]$/ => sub {
51                 my ($msg) = @_;
52                 $location = "$msg\n";
53                 return undef;
54         } ],
55
56         # Continuation of an include stack.
57         [ qr/^                 from .*[,:]$/ => sub {
58                 my ($msg) = @_;
59                 $location .= "$msg\n";
60                 return undef;
61         } ],
62
63         # A function, method, constructor, or destructor is the site of a problem
64         [ qr/In ((con|de)structor|(member )?function)/ => sub {
65                 my ($msg) = @_;
66                 # If a complete location string is waiting then probably we dropped an error, so drop the location for a new one.
67                 if ($location =~ m/In ((con|de)structor|(member )?function)/) {
68                         $location = "$msg\n";
69                 } else {
70                         $location .= "$msg\n";
71                 }
72                 return undef;
73         } ],
74
75         [ qr/^.* warning: / => sub {
76                 my ($msg) = @_;
77                 my $str = $location . "\e[33;1m$msg\e[0m\n";
78                 $showncmdline = 1;
79                 $location = "";
80                 return $str;
81         } ],
82
83         [ qr/^.* error: / => sub {
84                 my ($msg) = @_;
85                 my $str = "";
86                 $str = "An error occured when executing:\e[37;1m $cc " . join(' ', @ARGV) . "\n" unless $showncmdline;
87                 $showncmdline = 1;
88                 $str .= $location . "\e[31;1m$msg\e[0m\n";
89                 $location = "";
90                 return $str;
91         } ],
92
93         [ qr/./ => sub {
94                 my ($msg) = @_;
95                 $msg = $location . $msg;
96                 $location = "";
97                 $msg =~ s/std::basic_string\<char\, std\:\:char_traits\<char\>, std::allocator\<char\> \>(\s+|)/std::string/g;
98                 $msg =~ s/std::basic_string\<char\, .*?irc_char_traits\<char\>, std::allocator\<char\> \>(\s+|)/irc::string/g;
99                 for my $stl (qw(deque vector list)) {
100                         $msg =~ s/std::$stl\<(\S+), std::allocator\<\1\> \>/std::$stl\<$1\>/g;
101                         $msg =~ s/std::$stl\<(std::pair\<\S+, \S+\>), std::allocator\<\1 \> \>/std::$stl<$1 >/g;
102                 }
103                 $msg =~ s/std::map\<(\S+), (\S+), std::less\<\1\>, std::allocator\<std::pair\<const \1, \2\> \> \>/std::map<$1, $2>/g;
104                 # Warning: These filters are GNU C++ specific!
105                 $msg =~ s/__gnu_cxx::__normal_iterator\<(\S+)\*, std::vector\<\1\> \>/std::vector<$1>::iterator/g;
106                 $msg =~ s/__gnu_cxx::__normal_iterator\<(std::pair\<\S+, \S+\>)\*, std::vector\<\1 \> \>/std::vector<$1 >::iterator/g;
107                 $msg =~ s/__gnu_cxx::__normal_iterator\<char\*, std::string\>/std::string::iterator/g;
108                 $msg =~ s/__gnu_cxx::__normal_iterator\<char\*, irc::string\>/irc::string::iterator/g;
109                 return $msg;
110         } ],
111 );
112
113 my $pid;
114
115 my ($r_stderr, $w_stderr);
116
117 my $name = "";
118 my $action = "";
119
120 if ($cc eq "ar") {
121         $name = $ARGV[1];
122         $action = "ARCHIVE";
123 } else {
124         foreach my $n (@ARGV)
125         {
126                 if ($n =~ /\.cpp$/)
127                 {
128                         my $f = $n;
129                         if (defined $ENV{SOURCEPATH}) {
130                                 $f =~ s#^$ENV{SOURCEPATH}/src/##;
131                         }
132                         if ($action eq "BUILD")
133                         {
134                                 $name .= " " . $f;
135                         }
136                         else
137                         {
138                                 $action = "BUILD";
139                                 $name = $f;
140                         }
141                 }
142                 elsif ($action eq "BUILD") # .cpp has priority.
143                 {
144                         next;
145                 }
146                 elsif ($n eq "-o")
147                 {
148                         $action = $name = $n;
149                 }
150                 elsif ($name eq "-o")
151                 {
152                         $action = "LINK";
153                         $name = $n;
154                 }
155         }
156 }
157
158 if (!defined($cc) || $cc eq "") {
159         die "Compiler not specified!\n";
160 }
161
162 pipe($r_stderr, $w_stderr) or die "pipe stderr: $!\n";
163
164 $pid = fork;
165
166 die "Cannot fork to start $cc! $!\n" unless defined($pid);
167
168 if ($pid) {
169
170         printf "\t\e[1;32m%-20s\e[0m%s\n", $action . ":", $name unless $name eq "";
171
172         my $fail = 0;
173         # Parent - Close child-side pipes.
174         close $w_stderr;
175         # Close STDIN to ensure no conflicts with child.
176         close STDIN;
177         # Now read each line of stderr
178 LINE:   while (defined(my $line = <$r_stderr>)) {
179                 chomp $line;
180
181                 for my $filter (@msgfilters) {
182                         my @caps;
183                         if (@caps = ($line =~ $filter->[0])) {
184                                 $@ = "";
185                                 $line = eval {
186                                         $filter->[1]->($line, @caps);
187                                 };
188                                 if ($@) {
189                                         # Note that $line is undef now.
190                                         $fail = 1;
191                                         print STDERR $@;
192                                 }
193                                 next LINE unless defined($line);
194                         }
195                 }
196                 # Chomp off newlines again, in case the filters put some back in.
197                 chomp $line;
198                 print STDERR "$line\n";
199         }
200         waitpid $pid, 0;
201         close $r_stderr;
202         my $exit = $?;
203         # Simulate the same exit, so make gets the right termination info.
204         if (POSIX::WIFSIGNALED($exit)) {
205                 # Make won't get the right termination info (it gets ours, not the compiler's), so we must tell the user what really happened ourselves!
206                 print STDERR "$cc killed by signal " . POSIX::WTERMSIGN($exit) . "\n";
207                 kill "TERM", getppid(); # Needed for bsd make.
208                 kill "TERM", $$;
209         }
210         else {
211                 if (POSIX::WEXITSTATUS($exit) == 0) {
212                         if ($fail) {
213                                 kill "TERM", getppid(); # Needed for bsd make.
214                                 kill "TERM", $$;
215                         }
216                         exit 0;
217                 } else {
218                         exit POSIX::WEXITSTATUS($exit);
219                 }
220         }
221 } else {
222         # Child - Close parent-side pipes.
223         close $r_stderr;
224         # Divert stderr
225         open STDERR, ">&", $w_stderr or die "Cannot divert STDERR: $!\n";
226         # Run the compiler!
227         exec { $cc } $cc, @ARGV;
228         die "exec $cc: $!\n";
229 }