]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - make/run-cc.pl
Merge pull request #1084 from SaberUK/insp20+fix-parallel-debug-install
[user/henk/code/inspircd.git] / make / run-cc.pl
1 #!/usr/bin/env perl
2
3 #
4 # InspIRCd -- Internet Relay Chat Daemon
5 #
6 #   Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
7 #   Copyright (C) 2008 Craig Edwards <craigedwards@brainbox.cc>
8 #
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.
12 #
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
16 # details.
17 #
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/>.
20 #
21
22
23 ### THIS IS DESIGNED TO BE RUN BY MAKE! DO NOT RUN FROM THE SHELL (because it MIGHT sigterm the shell)! ###
24
25 use strict;
26 use warnings FATAL => qw(all);
27
28 use POSIX ();
29
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.
33
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.
36
37 # The subs are passed the message, and anything the regex captured.
38
39 my $cc = shift(@ARGV);
40
41 my $showncmdline = 0;
42
43 # GCC's "location of error stuff", which accumulates the "In file included from" include stack
44 my $location = "";
45
46 my @msgfilters = (
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";
50                 $location = "";
51                 if ($type =~ m/::(basic_)?string/) {
52                         $errstr .= "$where (Did you forget to call c_str()?)\n";
53                 }
54                 die $errstr;
55         } ],
56
57         # Start of an include stack.
58         [ qr/^In file included from .*[,:]$/ => sub {
59                 my ($msg) = @_;
60                 $location = "$msg\n";
61                 return undef;
62         } ],
63
64         # Continuation of an include stack.
65         [ qr/^                 from .*[,:]$/ => sub {
66                 my ($msg) = @_;
67                 $location .= "$msg\n";
68                 return undef;
69         } ],
70
71         # A function, method, constructor, or destructor is the site of a problem
72         [ qr/In ((con|de)structor|(member )?function)/ => sub {
73                 my ($msg) = @_;
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)/) {
76                         $location = "$msg\n";
77                 } else {
78                         $location .= "$msg\n";
79                 }
80                 return undef;
81         } ],
82
83         [ qr/^.* warning: / => sub {
84                 my ($msg) = @_;
85                 my $str = $location . "\e[33;1m$msg\e[0m\n";
86                 $showncmdline = 1;
87                 $location = "";
88                 return $str;
89         } ],
90
91         [ qr/^.* error: / => sub {
92                 my ($msg) = @_;
93                 my $str = "";
94                 $str = "An error occured when executing:\e[37;1m $cc " . join(' ', @ARGV) . "\n" unless $showncmdline;
95                 $showncmdline = 1;
96                 $str .= $location . "\e[31;1m$msg\e[0m\n";
97                 $location = "";
98                 return $str;
99         } ],
100
101         [ qr/./ => sub {
102                 my ($msg) = @_;
103                 $msg = $location . $msg;
104                 $location = "";
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;
110                 }
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;
117                 return $msg;
118         } ],
119 );
120
121 my $pid;
122
123 my ($r_stderr, $w_stderr);
124
125 my $name = "";
126 my $action = "";
127
128 if ($cc eq "ar") {
129         $name = $ARGV[1];
130         $action = "ARCHIVE";
131 } else {
132         foreach my $n (@ARGV)
133         {
134                 if ($n =~ /\.cpp$/)
135                 {
136                         my $f = $n;
137                         if (defined $ENV{SOURCEPATH}) {
138                                 $f =~ s#^$ENV{SOURCEPATH}/src/##;
139                         }
140                         if ($action eq "BUILD")
141                         {
142                                 $name .= " " . $f;
143                         }
144                         else
145                         {
146                                 $action = "BUILD";
147                                 $name = $f;
148                         }
149                 }
150                 elsif ($action eq "BUILD") # .cpp has priority.
151                 {
152                         next;
153                 }
154                 elsif ($n eq "-o")
155                 {
156                         $action = $name = $n;
157                 }
158                 elsif ($name eq "-o")
159                 {
160                         $action = "LINK";
161                         $name = $n;
162                 }
163         }
164 }
165
166 if (!defined($cc) || $cc eq "") {
167         die "Compiler not specified!\n";
168 }
169
170 pipe($r_stderr, $w_stderr) or die "pipe stderr: $!\n";
171
172 $pid = fork;
173
174 die "Cannot fork to start $cc! $!\n" unless defined($pid);
175
176 if ($pid) {
177
178         printf "\t\e[1;32m%-20s\e[0m%s\n", $action . ":", $name unless $name eq "";
179
180         my $fail = 0;
181         # Parent - Close child-side pipes.
182         close $w_stderr;
183         # Close STDIN to ensure no conflicts with child.
184         close STDIN;
185         # Now read each line of stderr
186 LINE:   while (defined(my $line = <$r_stderr>)) {
187                 chomp $line;
188
189                 for my $filter (@msgfilters) {
190                         my @caps;
191                         if (@caps = ($line =~ $filter->[0])) {
192                                 $@ = "";
193                                 $line = eval {
194                                         $filter->[1]->($line, @caps);
195                                 };
196                                 if ($@) {
197                                         # Note that $line is undef now.
198                                         $fail = 1;
199                                         print STDERR $@;
200                                 }
201                                 next LINE unless defined($line);
202                         }
203                 }
204                 # Chomp off newlines again, in case the filters put some back in.
205                 chomp $line;
206                 print STDERR "$line\n";
207         }
208         waitpid $pid, 0;
209         close $r_stderr;
210         my $exit = $?;
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.
216                 kill "TERM", $$;
217         }
218         else {
219                 if (POSIX::WEXITSTATUS($exit) == 0) {
220                         if ($fail) {
221                                 kill "TERM", getppid(); # Needed for bsd make.
222                                 kill "TERM", $$;
223                         }
224                         exit 0;
225                 } else {
226                         exit POSIX::WEXITSTATUS($exit);
227                 }
228         }
229 } else {
230         # Child - Close parent-side pipes.
231         close $r_stderr;
232         # Divert stderr
233         open STDERR, ">&", $w_stderr or die "Cannot divert STDERR: $!\n";
234         # Run the compiler!
235         exec { $cc } $cc, @ARGV;
236         die "exec $cc: $!\n";
237 }