| 1 |
// Copyright (C) 2016 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 |
| 3 |
|
| 4 |
#include "msvc_nmake.h" |
| 5 |
#include "option.h" |
| 6 |
|
| 7 |
#include <qregularexpression.h> |
| 8 |
#include <qdir.h> |
| 9 |
#include <qdirlisting.h> |
| 10 |
#include <qset.h> |
| 11 |
|
| 12 |
#include <time.h> |
| 13 |
|
| 14 |
QT_BEGIN_NAMESPACE |
| 15 |
|
| 16 |
bool |
| 17 |
NmakeMakefileGenerator::writeMakefile(QTextStream &t) |
| 18 |
{ |
| 19 |
writeHeader(t); |
| 20 |
if (writeDummyMakefile(t)) |
| 21 |
return true; |
| 22 |
|
| 23 |
if(project->first("TEMPLATE") == "app" || |
| 24 |
project->first("TEMPLATE") == "lib" || |
| 25 |
project->first("TEMPLATE") == "aux") { |
| 26 |
writeNmakeParts(t); |
| 27 |
return MakefileGenerator::writeMakefile(t); |
| 28 |
} |
| 29 |
else if(project->first("TEMPLATE") == "subdirs") { |
| 30 |
writeSubDirs(t); |
| 31 |
return true; |
| 32 |
} |
| 33 |
return false; |
| 34 |
} |
| 35 |
|
| 36 |
void NmakeMakefileGenerator::writeSubMakeCall(QTextStream &t, const QString &callPrefix, |
| 37 |
const QString &makeArguments) |
| 38 |
{ |
| 39 |
// Pass MAKEFLAGS as environment variable to sub-make calls. |
| 40 |
// Unlike other make tools nmake doesn't do this automatically. |
| 41 |
t << "\n\t@set MAKEFLAGS=$(MAKEFLAGS)"; |
| 42 |
Win32MakefileGenerator::writeSubMakeCall(t, callPrefix, makeArguments); |
| 43 |
} |
| 44 |
|
| 45 |
ProStringList NmakeMakefileGenerator::extraSubTargetDependencies() |
| 46 |
{ |
| 47 |
return { "$(MAKEFILE)" }; |
| 48 |
} |
| 49 |
|
| 50 |
QString NmakeMakefileGenerator::defaultInstall(const QString &t) |
| 51 |
{ |
| 52 |
QString ret = Win32MakefileGenerator::defaultInstall(t); |
| 53 |
if (ret.isEmpty()) |
| 54 |
return ret; |
| 55 |
|
| 56 |
const QString root = installRoot(); |
| 57 |
ProStringList &uninst = project->values(ProKey(t + ".uninstall")); |
| 58 |
QString targetdir = fileFixify(project->first(ProKey(t + ".path")).toQString(), FileFixifyAbsolute); |
| 59 |
if(targetdir.right(1) != Option::dir_sep) |
| 60 |
targetdir += Option::dir_sep; |
| 61 |
|
| 62 |
if (project->isActiveConfig("debug_info")) { |
| 63 |
if (t == "dlltarget" || project->values(ProKey(t + ".CONFIG")).indexOf("no_dll") == -1) { |
| 64 |
const QFileInfo targetFileInfo(project->first("DESTDIR") + project->first("TARGET") |
| 65 |
+ project->first("TARGET_EXT")); |
| 66 |
const QString pdb_target = targetFileInfo.completeBaseName() + ".pdb"; |
| 67 |
QString src_targ = (project->isEmpty("DESTDIR") ? QString("$(DESTDIR)") : project->first("DESTDIR")) + pdb_target; |
| 68 |
QString dst_targ = filePrefixRoot(root, fileFixify(targetdir + pdb_target, FileFixifyAbsolute)); |
| 69 |
if(!ret.isEmpty()) |
| 70 |
ret += "\n\t"; |
| 71 |
ret += QString("-$(INSTALL_FILE) ") + escapeFilePath(src_targ) + ' ' + escapeFilePath(dst_targ); |
| 72 |
if(!uninst.isEmpty()) |
| 73 |
uninst.append("\n\t"); |
| 74 |
uninst.append("-$(DEL_FILE) " + escapeFilePath(dst_targ)); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
return ret; |
| 79 |
} |
| 80 |
|
| 81 |
QStringList &NmakeMakefileGenerator::findDependencies(const QString &file) |
| 82 |
{ |
| 83 |
QStringList &aList = MakefileGenerator::findDependencies(file); |
| 84 |
for(QStringList::Iterator it = Option::cpp_ext.begin(); it != Option::cpp_ext.end(); ++it) { |
| 85 |
if(file.endsWith(*it)) { |
| 86 |
if(!precompObj.isEmpty() && !aList.contains(precompObj)) |
| 87 |
aList += precompObj; |
| 88 |
break; |
| 89 |
} |
| 90 |
} |
| 91 |
for (QStringList::Iterator it = Option::c_ext.begin(); it != Option::c_ext.end(); ++it) { |
| 92 |
if (file.endsWith(*it)) { |
| 93 |
if (!precompObjC.isEmpty() && !aList.contains(precompObjC)) |
| 94 |
aList += precompObjC; |
| 95 |
break; |
| 96 |
} |
| 97 |
} |
| 98 |
return aList; |
| 99 |
} |
| 100 |
|
| 101 |
void NmakeMakefileGenerator::writeNmakeParts(QTextStream &t) |
| 102 |
{ |
| 103 |
writeStandardParts(t); |
| 104 |
|
| 105 |
// precompiled header |
| 106 |
if(usePCH) { |
| 107 |
QString precompRule = QString("-c -Yc -Fp%1 -Fo%2") |
| 108 |
.arg(escapeFilePath(precompPch), escapeFilePath(precompObj)); |
| 109 |
t << escapeDependencyPath(precompObj) << ": " << escapeDependencyPath(precompH) << ' ' |
| 110 |
<< finalizeDependencyPaths(findDependencies(precompH)).join(" \\\n\t\t") |
| 111 |
<< "\n\t$(CXX) " + precompRule +" $(CXXFLAGS) $(INCPATH) -TP " |
| 112 |
<< escapeFilePath(precompH) << Qt::endl << Qt::endl; |
| 113 |
} |
| 114 |
if (usePCHC) { |
| 115 |
QString precompRuleC = QString("-c -Yc -Fp%1 -Fo%2") |
| 116 |
.arg(escapeFilePath(precompPchC), escapeFilePath(precompObjC)); |
| 117 |
t << escapeDependencyPath(precompObjC) << ": " << escapeDependencyPath(precompH) << ' ' |
| 118 |
<< finalizeDependencyPaths(findDependencies(precompH)).join(" \\\n\t\t") |
| 119 |
<< "\n\t$(CC) " + precompRuleC +" $(CFLAGS) $(INCPATH) -TC " |
| 120 |
<< escapeFilePath(precompH) << Qt::endl << Qt::endl; |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
QString NmakeMakefileGenerator::var(const ProKey &value) const |
| 125 |
{ |
| 126 |
if (usePCH || usePCHC) { |
| 127 |
const bool isRunC = (value == "QMAKE_RUN_CC_IMP_BATCH" |
| 128 |
|| value == "QMAKE_RUN_CC_IMP" |
| 129 |
|| value == "QMAKE_RUN_CC"); |
| 130 |
const bool isRunCpp = (value == "QMAKE_RUN_CXX_IMP_BATCH" |
| 131 |
|| value == "QMAKE_RUN_CXX_IMP" |
| 132 |
|| value == "QMAKE_RUN_CXX"); |
| 133 |
if ((isRunCpp && usePCH) || (isRunC && usePCHC)) { |
| 134 |
QString precompH_f = escapeFilePath(fileFixify(precompH, FileFixifyBackwards)); |
| 135 |
QString precompRule = QString("-c -FI%1 -Yu%2 -Fp%3") |
| 136 |
.arg(precompH_f, precompH_f, escapeFilePath(isRunC ? precompPchC : precompPch)); |
| 137 |
// ### For clang_cl 8 we force inline methods to be compiled here instead |
| 138 |
// linking them from a pch.o file. We do this by pretending we are also doing |
| 139 |
// the pch.o generation step. |
| 140 |
if (project->isActiveConfig("clang_cl")) |
| 141 |
precompRule += QString(" -Xclang -building-pch-with-obj"); |
| 142 |
QString p = MakefileGenerator::var(value); |
| 143 |
p.replace(QLatin1String("-c"), precompRule); |
| 144 |
return p; |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
// Normal val |
| 149 |
return MakefileGenerator::var(value); |
| 150 |
} |
| 151 |
|
| 152 |
void NmakeMakefileGenerator::suppressBuiltinRules(QTextStream &) const |
| 153 |
{ |
| 154 |
} |
| 155 |
|
| 156 |
void NmakeMakefileGenerator::init() |
| 157 |
{ |
| 158 |
/* this should probably not be here, but I'm using it to wrap the .t files */ |
| 159 |
if(project->first("TEMPLATE") == "app") |
| 160 |
project->values("QMAKE_APP_FLAG").append("1"); |
| 161 |
else if(project->first("TEMPLATE") == "lib") |
| 162 |
project->values("QMAKE_LIB_FLAG").append("1"); |
| 163 |
else if(project->first("TEMPLATE") == "subdirs") { |
| 164 |
MakefileGenerator::init(); |
| 165 |
if(project->values("MAKEFILE").isEmpty()) |
| 166 |
project->values("MAKEFILE").append("Makefile"); |
| 167 |
return; |
| 168 |
} |
| 169 |
|
| 170 |
processVars(); |
| 171 |
|
| 172 |
project->values("LIBS") += project->values("RES_FILE"); |
| 173 |
|
| 174 |
if (!project->values("DEF_FILE").isEmpty()) { |
| 175 |
QString defFileName = fileFixify(project->first("DEF_FILE").toQString()); |
| 176 |
project->values("QMAKE_LFLAGS").append(QString("/DEF:") + escapeFilePath(defFileName)); |
| 177 |
} |
| 178 |
|
| 179 |
// set /VERSION for EXE/DLL header |
| 180 |
ProString major_minor = project->first("VERSION_PE_HEADER"); |
| 181 |
if (major_minor.isEmpty()) { |
| 182 |
ProString version = project->first("VERSION"); |
| 183 |
if (!version.isEmpty()) { |
| 184 |
int firstDot = version.indexOf("."); |
| 185 |
int secondDot = version.indexOf(".", firstDot + 1); |
| 186 |
major_minor = version.left(secondDot); |
| 187 |
} |
| 188 |
} |
| 189 |
if (!major_minor.isEmpty()) |
| 190 |
project->values("QMAKE_LFLAGS").append("/VERSION:" + major_minor); |
| 191 |
|
| 192 |
if (project->isEmpty("QMAKE_LINK_O_FLAG")) |
| 193 |
project->values("QMAKE_LINK_O_FLAG").append("/OUT:"); |
| 194 |
|
| 195 |
// Base class init! |
| 196 |
MakefileGenerator::init(); |
| 197 |
|
| 198 |
// Setup PCH variables |
| 199 |
precompH = project->first("PRECOMPILED_HEADER").toQString(); |
| 200 |
usePCH = !precompH.isEmpty() && project->isActiveConfig("precompile_header"); |
| 201 |
usePCHC = !precompH.isEmpty() && project->isActiveConfig("precompile_header_c"); |
| 202 |
if (usePCH) { |
| 203 |
// Created files |
| 204 |
precompObj = var("PRECOMPILED_DIR") + project->first("TARGET") + "_pch" + Option::obj_ext; |
| 205 |
precompPch = var("PRECOMPILED_DIR") + project->first("TARGET") + "_pch.pch"; |
| 206 |
// Add linking of precompObj (required for whole precompiled classes) |
| 207 |
// ### For clang_cl we currently let inline methods be generated in the normal objects, |
| 208 |
// since the PCH object is buggy (as of clang 8.0.0) |
| 209 |
if (!project->isActiveConfig("clang_cl")) |
| 210 |
project->values("OBJECTS") += precompObj; |
| 211 |
// Add pch file to cleanup |
| 212 |
project->values("QMAKE_CLEAN") += precompPch; |
| 213 |
// Return to variable pool |
| 214 |
project->values("PRECOMPILED_OBJECT") = ProStringList(precompObj); |
| 215 |
project->values("PRECOMPILED_PCH") = ProStringList(precompPch); |
| 216 |
} |
| 217 |
if (usePCHC) { |
| 218 |
precompObjC = var("PRECOMPILED_DIR") + project->first("TARGET") + "_pch_c" + Option::obj_ext; |
| 219 |
precompPchC = var("PRECOMPILED_DIR") + project->first("TARGET") + "_pch_c.pch"; |
| 220 |
if (!project->isActiveConfig("clang_cl")) |
| 221 |
project->values("OBJECTS") += precompObjC; |
| 222 |
project->values("QMAKE_CLEAN") += precompPchC; |
| 223 |
project->values("PRECOMPILED_OBJECT_C") = ProStringList(precompObjC); |
| 224 |
project->values("PRECOMPILED_PCH_C") = ProStringList(precompPchC); |
| 225 |
} |
| 226 |
|
| 227 |
const QFileInfo targetFileInfo(project->first("DESTDIR") + project->first("TARGET") |
| 228 |
+ project->first("TARGET_EXT")); |
| 229 |
const ProString targetBase = targetFileInfo.path() + '/' + targetFileInfo.completeBaseName(); |
| 230 |
if (project->first("TEMPLATE") == "lib" && project->isActiveConfig("shared")) { |
| 231 |
project->values("QMAKE_CLEAN").append(targetBase + ".exp"); |
| 232 |
project->values("QMAKE_DISTCLEAN").append(targetBase + ".lib"); |
| 233 |
} |
| 234 |
if (project->isActiveConfig("debug_info")) { |
| 235 |
QString pdbfile; |
| 236 |
QString distPdbFile = targetBase + ".pdb"; |
| 237 |
if (project->isActiveConfig("staticlib")) { |
| 238 |
// For static libraries, the compiler's pdb file and the dist pdb file are the same. |
| 239 |
pdbfile = distPdbFile; |
| 240 |
} else { |
| 241 |
// Use $${TARGET}.vc.pdb in the OBJECTS_DIR for the compiler and |
| 242 |
// $${TARGET}.pdb (the default) for the linker. |
| 243 |
pdbfile = var("OBJECTS_DIR") + project->first("TARGET") + ".vc.pdb"; |
| 244 |
} |
| 245 |
QString escapedPdbFile = escapeFilePath(pdbfile); |
| 246 |
project->values("QMAKE_CFLAGS").append("/Fd" + escapedPdbFile); |
| 247 |
project->values("QMAKE_CXXFLAGS").append("/Fd" + escapedPdbFile); |
| 248 |
project->values("QMAKE_CLEAN").append(pdbfile); |
| 249 |
project->values("QMAKE_DISTCLEAN").append(distPdbFile); |
| 250 |
} |
| 251 |
if (project->isActiveConfig("debug")) { |
| 252 |
project->values("QMAKE_CLEAN").append(targetBase + ".ilk"); |
| 253 |
project->values("QMAKE_CLEAN").append(targetBase + ".idb"); |
| 254 |
} |
| 255 |
|
| 256 |
if (project->values("QMAKE_APP_FLAG").isEmpty() && project->isActiveConfig("dll")) { |
| 257 |
ProStringList &defines = project->values("DEFINES"); |
| 258 |
if (!defines.contains("_WINDLL")) |
| 259 |
defines.append("_WINDLL"); |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
QStringList NmakeMakefileGenerator::sourceFilesForImplicitRulesFilter() |
| 264 |
{ |
| 265 |
QStringList filter; |
| 266 |
const QChar wildcard = QLatin1Char('*'); |
| 267 |
for (const QString &ext : std::as_const(Option::c_ext)) |
| 268 |
filter << wildcard + ext; |
| 269 |
for (const QString &ext : std::as_const(Option::cpp_ext)) |
| 270 |
filter << wildcard + ext; |
| 271 |
return filter; |
| 272 |
} |
| 273 |
|
| 274 |
void NmakeMakefileGenerator::writeImplicitRulesPart(QTextStream &t) |
| 275 |
{ |
| 276 |
t << "####### Implicit rules\n\n"; |
| 277 |
|
| 278 |
t << ".SUFFIXES:"; |
| 279 |
for(QStringList::Iterator cit = Option::c_ext.begin(); cit != Option::c_ext.end(); ++cit) |
| 280 |
t << " " << (*cit); |
| 281 |
for(QStringList::Iterator cppit = Option::cpp_ext.begin(); cppit != Option::cpp_ext.end(); ++cppit) |
| 282 |
t << " " << (*cppit); |
| 283 |
t << Qt::endl << Qt::endl; |
| 284 |
|
| 285 |
bool useInferenceRules = !project->isActiveConfig("no_batch"); |
| 286 |
QSet<QString> source_directories; |
| 287 |
if (useInferenceRules) { |
| 288 |
source_directories.insert("."); |
| 289 |
static const char * const directories[] = { "UI_SOURCES_DIR", "UI_DIR", nullptr }; |
| 290 |
for (int y = 0; directories[y]; y++) { |
| 291 |
QString dirTemp = project->first(directories[y]).toQString(); |
| 292 |
if (dirTemp.endsWith("\\")) |
| 293 |
dirTemp.truncate(dirTemp.size()-1); |
| 294 |
if(!dirTemp.isEmpty()) |
| 295 |
source_directories.insert(dirTemp); |
| 296 |
} |
| 297 |
static const char * const srcs[] = { "SOURCES", "GENERATED_SOURCES", nullptr }; |
| 298 |
for (int x = 0; srcs[x]; x++) { |
| 299 |
const ProStringList &l = project->values(srcs[x]); |
| 300 |
for (ProStringList::ConstIterator sit = l.begin(); sit != l.end(); ++sit) { |
| 301 |
QString sep = "\\"; |
| 302 |
if((*sit).indexOf(sep) == -1) |
| 303 |
sep = "/"; |
| 304 |
QString dir = (*sit).toQString().section(sep, 0, -2); |
| 305 |
if (!dir.isEmpty()) |
| 306 |
source_directories.insert(dir); |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
// nmake's inference rules might pick up the wrong files when encountering source files with |
| 311 |
// the same name in different directories. In this situation, turn inference rules off. |
| 312 |
QHash<QString, QString> fileNames; |
| 313 |
bool duplicatesFound = false; |
| 314 |
const QStringList sourceFilesFilter = sourceFilesForImplicitRulesFilter(); |
| 315 |
QStringList fixifiedSourceDirs = fileFixify(QList<QString>(source_directories.constBegin(), source_directories.constEnd()), FileFixifyAbsolute); |
| 316 |
fixifiedSourceDirs.removeDuplicates(); |
| 317 |
using F = QDirListing::IteratorFlag; |
| 318 |
for (const QString &sourceDir : std::as_const(fixifiedSourceDirs)) { |
| 319 |
for (const auto &dirEntry : QDirListing(sourceDir, sourceFilesFilter, F::FilesOnly | F::ResolveSymlinks)) { |
| 320 |
QString &duplicate = fileNames[dirEntry.completeBaseName()]; |
| 321 |
if (duplicate.isNull()) { |
| 322 |
duplicate = dirEntry.filePath(); |
| 323 |
} else { |
| 324 |
warn_msg(WarnLogic, "%s conflicts with %s", qPrintable(duplicate), |
| 325 |
qPrintable(dirEntry.filePath())); |
| 326 |
duplicatesFound = true; |
| 327 |
} |
| 328 |
} |
| 329 |
} |
| 330 |
if (duplicatesFound) { |
| 331 |
useInferenceRules = false; |
| 332 |
warn_msg(WarnLogic, "Automatically turning off nmake's inference rules. (CONFIG += no_batch)"); |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
if (useInferenceRules) { |
| 337 |
// Batchmode doesn't use the non implicit rules QMAKE_RUN_CXX & QMAKE_RUN_CC |
| 338 |
project->variables().remove("QMAKE_RUN_CXX"); |
| 339 |
project->variables().remove("QMAKE_RUN_CC"); |
| 340 |
|
| 341 |
for (const QString &sourceDir : std::as_const(source_directories)) { |
| 342 |
if (sourceDir.isEmpty()) |
| 343 |
continue; |
| 344 |
QString objDir = var("OBJECTS_DIR"); |
| 345 |
if (objDir == ".\\") |
| 346 |
objDir = ""; |
| 347 |
for(QStringList::Iterator cppit = Option::cpp_ext.begin(); cppit != Option::cpp_ext.end(); ++cppit) |
| 348 |
t << '{' << escapeDependencyPath(sourceDir) << '}' << (*cppit) |
| 349 |
<< '{' << escapeDependencyPath(objDir) << '}' << Option::obj_ext << "::\n\t" |
| 350 |
<< var("QMAKE_RUN_CXX_IMP_BATCH").replace(QRegularExpression("\\$@"), fileVar("OBJECTS_DIR")) |
| 351 |
<< "\n\t$<\n<<\n\n"; |
| 352 |
for(QStringList::Iterator cit = Option::c_ext.begin(); cit != Option::c_ext.end(); ++cit) |
| 353 |
t << '{' << escapeDependencyPath(sourceDir) << '}' << (*cit) |
| 354 |
<< '{' << escapeDependencyPath(objDir) << '}' << Option::obj_ext << "::\n\t" |
| 355 |
<< var("QMAKE_RUN_CC_IMP_BATCH").replace(QRegularExpression("\\$@"), fileVar("OBJECTS_DIR")) |
| 356 |
<< "\n\t$<\n<<\n\n"; |
| 357 |
} |
| 358 |
} else { |
| 359 |
for(QStringList::Iterator cppit = Option::cpp_ext.begin(); cppit != Option::cpp_ext.end(); ++cppit) |
| 360 |
t << (*cppit) << Option::obj_ext << ":\n\t" << var("QMAKE_RUN_CXX_IMP") << Qt::endl << Qt::endl; |
| 361 |
for(QStringList::Iterator cit = Option::c_ext.begin(); cit != Option::c_ext.end(); ++cit) |
| 362 |
t << (*cit) << Option::obj_ext << ":\n\t" << var("QMAKE_RUN_CC_IMP") << Qt::endl << Qt::endl; |
| 363 |
} |
| 364 |
|
| 365 |
} |
| 366 |
|
| 367 |
void NmakeMakefileGenerator::writeBuildRulesPart(QTextStream &t) |
| 368 |
{ |
| 369 |
const ProString templateName = project->first("TEMPLATE"); |
| 370 |
|
| 371 |
t << "first: all\n"; |
| 372 |
t << "all: " << escapeDependencyPath(fileFixify(Option::output.fileName())) |
| 373 |
<< ' ' << depVar("ALL_DEPS") << ' ' << depVar("DEST_TARGET") << "\n\n"; |
| 374 |
t << depVar("DEST_TARGET") << ": " |
| 375 |
<< depVar("PRE_TARGETDEPS") << " $(OBJECTS) " << depVar("POST_TARGETDEPS"); |
| 376 |
if (templateName == "aux") { |
| 377 |
t << "\n\n"; |
| 378 |
return; |
| 379 |
} |
| 380 |
|
| 381 |
if(!project->isEmpty("QMAKE_PRE_LINK")) |
| 382 |
t << "\n\t" <<var("QMAKE_PRE_LINK"); |
| 383 |
if(project->isActiveConfig("staticlib")) { |
| 384 |
t << "\n\t$(LIBAPP) $(LIBFLAGS) " << var("QMAKE_LINK_O_FLAG") << "$(DESTDIR_TARGET) @<<\n\t "; |
| 385 |
writeResponseFileFiles(t, project->values("OBJECTS")); |
| 386 |
t << "<<"; |
| 387 |
} else { |
| 388 |
const bool embedManifest = ((templateName == "app" && project->isActiveConfig("embed_manifest_exe")) |
| 389 |
|| (templateName == "lib" && project->isActiveConfig("embed_manifest_dll") |
| 390 |
&& !(project->isActiveConfig("plugin") && project->isActiveConfig("no_plugin_manifest")) |
| 391 |
)); |
| 392 |
if (embedManifest) { |
| 393 |
bool generateManifest = false; |
| 394 |
const QString target = var("DEST_TARGET"); |
| 395 |
QString manifest = project->first("QMAKE_MANIFEST").toQString(); |
| 396 |
QString extraLFlags; |
| 397 |
const bool linkerSupportsEmbedding = (msvcVersion() >= 1200); |
| 398 |
if (manifest.isEmpty()) { |
| 399 |
generateManifest = true; |
| 400 |
if (linkerSupportsEmbedding) { |
| 401 |
extraLFlags = "/MANIFEST:embed"; |
| 402 |
} else { |
| 403 |
manifest = target + ".embed.manifest"; |
| 404 |
extraLFlags += "/MANIFEST /MANIFESTFILE:" + escapeFilePath(manifest); |
| 405 |
project->values("QMAKE_CLEAN") << manifest; |
| 406 |
} |
| 407 |
} else { |
| 408 |
manifest = fileFixify(manifest); |
| 409 |
if (linkerSupportsEmbedding) |
| 410 |
extraLFlags = "/MANIFEST:embed /MANIFESTINPUT:" + escapeFilePath(manifest); |
| 411 |
} |
| 412 |
|
| 413 |
const QString resourceId = (templateName == "app") ? "1" : "2"; |
| 414 |
const bool incrementalLinking = project->values("QMAKE_LFLAGS").toQStringList().filter(QRegularExpression("(/|-)INCREMENTAL:NO")).isEmpty(); |
| 415 |
if (incrementalLinking && !linkerSupportsEmbedding) { |
| 416 |
// Link a resource that contains the manifest without modifying the exe/dll after linking. |
| 417 |
|
| 418 |
QString manifest_rc = target + "_manifest.rc"; |
| 419 |
QString manifest_res = target + "_manifest.res"; |
| 420 |
project->values("QMAKE_CLEAN") << manifest_rc << manifest_res; |
| 421 |
manifest_rc = escapeFilePath(manifest_rc); |
| 422 |
manifest_res = escapeFilePath(manifest_res); |
| 423 |
|
| 424 |
t << "\n\techo " << resourceId |
| 425 |
<< " /* CREATEPROCESS_MANIFEST_RESOURCE_ID */ 24 /* RT_MANIFEST */ " |
| 426 |
<< cQuoted(manifest) << '>' << manifest_rc; |
| 427 |
|
| 428 |
if (generateManifest) { |
| 429 |
manifest = escapeFilePath(manifest); |
| 430 |
QString manifest_bak = escapeFilePath(target + "_manifest.bak"); |
| 431 |
project->values("QMAKE_CLEAN") << manifest_bak; |
| 432 |
t << "\n\tif not exist $(DESTDIR_TARGET) if exist " << manifest |
| 433 |
<< " del " << manifest; |
| 434 |
t << "\n\tif exist " << manifest << " copy /Y " << manifest << ' ' << manifest_bak; |
| 435 |
const QString extraInlineFileContent = "\n!IF EXIST(" + manifest_res + ")\n" + manifest_res + "\n!ENDIF"; |
| 436 |
t << "\n\t"; |
| 437 |
writeLinkCommand(t, extraLFlags, extraInlineFileContent); |
| 438 |
t << "\n\tif exist " << manifest_bak << " fc /b " << manifest << ' ' << manifest_bak << " >NUL || del " << manifest_bak; |
| 439 |
t << "\n\tif not exist " << manifest_bak << " rc.exe /fo" << manifest_res << ' ' << manifest_rc; |
| 440 |
t << "\n\tif not exist " << manifest_bak << ' '; |
| 441 |
writeLinkCommand(t, extraLFlags, manifest_res); |
| 442 |
t << "\n\tif exist " << manifest_bak << " del " << manifest_bak; |
| 443 |
} else { |
| 444 |
t << "\n\trc.exe /fo" << manifest_res << " " << manifest_rc; |
| 445 |
t << "\n\t"; |
| 446 |
writeLinkCommand(t, extraLFlags, manifest_res); |
| 447 |
} |
| 448 |
} else { |
| 449 |
// directly embed the manifest in the executable after linking |
| 450 |
t << "\n\t"; |
| 451 |
writeLinkCommand(t, extraLFlags); |
| 452 |
if (!linkerSupportsEmbedding) { |
| 453 |
t << "\n\tmt.exe /nologo /manifest " << escapeFilePath(manifest) |
| 454 |
<< " /outputresource:$(DESTDIR_TARGET);" << resourceId; |
| 455 |
} |
| 456 |
} |
| 457 |
} else { |
| 458 |
t << "\n\t"; |
| 459 |
writeLinkCommand(t); |
| 460 |
} |
| 461 |
} |
| 462 |
if(!project->isEmpty("QMAKE_POST_LINK")) { |
| 463 |
t << "\n\t" << var("QMAKE_POST_LINK"); |
| 464 |
} |
| 465 |
t << Qt::endl; |
| 466 |
} |
| 467 |
|
| 468 |
void NmakeMakefileGenerator::writeLinkCommand(QTextStream &t, const QString &extraFlags, const QString &extraInlineFileContent) |
| 469 |
{ |
| 470 |
t << "$(LINKER) $(LFLAGS)"; |
| 471 |
if (!extraFlags.isEmpty()) |
| 472 |
t << ' ' << extraFlags; |
| 473 |
t << " " << var("QMAKE_LINK_O_FLAG") << "$(DESTDIR_TARGET) @<<\n"; |
| 474 |
writeResponseFileFiles(t, project->values("OBJECTS")); |
| 475 |
t << "$(LIBS)\n"; |
| 476 |
if (!extraInlineFileContent.isEmpty()) |
| 477 |
t << extraInlineFileContent << '\n'; |
| 478 |
t << "<<"; |
| 479 |
} |
| 480 |
|
| 481 |
void NmakeMakefileGenerator::writeResponseFileFiles(QTextStream &t, const ProStringList &files) |
| 482 |
{ |
| 483 |
// Add line breaks in file lists in reponse files to work around LNK1170. |
| 484 |
// The actual line length limit is 131070, but let's use a smaller limit |
| 485 |
// in case other tools are similarly hampered. |
| 486 |
const int maxLineLength = 1000; |
| 487 |
int len = 0; |
| 488 |
for (const ProString &file : files) { |
| 489 |
const ProString escapedFilePath = escapeFilePath(file); |
| 490 |
if (len) { |
| 491 |
if (len + escapedFilePath.length() > maxLineLength) { |
| 492 |
t << '\n'; |
| 493 |
len = 0; |
| 494 |
} else { |
| 495 |
t << ' '; |
| 496 |
len++; |
| 497 |
} |
| 498 |
} |
| 499 |
t << escapedFilePath; |
| 500 |
len += escapedFilePath.length(); |
| 501 |
} |
| 502 |
t << '\n'; |
| 503 |
} |
| 504 |
|
| 505 |
int NmakeMakefileGenerator::msvcVersion() const |
| 506 |
{ |
| 507 |
const int fallbackVersion = 800; // Visual Studio 2005 |
| 508 |
const QString ver = project->first(ProKey("MSVC_VER")).toQString(); |
| 509 |
bool ok; |
| 510 |
float f = ver.toFloat(&ok); |
| 511 |
return ok ? int(f * 100) : fallbackVersion; |
| 512 |
} |
| 513 |
|
| 514 |
QT_END_NAMESPACE |