2 # Copyright (C) 2005 Nikolas Zimmermann <wildfox@kde.org>
3 # Copyright (C) 2011 Google Inc.
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Library General Public
7 # License as published by the Free Software Foundation; either
8 # version 2 of the License, or (at your option) any later version.
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Library General Public License for more details.
15 # You should have received a copy of the GNU Library General Public License
16 # along with this library; see the file COPYING.LIB. If not, write to
17 # the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 # Boston, MA 02110-1301, USA.
30 our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
33 @EXPORT = qw(&applyPreprocessor);
38 # Returns an array of lines.
43 my $preprocessor = shift;
49 $preprocessor = $ENV{CC};
50 } elsif (($Config::Config{'osname'}) =~ /solaris/i) {
51 $preprocessor = "/usr/sfw/bin/gcc";
53 $preprocessor = "/usr/bin/gcc";
55 push(@args, qw(-E -P -x c++));
58 # Remove double quotations from $defines and extract macros.
59 # For example, if $defines is ' "A=1" "B=1" C=1 "" D ',
60 # then it is converted into four macros -DA=1, -DB=1, -DC=1 and -DD.
62 my @macros = grep { $_ } split(/\s+/, $defines); # grep skips empty macros.
63 @macros = map { "-D$_" } @macros;
66 if ($Config{osname} eq "cygwin" || $Config{osname} eq 'MSWin32') {
67 # This call can fail if Windows rebases cygwin, so retry a few times until it succeeds.
68 for (my $tries = 0; !$pid && ($tries < 20); $tries++) {
70 # Suppress STDERR so that if we're using cl.exe, the output
71 # name isn't needlessly echoed.
72 use Symbol 'gensym'; my $err = gensym;
73 $pid = open3(\*PP_IN, \*PP_OUT, $err, split(' ', $preprocessor), @args, @macros, $fileName);
80 $pid = open2(\*PP_OUT, \*PP_IN, split(' ', $preprocessor), @args, @macros, $fileName);
83 my @documentContent = <PP_OUT>;
86 return @documentContent;