unifiy initialization of QMAKE_LIBS{,_PRIVATE} among windows generators
[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     processVars();
247
248     if (!project->values("RES_FILE").isEmpty()) {
249         project->values("QMAKE_LIBS") += escapeFilePaths(project->values("RES_FILE"));
250     }
251
252     if (!project->values("DEF_FILE").isEmpty()) {
253         QString defFileName = fileFixify(project->values("DEF_FILE")).first();
254         project->values("QMAKE_LFLAGS").append(QString("/DEF:") + escapeFilePath(defFileName));
255     }
256
257     if(!project->values("VERSION").isEmpty()) {
258         QString version = project->values("VERSION")[0];
259         int firstDot = version.indexOf(".");
260         QString major = version.left(firstDot);
261         QString minor = version.right(version.length() - firstDot - 1);
262         minor.replace(".", "");
263         project->values("QMAKE_LFLAGS").append("/VERSION:" + major + "." + minor);
264     }
265
266     // Base class init!
267     MakefileGenerator::init();
268
269     // Setup PCH variables
270     precompH = project->first("PRECOMPILED_HEADER");
271     usePCH = !precompH.isEmpty() && project->isActiveConfig("precompile_header");
272     if (usePCH) {
273         // Created files
274         precompObj = var("PRECOMPILED_DIR") + project->first("TARGET") + "_pch" + Option::obj_ext;
275         precompPch = var("PRECOMPILED_DIR") + project->first("TARGET") + "_pch.pch";
276         // Add linking of precompObj (required for whole precompiled classes)
277         project->values("OBJECTS")                  += precompObj;
278         // Add pch file to cleanup
279         project->values("QMAKE_CLEAN")          += precompPch;
280         // Return to variable pool
281         project->values("PRECOMPILED_OBJECT") = QStringList(precompObj);
282         project->values("PRECOMPILED_PCH")    = QStringList(precompPch);
283     }
284
285     QString version = project->first("TARGET_VERSION_EXT");
286     if(project->isActiveConfig("shared")) {
287         project->values("QMAKE_CLEAN").append(project->first("DESTDIR") + project->first("TARGET") + version + ".exp");
288     }
289     if(project->isActiveConfig("debug")) {
290         project->values("QMAKE_DISTCLEAN").append(project->first("DESTDIR") + project->first("TARGET") + version + ".pdb");
291         project->values("QMAKE_CLEAN").append(project->first("DESTDIR") + project->first("TARGET") + version + ".ilk");
292         project->values("QMAKE_CLEAN").append("vc*.pdb");
293         project->values("QMAKE_CLEAN").append("vc*.idb");
294     }
295 }
296
297 void NmakeMakefileGenerator::writeLibDirPart(QTextStream &t)
298 {
299     QStringList libDirs = project->values("QMAKE_LIBDIR");
300     for (int i = 0; i < libDirs.size(); ++i)
301         libDirs[i].remove("\"");
302     t << valGlue(libDirs,"/LIBPATH:\"","\" /LIBPATH:\"","\"") << " ";
303 }
304
305 void NmakeMakefileGenerator::writeImplicitRulesPart(QTextStream &t)
306 {
307     t << ".SUFFIXES:";
308     for(QStringList::Iterator cit = Option::c_ext.begin(); cit != Option::c_ext.end(); ++cit)
309         t << " " << (*cit);
310     for(QStringList::Iterator cppit = Option::cpp_ext.begin(); cppit != Option::cpp_ext.end(); ++cppit)
311         t << " " << (*cppit);
312     t << endl << endl;
313
314     if(!project->isActiveConfig("no_batch")) {
315         // Batchmode doesn't use the non implicit rules QMAKE_RUN_CXX & QMAKE_RUN_CC
316         project->variables().remove("QMAKE_RUN_CXX");
317         project->variables().remove("QMAKE_RUN_CC");
318
319         QHash<QString, void*> source_directories;
320         source_directories.insert(".", (void*)1);
321         QString directories[] = { QString("UI_SOURCES_DIR"), QString("UI_DIR"), QString() };
322         for(int y = 0; !directories[y].isNull(); y++) {
323             QString dirTemp = project->first(directories[y]);
324             if (dirTemp.endsWith("\\"))
325                 dirTemp.truncate(dirTemp.length()-1);
326             if(!dirTemp.isEmpty())
327                 source_directories.insert(dirTemp, (void*)1);
328         }
329         QString srcs[] = { QString("SOURCES"), QString("GENERATED_SOURCES"), QString() };
330         for(int x = 0; !srcs[x].isNull(); x++) {
331             const QStringList &l = project->values(srcs[x]);
332             for (QStringList::ConstIterator sit = l.begin(); sit != l.end(); ++sit) {
333                 QString sep = "\\";
334                 if((*sit).indexOf(sep) == -1)
335                     sep = "/";
336                 QString dir = (*sit).section(sep, 0, -2);
337                 if(!dir.isEmpty() && !source_directories[dir])
338                     source_directories.insert(dir, (void*)1);
339             }
340         }
341
342         for(QHash<QString, void*>::Iterator it(source_directories.begin()); it != source_directories.end(); ++it) {
343             if(it.key().isEmpty())
344                 continue;
345             QString objDir = var("OBJECTS_DIR");
346             if (objDir == ".\\")
347                 objDir = "";
348             for(QStringList::Iterator cppit = Option::cpp_ext.begin(); cppit != Option::cpp_ext.end(); ++cppit)
349                 t << "{" << it.key() << "}" << (*cppit) << "{" << objDir << "}" << Option::obj_ext << "::\n\t"
350                   << var("QMAKE_RUN_CXX_IMP_BATCH").replace(QRegExp("\\$@"), var("OBJECTS_DIR")) << endl << "\t$<" << endl << "<<" << endl << endl;
351             for(QStringList::Iterator cit = Option::c_ext.begin(); cit != Option::c_ext.end(); ++cit)
352                 t << "{" << it.key() << "}" << (*cit) << "{" << objDir << "}" << Option::obj_ext << "::\n\t"
353                   << var("QMAKE_RUN_CC_IMP_BATCH").replace(QRegExp("\\$@"), var("OBJECTS_DIR")) << endl << "\t$<" << endl << "<<" << endl << endl;
354         }
355     } else {
356         for(QStringList::Iterator cppit = Option::cpp_ext.begin(); cppit != Option::cpp_ext.end(); ++cppit)
357             t << (*cppit) << Option::obj_ext << ":\n\t" << var("QMAKE_RUN_CXX_IMP") << endl << endl;
358         for(QStringList::Iterator cit = Option::c_ext.begin(); cit != Option::c_ext.end(); ++cit)
359             t << (*cit) << Option::obj_ext << ":\n\t" << var("QMAKE_RUN_CC_IMP") << endl << endl;
360     }
361
362 }
363
364 void NmakeMakefileGenerator::writeBuildRulesPart(QTextStream &t)
365 {
366     if (project->first("TEMPLATE") == "aux") {
367         t << "first:" << endl;
368         t << "all:" << endl;
369         return;
370     }
371
372     t << "first: all" << endl;
373     t << "all: " << fileFixify(Option::output.fileName()) << " " << varGlue("ALL_DEPS"," "," "," ") << "$(DESTDIR_TARGET)" << endl << endl;
374     t << "$(DESTDIR_TARGET): " << var("PRE_TARGETDEPS") << " $(OBJECTS) " << var("POST_TARGETDEPS");
375
376     if(!project->isEmpty("QMAKE_PRE_LINK"))
377         t << "\n\t" <<var("QMAKE_PRE_LINK");
378     if(project->isActiveConfig("staticlib")) {
379         t << "\n\t" << "$(LIBAPP) $(LIBFLAGS) /OUT:$(DESTDIR_TARGET) @<<" << "\n\t  "
380           << "$(OBJECTS)";
381     } else {
382         t << "\n\t" << "$(LINK) $(LFLAGS) /OUT:$(DESTDIR_TARGET) @<< " << "\n\t  "
383           << "$(OBJECTS) $(LIBS)";
384     }
385     t << endl << "<<";
386     QString signature = !project->isEmpty("SIGNATURE_FILE") ? var("SIGNATURE_FILE") : var("DEFAULT_SIGNATURE");
387     bool useSignature = !signature.isEmpty() && !project->isActiveConfig("staticlib") && 
388                         !project->isEmpty("CE_SDK") && !project->isEmpty("CE_ARCH");
389     if(useSignature) {
390         t << "\n\tsigntool sign /F " << signature << " $(DESTDIR_TARGET)";
391     }
392     if(!project->isEmpty("QMAKE_POST_LINK")) {
393         t << "\n\t" << var("QMAKE_POST_LINK");
394     }
395     t << endl;
396 }
397
398 QT_END_NAMESPACE