]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - make/run-cc.pl
1ae219f69922a777792b5fce98373137416f3187
[user/henk/code/inspircd.git] / make / run-cc.pl
1 #!/usr/bin/perl
2
3 ### THIS IS DESIGNED TO BE RUN BY MAKE! DO NOT RUN FROM THE SHELL (because it MIGHT sigterm the shell)! ###
4
5 use strict;
6 use warnings FATAL => qw(all);
7
8 use POSIX ();
9
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.
13
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.
16
17 # The subs are passed the message, and anything the regex captured.
18
19 my $cc = shift(@ARGV);
20
21 my $showncmdline = 0;
22
23 # GCC's "location of error stuff", which accumulates the "In file included from" include stack
24 my $location = "";
25
26 my @msgfilters = (
27         [ qr/^(.*) warning: cannot pass objects of non-POD type `(.*)' through `\.\.\.'; call will abort at runtime/ => sub {
28                 my ($msg, $where, $type) = @_;
29                 print $location;
30                 $location = "";
31                 my $errstr = "$where error: cannot pass objects of non-POD type `$type' through `...'\n";
32                 if ($type =~ m/::(basic_)?string/) {
33                         $errstr .= "$where (Did you forget to call c_str()?)\n";
34                 }
35                 die $errstr;
36         } ],
37
38         # Start of an include stack.
39         [ qr/^In file included from .*[,:]$/ => sub {
40                 my ($msg) = @_;
41                 $location = "$msg\n";
42                 return undef;
43         } ],
44
45         # Continuation of an include stack.
46         [ qr/^                 from .*[,:]$/ => sub {
47                 my ($msg) = @_;
48                 $location .= "$msg\n";
49                 return undef;
50         } ],
51
52         # A function, method, constructor, or destructor is the site of a problem
53         [ qr/In ((con|de)structor|(member )?function)/ => sub {
54                 my ($msg) = @_;
55                 # If a complete location string is waiting then probably we dropped an error, so drop the location for a new one.
56                 if ($location =~ m/In ((con|de)structor|(member )?function)/) {
57                         $location = "$msg\n";
58                 } else {
59                         $location .= "$msg\n";
60                 }
61                 return undef;
62         } ],
63
64         [ qr/^.* warning: / => sub {
65                 my ($msg) = @_;
66                 my $str = $location . "\e[33;1m$msg\e[0m\n";
67                 $location = "";
68                 return $str;
69         } ],
70
71         [ qr/^.* error: / => sub {
72                 my ($msg) = @_;
73                 my $str = "";
74                 $str = "An error occured when executing:\e[37;1m $cc " . join(' ', @ARGV) . "\n" unless $showncmdline;
75                 $showncmdline = 1;
76                 $str .= $location . "\e[31;1m$msg\e[0m\n";
77                 $location = "";
78                 return $str;
79         } ],
80
81         [ qr/./ => sub {
82                 my ($msg) = @_;
83                 $msg = $location . $msg;
84                 $location = "";
85                 $msg =~ s/std::basic_string\<char\, std\:\:char_traits\<char\>, std::allocator\<char\> \>(\s+|)/std::string/g;
86                 $msg =~ s/std::basic_string\<char\, .*?irc_char_traits\<char\>, std::allocator\<char\> \>(\s+|)/irc::string/g;
87                 for my $stl (qw(deque vector list)) {
88                         $msg =~ s/std::$stl\<(\S+), std::allocator\<(\1)\> \>/std::$stl\<$1\>/g;
89                 }
90                 $msg =~ s/std::map\<(\S+), (\S+), std::less\<\1\>, std::allocator\<std::pair\<const \1, \2\> \> \>/std::map\<$1, $2\>/g;
91                 return $msg;
92         } ],
93 );
94
95 my $pid;
96
97 my ($r_stderr, $w_stderr);
98
99 my $name = "";
100 my $action = "";
101
102 if ($cc eq "ar") {
103         $name = $ARGV[1];
104         $action = "ARCHIVE";
105 } else {
106         foreach my $n (@ARGV)
107         {
108                 if ($n =~ /\.cpp$/)
109                 {
110                         if ($action eq "BUILD")
111                         {
112                                 $name .= " " . $n;
113                         }
114                         else
115                         {
116                                 $action = "BUILD";
117                                 $name = $n;
118                         }
119                 }
120                 elsif ($action eq "BUILD") # .cpp has priority.
121                 {
122                         next;
123                 }
124                 elsif ($n eq "-o")
125                 {
126                         $action = $name = $n;
127                 }
128                 elsif ($name eq "-o")
129                 {
130                         $action = "LINK";
131                         $name = $n;
132                 }
133         }
134 }
135
136 if (!defined($cc) || $cc eq "") {
137         die "Compiler not specified!\n";
138 }
139
140 pipe($r_stderr, $w_stderr) or die "pipe stderr: $!\n";
141
142 $pid = fork;
143
144 die "Cannot fork to start $cc! $!\n" unless defined($pid);
145
146 if ($pid) {
147
148         printf "\t\e[1;32m%-20s\e[0m%s\n", $action . ":", $name unless $name eq "";
149
150         my $fail = 0;
151         # Parent - Close child-side pipes.
152         close $w_stderr;
153         # Close STDIN to ensure no conflicts with child.
154         close STDIN;
155         # Now read each line of stderr
156 LINE:   while (defined(my $line = <$r_stderr>)) {
157                 chomp $line;
158
159                 # someone come up with a better way of doing this, it cant go in message filters as message filters
160                 # cant do straight-out replace.
161                 #
162                 # The order of these replacements is IMPORTANT. DO NOT REORDER THEM.
163
164
165                 for my $filter (@msgfilters) {
166                         my @caps;
167                         if (@caps = ($line =~ $filter->[0])) {
168                                 $@ = "";
169                                 $line = eval {
170                                         $filter->[1]->($line, @caps);
171                                 };
172                                 if ($@) {
173                                         # Note that $line is undef now.
174                                         $fail = 1;
175                                         print STDERR $@;
176                                 }
177                                 next LINE unless defined($line);
178                         }
179                 }
180                 # Chomp off newlines again, in case the filters put some back in.
181                 chomp $line;
182                 print STDERR "$line\n";
183         }
184         waitpid $pid, 0;
185         close $r_stderr;
186         my $exit = $?;
187         # Simulate the same exit, so make gets the right termination info.
188         if (POSIX::WIFSIGNALED($exit)) {
189                 # 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!
190                 print STDERR "$cc killed by signal " . POSIX::WTERMSIGN($exit) . "\n";
191                 kill "TERM", getppid(); # Needed for bsd make.
192                 kill "TERM", $$;
193         }
194         else {
195                 if (POSIX::WEXITSTATUS($exit) == 0) {
196                         if ($fail) {
197                                 kill "TERM", getppid(); # Needed for bsd make.
198                                 kill "TERM", $$;
199                         }
200                         exit 0;
201                 } else {
202                         exit POSIX::WEXITSTATUS($exit);
203                 }
204         }
205 } else {
206         # Child - Close parent-side pipes.
207         close $r_stderr;
208         # Divert stderr
209         open STDERR, ">&", $w_stderr or die "Cannot divert STDERR: $!\n";
210         # Run the compiler!
211         exec { $cc } $cc, @ARGV;
212         die "exec $cc: $!\n";
213 }