]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - make/run-cc.pl
Chain ValidateServerName onto ValidateHostname so that the servername gets hostname...
[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                 $location = "";
67                 return $str;
68         } ],
69
70         [ qr/^.* error: / => sub {
71                 my ($msg) = @_;
72                 my $str = "";
73                 $str = "An error occured when executing:\e[37;1m $cc " . join(' ', @ARGV) . "\n" unless $showncmdline;
74                 $showncmdline = 1;
75                 $str .= $location . "\e[31;1m$msg\e[0m\n";
76                 $location = "";
77                 return $str;
78         } ],
79
80         [ qr/./ => sub {
81                 my ($msg) = @_;
82                 $msg = $location . $msg;
83                 $location = "";
84                 $msg =~ s/std::basic_string\<char\, std\:\:char_traits\<char\>, std::allocator\<char\> \>(\s+|)/std::string/g;
85                 $msg =~ s/std::basic_string\<char\, .*?irc_char_traits\<char\>, std::allocator\<char\> \>(\s+|)/irc::string/g;
86                 for my $stl (qw(deque vector list)) {
87                         $msg =~ s/std::$stl\<(\S+), std::allocator\<\1\> \>/std::$stl\<$1\>/g;
88                         $msg =~ s/std::$stl\<(std::pair\<\S+, \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                 # Warning: These filters are GNU C++ specific!
92                 $msg =~ s/__gnu_cxx::__normal_iterator\<(\S+)\*, std::vector\<\1\> \>/std::vector<$1>::iterator/g;
93                 $msg =~ s/__gnu_cxx::__normal_iterator\<(std::pair\<\S+, \S+\>)\*, std::vector\<\1 \> \>/std::vector<$1 >::iterator/g;
94                 $msg =~ s/__gnu_cxx::__normal_iterator\<char\*, std::string\>/std::string::iterator/g;
95                 $msg =~ s/__gnu_cxx::__normal_iterator\<char\*, irc::string\>/irc::string::iterator/g;
96                 return $msg;
97         } ],
98 );
99
100 my $pid;
101
102 my ($r_stderr, $w_stderr);
103
104 my $name = "";
105 my $action = "";
106
107 if ($cc eq "ar") {
108         $name = $ARGV[1];
109         $action = "ARCHIVE";
110 } else {
111         foreach my $n (@ARGV)
112         {
113                 if ($n =~ /\.cpp$/)
114                 {
115                         if ($action eq "BUILD")
116                         {
117                                 $name .= " " . $n;
118                         }
119                         else
120                         {
121                                 $action = "BUILD";
122                                 $name = $n;
123                         }
124                 }
125                 elsif ($action eq "BUILD") # .cpp has priority.
126                 {
127                         next;
128                 }
129                 elsif ($n eq "-o")
130                 {
131                         $action = $name = $n;
132                 }
133                 elsif ($name eq "-o")
134                 {
135                         $action = "LINK";
136                         $name = $n;
137                 }
138         }
139 }
140
141 if (!defined($cc) || $cc eq "") {
142         die "Compiler not specified!\n";
143 }
144
145 pipe($r_stderr, $w_stderr) or die "pipe stderr: $!\n";
146
147 $pid = fork;
148
149 die "Cannot fork to start $cc! $!\n" unless defined($pid);
150
151 if ($pid) {
152
153         printf "\t\e[1;32m%-20s\e[0m%s\n", $action . ":", $name unless $name eq "";
154
155         my $fail = 0;
156         # Parent - Close child-side pipes.
157         close $w_stderr;
158         # Close STDIN to ensure no conflicts with child.
159         close STDIN;
160         # Now read each line of stderr
161 LINE:   while (defined(my $line = <$r_stderr>)) {
162                 chomp $line;
163
164                 for my $filter (@msgfilters) {
165                         my @caps;
166                         if (@caps = ($line =~ $filter->[0])) {
167                                 $@ = "";
168                                 $line = eval {
169                                         $filter->[1]->($line, @caps);
170                                 };
171                                 if ($@) {
172                                         # Note that $line is undef now.
173                                         $fail = 1;
174                                         print STDERR $@;
175                                 }
176                                 next LINE unless defined($line);
177                         }
178                 }
179                 # Chomp off newlines again, in case the filters put some back in.
180                 chomp $line;
181                 print STDERR "$line\n";
182         }
183         waitpid $pid, 0;
184         close $r_stderr;
185         my $exit = $?;
186         # Simulate the same exit, so make gets the right termination info.
187         if (POSIX::WIFSIGNALED($exit)) {
188                 # 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!
189                 print STDERR "$cc killed by signal " . POSIX::WTERMSIGN($exit) . "\n";
190                 kill "TERM", getppid(); # Needed for bsd make.
191                 kill "TERM", $$;
192         }
193         else {
194                 if (POSIX::WEXITSTATUS($exit) == 0) {
195                         if ($fail) {
196                                 kill "TERM", getppid(); # Needed for bsd make.
197                                 kill "TERM", $$;
198                         }
199                         exit 0;
200                 } else {
201                         exit POSIX::WEXITSTATUS($exit);
202                 }
203         }
204 } else {
205         # Child - Close parent-side pipes.
206         close $r_stderr;
207         # Divert stderr
208         open STDERR, ">&", $w_stderr or die "Cannot divert STDERR: $!\n";
209         # Run the compiler!
210         exec { $cc } $cc, @ARGV;
211         die "exec $cc: $!\n";
212 }