]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - make/run-cc.pl
7ea2f39d009b85120a0f4849a4df3f10bfdcbe1c
[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         } ],
43
44         # Continuation of an include stack.
45         [ qr/^                 from .*[,:]$/ => sub {
46                 my ($msg) = @_;
47                 $location .= "$msg\n";
48         } ],
49
50         # A function, method, constructor, or destructor is the site of a problem
51         [ qr/In ((con|de)structor|(member )?function)/ => sub {
52                 my ($msg) = @_;
53                 # If a complete location string is waiting then probably we dropped an error, so drop the location for a new one.
54                 if ($location =~ m/In ((con|de)structor|(member )?function)/) {
55                         $location = "$msg\n";
56                 } else {
57                         $location .= "$msg\n";
58                 }
59         } ],
60
61         [ qr/^.* warning: / => sub {
62                 my ($msg) = @_;
63                 print $location;
64                 $location = "";
65                 print STDERR "\e[33;1m$msg\e[0m\n";
66         } ],
67
68         [ qr/^.* error: / => sub {
69                 my ($msg) = @_;
70                 print STDERR "An error occured when executing:\e[37;1m $cc " . join(' ', @ARGV) . "\n" unless $showncmdline;
71                 $showncmdline = 1;
72                 print $location;
73                 $location = "";
74                 print STDERR "\e[31;1m$msg\e[0m\n";
75         } ],
76 );
77
78 my $pid;
79
80 my ($r_stderr, $w_stderr);
81
82 my $name = "";
83 my $action = "";
84
85 if ($cc eq "ar") {
86         $name = $ARGV[1];
87         $action = "ARCHIVE";
88 } else {
89         foreach my $n (@ARGV)
90         {
91                 if ($n =~ /\.cpp$/)
92                 {
93                         if ($action eq "BUILD")
94                         {
95                                 $name .= " " . $n;
96                         }
97                         else
98                         {
99                                 $action = "BUILD";
100                                 $name = $n;
101                         }
102                 }
103                 elsif ($action eq "BUILD") # .cpp has priority.
104                 {
105                         next;
106                 }
107                 elsif ($n eq "-o")
108                 {
109                         $action = $name = $n;
110                 }
111                 elsif ($name eq "-o")
112                 {
113                         $action = "LINK";
114                         $name = $n;
115                 }
116         }
117 }
118
119 if (!defined($cc) || $cc eq "") {
120         die "Compiler not specified!\n";
121 }
122
123 pipe($r_stderr, $w_stderr) or die "pipe stderr: $!\n";
124
125 $pid = fork;
126
127 die "Cannot fork to start $cc! $!\n" unless defined($pid);
128
129 if ($pid) {
130
131         printf "\t\e[1;32m%-20s\e[0m%s\n", $action . ":", $name unless $name eq "";
132
133         my $fail = 0;
134         # Parent - Close child-side pipes.
135         close $w_stderr;
136         # Close STDIN to ensure no conflicts with child.
137         close STDIN;
138         # Now read each line of stderr
139 LINE:   while (defined(my $line = <$r_stderr>)) {
140                 chomp $line;
141
142                 # someone come up with a better way of doing this, it cant go in message filters as message filters
143                 # cant do straight-out replace.
144                 #
145                 # The order of these replacements is IMPORTANT. DO NOT REORDER THEM.
146
147                 $line =~ s/std\:\:basic_string\<char\, std\:\:char_traits\<char\>, std::allocator\<char\> \>(\s+|)/std::string/g;
148                 $line =~ s/std\:\:basic_string\<char\, .*?irc_char_traits\<char\>, std::allocator\<char\> \>(\s+|)/irc::string/g;
149                 $line =~ s/std\:\:deque\<(\S+)\, std::allocator\<\S+\> \>/std::deque<$1>/g;
150
151                 for my $filter (@msgfilters) {
152                         my @caps;
153                         if (@caps = ($line =~ $filter->[0])) {
154                                 $@ = "";
155                                 eval {
156                                         $filter->[1]->($line, @caps);
157                                 };
158                                 if ($@) {
159                                         $fail = 1;
160                                         print STDERR $@;
161                                 }
162                                 next LINE;
163                         }
164                 }
165                 print STDERR "$line\n";
166         }
167         waitpid $pid, 0;
168         close $r_stderr;
169         my $exit = $?;
170         # Simulate the same exit, so make gets the right termination info.
171         if (POSIX::WIFSIGNALED($exit)) {
172                 # 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!
173                 print STDERR "$cc killed by signal " . POSIX::WTERMSIGN($exit) . "\n";
174                 kill "TERM", getppid(); # Needed for bsd make.
175                 kill "TERM", $$;
176         }
177         else {
178                 if (POSIX::WEXITSTATUS($exit) == 0) {
179                         if ($fail) {
180                                 kill "TERM", getppid(); # Needed for bsd make.
181                                 kill "TERM", $$;
182                         }
183                         exit 0;
184                 } else {
185                         exit POSIX::WEXITSTATUS($exit);
186                 }
187         }
188 } else {
189         # Child - Close parent-side pipes.
190         close $r_stderr;
191         # Divert stderr
192         open STDERR, ">&", $w_stderr or die "Cannot divert STDERR: $!\n";
193         # Run the compiler!
194         exec { $cc } $cc, @ARGV;
195         die "exec $cc: $!\n";
196 }