574d34893307bc4b1507e879d1aa2a900d81ce46
[profile/ivi/qtbase.git] / qmake / generators / win32 / winmakefile.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the qmake application of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "winmakefile.h"
43 #include "option.h"
44 #include "project.h"
45 #include "meta.h"
46 #include <qtextstream.h>
47 #include <qstring.h>
48 #include <qhash.h>
49 #include <qregexp.h>
50 #include <qstringlist.h>
51 #include <qdir.h>
52 #include <stdlib.h>
53
54 QT_BEGIN_NAMESPACE
55
56 Win32MakefileGenerator::Win32MakefileGenerator() : MakefileGenerator()
57 {
58 }
59
60 int
61 Win32MakefileGenerator::findHighestVersion(const QString &d, const QString &stem, const QString &ext)
62 {
63     QString bd = Option::fixPathToLocalOS(d, true);
64     if(!exists(bd))
65         return -1;
66
67     QMakeMetaInfo libinfo;
68     bool libInfoRead = libinfo.readLib(bd + Option::dir_sep + stem);
69
70     // If the library, for which we're trying to find the highest version
71     // number, is a static library
72     if (libInfoRead && libinfo.values("QMAKE_PRL_CONFIG").contains("staticlib"))
73         return -1;
74
75     if(!project->values("QMAKE_" + stem.toUpper() + "_VERSION_OVERRIDE").isEmpty())
76         return project->values("QMAKE_" + stem.toUpper() + "_VERSION_OVERRIDE").first().toInt();
77
78     int biggest=-1;
79     if(!project->isActiveConfig("no_versionlink")) {
80         static QHash<QString, QStringList> dirEntryListCache;
81         QStringList entries = dirEntryListCache.value(bd);
82         if (entries.isEmpty()) {
83             QDir dir(bd);
84             entries = dir.entryList();
85             dirEntryListCache.insert(bd, entries);
86         }
87
88         QRegExp regx(QString("((lib)?%1([0-9]*)).(%2|prl)$").arg(stem).arg(ext), Qt::CaseInsensitive);
89         for(QStringList::Iterator it = entries.begin(); it != entries.end(); ++it) {
90             if(regx.exactMatch((*it))) {
91                                 if (!regx.cap(3).isEmpty()) {
92                                         bool ok = true;
93                                         int num = regx.cap(3).toInt(&ok);
94                                         biggest = qMax(biggest, (!ok ? -1 : num));
95                                 }
96                         }
97         }
98     }
99     if(libInfoRead
100        && !libinfo.values("QMAKE_PRL_CONFIG").contains("staticlib")
101        && !libinfo.isEmpty("QMAKE_PRL_VERSION"))
102        biggest = qMax(biggest, libinfo.first("QMAKE_PRL_VERSION").replace(".", "").toInt());
103     return biggest;
104 }
105
106 bool
107 Win32MakefileGenerator::findLibraries()
108 {
109     QList<QMakeLocalFileName> dirs;
110     {
111         const QStringList &libpaths = project->values("QMAKE_LIBDIR");
112         for (QStringList::ConstIterator libpathit = libpaths.begin();
113             libpathit != libpaths.end(); ++libpathit)
114             dirs.append(QMakeLocalFileName((*libpathit)));
115     }
116   const QString lflags[] = { "QMAKE_LIBS", "QMAKE_LIBS_PRIVATE", QString() };
117   for (int i = 0; !lflags[i].isNull(); i++) {
118     QStringList &l = project->values(lflags[i]);
119     for(QStringList::Iterator it = l.begin(); it != l.end();) {
120         QChar quote;
121         bool modified_opt = false, remove = false;
122         QString opt = (*it).trimmed();
123         if((opt[0] == '\'' || opt[0] == '"') && opt[(int)opt.length()-1] == opt[0]) {
124             quote = opt[0];
125             opt = opt.mid(1, opt.length()-2);
126         }
127         if(opt.startsWith("/LIBPATH:")) {
128             dirs.append(QMakeLocalFileName(opt.mid(9)));
129         } else if(opt.startsWith("-L") || opt.startsWith("/L")) {
130             QString libpath = opt.mid(2);
131             QMakeLocalFileName l(libpath);
132             if(!dirs.contains(l)) {
133                 dirs.append(l);
134                 modified_opt = true;
135                 if (!quote.isNull()) {
136                     libpath = quote + libpath + quote;
137                     quote = QChar();
138                 }
139                 (*it) = "/LIBPATH:" + libpath;
140             } else {
141                 remove = true;
142             }
143         } else if(opt.startsWith("-l") || opt.startsWith("/l")) {
144             QString lib = opt.right(opt.length() - 2), out;
145             if(!lib.isEmpty()) {
146                 QString suffix;
147                 if(!project->isEmpty("QMAKE_" + lib.toUpper() + "_SUFFIX"))
148                     suffix = project->first("QMAKE_" + lib.toUpper() + "_SUFFIX");
149                 for(QList<QMakeLocalFileName>::Iterator it = dirs.begin();
150                     it != dirs.end(); ++it) {
151                     QString extension;
152                     int ver = findHighestVersion((*it).local(), lib);
153                     if(ver > 0)
154                         extension += QString::number(ver);
155                     extension += suffix;
156                     extension += ".lib";
157                     if(QMakeMetaInfo::libExists((*it).local() + Option::dir_sep + lib) ||
158                        exists((*it).local() + Option::dir_sep + lib + extension)) {
159                         out = (*it).real() + Option::dir_sep + lib + extension;
160                         if (out.contains(QLatin1Char(' '))) {
161                             out.prepend(QLatin1Char('\"'));
162                             out.append(QLatin1Char('\"'));
163                         }
164                         break;
165                     }
166                 }
167             }
168             if(out.isEmpty())
169                 out = lib + ".lib";
170             modified_opt = true;
171             (*it) = out;
172         } else if(!exists(Option::fixPathToLocalOS(opt))) {
173             QList<QMakeLocalFileName> lib_dirs;
174             QString file = opt;
175             int slsh = file.lastIndexOf(Option::dir_sep);
176             if(slsh != -1) {
177                 lib_dirs.append(QMakeLocalFileName(file.left(slsh+1)));
178                 file = file.right(file.length() - slsh - 1);
179             } else {
180                 lib_dirs = dirs;
181             }
182             if(file.endsWith(".lib")) {
183                 file = file.left(file.length() - 4);
184                 if(!file.at(file.length()-1).isNumber()) {
185                     QString suffix;
186                     if(!project->isEmpty("QMAKE_" + file.section(Option::dir_sep, -1).toUpper() + "_SUFFIX"))
187                         suffix = project->first("QMAKE_" + file.section(Option::dir_sep, -1).toUpper() + "_SUFFIX");
188                     for(QList<QMakeLocalFileName>::Iterator dep_it = lib_dirs.begin(); dep_it != lib_dirs.end(); ++dep_it) {
189                         QString lib_tmpl(file + "%1" + suffix + ".lib");
190                         int ver = findHighestVersion((*dep_it).local(), file);
191                         if(ver != -1) {
192                             if(ver)
193                                 lib_tmpl = lib_tmpl.arg(ver);
194                             else
195                                 lib_tmpl = lib_tmpl.arg("");
196                             if(slsh != -1) {
197                                 QString dir = (*dep_it).real();
198                                 if(!dir.endsWith(Option::dir_sep))
199                                     dir += Option::dir_sep;
200                                 lib_tmpl.prepend(dir);
201                             }
202                             modified_opt = true;
203                             (*it) = lib_tmpl;
204                             break;
205                         }
206                     }
207                 }
208             }
209         }
210         if(remove) {
211             it = l.erase(it);
212         } else {
213             if(!quote.isNull() && modified_opt)
214                 (*it) = quote + (*it) + quote;
215             ++it;
216         }
217     }
218   }
219     return true;
220 }
221
222 void
223 Win32MakefileGenerator::processPrlFiles()
224 {
225     QHash<QString, bool> processed;
226     QList<QMakeLocalFileName> libdirs;
227     {
228         const QStringList &libpaths = project->values("QMAKE_LIBDIR");
229         for (QStringList::ConstIterator libpathit = libpaths.begin(); libpathit != libpaths.end(); ++libpathit)
230             libdirs.append(QMakeLocalFileName((*libpathit)));
231     }
232     for(bool ret = false; true; ret = false) {
233         //read in any prl files included..
234         QStringList l_out;
235         QStringList l = project->values("QMAKE_LIBS");
236         for(QStringList::Iterator it = l.begin(); it != l.end(); ++it) {
237             QString opt = (*it).trimmed();
238             if((opt[0] == '\'' || opt[0] == '"') && opt[(int)opt.length()-1] == opt[0])
239                 opt = opt.mid(1, opt.length()-2);
240             if(opt.startsWith("/")) {
241                 if(opt.startsWith("/LIBPATH:")) {
242                     QMakeLocalFileName l(opt.mid(9));
243                     if(!libdirs.contains(l))
244                         libdirs.append(l);
245                 }
246             } else if(!processed.contains(opt)) {
247                 if(processPrlFile(opt)) {
248                     processed.insert(opt, true);
249                     ret = true;
250                 } else if(QDir::isRelativePath(opt) || opt.startsWith("-l")) {
251                     QString tmp;
252                     if (opt.startsWith("-l"))
253                         tmp = opt.mid(2);
254                     else
255                         tmp = opt;
256                     for(QList<QMakeLocalFileName>::Iterator it = libdirs.begin(); it != libdirs.end(); ++it) {
257                         QString prl = (*it).local() + Option::dir_sep + tmp;
258                         // the original is used as the key
259                         QString orgprl = prl;
260                         if(processed.contains(prl)) {
261                             break;
262                         } else if(processPrlFile(prl)) {
263                             processed.insert(orgprl, true);
264                             ret = true;
265                             break;
266                         }
267                     }
268                 }
269             }
270             if(!opt.isEmpty())
271                 l_out.append(opt);
272         }
273         if(ret)
274             l = l_out;
275         else
276             break;
277     }
278 }
279
280
281 void Win32MakefileGenerator::processVars()
282 {
283     //If the TARGET looks like a path split it into DESTDIR and the resulting TARGET
284     if(!project->isEmpty("TARGET")) {
285         QString targ = project->first("TARGET");
286         int slsh = qMax(targ.lastIndexOf('/'), targ.lastIndexOf(Option::dir_sep));
287         if(slsh != -1) {
288             if(project->isEmpty("DESTDIR"))
289                 project->values("DESTDIR").append("");
290             else if(project->first("DESTDIR").right(1) != Option::dir_sep)
291                 project->values("DESTDIR") = QStringList(project->first("DESTDIR") + Option::dir_sep);
292             project->values("DESTDIR") = QStringList(project->first("DESTDIR") + targ.left(slsh+1));
293             project->values("TARGET") = QStringList(targ.mid(slsh+1));
294         }
295     }
296
297     project->values("QMAKE_ORIG_TARGET") = project->values("TARGET");
298     if (project->isEmpty("QMAKE_PROJECT_NAME"))
299         project->values("QMAKE_PROJECT_NAME") = project->values("QMAKE_ORIG_TARGET");
300     else if (project->first("TEMPLATE").startsWith("vc"))
301         project->values("MAKEFILE") = project->values("QMAKE_PROJECT_NAME");
302
303     if (!project->values("QMAKE_INCDIR").isEmpty())
304         project->values("INCLUDEPATH") += project->values("QMAKE_INCDIR");
305
306     if (!project->values("VERSION").isEmpty()) {
307         QStringList l = project->first("VERSION").split('.');
308         if (l.size() > 0)
309             project->values("VER_MAJ").append(l[0]);
310         if (l.size() > 1)
311             project->values("VER_MIN").append(l[1]);
312     }
313
314     // TARGET_VERSION_EXT will be used to add a version number onto the target name
315     if (project->values("TARGET_VERSION_EXT").isEmpty()
316         && !project->values("VER_MAJ").isEmpty())
317         project->values("TARGET_VERSION_EXT").append(project->values("VER_MAJ").first());
318
319     if(project->isEmpty("QMAKE_COPY_FILE"))
320         project->values("QMAKE_COPY_FILE").append("$(COPY)");
321     if(project->isEmpty("QMAKE_COPY_DIR"))
322         project->values("QMAKE_COPY_DIR").append("xcopy /s /q /y /i");
323     if(project->isEmpty("QMAKE_INSTALL_FILE"))
324         project->values("QMAKE_INSTALL_FILE").append("$(COPY_FILE)");
325     if(project->isEmpty("QMAKE_INSTALL_PROGRAM"))
326         project->values("QMAKE_INSTALL_PROGRAM").append("$(COPY_FILE)");
327     if(project->isEmpty("QMAKE_INSTALL_DIR"))
328         project->values("QMAKE_INSTALL_DIR").append("$(COPY_DIR)");
329
330     fixTargetExt();
331     processRcFileVar();
332     processFileTagsVar();
333
334     QStringList &incDir = project->values("INCLUDEPATH");
335     for(QStringList::Iterator incDir_it = incDir.begin(); incDir_it != incDir.end(); ++incDir_it) {
336     if(!(*incDir_it).isEmpty())
337         (*incDir_it) = Option::fixPathToTargetOS((*incDir_it), false, false);
338     }
339     QStringList &libDir = project->values("QMAKE_LIBDIR");
340     for(QStringList::Iterator libDir_it = libDir.begin(); libDir_it != libDir.end(); ++libDir_it) {
341     if(!(*libDir_it).isEmpty())
342         (*libDir_it) = Option::fixPathToTargetOS((*libDir_it), false, false);
343     }
344
345     if (project->values("TEMPLATE").contains("app")) {
346         project->values("QMAKE_CFLAGS") += project->values("QMAKE_CFLAGS_APP");
347         project->values("QMAKE_CXXFLAGS") += project->values("QMAKE_CXXFLAGS_APP");
348         project->values("QMAKE_LFLAGS") += project->values("QMAKE_LFLAGS_APP");
349     } else if (project->values("TEMPLATE").contains("lib") && project->isActiveConfig("dll")) {
350         if(!project->isActiveConfig("plugin") || !project->isActiveConfig("plugin_no_share_shlib_cflags")) {
351             project->values("QMAKE_CFLAGS") += project->values("QMAKE_CFLAGS_SHLIB");
352             project->values("QMAKE_CXXFLAGS") += project->values("QMAKE_CXXFLAGS_SHLIB");
353         }
354         if (project->isActiveConfig("plugin")) {
355             project->values("QMAKE_CFLAGS") += project->values("QMAKE_CFLAGS_PLUGIN");
356             project->values("QMAKE_CXXFLAGS") += project->values("QMAKE_CXXFLAGS_PLUGIN");
357             project->values("QMAKE_LFLAGS") += project->values("QMAKE_LFLAGS_PLUGIN");
358         } else {
359             project->values("QMAKE_LFLAGS") += project->values("QMAKE_LFLAGS_SHLIB");
360         }
361     }
362 }
363
364 void Win32MakefileGenerator::fixTargetExt()
365 {
366     if (project->isEmpty("QMAKE_EXTENSION_STATICLIB"))
367         project->values("QMAKE_EXTENSION_STATICLIB").append("lib");
368     if (project->isEmpty("QMAKE_EXTENSION_SHLIB"))
369         project->values("QMAKE_EXTENSION_SHLIB").append("dll");
370
371     if (!project->values("QMAKE_APP_FLAG").isEmpty()) {
372         project->values("TARGET_EXT").append(".exe");
373     } else if (project->isActiveConfig("shared")) {
374         project->values("TARGET_EXT").append(project->first("TARGET_VERSION_EXT") + "."
375                 + project->first("QMAKE_EXTENSION_SHLIB"));
376         project->values("TARGET").first() = project->first("QMAKE_PREFIX_SHLIB") + project->first("TARGET");
377     } else {
378         project->values("TARGET_EXT").append("." + project->first("QMAKE_EXTENSION_STATICLIB"));
379         project->values("TARGET").first() = project->first("QMAKE_PREFIX_STATICLIB") + project->first("TARGET");
380     }
381 }
382
383 void Win32MakefileGenerator::processRcFileVar()
384 {
385     if (Option::qmake_mode == Option::QMAKE_GENERATE_NOTHING)
386         return;
387
388     if (((!project->values("VERSION").isEmpty())
389         && project->values("RC_FILE").isEmpty()
390         && project->values("RES_FILE").isEmpty()
391         && !project->isActiveConfig("no_generated_target_info")
392         && (project->isActiveConfig("shared") || !project->values("QMAKE_APP_FLAG").isEmpty()))
393         || !project->values("QMAKE_WRITE_DEFAULT_RC").isEmpty()){
394
395         QByteArray rcString;
396         QTextStream ts(&rcString, QFile::WriteOnly);
397
398         QStringList vers = project->values("VERSION").first().split(".");
399         for (int i = vers.size(); i < 4; i++)
400             vers += "0";
401         QString versionString = vers.join(".");
402
403         QString companyName;
404         if (!project->values("QMAKE_TARGET_COMPANY").isEmpty())
405             companyName = project->values("QMAKE_TARGET_COMPANY").join(" ");
406
407         QString description;
408         if (!project->values("QMAKE_TARGET_DESCRIPTION").isEmpty())
409             description = project->values("QMAKE_TARGET_DESCRIPTION").join(" ");
410
411         QString copyright;
412         if (!project->values("QMAKE_TARGET_COPYRIGHT").isEmpty())
413             copyright = project->values("QMAKE_TARGET_COPYRIGHT").join(" ");
414
415         QString productName;
416         if (!project->values("QMAKE_TARGET_PRODUCT").isEmpty())
417             productName = project->values("QMAKE_TARGET_PRODUCT").join(" ");
418         else
419             productName = project->values("TARGET").first();
420
421         QString originalName = project->values("TARGET").first() + project->values("TARGET_EXT").first();
422         int rcLang = project->intValue("RC_LANG", 1033);            // default: English(USA)
423         int rcCodePage = project->intValue("RC_CODEPAGE", 1200);    // default: Unicode
424
425         ts << "# if defined(UNDER_CE)" << endl;
426         ts << "#  include <winbase.h>" << endl;
427         ts << "# else" << endl;
428         ts << "#  include <winver.h>" << endl;
429         ts << "# endif" << endl;
430         ts << endl;
431         ts << "VS_VERSION_INFO VERSIONINFO" << endl;
432         ts << "\tFILEVERSION " << QString(versionString).replace(".", ",") << endl;
433         ts << "\tPRODUCTVERSION " << QString(versionString).replace(".", ",") << endl;
434         ts << "\tFILEFLAGSMASK 0x3fL" << endl;
435         ts << "#ifdef _DEBUG" << endl;
436         ts << "\tFILEFLAGS VS_FF_DEBUG" << endl;
437         ts << "#else" << endl;
438         ts << "\tFILEFLAGS 0x0L" << endl;
439         ts << "#endif" << endl;
440         ts << "\tFILEOS VOS__WINDOWS32" << endl;
441         if (project->isActiveConfig("shared"))
442             ts << "\tFILETYPE VFT_DLL" << endl;
443         else
444             ts << "\tFILETYPE VFT_APP" << endl;
445         ts << "\tFILESUBTYPE 0x0L" << endl;
446         ts << "\tBEGIN" << endl;
447         ts << "\t\tBLOCK \"StringFileInfo\"" << endl;
448         ts << "\t\tBEGIN" << endl;
449         ts << "\t\t\tBLOCK \""
450            << QString("%1%2").arg(rcLang, 4, 16, QLatin1Char('0')).arg(rcCodePage, 4, 16, QLatin1Char('0'))
451            << "\"" << endl;
452         ts << "\t\t\tBEGIN" << endl;
453         ts << "\t\t\t\tVALUE \"CompanyName\", \"" << companyName << "\\0\"" << endl;
454         ts << "\t\t\t\tVALUE \"FileDescription\", \"" <<  description << "\\0\"" << endl;
455         ts << "\t\t\t\tVALUE \"FileVersion\", \"" << versionString << "\\0\"" << endl;
456         ts << "\t\t\t\tVALUE \"LegalCopyright\", \"" << copyright << "\\0\"" << endl;
457         ts << "\t\t\t\tVALUE \"OriginalFilename\", \"" << originalName << "\\0\"" << endl;
458         ts << "\t\t\t\tVALUE \"ProductName\", \"" << productName << "\\0\"" << endl;
459         ts << "\t\t\tEND" << endl;
460         ts << "\t\tEND" << endl;
461         ts << "\t\tBLOCK \"VarFileInfo\"" << endl;
462         ts << "\t\tBEGIN" << endl;
463         ts << "\t\t\tVALUE \"Translation\", "
464            << QString("0x%1").arg(rcLang, 4, 16, QLatin1Char('0'))
465            << ", " << QString("%1").arg(rcCodePage, 4) << endl;
466         ts << "\t\tEND" << endl;
467         ts << "\t\tBLOCK \"VarFileInfo\"" << endl;
468         ts << "\t\tBEGIN" << endl;
469         ts << "\t\t\tVALUE \"Translation\", 0x409, 1200" << endl;
470         ts << "\t\tEND" << endl;
471         ts << "\tEND" << endl;
472         ts << "/* End of Version info */" << endl;
473         ts << endl;
474
475         ts.flush();
476
477
478         QString rcFilename = project->values("OUT_PWD").first()
479                            + "/"
480                            + project->values("TARGET").first()
481                            + "_resource"
482                            + ".rc";
483         QFile rcFile(QDir::cleanPath(rcFilename));
484
485         bool writeRcFile = true;
486         if (rcFile.exists() && rcFile.open(QFile::ReadOnly)) {
487             writeRcFile = rcFile.readAll() != rcString;
488             rcFile.close();
489         }
490         if (writeRcFile) {
491             bool ok;
492             ok = rcFile.open(QFile::WriteOnly);
493             if (!ok) {
494                 // The file can't be opened... try creating the containing
495                 // directory first (needed for clean shadow builds)
496                 QDir().mkpath(QFileInfo(rcFile).path());
497                 ok = rcFile.open(QFile::WriteOnly);
498             }
499             if (!ok) {
500                 ::fprintf(stderr, "Cannot open for writing: %s", rcFile.fileName().toLatin1().constData());
501                 ::exit(1);
502             }
503             rcFile.write(rcString);
504             rcFile.close();
505         }
506         if (project->values("QMAKE_WRITE_DEFAULT_RC").isEmpty())
507             project->values("RC_FILE").insert(0, rcFile.fileName());
508     }
509     if (!project->values("RC_FILE").isEmpty()) {
510         if (!project->values("RES_FILE").isEmpty()) {
511             fprintf(stderr, "Both rc and res file specified.\n");
512             fprintf(stderr, "Please specify one of them, not both.");
513             exit(1);
514         }
515         QString resFile = project->values("RC_FILE").first();
516
517         // if this is a shadow build then use the absolute path of the rc file
518         if (Option::output_dir != qmake_getpwd()) {
519             QFileInfo fi(resFile);
520             project->values("RC_FILE").first() = fi.absoluteFilePath();
521         }
522
523         resFile.replace(".rc", Option::res_ext);
524         project->values("RES_FILE").prepend(fileInfo(resFile).fileName());
525         if (!project->values("OBJECTS_DIR").isEmpty()) {
526             QString resDestDir;
527             if (project->isActiveConfig("staticlib"))
528                 resDestDir = fileInfo(project->first("DESTDIR")).absoluteFilePath();
529             else
530                 resDestDir = project->first("OBJECTS_DIR");
531             resDestDir.append(Option::dir_sep);
532             project->values("RES_FILE").first().prepend(resDestDir);
533         }
534         project->values("RES_FILE").first() = Option::fixPathToTargetOS(project->values("RES_FILE").first(), false, false);
535         project->values("POST_TARGETDEPS") += project->values("RES_FILE");
536         project->values("CLEAN_FILES") += project->values("RES_FILE");
537     }
538 }
539
540 void Win32MakefileGenerator::processFileTagsVar()
541 {
542     QStringList tags;
543     tags << "SOURCES" << "GENERATED_SOURCES" << "DEF_FILE" << "RC_FILE"
544          << "TARGET" << "QMAKE_LIBS" << "DESTDIR" << "DLLDESTDIR" << "INCLUDEPATH";
545     if(!project->isEmpty("QMAKE_EXTRA_COMPILERS")) {
546         const QStringList &quc = project->values("QMAKE_EXTRA_COMPILERS");
547         for(QStringList::ConstIterator it = quc.begin(); it != quc.end(); ++it)
548             tags += project->values((*it)+".input");
549     }
550
551     //clean path
552     QStringList &filetags = project->values("QMAKE_FILETAGS");
553     for(int i = 0; i < tags.size(); ++i)
554         filetags += Option::fixPathToTargetOS(tags.at(i), false);
555 }
556
557 void Win32MakefileGenerator::writeCleanParts(QTextStream &t)
558 {
559     t << "clean: compiler_clean " << var("CLEAN_DEPS");
560     {
561         const char *clean_targets[] = { "OBJECTS", "QMAKE_CLEAN", "CLEAN_FILES", 0 };
562         for(int i = 0; clean_targets[i]; ++i) {
563             const QStringList &list = project->values(clean_targets[i]);
564             const QString del_statement("-$(DEL_FILE)");
565             if(project->isActiveConfig("no_delete_multiple_files")) {
566                 for(QStringList::ConstIterator it = list.begin(); it != list.end(); ++it)
567                     t << "\n\t" << del_statement << " " << escapeFilePath((*it));
568             } else {
569                 QString files, file;
570                 const int commandlineLimit = 2047; // NT limit, expanded
571                 for(QStringList::ConstIterator it = list.begin(); it != list.end(); ++it) {
572                     file = " " + escapeFilePath((*it));
573                     if(del_statement.length() + files.length() +
574                        qMax(fixEnvVariables(file).length(), file.length()) > commandlineLimit) {
575                         t << "\n\t" << del_statement << files;
576                         files.clear();
577                     }
578                     files += file;
579                 }
580                 if(!files.isEmpty())
581                     t << "\n\t" << del_statement << files;
582             }
583         }
584     }
585     t << endl << endl;
586
587     t << "distclean: clean";
588     {
589         const char *clean_targets[] = { "QMAKE_DISTCLEAN", 0 };
590         for(int i = 0; clean_targets[i]; ++i) {
591             const QStringList &list = project->values(clean_targets[i]);
592             const QString del_statement("-$(DEL_FILE)");
593             if(project->isActiveConfig("no_delete_multiple_files")) {
594                 for(QStringList::ConstIterator it = list.begin(); it != list.end(); ++it)
595                     t << "\n\t" << del_statement << " "
596                       << escapeFilePath(Option::fixPathToTargetOS(*it));
597             } else {
598                 QString files, file;
599                 const int commandlineLimit = 2047; // NT limit, expanded
600                 for(QStringList::ConstIterator it = list.begin(); it != list.end(); ++it) {
601                     file = " " + escapeFilePath(Option::fixPathToTargetOS(*it));
602                     if(del_statement.length() + files.length() +
603                        qMax(fixEnvVariables(file).length(), file.length()) > commandlineLimit) {
604                         t << "\n\t" << del_statement << files;
605                         files.clear();
606                     }
607                     files += file;
608                 }
609                 if(!files.isEmpty())
610                     t << "\n\t" << del_statement << files;
611             }
612         }
613     }
614     t << "\n\t-$(DEL_FILE) $(DESTDIR_TARGET)" << endl;
615     {
616         QString ofile = Option::fixPathToTargetOS(fileFixify(Option::output.fileName()));
617         if(!ofile.isEmpty())
618             t << "\t-$(DEL_FILE) " << ofile << endl;
619     }
620     t << endl;
621 }
622
623 void Win32MakefileGenerator::writeIncPart(QTextStream &t)
624 {
625     t << "INCPATH       = ";
626
627     const QStringList &incs = project->values("INCLUDEPATH");
628     for(int i = 0; i < incs.size(); ++i) {
629         QString inc = incs.at(i);
630         inc.replace(QRegExp("\\\\$"), "");
631         inc.replace(QRegExp("\""), "");
632         if(!inc.isEmpty())
633             t << "-I" << "\"" << inc << "\" ";
634     }
635     t << "-I\"" << specdir() << "\""
636       << endl;
637 }
638
639 void Win32MakefileGenerator::writeStandardParts(QTextStream &t)
640 {
641     t << "####### Compiler, tools and options" << endl << endl;
642     t << "CC            = " << var("QMAKE_CC") << endl;
643     t << "CXX           = " << var("QMAKE_CXX") << endl;
644     t << "DEFINES       = "
645       << varGlue("PRL_EXPORT_DEFINES","-D"," -D"," ")
646       << varGlue("DEFINES","-D"," -D","") << endl;
647     t << "CFLAGS        = " << var("QMAKE_CFLAGS") << " $(DEFINES)" << endl;
648     t << "CXXFLAGS      = " << var("QMAKE_CXXFLAGS") << " $(DEFINES)" << endl;
649
650     writeIncPart(t);
651     writeLibsPart(t);
652
653     t << "QMAKE         = " << var("QMAKE_QMAKE") << endl;
654     t << "IDC           = " << (project->isEmpty("QMAKE_IDC") ? QString("idc") :
655                               Option::fixPathToTargetOS(var("QMAKE_IDC"), false)) << endl;
656     t << "IDL           = " << (project->isEmpty("QMAKE_IDL") ? QString("midl") :
657                               Option::fixPathToTargetOS(var("QMAKE_IDL"), false)) << endl;
658     t << "ZIP           = " << var("QMAKE_ZIP") << endl;
659     t << "DEF_FILE      = " << varList("DEF_FILE") << endl;
660     t << "RES_FILE      = " << varList("RES_FILE") << endl; // Not on mingw, can't see why not though...
661     t << "COPY          = " << var("QMAKE_COPY") << endl;
662     t << "SED           = " << var("QMAKE_STREAM_EDITOR") << endl;
663     t << "COPY_FILE     = " << var("QMAKE_COPY_FILE") << endl;
664     t << "COPY_DIR      = " << var("QMAKE_COPY_DIR") << endl;
665     t << "DEL_FILE      = " << var("QMAKE_DEL_FILE") << endl;
666     t << "DEL_DIR       = " << var("QMAKE_DEL_DIR") << endl;
667     t << "MOVE          = " << var("QMAKE_MOVE") << endl;
668     t << "CHK_DIR_EXISTS= " << var("QMAKE_CHK_DIR_EXISTS") << endl;
669     t << "MKDIR         = " << var("QMAKE_MKDIR") << endl;
670     t << "INSTALL_FILE    = " << var("QMAKE_INSTALL_FILE") << endl;
671     t << "INSTALL_PROGRAM = " << var("QMAKE_INSTALL_PROGRAM") << endl;
672     t << "INSTALL_DIR     = " << var("QMAKE_INSTALL_DIR") << endl;
673     t << endl;
674
675     t << "####### Output directory" << endl << endl;
676     if(!project->values("OBJECTS_DIR").isEmpty())
677         t << "OBJECTS_DIR   = " << var("OBJECTS_DIR").replace(QRegExp("\\\\$"),"") << endl;
678     else
679         t << "OBJECTS_DIR   = . " << endl;
680     t << endl;
681
682     t << "####### Files" << endl << endl;
683     t << "SOURCES       = " << valList(escapeFilePaths(project->values("SOURCES")))
684       << " " << valList(escapeFilePaths(project->values("GENERATED_SOURCES"))) << endl;
685
686     // do this here so we can set DEST_TARGET to be the complete path to the final target if it is needed.
687     QString orgDestDir = var("DESTDIR");
688     QString destDir = Option::fixPathToTargetOS(orgDestDir, false);
689     if (!destDir.isEmpty() && (orgDestDir.endsWith('/') || orgDestDir.endsWith(Option::dir_sep)))
690         destDir += Option::dir_sep;
691     QString target = QString(project->first("TARGET")+project->first("TARGET_EXT"));
692     target.remove("\"");
693     project->values("DEST_TARGET").prepend(destDir + target);
694
695     writeObjectsPart(t);
696
697     writeExtraCompilerVariables(t);
698     writeExtraVariables(t);
699
700     t << "DIST          = " << varList("DISTFILES") << endl;
701     t << "QMAKE_TARGET  = " << var("QMAKE_ORIG_TARGET") << endl;
702     // The comment is important to maintain variable compatibility with Unix
703     // Makefiles, while not interpreting a trailing-slash as a linebreak
704     t << "DESTDIR        = " << escapeFilePath(destDir) << " #avoid trailing-slash linebreak" << endl;
705     t << "TARGET         = " << escapeFilePath(target) << endl;
706     t << "DESTDIR_TARGET = " << escapeFilePath(var("DEST_TARGET")) << endl;
707     t << endl;
708
709     t << "####### Implicit rules" << endl << endl;
710     writeImplicitRulesPart(t);
711
712     t << "####### Build rules" << endl << endl;
713     writeBuildRulesPart(t);
714
715     if(project->isActiveConfig("shared") && !project->values("DLLDESTDIR").isEmpty()) {
716         QStringList dlldirs = project->values("DLLDESTDIR");
717         for (QStringList::Iterator dlldir = dlldirs.begin(); dlldir != dlldirs.end(); ++dlldir) {
718             t << "\t" << "-$(COPY_FILE) \"$(DESTDIR_TARGET)\" " << Option::fixPathToTargetOS(*dlldir, false) << endl;
719         }
720     }
721     t << endl;
722
723     writeRcFilePart(t);
724
725     writeMakeQmake(t);
726
727     QStringList dist_files = fileFixify(Option::mkfile::project_files);
728     if(!project->isEmpty("QMAKE_INTERNAL_INCLUDED_FILES"))
729         dist_files += project->values("QMAKE_INTERNAL_INCLUDED_FILES");
730     if(!project->isEmpty("TRANSLATIONS"))
731         dist_files << var("TRANSLATIONS");
732     if(!project->isEmpty("FORMS")) {
733         const QStringList &forms = project->values("FORMS");
734         for (QStringList::ConstIterator formit = forms.begin(); formit != forms.end(); ++formit) {
735             QString ui_h = fileFixify((*formit) + Option::h_ext.first());
736             if(exists(ui_h))
737                 dist_files << ui_h;
738         }
739     }
740     t << "dist:" << "\n\t"
741       << "$(ZIP) " << var("QMAKE_ORIG_TARGET") << ".zip " << "$(SOURCES) $(DIST) "
742       << dist_files.join(" ") << " " << var("TRANSLATIONS") << " ";
743     if(!project->isEmpty("QMAKE_EXTRA_COMPILERS")) {
744         const QStringList &quc = project->values("QMAKE_EXTRA_COMPILERS");
745         for(QStringList::ConstIterator it = quc.begin(); it != quc.end(); ++it) {
746             const QStringList &inputs = project->values((*it)+".input");
747             for(QStringList::ConstIterator input = inputs.begin(); input != inputs.end(); ++input) {
748                 t << (*input) << " ";
749             }
750         }
751     }
752     t << endl << endl;
753
754     writeCleanParts(t);
755     writeExtraTargets(t);
756     writeExtraCompilerTargets(t);
757     t << endl << endl;
758 }
759
760 void Win32MakefileGenerator::writeLibsPart(QTextStream &t)
761 {
762     if(project->isActiveConfig("staticlib") && project->first("TEMPLATE") == "lib") {
763         t << "LIBAPP        = " << var("QMAKE_LIB") << endl;
764         t << "LIBFLAGS      = " << var("QMAKE_LIBFLAGS") << endl;
765     } else {
766         t << "LINK          = " << var("QMAKE_LINK") << endl;
767         t << "LFLAGS        = " << var("QMAKE_LFLAGS") << endl;
768         t << "LIBS          = ";
769         if(!project->values("QMAKE_LIBDIR").isEmpty())
770             writeLibDirPart(t);
771         t << var("QMAKE_LIBS") << " " << var("QMAKE_LIBS_PRIVATE") << endl;
772     }
773 }
774
775 void Win32MakefileGenerator::writeLibDirPart(QTextStream &t)
776 {
777     QStringList libDirs = project->values("QMAKE_LIBDIR");
778     for (int i = 0; i < libDirs.size(); ++i)
779         libDirs[i].remove("\"");
780     t << valGlue(libDirs,"-L\"","\" -L\"","\"") << " ";
781 }
782
783 void Win32MakefileGenerator::writeObjectsPart(QTextStream &t)
784 {
785     t << "OBJECTS       = " << valList(escapeDependencyPaths(project->values("OBJECTS"))) << endl;
786 }
787
788 void Win32MakefileGenerator::writeImplicitRulesPart(QTextStream &t)
789 {
790     t << ".SUFFIXES:";
791     for(QStringList::Iterator cppit = Option::cpp_ext.begin(); cppit != Option::cpp_ext.end(); ++cppit)
792         t << " " << (*cppit);
793     for(QStringList::Iterator cit = Option::c_ext.begin(); cit != Option::c_ext.end(); ++cit)
794         t << " " << (*cit);
795     t << endl << endl;
796     for(QStringList::Iterator cppit = Option::cpp_ext.begin(); cppit != Option::cpp_ext.end(); ++cppit)
797         t << (*cppit) << Option::obj_ext << ":\n\t" << var("QMAKE_RUN_CXX_IMP") << endl << endl;
798     for(QStringList::Iterator cit = Option::c_ext.begin(); cit != Option::c_ext.end(); ++cit)
799         t << (*cit) << Option::obj_ext << ":\n\t" << var("QMAKE_RUN_CC_IMP") << endl << endl;
800 }
801
802 void Win32MakefileGenerator::writeBuildRulesPart(QTextStream &)
803 {
804 }
805
806 void Win32MakefileGenerator::writeRcFilePart(QTextStream &t)
807 {
808     if(!project->values("RC_FILE").isEmpty()) {
809         const QString res_file = project->first("RES_FILE"),
810                        rc_file = fileFixify(project->first("RC_FILE"));
811         // The resource tool needs to have the same defines passed in as the compiler, since you may
812         // use these defines in the .rc file itself. Also, we need to add the _DEBUG define manually
813         // since the compiler defines this symbol by itself, and we use it in the automatically
814         // created rc file when VERSION is define the .pro file.
815         t << res_file << ": " << rc_file << "\n\t"
816           << var("QMAKE_RC") << (project->isActiveConfig("debug") ? " -D_DEBUG" : "") << " $(DEFINES) -fo " << res_file << " " << rc_file;
817         t << endl << endl;
818     }
819 }
820
821 QString Win32MakefileGenerator::getLibTarget()
822 {
823     return QString(project->first("TARGET") + project->first("TARGET_VERSION_EXT") + ".lib");
824 }
825
826 QString Win32MakefileGenerator::defaultInstall(const QString &t)
827 {
828     if((t != "target" && t != "dlltarget") ||
829        (t == "dlltarget" && (project->first("TEMPLATE") != "lib" || !project->isActiveConfig("shared"))) ||
830         project->first("TEMPLATE") == "subdirs")
831        return QString();
832
833     const QString root = "$(INSTALL_ROOT)";
834     QStringList &uninst = project->values(t + ".uninstall");
835     QString ret;
836     QString targetdir = Option::fixPathToTargetOS(project->first(t + ".path"), false);
837     targetdir = fileFixify(targetdir, FileFixifyAbsolute);
838     if(targetdir.right(1) != Option::dir_sep)
839         targetdir += Option::dir_sep;
840
841     if(t == "target" && project->first("TEMPLATE") == "lib") {
842         if(project->isActiveConfig("create_prl") && !project->isActiveConfig("no_install_prl") &&
843            !project->isEmpty("QMAKE_INTERNAL_PRL_FILE")) {
844             QString dst_prl = Option::fixPathToTargetOS(project->first("QMAKE_INTERNAL_PRL_FILE"));
845             int slsh = dst_prl.lastIndexOf(Option::dir_sep);
846             if(slsh != -1)
847                 dst_prl = dst_prl.right(dst_prl.length() - slsh - 1);
848             dst_prl = filePrefixRoot(root, targetdir + dst_prl);
849             ret += "-$(INSTALL_FILE) \"" + project->first("QMAKE_INTERNAL_PRL_FILE") + "\" \"" + dst_prl + "\"";
850             if(!uninst.isEmpty())
851                 uninst.append("\n\t");
852             uninst.append("-$(DEL_FILE) \"" + dst_prl + "\"");
853         }
854         if(project->isActiveConfig("create_pc")) {
855             QString dst_pc = pkgConfigFileName(false);
856             if (!dst_pc.isEmpty()) {
857                 dst_pc = filePrefixRoot(root, targetdir + dst_pc);
858                 const QString dst_pc_dir = fileInfo(dst_pc).path();
859                 if (!dst_pc_dir.isEmpty()) {
860                     if (!ret.isEmpty())
861                         ret += "\n\t";
862                     ret += mkdir_p_asstring(dst_pc_dir, true);
863                 }
864                 if(!ret.isEmpty())
865                     ret += "\n\t";
866                 const QString replace_rule("QMAKE_PKGCONFIG_INSTALL_REPLACE");
867                 if (project->isEmpty(replace_rule)
868                     || project->isActiveConfig("no_sed_meta_install")
869                     || project->isEmpty("QMAKE_STREAM_EDITOR")) {
870                     ret += "-$(INSTALL_FILE) \"" + pkgConfigFileName(true) + "\" \"" + dst_pc + "\"";
871                 } else {
872                     ret += "-$(SED)";
873                     QStringList replace_rules = project->values(replace_rule);
874                     for (int r = 0; r < replace_rules.size(); ++r) {
875                         const QString match = project->first(replace_rules.at(r) + ".match"),
876                                     replace = project->first(replace_rules.at(r) + ".replace");
877                         if (!match.isEmpty() /*&& match != replace*/)
878                             ret += " -e \"s," + match + "," + replace + ",g\"";
879                     }
880                     ret += " \"" + pkgConfigFileName(true) + "\" >\"" + dst_pc + "\"";
881                 }
882                 if(!uninst.isEmpty())
883                     uninst.append("\n\t");
884                 uninst.append("-$(DEL_FILE) \"" + dst_pc + "\"");
885             }
886         }
887         if(project->isActiveConfig("shared") && !project->isActiveConfig("plugin")) {
888             QString lib_target = getLibTarget();
889             lib_target.remove('"');
890             QString src_targ = (project->isEmpty("DESTDIR") ? QString("$(DESTDIR)") : project->first("DESTDIR")) + lib_target;
891             QString dst_targ = filePrefixRoot(root, fileFixify(targetdir + lib_target, FileFixifyAbsolute));
892             if(!ret.isEmpty())
893                 ret += "\n\t";
894             ret += QString("-$(INSTALL_FILE)") + " \"" + src_targ + "\" \"" + dst_targ + "\"";
895             if(!uninst.isEmpty())
896                 uninst.append("\n\t");
897             uninst.append("-$(DEL_FILE) \"" + dst_targ + "\"");
898         }
899     }
900
901     if(t == "dlltarget" || project->values(t + ".CONFIG").indexOf("no_dll") == -1) {
902         QString src_targ = "$(DESTDIR_TARGET)";
903         QString dst_targ = filePrefixRoot(root, fileFixify(targetdir + "$(TARGET)", FileFixifyAbsolute));
904         if(!ret.isEmpty())
905             ret += "\n\t";
906         ret += QString("-$(INSTALL_FILE)") + " \"" + src_targ + "\" \"" + dst_targ + "\"";
907         if(!uninst.isEmpty())
908             uninst.append("\n\t");
909         uninst.append("-$(DEL_FILE) \"" + dst_targ + "\"");
910     }
911     return ret;
912 }
913
914 QString Win32MakefileGenerator::escapeFilePath(const QString &path) const
915 {
916     QString ret = path;
917     if(!ret.isEmpty()) {
918         ret = unescapeFilePath(ret);
919         if(ret.contains(" "))
920             ret = "\"" + ret + "\"";
921         debug_msg(2, "EscapeFilePath: %s -> %s", path.toLatin1().constData(), ret.toLatin1().constData());
922     }
923     return ret;
924 }
925
926 QT_END_NAMESPACE