1
/****************************************************************************
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
5
** This file is part of the porting application of the Qt Toolkit.
7
** This file may be distributed under the terms of the Q Public License
8
** as defined by Trolltech AS of Norway and appearing in the file
9
** LICENSE.QPL included in the packaging of this file.
11
** This file may be distributed and/or modified under the terms of the
12
** GNU General Public License version 2 as published by the Free Software
13
** Foundation and appearing in the file LICENSE.GPL included in the
14
** packaging of this file.
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
17
** information about Qt Commercial License Agreements.
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
21
** Contact info@trolltech.com if any conditions of this licensing are
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27
****************************************************************************/
29
#include "projectporter.h"
33
#include <QStringList>
36
#include "proparser.h"
37
#include "textreplacement.h"
38
#include "fileporter.h"
40
#include "translationunit.h"
41
#include "codemodelattributes.h"
43
using namespace TokenEngine;
45
ProjectPorter::ProjectPorter(QString basePath, QStringList includeDirectories, QStringList qt3HeadersFilenames)
47
,includeDirectories(includeDirectories)
48
,defaultDefinitions(defaultMacros(preprocessorCache))
49
,filePorter(preprocessorCache)
50
,qt3HeadersFilenames(qt3HeadersFilenames)
55
void ProjectPorter::enableCppParsing(bool enable)
60
void ProjectPorter::enableMissingFilesWarnings(bool enable)
65
void ProjectPorter::portProject(QString fileName)
67
QFileInfo fileInfo(fileName);
68
portProject(fileInfo.path(), fileInfo.fileName());
74
void ProjectPorter::portFile(QString fileName)
77
IncludeFiles includeFiles(basePath, includeDirectories);
79
PreprocessorController preprocessor(includeFiles, preprocessorCache, qt3HeadersFilenames);
80
connect(&preprocessor, SIGNAL(error(QString, QString)), SLOT(error(QString, QString)));
82
Rpp::DefineMap definitionsCopy = *defaultDefinitions;
84
TokenSectionSequence translationUnit = preprocessor.evaluate(fileName, &definitionsCopy);
86
TranslationUnit translationUnitData = TranslationUnitAnalyzer().analyze(translationUnit);
88
// Enable attribute generation for this file.
89
enableAttributes(includeFiles, fileName);
90
// Generate attributes.
91
CodeModelAttributes().createAttributes(translationUnitData);
94
portFiles(QString(), QStringList() << fileName);
97
void ProjectPorter::error(QString type, QString text)
99
if (warnings && type == "Error")
100
cout << "Warning: " << text.toLocal8Bit().constData() << endl;
103
void ProjectPorter::portProject(QString basePath, QString proFileName)
105
QString fullInFileName = basePath + "/" + proFileName;
106
QFileInfo infileInfo(fullInFileName);
107
if (!infileInfo.exists()) {
108
cout << "Could not open file: " << QDir::convertSeparators(fullInFileName).toLocal8Bit().constData() << endl;
112
QString proFileContents = loadFile(fullInFileName);
113
QMap<QString, QString> proFileMap = proFileTagMap(proFileContents, QDir(basePath).absolutePath());
116
// Check if this is a TEMPLATE = subdirs .pro file, in that case we
117
// process each subdir (recursively).
119
QString templateTag = proFileMap["TEMPLATE"];
120
if (templateTag == "subdirs") {
121
QStringList subdirs = proFileMap["SUBDIRS"].split(" ", QString::SkipEmptyParts);
122
foreach(QString subdir, subdirs) {
123
QString newBasePath = basePath + "/" + subdir;
124
QStringList dirsInSubdir = subdir.split(QRegExp("/|\\\\"), QString::SkipEmptyParts);
125
QString newProFileName = dirsInSubdir.last() + ".pro";
126
portProject(newBasePath, newProFileName);
131
// Get headers and sources file names from .pro file.
132
QStringList sources = proFileMap["SOURCES"].split(" ", QString::SkipEmptyParts);
133
QStringList headers = proFileMap["HEADERS"].split(" ", QString::SkipEmptyParts);
134
QStringList forms = proFileMap["FORMS"].split(" ", QString::SkipEmptyParts);
136
for (int i = 0; i < forms.size(); ++i) {
137
QString ui_h = forms.at(i) + ".h";
138
if (QFile::exists(basePath + "/" + ui_h))
144
// Get include paths from the pro file.
145
QStringList includeProPaths = proFileMap["INCLUDEPATH"].split(" ", QString::SkipEmptyParts);
146
QStringList dependProPaths = proFileMap["DEPENDPATH"].split(" ", QString::SkipEmptyParts);
147
IncludeFiles includeFiles(basePath, includeDirectories + includeProPaths + dependProPaths);
149
PreprocessorController preprocessorController(includeFiles, preprocessorCache, qt3HeadersFilenames);
150
connect(&preprocessorController, SIGNAL(error(QString, QString)), SLOT(error(QString, QString)));
152
TranslationUnitAnalyzer translationUnitAnalyzer;
153
CodeModelAttributes codeModelAttributes;
155
// Enable attribute generation for header files.
156
foreach(QString headerFile, headers)
157
enableAttributes(includeFiles, headerFile);
159
// Enable attribute generation for ui.h files.
160
foreach(QString headerFile, uidoth)
161
enableAttributes(includeFiles, headerFile);
163
// Analyze each translation unit. (one per cpp file)
164
foreach(QString sourceFile, sources) {
165
cout << "." << std::flush;
166
Rpp::DefineMap definitionsCopy = *defaultDefinitions;
167
TokenSectionSequence translationUnit =
168
preprocessorController.evaluate(sourceFile, &definitionsCopy);
169
TranslationUnit translationUnitData =
170
translationUnitAnalyzer.analyze(translationUnit);
172
// Enable attribute generation for this file.
173
enableAttributes(includeFiles, sourceFile);
175
codeModelAttributes.createAttributes(translationUnitData);
182
portFiles(basePath, sources);
183
portFiles(basePath, headers);
184
if (!uidoth.isEmpty())
185
portFiles(basePath, uidoth);
187
Logger::instance()->globalState["currentFileName"] = proFileName;
188
Logger::instance()->beginSection();
189
portProFile(fullInFileName, proFileMap);
193
Port each file given in the fileNames list. If a file name is relative
194
it is assumed to be relative to basePath.
196
void ProjectPorter::portFiles(QString basePath, QStringList fileNames)
198
foreach(QString fileName, fileNames) {
199
QString fullFilePath;
200
QFileInfo fileInfo(fileName);
201
if (fileInfo.isAbsolute()) {
202
fullFilePath = QDir::cleanPath(fileName);
204
fullFilePath = QDir::cleanPath(basePath + "/" + fileName);
207
QFileInfo fullFilePathInfo(fullFilePath);
208
if (!fullFilePathInfo.exists()) {
209
cout << "Could not find file:" <<
210
QDir::convertSeparators(fullFilePath).toLocal8Bit().constData() <<endl;
214
if (!processedFilesSet.contains(fullFilePath)){
215
Logger::instance()->globalState["currentFileName"] = fullFilePath;
216
filePorter.port(fullFilePath);
217
processedFilesSet.insert(fullFilePath);
222
void ProjectPorter::portProFile(QString fileName, QMap<QString, QString> tagMap)
225
QFile proFile(fileName);
226
if (!proFile.open(QIODevice::ReadOnly))
228
QTextStream proTextStream(&proFile);
230
while (!proTextStream.atEnd())
231
lines += proTextStream.readLine();
235
// Find out what modules we should add to the QT variable.
236
QSet<QByteArray> qtModules;
238
// Add qt3support to the Qt tag
239
qtModules.insert("qt3support");
241
// Read CONFIG and add other modules.
242
QStringList config = tagMap["CONFIG"].split(" ", QString::SkipEmptyParts);
243
if (config.contains("opengl"))
244
qtModules.insert("opengl");
245
if (config.contains("xml"))
246
qtModules.insert("xml");
247
if (config.contains("sql"))
248
qtModules.insert("sql");
249
if (config.contains("network"))
250
qtModules.insert("network");
252
// Get set of used modules from the file porter.
253
qtModules += filePorter.usedQtModules();
255
// Remove gui and core.
256
qtModules.remove("gui");
257
qtModules.remove("core");
259
// Qt3Support is already added.
260
qtModules.remove("3support");
262
// Remove modules already present in the QT variable.
263
QStringList qt = tagMap["QT"].split(" ", QString::SkipEmptyParts);
264
foreach(QString name, qt) {
265
qtModules.remove(name.toLatin1());
268
Logger *logger = Logger::instance();
269
bool changesMade = false;
271
if (!qtModules.isEmpty()) {
273
QString insertText = "QT += ";
274
foreach(QByteArray module, qtModules) {
275
insertText += module + QLatin1Char(' ');
277
lines += QString("#The following line was inserted by qt3to4");
279
QString logText = "In file "
280
+ logger->globalState.value("currentFileName")
283
logger->addEntry(new PlainLogEntry("Info", "Porting", logText));
286
// Add uic3 if we have forms.
287
if (!tagMap["FORMS"].isEmpty() || !tagMap["INTERFACES"].isEmpty()) {
289
lines += QString("#The following line was inserted by qt3to4");
290
QString insertText = "CONFIG += uic3\n";
292
QString logText = "In file "
293
+ logger->globalState.value("currentFileName")
296
logger->addEntry(new PlainLogEntry("Info", "Porting", logText));
299
// Comment out any REQUIRES tag.
300
if (!tagMap["REQUIRES"].isEmpty()) {
302
QString insertText("#The following line was commented out by qt3to4");
303
for (int i = 0; i < lines.count(); ++i) {
304
if (lines.at(i).startsWith("REQUIRES")) {
305
QString lineCopy = lines.at(i);
306
lineCopy.prepend('#');
308
lines.insert(i, insertText);
309
++i; //skip ahead, we just insertet a line at i.
310
QString logText = "In file "
311
+ logger->globalState.value("currentFileName")
312
+ ": Commented out REQUIRES section";
313
logger->addEntry(new PlainLogEntry("Info", "Porting", logText));
318
// Check if any changes has been made.
320
Logger::instance()->addEntry(
321
new PlainLogEntry("Info", "Porting", QLatin1String("No changes made to file ") + fileName));
322
Logger::instance()->commitSection();
326
// Write lines to array.
328
QTextStream outProFileStream(&bob);
329
foreach(QString line, lines)
330
outProFileStream << line << QLatin1Char('\n');
331
outProFileStream.flush();
333
// Write array to file, commit log if write was successful.
334
FileWriter::WriteResult result = FileWriter::instance()->writeFileVerbously(fileName, bob);
335
if (result == FileWriter::WriteSucceeded) {
336
logger->commitSection();
337
} else if (result == FileWriter::WriteFailed) {
338
logger->revertSection();
340
new PlainLogEntry("Error", "Porting", QLatin1String("Error writing to file ") + fileName));
341
} else if (result == FileWriter::WriteSkipped) {
342
logger->revertSection();
344
new PlainLogEntry("Error", "Porting", QLatin1String("User skipped file ") + fileName));
347
logger->revertSection();
348
const QString errorString = QLatin1String("Internal error in qt3to4 - FileWriter returned invalid result code while writing to ") + fileName;
349
logger->addEntry(new PlainLogEntry("Error", "Porting", errorString));
354
Enables attribute generation for fileName. The file is looked up using the
355
provied includeFiles object.
357
void ProjectPorter::enableAttributes(const IncludeFiles &includeFiles, const QString &fileName)
359
QString resolvedFilePath = includeFiles.resolve(fileName);
360
if (!QFile::exists(resolvedFilePath))
361
resolvedFilePath = includeFiles.angleBracketLookup(fileName);
362
if (!QFile::exists(resolvedFilePath))
365
TokenContainer tokenContainer = preprocessorCache.sourceTokens(resolvedFilePath);
366
TokenAttributes *attributes = tokenContainer.tokenAttributes();
367
attributes->addAttribute("CreateAttributes", "True");