1 | #!/boot/common/bin/perl -w
|
---|
2 |
|
---|
3 | #
|
---|
4 | # colorgcc
|
---|
5 | #
|
---|
6 | # Version: 1.3.2
|
---|
7 | #
|
---|
8 | # $Id: colorgcc,v 1.10 1999/04/29 17:15:52 jamoyers Exp $
|
---|
9 | #
|
---|
10 | # A wrapper to colorize the output from compilers whose messages
|
---|
11 | # match the "gcc" format.
|
---|
12 | #
|
---|
13 | # Requires the ANSIColor module from CPAN.
|
---|
14 | #
|
---|
15 | # Usage:
|
---|
16 | #
|
---|
17 | # In a directory that occurs in your PATH _before_ the directory
|
---|
18 | # where the compiler lives, create a softlink to colorgcc for
|
---|
19 | # each compiler you want to colorize:
|
---|
20 | #
|
---|
21 | # g++ -> colorgcc
|
---|
22 | # gcc -> colorgcc
|
---|
23 | # cc -> colorgcc
|
---|
24 | # etc.
|
---|
25 | #
|
---|
26 | # That's it. When "g++" is invoked, colorgcc is run instead.
|
---|
27 | # colorgcc looks at the program name to figure out which compiler to run.
|
---|
28 | #
|
---|
29 | # The default settings can be overridden with ~/.colorgccrc.
|
---|
30 | # See the comments in the sample .colorgccrc for more information.
|
---|
31 | #
|
---|
32 | # Note:
|
---|
33 | #
|
---|
34 | # colorgcc will only emit color codes if:
|
---|
35 | #
|
---|
36 | # (1) Its STDOUT is a tty and
|
---|
37 | # (2) the value of $TERM is not listed in the "nocolor" option.
|
---|
38 | #
|
---|
39 | # If colorgcc colorizes the output, the compiler's STDERR will be
|
---|
40 | # combined with STDOUT. Otherwise, colorgcc just passes the output from
|
---|
41 | # the compiler through without modification.
|
---|
42 | #
|
---|
43 | # Author: Jamie Moyers <jmoyers@geeks.com>
|
---|
44 | # Started: April 20, 1999
|
---|
45 | # Licence: GNU Public License
|
---|
46 | #
|
---|
47 | # Credits:
|
---|
48 | #
|
---|
49 | # I got the idea for this from a script called "color_cvs":
|
---|
50 | # color_cvs .03 Adrian Likins <adrian@gimp.org> <adrian@redhat.com>
|
---|
51 | #
|
---|
52 | # <seh4@ix.netcom.com> (Scott Harrington)
|
---|
53 | # Much improved handling of compiler command line arguments.
|
---|
54 | # exec compiler when not colorizing to preserve STDOUT, STDERR.
|
---|
55 | # Fixed my STDIN kludge.
|
---|
56 | #
|
---|
57 | # <ecarotti@athena.polito.it> (Elias S. G. Carotti)
|
---|
58 | # Corrected handling of text like -DPACKAGE=\"Package\"
|
---|
59 | # Spotted return code bug.
|
---|
60 | #
|
---|
61 | # <erwin@erwin.andreasen.org> (Erwin S. Andreasen)
|
---|
62 | # <schurchi@ucsd.edu> (Steve Churchill)
|
---|
63 | # Return code bug fixes.
|
---|
64 | #
|
---|
65 | # <rik@kde.org> (Rik Hemsley)
|
---|
66 | # Found STDIN bug.
|
---|
67 | #
|
---|
68 | # Changes:
|
---|
69 | #
|
---|
70 | # 1.3.2 Better handling of command line arguments to compiler.
|
---|
71 | #
|
---|
72 | # If we aren't colorizing output, we just exec the compiler which
|
---|
73 | # preserves the original STDOUT and STDERR.
|
---|
74 | #
|
---|
75 | # Removed STDIN kludge. STDIN being passed correctly now.
|
---|
76 | #
|
---|
77 | # 1.3.1 Added kludge to copy STDIN to the compiler's STDIN.
|
---|
78 | #
|
---|
79 | # 1.3.0 Now correctly returns (I hope) the return code of the compiler
|
---|
80 | # process as its own.
|
---|
81 | #
|
---|
82 | # 1.2.1 Applied patch to handle text similar to -DPACKAGE=\"Package\".
|
---|
83 | #
|
---|
84 | # 1.2.0 Added tty check. If STDOUT is not a tty, don't do color.
|
---|
85 | #
|
---|
86 | # 1.1.0 Added the "nocolor" option to turn off the color if the terminal type
|
---|
87 | # ($TERM) is listed.
|
---|
88 | #
|
---|
89 | # 1.0.0 Initial Version
|
---|
90 |
|
---|
91 | use Term::ANSIColor;
|
---|
92 | use IPC::Open3;
|
---|
93 |
|
---|
94 | sub initDefaults
|
---|
95 | {
|
---|
96 | $compilerPaths{"distcc"} = "/boot/common/bin/distcc_real";
|
---|
97 |
|
---|
98 | $nocolor{"dumb"} = "true";
|
---|
99 |
|
---|
100 | $colors{"srcColor"} = color("cyan");
|
---|
101 | $colors{"introColor"} = color("blue");
|
---|
102 |
|
---|
103 | $colors{"warningFileNameColor"} = color("yellow");
|
---|
104 | $colors{"warningNumberColor"} = color("yellow");
|
---|
105 | $colors{"warningMessageColor"} = color("yellow");
|
---|
106 |
|
---|
107 | $colors{"errorFileNameColor"} = color("bold red");
|
---|
108 | $colors{"errorNumberColor"} = color("bold red");
|
---|
109 | $colors{"errorMessageColor"} = color("bold red");
|
---|
110 | }
|
---|
111 |
|
---|
112 | sub loadPreferences
|
---|
113 | {
|
---|
114 | # Usage: loadPreferences("filename");
|
---|
115 |
|
---|
116 | my($filename) = @_;
|
---|
117 |
|
---|
118 | open(PREFS, "<$filename") || return;
|
---|
119 |
|
---|
120 | while(<PREFS>)
|
---|
121 | {
|
---|
122 | next if (m/^\#.*/); # It's a comment.
|
---|
123 | next if (!m/(.*):\s*(.*)/); # It's not of the form "foo: bar".
|
---|
124 |
|
---|
125 | $option = $1;
|
---|
126 | $value = $2;
|
---|
127 |
|
---|
128 | if ($option =~ m/cc|c\+\+|gcc|g\+\+/)
|
---|
129 | {
|
---|
130 | $compilerPaths{$option} = $value;
|
---|
131 | }
|
---|
132 | elsif ($option eq "nocolor")
|
---|
133 | {
|
---|
134 | # The nocolor option lists terminal types, separated by
|
---|
135 | # spaces, not to do color on.
|
---|
136 | foreach $termtype (split(/\s+/, $value))
|
---|
137 | {
|
---|
138 | $nocolor{$termtype} = "true";
|
---|
139 | }
|
---|
140 | }
|
---|
141 | else
|
---|
142 | {
|
---|
143 | $colors{$option} = color($value);
|
---|
144 | }
|
---|
145 | }
|
---|
146 | close(PREFS);
|
---|
147 | }
|
---|
148 |
|
---|
149 | sub srcscan
|
---|
150 | {
|
---|
151 | # Usage: srcscan($text, $normalColor)
|
---|
152 | # $text -- the text to colorize
|
---|
153 | # $normalColor -- The escape sequence to use for non-source text.
|
---|
154 |
|
---|
155 | # Looks for text between ` and ', and colors it srcColor.
|
---|
156 |
|
---|
157 | my($line, $normalColor) = @_;
|
---|
158 |
|
---|
159 | my($srcon) = color("reset") . $colors{"srcColor"};
|
---|
160 | my($srcoff) = color("reset") . $normalColor;
|
---|
161 |
|
---|
162 | $line = $normalColor . $line;
|
---|
163 |
|
---|
164 | # This substitute replaces `foo' with `AfooB' where A is the escape
|
---|
165 | # sequence that turns on the the desired source color, and B is the
|
---|
166 | # escape sequence that returns to $normalColor.
|
---|
167 | $line =~ s/\`(.*?)\'/\`$srcon$1$srcoff\'/g;
|
---|
168 |
|
---|
169 | print($line, color("reset"));
|
---|
170 | }
|
---|
171 |
|
---|
172 | #
|
---|
173 | # Main program
|
---|
174 | #
|
---|
175 |
|
---|
176 | # Set up default values for colors and compilers.
|
---|
177 | initDefaults();
|
---|
178 |
|
---|
179 | # Read the configuration file, if there is one.
|
---|
180 | $configFile = $ENV{"HOME"} . "/.colorgccrc";
|
---|
181 | if (-f $configFile)
|
---|
182 | {
|
---|
183 | loadPreferences($configFile);
|
---|
184 | }
|
---|
185 |
|
---|
186 | # Figure out which compiler to invoke based on our program name.
|
---|
187 | $0 =~ m%.*/(.*)$%;
|
---|
188 | $progName = $1 || $0;
|
---|
189 |
|
---|
190 | $compiler = $compilerPaths{$progName} || $compilerPaths{"gcc"};
|
---|
191 |
|
---|
192 | # Get the terminal type.
|
---|
193 | $terminal = $ENV{"TERM"} || "dumb";
|
---|
194 |
|
---|
195 | # If it's in the list of terminal types not to color, or if
|
---|
196 | # we're writing to something that's not a tty, don't do color.
|
---|
197 | if (! -t STDOUT || $nocolor{$terminal})
|
---|
198 | {
|
---|
199 | exec $compiler, @ARGV
|
---|
200 | or die("Couldn't exec");
|
---|
201 | }
|
---|
202 |
|
---|
203 | # Keep the pid of the compiler process so we can get its return
|
---|
204 | # code and use that as our return code.
|
---|
205 | $compiler_pid = open3('<&STDIN', \*GCCOUT, '', $compiler, @ARGV);
|
---|
206 |
|
---|
207 | # Colorize the output from the compiler.
|
---|
208 | while(<GCCOUT>)
|
---|
209 | {
|
---|
210 | if (m/^(.*?):([0-9]+):(.*)$/) # filename:lineno:message
|
---|
211 | {
|
---|
212 | $field1 = $1 || "";
|
---|
213 | $field2 = $2 || "";
|
---|
214 | $field3 = $3 || "";
|
---|
215 |
|
---|
216 | if ($field3 =~ m/\s+warning:.*/)
|
---|
217 | {
|
---|
218 | # Warning
|
---|
219 | print($colors{"warningFileNameColor"}, "$field1:", color("reset"));
|
---|
220 | print($colors{"warningNumberColor"}, "$field2:", color("reset"));
|
---|
221 | srcscan($field3, $colors{"warningMessageColor"});
|
---|
222 | }
|
---|
223 | else
|
---|
224 | {
|
---|
225 | # Error
|
---|
226 | print($colors{"errorFileNameColor"}, "$field1:", color("reset"));
|
---|
227 | print($colors{"errorNumberColor"}, "$field2:", color("reset"));
|
---|
228 | srcscan($field3, $colors{"errorMessageColor"});
|
---|
229 | }
|
---|
230 | print("\n");
|
---|
231 | }
|
---|
232 | elsif (m/^(.*?):(.+):$/) # filename:message:
|
---|
233 | {
|
---|
234 | # No line number, treat as an "introductory" line of text.
|
---|
235 | srcscan($_, $colors{"introColor"});
|
---|
236 | }
|
---|
237 | else # Anything else.
|
---|
238 | {
|
---|
239 | # Doesn't seem to be a warning or an error. Print normally.
|
---|
240 | print(color("reset"), $_);
|
---|
241 | }
|
---|
242 | }
|
---|
243 |
|
---|
244 | # Get the return code of the compiler and exit with that.
|
---|
245 | waitpid($compiler_pid, 0);
|
---|
246 | exit ($? >> 8);
|
---|
247 |
|
---|
248 |
|
---|
249 |
|
---|
250 |
|
---|
251 |
|
---|