3 ### THIS IS DESIGNED TO BE RUN BY MAKE! DO NOT RUN FROM THE SHELL (because it MIGHT sigterm the shell)! ###
6 use warnings FATAL => qw(all);
10 # Runs the compiler, passing it the given arguments.
11 # Filters select output from the compiler's standard error channel and
12 # can take different actions as a result.
14 # 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)
15 # even though we use the => in it.
17 # The subs are passed the message, and anything the regex captured.
19 my $cc = shift(@ARGV);
23 # GCC's "location of error stuff", which accumulates the "In file included from" include stack
27 [ qr/^(.*) warning: cannot pass objects of non-POD type `(.*)' through `\.\.\.'; call will abort at runtime/ => sub {
28 my ($msg, $where, $type) = @_;
29 my $errstr = $location . "$where error: cannot pass objects of non-POD type `$type' through `...'\n";
31 if ($type =~ m/::(basic_)?string/) {
32 $errstr .= "$where (Did you forget to call c_str()?)\n";
37 # Start of an include stack.
38 [ qr/^In file included from .*[,:]$/ => sub {
44 # Continuation of an include stack.
45 [ qr/^ from .*[,:]$/ => sub {
47 $location .= "$msg\n";
51 # A function, method, constructor, or destructor is the site of a problem
52 [ qr/In ((con|de)structor|(member )?function)/ => sub {
54 # If a complete location string is waiting then probably we dropped an error, so drop the location for a new one.
55 if ($location =~ m/In ((con|de)structor|(member )?function)/) {
58 $location .= "$msg\n";
63 [ qr/^.* warning: / => sub {
65 my $str = $location . "\e[33;1m$msg\e[0m\n";
70 [ qr/^.* error: / => sub {
73 $str = "An error occured when executing:\e[37;1m $cc " . join(' ', @ARGV) . "\n" unless $showncmdline;
75 $str .= $location . "\e[31;1m$msg\e[0m\n";
82 $msg = $location . $msg;
84 $msg =~ s/std::basic_string\<char\, std\:\:char_traits\<char\>, std::allocator\<char\> \>(\s+|)/std::string/g;
85 $msg =~ s/std::basic_string\<char\, .*?irc_char_traits\<char\>, std::allocator\<char\> \>(\s+|)/irc::string/g;
86 for my $stl (qw(deque vector list)) {
87 $msg =~ s/std::$stl\<(\S+), std::allocator\<\1\> \>/std::$stl\<$1\>/g;
88 $msg =~ s/std::$stl\<(std::pair\<\S+, \S+\>), std::allocator\<\1 \> \>/std::$stl<$1 >/g;
90 $msg =~ s/std::map\<(\S+), (\S+), std::less\<\1\>, std::allocator\<std::pair\<const \1, \2\> \> \>/std::map<$1, $2>/g;
91 # Warning: These filters are GNU C++ specific!
92 $msg =~ s/__gnu_cxx::__normal_iterator\<(\S+)\*, std::vector\<\1\> \>/std::vector<$1>::iterator/g;
93 $msg =~ s/__gnu_cxx::__normal_iterator\<(std::pair\<\S+, \S+\>)\*, std::vector\<\1 \> \>/std::vector<$1 >::iterator/g;
94 $msg =~ s/__gnu_cxx::__normal_iterator\<char\*, std::string\>/std::string::iterator/g;
95 $msg =~ s/__gnu_cxx::__normal_iterator\<char\*, irc::string\>/irc::string::iterator/g;
102 my ($r_stderr, $w_stderr);
111 foreach my $n (@ARGV)
115 if ($action eq "BUILD")
125 elsif ($action eq "BUILD") # .cpp has priority.
131 $action = $name = $n;
133 elsif ($name eq "-o")
141 if (!defined($cc) || $cc eq "") {
142 die "Compiler not specified!\n";
145 pipe($r_stderr, $w_stderr) or die "pipe stderr: $!\n";
149 die "Cannot fork to start $cc! $!\n" unless defined($pid);
153 printf "\t\e[1;32m%-20s\e[0m%s\n", $action . ":", $name unless $name eq "";
156 # Parent - Close child-side pipes.
158 # Close STDIN to ensure no conflicts with child.
160 # Now read each line of stderr
161 LINE: while (defined(my $line = <$r_stderr>)) {
164 for my $filter (@msgfilters) {
166 if (@caps = ($line =~ $filter->[0])) {
169 $filter->[1]->($line, @caps);
172 # Note that $line is undef now.
176 next LINE unless defined($line);
179 # Chomp off newlines again, in case the filters put some back in.
181 print STDERR "$line\n";
186 # Simulate the same exit, so make gets the right termination info.
187 if (POSIX::WIFSIGNALED($exit)) {
188 # 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!
189 print STDERR "$cc killed by signal " . POSIX::WTERMSIGN($exit) . "\n";
190 kill "TERM", getppid(); # Needed for bsd make.
194 if (POSIX::WEXITSTATUS($exit) == 0) {
196 kill "TERM", getppid(); # Needed for bsd make.
201 exit POSIX::WEXITSTATUS($exit);
205 # Child - Close parent-side pipes.
208 open STDERR, ">&", $w_stderr or die "Cannot divert STDERR: $!\n";
210 exec { $cc } $cc, @ARGV;
211 die "exec $cc: $!\n";