1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the qmake application of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
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 http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file. Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "msvc_nmake.h"
43 #include "option.h"
44 #include <qregexp.h>
45 #include <qhash.h>
46 #include <qdir.h>
47 #include <time.h>
48
49 QT_BEGIN_NAMESPACE
50
NmakeMakefileGenerator()51 NmakeMakefileGenerator::NmakeMakefileGenerator() : Win32MakefileGenerator(), init_flag(false)
52 {
53
54 }
55
56 bool
writeMakefile(QTextStream & t)57 NmakeMakefileGenerator::writeMakefile(QTextStream &t)
58 {
59 writeHeader(t);
60 if(!project->values("QMAKE_FAILED_REQUIREMENTS").isEmpty()) {
61 QStringList &qut = project->values("QMAKE_EXTRA_TARGETS");
62 for(QStringList::ConstIterator it = qut.begin(); it != qut.end(); ++it)
63 t << *it << " ";
64 t << "all first clean:" << "\n\t"
65 << "@echo \"Some of the required modules ("
66 << var("QMAKE_FAILED_REQUIREMENTS") << ") are not available.\"" << "\n\t"
67 << "@echo \"Skipped.\"" << endl << endl;
68 writeMakeQmake(t);
69 return true;
70 }
71
72 if(project->first("TEMPLATE") == "app" ||
73 project->first("TEMPLATE") == "lib" ||
74 project->first("TEMPLATE") == "aux") {
75 #if 0
76 if(Option::mkfile::do_stub_makefile)
77 return MakefileGenerator::writeStubMakefile(t);
78 #endif
79 writeNmakeParts(t);
80 return MakefileGenerator::writeMakefile(t);
81 }
82 else if(project->first("TEMPLATE") == "subdirs") {
83 writeSubDirs(t);
84 return true;
85 }
86 return false;
87 }
88
writeSubMakeCall(QTextStream & t,const QString & callPrefix,const QString & makeArguments,const QString & callPostfix)89 void NmakeMakefileGenerator::writeSubMakeCall(QTextStream &t, const QString &callPrefix,
90 const QString &makeArguments, const QString &callPostfix)
91 {
92 // Pass MAKEFLAGS as environment variable to sub-make calls.
93 // Unlike other make tools nmake doesn't do this automatically.
94 t << "\n\t@set MAKEFLAGS=$(MAKEFLAGS)";
95 Win32MakefileGenerator::writeSubMakeCall(t, callPrefix, makeArguments, callPostfix);
96 }
97
getPdbTarget()98 QString NmakeMakefileGenerator::getPdbTarget()
99 {
100 return QString(project->first("TARGET") + project->first("TARGET_VERSION_EXT") + ".pdb");
101 }
102
defaultInstall(const QString & t)103 QString NmakeMakefileGenerator::defaultInstall(const QString &t)
104 {
105 if((t != "target" && t != "dlltarget") ||
106 (t == "dlltarget" && (project->first("TEMPLATE") != "lib" || !project->isActiveConfig("shared"))) ||
107 project->first("TEMPLATE") == "subdirs")
108 return QString();
109
110 QString ret = Win32MakefileGenerator::defaultInstall(t);
111
112 const QString root = "$(INSTALL_ROOT)";
113 QStringList &uninst = project->values(t + ".uninstall");
114 QString targetdir = Option::fixPathToTargetOS(project->first(t + ".path"), false);
115 targetdir = fileFixify(targetdir, FileFixifyAbsolute);
116 if(targetdir.right(1) != Option::dir_sep)
117 targetdir += Option::dir_sep;
118
119 if(t == "target" && project->first("TEMPLATE") == "lib") {
120 if(project->isActiveConfig("shared") && project->isActiveConfig("debug")) {
121 QString pdb_target = getPdbTarget();
122 pdb_target.remove('"');
123 QString src_targ = (project->isEmpty("DESTDIR") ? QString("$(DESTDIR)") : project->first("DESTDIR")) + pdb_target;
124 QString dst_targ = filePrefixRoot(root, fileFixify(targetdir + pdb_target, FileFixifyAbsolute));
125 if(!ret.isEmpty())
126 ret += "\n\t";
127 ret += QString("-$(INSTALL_FILE)") + " \"" + src_targ + "\" \"" + dst_targ + "\"";
128 if(!uninst.isEmpty())
129 uninst.append("\n\t");
130 uninst.append("-$(DEL_FILE) \"" + dst_targ + "\"");
131 }
132 }
133
134 return ret;
135 }
136
findDependencies(const QString & file)137 QStringList &NmakeMakefileGenerator::findDependencies(const QString &file)
138 {
139 QStringList &aList = MakefileGenerator::findDependencies(file);
140 // Note: The QMAKE_IMAGE_COLLECTION file have all images
141 // as dependency, so don't add precompiled header then
142 if (file == project->first("QMAKE_IMAGE_COLLECTION"))
143 return aList;
144 for(QStringList::Iterator it = Option::cpp_ext.begin(); it != Option::cpp_ext.end(); ++it) {
145 if(file.endsWith(*it)) {
146 if(!precompObj.isEmpty() && !aList.contains(precompObj))
147 aList += precompObj;
148 break;
149 }
150 }
151 return aList;
152 }
153
writeNmakeParts(QTextStream & t)154 void NmakeMakefileGenerator::writeNmakeParts(QTextStream &t)
155 {
156 writeStandardParts(t);
157
158 // precompiled header
159 if(usePCH) {
160 QString precompRule = QString("-c -Yc -Fp%1 -Fo%2").arg(precompPch).arg(precompObj);
161 t << precompObj << ": " << precompH << " " << findDependencies(precompH).join(" \\\n\t\t")
162 << "\n\t" << "$(CXX) " + precompRule +" $(CXXFLAGS) $(INCPATH) -TP " << precompH << endl << endl;
163 }
164 }
165
var(const QString & value)166 QString NmakeMakefileGenerator::var(const QString &value)
167 {
168 if (usePCH) {
169 if ((value == "QMAKE_RUN_CXX_IMP_BATCH"
170 || value == "QMAKE_RUN_CXX_IMP"
171 || value == "QMAKE_RUN_CXX")) {
172 QFileInfo precompHInfo(fileInfo(precompH));
173 QString precompRule = QString("-c -FI%1 -Yu%2 -Fp%3")
174 .arg(precompHInfo.fileName())
175 .arg(precompHInfo.fileName())
176 .arg(precompPch);
177 QString p = MakefileGenerator::var(value);
178 p.replace("-c", precompRule);
179 // Cannot use -Gm with -FI & -Yu, as this gives an
180 // internal compiler error, on the newer compilers
181 // ### work-around for a VS 2003 bug. Move to some prf file or remove completely.
182 p.remove("-Gm");
183 return p;
184 } else if (value == "QMAKE_CXXFLAGS") {
185 // Remove internal compiler error option
186 // ### work-around for a VS 2003 bug. Move to some prf file or remove completely.
187 return MakefileGenerator::var(value).remove("-Gm");
188 }
189 }
190
191 // Normal val
192 return MakefileGenerator::var(value);
193 }
194
init()195 void NmakeMakefileGenerator::init()
196 {
197 if(init_flag)
198 return;
199 init_flag = true;
200
201 /* this should probably not be here, but I'm using it to wrap the .t files */
202 if(project->first("TEMPLATE") == "app")
203 project->values("QMAKE_APP_FLAG").append("1");
204 else if(project->first("TEMPLATE") == "lib")
205 project->values("QMAKE_LIB_FLAG").append("1");
206 else if(project->first("TEMPLATE") == "subdirs") {
207 MakefileGenerator::init();
208 if(project->values("MAKEFILE").isEmpty())
209 project->values("MAKEFILE").append("Makefile");
210 if(project->isEmpty("QMAKE_COPY_FILE"))
211 project->values("QMAKE_COPY_FILE").append("$(COPY)");
212 if(project->isEmpty("QMAKE_COPY_DIR"))
213 project->values("QMAKE_COPY_DIR").append("xcopy /s /q /y /i");
214 if(project->isEmpty("QMAKE_INSTALL_FILE"))
215 project->values("QMAKE_INSTALL_FILE").append("$(COPY_FILE)");
216 if(project->isEmpty("QMAKE_INSTALL_PROGRAM"))
217 project->values("QMAKE_INSTALL_PROGRAM").append("$(COPY_FILE)");
218 if(project->isEmpty("QMAKE_INSTALL_DIR"))
219 project->values("QMAKE_INSTALL_DIR").append("$(COPY_DIR)");
220 return;
221 }
222
223 project->values("QMAKE_LIBS") += escapeFilePaths(project->values("LIBS"));
224 project->values("QMAKE_LIBS_PRIVATE") += escapeFilePaths(project->values("LIBS_PRIVATE"));
225 processVars();
226
227 if (!project->values("RES_FILE").isEmpty()) {
228 project->values("QMAKE_LIBS") += escapeFilePaths(project->values("RES_FILE"));
229 }
230
231 if (!project->values("DEF_FILE").isEmpty()) {
232 QString defFileName = fileFixify(project->values("DEF_FILE")).first();
233 project->values("QMAKE_LFLAGS").append(QString("/DEF:") + escapeFilePath(defFileName));
234 }
235
236 if(!project->values("VERSION").isEmpty()) {
237 QString version = project->values("VERSION")[0];
238 int firstDot = version.indexOf(".");
239 QString major = version.left(firstDot);
240 QString minor = version.right(version.length() - firstDot - 1);
241 minor.replace(".", "");
242 project->values("QMAKE_LFLAGS").append("/VERSION:" + major + "." + minor);
243 }
244
245 // Base class init!
246 MakefileGenerator::init();
247
248 // Setup PCH variables
249 precompH = project->first("PRECOMPILED_HEADER");
250 usePCH = !precompH.isEmpty() && project->isActiveConfig("precompile_header");
251 if (usePCH) {
252 // Created files
253 precompObj = var("PRECOMPILED_DIR") + project->first("TARGET") + "_pch" + Option::obj_ext;
254 precompPch = var("PRECOMPILED_DIR") + project->first("TARGET") + "_pch.pch";
255 // Add linking of precompObj (required for whole precompiled classes)
256 project->values("OBJECTS") += precompObj;
257 // Add pch file to cleanup
258 project->values("QMAKE_CLEAN") += precompPch;
259 // Return to variable pool
260 project->values("PRECOMPILED_OBJECT") = QStringList(precompObj);
261 project->values("PRECOMPILED_PCH") = QStringList(precompPch);
262 }
263
264 QString version = project->first("TARGET_VERSION_EXT");
265 if(project->isActiveConfig("shared")) {
266 project->values("QMAKE_CLEAN").append(project->first("DESTDIR") + project->first("TARGET") + version + ".exp");
267 }
268 if(project->isActiveConfig("debug")) {
269 project->values("QMAKE_DISTCLEAN").append(project->first("DESTDIR") + project->first("TARGET") + version + ".pdb");
270 project->values("QMAKE_CLEAN").append(project->first("DESTDIR") + project->first("TARGET") + version + ".ilk");
271 project->values("QMAKE_CLEAN").append("vc*.pdb");
272 project->values("QMAKE_CLEAN").append("vc*.idb");
273 project->values("DEFINES").removeAll("NDEBUG");
274 } else {
275 QStringList &defines = project->values("DEFINES");
276 if (!defines.contains("NDEBUG"))
277 defines.append("NDEBUG");
278 }
279 }
280
writeLibDirPart(QTextStream & t)281 void NmakeMakefileGenerator::writeLibDirPart(QTextStream &t)
282 {
283 QStringList libDirs = project->values("QMAKE_LIBDIR");
284 for (int i = 0; i < libDirs.size(); ++i)
285 libDirs[i].remove("\"");
286 t << valGlue(libDirs,"/LIBPATH:\"","\" /LIBPATH:\"","\"") << " ";
287 }
288
writeImplicitRulesPart(QTextStream & t)289 void NmakeMakefileGenerator::writeImplicitRulesPart(QTextStream &t)
290 {
291 t << ".SUFFIXES:";
292 for(QStringList::Iterator cit = Option::c_ext.begin(); cit != Option::c_ext.end(); ++cit)
293 t << " " << (*cit);
294 for(QStringList::Iterator cppit = Option::cpp_ext.begin(); cppit != Option::cpp_ext.end(); ++cppit)
295 t << " " << (*cppit);
296 t << endl << endl;
297
298 if(!project->isActiveConfig("no_batch")) {
299 // Batchmode doesn't use the non implicit rules QMAKE_RUN_CXX & QMAKE_RUN_CC
300 project->variables().remove("QMAKE_RUN_CXX");
301 project->variables().remove("QMAKE_RUN_CC");
302
303 QHash<QString, void*> source_directories;
304 source_directories.insert(".", (void*)1);
305 QString directories[] = { QString("UI_SOURCES_DIR"), QString("UI_DIR"), QString() };
306 for(int y = 0; !directories[y].isNull(); y++) {
307 QString dirTemp = project->first(directories[y]);
308 if (dirTemp.endsWith("\\"))
309 dirTemp.truncate(dirTemp.length()-1);
310 if(!dirTemp.isEmpty())
311 source_directories.insert(dirTemp, (void*)1);
312 }
313 QString srcs[] = { QString("SOURCES"), QString("GENERATED_SOURCES"), QString() };
314 for(int x = 0; !srcs[x].isNull(); x++) {
315 QStringList &l = project->values(srcs[x]);
316 for(QStringList::Iterator sit = l.begin(); sit != l.end(); ++sit) {
317 QString sep = "\\";
318 if((*sit).indexOf(sep) == -1)
319 sep = "/";
320 QString dir = (*sit).section(sep, 0, -2);
321 if(!dir.isEmpty() && !source_directories[dir])
322 source_directories.insert(dir, (void*)1);
323 }
324 }
325
326 for(QHash<QString, void*>::Iterator it(source_directories.begin()); it != source_directories.end(); ++it) {
327 if(it.key().isEmpty())
328 continue;
329 QString objDir = var("OBJECTS_DIR");
330 if (objDir == ".\\")
331 objDir = "";
332 for(QStringList::Iterator cppit = Option::cpp_ext.begin(); cppit != Option::cpp_ext.end(); ++cppit)
333 t << "{" << it.key() << "}" << (*cppit) << "{" << objDir << "}" << Option::obj_ext << "::\n\t"
334 << var("QMAKE_RUN_CXX_IMP_BATCH").replace(QRegExp("\\$@"), var("OBJECTS_DIR")) << endl << "\t$<" << endl << "<<" << endl << endl;
335 for(QStringList::Iterator cit = Option::c_ext.begin(); cit != Option::c_ext.end(); ++cit)
336 t << "{" << it.key() << "}" << (*cit) << "{" << objDir << "}" << Option::obj_ext << "::\n\t"
337 << var("QMAKE_RUN_CC_IMP_BATCH").replace(QRegExp("\\$@"), var("OBJECTS_DIR")) << endl << "\t$<" << endl << "<<" << endl << endl;
338 }
339 } else {
340 for(QStringList::Iterator cppit = Option::cpp_ext.begin(); cppit != Option::cpp_ext.end(); ++cppit)
341 t << (*cppit) << Option::obj_ext << ":\n\t" << var("QMAKE_RUN_CXX_IMP") << endl << endl;
342 for(QStringList::Iterator cit = Option::c_ext.begin(); cit != Option::c_ext.end(); ++cit)
343 t << (*cit) << Option::obj_ext << ":\n\t" << var("QMAKE_RUN_CC_IMP") << endl << endl;
344 }
345
346 }
347
writeBuildRulesPart(QTextStream & t)348 void NmakeMakefileGenerator::writeBuildRulesPart(QTextStream &t)
349 {
350 if (project->first("TEMPLATE") == "aux") {
351 t << "first:" << endl;
352 return;
353 }
354
355 t << "first: all" << endl;
356 t << "all: " << fileFixify(Option::output.fileName()) << " " << varGlue("ALL_DEPS"," "," "," ") << "$(DESTDIR_TARGET)" << endl << endl;
357 t << "$(DESTDIR_TARGET): " << var("PRE_TARGETDEPS") << " $(OBJECTS) " << var("POST_TARGETDEPS");
358
359 if(!project->isEmpty("QMAKE_PRE_LINK"))
360 t << "\n\t" <<var("QMAKE_PRE_LINK");
361 if(project->isActiveConfig("staticlib")) {
362 t << "\n\t" << "$(LIBAPP) $(LIBFLAGS) /OUT:$(DESTDIR_TARGET) @<<" << "\n\t "
363 << "$(OBJECTS)";
364 } else {
365 t << "\n\t" << "$(LINK) $(LFLAGS) /OUT:$(DESTDIR_TARGET) @<< " << "\n\t "
366 << "$(OBJECTS) $(LIBS)";
367 }
368 t << endl << "<<";
369 QString signature = !project->isEmpty("SIGNATURE_FILE") ? var("SIGNATURE_FILE") : var("DEFAULT_SIGNATURE");
370 bool useSignature = !signature.isEmpty() && !project->isActiveConfig("staticlib") &&
371 !project->isEmpty("CE_SDK") && !project->isEmpty("CE_ARCH");
372 if(useSignature) {
373 t << "\n\tsigntool sign /F " << signature << " $(DESTDIR_TARGET)";
374 }
375 if(!project->isEmpty("QMAKE_POST_LINK")) {
376 t << "\n\t" << var("QMAKE_POST_LINK");
377 }
378 t << endl;
379 }
380
381 QT_END_NAMESPACE
382