]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - make/run-cc.pl
Be consistent. Use ServerInstance in all places instead of 'Instance' in half. This...
[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                 my $errstr = $location . "$where error: cannot pass objects of non-POD type `$type' through `...'\n";
30                 $location = "";
31                 if ($type =~ m/::(basic_)?string/) {
32                         $errstr .= "$where (Did you forget to call c_str()?)\n";
33                 }
34                 die $errstr;
35         } ],
36
37         # Start of an include stack.
38         [ qr/^In file included from .*[,:]$/ => sub {
39                 my ($msg) = @_;
40                 $location = "$msg\n";
41                 return undef;
42         } ],
43
44         # Continuation of an include stack.
45         [ qr/^                 from .*[,:]$/ => sub {
46                 my ($msg) = @_;
47                 $location .= "$msg\n";
48                 return undef;
49         } ],
50
51         # A function, method, constructor, or destructor is the site of a problem
52         [ qr/In ((con|de)structor|(member )?function)/ => sub {
53                 my ($msg) = @_;
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)/) {
56                         $location = "$msg\n";
57                 } else {
58                         $location .= "$msg\n";
59                 }
60                 return undef;
61         } ],
62
63         [ qr/^.* warning: / => sub {
64                 my ($msg) = @_;
65                 my $str = $location . "\e[33;1m$msg\e[0m\n";
66                 $showncmdline = 1;
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                         $msg =~ s/std::$stl\<(std::pair\<\S+, \S+\>), std::allocator\<\1 \> \>/std::$stl<$1 >/g;
90                 }
91                 $msg =~ s/std::map\<(\S+), (\S+), std::less\<\1\>, std::allocator\<std::pair\<const \1, \2\> \> \>/std::map<$1, $2>/g;
92                 # Warning: These filters are GNU C++ specific!
93                 $msg =~ s/__gnu_cxx::__normal_iterator\<(\S+)\*, std::vector\<\1\> \>/std::vector<$1>::iterator/g;
94                 $msg =~ s/__gnu_cxx::__normal_iterator\<(std::pair\<\S+, \S+\>)\*, std::vector\<\1 \> \>/std::vector<$1 >::iterator/g;
95                 $msg =~ s/__gnu_cxx::__normal_iterator\<char\*, std::string\>/std::string::iterator/g;
96                 $msg =~ s/__gnu_cxx::__normal_iterator\<char\*, irc::string\>/irc::string::iterator/g;
97                 return $msg;
98         } ],
99 );
100
101 my $pid;
102
103 my ($r_stderr, $w_stderr);
104
105 my $name = "";
106 my $action = "";
107
108 if ($cc eq "ar") {
109         $name = $ARGV[1];
110         $action = "ARCHIVE";
111 } else {
112         foreach my $n (@ARGV)
113         {
114                 if ($n =~ /\.cpp$/)
115                 {
116                         if ($action eq "BUILD")
117                         {
118                                 $name .= " " . $n;
119                         }
120                         else
121                         {
122                                 $action = "BUILD";
123                                 $name = $n;
124                         }
125                 }
126                 elsif ($action eq "BUILD") # .cpp has priority.
127                 {
128                         next;
129                 }
130                 elsif ($n eq "-o")
131                 {
132                         $action = $name = $n;
133                 }
134                 elsif ($name eq "-o")
135                 {
136                         $action = "LINK";
137                         $name = $n;
138                 }
139         }
140 }
141
142 if (!defined($cc) || $cc eq "") {
143         die "Compiler not specified!\n";
144 }
145
146 pipe($r_stderr, $w_stderr) or die "pipe stderr: $!\n";
147
148 $pid = fork;
149
150 die "Cannot fork to start $cc! $!\n" unless defined($pid);
151
152 if ($pid) {
153
154         printf "\t\e[1;32m%-20s\e[0m%s\n", $action . ":", $name unless $name eq "";
155
156         my $fail = 0;
157         # Parent - Close child-side pipes.
158         close $w_stderr;
159         # Close STDIN to ensure no conflicts with child.
160         close STDIN;
161         # Now read each line of stderr
162 LINE:   while (defined(my $line = <$r_stderr>)) {
163                 chomp $line;
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 }