1 /****************************************************************************
3 ** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved.
5 ** This file is part of the Qt Script Generator project on Trolltech Labs.
7 ** This file may be used under the terms of the GNU General Public
8 ** License version 2.0 as published by the Free Software Foundation
9 ** and appearing in the file LICENSE.GPL included in the packaging of
10 ** this file. Please review the following information to ensure GNU
11 ** General Public Licensing requirements will be met:
12 ** http://www.trolltech.com/products/qt/opensource.html
14 ** If you are unsure which license is appropriate for your use, please
15 ** review the following information:
16 ** http://www.trolltech.com/products/qt/licensing.html or contact the
17 ** sales department at sales@trolltech.com.
19 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
20 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22 ****************************************************************************/
34 static bool preprocess(const QString
&sourceFile
, const QString
&targetFile
, const QString
&commandLineIncludes
= QString())
36 rpp::pp_environment env
;
37 rpp::pp
preprocess(env
);
39 rpp::pp_null_output_iterator null_out
;
41 const char *ppconfig
= ":/trolltech/generator/parser/rpp/pp-qt-configuration";
44 if (!file
.open(QFile::ReadOnly
)) {
45 fprintf(stderr
, "Preprocessor configuration file not found '%s'\n", ppconfig
);
49 QByteArray ba
= file
.readAll();
51 preprocess
.operator() (ba
.constData(), ba
.constData() + ba
.size(), null_out
);
54 includes
<< QString(".");
56 #if defined(Q_OS_WIN32)
57 char *path_splitter
= ";";
59 const char *path_splitter
= ":";
62 // Environment INCLUDE
63 QString includePath
= getenv("INCLUDE");
64 if (!includePath
.isEmpty())
65 includes
+= includePath
.split(path_splitter
);
67 // Includes from the command line
68 if (!commandLineIncludes
.isEmpty())
69 includes
+= commandLineIncludes
.split(path_splitter
);
72 QString qtdir
= getenv ("QTDIR");
73 if (qtdir
.isEmpty()) {
75 qWarning("QTDIR environment variable not set. Assuming standard binary install using frameworks.");
76 QString frameworkDir
= "/Library/Frameworks";
77 includes
<< (frameworkDir
+ "/QtXml.framework/Headers");
78 includes
<< (frameworkDir
+ "/QtNetwork.framework/Headers");
79 includes
<< (frameworkDir
+ "/QtCore.framework/Headers");
80 includes
<< (frameworkDir
+ "/QtGui.framework/Headers");
81 includes
<< (frameworkDir
+ "/QtOpenGL.framework/Headers");
82 includes
<< frameworkDir
;
84 qWarning("QTDIR environment variable not set. This may cause problems with finding the necessary include files.");
88 includes
<< (qtdir
+ "/QtXml");
89 includes
<< (qtdir
+ "/QtNetwork");
90 includes
<< (qtdir
+ "/QtCore");
91 includes
<< (qtdir
+ "/QtGui");
92 includes
<< (qtdir
+ "/QtOpenGL");
96 foreach (QString include
, includes
)
97 preprocess
.push_include_path(QDir::convertSeparators(include
).toStdString());
99 QString currentDir
= QDir::current().absolutePath();
100 QFileInfo
sourceInfo(sourceFile
);
101 QDir::setCurrent(sourceInfo
.absolutePath());
104 result
.reserve (20 * 1024); // 20K
106 result
+= "# 1 \"builtins\"\n";
108 result
+= sourceFile
.toStdString();
111 preprocess
.file (sourceInfo
.fileName().toStdString(),
112 rpp::pp_output_iterator
<std::string
> (result
));
114 QDir::setCurrent(currentDir
);
117 if (!f
.open(QIODevice::WriteOnly
| QIODevice::Text
)) {
118 fprintf(stderr
, "Failed to write preprocessed file: %s\n", qPrintable(targetFile
));
120 f
.write(result
.c_str(), result
.length());