]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - make/run-cc.pl
Nuke TIMESYNC from orbit \o/
[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 foreach my $n (@ARGV)
86 {
87         if ($n =~ /\.cpp$/)
88         {
89                 $action = "BUILD";
90                 $name = $n;
91                 last;
92         }
93         elsif ($n eq "-o")
94         {
95                 $action = $name = $n;
96         }
97         elsif ($name eq "-o")
98         {
99                 $action = "LINK";
100                 $name = $n;
101         }
102 }
103
104 if (!defined($cc) || $cc eq "") {
105         die "Compiler not specified!\n";
106 }
107
108 pipe($r_stderr, $w_stderr) or die "pipe stderr: $!\n";
109
110 $pid = fork;
111
112 die "Cannot fork to start gcc! $!\n" unless defined($pid);
113
114 if ($pid) {
115
116         print "\t\e[1;32m$action:\e[0m\t\t$name\n" unless $name eq "";
117
118         my $fail = 0;
119         # Parent - Close child-side pipes.
120         close $w_stderr;
121         # Close STDIN to ensure no conflicts with child.
122         close STDIN;
123         # Now read each line of stderr
124 LINE:   while (defined(my $line = <$r_stderr>)) {
125                 chomp $line;
126                 for my $filter (@msgfilters) {
127                         my @caps;
128                         if (@caps = ($line =~ $filter->[0])) {
129                                 $@ = "";
130                                 eval {
131                                         $filter->[1]->($line, @caps);
132                                 };
133                                 if ($@) {
134                                         $fail = 1;
135                                         print STDERR $@;
136                                 }
137                                 next LINE;
138                         }
139                 }
140                 print STDERR "$line\n";
141         }
142         waitpid $pid, 0;
143         close $r_stderr;
144         my $exit = $?;
145         # Simulate the same exit, so make gets the right termination info.
146         if (POSIX::WIFSIGNALED($exit)) {
147                 # 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!
148                 print STDERR "$cc killed by signal " . POSIX::WTERMSIGN($exit) . "\n";
149                 kill "TERM", getppid(); # Needed for bsd make.
150                 kill "TERM", $$;
151         }
152         else {
153                 if (POSIX::WEXITSTATUS($exit) == 0) {
154                         if ($fail) {
155                                 kill "TERM", getppid(); # Needed for bsd make.
156                                 kill "TERM", $$;
157                         }
158                         exit 0;
159                 } else {
160                         exit POSIX::WEXITSTATUS($exit);
161                 }
162         }
163 } else {
164         # Child - Close parent-side pipes.
165         close $r_stderr;
166         # Divert stderr
167         open STDERR, ">&", $w_stderr or die "Cannot divert STDERR: $!\n";
168         # Run the compiler!
169         exec { $cc } $cc, @ARGV;
170         die "exec $cc: $!\n";
171 }