don't use a different de-duplication algo for the library list in mingw prl files
[profile/ivi/qtbase.git] / qmake / generators / win32 / mingw_make.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 "mingw_make.h"
43 #include "option.h"
44 #include "meta.h"
45 #include <qregexp.h>
46 #include <qdir.h>
47 #include <stdlib.h>
48 #include <time.h>
49
50 QT_BEGIN_NAMESPACE
51
52 MingwMakefileGenerator::MingwMakefileGenerator() : Win32MakefileGenerator(), init_flag(false)
53 {
54     if (isWindowsShell())
55         quote = "\"";
56     else
57         quote = "'";
58 }
59
60 QString MingwMakefileGenerator::escapeDependencyPath(const QString &path) const
61 {
62     QString ret = path;
63     ret.remove('\"');
64     ret.replace('\\', "/");
65     ret.replace(' ', "\\ ");
66     return ret;
67 }
68
69 QString MingwMakefileGenerator::getLibTarget()
70 {
71     return QString("lib" + project->first("TARGET") + project->first("TARGET_VERSION_EXT") + ".a");
72 }
73
74 bool MingwMakefileGenerator::findLibraries()
75 {
76     return findLibraries("QMAKE_LIBS") && findLibraries("QMAKE_LIBS_PRIVATE");
77 }
78
79 bool MingwMakefileGenerator::findLibraries(const QString &where)
80 {
81     QStringList &l = project->values(where);
82
83     QList<QMakeLocalFileName> dirs;
84     {
85         const QStringList &libpaths = project->values("QMAKE_LIBDIR");
86         for (QStringList::ConstIterator libpathit = libpaths.begin();
87             libpathit != libpaths.end(); ++libpathit)
88             dirs.append(QMakeLocalFileName((*libpathit)));
89     }
90
91     QStringList::Iterator it = l.begin();
92     while (it != l.end()) {
93         if ((*it).startsWith("-l")) {
94             QString steam = (*it).mid(2), out;
95             QString suffix;
96             if (!project->isEmpty("QMAKE_" + steam.toUpper() + "_SUFFIX"))
97                 suffix = project->first("QMAKE_" + steam.toUpper() + "_SUFFIX");
98             for (QList<QMakeLocalFileName>::Iterator dir_it = dirs.begin(); dir_it != dirs.end(); ++dir_it) {
99                 QString extension;
100                 int ver = findHighestVersion((*dir_it).local(), steam, "dll.a|a");
101                 if (ver != -1)
102                     extension += QString::number(ver);
103                 extension += suffix;
104                 if(QMakeMetaInfo::libExists((*dir_it).local() + Option::dir_sep + steam) ||
105                     exists((*dir_it).local() + Option::dir_sep + steam + extension + ".a") ||
106                     exists((*dir_it).local() + Option::dir_sep + steam + extension + ".dll.a")) {
107                         out = (*it) + extension;
108                         break;
109                 }
110             }
111             if (!out.isEmpty()) // We assume if it never finds it that its correct
112                 (*it) = out;
113             } else if((*it).startsWith("-L")) {
114             dirs.append(QMakeLocalFileName((*it).mid(2)));
115         }
116
117         ++it;
118     }
119     return true;
120 }
121
122 bool MingwMakefileGenerator::writeMakefile(QTextStream &t)
123 {
124     writeHeader(t);
125     if(!project->values("QMAKE_FAILED_REQUIREMENTS").isEmpty()) {
126         t << "all clean:" << "\n\t"
127           << "@echo \"Some of the required modules ("
128           << var("QMAKE_FAILED_REQUIREMENTS") << ") are not available.\"" << "\n\t"
129           << "@echo \"Skipped.\"" << endl << endl;
130         writeMakeQmake(t);
131         return true;
132     }
133
134     if(project->first("TEMPLATE") == "app" ||
135        project->first("TEMPLATE") == "lib" ||
136        project->first("TEMPLATE") == "aux") {
137         if(project->isActiveConfig("create_pc") && project->first("TEMPLATE") == "lib")
138             writePkgConfigFile();
139
140         if(Option::mkfile::do_stub_makefile) {
141             t << "QMAKE    = " << var("QMAKE_QMAKE") << endl;
142             const QStringList &qut = project->values("QMAKE_EXTRA_TARGETS");
143             for(QStringList::ConstIterator it = qut.begin(); it != qut.end(); ++it)
144                 t << *it << " ";
145             t << "first all clean install distclean uninstall: qmake" << endl
146               << "qmake_all:" << endl;
147             writeMakeQmake(t);
148             if(project->isEmpty("QMAKE_NOFORCE"))
149                 t << "FORCE:" << endl << endl;
150             return true;
151         }
152         writeMingwParts(t);
153         return MakefileGenerator::writeMakefile(t);
154     }
155     else if(project->first("TEMPLATE") == "subdirs") {
156         writeSubDirs(t);
157         return true;
158     }
159     return false;
160  }
161
162 void createLdObjectScriptFile(const QString &fileName, const QStringList &objList)
163 {
164     QString filePath = Option::output_dir + QDir::separator() + fileName;
165     QFile file(filePath);
166     if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
167         QTextStream t(&file);
168         t << "INPUT(" << endl;
169         for (QStringList::ConstIterator it = objList.constBegin(); it != objList.constEnd(); ++it) {
170             if (QDir::isRelativePath(*it))
171                 t << "./" << *it << endl;
172             else
173                 t << *it << endl;
174         }
175         t << ");" << endl;
176         t.flush();
177         file.close();
178     }
179 }
180
181 void createArObjectScriptFile(const QString &fileName, const QString &target, const QStringList &objList)
182 {
183     QString filePath = Option::output_dir + QDir::separator() + fileName;
184     QFile file(filePath);
185     if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
186         QTextStream t(&file);
187         t << "CREATE " << target << endl;
188         for (QStringList::ConstIterator it = objList.constBegin(); it != objList.constEnd(); ++it) {
189             t << "ADDMOD " << *it << endl;
190         }
191         t << "SAVE" << endl;
192         t.flush();
193         file.close();
194     }
195 }
196
197 void createRvctObjectScriptFile(const QString &fileName, const QStringList &objList)
198 {
199     QString filePath = Option::output_dir + QDir::separator() + fileName;
200     QFile file(filePath);
201     if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
202         QTextStream t(&file);
203         for (QStringList::ConstIterator it = objList.constBegin(); it != objList.constEnd(); ++it) {
204             if (QDir::isRelativePath(*it))
205                 t << "./" << *it << endl;
206             else
207                 t << *it << endl;
208         }
209         t.flush();
210         file.close();
211     }
212 }
213
214 void MingwMakefileGenerator::writeMingwParts(QTextStream &t)
215 {
216     writeStandardParts(t);
217
218     if (!preCompHeaderOut.isEmpty()) {
219         QString header = project->first("PRECOMPILED_HEADER");
220         QString cHeader = preCompHeaderOut + Option::dir_sep + "c";
221         t << escapeDependencyPath(cHeader) << ": " << escapeDependencyPath(header) << " "
222           << escapeDependencyPaths(findDependencies(header)).join(" \\\n\t\t")
223           << "\n\t" << mkdir_p_asstring(preCompHeaderOut)
224           << "\n\t" << "$(CC) -x c-header -c $(CFLAGS) $(INCPATH) -o " << cHeader << " " << header
225           << endl << endl;
226         QString cppHeader = preCompHeaderOut + Option::dir_sep + "c++";
227         t << escapeDependencyPath(cppHeader) << ": " << escapeDependencyPath(header) << " "
228           << escapeDependencyPaths(findDependencies(header)).join(" \\\n\t\t")
229           << "\n\t" << mkdir_p_asstring(preCompHeaderOut)
230           << "\n\t" << "$(CXX) -x c++-header -c $(CXXFLAGS) $(INCPATH) -o " << cppHeader << " " << header
231           << endl << endl;
232     }
233 }
234
235 void MingwMakefileGenerator::init()
236 {
237     if(init_flag)
238         return;
239     init_flag = true;
240
241     /* this should probably not be here, but I'm using it to wrap the .t files */
242     if(project->first("TEMPLATE") == "app")
243         project->values("QMAKE_APP_FLAG").append("1");
244     else if(project->first("TEMPLATE") == "lib")
245         project->values("QMAKE_LIB_FLAG").append("1");
246     else if(project->first("TEMPLATE") == "subdirs") {
247         MakefileGenerator::init();
248         if(project->isEmpty("QMAKE_COPY_FILE"))
249             project->values("QMAKE_COPY_FILE").append("$(COPY)");
250         if(project->isEmpty("QMAKE_COPY_DIR"))
251             project->values("QMAKE_COPY_DIR").append("xcopy /s /q /y /i");
252         if(project->isEmpty("QMAKE_INSTALL_FILE"))
253             project->values("QMAKE_INSTALL_FILE").append("$(COPY_FILE)");
254         if(project->isEmpty("QMAKE_INSTALL_PROGRAM"))
255             project->values("QMAKE_INSTALL_PROGRAM").append("$(COPY_FILE)");
256         if(project->isEmpty("QMAKE_INSTALL_DIR"))
257             project->values("QMAKE_INSTALL_DIR").append("$(COPY_DIR)");
258         if(project->values("MAKEFILE").isEmpty())
259             project->values("MAKEFILE").append("Makefile");
260         return;
261     }
262
263     project->values("TARGET_PRL").append(project->first("TARGET"));
264
265     processVars();
266
267     if (!project->values("RES_FILE").isEmpty()) {
268         project->values("QMAKE_LIBS") += escapeFilePaths(project->values("RES_FILE"));
269     }
270
271     // LIBS defined in Profile comes first for gcc
272     project->values("QMAKE_LIBS") += escapeFilePaths(project->values("LIBS"));
273     project->values("QMAKE_LIBS_PRIVATE") += escapeFilePaths(project->values("LIBS_PRIVATE"));
274
275     QString targetfilename = project->values("TARGET").first();
276     QStringList &configs = project->values("CONFIG");
277
278     if(project->isActiveConfig("qt_dll"))
279         if(configs.indexOf("qt") == -1)
280             configs.append("qt");
281
282     if (project->isActiveConfig("dll")) {
283         QString destDir = "";
284         if(!project->first("DESTDIR").isEmpty())
285             destDir = Option::fixPathToTargetOS(project->first("DESTDIR") + Option::dir_sep, false, false);
286         project->values("MINGW_IMPORT_LIB").prepend(destDir + "lib" + project->first("TARGET")
287                                                          + project->first("TARGET_VERSION_EXT") + ".a");
288         project->values("QMAKE_LFLAGS").append(QString("-Wl,--out-implib,") + project->first("MINGW_IMPORT_LIB"));
289     }
290
291     if (!project->values("DEF_FILE").isEmpty()) {
292         QString defFileName = fileFixify(project->values("DEF_FILE")).first();
293         project->values("QMAKE_LFLAGS").append(QString("-Wl,") + escapeFilePath(defFileName));
294     }
295
296     MakefileGenerator::init();
297
298     // precomp
299     if (!project->first("PRECOMPILED_HEADER").isEmpty()
300         && project->isActiveConfig("precompile_header")) {
301         QString preCompHeader = var("PRECOMPILED_DIR")
302                          + QFileInfo(project->first("PRECOMPILED_HEADER")).fileName();
303         preCompHeaderOut = preCompHeader + ".gch";
304         project->values("QMAKE_CLEAN").append(preCompHeaderOut + Option::dir_sep + "c");
305         project->values("QMAKE_CLEAN").append(preCompHeaderOut + Option::dir_sep + "c++");
306
307         project->values("QMAKE_RUN_CC").clear();
308         project->values("QMAKE_RUN_CC").append("$(CC) -c -include " + preCompHeader +
309                                                     " $(CFLAGS) $(INCPATH) -o $obj $src");
310         project->values("QMAKE_RUN_CC_IMP").clear();
311         project->values("QMAKE_RUN_CC_IMP").append("$(CC)  -c -include " + preCompHeader +
312                                                         " $(CFLAGS) $(INCPATH) -o $@ $<");
313         project->values("QMAKE_RUN_CXX").clear();
314         project->values("QMAKE_RUN_CXX").append("$(CXX) -c -include " + preCompHeader +
315                                                      " $(CXXFLAGS) $(INCPATH) -o $obj $src");
316         project->values("QMAKE_RUN_CXX_IMP").clear();
317         project->values("QMAKE_RUN_CXX_IMP").append("$(CXX) -c -include " + preCompHeader +
318                                                          " $(CXXFLAGS) $(INCPATH) -o $@ $<");
319     }
320
321     if(project->isActiveConfig("dll")) {
322         project->values("QMAKE_CLEAN").append(project->first("MINGW_IMPORT_LIB"));
323     }
324 }
325
326 void MingwMakefileGenerator::fixTargetExt()
327 {
328     if (project->isActiveConfig("staticlib") && project->first("TEMPLATE") == "lib") {
329         project->values("QMAKE_LFLAGS").append("-static");
330     }
331     Win32MakefileGenerator::fixTargetExt();
332 }
333
334 void MingwMakefileGenerator::writeIncPart(QTextStream &t)
335 {
336     t << "INCPATH       = ";
337
338     const QStringList &incs = project->values("INCLUDEPATH");
339     for (QStringList::ConstIterator incit = incs.begin(); incit != incs.end(); ++incit) {
340         QString inc = (*incit);
341         inc.replace(QRegExp("\\\\$"), "");
342         inc.replace(QRegExp("\""), "");
343         t << "-I" << quote << inc << quote << " ";
344     }
345     t << "-I" << quote << specdir() << quote
346       << endl;
347 }
348
349 void MingwMakefileGenerator::writeLibsPart(QTextStream &t)
350 {
351     if(project->isActiveConfig("staticlib") && project->first("TEMPLATE") == "lib") {
352         t << "LIB        =        " << var("QMAKE_LIB") << endl;
353     } else {
354         t << "LINK        =        " << var("QMAKE_LINK") << endl;
355         t << "LFLAGS        =        " << var("QMAKE_LFLAGS") << endl;
356         t << "LIBS        =        ";
357         if(!project->values("QMAKE_LIBDIR").isEmpty())
358             writeLibDirPart(t);
359         if (project->isActiveConfig("rvct_linker")) {
360             t << var("QMAKE_LIBS") << ' '
361               << var("QMAKE_LIBS_PRIVATE") << endl;
362         } else {
363             t << var("QMAKE_LIBS").replace(QRegExp("(\\slib|^lib)")," -l") << ' '
364               << var("QMAKE_LIBS_PRIVATE").replace(QRegExp("(\\slib|^lib)")," -l") << endl;
365         }
366     }
367 }
368
369 void MingwMakefileGenerator::writeLibDirPart(QTextStream &t)
370 {
371     QStringList libDirs = project->values("QMAKE_LIBDIR");
372     QString libArg = QString::fromLatin1("-L");
373     if (project->isActiveConfig("rvct_linker"))
374         libArg = QString::fromLatin1("--userlibpath ");
375     for (int i = 0; i < libDirs.size(); ++i) {
376         libDirs[i].remove("\"");
377         if (libDirs[i].endsWith("\\"))
378             libDirs[i].chop(1);
379     }
380     t << valGlue(libDirs, libArg+quote, quote+" "+libArg+quote, quote) << " ";
381 }
382
383 void MingwMakefileGenerator::writeObjectsPart(QTextStream &t)
384 {
385     if (project->values("OBJECTS").count() < var("QMAKE_LINK_OBJECT_MAX").toInt()) {
386         objectsLinkLine = "$(OBJECTS)";
387     } else if (project->isActiveConfig("staticlib") && project->first("TEMPLATE") == "lib") {
388         QString ar_script_file = var("QMAKE_LINK_OBJECT_SCRIPT") + "." + var("TARGET");
389         if (!var("BUILD_NAME").isEmpty()) {
390             ar_script_file += "." + var("BUILD_NAME");
391         }
392         // QMAKE_LIB is used for win32, including mingw, whereas QMAKE_AR is used on Unix.
393         if (project->isActiveConfig("rvct_linker")) {
394             createRvctObjectScriptFile(ar_script_file, project->values("OBJECTS"));
395             QString ar_cmd = project->values("QMAKE_LIB").join(" ");
396             if (ar_cmd.isEmpty())
397                 ar_cmd = "armar --create";
398             objectsLinkLine = ar_cmd + " " + var("DEST_TARGET") + " --via " + escapeFilePath(ar_script_file);
399         } else {
400             // Strip off any options since the ar commands will be read from file.
401             QString ar_cmd = var("QMAKE_LIB").section(" ", 0, 0);;
402             if (ar_cmd.isEmpty())
403                 ar_cmd = "ar";
404             createArObjectScriptFile(ar_script_file, var("DEST_TARGET"), project->values("OBJECTS"));
405             objectsLinkLine = ar_cmd + " -M < " + escapeFilePath(ar_script_file);
406         }
407     } else {
408         QString ld_script_file = var("QMAKE_LINK_OBJECT_SCRIPT") + "." + var("TARGET");
409         if (!var("BUILD_NAME").isEmpty()) {
410             ld_script_file += "." + var("BUILD_NAME");
411         }
412         if (project->isActiveConfig("rvct_linker")) {
413             createRvctObjectScriptFile(ld_script_file, project->values("OBJECTS"));
414             objectsLinkLine = QString::fromLatin1("--via ") + escapeFilePath(ld_script_file);
415         } else {
416             createLdObjectScriptFile(ld_script_file, project->values("OBJECTS"));
417             objectsLinkLine = escapeFilePath(ld_script_file);
418         }
419     }
420     Win32MakefileGenerator::writeObjectsPart(t);
421 }
422
423 void MingwMakefileGenerator::writeBuildRulesPart(QTextStream &t)
424 {
425     if (project->first("TEMPLATE") == "aux") {
426         t << "first:" << endl;
427         t << "all:" << endl;
428         return;
429     }
430
431     t << "first: all" << endl;
432     t << "all: " << escapeDependencyPath(fileFixify(Option::output.fileName())) << " " << valGlue(escapeDependencyPaths(project->values("ALL_DEPS"))," "," "," ") << " $(DESTDIR_TARGET)" << endl << endl;
433     t << "$(DESTDIR_TARGET): " << var("PRE_TARGETDEPS") << " $(OBJECTS) " << var("POST_TARGETDEPS");
434     if(!project->isEmpty("QMAKE_PRE_LINK"))
435         t << "\n\t" <<var("QMAKE_PRE_LINK");
436     if(project->isActiveConfig("staticlib") && project->first("TEMPLATE") == "lib") {
437         if (project->values("OBJECTS").count() < var("QMAKE_LINK_OBJECT_MAX").toInt()) {
438             t << "\n\t" << "$(LIB) $(DESTDIR_TARGET) " << objectsLinkLine << " " ;
439         } else {
440             t << "\n\t" << objectsLinkLine << " " ;
441         }
442     } else {
443         t << "\n\t" << "$(LINK) $(LFLAGS) -o $(DESTDIR_TARGET) " << objectsLinkLine << " " << " $(LIBS)";
444     }
445     if(!project->isEmpty("QMAKE_POST_LINK"))
446         t << "\n\t" <<var("QMAKE_POST_LINK");
447     t << endl;
448 }
449
450 void MingwMakefileGenerator::writeRcFilePart(QTextStream &t)
451 {
452     const QString rc_file = fileFixify(project->first("RC_FILE"));
453
454     QString incPathStr = fileInfo(rc_file).path();
455     if (incPathStr != "." && QDir::isRelativePath(incPathStr))
456         incPathStr.prepend("./");
457
458     if (!rc_file.isEmpty()) {
459         t << escapeDependencyPath(var("RES_FILE")) << ": " << rc_file << "\n\t"
460           << var("QMAKE_RC") << " -i " << rc_file << " -o " << var("RES_FILE") 
461           << " --include-dir=" << incPathStr << " $(DEFINES)" << endl << endl;
462     }
463 }
464
465 QStringList &MingwMakefileGenerator::findDependencies(const QString &file)
466 {
467     QStringList &aList = MakefileGenerator::findDependencies(file);
468     // Note: The QMAKE_IMAGE_COLLECTION file have all images
469     // as dependency, so don't add precompiled header then
470     if (file == project->first("QMAKE_IMAGE_COLLECTION")
471         || preCompHeaderOut.isEmpty())
472         return aList;
473     for (QStringList::Iterator it = Option::c_ext.begin(); it != Option::c_ext.end(); ++it) {
474         if (file.endsWith(*it)) {
475             QString cHeader = preCompHeaderOut + Option::dir_sep + "c";
476             if (!aList.contains(cHeader))
477                 aList += cHeader;
478             break;
479         }
480     }
481     for (QStringList::Iterator it = Option::cpp_ext.begin(); it != Option::cpp_ext.end(); ++it) {
482         if (file.endsWith(*it)) {
483             QString cppHeader = preCompHeaderOut + Option::dir_sep + "c++";
484             if (!aList.contains(cppHeader))
485                 aList += cppHeader;
486             break;
487         }
488     }
489     return aList;
490 }
491
492 QT_END_NAMESPACE