1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Copyright (C) 2016 Intel Corporation.
5 ** Contact: https://www.qt.io/licensing/
6 **
7 ** This file is part of the qmake application of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:GPL-EXCEPT$
10 ** Commercial License Usage
11 ** Licensees holding valid commercial Qt licenses may use this file in
12 ** accordance with the commercial license agreement provided with the
13 ** Software or, alternatively, in accordance with the terms contained in
14 ** a written agreement between you and The Qt Company. For licensing terms
15 ** and conditions see https://www.qt.io/terms-conditions. For further
16 ** information use the contact form at https://www.qt.io/contact-us.
17 **
18 ** GNU General Public License Usage
19 ** Alternatively, this file may be used under the terms of the GNU
20 ** General Public License version 3 as published by the Free Software
21 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
22 ** included in the packaging of this file. Please review the following
23 ** information to ensure the GNU General Public License requirements will
24 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
25 **
26 ** $QT_END_LICENSE$
27 **
28 ****************************************************************************/
29 
30 #include "unixmake.h"
31 #include "option.h"
32 #include "meta.h"
33 #include <qregexp.h>
34 #include <qbytearray.h>
35 #include <qfile.h>
36 #include <qdir.h>
37 #include <qdebug.h>
38 #include <time.h>
39 
40 QT_BEGIN_NAMESPACE
41 
42 void
writePrlFile(QTextStream & t)43 UnixMakefileGenerator::writePrlFile(QTextStream &t)
44 {
45     MakefileGenerator::writePrlFile(t);
46     const ProString tmplt = project->first("TEMPLATE");
47     if (tmplt != "lib" && tmplt != "aux")
48         return;
49     // libtool support
50     if (project->isActiveConfig("create_libtool")) {
51         writeLibtoolFile();
52     }
53     // pkg-config support
54     if (project->isActiveConfig("create_pc"))
55         writePkgConfigFile();
56 }
57 
58 bool
writeMakefile(QTextStream & t)59 UnixMakefileGenerator::writeMakefile(QTextStream &t)
60 {
61 
62     writeHeader(t);
63     if (writeDummyMakefile(t))
64         return true;
65 
66     if (project->first("TEMPLATE") == "app" ||
67         project->first("TEMPLATE") == "lib" ||
68         project->first("TEMPLATE") == "aux") {
69         writeMakeParts(t);
70         return MakefileGenerator::writeMakefile(t);
71     } else if (project->first("TEMPLATE") == "subdirs") {
72         MakefileGenerator::writeSubDirs(t);
73         return true;
74     }
75     return false;
76 }
77 
78 void
writeDefaultVariables(QTextStream & t)79 UnixMakefileGenerator::writeDefaultVariables(QTextStream &t)
80 {
81     MakefileGenerator::writeDefaultVariables(t);
82     t << "TAR           = " << var("QMAKE_TAR") << Qt::endl;
83     t << "COMPRESS      = " << var("QMAKE_GZIP") << Qt::endl;
84 
85     if (project->isEmpty("QMAKE_DISTNAME")) {
86         ProString distname = project->first("QMAKE_ORIG_TARGET");
87         if (!project->isActiveConfig("no_dist_version"))
88             distname += project->first("VERSION");
89         project->values("QMAKE_DISTNAME") = distname;
90     }
91     t << "DISTNAME      = " << fileVar("QMAKE_DISTNAME") << Qt::endl;
92 
93     if (project->isEmpty("QMAKE_DISTDIR"))
94         project->values("QMAKE_DISTDIR") = project->first("QMAKE_DISTNAME");
95     t << "DISTDIR = " << escapeFilePath(fileFixify(
96             (project->isEmpty("OBJECTS_DIR") ? ProString(".tmp/") : project->first("OBJECTS_DIR")) + project->first("QMAKE_DISTDIR"),
97             FileFixifyFromOutdir | FileFixifyAbsolute)) << Qt::endl;
98 }
99 
100 void
writeSubTargets(QTextStream & t,QList<MakefileGenerator::SubTarget * > targets,int flags)101 UnixMakefileGenerator::writeSubTargets(QTextStream &t, QList<MakefileGenerator::SubTarget*> targets, int flags)
102 {
103     MakefileGenerator::writeSubTargets(t, targets, flags);
104 
105     t << "dist: distdir FORCE" << Qt::endl;
106     t << "\t(cd `dirname $(DISTDIR)` && $(TAR) $(DISTNAME).tar $(DISTNAME) && $(COMPRESS) $(DISTNAME).tar)"
107          " && $(MOVE) `dirname $(DISTDIR)`/$(DISTNAME).tar.gz . && $(DEL_FILE) -r $(DISTDIR)";
108     t << Qt::endl << Qt::endl;
109 
110     t << "distdir:";
111     for (int target = 0; target < targets.size(); ++target) {
112         SubTarget *subtarget = targets.at(target);
113         t << " " << subtarget->target << "-distdir";
114     }
115     t << " FORCE\n\t"
116       << mkdir_p_asstring("$(DISTDIR)", false) << "\n\t"
117       << "$(COPY_FILE) --parents " << fileVar("DISTFILES") << " $(DISTDIR)" << Option::dir_sep << Qt::endl << Qt::endl;
118 
119     const QString abs_source_path = project->first("QMAKE_ABSOLUTE_SOURCE_PATH").toQString();
120     for (int target = 0; target < targets.size(); ++target) {
121         SubTarget *subtarget = targets.at(target);
122         QString in_directory = subtarget->in_directory;
123         if (!in_directory.isEmpty() && !in_directory.endsWith(Option::dir_sep))
124             in_directory += Option::dir_sep;
125         QString out_directory = subtarget->out_directory;
126         if (!out_directory.isEmpty() && !out_directory.endsWith(Option::dir_sep))
127             out_directory += Option::dir_sep;
128         if (!abs_source_path.isEmpty() && out_directory.startsWith(abs_source_path))
129             out_directory = Option::output_dir + out_directory.mid(abs_source_path.length());
130 
131         QString dist_directory = out_directory;
132         if (dist_directory.endsWith(Option::dir_sep))
133             dist_directory.chop(Option::dir_sep.length());
134         if (!dist_directory.startsWith(Option::dir_sep))
135             dist_directory.prepend(Option::dir_sep);
136 
137         QString out_directory_cdin = out_directory.isEmpty() ? "\n\t"
138                                                              : "\n\tcd " + escapeFilePath(out_directory) + " && ";
139         QString makefilein = " -e -f " + escapeFilePath(subtarget->makefile)
140                 + " distdir DISTDIR=$(DISTDIR)" + escapeFilePath(dist_directory);
141 
142         QString out = subtarget->makefile;
143         QString in = escapeFilePath(fileFixify(in_directory + subtarget->profile, FileFixifyAbsolute));
144         if (out.startsWith(in_directory))
145             out.remove(0, in_directory.length());
146 
147         t << subtarget->target << "-distdir: FORCE";
148         writeSubTargetCall(t, in_directory, in, out_directory, escapeFilePath(out),
149                            out_directory_cdin, makefilein);
150         t << Qt::endl;
151     }
152 }
153 
rfc1034Identifier(const QString & str)154 static QString rfc1034Identifier(const QString &str)
155 {
156     QString s = str;
157     for (QChar &ch : s) {
158         const char c = ch.toLatin1();
159 
160         const bool okChar = (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z')
161                 || (c >= 'a' && c <= 'z') || c == '-' || c == '.';
162         if (!okChar)
163             ch = QChar::fromLatin1('-');
164     }
165     return s;
166 }
167 
escapeDir(const QString & dir)168 static QString escapeDir(const QString &dir)
169 {
170     // When building on non-MSys MinGW, the path ends with a backslash, which
171     // GNU make will interpret that as a line continuation. Doubling the backslash
172     // avoids the problem, at the cost of the variable containing *both* backslashes.
173     if (dir.endsWith('\\'))
174         return dir + '\\';
175     return dir;
176 }
177 
178 void
writeMakeParts(QTextStream & t)179 UnixMakefileGenerator::writeMakeParts(QTextStream &t)
180 {
181     bool do_incremental = (project->isActiveConfig("incremental") &&
182                            !project->values("QMAKE_INCREMENTAL").isEmpty() &&
183                            (!project->values("QMAKE_APP_FLAG").isEmpty() ||
184                             (!project->isActiveConfig("staticlib")))),
185          src_incremental=false;
186 
187     ProStringList &bundledFiles = project->values("QMAKE_BUNDLED_FILES");
188 
189     writeExportedVariables(t);
190 
191     t << "####### Compiler, tools and options\n\n";
192     t << "CC            = " << var("QMAKE_CC") << Qt::endl;
193     t << "CXX           = " << var("QMAKE_CXX") << Qt::endl;
194     t << "DEFINES       = "
195       << varGlue("PRL_EXPORT_DEFINES","-D"," -D"," ")
196       << varGlue("DEFINES","-D"," -D","") << Qt::endl;
197     t << "CFLAGS        = " << var("QMAKE_CFLAGS") << " $(DEFINES)\n";
198     t << "CXXFLAGS      = " << var("QMAKE_CXXFLAGS") << " $(DEFINES)\n";
199     t << "INCPATH       =";
200     {
201         const ProStringList &incs = project->values("INCLUDEPATH");
202         for(int i = 0; i < incs.size(); ++i) {
203             const ProString &inc = incs.at(i);
204             if (inc.isEmpty())
205                 continue;
206 
207             t << " -I" << escapeFilePath(inc);
208         }
209     }
210     if(!project->isEmpty("QMAKE_FRAMEWORKPATH_FLAGS"))
211        t << " " << var("QMAKE_FRAMEWORKPATH_FLAGS");
212     t << Qt::endl;
213 
214     writeDefaultVariables(t);
215 
216     if(!project->isActiveConfig("staticlib")) {
217         t << "LINK          = " << var("QMAKE_LINK") << Qt::endl;
218         t << "LFLAGS        = " << var("QMAKE_LFLAGS") << Qt::endl;
219         t << "LIBS          = $(SUBLIBS) " << fixLibFlags("LIBS").join(' ') << ' '
220                                            << fixLibFlags("LIBS_PRIVATE").join(' ') << ' '
221                                            << fixLibFlags("QMAKE_LIBS").join(' ') << ' '
222                                            << fixLibFlags("QMAKE_LIBS_PRIVATE").join(' ') << Qt::endl;
223     }
224 
225     t << "AR            = " << var("QMAKE_AR") << Qt::endl;
226     t << "RANLIB        = " << var("QMAKE_RANLIB") << Qt::endl;
227     t << "SED           = " << var("QMAKE_STREAM_EDITOR") << Qt::endl;
228     t << "STRIP         = " << var("QMAKE_STRIP") << Qt::endl;
229 
230     t << Qt::endl;
231 
232     t << "####### Output directory\n\n";
233     // This is used in commands by some .prf files.
234     if (! project->values("OBJECTS_DIR").isEmpty())
235         t << "OBJECTS_DIR   = " << escapeDir(fileVar("OBJECTS_DIR")) << Qt::endl;
236     else
237         t << "OBJECTS_DIR   = ./\n";
238     t << Qt::endl;
239 
240     /* files */
241     t << "####### Files\n\n";
242     // This is used by the dist target.
243     t << "SOURCES       = " << fileVarList("SOURCES") << ' ' << fileVarList("GENERATED_SOURCES") << Qt::endl;
244     auto objectParts = writeObjectsPart(t, do_incremental);
245     src_incremental = objectParts.first;
246     if(do_incremental && !src_incremental)
247         do_incremental = false;
248     t << "DIST          = " << valList(fileFixify(project->values("DISTFILES").toQStringList())) << " "
249                             << fileVarList("HEADERS") << ' ' << fileVarList("SOURCES") << Qt::endl;
250     t << "QMAKE_TARGET  = " << fileVar("QMAKE_ORIG_TARGET") << Qt::endl;
251     t << "DESTDIR       = " << escapeDir(fileVar("DESTDIR")) << Qt::endl;
252     t << "TARGET        = " << fileVar("TARGET") << Qt::endl;
253     if(project->isActiveConfig("plugin")) {
254         t << "TARGETD       = " << fileVar("TARGET") << Qt::endl;
255     } else if(!project->isActiveConfig("staticlib") && project->values("QMAKE_APP_FLAG").isEmpty()) {
256         t << "TARGETA       = " << fileVar("TARGETA") << Qt::endl;
257         if(!project->isEmpty("QMAKE_BUNDLE")) {
258             t << "TARGETD       = " << fileVar("TARGET_x.y") << Qt::endl;
259             t << "TARGET0       = " << fileVar("TARGET_") << Qt::endl;
260         } else if (!project->isActiveConfig("unversioned_libname")) {
261             t << "TARGET0       = " << fileVar("TARGET_") << Qt::endl;
262             if (project->isEmpty("QMAKE_HPUX_SHLIB")) {
263                 t << "TARGETD       = " << fileVar("TARGET_x.y.z") << Qt::endl;
264                 t << "TARGET1       = " << fileVar("TARGET_x") << Qt::endl;
265                 t << "TARGET2       = " << fileVar("TARGET_x.y") << Qt::endl;
266             } else {
267                 t << "TARGETD       = " << fileVar("TARGET_x") << Qt::endl;
268             }
269         }
270     }
271     writeExtraCompilerVariables(t);
272     writeExtraVariables(t);
273     t << Qt::endl;
274 
275     // blasted includes
276     const ProStringList &qeui = project->values("QMAKE_EXTRA_INCLUDES");
277     ProStringList::ConstIterator it;
278     for(it = qeui.begin(); it != qeui.end(); ++it)
279         t << "include " << escapeDependencyPath(*it) << Qt::endl;
280 
281     /* rules */
282     t << "first:" << (!project->isActiveConfig("no_default_goal_deps") ? " all" : "") << "\n";
283 
284     if(include_deps) {
285         if (project->isActiveConfig("gcc_MD_depends")) {
286             ProStringList objects = project->values("OBJECTS");
287             for (ProStringList::Iterator it = objects.begin(); it != objects.end(); ++it) {
288                 QString d_file = (*it).toQString().replace(QRegExp(Option::obj_ext + "$"), ".d");
289                 t << "-include " << escapeDependencyPath(d_file) << Qt::endl;
290                 project->values("QMAKE_DISTCLEAN") << d_file;
291             }
292         } else {
293             QString cmd=var("QMAKE_CFLAGS_DEPS") + " ";
294             cmd += varGlue("DEFINES","-D"," -D","") + varGlue("PRL_EXPORT_DEFINES"," -D"," -D","");
295             if(!project->isEmpty("QMAKE_ABSOLUTE_SOURCE_PATH"))
296                 cmd += " -I" + fileVar("QMAKE_ABSOLUTE_SOURCE_PATH") + ' ';
297             cmd += " $(INCPATH) " + fileVarGlue("DEPENDPATH", "-I", " -I", "");
298             ProString odir;
299             if(!project->values("OBJECTS_DIR").isEmpty())
300                 odir = project->first("OBJECTS_DIR");
301             QString odird = escapeDependencyPath(odir.toQString());
302 
303             QString pwd = escapeFilePath(fileFixify(qmake_getpwd()));
304 
305             t << "###### Dependencies\n\n";
306             t << odird << ".deps/%.d: " << pwd << "/%.cpp\n\t";
307             if(project->isActiveConfig("echo_depend_creation"))
308                 t << "@echo Creating depend for $<\n\t";
309             t << mkdir_p_asstring("$(@D)", false) << "\n\t"
310               << "@$(CXX) " << cmd << " $< | sed \"s,^\\($(*F).o\\):," << odir << "\\1:,g\" >$@\n\n";
311 
312             t << odird << ".deps/%.d: " << pwd << "/%.c\n\t";
313             if(project->isActiveConfig("echo_depend_creation"))
314                 t << "@echo Creating depend for $<\n\t";
315             t << mkdir_p_asstring("$(@D)", false) << "\n\t"
316               << "@$(CC) " << cmd << " $< | sed \"s,^\\($(*F).o\\):," << odir << "\\1:,g\" >$@\n\n";
317 
318             static const char * const src[] = { "SOURCES", "GENERATED_SOURCES", nullptr };
319             for (int x = 0; src[x]; x++) {
320                 const ProStringList &l = project->values(src[x]);
321                 for (ProStringList::ConstIterator it = l.begin(); it != l.end(); ++it) {
322                     if(!(*it).isEmpty()) {
323                         QString d_file;
324                         for(QStringList::Iterator cit = Option::c_ext.begin();
325                             cit != Option::c_ext.end(); ++cit) {
326                             if((*it).endsWith((*cit))) {
327                                 d_file = (*it).left((*it).length() - (*cit).length()).toQString();
328                                 break;
329                             }
330                         }
331                         if(d_file.isEmpty()) {
332                             for(QStringList::Iterator cppit = Option::cpp_ext.begin();
333                                 cppit != Option::cpp_ext.end(); ++cppit) {
334                                 if((*it).endsWith((*cppit))) {
335                                     d_file = (*it).left((*it).length() - (*cppit).length()).toQString();
336                                     break;
337                                 }
338                             }
339                         }
340 
341                         if(!d_file.isEmpty()) {
342                             d_file = odir + ".deps/" + fileFixify(d_file, FileFixifyBackwards) + ".d";
343                             QString d_file_d = escapeDependencyPath(d_file);
344                             QStringList deps = findDependencies((*it).toQString()).filter(QRegExp(
345                                         "((^|/)" + Option::h_moc_mod + "|" + Option::cpp_moc_ext + "$)"));
346                             if(!deps.isEmpty())
347                                 t << d_file_d << ": " << finalizeDependencyPaths(deps).join(' ') << Qt::endl;
348                             t << "-include " << d_file_d << Qt::endl;
349                             project->values("QMAKE_DISTCLEAN") += d_file;
350                         }
351                     }
352                 }
353             }
354         }
355     }
356 
357     t << "####### Build rules\n\n";
358     if(!project->values("SUBLIBS").isEmpty()) {
359         ProString libdir = "tmp/";
360         if(!project->isEmpty("SUBLIBS_DIR"))
361             libdir = project->first("SUBLIBS_DIR");
362         t << "SUBLIBS       = ";
363         const ProStringList &l = project->values("SUBLIBS");
364         for (ProStringList::ConstIterator it = l.begin(); it != l.end(); ++it)
365             t << escapeFilePath(libdir + project->first("QMAKE_PREFIX_STATICLIB") + (*it) + '.'
366                                 + project->first("QMAKE_EXTENSION_STATICLIB")) << ' ';
367         t << Qt::endl << Qt::endl;
368     }
369     QString target_deps;
370     if ((project->isActiveConfig("depend_prl") || project->isActiveConfig("fast_depend_prl"))
371         && !project->isEmpty("QMAKE_PRL_INTERNAL_FILES")) {
372         const ProStringList &l = project->values("QMAKE_PRL_INTERNAL_FILES");
373         ProStringList::ConstIterator it;
374         for(it = l.begin(); it != l.end(); ++it) {
375             QMakeMetaInfo libinfo;
376             if (libinfo.readLib((*it).toQString()) && !libinfo.isEmpty("QMAKE_PRL_BUILD_DIR")) {
377                 ProString dir;
378                 int slsh = (*it).lastIndexOf(Option::dir_sep);
379                 if(slsh != -1)
380                     dir = (*it).left(slsh + 1);
381                 QString targ = dir + libinfo.first("QMAKE_PRL_TARGET");
382                 QString targ_d = escapeDependencyPath(targ);
383                 target_deps += ' ' + targ_d;
384                 t << targ_d;
385                 if (project->isActiveConfig("fast_depend_prl"))
386                     t << ":\n\t@echo \"Creating '";
387                 else
388                     t << ": FORCE\n\t@echo \"Creating/updating '";
389                 t << targ << "'\"\n\t"
390                   << "(cd " << escapeFilePath(libinfo.first("QMAKE_PRL_BUILD_DIR")) << ';'
391                   << "$(MAKE))\n";
392             }
393         }
394     }
395     QString deps = escapeDependencyPath(fileFixify(Option::output.fileName()));
396     QString allDeps;
397     if (!project->values("QMAKE_APP_FLAG").isEmpty() || project->first("TEMPLATE") == "aux") {
398         QString destdir = project->first("DESTDIR").toQString();
399         if(!project->isEmpty("QMAKE_BUNDLE")) {
400             QString bundle_loc = project->first("QMAKE_BUNDLE_LOCATION").toQString();
401             if(!bundle_loc.isEmpty() && !bundle_loc.startsWith("/"))
402                 bundle_loc.prepend("/");
403             if(!bundle_loc.endsWith("/"))
404                 bundle_loc += "/";
405             destdir += project->first("QMAKE_BUNDLE") + bundle_loc;
406         }
407         if(do_incremental) {
408             //incremental target
409             QString incr_target = var("TARGET") + "_incremental";
410             if(incr_target.indexOf(Option::dir_sep) != -1)
411                 incr_target = incr_target.right(incr_target.length() -
412                                                 (incr_target.lastIndexOf(Option::dir_sep) + 1));
413             QString incr_deps, incr_objs;
414             if(project->first("QMAKE_INCREMENTAL_STYLE") == "ld") {
415                 QString incr_target_dir = var("OBJECTS_DIR") + incr_target + Option::obj_ext;
416                 QString incr_target_dir_d = escapeDependencyPath(incr_target_dir);
417                 QString incr_target_dir_f = escapeFilePath(incr_target_dir);
418                 //actual target
419                 t << incr_target_dir_d << ": $(OBJECTS)\n\t"
420                   << "ld -r  -o " << incr_target_dir_f << " $(OBJECTS)\n";
421                 //communicated below
422                 deps.prepend(incr_target_dir_d + ' ');
423                 incr_deps = "$(INCREMENTAL_OBJECTS)";
424                 if(!incr_objs.isEmpty())
425                     incr_objs += " ";
426                 incr_objs += incr_target_dir_f;
427             } else {
428                 //actual target
429                 QString incr_target_dir = var("DESTDIR") + "lib" + incr_target + "." +
430                                           project->first("QMAKE_EXTENSION_SHLIB");
431                 QString incr_target_dir_d = escapeDependencyPath(incr_target_dir);
432                 QString incr_target_dir_f = escapeFilePath(incr_target_dir);
433                 QString incr_lflags = var("QMAKE_LFLAGS_SHLIB") + " ";
434                 if(project->isActiveConfig("debug"))
435                     incr_lflags += var("QMAKE_LFLAGS_DEBUG");
436                 else if (project->isActiveConfig("debug_info"))
437                     incr_lflags += var("QMAKE_LFLAGS_RELEASE_WITH_DEBUGINFO");
438                 else
439                     incr_lflags += var("QMAKE_LFLAGS_RELEASE");
440                 t << incr_target_dir_d << ": $(INCREMENTAL_OBJECTS)\n\t";
441                 if(!destdir.isEmpty())
442                     t << "\n\t" << mkdir_p_asstring(destdir) << "\n\t";
443                 t << "$(LINK) " << incr_lflags << " -o "<< incr_target_dir_f <<
444                     " $(INCREMENTAL_OBJECTS)\n";
445                 //communicated below
446                 if(!destdir.isEmpty()) {
447                     if(!incr_objs.isEmpty())
448                         incr_objs += " ";
449                     incr_objs += "-L" + escapeFilePath(destdir);
450                 } else {
451                     if(!incr_objs.isEmpty())
452                         incr_objs += " ";
453                     incr_objs += "-L" + escapeFilePath(qmake_getpwd());
454                 }
455                 if(!incr_objs.isEmpty())
456                     incr_objs += " ";
457                 incr_objs += " -l" + escapeFilePath(incr_target);
458                 deps.prepend(incr_target_dir_d + ' ');
459                 incr_deps = "$(OBJECTS)";
460             }
461 
462             //real target
463             t << var("TARGET") << ": " << depVar("PRE_TARGETDEPS") << ' ' << incr_deps << ' ' << target_deps
464               << ' ' << depVar("POST_TARGETDEPS") << "\n\t";
465             if(!destdir.isEmpty())
466                 t << "\n\t" << mkdir_p_asstring(destdir) << "\n\t";
467             if(!project->isEmpty("QMAKE_PRE_LINK"))
468                 t << var("QMAKE_PRE_LINK") << "\n\t";
469             t << "$(LINK) $(LFLAGS) " << var("QMAKE_LINK_O_FLAG") << "$(TARGET) " << incr_deps << " " << incr_objs << " $(OBJCOMP) $(LIBS)";
470             if(!project->isEmpty("QMAKE_POST_LINK"))
471                 t << "\n\t" << var("QMAKE_POST_LINK");
472             t << Qt::endl << Qt::endl;
473         } else {
474             t << depVar("TARGET") << ": " << depVar("PRE_TARGETDEPS") << " $(OBJECTS) "
475               << target_deps << ' ' << depVar("POST_TARGETDEPS") << "\n\t";
476             if (project->first("TEMPLATE") != "aux") {
477                 if (!destdir.isEmpty())
478                     t << mkdir_p_asstring(destdir) << "\n\t";
479                 if (!project->isEmpty("QMAKE_PRE_LINK"))
480                     t << var("QMAKE_PRE_LINK") << "\n\t";
481                 t << "$(LINK) $(LFLAGS) " << var("QMAKE_LINK_O_FLAG") << "$(TARGET) "
482                   << objectParts.second << " $(OBJCOMP) $(LIBS)";
483                 if (!project->isEmpty("QMAKE_POST_LINK"))
484                     t << "\n\t" << var("QMAKE_POST_LINK");
485             }
486             t << Qt::endl << Qt::endl;
487         }
488         allDeps = ' ' + depVar("TARGET");
489     } else if(!project->isActiveConfig("staticlib")) {
490         QString destdir_r = project->first("DESTDIR").toQString(), incr_deps;
491         if(!project->isEmpty("QMAKE_BUNDLE")) {
492             QString bundle_loc = project->first("QMAKE_BUNDLE_LOCATION").toQString();
493             if(!bundle_loc.isEmpty() && !bundle_loc.startsWith("/"))
494                 bundle_loc.prepend("/");
495             if(!bundle_loc.endsWith("/"))
496                 bundle_loc += "/";
497             destdir_r += project->first("QMAKE_BUNDLE") + bundle_loc;
498         }
499         QString destdir_d = escapeDependencyPath(destdir_r);
500         QString destdir = escapeFilePath(destdir_r);
501 
502         if(do_incremental) {
503             ProString s_ext = project->first("QMAKE_EXTENSION_SHLIB");
504             QString incr_target = var("QMAKE_ORIG_TARGET").replace(
505                 QRegExp("\\." + s_ext), "").replace(QRegExp("^lib"), "") + "_incremental";
506             if(incr_target.indexOf(Option::dir_sep) != -1)
507                 incr_target = incr_target.right(incr_target.length() -
508                                                 (incr_target.lastIndexOf(Option::dir_sep) + 1));
509 
510             if(project->first("QMAKE_INCREMENTAL_STYLE") == "ld") {
511                 QString incr_target_dir = var("OBJECTS_DIR") + incr_target + Option::obj_ext;
512                 QString incr_target_dir_d = escapeDependencyPath(incr_target_dir);
513                 QString incr_target_dir_f = escapeFilePath(incr_target_dir);
514                 //actual target
515                 const QString link_deps = "$(OBJECTS) ";
516                 t << incr_target_dir_d << ": " << link_deps << "\n\t"
517                   << "ld -r  -o " << incr_target_dir_f << ' ' << link_deps << Qt::endl;
518                 //communicated below
519                 ProStringList &cmd = project->values("QMAKE_LINK_SHLIB_CMD");
520                 cmd[0] = cmd.at(0).toQString().replace(QLatin1String("$(OBJECTS) "), QLatin1String("$(INCREMENTAL_OBJECTS)")); //ick
521                 cmd.append(incr_target_dir_f);
522                 deps.prepend(incr_target_dir_d + ' ');
523                 incr_deps = "$(INCREMENTAL_OBJECTS)";
524             } else {
525                 //actual target
526                 QString incr_target_dir = destdir_r + "lib" + incr_target + '.' + s_ext;
527                 QString incr_target_dir_d = escapeDependencyPath(incr_target_dir);
528                 QString incr_target_dir_f = escapeFilePath(incr_target_dir);
529                 QString incr_lflags = var("QMAKE_LFLAGS_SHLIB") + " ";
530                 if(!project->isEmpty("QMAKE_LFLAGS_INCREMENTAL"))
531                     incr_lflags += var("QMAKE_LFLAGS_INCREMENTAL") + " ";
532                 if(project->isActiveConfig("debug"))
533                     incr_lflags += var("QMAKE_LFLAGS_DEBUG");
534                 else if (project->isActiveConfig("debug_info"))
535                     incr_lflags += var("QMAKE_LFLAGS_RELEASE_WITH_DEBUGINFO");
536                 else
537                     incr_lflags += var("QMAKE_LFLAGS_RELEASE");
538                 t << incr_target_dir_d << ": $(INCREMENTAL_OBJECTS)\n\t";
539                 if(!destdir.isEmpty())
540                     t << mkdir_p_asstring(destdir, false) << "\n\t";
541                 t << "$(LINK) " << incr_lflags << ' ' << var("QMAKE_LINK_O_FLAG") << incr_target_dir_f <<
542                     " $(INCREMENTAL_OBJECTS)\n";
543                 //communicated below
544                 ProStringList &cmd = project->values("QMAKE_LINK_SHLIB_CMD");
545                 if(!destdir.isEmpty())
546                     cmd.append(" -L" + destdir);
547                 cmd.append(" -l" + escapeFilePath(incr_target));
548                 deps.prepend(incr_target_dir_d + ' ');
549                 incr_deps = "$(OBJECTS)";
550             }
551 
552             //real target
553             t << destdir_d << depVar("TARGET") << ": " << depVar("PRE_TARGETDEPS") << ' '
554               << incr_deps << " $(SUBLIBS) " << target_deps << ' ' << depVar("POST_TARGETDEPS");
555         } else {
556             ProStringList &cmd = project->values("QMAKE_LINK_SHLIB_CMD");
557             cmd[0] = cmd.at(0).toQString().replace(QLatin1String("$(OBJECTS)"), objectParts.second);
558             t << destdir_d << depVar("TARGET") << ": " << depVar("PRE_TARGETDEPS")
559               << " $(OBJECTS) $(SUBLIBS) $(OBJCOMP) " << target_deps
560               << ' ' << depVar("POST_TARGETDEPS");
561         }
562         allDeps = ' ' + destdir_d + depVar("TARGET");
563         if(!destdir.isEmpty())
564             t << "\n\t" << mkdir_p_asstring(destdir, false);
565         if(!project->isEmpty("QMAKE_PRE_LINK"))
566             t << "\n\t" << var("QMAKE_PRE_LINK");
567 
568         if (project->isActiveConfig("plugin")) {
569             t << "\n\t"
570               << "-$(DEL_FILE) $(TARGET)\n\t"
571               << var("QMAKE_LINK_SHLIB_CMD");
572             if(!destdir.isEmpty())
573                 t << "\n\t"
574                   << "-$(MOVE) $(TARGET) " << destdir << "$(TARGET)";
575             if(!project->isEmpty("QMAKE_POST_LINK"))
576                 t << "\n\t" << var("QMAKE_POST_LINK");
577             t << Qt::endl << Qt::endl;
578         } else if(!project->isEmpty("QMAKE_BUNDLE")) {
579             bundledFiles << destdir_r + var("TARGET");
580             t << "\n\t"
581               << "-$(DEL_FILE) $(TARGET) $(TARGET0) $(DESTDIR)$(TARGET0)\n\t"
582               << var("QMAKE_LINK_SHLIB_CMD") << "\n\t"
583               << mkdir_p_asstring("\"`dirname $(DESTDIR)$(TARGETD)`\"", false) << "\n\t"
584               << "-$(MOVE) $(TARGET) $(DESTDIR)$(TARGETD)\n\t"
585               << mkdir_p_asstring("\"`dirname $(DESTDIR)$(TARGET0)`\"", false) << "\n\t";
586             if (!project->isActiveConfig("shallow_bundle"))
587                 t << varGlue("QMAKE_LN_SHLIB", "-", " ",
588                              " Versions/Current/$(TARGET) $(DESTDIR)$(TARGET0)") << "\n\t";
589             if(!project->isEmpty("QMAKE_POST_LINK"))
590                 t << "\n\t" << var("QMAKE_POST_LINK");
591             t << Qt::endl << Qt::endl;
592         } else if(project->isEmpty("QMAKE_HPUX_SHLIB")) {
593             t << "\n\t";
594 
595             if (!project->isActiveConfig("unversioned_libname"))
596                 t << "-$(DEL_FILE) $(TARGET) $(TARGET0) $(TARGET1) $(TARGET2)";
597             else
598                 t << "-$(DEL_FILE) $(TARGET)";
599 
600             t << "\n\t" << var("QMAKE_LINK_SHLIB_CMD");
601 
602             if (!project->isActiveConfig("unversioned_libname")) {
603                 t << "\n\t"
604                   << varGlue("QMAKE_LN_SHLIB","-"," "," $(TARGET) $(TARGET0)") << "\n\t"
605                   << varGlue("QMAKE_LN_SHLIB","-"," "," $(TARGET) $(TARGET1)") << "\n\t"
606                   << varGlue("QMAKE_LN_SHLIB","-"," "," $(TARGET) $(TARGET2)");
607             }
608             if (!destdir.isEmpty()) {
609                 t << "\n\t"
610                   << "-$(DEL_FILE) " << destdir << "$(TARGET)\n\t"
611                   << "-$(MOVE) $(TARGET) " << destdir << "$(TARGET)";
612 
613                 if (!project->isActiveConfig("unversioned_libname")) {
614                     t << "\n\t"
615                       << "-$(DEL_FILE) " << destdir << "$(TARGET0)\n\t"
616                       << "-$(DEL_FILE) " << destdir << "$(TARGET1)\n\t"
617                       << "-$(DEL_FILE) " << destdir << "$(TARGET2)\n\t"
618                       << "-$(MOVE) $(TARGET0) " << destdir << "$(TARGET0)\n\t"
619                       << "-$(MOVE) $(TARGET1) " << destdir << "$(TARGET1)\n\t"
620                       << "-$(MOVE) $(TARGET2) " << destdir << "$(TARGET2)";
621                 }
622             }
623             if(!project->isEmpty("QMAKE_POST_LINK"))
624                 t << "\n\t" << var("QMAKE_POST_LINK");
625             t << Qt::endl << Qt::endl;
626         } else {
627             t << "\n\t"
628               << "-$(DEL_FILE) $(TARGET) $(TARGET0)\n\t"
629               << var("QMAKE_LINK_SHLIB_CMD") << "\n\t";
630             t << varGlue("QMAKE_LN_SHLIB",""," "," $(TARGET) $(TARGET0)");
631             if(!destdir.isEmpty())
632                 t  << "\n\t"
633                    << "-$(DEL_FILE) " << destdir << "$(TARGET)\n\t"
634                    << "-$(DEL_FILE) " << destdir << "$(TARGET0)\n\t"
635                    << "-$(MOVE) $(TARGET) " << destdir << "$(TARGET)\n\t"
636                    << "-$(MOVE) $(TARGET0) " << destdir << "$(TARGET0)\n\t";
637             if(!project->isEmpty("QMAKE_POST_LINK"))
638                 t << "\n\t" << var("QMAKE_POST_LINK");
639             t << Qt::endl << Qt::endl;
640         }
641         t << Qt::endl << Qt::endl;
642 
643         if (! project->isActiveConfig("plugin")) {
644             t << "staticlib: " << depVar("TARGETA") << "\n\n";
645             t << depVar("TARGETA") << ": " << depVar("PRE_TARGETDEPS") << " $(OBJECTS) $(OBJCOMP)";
646             if(do_incremental)
647                 t << " $(INCREMENTAL_OBJECTS)";
648             t << ' ' << depVar("POST_TARGETDEPS") << "\n\t";
649             if (!project->isEmpty("QMAKE_PRE_LINK"))
650                 t << var("QMAKE_PRE_LINK") << "\n\t";
651             t << "-$(DEL_FILE) $(TARGETA) \n\t"
652               << var("QMAKE_AR_CMD");
653             if(do_incremental)
654                 t << " $(INCREMENTAL_OBJECTS)";
655             if (!project->isEmpty("QMAKE_POST_LINK"))
656                 t << "\n\t" << var("QMAKE_POST_LINK");
657             if(!project->isEmpty("QMAKE_RANLIB"))
658                 t << "\n\t$(RANLIB) $(TARGETA)";
659             t << Qt::endl << Qt::endl;
660         }
661     } else {
662         QString destdir_r = project->first("DESTDIR").toQString();
663         QString destdir_d = escapeDependencyPath(destdir_r);
664         QString destdir = escapeFilePath(destdir_r);
665         allDeps = ' ' + destdir_d + depVar("TARGET");
666         t << "staticlib: " << destdir_d << "$(TARGET)\n\n"
667           << destdir_d << depVar("TARGET") << ": " << depVar("PRE_TARGETDEPS")
668           << " $(OBJECTS) $(OBJCOMP) " << depVar("POST_TARGETDEPS") << "\n\t";
669         if (!destdir.isEmpty())
670             t << mkdir_p_asstring(destdir, false) << "\n\t";
671         if (!project->isEmpty("QMAKE_PRE_LINK"))
672             t << var("QMAKE_PRE_LINK") << "\n\t";
673         t << "-$(DEL_FILE) " << destdir << "$(TARGET)\n\t"
674           << var("QMAKE_AR_CMD") << "\n";
675         if (!project->isEmpty("QMAKE_POST_LINK"))
676             t << "\t" << var("QMAKE_POST_LINK") << "\n";
677         if (!project->isEmpty("QMAKE_RANLIB"))
678             t << "\t$(RANLIB) " << destdir << "$(TARGET)\n";
679         t << Qt::endl << Qt::endl;
680     }
681 
682     writeMakeQmake(t);
683     if(project->isEmpty("QMAKE_FAILED_REQUIREMENTS") && !project->isActiveConfig("no_autoqmake")) {
684         QStringList meta_files;
685         if (project->isActiveConfig("create_libtool") && project->first("TEMPLATE") == "lib") { //libtool
686             meta_files += libtoolFileName();
687         }
688         if(project->isActiveConfig("create_pc") && project->first("TEMPLATE") == "lib") { //pkg-config
689             meta_files += pkgConfigFileName();
690         }
691         if(!meta_files.isEmpty())
692             t << escapeDependencyPaths(meta_files).join(" ") << ": \n\t"
693               << "@$(QMAKE) -prl " << escapeFilePath(project->projectFile()) << ' ' << buildArgs(true) << Qt::endl;
694     }
695 
696     if (!project->isEmpty("QMAKE_BUNDLE")) {
697         QHash<QString, QString> symlinks;
698         ProStringList &alldeps = project->values("ALL_DEPS");
699         QString bundle_dir = project->first("DESTDIR") + project->first("QMAKE_BUNDLE") + "/";
700         if (!project->first("QMAKE_PKGINFO").isEmpty()) {
701             ProString pkginfo = project->first("QMAKE_PKGINFO");
702             ProString pkginfo_f = escapeFilePath(pkginfo);
703             ProString pkginfo_d = escapeDependencyPath(pkginfo);
704             bundledFiles << pkginfo;
705             alldeps << pkginfo;
706             QString destdir = bundle_dir + "Contents";
707             t << pkginfo_d << ": \n\t";
708             if (!destdir.isEmpty())
709                 t << mkdir_p_asstring(destdir) << "\n\t";
710             t << "@$(DEL_FILE) " << pkginfo_f << "\n\t"
711               << "@echo \"APPL"
712               << (project->isEmpty("QMAKE_PKGINFO_TYPEINFO")
713                   ? QString::fromLatin1("????") : project->first("QMAKE_PKGINFO_TYPEINFO").left(4))
714               << "\" > " << pkginfo_f << Qt::endl;
715         }
716         if (!project->first("QMAKE_BUNDLE_RESOURCE_FILE").isEmpty()) {
717             ProString resources = project->first("QMAKE_BUNDLE_RESOURCE_FILE");
718             ProString resources_f = escapeFilePath(resources);
719             ProString resources_d = escapeDependencyPath(resources);
720             bundledFiles << resources;
721             alldeps << resources;
722             QString destdir = bundle_dir + "Contents/Resources";
723             t << resources_d << ": \n\t";
724             t << mkdir_p_asstring(destdir) << "\n\t";
725             t << "@touch " << resources_f << "\n\t\n";
726         }
727         //copy the plist
728         while (!project->isActiveConfig("no_plist")) {  // 'while' just to be able to 'break'
729             QString info_plist = project->first("QMAKE_INFO_PLIST").toQString();
730             if (info_plist.isEmpty()) {
731                 info_plist = escapeFilePath(specdir() + QDir::separator() + "Info.plist." + project->first("TEMPLATE"));
732             } else if (!exists(Option::normalizePath(info_plist))) {
733                 warn_msg(WarnLogic, "Could not resolve Info.plist: '%s'. Check if QMAKE_INFO_PLIST points to a valid file.",
734                          info_plist.toLatin1().constData());
735                 break;
736             } else {
737                 info_plist = escapeFilePath(fileFixify(info_plist));
738             }
739             bool isFramework = project->first("TEMPLATE") == "lib"
740                 && !project->isActiveConfig("plugin")
741                 && project->isActiveConfig("lib_bundle");
742             bool isShallowBundle = project->isActiveConfig("shallow_bundle");
743             QString info_plist_out = bundle_dir +
744                 (!isShallowBundle
745                     ? (isFramework
746                         ? ("Versions/" + project->first("QMAKE_FRAMEWORK_VERSION") + "/Resources/")
747                         : "Contents/")
748                     : QString())
749                 + "Info.plist";
750             bundledFiles << info_plist_out;
751             alldeps << info_plist_out;
752             QString destdir = info_plist_out.section(Option::dir_sep, 0, -2);
753             t << escapeDependencyPath(info_plist_out) << ": \n\t";
754             info_plist_out = escapeFilePath(info_plist_out);
755             if (!destdir.isEmpty())
756                 t << mkdir_p_asstring(destdir) << "\n\t";
757             ProStringList commonSedArgs;
758             if (!project->values("VERSION").isEmpty()) {
759                 const ProString shortVersion =
760                     project->first("VER_MAJ") + "." +
761                     project->first("VER_MIN");
762                 commonSedArgs << "-e \"s,@SHORT_VERSION@," << shortVersion << ",g\" ";
763                 commonSedArgs << "-e \"s,\\$${QMAKE_SHORT_VERSION}," << shortVersion << ",g\" ";
764                 const ProString fullVersion =
765                     project->first("VER_MAJ") + "." +
766                     project->first("VER_MIN") + "." +
767                     project->first("VER_PAT");
768                 commonSedArgs << "-e \"s,@FULL_VERSION@," << fullVersion << ",g\" ";
769                 commonSedArgs << "-e \"s,\\$${QMAKE_FULL_VERSION}," << fullVersion << ",g\" ";
770             }
771             const ProString typeInfo = project->isEmpty("QMAKE_PKGINFO_TYPEINFO")
772                 ? QString::fromLatin1("????")
773                 : project->first("QMAKE_PKGINFO_TYPEINFO").left(4);
774             commonSedArgs << "-e \"s,@TYPEINFO@," << typeInfo << ",g\" ";
775             commonSedArgs << "-e \"s,\\$${QMAKE_PKGINFO_TYPEINFO}," << typeInfo << ",g\" ";
776 
777             QString bundlePrefix = project->first("QMAKE_TARGET_BUNDLE_PREFIX").toQString();
778             if (bundlePrefix.isEmpty())
779                 bundlePrefix = "com.yourcompany";
780             if (bundlePrefix.endsWith("."))
781                 bundlePrefix.chop(1);
782             QString bundleIdentifier =  bundlePrefix + "." + var("QMAKE_BUNDLE");
783             if (bundleIdentifier.endsWith(".app"))
784                 bundleIdentifier.chop(4);
785             if (bundleIdentifier.endsWith(".framework"))
786                 bundleIdentifier.chop(10);
787             // replace invalid bundle id characters
788             bundleIdentifier = rfc1034Identifier(bundleIdentifier);
789             commonSedArgs << "-e \"s,@BUNDLEIDENTIFIER@," << bundleIdentifier << ",g\" ";
790             commonSedArgs << "-e \"s,\\$${PRODUCT_BUNDLE_IDENTIFIER}," << bundleIdentifier << ",g\" ";
791 
792             commonSedArgs << "-e \"s,\\$${MACOSX_DEPLOYMENT_TARGET},"
793                           << project->first("QMAKE_MACOSX_DEPLOYMENT_TARGET").toQString() << ",g\" ";
794             commonSedArgs << "-e \"s,\\$${IPHONEOS_DEPLOYMENT_TARGET},"
795                           << project->first("QMAKE_IPHONEOS_DEPLOYMENT_TARGET").toQString() << ",g\" ";
796             commonSedArgs << "-e \"s,\\$${TVOS_DEPLOYMENT_TARGET},"
797                           << project->first("QMAKE_TVOS_DEPLOYMENT_TARGET").toQString() << ",g\" ";
798             commonSedArgs << "-e \"s,\\$${WATCHOS_DEPLOYMENT_TARGET},"
799                           << project->first("QMAKE_WATCHOS_DEPLOYMENT_TARGET").toQString() << ",g\" ";
800 
801             if (!isFramework) {
802                 ProString app_bundle_name = var("QMAKE_APPLICATION_BUNDLE_NAME");
803                 if (app_bundle_name.isEmpty())
804                     app_bundle_name = var("QMAKE_ORIG_TARGET");
805 
806                 ProString plugin_bundle_name = var("QMAKE_PLUGIN_BUNDLE_NAME");
807                 if (plugin_bundle_name.isEmpty())
808                     plugin_bundle_name = var("QMAKE_ORIG_TARGET");
809 
810                 QString icon = fileFixify(var("ICON"));
811                 t << "@$(DEL_FILE) " << info_plist_out << "\n\t"
812                   << "@sed ";
813                 for (const ProString &arg : qAsConst(commonSedArgs))
814                     t << arg;
815                 const QString iconName = icon.section(Option::dir_sep, -1);
816                 t << "-e \"s,@ICON@," << iconName << ",g\" "
817                   << "-e \"s,\\$${ASSETCATALOG_COMPILER_APPICON_NAME}," << iconName << ",g\" "
818                   << "-e \"s,@EXECUTABLE@," << app_bundle_name << ",g\" "
819                   << "-e \"s,@LIBRARY@," << plugin_bundle_name << ",g\" "
820                   << "-e \"s,\\$${EXECUTABLE_NAME}," << (app_bundle_name.isEmpty() ? app_bundle_name : plugin_bundle_name) << ",g\" "
821                   << "-e \"s,@TYPEINFO@,"<< typeInfo << ",g\" "
822                   << "-e \"s,\\$${QMAKE_PKGINFO_TYPEINFO},"<< typeInfo << ",g\" "
823                   << "" << info_plist << " >" << info_plist_out << Qt::endl;
824                 //copy the icon
825                 if (!project->isEmpty("ICON")) {
826                     QString dir = bundle_dir + "Contents/Resources/";
827                     const QString icon_path = dir + icon.section(Option::dir_sep, -1);
828                     QString icon_path_f = escapeFilePath(icon_path);
829                     bundledFiles << icon_path;
830                     alldeps << icon_path;
831                     t << escapeDependencyPath(icon_path) << ": " << escapeDependencyPath(icon) << "\n\t"
832                       << mkdir_p_asstring(dir) << "\n\t"
833                       << "@$(DEL_FILE) " << icon_path_f << "\n\t"
834                       << "@$(COPY_FILE) " << escapeFilePath(icon) << ' ' << icon_path_f << Qt::endl;
835                 }
836             } else {
837                 ProString lib_bundle_name = var("QMAKE_FRAMEWORK_BUNDLE_NAME");
838                 if (lib_bundle_name.isEmpty())
839                     lib_bundle_name = var("QMAKE_ORIG_TARGET");
840 
841                 if (!isShallowBundle)
842                     symlinks[bundle_dir + "Resources"] = "Versions/Current/Resources";
843                 t << "@$(DEL_FILE) " << info_plist_out << "\n\t"
844                   << "@sed ";
845                 for (const ProString &arg : qAsConst(commonSedArgs))
846                     t << arg;
847                 t << "-e \"s,@LIBRARY@," << lib_bundle_name << ",g\" "
848                   << "-e \"s,\\$${EXECUTABLE_NAME}," << lib_bundle_name << ",g\" "
849                   << "-e \"s,@TYPEINFO@," << typeInfo << ",g\" "
850                   << "-e \"s,\\$${QMAKE_PKGINFO_TYPEINFO}," << typeInfo << ",g\" "
851                   << "" << info_plist << " >" << info_plist_out << Qt::endl;
852             }
853             break;
854         } // project->isActiveConfig("no_plist")
855         //copy other data
856         if(!project->isEmpty("QMAKE_BUNDLE_DATA")) {
857             const ProStringList &bundle_data = project->values("QMAKE_BUNDLE_DATA");
858             for(int i = 0; i < bundle_data.count(); i++) {
859                 const ProStringList &files = project->values(ProKey(bundle_data[i] + ".files"));
860                 QString path = bundle_dir;
861                 const ProKey pkey(bundle_data[i] + ".path");
862                 if (!project->isActiveConfig("shallow_bundle")) {
863                     const ProKey vkey(bundle_data[i] + ".version");
864                     if (!project->isEmpty(vkey)) {
865                         QString version = project->first(vkey) + "/" +
866                                           project->first("QMAKE_FRAMEWORK_VERSION") + "/";
867                         ProString name = project->first(pkey);
868                         int pos = name.indexOf('/');
869                         if (pos > 0)
870                             name = name.mid(0, pos);
871                         symlinks[Option::fixPathToTargetOS(path + name)] =
872                                 project->first(vkey) + "/Current/" + name;
873                         path += version;
874                     }
875                 }
876                 path += project->first(pkey).toQString();
877                 path = Option::fixPathToTargetOS(path);
878                 for(int file = 0; file < files.count(); file++) {
879                     QString fn = files.at(file).toQString();
880                     QString src = fileFixify(fn, FileFixifyAbsolute);
881                     if (!QFile::exists(src))
882                         src = fileFixify(fn, FileFixifyFromOutdir);
883                     else
884                         src = fileFixify(fn);
885                     QString dst = path + Option::dir_sep + fileInfo(fn).fileName();
886                     bundledFiles << dst;
887                     alldeps << dst;
888                     t << escapeDependencyPath(dst) << ": " << escapeDependencyPath(src) << "\n\t"
889                       << mkdir_p_asstring(path) << "\n\t";
890                     src = escapeFilePath(src);
891                     dst = escapeFilePath(dst);
892                     QFileInfo fi(fileInfo(fn));
893                     if(fi.isDir())
894                         t << "@$(DEL_FILE) -r " << dst << "\n\t"
895                           << "@$(COPY_DIR) " << src << " " << dst << Qt::endl;
896                     else
897                         t << "@$(DEL_FILE) " << dst << "\n\t"
898                           << "@$(COPY_FILE) " << src << " " << dst << Qt::endl;
899                 }
900             }
901         }
902         if (!symlinks.isEmpty()) {
903             QString bundle_dir_f = escapeFilePath(bundle_dir);
904             QHash<QString, QString>::ConstIterator symIt = symlinks.constBegin(),
905                                                    symEnd = symlinks.constEnd();
906             for (; symIt != symEnd; ++symIt) {
907                 bundledFiles << symIt.key();
908                 alldeps << symIt.key();
909                 t << escapeDependencyPath(symIt.key()) << ":\n\t"
910                   << mkdir_p_asstring(bundle_dir) << "\n\t"
911                   << "@$(SYMLINK) " << escapeFilePath(symIt.value()) << ' ' << bundle_dir_f << Qt::endl;
912             }
913 
914             if (!project->isActiveConfig("shallow_bundle")) {
915                 QString currentLink = bundle_dir + "Versions/Current";
916                 QString currentLink_f = escapeDependencyPath(currentLink);
917                 bundledFiles << currentLink;
918                 alldeps << currentLink;
919                 t << currentLink_f << ": $(MAKEFILE)\n\t"
920                   << mkdir_p_asstring(bundle_dir + "Versions") << "\n\t"
921                   << "@-$(DEL_FILE) " << currentLink_f << "\n\t"
922                   << "@$(SYMLINK) " << project->first("QMAKE_FRAMEWORK_VERSION")
923                              << ' ' << currentLink_f << Qt::endl;
924             }
925         }
926     }
927 
928     t << Qt::endl << "all: " << deps
929       << valGlue(escapeDependencyPaths(project->values("ALL_DEPS")), " \\\n\t\t", " \\\n\t\t", "")
930       << allDeps << Qt::endl << Qt::endl;
931 
932     t << "dist: distdir FORCE\n\t";
933     t << "(cd `dirname $(DISTDIR)` && $(TAR) $(DISTNAME).tar $(DISTNAME) && $(COMPRESS) $(DISTNAME).tar)"
934          " && $(MOVE) `dirname $(DISTDIR)`" << Option::dir_sep << "$(DISTNAME).tar.gz ."
935          " && $(DEL_FILE) -r $(DISTDIR)";
936     t << Qt::endl << Qt::endl;
937 
938     t << "distdir: FORCE\n\t"
939       << mkdir_p_asstring("$(DISTDIR)", false) << "\n\t"
940       << "$(COPY_FILE) --parents $(DIST) $(DISTDIR)" << Option::dir_sep << Qt::endl;
941     if(!project->isEmpty("QMAKE_EXTRA_COMPILERS")) {
942         const ProStringList &quc = project->values("QMAKE_EXTRA_COMPILERS");
943         for (ProStringList::ConstIterator it = quc.begin(); it != quc.end(); ++it) {
944             const ProStringList &var = project->values(ProKey(*it + ".input"));
945             for (ProStringList::ConstIterator var_it = var.begin(); var_it != var.end(); ++var_it) {
946                 const ProStringList &val = project->values((*var_it).toKey());
947                 if(val.isEmpty())
948                     continue;
949                 t << "\t$(COPY_FILE) --parents " << escapeFilePaths(val).join(' ')
950                                                  << " $(DISTDIR)" << Option::dir_sep << Qt::endl;
951             }
952         }
953     }
954     if(!project->isEmpty("TRANSLATIONS"))
955         t << "\t$(COPY_FILE) --parents " << fileVar("TRANSLATIONS") << " $(DISTDIR)" << Option::dir_sep << Qt::endl;
956     t << Qt::endl << Qt::endl;
957 
958     QString clean_targets = " compiler_clean " + depVar("CLEAN_DEPS");
959     if(do_incremental) {
960         t << "incrclean:\n";
961         if(src_incremental)
962             t << "\t-$(DEL_FILE) $(INCREMENTAL_OBJECTS)\n";
963         t << Qt::endl;
964     }
965 
966     t << "clean:" << clean_targets << "\n\t";
967     if(!project->isEmpty("OBJECTS")) {
968         t << "-$(DEL_FILE) $(OBJECTS)\n\t";
969     }
970     if(doPrecompiledHeaders() && !project->isEmpty("PRECOMPILED_HEADER")) {
971         ProStringList precomp_files;
972         ProString precomph_out_dir;
973 
974         if(!project->isEmpty("PRECOMPILED_DIR"))
975             precomph_out_dir = project->first("PRECOMPILED_DIR");
976         precomph_out_dir += project->first("QMAKE_ORIG_TARGET");
977         precomph_out_dir += project->first("QMAKE_PCH_OUTPUT_EXT");
978 
979         if (project->isActiveConfig("icc_pch_style")) {
980             // icc style
981             ProString pchBaseName = project->first("QMAKE_ORIG_TARGET");
982             ProStringList pchArchs = project->values("QMAKE_PCH_ARCHS");
983             if (pchArchs.isEmpty())
984                 pchArchs << ProString(); // normal single-arch PCH
985             for (const ProString &arch : qAsConst(pchArchs)) {
986                 ProString pchOutput;
987                 if (!project->isEmpty("PRECOMPILED_DIR"))
988                     pchOutput = project->first("PRECOMPILED_DIR");
989                 pchOutput += pchBaseName + project->first("QMAKE_PCH_OUTPUT_EXT");
990                 if (!arch.isEmpty())
991                     pchOutput = ProString(pchOutput.toQString().replace(
992                         QStringLiteral("${QMAKE_PCH_ARCH}"), arch.toQString()));
993 
994                 ProString sourceFile = pchOutput + Option::cpp_ext.first();
995                 ProString objectFile = createObjectList(ProStringList(sourceFile)).first();
996                 precomp_files << precomph_out_dir << sourceFile << objectFile;
997             }
998         } else {
999             // gcc style (including clang_pch_style)
1000             precomph_out_dir += Option::dir_sep;
1001 
1002             ProString header_prefix = project->first("QMAKE_PRECOMP_PREFIX");
1003             ProString header_suffix = project->isActiveConfig("clang_pch_style")
1004                                ? project->first("QMAKE_PCH_OUTPUT_EXT") : "";
1005 
1006             for (const ProString &compiler : project->values("QMAKE_BUILTIN_COMPILERS")) {
1007                 if (project->isEmpty(ProKey("QMAKE_" + compiler + "FLAGS_PRECOMPILE")))
1008                     continue;
1009                 ProString language = project->first(ProKey("QMAKE_LANGUAGE_" + compiler));
1010                 if (language.isEmpty())
1011                     continue;
1012 
1013                 ProStringList pchArchs = project->values("QMAKE_PCH_ARCHS");
1014                 if (pchArchs.isEmpty())
1015                     pchArchs << ProString(); // normal single-arch PCH
1016                 for (const ProString &arch : qAsConst(pchArchs)) {
1017                     QString file = precomph_out_dir + header_prefix + language + header_suffix;
1018                     if (!arch.isEmpty())
1019                         file.replace(QStringLiteral("${QMAKE_PCH_ARCH}"), arch.toQString());
1020                     precomp_files += file;
1021                 }
1022             }
1023         }
1024         t << "-$(DEL_FILE) " << escapeFilePaths(precomp_files).join(' ') << "\n\t";
1025     }
1026     if(src_incremental)
1027         t << "-$(DEL_FILE) $(INCREMENTAL_OBJECTS)\n\t";
1028     t << fileVarGlue("QMAKE_CLEAN","-$(DEL_FILE) "," ","\n\t")
1029       << "-$(DEL_FILE) *~ core *.core\n"
1030       << fileVarGlue("CLEAN_FILES","\t-$(DEL_FILE) "," ","") << Qt::endl << Qt::endl;
1031 
1032     ProString destdir = project->first("DESTDIR");
1033     if (!destdir.isEmpty() && !destdir.endsWith(Option::dir_sep))
1034         destdir += Option::dir_sep;
1035     t << "distclean: clean " << depVar("DISTCLEAN_DEPS") << '\n';
1036     if(!project->isEmpty("QMAKE_BUNDLE")) {
1037         QString bundlePath = escapeFilePath(destdir + project->first("QMAKE_BUNDLE"));
1038         t << "\t-$(DEL_FILE) -r " << bundlePath << Qt::endl;
1039     } else if (project->isActiveConfig("staticlib") || project->isActiveConfig("plugin")) {
1040         t << "\t-$(DEL_FILE) " << escapeFilePath(destdir) << "$(TARGET) \n";
1041     } else if (project->values("QMAKE_APP_FLAG").isEmpty()) {
1042         destdir = escapeFilePath(destdir);
1043         t << "\t-$(DEL_FILE) " << destdir << "$(TARGET) \n";
1044         if (!project->isActiveConfig("unversioned_libname")) {
1045             t << "\t-$(DEL_FILE) " << destdir << "$(TARGET0) " << destdir << "$(TARGET1) "
1046               << destdir << "$(TARGET2) $(TARGETA)\n";
1047         } else {
1048             t << "\t-$(DEL_FILE) $(TARGETA)\n";
1049         }
1050     } else {
1051         t << "\t-$(DEL_FILE) $(TARGET) \n";
1052     }
1053     t << fileVarGlue("QMAKE_DISTCLEAN","\t-$(DEL_FILE) "," ","\n");
1054     {
1055         QString ofile = fileFixify(Option::output.fileName());
1056         if(!ofile.isEmpty())
1057             t << "\t-$(DEL_FILE) " << escapeFilePath(ofile) << Qt::endl;
1058     }
1059     t << Qt::endl << Qt::endl;
1060 
1061     t << "####### Sub-libraries\n\n";
1062     if (!project->values("SUBLIBS").isEmpty()) {
1063         ProString libdir = "tmp/";
1064         if (!project->isEmpty("SUBLIBS_DIR"))
1065             libdir = project->first("SUBLIBS_DIR");
1066         const ProStringList &l = project->values("SUBLIBS");
1067         for (it = l.begin(); it != l.end(); ++it)
1068             t << escapeDependencyPath(libdir + project->first("QMAKE_PREFIX_STATICLIB") + (*it) + '.'
1069                                       + project->first("QMAKE_EXTENSION_STATICLIB")) << ":\n\t"
1070               << var(ProKey("MAKELIB" + *it)) << Qt::endl << Qt::endl;
1071     }
1072 
1073     if(doPrecompiledHeaders() && !project->isEmpty("PRECOMPILED_HEADER")) {
1074         QString pchInput = project->first("PRECOMPILED_HEADER").toQString();
1075         t << "###### Precompiled headers\n";
1076         for (const ProString &compiler : project->values("QMAKE_BUILTIN_COMPILERS")) {
1077             QString pchOutputDir;
1078             QString pchFlags = var(ProKey("QMAKE_" + compiler + "FLAGS_PRECOMPILE"));
1079             if(pchFlags.isEmpty())
1080                 continue;
1081 
1082             QString cflags;
1083             if (compiler == "C" || compiler == "OBJC")
1084                 cflags += " $(CFLAGS)";
1085             else
1086                 cflags += " $(CXXFLAGS)";
1087 
1088             ProStringList pchArchs = project->values("QMAKE_PCH_ARCHS");
1089             if (pchArchs.isEmpty())
1090                 pchArchs << ProString(); // normal single-arch PCH
1091             ProString pchBaseName = project->first("QMAKE_ORIG_TARGET");
1092             ProString pchOutput;
1093             if(!project->isEmpty("PRECOMPILED_DIR"))
1094                 pchOutput = project->first("PRECOMPILED_DIR");
1095             pchOutput += pchBaseName;
1096             pchOutput += project->first("QMAKE_PCH_OUTPUT_EXT");
1097 
1098             if (!project->isActiveConfig("icc_pch_style")) {
1099                 // gcc style (including clang_pch_style)
1100                 ProString header_prefix = project->first("QMAKE_PRECOMP_PREFIX");
1101                 ProString header_suffix = project->isActiveConfig("clang_pch_style")
1102                                   ? project->first("QMAKE_PCH_OUTPUT_EXT") : "";
1103                 pchOutput += Option::dir_sep;
1104                 pchOutputDir = pchOutput.toQString();
1105 
1106                 QString language = project->first(ProKey("QMAKE_LANGUAGE_" + compiler)).toQString();
1107                 if (language.isEmpty())
1108                     continue;
1109 
1110                 pchOutput += header_prefix + language + header_suffix;
1111             }
1112             pchFlags.replace(QLatin1String("${QMAKE_PCH_INPUT}"), escapeFilePath(pchInput))
1113                     .replace(QLatin1String("${QMAKE_PCH_OUTPUT_BASE}"), escapeFilePath(pchBaseName.toQString()));
1114             for (const ProString &arch : qAsConst(pchArchs)) {
1115                 auto pchArchOutput = pchOutput.toQString();
1116                 if (!arch.isEmpty())
1117                     pchArchOutput.replace(QStringLiteral("${QMAKE_PCH_ARCH}"), arch.toQString());
1118 
1119                 const auto pchFilePath_d = escapeDependencyPath(pchArchOutput);
1120                 if (!arch.isEmpty()) {
1121                     t << pchFilePath_d << ": " << "EXPORT_ARCH_ARGS = -arch " << arch << "\n\n";
1122                     t << pchFilePath_d << ": "
1123                       << "EXPORT_QMAKE_XARCH_CFLAGS = $(EXPORT_QMAKE_XARCH_CFLAGS_" << arch << ")" << "\n\n";
1124                     t << pchFilePath_d << ": "
1125                       << "EXPORT_QMAKE_XARCH_LFLAGS = $(EXPORT_QMAKE_XARCH_LFLAGS_" << arch << ")" << "\n\n";
1126                 }
1127                 t << pchFilePath_d << ": " << escapeDependencyPath(pchInput) << ' '
1128                   << finalizeDependencyPaths(findDependencies(pchInput)).join(" \\\n\t\t");
1129                 if (project->isActiveConfig("icc_pch_style")) {
1130                     QString sourceFile = pchArchOutput + Option::cpp_ext.first();
1131                     QString sourceFile_f = escapeFilePath(sourceFile);
1132                     QString objectFile = createObjectList(ProStringList(sourceFile)).first().toQString();
1133 
1134                     pchFlags.replace(QLatin1String("${QMAKE_PCH_TEMP_SOURCE}"), sourceFile_f)
1135                             .replace(QLatin1String("${QMAKE_PCH_TEMP_OBJECT}"), escapeFilePath(objectFile));
1136 
1137                     t << "\n\techo \"// Automatically generated, do not modify\" > " << sourceFile_f
1138                       << "\n\trm -f " << escapeFilePath(pchArchOutput);
1139                 } else {
1140                     QString outDir = pchOutputDir;
1141                     if (!arch.isEmpty())
1142                         outDir.replace(QStringLiteral("${QMAKE_PCH_ARCH}"), arch.toQString());
1143                     t << "\n\t" << mkdir_p_asstring(outDir);
1144                 }
1145 
1146                 auto pchArchFlags = pchFlags;
1147                 pchArchFlags.replace(QLatin1String("${QMAKE_PCH_OUTPUT}"), escapeFilePath(pchArchOutput));
1148 
1149                 QString compilerExecutable;
1150                 if (compiler == "C" || compiler == "OBJC")
1151                     compilerExecutable = "$(CC)";
1152                 else
1153                     compilerExecutable = "$(CXX)";
1154 
1155                 // compile command
1156                 t << "\n\t" << compilerExecutable << cflags << " $(INCPATH) " << pchArchFlags << Qt::endl << Qt::endl;
1157             }
1158         }
1159     }
1160 
1161     writeExtraTargets(t);
1162     writeExtraCompilerTargets(t);
1163 }
1164 
init2()1165 void UnixMakefileGenerator::init2()
1166 {
1167     if(project->isEmpty("QMAKE_FRAMEWORK_VERSION"))
1168         project->values("QMAKE_FRAMEWORK_VERSION").append(project->first("VER_MAJ"));
1169 
1170     if (project->first("TEMPLATE") == "aux") {
1171         project->values("PRL_TARGET") = {
1172             project->first("QMAKE_PREFIX_STATICLIB") +
1173             project->first("TARGET")
1174         };
1175     } else if (!project->values("QMAKE_APP_FLAG").isEmpty()) {
1176         if(!project->isEmpty("QMAKE_BUNDLE")) {
1177             ProString bundle_loc = project->first("QMAKE_BUNDLE_LOCATION");
1178             if(!bundle_loc.isEmpty() && !bundle_loc.startsWith("/"))
1179                 bundle_loc.prepend("/");
1180             if(!bundle_loc.endsWith("/"))
1181                 bundle_loc += "/";
1182             project->values("TARGET").first().prepend(project->first("QMAKE_BUNDLE") + bundle_loc);
1183         }
1184         if(!project->isEmpty("TARGET"))
1185             project->values("TARGET").first().prepend(project->first("DESTDIR"));
1186     } else if (project->isActiveConfig("staticlib")) {
1187         project->values("PRL_TARGET") =
1188             project->values("TARGET").first().prepend(project->first("QMAKE_PREFIX_STATICLIB"));
1189         project->values("TARGET").first() += "." + project->first("QMAKE_EXTENSION_STATICLIB");
1190         if(project->values("QMAKE_AR_CMD").isEmpty())
1191             project->values("QMAKE_AR_CMD").append("$(AR) $(DESTDIR)$(TARGET) $(OBJECTS)");
1192     } else {
1193         project->values("TARGETA").append(project->first("DESTDIR") + project->first("QMAKE_PREFIX_STATICLIB")
1194                 + project->first("TARGET") + "." + project->first("QMAKE_EXTENSION_STATICLIB"));
1195 
1196         ProStringList &ar_cmd = project->values("QMAKE_AR_CMD");
1197         if (!ar_cmd.isEmpty())
1198             ar_cmd[0] = ar_cmd.at(0).toQString().replace(QLatin1String("(TARGET)"), QLatin1String("(TARGETA)"));
1199         else
1200             ar_cmd.append("$(AR) $(TARGETA) $(OBJECTS)");
1201         if (!project->isEmpty("QMAKE_BUNDLE")) {
1202             project->values("PRL_TARGET").prepend(project->first("QMAKE_BUNDLE") +
1203                 "/Versions/" + project->first("QMAKE_FRAMEWORK_VERSION") +
1204                 "/Resources/" + project->first("TARGET"));
1205             ProString bundle_loc = project->first("QMAKE_BUNDLE_LOCATION");
1206             if(!bundle_loc.isEmpty() && !bundle_loc.startsWith("/"))
1207                 bundle_loc.prepend("/");
1208             if(!bundle_loc.endsWith("/"))
1209                 bundle_loc += "/";
1210             const QString target = project->first("QMAKE_BUNDLE") +
1211                                         bundle_loc + project->first("TARGET");
1212             project->values("TARGET_").append(target);
1213             if (!project->isActiveConfig("shallow_bundle")) {
1214                 project->values("TARGET_x.y").append(project->first("QMAKE_BUNDLE") +
1215                                                           "/Versions/" +
1216                                                           project->first("QMAKE_FRAMEWORK_VERSION") +
1217                                                           bundle_loc + project->first("TARGET"));
1218             } else {
1219                 project->values("TARGET_x.y").append(target);
1220             }
1221         } else if(project->isActiveConfig("plugin")) {
1222             QString prefix;
1223             if(!project->isActiveConfig("no_plugin_name_prefix"))
1224                 prefix = "lib";
1225             project->values("PRL_TARGET").prepend(prefix + project->first("TARGET"));
1226             project->values("TARGET_x.y.z").append(prefix +
1227                                                         project->first("TARGET") + "." +
1228                                                         project->first("QMAKE_EXTENSION_PLUGIN"));
1229             if(project->isActiveConfig("lib_version_first"))
1230                 project->values("TARGET_x").append(prefix + project->first("TARGET") + "." +
1231                                                         project->first("VER_MAJ") + "." +
1232                                                         project->first("QMAKE_EXTENSION_PLUGIN"));
1233             else
1234                 project->values("TARGET_x").append(prefix + project->first("TARGET") + "." +
1235                                                         project->first("QMAKE_EXTENSION_PLUGIN") +
1236                                                         "." + project->first("VER_MAJ"));
1237             project->values("TARGET") = project->values("TARGET_x.y.z");
1238         } else if (!project->isEmpty("QMAKE_HPUX_SHLIB")) {
1239             project->values("PRL_TARGET").prepend("lib" + project->first("TARGET"));
1240             project->values("TARGET_").append("lib" + project->first("TARGET") + ".sl");
1241             if(project->isActiveConfig("lib_version_first"))
1242                 project->values("TARGET_x").append("lib" + project->first("VER_MAJ") + "." +
1243                                                         project->first("TARGET"));
1244             else
1245                 project->values("TARGET_x").append("lib" + project->first("TARGET") + "." +
1246                                                         project->first("VER_MAJ"));
1247             project->values("TARGET") = project->values("TARGET_x");
1248         } else if (!project->isEmpty("QMAKE_AIX_SHLIB")) {
1249             project->values("PRL_TARGET").prepend("lib" + project->first("TARGET"));
1250             project->values("TARGET_").append(project->first("QMAKE_PREFIX_STATICLIB") + project->first("TARGET")
1251                     + "." + project->first("QMAKE_EXTENSION_STATICLIB"));
1252             if(project->isActiveConfig("lib_version_first")) {
1253                 project->values("TARGET_x").append("lib" + project->first("TARGET") + "." +
1254                                                         project->first("VER_MAJ") + "." +
1255                                                         project->first("QMAKE_EXTENSION_SHLIB"));
1256                 project->values("TARGET_x.y").append("lib" + project->first("TARGET") + "." +
1257                                                           project->first("VER_MAJ") +
1258                                                           "." + project->first("VER_MIN") + "." +
1259                                                           project->first("QMAKE_EXTENSION_SHLIB"));
1260                 project->values("TARGET_x.y.z").append("lib" + project->first("TARGET") + "." +
1261                                                             project->first("VER_MAJ") + "." +
1262                                                             project->first("VER_MIN") + "." +
1263                                                             project->first("VER_PAT") + "." +
1264                                                             project->first("QMAKE_EXTENSION_SHLIB"));
1265             } else {
1266                 project->values("TARGET_x").append("lib" + project->first("TARGET") + "." +
1267                                                         project->first("QMAKE_EXTENSION_SHLIB") +
1268                                                         "." + project->first("VER_MAJ"));
1269                 project->values("TARGET_x.y").append("lib" + project->first("TARGET") + "." +
1270                                                           project->first("QMAKE_EXTENSION_SHLIB") +
1271                                                           "." + project->first("VER_MAJ") +
1272                                                           "." + project->first("VER_MIN"));
1273                 project->values("TARGET_x.y.z").append("lib" + project->first("TARGET") + "." +
1274                                                             project->first("QMAKE_EXTENSION_SHLIB") + "." +
1275                                                             project->first("VER_MAJ") + "." +
1276                                                             project->first("VER_MIN") + "." +
1277                                                             project->first("VER_PAT"));
1278             }
1279             project->values("TARGET") = project->values("TARGET_x.y.z");
1280         } else {
1281             project->values("PRL_TARGET").prepend("lib" + project->first("TARGET"));
1282             project->values("TARGET_").append("lib" + project->first("TARGET") + "." +
1283                                                    project->first("QMAKE_EXTENSION_SHLIB"));
1284             if(project->isActiveConfig("lib_version_first")) {
1285                 project->values("TARGET_x").append("lib" + project->first("TARGET") + "." +
1286                                                         project->first("VER_MAJ") + "." +
1287                                                         project->first("QMAKE_EXTENSION_SHLIB"));
1288                 project->values("TARGET_x.y").append("lib" + project->first("TARGET") + "." +
1289                                                           project->first("VER_MAJ") +
1290                                                           "." + project->first("VER_MIN") + "." +
1291                                                           project->first("QMAKE_EXTENSION_SHLIB"));
1292                 project->values("TARGET_x.y.z").append("lib" + project->first("TARGET") + "." +
1293                                                             project->first("VER_MAJ") + "." +
1294                                                             project->first("VER_MIN") +  "." +
1295                                                             project->first("VER_PAT") + "." +
1296                                                             project->first("QMAKE_EXTENSION_SHLIB"));
1297             } else {
1298                 project->values("TARGET_x").append("lib" + project->first("TARGET") + "." +
1299                                                         project->first("QMAKE_EXTENSION_SHLIB") +
1300                                                         "." + project->first("VER_MAJ"));
1301                 project->values("TARGET_x.y").append("lib" + project->first("TARGET") + "." +
1302                                                       project->first("QMAKE_EXTENSION_SHLIB")
1303                                                       + "." + project->first("VER_MAJ") +
1304                                                       "." + project->first("VER_MIN"));
1305                 project->values("TARGET_x.y.z").append("lib" + project->first("TARGET") +
1306                                                             "." +
1307                                                             project->first(
1308                                                                 "QMAKE_EXTENSION_SHLIB") + "." +
1309                                                             project->first("VER_MAJ") + "." +
1310                                                             project->first("VER_MIN") +  "." +
1311                                                             project->first("VER_PAT"));
1312             }
1313             if (project->isActiveConfig("unversioned_libname"))
1314                 project->values("TARGET") = project->values("TARGET_");
1315             else
1316                 project->values("TARGET") = project->values("TARGET_x.y.z");
1317         }
1318         if (!project->values("QMAKE_LFLAGS_SONAME").isEmpty()) {
1319             ProString soname;
1320             if(project->isActiveConfig("plugin")) {
1321                 if(!project->values("TARGET").isEmpty())
1322                     soname += project->first("TARGET");
1323             } else if(!project->isEmpty("QMAKE_BUNDLE")) {
1324                 soname += project->first("TARGET_x.y");
1325             } else if(project->isActiveConfig("unversioned_soname")) {
1326                 soname = "lib" + project->first("QMAKE_ORIG_TARGET")
1327                     + "." + project->first("QMAKE_EXTENSION_SHLIB");
1328             } else if(!project->values("TARGET_x").isEmpty()) {
1329                 soname += project->first("TARGET_x");
1330             }
1331             if(!soname.isEmpty()) {
1332                 if(project->isActiveConfig("absolute_library_soname") &&
1333                    project->values("INSTALLS").indexOf("target") != -1 &&
1334                    !project->isEmpty("target.path")) {
1335                     QString instpath = Option::fixPathToTargetOS(project->first("target.path").toQString());
1336                     if(!instpath.endsWith(Option::dir_sep))
1337                         instpath += Option::dir_sep;
1338                     soname.prepend(instpath);
1339                 } else if (!project->isEmpty("QMAKE_SONAME_PREFIX")) {
1340                     QString sonameprefix = project->first("QMAKE_SONAME_PREFIX").toQString();
1341                     if (!sonameprefix.startsWith('@'))
1342                         sonameprefix = Option::fixPathToTargetOS(sonameprefix, false);
1343                     if (!sonameprefix.endsWith(Option::dir_sep))
1344                         sonameprefix += Option::dir_sep;
1345                     soname.prepend(sonameprefix);
1346                 }
1347                 project->values("QMAKE_LFLAGS_SONAME").first() += escapeFilePath(soname);
1348             }
1349         }
1350         if (project->values("QMAKE_LINK_SHLIB_CMD").isEmpty())
1351             project->values("QMAKE_LINK_SHLIB_CMD").append(
1352                 "$(LINK) $(LFLAGS) " + project->first("QMAKE_LINK_O_FLAG") + "$(TARGET) $(OBJECTS) $(LIBS) $(OBJCOMP)");
1353     }
1354     if (!project->values("QMAKE_APP_FLAG").isEmpty() || project->first("TEMPLATE") == "aux") {
1355         project->values("QMAKE_CFLAGS") += project->values("QMAKE_CFLAGS_APP");
1356         project->values("QMAKE_CXXFLAGS") += project->values("QMAKE_CXXFLAGS_APP");
1357         project->values("QMAKE_LFLAGS") += project->values("QMAKE_LFLAGS_APP");
1358     } else if (project->isActiveConfig("dll")) {
1359         if(!project->isActiveConfig("plugin") || !project->isActiveConfig("plugin_no_share_shlib_cflags")) {
1360             project->values("QMAKE_CFLAGS") += project->values("QMAKE_CFLAGS_SHLIB");
1361             project->values("QMAKE_CXXFLAGS") += project->values("QMAKE_CXXFLAGS_SHLIB");
1362         }
1363         if (project->isActiveConfig("plugin")) {
1364             project->values("QMAKE_CFLAGS") += project->values("QMAKE_CFLAGS_PLUGIN");
1365             project->values("QMAKE_CXXFLAGS") += project->values("QMAKE_CXXFLAGS_PLUGIN");
1366             project->values("QMAKE_LFLAGS") += project->values("QMAKE_LFLAGS_PLUGIN");
1367             if (project->isActiveConfig("plugin_with_soname"))
1368                 project->values("QMAKE_LFLAGS") += project->values("QMAKE_LFLAGS_SONAME");
1369         } else {
1370             project->values("QMAKE_LFLAGS") += project->values("QMAKE_LFLAGS_SHLIB");
1371             if(!project->isEmpty("QMAKE_LFLAGS_COMPAT_VERSION")) {
1372                 if(project->isEmpty("COMPAT_VERSION"))
1373                     project->values("QMAKE_LFLAGS") += QString(project->first("QMAKE_LFLAGS_COMPAT_VERSION") +
1374                                                                     project->first("VER_MAJ") + "." +
1375                                                                     project->first("VER_MIN"));
1376                 else
1377                     project->values("QMAKE_LFLAGS") += QString(project->first("QMAKE_LFLAGS_COMPAT_VERSION") +
1378                                                                     project->first("COMPATIBILITY_VERSION"));
1379             }
1380             if(!project->isEmpty("QMAKE_LFLAGS_VERSION")) {
1381                 project->values("QMAKE_LFLAGS") += QString(project->first("QMAKE_LFLAGS_VERSION") +
1382                                                                 project->first("VER_MAJ") + "." +
1383                                                                 project->first("VER_MIN") + "." +
1384                                                                 project->first("VER_PAT"));
1385             }
1386             project->values("QMAKE_LFLAGS") += project->values("QMAKE_LFLAGS_SONAME");
1387         }
1388     }
1389 
1390     if (include_deps && project->isActiveConfig("gcc_MD_depends")) {
1391         ProString MD_flag("-MD");
1392         project->values("QMAKE_CFLAGS") += MD_flag;
1393         project->values("QMAKE_CXXFLAGS") += MD_flag;
1394     }
1395 }
1396 
1397 QString
libtoolFileName(bool fixify)1398 UnixMakefileGenerator::libtoolFileName(bool fixify)
1399 {
1400     QString ret = var("TARGET");
1401     int slsh = ret.lastIndexOf(Option::dir_sep);
1402     if(slsh != -1)
1403         ret = ret.right(ret.length() - slsh - 1);
1404     int dot = ret.indexOf('.');
1405     if(dot != -1)
1406         ret = ret.left(dot);
1407     ret += Option::libtool_ext;
1408     if(!project->isEmpty("QMAKE_LIBTOOL_DESTDIR"))
1409         ret.prepend(project->first("QMAKE_LIBTOOL_DESTDIR") + Option::dir_sep);
1410     if(fixify) {
1411         if(QDir::isRelativePath(ret) && !project->isEmpty("DESTDIR"))
1412             ret.prepend(project->first("DESTDIR").toQString());
1413         ret = fileFixify(ret, FileFixifyBackwards);
1414     }
1415     return ret;
1416 }
1417 
1418 void
writeLibtoolFile()1419 UnixMakefileGenerator::writeLibtoolFile()
1420 {
1421     auto fixDependencyLibs
1422             = [this](const ProStringList &libs)
1423               {
1424                   ProStringList result;
1425                   for (auto lib : libs) {
1426                       auto fi = fileInfo(lib.toQString());
1427                       if (fi.isAbsolute()) {
1428                           const QString libDirArg = "-L" + fi.path();
1429                           if (!result.contains(libDirArg))
1430                               result += libDirArg;
1431                           QString namespec = fi.fileName();
1432                           int dotPos = namespec.lastIndexOf('.');
1433                           if (dotPos != -1 && namespec.startsWith("lib")) {
1434                               namespec.truncate(dotPos);
1435                               namespec.remove(0, 3);
1436                           } else {
1437                               debug_msg(1, "Ignoring dependency library %s",
1438                                         lib.toLatin1().constData());
1439                               continue;
1440                           }
1441                           result += "-l" + namespec;
1442                       } else {
1443                           result += lib;
1444                       }
1445                   }
1446                   return result;
1447               };
1448 
1449     QString fname = libtoolFileName(), lname = fname;
1450     debug_msg(1, "Writing libtool file %s", fname.toLatin1().constData());
1451     mkdir(fileInfo(fname).path());
1452     int slsh = lname.lastIndexOf(Option::dir_sep);
1453     if(slsh != -1)
1454         lname = lname.right(lname.length() - slsh - 1);
1455     QFile ft(fname);
1456     if(!ft.open(QIODevice::WriteOnly))
1457         return;
1458     QString ffname(fileFixify(fname));
1459     project->values("ALL_DEPS").append(ffname);
1460     project->values("QMAKE_DISTCLEAN").append(ffname);
1461 
1462     QTextStream t(&ft);
1463     t << "# " << lname << " - a libtool library file\n";
1464     t << "# Generated by qmake/libtool (" QMAKE_VERSION_STR ") (Qt "
1465       << QT_VERSION_STR << ")";
1466     t << "\n";
1467 
1468     t << "# The name that we can dlopen(3).\n"
1469       << "dlname='" << fileVar(project->isActiveConfig("plugin") ? "TARGET" : "TARGET_x")
1470       << "'\n\n";
1471 
1472     t << "# Names of this library.\n";
1473     t << "library_names='";
1474     if(project->isActiveConfig("plugin")) {
1475         t << fileVar("TARGET");
1476     } else {
1477         if (project->isEmpty("QMAKE_HPUX_SHLIB"))
1478             t << fileVar("TARGET_x.y.z") << ' ';
1479         t << fileVar("TARGET_x") << ' ' << fileVar("TARGET_");
1480     }
1481     t << "'\n\n";
1482 
1483     t << "# The name of the static archive.\n"
1484       << "old_library='" << escapeFilePath(lname.left(lname.length()-Option::libtool_ext.length()))
1485                          << ".a'\n\n";
1486 
1487     t << "# Libraries that this one depends upon.\n";
1488     static const ProKey libVars[] = { "LIBS", "QMAKE_LIBS" };
1489     ProStringList libs;
1490     for (auto var : libVars)
1491         libs += fixLibFlags(var);
1492     t << "dependency_libs='" << fixDependencyLibs(libs).join(' ') << "'\n\n";
1493 
1494     t << "# Version information for " << lname << "\n";
1495     int maj = project->first("VER_MAJ").toInt();
1496     int min = project->first("VER_MIN").toInt();
1497     int pat = project->first("VER_PAT").toInt();
1498     t << "current=" << (10*maj + min) << "\n" // best I can think of
1499       << "age=0\n"
1500       << "revision=" << pat << "\n\n";
1501 
1502     t << "# Is this an already installed library.\n"
1503         "installed=yes\n\n"; // ###
1504 
1505     t << "# Files to dlopen/dlpreopen.\n"
1506         "dlopen=''\n"
1507         "dlpreopen=''\n\n";
1508 
1509     ProString install_dir = project->first("QMAKE_LIBTOOL_LIBDIR");
1510     if(install_dir.isEmpty())
1511         install_dir = project->first("target.path");
1512     if(install_dir.isEmpty())
1513         install_dir = project->first("DESTDIR");
1514     t << "# Directory that this library needs to be installed in:\n"
1515         "libdir='" << Option::fixPathToTargetOS(install_dir.toQString(), false) << "'\n";
1516 }
1517 
writeObjectsPart(QTextStream & t,bool do_incremental)1518 std::pair<bool, QString> UnixMakefileGenerator::writeObjectsPart(QTextStream &t, bool do_incremental)
1519 {
1520     bool src_incremental = false;
1521     QString objectsLinkLine;
1522     const ProStringList &objs = project->values("OBJECTS");
1523     if (do_incremental) {
1524         const ProStringList &incrs = project->values("QMAKE_INCREMENTAL");
1525         ProStringList incrs_out;
1526         t << "OBJECTS       = ";
1527         for (ProStringList::ConstIterator objit = objs.begin(); objit != objs.end(); ++objit) {
1528             bool increment = false;
1529             for (ProStringList::ConstIterator incrit = incrs.begin(); incrit != incrs.end(); ++incrit) {
1530                 if ((*objit).toQString().indexOf(QRegExp((*incrit).toQString(), Qt::CaseSensitive,
1531                                                          QRegExp::Wildcard)) != -1) {
1532                     increment = true;
1533                     incrs_out.append((*objit));
1534                     break;
1535                 }
1536             }
1537             if (!increment)
1538                 t << "\\\n\t\t" << (*objit);
1539         }
1540         if (incrs_out.count() == objs.count()) { //we just switched places, no real incrementals to be done!
1541             t << escapeFilePaths(incrs_out).join(QString(" \\\n\t\t")) << Qt::endl;
1542         } else if (!incrs_out.count()) {
1543             t << Qt::endl;
1544         } else {
1545             src_incremental = true;
1546             t << Qt::endl;
1547             t << "INCREMENTAL_OBJECTS = "
1548               << escapeFilePaths(incrs_out).join(QString(" \\\n\t\t")) << Qt::endl;
1549         }
1550     } else {
1551         const ProString &objMax = project->first("QMAKE_LINK_OBJECT_MAX");
1552         // Used all over the place in both deps and commands.
1553         if (objMax.isEmpty() || project->values("OBJECTS").count() < objMax.toInt()) {
1554             objectsLinkLine = "$(OBJECTS)";
1555         } else {
1556             QString ld_response_file = fileVar("OBJECTS_DIR");
1557             ld_response_file += var("QMAKE_LINK_OBJECT_SCRIPT") + "." + var("QMAKE_TARGET");
1558             if (!var("BUILD_NAME").isEmpty())
1559                 ld_response_file += "." + var("BUILD_NAME");
1560             if (!var("MAKEFILE").isEmpty())
1561                 ld_response_file += "." + var("MAKEFILE");
1562             createResponseFile(ld_response_file, objs);
1563             objectsLinkLine = "@" + escapeFilePath(ld_response_file);
1564         }
1565         t << "OBJECTS       = " << valList(escapeDependencyPaths(objs)) << Qt::endl;
1566     }
1567     return std::make_pair(src_incremental, objectsLinkLine);
1568 }
1569 
1570 QT_END_NAMESPACE
1571