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