preprocessor.pm 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #
  2. # Copyright (C) 2005 Nikolas Zimmermann <wildfox@kde.org>
  3. # Copyright (C) 2011 Google Inc.
  4. #
  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.
  9. #
  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.
  14. #
  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.
  19. #
  20. use strict;
  21. use warnings;
  22. use Config;
  23. use IPC::Open2;
  24. use IPC::Open3;
  25. BEGIN {
  26. use Exporter ();
  27. our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
  28. $VERSION = 1.00;
  29. @ISA = qw(Exporter);
  30. @EXPORT = qw(&applyPreprocessor);
  31. %EXPORT_TAGS = ( );
  32. @EXPORT_OK = ();
  33. }
  34. # Returns an array of lines.
  35. sub applyPreprocessor
  36. {
  37. my $fileName = shift;
  38. my $defines = shift;
  39. my $preprocessor = shift;
  40. my @args = ();
  41. if (!$preprocessor) {
  42. require Config;
  43. if ($ENV{CC}) {
  44. $preprocessor = $ENV{CC};
  45. } elsif (($Config::Config{'osname'}) =~ /solaris/i) {
  46. $preprocessor = "/usr/sfw/bin/gcc";
  47. } elsif (-x "/usr/bin/clang") {
  48. $preprocessor = "/usr/bin/clang";
  49. } else {
  50. $preprocessor = "/usr/bin/gcc";
  51. }
  52. push(@args, qw(-E -P -x c++));
  53. }
  54. # Remove double quotations from $defines and extract macros.
  55. # For example, if $defines is ' "A=1" "B=1" C=1 "" D ',
  56. # then it is converted into four macros -DA=1, -DB=1, -DC=1 and -DD.
  57. $defines =~ s/\"//g;
  58. my @macros = grep { $_ } split(/\s+/, $defines); # grep skips empty macros.
  59. @macros = map { "-D$_" } @macros;
  60. my $pid = 0;
  61. if ($Config{osname} eq "cygwin" || $Config{osname} eq 'MSWin32') {
  62. # This call can fail if Windows rebases cygwin, so retry a few times until it succeeds.
  63. for (my $tries = 0; !$pid && ($tries < 20); $tries++) {
  64. eval {
  65. # Suppress STDERR so that if we're using cl.exe, the output
  66. # name isn't needlessly echoed.
  67. use Symbol 'gensym'; my $err = gensym;
  68. $pid = open3(\*PP_IN, \*PP_OUT, $err, split(' ', $preprocessor), @args, @macros, $fileName);
  69. 1;
  70. } or do {
  71. sleep 1;
  72. }
  73. };
  74. } else {
  75. $pid = open2(\*PP_OUT, \*PP_IN, split(' ', $preprocessor), @args, @macros, $fileName);
  76. }
  77. close PP_IN;
  78. my @documentContent = <PP_OUT>;
  79. close PP_OUT;
  80. waitpid($pid, 0);
  81. return @documentContent;
  82. }
  83. 1;