]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - make/run-cc.pl
Commit patch from danieldg that makes a ton of stuff const-safe for latest warn-happy...
[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 = "$where error: cannot pass objects of non-POD type `$type' through `...'\n";
30                 if ($type =~ m/::string/) {
31                         $errstr .= "$where (Did you forget to call c_str()?)\n";
32                 }
33                 die $errstr;
34         } ],
35
36         # Start of an include stack.
37         [ qr/^In file included from .*[,:]$/ => sub {
38                 my ($msg) = @_;
39                 $location = "$msg\n";
40         } ],
41
42         # Continuation of an include stack.
43         [ qr/^                 from .*[,:]$/ => sub {
44                 my ($msg) = @_;
45                 $location .= "$msg\n";
46         } ],
47
48         # A function, method, constructor, or destructor is the site of a problem
49         [ qr/In ((con|de)structor|(member )?function)/ => sub {
50                 my ($msg) = @_;
51                 # If a complete location string is waiting then probably we dropped an error, so drop the location for a new one.
52                 if ($location =~ m/In ((con|de)structor|(member )?function)/) {
53                         $location = "$msg\n";
54                 } else {
55                         $location .= "$msg\n";
56                 }
57         } ],
58
59         [ qr/^.* warning: / => sub {
60                 my ($msg) = @_;
61                 print $location;
62                 $location = "";
63                 print STDERR "\e[33;1m$msg\e[0m\n";
64         } ],
65
66         [ qr/^.* error: / => sub {
67                 my ($msg) = @_;
68                 print STDERR "An error occured when executing:\e[37;1m $cc " . join(' ', @ARGV) . "\n" unless $showncmdline;
69                 $showncmdline = 1;
70                 print $location;
71                 $location = "";
72                 print STDERR "\e[31;1m$msg\e[0m\n";
73         } ],
74 );
75
76 my $pid;
77
78 my ($r_stderr, $w_stderr);
79
80 my $name = "";
81 my $action = "";
82
83 foreach my $n (@ARGV)
84 {
85         if ($n =~ /\.cpp$/)
86         {
87                 $action = "BUILD";
88                 $name = $n;
89                 last;
90         }
91         elsif ($n eq "-o")
92         {
93                 $action = $name = $n;
94         }
95         elsif ($name eq "-o")
96         {
97                 $action = "LINK";
98                 $name = $n;
99         }
100 }
101
102 if (!defined($cc) || $cc eq "") {
103         die "Compiler not specified!\n";
104 }
105
106 pipe($r_stderr, $w_stderr) or die "pipe stderr: $!\n";
107
108 $pid = fork;
109
110 die "Cannot fork to start gcc! $!\n" unless defined($pid);
111
112 if ($pid) {
113
114         print "\t\e[1;32m$action:\e[0m\t\t$name\n" unless $name eq "";
115
116         my $fail = 0;
117         # Parent - Close child-side pipes.
118         close $w_stderr;
119         # Close STDIN to ensure no conflicts with child.
120         close STDIN;
121         # Now read each line of stderr
122 LINE:   while (defined(my $line = <$r_stderr>)) {
123                 chomp $line;
124                 for my $filter (@msgfilters) {
125                         my @caps;
126                         if (@caps = ($line =~ $filter->[0])) {
127                                 $@ = "";
128                                 eval {
129                                         $filter->[1]->($line, @caps);
130                                 };
131                                 if ($@) {
132                                         $fail = 1;
133                                         print STDERR $@;
134                                 }
135                                 next LINE;
136                         }
137                 }
138                 print STDERR "$line\n";
139         }
140         waitpid $pid, 0;
141         close $r_stderr;
142         my $exit = $?;
143         # Simulate the same exit, so make gets the right termination info.
144         if (POSIX::WIFSIGNALED($exit)) {
145                 # 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!
146                 print STDERR "$cc killed by signal " . POSIX::WTERMSIGN($exit) . "\n";
147                 kill "TERM", getppid(); # Needed for bsd make.
148                 kill "TERM", $$;
149         }
150         else {
151                 if (POSIX::WEXITSTATUS($exit) == 0) {
152                         if ($fail) {
153                                 kill "TERM", getppid(); # Needed for bsd make.
154                                 kill "TERM", $$;
155                         }
156                         exit 0;
157                 } else {
158                         exit POSIX::WEXITSTATUS($exit);
159                 }
160         }
161 } else {
162         # Child - Close parent-side pipes.
163         close $r_stderr;
164         # Divert stderr
165         open STDERR, ">&", $w_stderr or die "Cannot divert STDERR: $!\n";
166         # Run the compiler!
167         exec { $cc } $cc, @ARGV;
168         die "exec $cc: $!\n";
169 }