]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - make/run-cc.pl
Add <badchan:redirect>: redirects users attempting to join a bad channel to a second...
[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 @msgfilters = (
22         [ qr/^(.*) warning: cannot pass objects of non-POD type `(.*)' through `\.\.\.'; call will abort at runtime/ => sub {
23                 my ($msg, $where, $type) = @_;
24                 my $errstr = "$where error: cannot pass objects of non-POD type `$type' through `...'\n";
25                 if ($type =~ m/::string/) {
26                         $errstr .= "$where (Did you forget to call c_str()?)\n";
27                 }
28                 die $errstr;
29         } ],
30
31         [ qr/^.* warning: / => sub {
32                 my ($msg) = @_;
33                 print STDERR "\e[33;1m$msg\e[0m\n";
34         } ],
35
36         [ qr/^.* error: / => sub {
37                 my ($msg) = @_;
38                 print STDERR "An error occured when executing:\e[37;1m $cc " . join(' ', @ARGV) . "\n";
39                 print STDERR "\e[31;1m$msg\e[0m\n";
40         } ],
41 );
42
43 my $pid;
44
45 my ($r_stderr, $w_stderr);
46
47 my $name = "";
48
49 foreach my $n (@ARGV)
50 {
51         if ($n =~ /\.cpp$/)
52         {
53                 $name = $n;
54         }
55 }
56
57 if (!defined($cc) || $cc eq "") {
58         die "Compiler not specified!\n";
59 }
60
61 pipe($r_stderr, $w_stderr) or die "pipe stderr: $!\n";
62
63 $pid = fork;
64
65 die "Cannot fork to start gcc! $!\n" unless defined($pid);
66
67 if ($pid) {
68
69         print "\t\e[1;32mBUILD:\e[0m\t\t$name\n" unless $name eq "";
70
71         my $fail = 0;
72         # Parent - Close child-side pipes.
73         close $w_stderr;
74         # Close STDIN to ensure no conflicts with child.
75         close STDIN;
76         # Now read each line of stderr
77 LINE:   while (defined(my $line = <$r_stderr>)) {
78                 chomp $line;
79                 for my $filter (@msgfilters) {
80                         my @caps;
81                         if (@caps = ($line =~ $filter->[0])) {
82                                 $@ = "";
83                                 eval {
84                                         $filter->[1]->($line, @caps);
85                                 };
86                                 if ($@) {
87                                         $fail = 1;
88                                         print STDERR $@;
89                                 }
90                                 next LINE;
91                         }
92                 }
93                 print STDERR "$line\n";
94         }
95         waitpid $pid, 0;
96         close $r_stderr;
97         my $exit = $?;
98         # Simulate the same exit, so make gets the right termination info.
99         if (POSIX::WIFSIGNALED($exit)) {
100                 # 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!
101                 print STDERR "$cc killed by signal " . POSIX::WTERMSIGN($exit) . "\n";
102                 kill "TERM", getppid(); # Needed for bsd make.
103                 kill "TERM", $$;
104         }
105         else {
106                 if (POSIX::WEXITSTATUS($exit) == 0) {
107                         if ($fail) {
108                                 kill "TERM", getppid(); # Needed for bsd make.
109                                 kill "TERM", $$;
110                         }
111                         exit 0;
112                 } else {
113                         exit POSIX::WEXITSTATUS($exit);
114                 }
115         }
116 } else {
117         # Child - Close parent-side pipes.
118         close $r_stderr;
119         # Divert stderr
120         open STDERR, ">&", $w_stderr or die "Cannot divert STDERR: $!\n";
121         # Run the compiler!
122         exec { $cc } $cc, @ARGV;
123         die "exec $cc: $!\n";
124 }