1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the qmake application of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29#include "winmakefile.h"
30#include "option.h"
31#include "project.h"
32#include "meta.h"
33#include <qtextstream.h>
34#include <qstring.h>
35#include <qhash.h>
36#include <qregularexpression.h>
37#include <qstringlist.h>
38#include <qdir.h>
39#include <stdlib.h>
40
41#include <algorithm>
42
43QT_BEGIN_NAMESPACE
44
45ProString Win32MakefileGenerator::fixLibFlag(const ProString &lib)
46{
47 if (lib.startsWith("-l")) // Fallback for unresolved -l libs.
48 return escapeFilePath(lib.mid(2) + QLatin1String(".lib"));
49 if (lib.startsWith("-L")) // Lib search path. Needed only by -l above.
50 return QLatin1String("/LIBPATH:")
51 + escapeFilePath(Option::fixPathToTargetOS(lib.mid(2).toQString(), false));
52 return escapeFilePath(Option::fixPathToTargetOS(lib.toQString(), false));
53}
54
55MakefileGenerator::LibFlagType
56Win32MakefileGenerator::parseLibFlag(const ProString &flag, ProString *arg)
57{
58 LibFlagType ret = MakefileGenerator::parseLibFlag(flag, arg);
59 if (ret != LibFlagFile)
60 return ret;
61 // MSVC compatibility. This should be deprecated.
62 if (flag.startsWith("/LIBPATH:")) {
63 *arg = flag.mid(9);
64 return LibFlagPath;
65 }
66 // These are pure qmake inventions. They *really* should be deprecated.
67 if (flag.startsWith("/L")) {
68 *arg = flag.mid(2);
69 return LibFlagPath;
70 }
71 if (flag.startsWith("/l")) {
72 *arg = flag.mid(2);
73 return LibFlagLib;
74 }
75 return LibFlagFile;
76}
77
78class LibrarySearchPath : public QMakeLocalFileName
79{
80public:
81 LibrarySearchPath() = default;
82
83 LibrarySearchPath(const QString &s)
84 : QMakeLocalFileName(s)
85 {
86 }
87
88 LibrarySearchPath(QString &&s, bool isDefault = false)
89 : QMakeLocalFileName(std::move(s)), _default(isDefault)
90 {
91 }
92
93 bool isDefault() const { return _default; }
94
95private:
96 bool _default = false;
97};
98
99bool
100Win32MakefileGenerator::findLibraries(bool linkPrl, bool mergeLflags)
101{
102 ProStringList impexts = project->values("QMAKE_LIB_EXTENSIONS");
103 if (impexts.isEmpty())
104 impexts = project->values("QMAKE_EXTENSION_STATICLIB");
105 QList<LibrarySearchPath> dirs;
106 int libidx = 0;
107 for (const ProString &dlib : project->values("QMAKE_DEFAULT_LIBDIRS"))
108 dirs.append(LibrarySearchPath(dlib.toQString(), true));
109 static const char * const lflags[] = { "LIBS", "LIBS_PRIVATE",
110 "QMAKE_LIBS", "QMAKE_LIBS_PRIVATE", nullptr };
111 for (int i = 0; lflags[i]; i++) {
112 ProStringList &l = project->values(lflags[i]);
113 for (ProStringList::Iterator it = l.begin(); it != l.end();) {
114 const ProString &opt = *it;
115 ProString arg;
116 LibFlagType type = parseLibFlag(opt, &arg);
117 if (type == LibFlagPath) {
118 const QString argqstr = arg.toQString();
119 auto dit = std::find_if(dirs.cbegin(), dirs.cend(),
120 [&argqstr](const LibrarySearchPath &p)
121 {
122 return p.real() == argqstr;
123 });
124 int idx = dit == dirs.cend()
125 ? -1
126 : std::distance(dirs.cbegin(), dit);
127 if (idx >= 0 && idx < libidx) {
128 it = l.erase(it);
129 continue;
130 }
131 const LibrarySearchPath lp(argqstr);
132 dirs.insert(libidx++, lp);
133 (*it) = "-L" + lp.real();
134 } else if (type == LibFlagLib) {
135 QString lib = arg.toQString();
136 ProString verovr =
137 project->first(ProKey("QMAKE_" + lib.toUpper() + "_VERSION_OVERRIDE"));
138 for (auto dir_it = dirs.begin(); dir_it != dirs.end(); ++dir_it) {
139 QString cand = (*dir_it).real() + Option::dir_sep + lib;
140 if (linkPrl && processPrlFile(cand, true)) {
141 (*it) = cand;
142 goto found;
143 }
144 QString libBase = (*dir_it).local() + '/' + lib + verovr;
145 for (ProStringList::ConstIterator extit = impexts.cbegin();
146 extit != impexts.cend(); ++extit) {
147 if (exists(libBase + '.' + *extit)) {
148 *it = (dir_it->isDefault() ? lib : cand)
149 + verovr + '.' + *extit;
150 goto found;
151 }
152 }
153 }
154 // We assume if it never finds it that it's correct
155 found: ;
156 } else if (linkPrl && type == LibFlagFile) {
157 QString lib = opt.toQString();
158 if (fileInfo(lib).isAbsolute()) {
159 if (processPrlFile(lib, false))
160 (*it) = lib;
161 } else {
162 for (auto dir_it = dirs.begin(); dir_it != dirs.end(); ++dir_it) {
163 QString cand = (*dir_it).real() + Option::dir_sep + lib;
164 if (processPrlFile(cand, false)) {
165 (*it) = cand;
166 break;
167 }
168 }
169 }
170 }
171
172 ProStringList &prl_libs = project->values("QMAKE_CURRENT_PRL_LIBS");
173 for (int prl = 0; prl < prl_libs.size(); ++prl)
174 it = l.insert(++it, prl_libs.at(prl));
175 prl_libs.clear();
176 ++it;
177 }
178 if (mergeLflags) {
179 ProStringList lopts;
180 for (int lit = 0; lit < l.size(); ++lit) {
181 ProString opt = l.at(lit);
182 if (opt.startsWith(QLatin1String("-L"))) {
183 if (!lopts.contains(opt))
184 lopts.append(opt);
185 } else {
186 // Make sure we keep the dependency order of libraries
187 lopts.removeAll(opt);
188 lopts.append(opt);
189 }
190 }
191 l = lopts;
192 }
193 }
194 return true;
195}
196
197bool Win32MakefileGenerator::processPrlFileBase(QString &origFile, QStringView origName,
198 QStringView fixedBase, int slashOff)
199{
200 if (MakefileGenerator::processPrlFileBase(origFile, origName, fixedBase, slashOff))
201 return true;
202 for (int off = fixedBase.length(); off > slashOff; off--) {
203 if (!fixedBase.at(off - 1).isDigit()) {
204 if (off != fixedBase.length()) {
205 return MakefileGenerator::processPrlFileBase(
206 origFile, origName, fixedBase.left(off), slashOff);
207 }
208 break;
209 }
210 }
211 return false;
212}
213
214void Win32MakefileGenerator::processVars()
215{
216 if (project->first("TEMPLATE").endsWith("aux"))
217 return;
218
219 project->values("PRL_TARGET") =
220 project->values("QMAKE_ORIG_TARGET") = project->values("TARGET");
221 if (project->isEmpty("QMAKE_PROJECT_NAME"))
222 project->values("QMAKE_PROJECT_NAME") = project->values("QMAKE_ORIG_TARGET");
223 else if (project->first("TEMPLATE").startsWith("vc"))
224 project->values("MAKEFILE") = project->values("QMAKE_PROJECT_NAME");
225
226 project->values("QMAKE_INCDIR") += project->values("QMAKE_INCDIR_POST");
227 project->values("QMAKE_LIBDIR") += project->values("QMAKE_LIBDIR_POST");
228
229 if (!project->values("QMAKE_INCDIR").isEmpty())
230 project->values("INCLUDEPATH") += project->values("QMAKE_INCDIR");
231
232 if (!project->values("VERSION").isEmpty()) {
233 QStringList l = project->first("VERSION").toQString().split('.');
234 if (l.size() > 0)
235 project->values("VER_MAJ").append(l[0]);
236 if (l.size() > 1)
237 project->values("VER_MIN").append(l[1]);
238 }
239
240 // TARGET_VERSION_EXT will be used to add a version number onto the target name
241 if (!project->isActiveConfig("skip_target_version_ext")
242 && project->values("TARGET_VERSION_EXT").isEmpty()
243 && !project->values("VER_MAJ").isEmpty())
244 project->values("TARGET_VERSION_EXT").append(project->first("VER_MAJ"));
245
246 fixTargetExt();
247 processRcFileVar();
248
249 ProStringList libs;
250 ProStringList &libDir = project->values("QMAKE_LIBDIR");
251 for (ProStringList::Iterator libDir_it = libDir.begin(); libDir_it != libDir.end(); ++libDir_it) {
252 QString lib = (*libDir_it).toQString();
253 if (!lib.isEmpty()) {
254 if (lib.endsWith('\\'))
255 lib.chop(1);
256 libs << QLatin1String("-L") + lib;
257 }
258 }
259 ProStringList &qmklibs = project->values("LIBS");
260 qmklibs = libs + qmklibs;
261
262 if (project->values("TEMPLATE").contains("app")) {
263 project->values("QMAKE_CFLAGS") += project->values("QMAKE_CFLAGS_APP");
264 project->values("QMAKE_CXXFLAGS") += project->values("QMAKE_CXXFLAGS_APP");
265 project->values("QMAKE_LFLAGS") += project->values("QMAKE_LFLAGS_APP");
266 } else if (project->values("TEMPLATE").contains("lib") && project->isActiveConfig("dll")) {
267 if(!project->isActiveConfig("plugin") || !project->isActiveConfig("plugin_no_share_shlib_cflags")) {
268 project->values("QMAKE_CFLAGS") += project->values("QMAKE_CFLAGS_SHLIB");
269 project->values("QMAKE_CXXFLAGS") += project->values("QMAKE_CXXFLAGS_SHLIB");
270 }
271 if (project->isActiveConfig("plugin")) {
272 project->values("QMAKE_CFLAGS") += project->values("QMAKE_CFLAGS_PLUGIN");
273 project->values("QMAKE_CXXFLAGS") += project->values("QMAKE_CXXFLAGS_PLUGIN");
274 project->values("QMAKE_LFLAGS") += project->values("QMAKE_LFLAGS_PLUGIN");
275 } else {
276 project->values("QMAKE_LFLAGS") += project->values("QMAKE_LFLAGS_SHLIB");
277 }
278 }
279}
280
281void Win32MakefileGenerator::fixTargetExt()
282{
283 if (!project->values("QMAKE_APP_FLAG").isEmpty()) {
284 project->values("TARGET_EXT").append(".exe");
285 } else if (project->isActiveConfig("shared")) {
286 project->values("LIB_TARGET").prepend(project->first("QMAKE_PREFIX_STATICLIB")
287 + project->first("TARGET") + project->first("TARGET_VERSION_EXT")
288 + '.' + project->first("QMAKE_EXTENSION_STATICLIB"));
289 project->values("TARGET_EXT").append(project->first("TARGET_VERSION_EXT") + "."
290 + project->first("QMAKE_EXTENSION_SHLIB"));
291 project->values("TARGET").first() = project->first("QMAKE_PREFIX_SHLIB") + project->first("TARGET");
292 } else {
293 project->values("TARGET_EXT").append("." + project->first("QMAKE_EXTENSION_STATICLIB"));
294 project->values("TARGET").first() = project->first("QMAKE_PREFIX_STATICLIB") + project->first("TARGET");
295 project->values("LIB_TARGET").prepend(project->first("TARGET") + project->first("TARGET_EXT")); // for the .prl only
296 }
297}
298
299void Win32MakefileGenerator::processRcFileVar()
300{
301 if (Option::qmake_mode == Option::QMAKE_GENERATE_NOTHING)
302 return;
303
304 const QString manifestFile = project->first("QMAKE_MANIFEST").toQString();
305 if (((!project->values("VERSION").isEmpty() || !project->values("RC_ICONS").isEmpty() || !manifestFile.isEmpty())
306 && project->values("RC_FILE").isEmpty()
307 && project->values("RES_FILE").isEmpty()
308 && !project->isActiveConfig("no_generated_target_info")
309 && (project->isActiveConfig("shared") || !project->values("QMAKE_APP_FLAG").isEmpty()))
310 || !project->values("QMAKE_WRITE_DEFAULT_RC").isEmpty()){
311
312 QByteArray rcString;
313 QTextStream ts(&rcString, QFile::WriteOnly);
314
315 QStringList vers = project->first("VERSION").toQString().split(".", Qt::SkipEmptyParts);
316 for (int i = vers.size(); i < 4; i++)
317 vers += "0";
318 QString versionString = vers.join('.');
319
320 QStringList rcIcons;
321 const auto icons = project->values("RC_ICONS");
322 rcIcons.reserve(icons.size());
323 for (const ProString &icon : icons)
324 rcIcons.append(fileFixify(icon.toQString(), FileFixifyAbsolute));
325
326 QString companyName;
327 if (!project->values("QMAKE_TARGET_COMPANY").isEmpty())
328 companyName = project->values("QMAKE_TARGET_COMPANY").join(' ');
329
330 QString description;
331 if (!project->values("QMAKE_TARGET_DESCRIPTION").isEmpty())
332 description = project->values("QMAKE_TARGET_DESCRIPTION").join(' ');
333
334 QString copyright;
335 if (!project->values("QMAKE_TARGET_COPYRIGHT").isEmpty())
336 copyright = project->values("QMAKE_TARGET_COPYRIGHT").join(' ');
337
338 QString productName;
339 if (!project->values("QMAKE_TARGET_PRODUCT").isEmpty())
340 productName = project->values("QMAKE_TARGET_PRODUCT").join(' ');
341 else
342 productName = project->first("TARGET").toQString();
343
344 QString originalName;
345 if (!project->values("QMAKE_TARGET_ORIGINAL_FILENAME").isEmpty())
346 originalName = project->values("QMAKE_TARGET_ORIGINAL_FILENAME").join(' ');
347 else
348 originalName = project->first("TARGET") + project->first("TARGET_EXT");
349
350 QString internalName;
351 if (!project->values("QMAKE_TARGET_INTERNALNAME").isEmpty())
352 internalName = project->values("QMAKE_TARGET_INTERNALNAME").join(' ');
353
354 QString comments;
355 if (!project->values("QMAKE_TARGET_COMMENTS").isEmpty())
356 comments = project->values("QMAKE_TARGET_COMMENTS").join(' ');
357
358 QString trademarks;
359 if (!project->values("QMAKE_TARGET_TRADEMARKS").isEmpty())
360 trademarks = project->values("QMAKE_TARGET_TRADEMARKS").join(' ');
361
362 int rcLang = project->intValue("RC_LANG", 1033); // default: English(USA)
363 int rcCodePage = project->intValue("RC_CODEPAGE", 1200); // default: Unicode
364
365 ts << "#include <windows.h>\n";
366 ts << Qt::endl;
367 if (!rcIcons.isEmpty()) {
368 for (int i = 0; i < rcIcons.size(); ++i)
369 ts << QString("IDI_ICON%1\tICON\t%2").arg(i + 1).arg(cQuoted(rcIcons[i])) << Qt::endl;
370 ts << Qt::endl;
371 }
372 if (!manifestFile.isEmpty()) {
373 QString manifestResourceId;
374 if (project->first("TEMPLATE") == "lib")
375 manifestResourceId = QStringLiteral("ISOLATIONAWARE_MANIFEST_RESOURCE_ID");
376 else
377 manifestResourceId = QStringLiteral("CREATEPROCESS_MANIFEST_RESOURCE_ID");
378 ts << manifestResourceId << " RT_MANIFEST \"" << manifestFile << "\"\n";
379 }
380 ts << "VS_VERSION_INFO VERSIONINFO\n";
381 ts << "\tFILEVERSION " << QString(versionString).replace(".", ",") << Qt::endl;
382 ts << "\tPRODUCTVERSION " << QString(versionString).replace(".", ",") << Qt::endl;
383 ts << "\tFILEFLAGSMASK 0x3fL\n";
384 ts << "#ifdef _DEBUG\n";
385 ts << "\tFILEFLAGS VS_FF_DEBUG\n";
386 ts << "#else\n";
387 ts << "\tFILEFLAGS 0x0L\n";
388 ts << "#endif\n";
389 ts << "\tFILEOS VOS_NT_WINDOWS32\n";
390 if (project->isActiveConfig("shared"))
391 ts << "\tFILETYPE VFT_DLL\n";
392 else
393 ts << "\tFILETYPE VFT_APP\n";
394 ts << "\tFILESUBTYPE VFT2_UNKNOWN\n";
395 ts << "\tBEGIN\n";
396 ts << "\t\tBLOCK \"StringFileInfo\"\n";
397 ts << "\t\tBEGIN\n";
398 ts << "\t\t\tBLOCK \""
399 << QString("%1%2").arg(rcLang, 4, 16, QLatin1Char('0')).arg(rcCodePage, 4, 16, QLatin1Char('0'))
400 << "\"\n";
401 ts << "\t\t\tBEGIN\n";
402 ts << "\t\t\t\tVALUE \"CompanyName\", \"" << companyName << "\\0\"\n";
403 ts << "\t\t\t\tVALUE \"FileDescription\", \"" << description << "\\0\"\n";
404 ts << "\t\t\t\tVALUE \"FileVersion\", \"" << versionString << "\\0\"\n";
405 ts << "\t\t\t\tVALUE \"LegalCopyright\", \"" << copyright << "\\0\"\n";
406 ts << "\t\t\t\tVALUE \"OriginalFilename\", \"" << originalName << "\\0\"\n";
407 ts << "\t\t\t\tVALUE \"ProductName\", \"" << productName << "\\0\"\n";
408 ts << "\t\t\t\tVALUE \"ProductVersion\", \"" << versionString << "\\0\"\n";
409 ts << "\t\t\t\tVALUE \"InternalName\", \"" << internalName << "\\0\"\n";
410 ts << "\t\t\t\tVALUE \"Comments\", \"" << comments << "\\0\"\n";
411 ts << "\t\t\t\tVALUE \"LegalTrademarks\", \"" << trademarks << "\\0\"\n";
412 ts << "\t\t\tEND\n";
413 ts << "\t\tEND\n";
414 ts << "\t\tBLOCK \"VarFileInfo\"\n";
415 ts << "\t\tBEGIN\n";
416 ts << "\t\t\tVALUE \"Translation\", "
417 << QString("0x%1").arg(rcLang, 4, 16, QLatin1Char('0'))
418 << ", " << QString("%1").arg(rcCodePage, 4) << Qt::endl;
419 ts << "\t\tEND\n";
420 ts << "\tEND\n";
421 ts << "/* End of Version info */\n";
422 ts << Qt::endl;
423
424 ts.flush();
425
426
427 QString rcFilename = project->first("OUT_PWD")
428 + "/"
429 + project->first("TARGET")
430 + "_resource"
431 + ".rc";
432 QFile rcFile(QDir::cleanPath(rcFilename));
433
434 bool writeRcFile = true;
435 if (rcFile.exists() && rcFile.open(QFile::ReadOnly)) {
436 writeRcFile = rcFile.readAll() != rcString;
437 rcFile.close();
438 }
439 if (writeRcFile) {
440 bool ok;
441 ok = rcFile.open(QFile::WriteOnly);
442 if (!ok) {
443 // The file can't be opened... try creating the containing
444 // directory first (needed for clean shadow builds)
445 QDir().mkpath(QFileInfo(rcFile).path());
446 ok = rcFile.open(QFile::WriteOnly);
447 }
448 if (!ok) {
449 ::fprintf(stderr, "Cannot open for writing: %s", rcFile.fileName().toLatin1().constData());
450 ::exit(1);
451 }
452 rcFile.write(rcString);
453 rcFile.close();
454 }
455 if (project->values("QMAKE_WRITE_DEFAULT_RC").isEmpty())
456 project->values("RC_FILE").insert(0, rcFile.fileName());
457 }
458 if (!project->values("RC_FILE").isEmpty()) {
459 if (!project->values("RES_FILE").isEmpty()) {
460 fprintf(stderr, "Both rc and res file specified.\n");
461 fprintf(stderr, "Please specify one of them, not both.");
462 exit(1);
463 }
464 QString resFile = project->first("RC_FILE").toQString();
465
466 // if this is a shadow build then use the absolute path of the rc file
467 if (Option::output_dir != qmake_getpwd()) {
468 QFileInfo fi(resFile);
469 project->values("RC_FILE").first() = fi.absoluteFilePath();
470 }
471
472 resFile.replace(QLatin1String(".rc"), Option::res_ext);
473 project->values("RES_FILE").prepend(fileInfo(resFile).fileName());
474 QString resDestDir;
475 if (project->isActiveConfig("staticlib"))
476 resDestDir = project->first("DESTDIR").toQString();
477 else
478 resDestDir = project->first("OBJECTS_DIR").toQString();
479 if (!resDestDir.isEmpty()) {
480 resDestDir.append(Option::dir_sep);
481 project->values("RES_FILE").first().prepend(resDestDir);
482 }
483 project->values("RES_FILE").first() = Option::fixPathToTargetOS(
484 project->first("RES_FILE").toQString(), false);
485 project->values("POST_TARGETDEPS") += project->values("RES_FILE");
486 project->values("CLEAN_FILES") += project->values("RES_FILE");
487 }
488}
489
490void Win32MakefileGenerator::writeCleanParts(QTextStream &t)
491{
492 t << "clean: compiler_clean " << depVar("CLEAN_DEPS");
493 {
494 const char *clean_targets[] = { "OBJECTS", "QMAKE_CLEAN", "CLEAN_FILES", nullptr };
495 for(int i = 0; clean_targets[i]; ++i) {
496 const ProStringList &list = project->values(clean_targets[i]);
497 const QString del_statement("-$(DEL_FILE)");
498 if(project->isActiveConfig("no_delete_multiple_files")) {
499 for (ProStringList::ConstIterator it = list.begin(); it != list.end(); ++it)
500 t << "\n\t" << del_statement
501 << ' ' << escapeFilePath(Option::fixPathToTargetOS((*it).toQString()));
502 } else {
503 QString files, file;
504 const int commandlineLimit = 2047; // NT limit, expanded
505 for (ProStringList::ConstIterator it = list.begin(); it != list.end(); ++it) {
506 file = ' ' + escapeFilePath(Option::fixPathToTargetOS((*it).toQString()));
507 if(del_statement.length() + files.length() +
508 qMax(fixEnvVariables(file).length(), file.length()) > commandlineLimit) {
509 t << "\n\t" << del_statement << files;
510 files.clear();
511 }
512 files += file;
513 }
514 if(!files.isEmpty())
515 t << "\n\t" << del_statement << files;
516 }
517 }
518 }
519 t << Qt::endl << Qt::endl;
520
521 t << "distclean: clean " << depVar("DISTCLEAN_DEPS");
522 {
523 const char *clean_targets[] = { "QMAKE_DISTCLEAN", nullptr };
524 for(int i = 0; clean_targets[i]; ++i) {
525 const ProStringList &list = project->values(clean_targets[i]);
526 const QString del_statement("-$(DEL_FILE)");
527 if(project->isActiveConfig("no_delete_multiple_files")) {
528 for (ProStringList::ConstIterator it = list.begin(); it != list.end(); ++it)
529 t << "\n\t" << del_statement << " "
530 << escapeFilePath(Option::fixPathToTargetOS((*it).toQString()));
531 } else {
532 QString files, file;
533 const int commandlineLimit = 2047; // NT limit, expanded
534 for (ProStringList::ConstIterator it = list.begin(); it != list.end(); ++it) {
535 file = " " + escapeFilePath(Option::fixPathToTargetOS((*it).toQString()));
536 if(del_statement.length() + files.length() +
537 qMax(fixEnvVariables(file).length(), file.length()) > commandlineLimit) {
538 t << "\n\t" << del_statement << files;
539 files.clear();
540 }
541 files += file;
542 }
543 if(!files.isEmpty())
544 t << "\n\t" << del_statement << files;
545 }
546 }
547 }
548 t << "\n\t-$(DEL_FILE) $(DESTDIR_TARGET)\n";
549 {
550 QString ofile = fileFixify(Option::output.fileName());
551 if(!ofile.isEmpty())
552 t << "\t-$(DEL_FILE) " << escapeFilePath(ofile) << Qt::endl;
553 }
554 t << Qt::endl;
555}
556
557void Win32MakefileGenerator::writeIncPart(QTextStream &t)
558{
559 t << "INCPATH = ";
560
561 const ProStringList &incs = project->values("INCLUDEPATH");
562 for(int i = 0; i < incs.size(); ++i) {
563 QString inc = incs.at(i).toQString();
564 inc.replace(QRegularExpression("\\\\$"), "");
565 if(!inc.isEmpty())
566 t << "-I" << escapeFilePath(inc) << ' ';
567 }
568 t << Qt::endl;
569}
570
571void Win32MakefileGenerator::writeStandardParts(QTextStream &t)
572{
573 writeExportedVariables(t);
574
575 t << "####### Compiler, tools and options\n\n";
576 t << "CC = " << var("QMAKE_CC") << Qt::endl;
577 t << "CXX = " << var("QMAKE_CXX") << Qt::endl;
578 t << "DEFINES = "
579 << varGlue("PRL_EXPORT_DEFINES","-D"," -D"," ")
580 << varGlue("DEFINES","-D"," -D","") << Qt::endl;
581 t << "CFLAGS = " << var("QMAKE_CFLAGS") << " $(DEFINES)\n";
582 t << "CXXFLAGS = " << var("QMAKE_CXXFLAGS") << " $(DEFINES)\n";
583
584 writeIncPart(t);
585 writeLibsPart(t);
586 writeDefaultVariables(t);
587 t << Qt::endl;
588
589 t << "####### Output directory\n\n";
590 if(!project->values("OBJECTS_DIR").isEmpty())
591 t << "OBJECTS_DIR = " << escapeFilePath(var("OBJECTS_DIR").remove(QRegularExpression("\\\\$"))) << Qt::endl;
592 else
593 t << "OBJECTS_DIR = . \n";
594 t << Qt::endl;
595
596 t << "####### Files\n\n";
597 t << "SOURCES = " << valList(escapeFilePaths(project->values("SOURCES")))
598 << " " << valList(escapeFilePaths(project->values("GENERATED_SOURCES"))) << Qt::endl;
599
600 // do this here so we can set DEST_TARGET to be the complete path to the final target if it is needed.
601 QString orgDestDir = var("DESTDIR");
602 QString destDir = Option::fixPathToTargetOS(orgDestDir, false);
603 if (!destDir.isEmpty() && (orgDestDir.endsWith('/') || orgDestDir.endsWith(Option::dir_sep)))
604 destDir += Option::dir_sep;
605 QString target = QString(project->first("TARGET")+project->first("TARGET_EXT"));
606 project->values("DEST_TARGET").prepend(destDir + target);
607
608 writeObjectsPart(t);
609
610 writeExtraCompilerVariables(t);
611 writeExtraVariables(t);
612
613 t << "DIST = " << fileVarList("DISTFILES") << ' '
614 << fileVarList("HEADERS") << ' ' << fileVarList("SOURCES") << Qt::endl;
615 t << "QMAKE_TARGET = " << fileVar("QMAKE_ORIG_TARGET") << Qt::endl; // unused
616 // The comment is important to maintain variable compatibility with Unix
617 // Makefiles, while not interpreting a trailing-slash as a linebreak
618 t << "DESTDIR = " << escapeFilePath(destDir) << " #avoid trailing-slash linebreak\n";
619 t << "TARGET = " << escapeFilePath(target) << Qt::endl;
620 t << "DESTDIR_TARGET = " << fileVar("DEST_TARGET") << Qt::endl;
621 t << Qt::endl;
622
623 writeImplicitRulesPart(t);
624
625 t << "####### Build rules\n\n";
626 writeBuildRulesPart(t);
627
628 if (project->first("TEMPLATE") != "aux") {
629 if (project->isActiveConfig("shared") && !project->values("DLLDESTDIR").isEmpty()) {
630 const ProStringList &dlldirs = project->values("DLLDESTDIR");
631 for (ProStringList::ConstIterator dlldir = dlldirs.begin(); dlldir != dlldirs.end(); ++dlldir) {
632 t << "\t-$(COPY_FILE) $(DESTDIR_TARGET) "
633 << escapeFilePath(Option::fixPathToTargetOS((*dlldir).toQString(), false)) << Qt::endl;
634 }
635 }
636 t << Qt::endl;
637
638 writeRcFilePart(t);
639 }
640
641 writeMakeQmake(t);
642
643 QStringList dist_files = fileFixify(Option::mkfile::project_files);
644 if(!project->isEmpty("QMAKE_INTERNAL_INCLUDED_FILES"))
645 dist_files += project->values("QMAKE_INTERNAL_INCLUDED_FILES").toQStringList();
646 if(!project->isEmpty("TRANSLATIONS"))
647 dist_files << var("TRANSLATIONS");
648 if(!project->isEmpty("FORMS")) {
649 const ProStringList &forms = project->values("FORMS");
650 for (ProStringList::ConstIterator formit = forms.begin(); formit != forms.end(); ++formit) {
651 QString ui_h = fileFixify((*formit) + Option::h_ext.first());
652 if(exists(ui_h))
653 dist_files << ui_h;
654 }
655 }
656 t << "dist:\n\t"
657 << "$(ZIP) " << var("QMAKE_ORIG_TARGET") << ".zip $(SOURCES) $(DIST) "
658 << escapeFilePaths(dist_files).join(' ') << ' ' << fileVar("TRANSLATIONS") << ' ';
659 if(!project->isEmpty("QMAKE_EXTRA_COMPILERS")) {
660 const ProStringList &quc = project->values("QMAKE_EXTRA_COMPILERS");
661 for (ProStringList::ConstIterator it = quc.begin(); it != quc.end(); ++it) {
662 const ProStringList &inputs = project->values(ProKey(*it + ".input"));
663 for (ProStringList::ConstIterator input = inputs.begin(); input != inputs.end(); ++input) {
664 const ProStringList &val = project->values((*input).toKey());
665 t << escapeFilePaths(val).join(' ') << ' ';
666 }
667 }
668 }
669 t << Qt::endl << Qt::endl;
670
671 writeCleanParts(t);
672 writeExtraTargets(t);
673 writeExtraCompilerTargets(t);
674 t << Qt::endl << Qt::endl;
675}
676
677void Win32MakefileGenerator::writeLibsPart(QTextStream &t)
678{
679 if(project->isActiveConfig("staticlib") && project->first("TEMPLATE") == "lib") {
680 t << "LIBAPP = " << var("QMAKE_LIB") << Qt::endl;
681 t << "LIBFLAGS = " << var("QMAKE_LIBFLAGS") << Qt::endl;
682 } else {
683 t << "LINKER = " << var("QMAKE_LINK") << Qt::endl;
684 t << "LFLAGS = " << var("QMAKE_LFLAGS") << Qt::endl;
685 t << "LIBS = " << fixLibFlags("LIBS").join(' ') << ' '
686 << fixLibFlags("LIBS_PRIVATE").join(' ') << ' '
687 << fixLibFlags("QMAKE_LIBS").join(' ') << ' '
688 << fixLibFlags("QMAKE_LIBS_PRIVATE").join(' ') << Qt::endl;
689 }
690}
691
692void Win32MakefileGenerator::writeObjectsPart(QTextStream &t)
693{
694 // Used in both deps and commands.
695 t << "OBJECTS = " << valList(escapeDependencyPaths(project->values("OBJECTS"))) << Qt::endl;
696}
697
698void Win32MakefileGenerator::writeImplicitRulesPart(QTextStream &)
699{
700}
701
702void Win32MakefileGenerator::writeBuildRulesPart(QTextStream &)
703{
704}
705
706void Win32MakefileGenerator::writeRcFilePart(QTextStream &t)
707{
708 if(!project->values("RC_FILE").isEmpty()) {
709 const ProString res_file = project->first("RES_FILE");
710 const QString rc_file = fileFixify(project->first("RC_FILE").toQString());
711
712 const ProStringList rcIncPaths = project->values("RC_INCLUDEPATH");
713 QString incPathStr;
714 for (int i = 0; i < rcIncPaths.count(); ++i) {
715 const ProString &path = rcIncPaths.at(i);
716 if (path.isEmpty())
717 continue;
718 incPathStr += QStringLiteral(" /i ");
719 incPathStr += escapeFilePath(path);
720 }
721
722 addSourceFile(rc_file, QMakeSourceFileInfo::SEEK_DEPS);
723 const QStringList rcDeps = QStringList(rc_file) << dependencies(rc_file);
724
725 // The resource tool may use defines. This might be the same defines passed in as the
726 // compiler, since you may use these defines in the .rc file itself.
727 // As the escape syntax for the command line defines for RC is different from that for CL,
728 // we might have to set specific defines for RC.
729 ProString defines = varGlue("RC_DEFINES", " -D", " -D", "");
730 if (defines.isEmpty())
731 defines = ProString(" $(DEFINES)");
732
733 // Also, we need to add the _DEBUG define manually since the compiler defines this symbol
734 // by itself, and we use it in the automatically created rc file when VERSION is defined
735 // in the .pro file.
736 t << escapeDependencyPath(res_file) << ": "
737 << escapeDependencyPaths(rcDeps).join(' ') << "\n\t"
738 << var("QMAKE_RC") << (project->isActiveConfig("debug") ? " -D_DEBUG" : "")
739 << defines << incPathStr << " -fo " << escapeFilePath(res_file)
740 << ' ' << escapeFilePath(rc_file);
741 t << Qt::endl << Qt::endl;
742 }
743}
744
745QString Win32MakefileGenerator::defaultInstall(const QString &t)
746{
747 if((t != "target" && t != "dlltarget") ||
748 (t == "dlltarget" && (project->first("TEMPLATE") != "lib" || !project->isActiveConfig("shared"))) ||
749 project->first("TEMPLATE") == "subdirs" || project->first("TEMPLATE") == "aux")
750 return QString();
751
752 const QString root = installRoot();
753 ProStringList &uninst = project->values(ProKey(t + ".uninstall"));
754 QString ret;
755 QString targetdir = fileFixify(project->first(ProKey(t + ".path")).toQString(), FileFixifyAbsolute);
756 if(targetdir.right(1) != Option::dir_sep)
757 targetdir += Option::dir_sep;
758
759 const ProStringList &targets = project->values(ProKey(t + ".targets"));
760 for (int i = 0; i < targets.size(); ++i) {
761 QString src = targets.at(i).toQString(),
762 dst = escapeFilePath(filePrefixRoot(root, targetdir + src.section('/', -1)));
763 if (!ret.isEmpty())
764 ret += "\n\t";
765 ret += "$(QINSTALL) " + escapeFilePath(Option::fixPathToTargetOS(src, false)) + ' ' + dst;
766 if (!uninst.isEmpty())
767 uninst.append("\n\t");
768 uninst.append("-$(DEL_FILE) " + dst);
769 }
770
771 if(t == "target" && project->first("TEMPLATE") == "lib") {
772 if(project->isActiveConfig("create_prl") && !project->isActiveConfig("no_install_prl") &&
773 !project->isEmpty("QMAKE_INTERNAL_PRL_FILE")) {
774 QString dst_prl = Option::fixPathToTargetOS(project->first("QMAKE_INTERNAL_PRL_FILE").toQString());
775 int slsh = dst_prl.lastIndexOf(Option::dir_sep);
776 if(slsh != -1)
777 dst_prl = dst_prl.right(dst_prl.length() - slsh - 1);
778 dst_prl = filePrefixRoot(root, targetdir + dst_prl);
779 if (!ret.isEmpty())
780 ret += "\n\t";
781 ret += installMetaFile(ProKey("QMAKE_PRL_INSTALL_REPLACE"), project->first("QMAKE_INTERNAL_PRL_FILE").toQString(), dst_prl);
782 if(!uninst.isEmpty())
783 uninst.append("\n\t");
784 uninst.append("-$(DEL_FILE) " + escapeFilePath(dst_prl));
785 }
786 if(project->isActiveConfig("create_pc")) {
787 QString dst_pc = pkgConfigFileName(false);
788 if (!dst_pc.isEmpty()) {
789 dst_pc = filePrefixRoot(root, targetdir + dst_pc);
790 const QString dst_pc_dir = Option::fixPathToTargetOS(fileInfo(dst_pc).path(), false);
791 if (!dst_pc_dir.isEmpty()) {
792 if (!ret.isEmpty())
793 ret += "\n\t";
794 ret += mkdir_p_asstring(dst_pc_dir, true);
795 }
796 if(!ret.isEmpty())
797 ret += "\n\t";
798 ret += installMetaFile(ProKey("QMAKE_PKGCONFIG_INSTALL_REPLACE"), pkgConfigFileName(true), dst_pc);
799 if(!uninst.isEmpty())
800 uninst.append("\n\t");
801 uninst.append("-$(DEL_FILE) " + escapeFilePath(dst_pc));
802 }
803 }
804 if(project->isActiveConfig("shared") && !project->isActiveConfig("plugin")) {
805 ProString lib_target = project->first("LIB_TARGET");
806 QString src_targ = escapeFilePath(
807 (project->isEmpty("DESTDIR") ? QString("$(DESTDIR)") : project->first("DESTDIR"))
808 + lib_target);
809 QString dst_targ = escapeFilePath(
810 filePrefixRoot(root, fileFixify(targetdir + lib_target, FileFixifyAbsolute)));
811 if(!ret.isEmpty())
812 ret += "\n\t";
813 ret += QString("-$(INSTALL_FILE) ") + src_targ + ' ' + dst_targ;
814 if(!uninst.isEmpty())
815 uninst.append("\n\t");
816 uninst.append("-$(DEL_FILE) " + dst_targ);
817 }
818 }
819
820 if (t == "dlltarget" || project->values(ProKey(t + ".CONFIG")).indexOf("no_dll") == -1) {
821 QString src_targ = "$(DESTDIR_TARGET)";
822 QString dst_targ = escapeFilePath(
823 filePrefixRoot(root, fileFixify(targetdir + "$(TARGET)", FileFixifyAbsolute)));
824 if(!ret.isEmpty())
825 ret += "\n\t";
826 ret += QString("-$(INSTALL_FILE) ") + src_targ + ' ' + dst_targ;
827 if(!uninst.isEmpty())
828 uninst.append("\n\t");
829 uninst.append("-$(DEL_FILE) " + dst_targ);
830 }
831 return ret;
832}
833
834void Win32MakefileGenerator::writeDefaultVariables(QTextStream &t)
835{
836 MakefileGenerator::writeDefaultVariables(t);
837 t << "IDC = " << (project->isEmpty("QMAKE_IDC") ? QString("idc") : var("QMAKE_IDC"))
838 << Qt::endl;
839 t << "IDL = " << (project->isEmpty("QMAKE_IDL") ? QString("midl") : var("QMAKE_IDL"))
840 << Qt::endl;
841 t << "ZIP = " << var("QMAKE_ZIP") << Qt::endl;
842 t << "DEF_FILE = " << fileVar("DEF_FILE") << Qt::endl;
843 t << "RES_FILE = " << fileVar("RES_FILE") << Qt::endl; // Not on mingw, can't see why not though...
844 t << "SED = " << var("QMAKE_STREAM_EDITOR") << Qt::endl;
845 t << "MOVE = " << var("QMAKE_MOVE") << Qt::endl;
846}
847
848QString Win32MakefileGenerator::escapeFilePath(const QString &path) const
849{
850 QString ret = path;
851 if(!ret.isEmpty()) {
852 if (ret.contains(' ') || ret.contains('\t'))
853 ret = "\"" + ret + "\"";
854 debug_msg(2, "EscapeFilePath: %s -> %s", path.toLatin1().constData(), ret.toLatin1().constData());
855 }
856 return ret;
857}
858
859QString Win32MakefileGenerator::escapeDependencyPath(const QString &path) const
860{
861 QString ret = path;
862 if (!ret.isEmpty()) {
863 static const QRegularExpression criticalChars(QStringLiteral("([\t #])"));
864 if (ret.contains(criticalChars))
865 ret = "\"" + ret + "\"";
866 debug_msg(2, "EscapeDependencyPath: %s -> %s", path.toLatin1().constData(), ret.toLatin1().constData());
867 }
868 return ret;
869}
870
871QString Win32MakefileGenerator::cQuoted(const QString &str)
872{
873 QString ret = str;
874 ret.replace(QLatin1Char('\\'), QLatin1String("\\\\"));
875 ret.replace(QLatin1Char('"'), QLatin1String("\\\""));
876 ret.prepend(QLatin1Char('"'));
877 ret.append(QLatin1Char('"'));
878 return ret;
879}
880
881ProKey Win32MakefileGenerator::fullTargetVariable() const
882{
883 return "DEST_TARGET";
884}
885
886QT_END_NAMESPACE
887