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 = "$where error: cannot pass objects of non-POD type `$type' through `...'\n";
30 if ($type =~ m/::string/) {
31 $errstr .= "$where (Did you forget to call c_str()?)\n";
36 # Start of an include stack.
37 [ qr/^In file included from .*[,:]$/ => sub {
42 # Continuation of an include stack.
43 [ qr/^ from .*[,:]$/ => sub {
45 $location .= "$msg\n";
48 # A function, method, constructor, or destructor is the site of a problem
49 [ qr/In ((con|de)structor|(member )?function)/ => sub {
51 # If a complete location string is waiting then probably we dropped an error, so drop the location for a new one.
52 if ($location =~ m/In ((con|de)structor|(member )?function)/) {
55 $location .= "$msg\n";
59 [ qr/^.* warning: / => sub {
63 print STDERR "\e[33;1m$msg\e[0m\n";
66 [ qr/^.* error: / => sub {
68 print STDERR "An error occured when executing:\e[37;1m $cc " . join(' ', @ARGV) . "\n" unless $showncmdline;
72 print STDERR "\e[31;1m$msg\e[0m\n";
78 my ($r_stderr, $w_stderr);
102 if (!defined($cc) || $cc eq "") {
103 die "Compiler not specified!\n";
106 pipe($r_stderr, $w_stderr) or die "pipe stderr: $!\n";
110 die "Cannot fork to start gcc! $!\n" unless defined($pid);
114 print "\t\e[1;32m$action:\e[0m\t\t$name\n" unless $name eq "";
117 # Parent - Close child-side pipes.
119 # Close STDIN to ensure no conflicts with child.
121 # Now read each line of stderr
122 LINE: while (defined(my $line = <$r_stderr>)) {
124 for my $filter (@msgfilters) {
126 if (@caps = ($line =~ $filter->[0])) {
129 $filter->[1]->($line, @caps);
138 print STDERR "$line\n";
143 # Simulate the same exit, so make gets the right termination info.
144 if (POSIX::WIFSIGNALED($exit)) {
145 # 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!
146 print STDERR "$cc killed by signal " . POSIX::WTERMSIGN($exit) . "\n";
147 kill "TERM", getppid(); # Needed for bsd make.
151 if (POSIX::WEXITSTATUS($exit) == 0) {
153 kill "TERM", getppid(); # Needed for bsd make.
158 exit POSIX::WEXITSTATUS($exit);
162 # Child - Close parent-side pipes.
165 open STDERR, ">&", $w_stderr or die "Cannot divert STDERR: $!\n";
167 exec { $cc } $cc, @ARGV;
168 die "exec $cc: $!\n";