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