4 # InspIRCd -- Internet Relay Chat Daemon
6 # Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
7 # Copyright (C) 2008 Craig Edwards <craigedwards@brainbox.cc>
9 # This file is part of InspIRCd. InspIRCd is free software: you can
10 # redistribute it and/or modify it under the terms of the GNU General Public
11 # License as published by the Free Software Foundation, version 2.
13 # This program is distributed in the hope that it will be useful, but WITHOUT
14 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
23 ### THIS IS DESIGNED TO BE RUN BY MAKE! DO NOT RUN FROM THE SHELL (because it MIGHT sigterm the shell)! ###
26 use warnings FATAL => qw(all);
30 # Runs the compiler, passing it the given arguments.
31 # Filters select output from the compiler's standard error channel and
32 # can take different actions as a result.
34 # 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)
35 # even though we use the => in it.
37 # The subs are passed the message, and anything the regex captured.
39 my $cc = shift(@ARGV);
43 # GCC's "location of error stuff", which accumulates the "In file included from" include stack
47 [ qr/^(.*) warning: cannot pass objects of non-POD type `(.*)' through `\.\.\.'; call will abort at runtime/ => sub {
48 my ($msg, $where, $type) = @_;
49 my $errstr = $location . "$where error: cannot pass objects of non-POD type `$type' through `...'\n";
51 if ($type =~ m/::(basic_)?string/) {
52 $errstr .= "$where (Did you forget to call c_str()?)\n";
57 # Start of an include stack.
58 [ qr/^In file included from .*[,:]$/ => sub {
64 # Continuation of an include stack.
65 [ qr/^ from .*[,:]$/ => sub {
67 $location .= "$msg\n";
71 # A function, method, constructor, or destructor is the site of a problem
72 [ qr/In ((con|de)structor|(member )?function)/ => sub {
74 # If a complete location string is waiting then probably we dropped an error, so drop the location for a new one.
75 if ($location =~ m/In ((con|de)structor|(member )?function)/) {
78 $location .= "$msg\n";
83 [ qr/^.* warning: / => sub {
85 my $str = $location . "\e[33;1m$msg\e[0m\n";
91 [ qr/^.* error: / => sub {
94 $str = "An error occured when executing:\e[37;1m $cc " . join(' ', @ARGV) . "\n" unless $showncmdline;
96 $str .= $location . "\e[31;1m$msg\e[0m\n";
103 $msg = $location . $msg;
105 $msg =~ s/std::basic_string\<char\, std\:\:char_traits\<char\>, std::allocator\<char\> \>(\s+|)/std::string/g;
106 $msg =~ s/std::basic_string\<char\, .*?irc_char_traits\<char\>, std::allocator\<char\> \>(\s+|)/irc::string/g;
107 for my $stl (qw(deque vector list)) {
108 $msg =~ s/std::$stl\<(\S+), std::allocator\<\1\> \>/std::$stl\<$1\>/g;
109 $msg =~ s/std::$stl\<(std::pair\<\S+, \S+\>), std::allocator\<\1 \> \>/std::$stl<$1 >/g;
111 $msg =~ s/std::map\<(\S+), (\S+), std::less\<\1\>, std::allocator\<std::pair\<const \1, \2\> \> \>/std::map<$1, $2>/g;
112 # Warning: These filters are GNU C++ specific!
113 $msg =~ s/__gnu_cxx::__normal_iterator\<(\S+)\*, std::vector\<\1\> \>/std::vector<$1>::iterator/g;
114 $msg =~ s/__gnu_cxx::__normal_iterator\<(std::pair\<\S+, \S+\>)\*, std::vector\<\1 \> \>/std::vector<$1 >::iterator/g;
115 $msg =~ s/__gnu_cxx::__normal_iterator\<char\*, std::string\>/std::string::iterator/g;
116 $msg =~ s/__gnu_cxx::__normal_iterator\<char\*, irc::string\>/irc::string::iterator/g;
123 my ($r_stderr, $w_stderr);
132 foreach my $n (@ARGV)
137 if (defined $ENV{SOURCEPATH}) {
138 $f =~ s#^$ENV{SOURCEPATH}/src/##;
140 if ($action eq "BUILD")
150 elsif ($action eq "BUILD") # .cpp has priority.
156 $action = $name = $n;
158 elsif ($name eq "-o")
166 if (!defined($cc) || $cc eq "") {
167 die "Compiler not specified!\n";
170 pipe($r_stderr, $w_stderr) or die "pipe stderr: $!\n";
174 die "Cannot fork to start $cc! $!\n" unless defined($pid);
178 printf "\t\e[1;32m%-20s\e[0m%s\n", $action . ":", $name unless $name eq "";
181 # Parent - Close child-side pipes.
183 # Close STDIN to ensure no conflicts with child.
185 # Now read each line of stderr
186 LINE: while (defined(my $line = <$r_stderr>)) {
189 for my $filter (@msgfilters) {
191 if (@caps = ($line =~ $filter->[0])) {
194 $filter->[1]->($line, @caps);
197 # Note that $line is undef now.
201 next LINE unless defined($line);
204 # Chomp off newlines again, in case the filters put some back in.
206 print STDERR "$line\n";
211 # Simulate the same exit, so make gets the right termination info.
212 if (POSIX::WIFSIGNALED($exit)) {
213 # 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!
214 print STDERR "$cc killed by signal " . POSIX::WTERMSIGN($exit) . "\n";
215 kill "TERM", getppid(); # Needed for bsd make.
219 if (POSIX::WEXITSTATUS($exit) == 0) {
221 kill "TERM", getppid(); # Needed for bsd make.
226 exit POSIX::WEXITSTATUS($exit);
230 # Child - Close parent-side pipes.
233 open STDERR, ">&", $w_stderr or die "Cannot divert STDERR: $!\n";
235 exec { $cc } $cc, @ARGV;
236 die "exec $cc: $!\n";