| 
									
										
										
										
											2012-10-02 09:12:39 +02:00
										 |  |  | /****************************************************************************
 | 
					
						
							| 
									
										
										
										
											2011-02-18 14:48:16 +01:00
										 |  |  | ** | 
					
						
							| 
									
										
										
										
											2013-01-28 17:12:19 +01:00
										 |  |  | ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). | 
					
						
							| 
									
										
										
										
											2012-10-02 09:12:39 +02:00
										 |  |  | ** Contact: http://www.qt-project.org/legal
 | 
					
						
							| 
									
										
										
										
											2011-02-18 14:48:16 +01:00
										 |  |  | ** | 
					
						
							| 
									
										
										
										
											2012-10-02 09:12:39 +02:00
										 |  |  | ** This file is part of Qt Creator. | 
					
						
							| 
									
										
										
										
											2011-02-18 14:48:16 +01:00
										 |  |  | ** | 
					
						
							| 
									
										
										
										
											2012-10-02 09:12:39 +02:00
										 |  |  | ** Commercial License Usage | 
					
						
							|  |  |  | ** Licensees holding valid commercial Qt licenses may use this file in | 
					
						
							|  |  |  | ** accordance with the commercial license agreement provided with the | 
					
						
							|  |  |  | ** Software or, alternatively, in accordance with the terms contained in | 
					
						
							|  |  |  | ** a written agreement between you and Digia.  For licensing terms and | 
					
						
							|  |  |  | ** conditions see http://qt.digia.com/licensing.  For further information
 | 
					
						
							|  |  |  | ** use the contact form at http://qt.digia.com/contact-us.
 | 
					
						
							| 
									
										
										
										
											2011-02-18 14:48:16 +01:00
										 |  |  | ** | 
					
						
							|  |  |  | ** GNU Lesser General Public License Usage | 
					
						
							| 
									
										
										
										
											2012-10-02 09:12:39 +02:00
										 |  |  | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
					
						
							|  |  |  | ** General Public License version 2.1 as published by the Free Software | 
					
						
							|  |  |  | ** Foundation and appearing in the file LICENSE.LGPL included in the | 
					
						
							|  |  |  | ** packaging of this file.  Please review the following information to | 
					
						
							|  |  |  | ** ensure the GNU Lesser General Public License version 2.1 requirements | 
					
						
							|  |  |  | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
 | 
					
						
							|  |  |  | ** | 
					
						
							|  |  |  | ** In addition, as a special exception, Digia gives you certain additional | 
					
						
							|  |  |  | ** rights.  These rights are described in the Digia Qt LGPL Exception | 
					
						
							| 
									
										
										
										
											2011-02-18 14:48:16 +01:00
										 |  |  | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | 
					
						
							|  |  |  | ** | 
					
						
							| 
									
										
										
										
											2012-10-02 09:12:39 +02:00
										 |  |  | ****************************************************************************/ | 
					
						
							| 
									
										
										
										
											2009-03-09 15:49:51 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <PreprocessorEnvironment.h>
 | 
					
						
							|  |  |  | #include <PreprocessorClient.h>
 | 
					
						
							|  |  |  | #include <pp.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-13 15:29:45 +02:00
										 |  |  | #include <QCoreApplication>
 | 
					
						
							| 
									
										
										
										
											2009-03-09 15:49:51 +01:00
										 |  |  | #include <QFile>
 | 
					
						
							| 
									
										
										
										
											2009-05-13 15:29:45 +02:00
										 |  |  | #include <QStringList>
 | 
					
						
							|  |  |  | #include <QDir>
 | 
					
						
							| 
									
										
										
										
											2012-08-06 13:42:46 +02:00
										 |  |  | #include <QDebug>
 | 
					
						
							| 
									
										
										
										
											2009-05-13 15:29:45 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-09 15:49:51 +01:00
										 |  |  | #include <iostream>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | using namespace CPlusPlus; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-13 15:29:45 +02:00
										 |  |  | class MakeDepend: public Client | 
					
						
							| 
									
										
										
										
											2009-03-09 15:49:51 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-05-13 15:29:45 +02:00
										 |  |  |     Environment *env; | 
					
						
							|  |  |  |     QList<QDir> systemDirs; | 
					
						
							| 
									
										
										
										
											2009-05-13 18:29:35 +02:00
										 |  |  |     QStringList included; | 
					
						
							| 
									
										
										
										
											2009-05-13 15:29:45 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |     MakeDepend(Environment *env) | 
					
						
							|  |  |  |         : env(env) | 
					
						
							|  |  |  |     { } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-13 18:29:35 +02:00
										 |  |  |     QStringList includedFiles() const | 
					
						
							|  |  |  |     { return included; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-13 15:29:45 +02:00
										 |  |  |     void addSystemDir(const QDir &dir) | 
					
						
							|  |  |  |     { systemDirs.append(dir); } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     void addSystemDir(const QString &path) | 
					
						
							|  |  |  |     { systemDirs.append(QDir(path)); } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     virtual void macroAdded(const Macro &) | 
					
						
							|  |  |  |     { } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-13 18:29:35 +02:00
										 |  |  |     void addInclude(const QString &absoluteFilePath) | 
					
						
							|  |  |  |     { included.append(absoluteFilePath); } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-29 10:03:32 +02:00
										 |  |  |     virtual void sourceNeeded(QString &fileName, IncludeType mode, unsigned) | 
					
						
							| 
									
										
										
										
											2009-05-13 15:29:45 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2009-06-29 10:03:32 +02:00
										 |  |  |         const QString currentFile = env->currentFile; | 
					
						
							| 
									
										
										
										
											2009-05-13 18:29:35 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-13 15:29:45 +02:00
										 |  |  |         if (mode == IncludeLocal) { | 
					
						
							| 
									
										
										
										
											2009-05-13 18:29:35 +02:00
										 |  |  |             const QFileInfo currentFileInfo(currentFile); | 
					
						
							|  |  |  |             const QDir dir = currentFileInfo.dir(); | 
					
						
							| 
									
										
										
										
											2009-05-13 15:29:45 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-13 18:29:35 +02:00
										 |  |  |             // ### cleanup
 | 
					
						
							| 
									
										
										
										
											2009-05-13 15:29:45 +02:00
										 |  |  |             QFileInfo fileInfo(dir, fileName); | 
					
						
							|  |  |  |             if (fileInfo.exists()) { | 
					
						
							| 
									
										
										
										
											2009-05-13 18:29:35 +02:00
										 |  |  |                 addInclude(fileInfo.absoluteFilePath()); | 
					
						
							| 
									
										
										
										
											2009-05-13 15:29:45 +02:00
										 |  |  |                 return; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2009-03-09 15:49:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-13 15:29:45 +02:00
										 |  |  |         foreach (const QDir &dir, systemDirs) { | 
					
						
							|  |  |  |             QFileInfo fileInfo(dir, fileName); | 
					
						
							|  |  |  |             if (fileInfo.exists() && fileInfo.isFile()) { | 
					
						
							| 
									
										
										
										
											2009-05-13 18:29:35 +02:00
										 |  |  |                 addInclude(fileInfo.absoluteFilePath()); | 
					
						
							| 
									
										
										
										
											2009-05-13 15:29:45 +02:00
										 |  |  |                 return; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-13 18:29:35 +02:00
										 |  |  | #ifdef PP_WITH_DIAGNOSTICS
 | 
					
						
							|  |  |  |         std::cerr << qPrintable(currentFile) << ':' << line << ": error: " | 
					
						
							|  |  |  |                 << qPrintable(fileName) << ": No such file or directory" << std::endl; | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2009-05-13 15:29:45 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-16 12:04:33 +01:00
										 |  |  |     virtual void passedMacroDefinitionCheck(unsigned, unsigned, const Macro &) | 
					
						
							| 
									
										
										
										
											2009-10-27 11:31:26 +01:00
										 |  |  |     { } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-04-24 13:44:41 +02:00
										 |  |  |     virtual void failedMacroDefinitionCheck(unsigned, const ByteArrayRef &) | 
					
						
							| 
									
										
										
										
											2009-10-27 11:31:26 +01:00
										 |  |  |     { } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-16 12:04:33 +01:00
										 |  |  |     virtual void notifyMacroReference(unsigned, unsigned, const Macro &) | 
					
						
							|  |  |  |     { } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     virtual void startExpandingMacro(unsigned, unsigned, const Macro &, | 
					
						
							| 
									
										
										
										
											2012-04-24 13:44:41 +02:00
										 |  |  |                                      const QVector<MacroArgumentReference> &) | 
					
						
							| 
									
										
										
										
											2009-05-13 15:29:45 +02:00
										 |  |  |     { } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     virtual void stopExpandingMacro(unsigned, const Macro &) | 
					
						
							|  |  |  |     { } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-03 20:48:18 +02:00
										 |  |  |     virtual void markAsIncludeGuard(const QByteArray &) | 
					
						
							|  |  |  |     { } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-13 15:29:45 +02:00
										 |  |  |     virtual void startSkippingBlocks(unsigned) | 
					
						
							|  |  |  |     { } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     virtual void stopSkippingBlocks(unsigned) | 
					
						
							|  |  |  |     { } | 
					
						
							| 
									
										
										
										
											2013-01-16 12:04:33 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     virtual void sourceNeeded(unsigned, QString &, IncludeType) | 
					
						
							|  |  |  |     { } | 
					
						
							| 
									
										
										
										
											2009-05-13 15:29:45 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-27 11:31:26 +01:00
										 |  |  | int make_depend(QCoreApplication *app); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-13 15:29:45 +02:00
										 |  |  | int main(int argc, char *argv[]) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2009-05-13 18:29:35 +02:00
										 |  |  |     QCoreApplication app(argc, argv); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-27 11:31:26 +01:00
										 |  |  |     QStringList args = app.arguments(); | 
					
						
							|  |  |  |     args.removeFirst(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     foreach (const QString &fileName, args) { | 
					
						
							|  |  |  |         QFile file(fileName); | 
					
						
							|  |  |  |         if (file.open(QFile::ReadOnly)) { | 
					
						
							|  |  |  |             QTextStream in(&file); | 
					
						
							|  |  |  |             const QString source = in.readAll(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             Environment env; | 
					
						
							|  |  |  |             Preprocessor pp(/*client=*/ 0, &env); | 
					
						
							| 
									
										
										
										
											2012-04-24 13:44:41 +02:00
										 |  |  |             const QByteArray preprocessedCode = pp.run(fileName, source); | 
					
						
							| 
									
										
										
										
											2009-10-27 11:31:26 +01:00
										 |  |  |             std::cout << preprocessedCode.constData(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-27 16:02:40 +02:00
										 |  |  | int make_depend(QCoreApplication *) | 
					
						
							| 
									
										
										
										
											2009-10-27 11:31:26 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-03-27 16:02:40 +02:00
										 |  |  |     QStringList todo = QCoreApplication::arguments(); | 
					
						
							| 
									
										
										
										
											2009-05-13 18:29:35 +02:00
										 |  |  |     todo.removeFirst(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (todo.isEmpty()) | 
					
						
							| 
									
										
										
										
											2011-08-09 13:43:56 +02:00
										 |  |  |         todo.append(QString::fromLocal8Bit(qgetenv("QTDIR")) + QLatin1String("/include/QtCore/QtCore")); | 
					
						
							| 
									
										
										
										
											2009-05-13 18:29:35 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     QMap<QString, QStringList> processed; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     while (! todo.isEmpty()) { | 
					
						
							|  |  |  |         const QString fn = todo.takeFirst(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (processed.contains(fn)) | 
					
						
							|  |  |  |             continue; | 
					
						
							| 
									
										
										
										
											2009-03-09 15:49:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-13 18:29:35 +02:00
										 |  |  |         QStringList deps; | 
					
						
							| 
									
										
										
										
											2009-03-09 15:49:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-13 18:29:35 +02:00
										 |  |  |         QFile file(fn); | 
					
						
							| 
									
										
										
										
											2009-05-13 15:29:45 +02:00
										 |  |  |         if (file.open(QFile::ReadOnly)) { | 
					
						
							|  |  |  |             // ### we should QTextStream here.
 | 
					
						
							|  |  |  |             const QByteArray code = file.readAll(); | 
					
						
							| 
									
										
										
										
											2009-05-13 18:29:35 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             Environment env; | 
					
						
							|  |  |  |             MakeDepend client(&env); | 
					
						
							| 
									
										
										
										
											2011-08-09 13:43:56 +02:00
										 |  |  |             client.addSystemDir(QString::fromLocal8Bit(qgetenv("QTDIR")) + QLatin1String("/include")); | 
					
						
							| 
									
										
										
										
											2009-05-13 18:29:35 +02:00
										 |  |  |             Preprocessor preproc(&client, &env); | 
					
						
							| 
									
										
										
										
											2012-04-24 13:44:41 +02:00
										 |  |  |             preproc.run(fn, code, /*result = */ 0); | 
					
						
							| 
									
										
										
										
											2009-05-13 18:29:35 +02:00
										 |  |  |             deps = client.includedFiles(); | 
					
						
							|  |  |  |             todo += deps; | 
					
						
							| 
									
										
										
										
											2009-05-13 15:29:45 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2009-05-13 18:29:35 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         processed.insert(fn, deps); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     QMapIterator<QString, QStringList> it(processed); | 
					
						
							|  |  |  |     while (it.hasNext()) { | 
					
						
							|  |  |  |         it.next(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (it.value().isEmpty()) | 
					
						
							|  |  |  |             continue; // no deps, nothing to do.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         std::cout << qPrintable(it.key()) << ": \\\n  " << qPrintable(it.value().join(QLatin1String(" \\\n  "))) | 
					
						
							|  |  |  |                 << std::endl << std::endl; | 
					
						
							| 
									
										
										
										
											2009-05-13 15:29:45 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2009-03-09 15:49:51 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } |