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