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