| 
									
										
										
										
											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>
 | 
					
						
							|  |  |  | #include <QtDebug>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     virtual void startExpandingMacro(unsigned, const Macro &, | 
					
						
							|  |  |  |                                      const QByteArray &, | 
					
						
							|  |  |  |                                      const QVector<MacroArgumentReference> &) | 
					
						
							|  |  |  |     { } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     virtual void stopExpandingMacro(unsigned, const Macro &) | 
					
						
							|  |  |  |     { } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     virtual void startSkippingBlocks(unsigned) | 
					
						
							|  |  |  |     { } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     virtual void stopSkippingBlocks(unsigned) | 
					
						
							|  |  |  |     { } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int main(int argc, char *argv[]) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2009-05-13 18:29:35 +02:00
										 |  |  |     QCoreApplication app(argc, argv); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     QStringList todo = app.arguments(); | 
					
						
							|  |  |  |     todo.removeFirst(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (todo.isEmpty()) | 
					
						
							|  |  |  |         todo.append(qgetenv("QTDIR") + "/include/QtCore/QtCore"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     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); | 
					
						
							|  |  |  |             client.addSystemDir(qgetenv("QTDIR") + "/include"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             Preprocessor preproc(&client, &env); | 
					
						
							| 
									
										
										
										
											2009-06-29 10:03:32 +02:00
										 |  |  |             preproc.preprocess(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; | 
					
						
							|  |  |  | } |