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