source: trunk/qmake/generators/makefile.cpp@ 569

Last change on this file since 569 was 566, checked in by Dmitry A. Kuminov, 15 years ago

qmake: Adopted to 4.6.1 changes.

File size: 130.8 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the qmake application of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "makefile.h"
43#include "option.h"
44#include "cachekeys.h"
45#include "meta.h"
46#include <qdir.h>
47#include <qfile.h>
48#include <qtextstream.h>
49#include <qregexp.h>
50#include <qhash.h>
51#include <qdebug.h>
52#include <qbuffer.h>
53#include <qsettings.h>
54#include <qdatetime.h>
55#if defined(Q_OS_UNIX)
56#include <unistd.h>
57#else
58#include <io.h>
59#endif
60#include <qdebug.h>
61#include <stdio.h>
62#include <stdlib.h>
63#include <time.h>
64#include <fcntl.h>
65#include <sys/types.h>
66#include <sys/stat.h>
67
68QT_BEGIN_NAMESPACE
69
70// Well, Windows doesn't have this, so here's the macro
71#ifndef S_ISDIR
72# define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
73#endif
74
75bool MakefileGenerator::canExecute(const QStringList &cmdline, int *a) const
76{
77 int argv0 = -1;
78 for(int i = 0; i < cmdline.count(); ++i) {
79 if(!cmdline.at(i).contains('=')) {
80 argv0 = i;
81 break;
82 }
83 }
84 if(a)
85 *a = argv0;
86 if(argv0 != -1) {
87 const QString c = Option::fixPathToLocalOS(cmdline.at(argv0), true);
88 if(exists(c))
89 return true;
90 }
91 return false;
92}
93
94QString MakefileGenerator::mkdir_p_asstring(const QString &dir, bool escape) const
95{
96 QString ret = "@$(CHK_DIR_EXISTS) ";
97 if(escape)
98 ret += escapeFilePath(dir);
99 else
100 ret += dir;
101 ret += " ";
102 if(isDosLikeShell())
103 ret += "$(MKDIR)";
104 else
105 ret += "|| $(MKDIR)";
106 ret += " ";
107 if(escape)
108 ret += escapeFilePath(dir);
109 else
110 ret += dir;
111 ret += " ";
112 return ret;
113}
114
115bool MakefileGenerator::mkdir(const QString &in_path) const
116{
117 QString path = Option::fixPathToLocalOS(in_path);
118 if(QFile::exists(path))
119 return true;
120
121 QDir d;
122 if(path.startsWith(QDir::separator())) {
123 d.cd(QString(QDir::separator()));
124 path.remove(0, 1);
125 }
126 bool ret = true;
127#if defined(Q_OS_WIN) || defined(Q_OS_OS2)
128 bool driveExists = true;
129 if(!QDir::isRelativePath(path)) {
130 if(QFile::exists(path.left(3))) {
131 d.cd(path.left(3));
132 path.remove(0, 3);
133 } else {
134 warn_msg(WarnLogic, "Cannot access drive '%s' (%s)",
135 path.left(3).toLatin1().data(), path.toLatin1().data());
136 driveExists = false;
137 }
138 }
139 if(driveExists)
140#endif
141 {
142 QStringList subs = path.split(QDir::separator());
143 for(QStringList::Iterator subit = subs.begin(); subit != subs.end(); ++subit) {
144 if(!d.cd(*subit)) {
145 d.mkdir((*subit));
146 if(d.exists((*subit))) {
147 d.cd((*subit));
148 } else {
149 ret = false;
150 break;
151 }
152 }
153 }
154 }
155 return ret;
156}
157
158// ** base makefile generator
159MakefileGenerator::MakefileGenerator() :
160 init_opath_already(false), init_already(false), no_io(false), project(0)
161{
162}
163
164
165void
166MakefileGenerator::verifyCompilers()
167{
168 QMap<QString, QStringList> &v = project->variables();
169 QStringList &quc = v["QMAKE_EXTRA_COMPILERS"];
170 for(int i = 0; i < quc.size(); ) {
171 bool error = false;
172 QString comp = quc.at(i);
173 if(v[comp + ".output"].isEmpty()) {
174 if(!v[comp + ".output_function"].isEmpty()) {
175 v[comp + ".output"].append("${QMAKE_FUNC_FILE_IN_" + v[comp + ".output_function"].first() + "}");
176 } else {
177 error = true;
178 warn_msg(WarnLogic, "Compiler: %s: No output file specified", comp.toLatin1().constData());
179 }
180 } else if(v[comp + ".input"].isEmpty()) {
181 error = true;
182 warn_msg(WarnLogic, "Compiler: %s: No input variable specified", comp.toLatin1().constData());
183 }
184 if(error)
185 quc.removeAt(i);
186 else
187 ++i;
188 }
189}
190
191void
192MakefileGenerator::initOutPaths()
193{
194 if(init_opath_already)
195 return;
196 verifyCompilers();
197 init_opath_already = true;
198 QMap<QString, QStringList> &v = project->variables();
199 //for shadow builds
200 if(!v.contains("QMAKE_ABSOLUTE_SOURCE_PATH")) {
201 if(Option::mkfile::do_cache && !Option::mkfile::cachefile.isEmpty() &&
202 v.contains("QMAKE_ABSOLUTE_SOURCE_ROOT")) {
203 QString root = v["QMAKE_ABSOLUTE_SOURCE_ROOT"].first();
204 root = QDir::fromNativeSeparators(root);
205 if(!root.isEmpty()) {
206 QFileInfo fi = fileInfo(Option::mkfile::cachefile);
207 if(!fi.makeAbsolute()) {
208 QString cache_r = fi.path(), pwd = Option::output_dir;
209 if(pwd.startsWith(cache_r) && !pwd.startsWith(root)) {
210 pwd = root + pwd.mid(cache_r.length());
211 if(exists(pwd))
212 v.insert("QMAKE_ABSOLUTE_SOURCE_PATH", QStringList(pwd));
213 }
214 }
215 }
216 }
217 }
218 if(!v["QMAKE_ABSOLUTE_SOURCE_PATH"].isEmpty()) {
219 QString &asp = v["QMAKE_ABSOLUTE_SOURCE_PATH"].first();
220 asp = QDir::fromNativeSeparators(asp);
221 if(asp.isEmpty() || asp == Option::output_dir) //if they're the same, why bother?
222 v["QMAKE_ABSOLUTE_SOURCE_PATH"].clear();
223 }
224
225 QString currentDir = qmake_getpwd(); //just to go back to
226
227 //some builtin directories
228 if(project->isEmpty("PRECOMPILED_DIR") && !project->isEmpty("OBJECTS_DIR"))
229 v["PRECOMPILED_DIR"] = v["OBJECTS_DIR"];
230 QString dirs[] = { QString("OBJECTS_DIR"), QString("DESTDIR"), QString("QMAKE_PKGCONFIG_DESTDIR"),
231 QString("SUBLIBS_DIR"), QString("DLLDESTDIR"), QString("QMAKE_LIBTOOL_DESTDIR"),
232 QString("PRECOMPILED_DIR"), QString() };
233 for(int x = 0; !dirs[x].isEmpty(); x++) {
234 if(v[dirs[x]].isEmpty())
235 continue;
236 const QString orig_path = v[dirs[x]].first();
237
238 QString &pathRef = v[dirs[x]].first();
239 pathRef = fileFixify(pathRef, Option::output_dir, Option::output_dir);
240
241#ifdef Q_OS_WIN
242 // We don't want to add a separator for DLLDESTDIR on Windows (###why?)
243 if(!(dirs[x] == "DLLDESTDIR"))
244#endif
245 {
246 if(!pathRef.endsWith(Option::dir_sep))
247 pathRef += Option::dir_sep;
248 }
249
250 if(noIO())
251 continue;
252
253 QString path = project->first(dirs[x]); //not to be changed any further
254 path = fileFixify(path, currentDir, Option::output_dir);
255 debug_msg(3, "Fixed output_dir %s (%s) into %s", dirs[x].toLatin1().constData(),
256 orig_path.toLatin1().constData(), path.toLatin1().constData());
257 if(!mkdir(path))
258 warn_msg(WarnLogic, "%s: Cannot access directory '%s'", dirs[x].toLatin1().constData(),
259 path.toLatin1().constData());
260 }
261
262 //out paths from the extra compilers
263 const QStringList &quc = project->values("QMAKE_EXTRA_COMPILERS");
264 for(QStringList::ConstIterator it = quc.begin(); it != quc.end(); ++it) {
265 QString tmp_out = project->values((*it) + ".output").first();
266 if(tmp_out.isEmpty())
267 continue;
268 const QStringList &tmp = project->values((*it) + ".input");
269 for(QStringList::ConstIterator it2 = tmp.begin(); it2 != tmp.end(); ++it2) {
270 QStringList &inputs = project->values((*it2));
271 for(QStringList::Iterator input = inputs.begin(); input != inputs.end(); ++input) {
272 (*input) = fileFixify((*input), Option::output_dir, Option::output_dir);
273 QString path = unescapeFilePath(replaceExtraCompilerVariables(tmp_out, (*input), QString()));
274 path = Option::fixPathToTargetOS(path);
275 int slash = path.lastIndexOf(Option::dir_sep);
276 if(slash != -1) {
277 path = path.left(slash);
278 // Make out path only if it does not contain makefile variables
279 if(!path.contains("${"))
280 if(path != "." &&
281 !mkdir(fileFixify(path, qmake_getpwd(), Option::output_dir)))
282 warn_msg(WarnLogic, "%s: Cannot access directory '%s'",
283 (*it).toLatin1().constData(), path.toLatin1().constData());
284 }
285 }
286 }
287 }
288
289 if(!v["DESTDIR"].isEmpty()) {
290 QDir d(v["DESTDIR"].first());
291 if(Option::fixPathToLocalOS(d.absolutePath()) == Option::fixPathToLocalOS(Option::output_dir))
292 v.remove("DESTDIR");
293 }
294}
295
296QMakeProject
297*MakefileGenerator::projectFile() const
298{
299 return project;
300}
301
302void
303MakefileGenerator::setProjectFile(QMakeProject *p)
304{
305 if(project)
306 return;
307 project = p;
308 init();
309 usePlatformDir();
310 findLibraries();
311 if(Option::qmake_mode == Option::QMAKE_GENERATE_MAKEFILE &&
312 project->isActiveConfig("link_prl")) //load up prl's'
313 processPrlFiles();
314}
315
316QStringList
317MakefileGenerator::findFilesInVPATH(QStringList l, uchar flags, const QString &vpath_var)
318{
319 QStringList vpath;
320 QMap<QString, QStringList> &v = project->variables();
321 for(int val_it = 0; val_it < l.count(); ) {
322 bool remove_file = false;
323 QString &val = l[val_it];
324 if(!val.isEmpty()) {
325 QString file = fixEnvVariables(val);
326 if(!(flags & VPATH_NoFixify))
327 file = fileFixify(file, qmake_getpwd(), Option::output_dir);
328 if (file.at(0) == '\"' && file.at(file.length() - 1) == '\"')
329 file = file.mid(1, file.length() - 2);
330
331 if(exists(file)) {
332 ++val_it;
333 continue;
334 }
335 bool found = false;
336 if(QDir::isRelativePath(val)) {
337 if(vpath.isEmpty()) {
338 if(!vpath_var.isEmpty())
339 vpath = v[vpath_var];
340 vpath += v["VPATH"] + v["QMAKE_ABSOLUTE_SOURCE_PATH"] + v["DEPENDPATH"];
341 if(Option::output_dir != qmake_getpwd())
342 vpath += Option::output_dir;
343 }
344 for(QStringList::Iterator vpath_it = vpath.begin();
345 vpath_it != vpath.end(); ++vpath_it) {
346 QString real_dir = Option::fixPathToLocalOS((*vpath_it));
347 if(exists(real_dir + QDir::separator() + val)) {
348 QString dir = (*vpath_it);
349 if(!dir.endsWith(Option::dir_sep))
350 dir += Option::dir_sep;
351 val = dir + val;
352 if(!(flags & VPATH_NoFixify))
353 val = fileFixify(val);
354 found = true;
355 debug_msg(1, "Found file through vpath %s -> %s",
356 file.toLatin1().constData(), val.toLatin1().constData());
357 break;
358 }
359 }
360 }
361 if(!found) {
362 QString dir, regex = val, real_dir;
363 if(regex.lastIndexOf(Option::dir_sep) != -1) {
364 dir = regex.left(regex.lastIndexOf(Option::dir_sep) + 1);
365 real_dir = dir;
366 if(!(flags & VPATH_NoFixify))
367 real_dir = fileFixify(real_dir, qmake_getpwd(), Option::output_dir);
368 regex.remove(0, dir.length());
369 }
370 if(real_dir.isEmpty() || exists(real_dir)) {
371 QStringList files = QDir(real_dir).entryList(QStringList(regex));
372 if(files.isEmpty()) {
373 debug_msg(1, "%s:%d Failure to find %s in vpath (%s)",
374 __FILE__, __LINE__,
375 val.toLatin1().constData(), vpath.join("::").toLatin1().constData());
376 if(flags & VPATH_RemoveMissingFiles)
377 remove_file = true;
378 else if(flags & VPATH_WarnMissingFiles)
379 warn_msg(WarnLogic, "Failure to find: %s", val.toLatin1().constData());
380 } else {
381 l.removeAt(val_it);
382 QString a;
383 for(int i = (int)files.count()-1; i >= 0; i--) {
384 if(files[i] == "." || files[i] == "..")
385 continue;
386 a = dir + files[i];
387 if(!(flags & VPATH_NoFixify))
388 a = fileFixify(a);
389 l.insert(val_it, a);
390 }
391 }
392 } else {
393 debug_msg(1, "%s:%d Cannot match %s%c%s, as %s does not exist.",
394 __FILE__, __LINE__, real_dir.toLatin1().constData(),
395 QDir::separator().toLatin1(),
396 regex.toLatin1().constData(), real_dir.toLatin1().constData());
397 if(flags & VPATH_RemoveMissingFiles)
398 remove_file = true;
399 else if(flags & VPATH_WarnMissingFiles)
400 warn_msg(WarnLogic, "Failure to find: %s", val.toLatin1().constData());
401 }
402 }
403 }
404 if(remove_file)
405 l.removeAt(val_it);
406 else
407 ++val_it;
408 }
409 return l;
410}
411
412void
413MakefileGenerator::initCompiler(const MakefileGenerator::Compiler &comp)
414{
415 QMap<QString, QStringList> &v = project->variables();
416 QStringList &l = v[comp.variable_in];
417 // find all the relevant file inputs
418 if(!init_compiler_already.contains(comp.variable_in)) {
419 init_compiler_already.insert(comp.variable_in, true);
420 if(!noIO())
421 l = findFilesInVPATH(l, (comp.flags & Compiler::CompilerRemoveNoExist) ?
422 VPATH_RemoveMissingFiles : VPATH_WarnMissingFiles, "VPATH_" + comp.variable_in);
423 }
424}
425
426void
427MakefileGenerator::init()
428{
429 initOutPaths();
430 if(init_already)
431 return;
432 verifyCompilers();
433 init_already = true;
434
435 QMap<QString, QStringList> &v = project->variables();
436 QStringList &quc = v["QMAKE_EXTRA_COMPILERS"];
437
438 //make sure the COMPILERS are in the correct input/output chain order
439 for(int comp_out = 0, jump_count = 0; comp_out < quc.size(); ++comp_out) {
440 continue_compiler_chain:
441 if(jump_count > quc.size()) //just to avoid an infinite loop here
442 break;
443 if(project->variables().contains(quc.at(comp_out) + ".variable_out")) {
444 const QStringList &outputs = project->variables().value(quc.at(comp_out) + ".variable_out");
445 for(int out = 0; out < outputs.size(); ++out) {
446 for(int comp_in = 0; comp_in < quc.size(); ++comp_in) {
447 if(comp_in == comp_out)
448 continue;
449 if(project->variables().contains(quc.at(comp_in) + ".input")) {
450 const QStringList &inputs = project->variables().value(quc.at(comp_in) + ".input");
451 for(int in = 0; in < inputs.size(); ++in) {
452 if(inputs.at(in) == outputs.at(out) && comp_out > comp_in) {
453 ++jump_count;
454 //move comp_out to comp_in and continue the compiler chain
455 quc.move(comp_out, comp_in);
456 comp_out = comp_in;
457 goto continue_compiler_chain;
458 }
459 }
460 }
461 }
462 }
463 }
464 }
465
466 if(!project->isEmpty("QMAKE_SUBSTITUTES")) {
467 const QStringList &subs = v["QMAKE_SUBSTITUTES"];
468 for(int i = 0; i < subs.size(); ++i) {
469 if(!subs.at(i).endsWith(".in")) {
470 warn_msg(WarnLogic, "Substitute '%s' does not end with '.in'",
471 subs.at(i).toLatin1().constData());
472 continue;
473 }
474 QFile in(fileFixify(subs.at(i))), out(fileInfo(subs.at(i)).fileName());
475 if(out.fileName().endsWith(".in"))
476 out.setFileName(out.fileName().left(out.fileName().length()-3));
477 if(in.open(QFile::ReadOnly)) {
478 QString contents;
479 QStack<int> state;
480 enum { IN_CONDITION, MET_CONDITION, PENDING_CONDITION };
481 for(int count = 1; !in.atEnd(); ++count) {
482 QString line = QString::fromUtf8(in.readLine());
483 if(line.startsWith("!!IF ")) {
484 if(state.isEmpty() || state.top() == IN_CONDITION) {
485 QString test = line.mid(5, line.length()-(5+1));
486 if(project->test(test))
487 state.push(IN_CONDITION);
488 else
489 state.push(PENDING_CONDITION);
490 } else {
491 state.push(MET_CONDITION);
492 }
493 } else if(line.startsWith("!!ELIF ")) {
494 if(state.isEmpty()) {
495 warn_msg(WarnLogic, "(%s:%d): Unexpected else condition",
496 in.fileName().toLatin1().constData(), count);
497 } else if(state.top() == PENDING_CONDITION) {
498 QString test = line.mid(7, line.length()-(7+1));
499 if(project->test(test)) {
500 state.pop();
501 state.push(IN_CONDITION);
502 }
503 } else if(state.top() == IN_CONDITION) {
504 state.pop();
505 state.push(MET_CONDITION);
506 }
507 } else if(line.startsWith("!!ELSE")) {
508 if(state.isEmpty()) {
509 warn_msg(WarnLogic, "(%s:%d): Unexpected else condition",
510 in.fileName().toLatin1().constData(), count);
511 } else if(state.top() == PENDING_CONDITION) {
512 state.pop();
513 state.push(IN_CONDITION);
514 } else if(state.top() == IN_CONDITION) {
515 state.pop();
516 state.push(MET_CONDITION);
517 }
518 } else if(line.startsWith("!!ENDIF")) {
519 if(state.isEmpty())
520 warn_msg(WarnLogic, "(%s:%d): Unexpected endif",
521 in.fileName().toLatin1().constData(), count);
522 else
523 state.pop();
524 } else if(state.isEmpty() || state.top() == IN_CONDITION) {
525 contents += project->expand(line).join(QString(Option::field_sep));
526 }
527 }
528 if(out.exists() && out.open(QFile::ReadOnly)) {
529 QString old = QString::fromUtf8(out.readAll());
530 if(contents == old) {
531 v["QMAKE_INTERNAL_INCLUDED_FILES"].append(subs.at(i));
532 continue;
533 }
534 out.close();
535 if(!out.remove()) {
536 warn_msg(WarnLogic, "Cannot clear substitute '%s'",
537 out.fileName().toLatin1().constData());
538 continue;
539 }
540 }
541 if(out.open(QFile::WriteOnly)) {
542 v["QMAKE_INTERNAL_INCLUDED_FILES"].append(subs.at(i));
543 out.write(contents.toUtf8());
544 } else {
545 warn_msg(WarnLogic, "Cannot open substitute for output '%s'",
546 out.fileName().toLatin1().constData());
547 }
548 } else {
549 warn_msg(WarnLogic, "Cannot open substitute for input '%s'",
550 in.fileName().toLatin1().constData());
551 }
552 }
553 }
554
555 int x;
556
557 //build up a list of compilers
558 QList<Compiler> compilers;
559 {
560 const char *builtins[] = { "OBJECTS", "SOURCES", "PRECOMPILED_HEADER", 0 };
561 for(x = 0; builtins[x]; ++x) {
562 Compiler compiler;
563 compiler.variable_in = builtins[x];
564 compiler.flags = Compiler::CompilerBuiltin;
565 compiler.type = QMakeSourceFileInfo::TYPE_C;
566 if(!strcmp(builtins[x], "OBJECTS"))
567 compiler.flags |= Compiler::CompilerNoCheckDeps;
568 compilers.append(compiler);
569 }
570 for(QStringList::ConstIterator it = quc.begin(); it != quc.end(); ++it) {
571 const QStringList &inputs = v[(*it) + ".input"];
572 for(x = 0; x < inputs.size(); ++x) {
573 Compiler compiler;
574 compiler.variable_in = inputs.at(x);
575 compiler.flags = Compiler::CompilerNoFlags;
576 if(v[(*it) + ".CONFIG"].indexOf("ignore_no_exist") != -1)
577 compiler.flags |= Compiler::CompilerRemoveNoExist;
578 if(v[(*it) + ".CONFIG"].indexOf("no_dependencies") != -1)
579 compiler.flags |= Compiler::CompilerNoCheckDeps;
580
581 QString dep_type;
582 if(!project->isEmpty((*it) + ".dependency_type"))
583 dep_type = project->first((*it) + ".dependency_type");
584 if (dep_type.isEmpty())
585 compiler.type = QMakeSourceFileInfo::TYPE_UNKNOWN;
586 else if(dep_type == "TYPE_UI")
587 compiler.type = QMakeSourceFileInfo::TYPE_UI;
588 else
589 compiler.type = QMakeSourceFileInfo::TYPE_C;
590 compilers.append(compiler);
591 }
592 }
593 }
594 { //do the path fixifying
595 QStringList paths;
596 for(x = 0; x < compilers.count(); ++x) {
597 if(!paths.contains(compilers.at(x).variable_in))
598 paths << compilers.at(x).variable_in;
599 }
600 paths << "INCLUDEPATH" << "QMAKE_INTERNAL_INCLUDED_FILES" << "PRECOMPILED_HEADER";
601 for(int y = 0; y < paths.count(); y++) {
602 QStringList &l = v[paths[y]];
603 for(QStringList::Iterator it = l.begin(); it != l.end(); ++it) {
604 if((*it).isEmpty())
605 continue;
606 if(exists((*it)))
607 (*it) = fileFixify((*it));
608 }
609 }
610 }
611
612 if(noIO() || !doDepends())
613 QMakeSourceFileInfo::setDependencyMode(QMakeSourceFileInfo::NonRecursive);
614 for(x = 0; x < compilers.count(); ++x)
615 initCompiler(compilers.at(x));
616
617 //merge actual compiler outputs into their variable_out. This is done last so that
618 //files are already properly fixified.
619 for(QStringList::Iterator it = quc.begin(); it != quc.end(); ++it) {
620 QString tmp_out = project->values((*it) + ".output").first();
621 if(tmp_out.isEmpty())
622 continue;
623 if(project->values((*it) + ".CONFIG").indexOf("combine") != -1) {
624 QStringList &compilerInputs = project->values((*it) + ".input");
625 // Don't generate compiler output if it doesn't have input.
626 if (compilerInputs.isEmpty() || project->values(compilerInputs.first()).isEmpty())
627 continue;
628 if(tmp_out.indexOf("$") == -1) {
629 if(!verifyExtraCompiler((*it), QString())) //verify
630 continue;
631 QString out = fileFixify(tmp_out, Option::output_dir, Option::output_dir);
632 bool pre_dep = (project->values((*it) + ".CONFIG").indexOf("target_predeps") != -1);
633 if(project->variables().contains((*it) + ".variable_out")) {
634 const QStringList &var_out = project->variables().value((*it) + ".variable_out");
635 for(int i = 0; i < var_out.size(); ++i) {
636 QString v = var_out.at(i);
637 if(v == QLatin1String("SOURCES"))
638 v = "GENERATED_SOURCES";
639 else if(v == QLatin1String("OBJECTS"))
640 pre_dep = false;
641 QStringList &list = project->values(v);
642 if(!list.contains(out))
643 list.append(out);
644 }
645 } else if(project->values((*it) + ".CONFIG").indexOf("no_link") == -1) {
646 QStringList &list = project->values("OBJECTS");
647 pre_dep = false;
648 if(!list.contains(out))
649 list.append(out);
650 } else {
651 QStringList &list = project->values("UNUSED_SOURCES");
652 if(!list.contains(out))
653 list.append(out);
654 }
655 if(pre_dep) {
656 QStringList &list = project->variables()["PRE_TARGETDEPS"];
657 if(!list.contains(out))
658 list.append(out);
659 }
660 }
661 } else {
662 QStringList &tmp = project->values((*it) + ".input");
663 for(QStringList::Iterator it2 = tmp.begin(); it2 != tmp.end(); ++it2) {
664 const QStringList inputs = project->values((*it2));
665 for(QStringList::ConstIterator input = inputs.constBegin(); input != inputs.constEnd(); ++input) {
666 if((*input).isEmpty())
667 continue;
668 QString in = Option::fixPathToTargetOS((*input), false);
669 if(!verifyExtraCompiler((*it), in)) //verify
670 continue;
671 QString out = replaceExtraCompilerVariables(tmp_out, (*input), QString());
672 out = fileFixify(out, Option::output_dir, Option::output_dir);
673 bool pre_dep = (project->values((*it) + ".CONFIG").indexOf("target_predeps") != -1);
674 if(project->variables().contains((*it) + ".variable_out")) {
675 const QStringList &var_out = project->variables().value((*it) + ".variable_out");
676 for(int i = 0; i < var_out.size(); ++i) {
677 QString v = var_out.at(i);
678 if(v == QLatin1String("SOURCES"))
679 v = "GENERATED_SOURCES";
680 else if(v == QLatin1String("OBJECTS"))
681 pre_dep = false;
682 QStringList &list = project->values(v);
683 if(!list.contains(out))
684 list.append(out);
685 }
686 } else if(project->values((*it) + ".CONFIG").indexOf("no_link") == -1) {
687 pre_dep = false;
688 QStringList &list = project->values("OBJECTS");
689 if(!list.contains(out))
690 list.append(out);
691 } else {
692 QStringList &list = project->values("UNUSED_SOURCES");
693 if(!list.contains(out))
694 list.append(out);
695 }
696 if(pre_dep) {
697 QStringList &list = project->variables()["PRE_TARGETDEPS"];
698 if(!list.contains(out))
699 list.append(out);
700 }
701 }
702 }
703 }
704 }
705
706 //handle dependencies
707 depHeuristicsCache.clear();
708 if(!noIO()) {
709 // dependency paths
710 QStringList incDirs = v["DEPENDPATH"] + v["QMAKE_ABSOLUTE_SOURCE_PATH"];
711 if(project->isActiveConfig("depend_includepath"))
712 incDirs += v["INCLUDEPATH"];
713 if(!project->isActiveConfig("no_include_pwd")) {
714 QString pwd = qmake_getpwd();
715 if(pwd.isEmpty())
716 pwd = ".";
717 incDirs += pwd;
718 }
719 QList<QMakeLocalFileName> deplist;
720 for(QStringList::Iterator it = incDirs.begin(); it != incDirs.end(); ++it)
721 deplist.append(QMakeLocalFileName(unescapeFilePath((*it))));
722 QMakeSourceFileInfo::setDependencyPaths(deplist);
723 debug_msg(1, "Dependency Directories: %s", incDirs.join(" :: ").toLatin1().constData());
724 //cache info
725 if(project->isActiveConfig("qmake_cache")) {
726 QString cache_file;
727 if(!project->isEmpty("QMAKE_INTERNAL_CACHE_FILE")) {
728 cache_file = QDir::fromNativeSeparators(project->first("QMAKE_INTERNAL_CACHE_FILE"));
729 } else {
730 cache_file = ".qmake.internal.cache";
731 if(project->isActiveConfig("build_pass"))
732 cache_file += ".BUILD." + project->first("BUILD_PASS");
733 }
734 if(cache_file.indexOf('/') == -1)
735 cache_file.prepend(Option::output_dir + '/');
736 QMakeSourceFileInfo::setCacheFile(cache_file);
737 }
738
739 //add to dependency engine
740 for(x = 0; x < compilers.count(); ++x) {
741 const MakefileGenerator::Compiler &comp = compilers.at(x);
742 if(!(comp.flags & Compiler::CompilerNoCheckDeps))
743 addSourceFiles(v[comp.variable_in], QMakeSourceFileInfo::SEEK_DEPS,
744 (QMakeSourceFileInfo::SourceFileType)comp.type);
745 }
746 }
747
748 processSources(); //remove anything in SOURCES which is included (thus it need not be linked in)
749
750 //all sources and generated sources must be turned into objects at some point (the one builtin compiler)
751 v["OBJECTS"] += createObjectList(v["SOURCES"]) + createObjectList(v["GENERATED_SOURCES"]);
752
753 //Translation files
754 if(!project->isEmpty("TRANSLATIONS")) {
755 QStringList &trf = project->values("TRANSLATIONS");
756 for(QStringList::Iterator it = trf.begin(); it != trf.end(); ++it)
757 (*it) = Option::fixPathToLocalOS((*it));
758 }
759
760 { //get the output_dir into the pwd
761 if(fileFixify(Option::output_dir) != fileFixify(qmake_getpwd()))
762 project->values("INCLUDEPATH").append(fileFixify(Option::output_dir,
763 Option::output_dir,
764 Option::output_dir));
765 }
766
767 //fix up the target deps
768 QString fixpaths[] = { QString("PRE_TARGETDEPS"), QString("POST_TARGETDEPS"), QString() };
769 for(int path = 0; !fixpaths[path].isNull(); path++) {
770 QStringList &l = v[fixpaths[path]];
771 for(QStringList::Iterator val_it = l.begin(); val_it != l.end(); ++val_it) {
772 if(!(*val_it).isEmpty())
773 (*val_it) = escapeDependencyPath(Option::fixPathToTargetOS((*val_it), false, false));
774 }
775 }
776
777 //extra depends
778 if(!project->isEmpty("DEPENDS")) {
779 QStringList &l = v["DEPENDS"];
780 for(QStringList::Iterator it = l.begin(); it != l.end(); ++it) {
781 QStringList files = v[(*it) + ".file"] + v[(*it) + ".files"]; //why do I support such evil things?
782 for(QStringList::Iterator file_it = files.begin(); file_it != files.end(); ++file_it) {
783 QStringList &out_deps = findDependencies(*file_it);
784 QStringList &in_deps = v[(*it) + ".depends"]; //even more evilness..
785 for(QStringList::Iterator dep_it = in_deps.begin(); dep_it != in_deps.end(); ++dep_it) {
786 if(exists(*dep_it)) {
787 out_deps.append(*dep_it);
788 } else {
789 QString dir, regex = Option::fixPathToLocalOS((*dep_it));
790 if(regex.lastIndexOf(Option::dir_sep) != -1) {
791 dir = regex.left(regex.lastIndexOf(Option::dir_sep) + 1);
792 regex.remove(0, dir.length());
793 }
794 QStringList files = QDir(dir).entryList(QStringList(regex));
795 if(files.isEmpty()) {
796 warn_msg(WarnLogic, "Dependency for [%s]: Not found %s", (*file_it).toLatin1().constData(),
797 (*dep_it).toLatin1().constData());
798 } else {
799 for(int i = 0; i < files.count(); i++)
800 out_deps.append(dir + files[i]);
801 }
802 }
803 }
804 }
805 }
806 }
807
808 // escape qmake command
809 if (!project->isEmpty("QMAKE_QMAKE")) {
810 project->values("QMAKE_QMAKE") = escapeFilePaths(project->values("QMAKE_QMAKE"));
811 }
812}
813
814bool
815MakefileGenerator::processPrlFile(QString &file)
816{
817 bool ret = false, try_replace_file=false;
818 QString meta_file, orig_file = file;
819 if(QMakeMetaInfo::libExists(file)) {
820 try_replace_file = true;
821 meta_file = file;
822 file = "";
823 } else {
824 QString tmp = file;
825 int ext = tmp.lastIndexOf('.');
826 if(ext != -1)
827 tmp = tmp.left(ext);
828 meta_file = tmp;
829 }
830// meta_file = fileFixify(meta_file);
831 QString real_meta_file = Option::fixPathToLocalOS(meta_file);
832 if(!meta_file.isEmpty()) {
833 QString f = fileFixify(real_meta_file, qmake_getpwd(), Option::output_dir);
834 if(QMakeMetaInfo::libExists(f)) {
835 QMakeMetaInfo libinfo;
836 debug_msg(1, "Processing PRL file: %s", real_meta_file.toLatin1().constData());
837 if(!libinfo.readLib(f)) {
838 fprintf(stderr, "Error processing meta file: %s\n", real_meta_file.toLatin1().constData());
839 } else if(project->isActiveConfig("no_read_prl_" + libinfo.type().toLower())) {
840 debug_msg(2, "Ignored meta file %s [%s]", real_meta_file.toLatin1().constData(), libinfo.type().toLatin1().constData());
841 } else {
842 ret = true;
843 QMap<QString, QStringList> &vars = libinfo.variables();
844 for(QMap<QString, QStringList>::Iterator it = vars.begin(); it != vars.end(); ++it)
845 processPrlVariable(it.key(), it.value());
846 if(try_replace_file && !libinfo.isEmpty("QMAKE_PRL_TARGET")) {
847 QString dir;
848 int slsh = real_meta_file.lastIndexOf(Option::dir_sep);
849 if(slsh != -1)
850 dir = real_meta_file.left(slsh+1);
851 file = libinfo.first("QMAKE_PRL_TARGET");
852 if(QDir::isRelativePath(file))
853 file.prepend(dir);
854 }
855 }
856 }
857 if(ret) {
858 QString mf = QMakeMetaInfo::findLib(meta_file);
859 if(project->values("QMAKE_PRL_INTERNAL_FILES").indexOf(mf) == -1)
860 project->values("QMAKE_PRL_INTERNAL_FILES").append(mf);
861 if(project->values("QMAKE_INTERNAL_INCLUDED_FILES").indexOf(mf) == -1)
862 project->values("QMAKE_INTERNAL_INCLUDED_FILES").append(mf);
863 }
864 }
865 if(try_replace_file && file.isEmpty()) {
866#if 0
867 warn_msg(WarnLogic, "Found prl [%s] file with no target [%s]!", meta_file.toLatin1().constData(),
868 orig_file.toLatin1().constData());
869#endif
870 file = orig_file;
871 }
872 return ret;
873}
874
875void
876MakefileGenerator::filterIncludedFiles(const QString &var)
877{
878 QStringList &inputs = project->values(var);
879 for(QStringList::Iterator input = inputs.begin(); input != inputs.end(); ) {
880 if(QMakeSourceFileInfo::included((*input)) > 0)
881 input = inputs.erase(input);
882 else
883 ++input;
884 }
885}
886
887void
888MakefileGenerator::processPrlVariable(const QString &var, const QStringList &l)
889{
890 if(var == "QMAKE_PRL_LIBS") {
891 QString where = "QMAKE_LIBS";
892 if(!project->isEmpty("QMAKE_INTERNAL_PRL_LIBS"))
893 where = project->first("QMAKE_INTERNAL_PRL_LIBS");
894 QStringList &out = project->values(where);
895 for(QStringList::ConstIterator it = l.begin(); it != l.end(); ++it) {
896 if(out.indexOf((*it)) == -1)
897 out.append((*it));
898 }
899 } else if(var == "QMAKE_PRL_DEFINES") {
900 QStringList &out = project->values("DEFINES");
901 for(QStringList::ConstIterator it = l.begin(); it != l.end(); ++it) {
902 if(out.indexOf((*it)) == -1 &&
903 project->values("PRL_EXPORT_DEFINES").indexOf((*it)) == -1)
904 out.append((*it));
905 }
906 }
907}
908
909void
910MakefileGenerator::processPrlFiles()
911{
912 QHash<QString, bool> processed;
913 for(bool ret = false; true; ret = false) {
914 //read in any prl files included..
915 QStringList l_out;
916 QString where = "QMAKE_LIBS";
917 if(!project->isEmpty("QMAKE_INTERNAL_PRL_LIBS"))
918 where = project->first("QMAKE_INTERNAL_PRL_LIBS");
919 QStringList &l = project->values(where);
920 for(QStringList::Iterator it = l.begin(); it != l.end(); ++it) {
921 QString file = (*it);
922 if(!processed.contains(file) && processPrlFile(file)) {
923 processed.insert(file, true);
924 ret = true;
925 }
926 if(!file.isEmpty())
927 l_out.append(file);
928 }
929 if(ret)
930 l = l_out;
931 else
932 break;
933 }
934}
935
936void
937MakefileGenerator::writePrlFile(QTextStream &t)
938{
939 QString target = project->first("TARGET");
940 int slsh = target.lastIndexOf(Option::dir_sep);
941 if(slsh != -1)
942 target.remove(0, slsh + 1);
943 QString bdir = Option::output_dir;
944 if(bdir.isEmpty())
945 bdir = qmake_getpwd();
946 t << "QMAKE_PRL_BUILD_DIR = " << bdir << endl;
947
948 if(!project->projectFile().isEmpty() && project->projectFile() != "-")
949 t << "QMAKE_PRO_INPUT = " << project->projectFile().section('/', -1) << endl;
950
951 if(!project->isEmpty("QMAKE_ABSOLUTE_SOURCE_PATH"))
952 t << "QMAKE_PRL_SOURCE_DIR = " << project->first("QMAKE_ABSOLUTE_SOURCE_PATH") << endl;
953 if(!project->isEmpty("TARGET_SHORT"))
954 t << "QMAKE_PRL_TARGET = " << project->first("TARGET_SHORT") << endl;
955 else
956 t << "QMAKE_PRL_TARGET = " << target << endl;
957 if(!project->isEmpty("PRL_EXPORT_DEFINES"))
958 t << "QMAKE_PRL_DEFINES = " << project->values("PRL_EXPORT_DEFINES").join(" ") << endl;
959 if(!project->isEmpty("PRL_EXPORT_CFLAGS"))
960 t << "QMAKE_PRL_CFLAGS = " << project->values("PRL_EXPORT_CFLAGS").join(" ") << endl;
961 if(!project->isEmpty("PRL_EXPORT_CXXFLAGS"))
962 t << "QMAKE_PRL_CXXFLAGS = " << project->values("PRL_EXPORT_CXXFLAGS").join(" ") << endl;
963 if(!project->isEmpty("CONFIG"))
964 t << "QMAKE_PRL_CONFIG = " << project->values("CONFIG").join(" ") << endl;
965 if(!project->isEmpty("TARGET_VERSION_EXT"))
966 t << "QMAKE_PRL_VERSION = " << project->first("TARGET_VERSION_EXT") << endl;
967 else if(!project->isEmpty("VERSION"))
968 t << "QMAKE_PRL_VERSION = " << project->first("VERSION") << endl;
969 if(project->isActiveConfig("staticlib") || project->isActiveConfig("explicitlib") ||
970 !project->isEmpty("PRL_EXPORT_LIBS")) {
971 t << "QMAKE_PRL_LIBS = ";
972 if (!project->isEmpty("PRL_EXPORT_LIBS")) {
973 // PRL_EXPORT_LIBS overrides anything else
974 t << project->values("PRL_EXPORT_LIBS").join(" ");
975 } else {
976 QStringList libs;
977 if(!project->isEmpty("QMAKE_INTERNAL_PRL_LIBS"))
978 libs = project->values("QMAKE_INTERNAL_PRL_LIBS");
979 else
980 libs << "QMAKE_LIBS"; //obvious one
981 if(project->isActiveConfig("staticlib"))
982 libs << "QMAKE_LIBS_PRIVATE";
983 for(QStringList::Iterator it = libs.begin(); it != libs.end(); ++it)
984 t << project->values((*it)).join(" ") << " ";
985 }
986 t << endl;
987 }
988}
989
990bool
991MakefileGenerator::writeProjectMakefile()
992{
993 usePlatformDir();
994 QTextStream t(&Option::output);
995
996 //header
997 writeHeader(t);
998
999 QList<SubTarget*> targets;
1000 {
1001 QStringList builds = project->values("BUILDS");
1002 for(QStringList::Iterator it = builds.begin(); it != builds.end(); ++it) {
1003 SubTarget *st = new SubTarget;
1004 targets.append(st);
1005 st->makefile = "$(MAKEFILE)." + (*it);
1006 st->name = (*it);
1007 st->target = project->isEmpty((*it) + ".target") ? (*it) : project->first((*it) + ".target");
1008 }
1009 }
1010 if(project->isActiveConfig("build_all")) {
1011 t << "first: all" << endl;
1012 QList<SubTarget*>::Iterator it;
1013
1014 //install
1015 t << "install: ";
1016 for(it = targets.begin(); it != targets.end(); ++it)
1017 t << (*it)->target << "-install ";
1018 t << endl;
1019
1020 //uninstall
1021 t << "uninstall: ";
1022 for(it = targets.begin(); it != targets.end(); ++it)
1023 t << (*it)->target << "-uninstall ";
1024 t << endl;
1025 } else {
1026 t << "first: " << targets.first()->target << endl
1027 << "install: " << targets.first()->target << "-install" << endl
1028 << "uninstall: " << targets.first()->target << "-uninstall" << endl;
1029 }
1030
1031 writeSubTargets(t, targets, SubTargetsNoFlags);
1032 if(!project->isActiveConfig("no_autoqmake")) {
1033 for(QList<SubTarget*>::Iterator it = targets.begin(); it != targets.end(); ++it)
1034 t << (*it)->makefile << ": " <<
1035 Option::fixPathToTargetOS(fileFixify(Option::output.fileName())) << endl;
1036 }
1037 qDeleteAll(targets);
1038 return true;
1039}
1040
1041bool
1042MakefileGenerator::write()
1043{
1044 if(!project)
1045 return false;
1046 writePrlFile();
1047 if(Option::qmake_mode == Option::QMAKE_GENERATE_MAKEFILE || //write makefile
1048 Option::qmake_mode == Option::QMAKE_GENERATE_PROJECT) {
1049 QTextStream t(&Option::output);
1050 if(!writeMakefile(t)) {
1051#if 1
1052 warn_msg(WarnLogic, "Unable to generate output for: %s [TEMPLATE %s]",
1053 Option::output.fileName().toLatin1().constData(),
1054 project->first("TEMPLATE").toLatin1().constData());
1055 if(Option::output.exists())
1056 Option::output.remove();
1057#endif
1058 }
1059 }
1060 return true;
1061}
1062
1063QString
1064MakefileGenerator::prlFileName(bool fixify)
1065{
1066 QString ret = project->first("TARGET_PRL");;
1067 if(ret.isEmpty())
1068 ret = project->first("TARGET");
1069 int slsh = ret.lastIndexOf(Option::dir_sep);
1070 if(slsh != -1)
1071 ret.remove(0, slsh);
1072 if(!ret.endsWith(Option::prl_ext)) {
1073 int dot = ret.indexOf('.');
1074 if(dot != -1)
1075 ret.truncate(dot);
1076 ret += Option::prl_ext;
1077 }
1078 if(!project->isEmpty("QMAKE_BUNDLE"))
1079 ret.prepend(project->first("QMAKE_BUNDLE") + Option::dir_sep);
1080 if(fixify) {
1081 if(!project->isEmpty("DESTDIR"))
1082 ret.prepend(project->first("DESTDIR"));
1083 ret = Option::fixPathToLocalOS(fileFixify(ret, qmake_getpwd(), Option::output_dir));
1084 }
1085 return ret;
1086}
1087
1088void
1089MakefileGenerator::writePrlFile()
1090{
1091 // note: on OS/2, we need .prl even for plugins since we use
1092 // QMAKE_PRL_TARGET from there to translate universally used long plugin
1093 // names to short names of actual OS/2 DLL libraries (which cannot exceed
1094 // the 8x3 limit)
1095
1096 if((Option::qmake_mode == Option::QMAKE_GENERATE_MAKEFILE ||
1097 Option::qmake_mode == Option::QMAKE_GENERATE_PRL)
1098 && project->values("QMAKE_FAILED_REQUIREMENTS").isEmpty()
1099 && project->isActiveConfig("create_prl")
1100 && (project->first("TEMPLATE") == "lib"
1101 || project->first("TEMPLATE") == "vclib")
1102 && (project->isActiveConfig("os2") || !project->isActiveConfig("plugin"))
1103 ) { //write prl file
1104 QString local_prl = prlFileName();
1105 QString prl = fileFixify(local_prl);
1106 mkdir(fileInfo(local_prl).path());
1107 QFile ft(local_prl);
1108 if(ft.open(QIODevice::WriteOnly)) {
1109 project->values("ALL_DEPS").append(prl);
1110 project->values("QMAKE_INTERNAL_PRL_FILE").append(prl);
1111 QTextStream t(&ft);
1112 writePrlFile(t);
1113 }
1114 }
1115}
1116
1117// Manipulate directories, so it's possible to build
1118// several cross-platform targets concurrently
1119void
1120MakefileGenerator::usePlatformDir()
1121{
1122 QString pltDir(project->first("QMAKE_PLATFORM_DIR"));
1123 if(pltDir.isEmpty())
1124 return;
1125 QChar sep = QDir::separator();
1126 QString slashPltDir = sep + pltDir;
1127
1128 QString dirs[] = { QString("OBJECTS_DIR"), QString("DESTDIR"), QString("QMAKE_PKGCONFIG_DESTDIR"),
1129 QString("SUBLIBS_DIR"), QString("DLLDESTDIR"), QString("QMAKE_LIBTOOL_DESTDIR"),
1130 QString("PRECOMPILED_DIR"), QString("QMAKE_LIBDIR_QT"), QString() };
1131 for(int i = 0; !dirs[i].isEmpty(); ++i) {
1132 QString filePath = project->first(dirs[i]);
1133 project->values(dirs[i]) = QStringList(filePath + (filePath.isEmpty() ? pltDir : slashPltDir));
1134 }
1135
1136 QString libs[] = { QString("QMAKE_LIBS_QT"), QString("QMAKE_LIBS_QT_THREAD"), QString("QMAKE_LIBS_QT_ENTRY"), QString() };
1137 for(int i = 0; !libs[i].isEmpty(); ++i) {
1138 QString filePath = project->first(libs[i]);
1139 int fpi = filePath.lastIndexOf(sep);
1140 if(fpi == -1)
1141 project->values(libs[i]).prepend(pltDir + sep);
1142 else
1143 project->values(libs[i]) = QStringList(filePath.left(fpi) + slashPltDir + filePath.mid(fpi));
1144 }
1145}
1146
1147void
1148MakefileGenerator::writeObj(QTextStream &t, const QString &src)
1149{
1150 QStringList &srcl = project->values(src);
1151 QStringList objl = createObjectList(srcl);
1152
1153 QStringList::Iterator oit = objl.begin();
1154 QStringList::Iterator sit = srcl.begin();
1155 QString stringSrc("$src");
1156 QString stringObj("$obj");
1157 for(;sit != srcl.end() && oit != objl.end(); ++oit, ++sit) {
1158 if((*sit).isEmpty())
1159 continue;
1160
1161 t << escapeDependencyPath((*oit)) << ": " << escapeDependencyPath((*sit)) << " " << escapeDependencyPaths(findDependencies((*sit))).join(" \\\n\t\t");
1162
1163 QString comp, cimp;
1164 for(QStringList::Iterator cppit = Option::cpp_ext.begin(); cppit != Option::cpp_ext.end(); ++cppit) {
1165 if((*sit).endsWith((*cppit))) {
1166 comp = "QMAKE_RUN_CXX";
1167 cimp = "QMAKE_RUN_CXX_IMP";
1168 break;
1169 }
1170 }
1171 if(comp.isEmpty()) {
1172 comp = "QMAKE_RUN_CC";
1173 cimp = "QMAKE_RUN_CC_IMP";
1174 }
1175 bool use_implicit_rule = !project->isEmpty(cimp);
1176 use_implicit_rule = false;
1177 if(use_implicit_rule) {
1178 if(!project->isEmpty("OBJECTS_DIR")) {
1179 use_implicit_rule = false;
1180 } else {
1181 int dot = (*sit).lastIndexOf('.');
1182 if(dot == -1 || ((*sit).left(dot) + Option::obj_ext != (*oit)))
1183 use_implicit_rule = false;
1184 }
1185 }
1186 if (!use_implicit_rule && !project->isEmpty(comp)) {
1187 QString p = var(comp), srcf(*sit);
1188 p.replace(stringSrc, escapeFilePath(srcf));
1189 p.replace(stringObj, escapeFilePath((*oit)));
1190 t << "\n\t" << p;
1191 }
1192 t << endl << endl;
1193 }
1194}
1195
1196QString
1197MakefileGenerator::filePrefixRoot(const QString &root, const QString &path)
1198{
1199 QString ret(root + path);
1200 if(path.length() > 2 && path[1] == ':') { //c:\foo
1201 if (Option::target_mode == Option::TARG_OS2_MODE)
1202 ret = root + path.mid(2);
1203 else
1204 ret = path.mid(0, 2) + root + path.mid(2);
1205 }
1206 while(ret.endsWith("\\"))
1207 ret = ret.left(ret.length()-1);
1208 return ret;
1209}
1210
1211void
1212MakefileGenerator::writeInstalls(QTextStream &t, const QString &installs, bool noBuild)
1213{
1214 const QString del_suffix =
1215 Option::target_mode == Option::TARG_OS2_MODE ?
1216 QString(" >nul 2>&1"): // reduce noise
1217 QString::null;
1218
1219 const QString inst_prefix =
1220 Option::target_mode == Option::TARG_OS2_MODE ?
1221 QString::null: // report errors (copy command overwrites quietly)
1222 QString("-");
1223
1224 QString rm_dir_contents("-$(DEL_FILE)");
1225 if (!isDosLikeShell()) //ick
1226 rm_dir_contents = "-$(DEL_FILE) -r";
1227
1228 QString all_installs, all_uninstalls;
1229 QStringList &l = project->values(installs);
1230 for(QStringList::Iterator it = l.begin(); it != l.end(); ++it) {
1231 QString pvar = (*it) + ".path";
1232 if(project->values((*it) + ".CONFIG").indexOf("no_path") == -1 &&
1233 project->values((*it) + ".CONFIG").indexOf("dummy_install") == -1 &&
1234 project->values(pvar).isEmpty()) {
1235 warn_msg(WarnLogic, "%s is not defined: install target not created\n", pvar.toLatin1().constData());
1236 continue;
1237 }
1238
1239 bool do_default = true;
1240 const QString root = "$(INSTALL_ROOT)";
1241 QString target, dst;
1242 if(project->values((*it) + ".CONFIG").indexOf("no_path") == -1 &&
1243 project->values((*it) + ".CONFIG").indexOf("dummy_install") == -1) {
1244 dst = fileFixify(unescapeFilePath(project->values(pvar).first()), FileFixifyAbsolute, false);
1245 }
1246 dst = escapeFilePath(dst);
1247 if(dst.right(Option::dir_sep.length()) != Option::dir_sep)
1248 dst += Option::dir_sep;
1249
1250 QStringList tmp, uninst = project->values((*it) + ".uninstall");
1251 //other
1252 tmp = project->values((*it) + ".extra");
1253 if(tmp.isEmpty())
1254 tmp = project->values((*it) + ".commands"); //to allow compatible name
1255 if(!tmp.isEmpty()) {
1256 do_default = false;
1257 if(!target.isEmpty())
1258 target += "\n\t";
1259 target += tmp.join(" ");
1260 }
1261 //masks
1262 tmp = findFilesInVPATH(project->values((*it) + ".files"), VPATH_NoFixify);
1263 tmp = fileFixify(tmp, FileFixifyAbsolute);
1264 if(!tmp.isEmpty()) {
1265 if(!target.isEmpty())
1266 target += "\n";
1267 do_default = false;
1268 for(QStringList::Iterator wild_it = tmp.begin(); wild_it != tmp.end(); ++wild_it) {
1269 QString wild = Option::fixPathToLocalOS((*wild_it), false, false);
1270 QString dirstr = qmake_getpwd(), filestr = wild;
1271 int slsh = filestr.lastIndexOf(Option::dir_sep);
1272 if(slsh != -1) {
1273 dirstr = filestr.left(slsh+1);
1274 filestr.remove(0, slsh+1);
1275 }
1276 if(!dirstr.endsWith(Option::dir_sep))
1277 dirstr += Option::dir_sep;
1278 if(exists(wild)) { //real file
1279 QString file = wild;
1280 QFileInfo fi(fileInfo(wild));
1281 if(!target.isEmpty())
1282 target += "\t";
1283 QString dst_file = filePrefixRoot(root, dst);
1284 if(fi.isDir() && project->isActiveConfig("copy_dir_files")) {
1285 if(!dst_file.endsWith(Option::dir_sep))
1286 dst_file += Option::dir_sep;
1287 dst_file += fi.fileName();
1288 }
1289 QString cmd;
1290 if (fi.isDir())
1291 cmd = inst_prefix + "$(INSTALL_DIR)";
1292 else if (fi.isExecutable())
1293 cmd = inst_prefix + "$(INSTALL_PROGRAM)";
1294 else
1295 cmd = inst_prefix + "$(INSTALL_FILE)";
1296 cmd += " " + escapeFilePath(wild) + " " + dst_file + "\n";
1297 target += cmd;
1298 if(!project->isActiveConfig("debug") && !project->isActiveConfig("nostrip") &&
1299 !fi.isDir() && fi.isExecutable() && !project->isEmpty("QMAKE_STRIP"))
1300 target += QString("\t-") + var("QMAKE_STRIP") + " " +
1301 filePrefixRoot(root, fileFixify(dst + filestr, FileFixifyAbsolute, false)) + "\n";
1302 if(!uninst.isEmpty())
1303 uninst.append("\n\t");
1304 uninst.append(rm_dir_contents + " " + filePrefixRoot(root, fileFixify(dst + filestr, FileFixifyAbsolute, false)) + del_suffix);
1305 continue;
1306 }
1307 QString local_dirstr = Option::fixPathToLocalOS(dirstr, true);
1308 QStringList files = QDir(local_dirstr).entryList(QStringList(filestr));
1309 if(project->values((*it) + ".CONFIG").indexOf("no_check_exist") != -1 && files.isEmpty()) {
1310 if(!target.isEmpty())
1311 target += "\t";
1312 QString dst_file = filePrefixRoot(root, dst);
1313 QFileInfo fi(fileInfo(wild));
1314 QString cmd = inst_prefix + QString(fi.isExecutable() ? "$(INSTALL_PROGRAM)" : "$(INSTALL_FILE)") + " " +
1315 wild + " " + dst_file + "\n";
1316 target += cmd;
1317 if(!uninst.isEmpty())
1318 uninst.append("\n\t");
1319 uninst.append(rm_dir_contents + " " + filePrefixRoot(root, fileFixify(dst + filestr, FileFixifyAbsolute, false)) + del_suffix);
1320 }
1321 for(int x = 0; x < files.count(); x++) {
1322 QString file = files[x];
1323 if(file == "." || file == "..") //blah
1324 continue;
1325 if(!uninst.isEmpty())
1326 uninst.append("\n\t");
1327 uninst.append(rm_dir_contents + " " + filePrefixRoot(root, fileFixify(dst + file, FileFixifyAbsolute, false)) + del_suffix);
1328 QFileInfo fi(fileInfo(dirstr + file));
1329 if(!target.isEmpty())
1330 target += "\t";
1331 QString dst_file = filePrefixRoot(root, fileFixify(dst, FileFixifyAbsolute, false));
1332 if(fi.isDir() && project->isActiveConfig("copy_dir_files")) {
1333 if(!dst_file.endsWith(Option::dir_sep))
1334 dst_file += Option::dir_sep;
1335 dst_file += fi.fileName();
1336 }
1337 QString cmd = inst_prefix + QString(fi.isDir() ? "$(INSTALL_DIR)" : "$(INSTALL_FILE)") + " " +
1338 dirstr + file + " " + dst_file + "\n";
1339 target += cmd;
1340 if(!project->isActiveConfig("debug") && !project->isActiveConfig("nostrip") &&
1341 !fi.isDir() && fi.isExecutable() && !project->isEmpty("QMAKE_STRIP"))
1342 target += QString("\t-") + var("QMAKE_STRIP") + " " +
1343 filePrefixRoot(root, fileFixify(dst + file, FileFixifyAbsolute, false)) +
1344 "\n";
1345 }
1346 }
1347 }
1348 //default?
1349 if(do_default) {
1350 target = defaultInstall((*it));
1351 uninst = project->values((*it) + ".uninstall");
1352 }
1353
1354 if(!target.isEmpty() || project->values((*it) + ".CONFIG").indexOf("dummy_install") != -1) {
1355 if(noBuild || project->values((*it) + ".CONFIG").indexOf("no_build") != -1)
1356 t << "install_" << (*it) << ":";
1357 else if(project->isActiveConfig("build_all"))
1358 t << "install_" << (*it) << ": all";
1359 else
1360 t << "install_" << (*it) << ": first";
1361 const QStringList &deps = project->values((*it) + ".depends");
1362 if(!deps.isEmpty()) {
1363 for(QStringList::ConstIterator dep_it = deps.begin(); dep_it != deps.end(); ++dep_it) {
1364 QString targ = var((*dep_it) + ".target");
1365 if(targ.isEmpty())
1366 targ = (*dep_it);
1367 t << " " << escapeDependencyPath(targ);
1368 }
1369 }
1370 if(project->isEmpty("QMAKE_NOFORCE"))
1371 t << " FORCE";
1372 t << "\n\t";
1373 const QStringList &dirs = project->values(pvar);
1374 for(QStringList::ConstIterator pit = dirs.begin(); pit != dirs.end(); ++pit) {
1375 QString tmp_dst = fileFixify((*pit), FileFixifyAbsolute, false);
1376 if (!isDosLikeShell() && !tmp_dst.endsWith(Option::dir_sep))
1377 tmp_dst += Option::dir_sep;
1378 t << mkdir_p_asstring(filePrefixRoot(root, tmp_dst)) << "\n\t";
1379 }
1380 t << target << endl << endl;
1381 if(!uninst.isEmpty()) {
1382 t << "uninstall_" << (*it) << ": ";
1383 if(project->isEmpty("QMAKE_NOFORCE"))
1384 t << " FORCE";
1385 t << "\n\t"
1386 << uninst.join(" ") << "\n\t"
1387 << "-$(DEL_DIR) " << filePrefixRoot(root, dst) << " " << del_suffix << endl << endl;
1388 }
1389 t << endl;
1390
1391 if(project->values((*it) + ".CONFIG").indexOf("no_default_install") == -1) {
1392 all_installs += QString("install_") + (*it) + " ";
1393 if(!uninst.isEmpty())
1394 all_uninstalls += "uninstall_" + (*it) + " ";
1395 }
1396 } else {
1397 debug_msg(1, "no definition for install %s: install target not created",(*it).toLatin1().constData());
1398 }
1399 }
1400 t << "install: " << var("INSTALLDEPS") << " " << all_installs;
1401 if(project->isEmpty("QMAKE_NOFORCE"))
1402 t << " FORCE";
1403 t << "\n\n";
1404 t << "uninstall: " << all_uninstalls << " " << var("UNINSTALLDEPS");
1405 if(project->isEmpty("QMAKE_NOFORCE"))
1406 t << " FORCE";
1407 t << "\n\n";
1408}
1409
1410QString
1411MakefileGenerator::var(const QString &var)
1412{
1413 return val(project->values(var));
1414}
1415
1416QString
1417MakefileGenerator::val(const QStringList &varList)
1418{
1419 return valGlue(varList, "", " ", "");
1420}
1421
1422QString
1423MakefileGenerator::varGlue(const QString &var, const QString &before, const QString &glue, const QString &after)
1424{
1425 return valGlue(project->values(var), before, glue, after);
1426}
1427
1428QString
1429MakefileGenerator::valGlue(const QStringList &varList, const QString &before, const QString &glue, const QString &after)
1430{
1431 QString ret;
1432 for(QStringList::ConstIterator it = varList.begin(); it != varList.end(); ++it) {
1433 if(!(*it).isEmpty()) {
1434 if(!ret.isEmpty())
1435 ret += glue;
1436 ret += (*it);
1437 }
1438 }
1439 return ret.isEmpty() ? QString("") : before + ret + after;
1440}
1441
1442
1443QString
1444MakefileGenerator::varList(const QString &var)
1445{
1446 return valList(project->values(var));
1447}
1448
1449QString
1450MakefileGenerator::valList(const QStringList &varList)
1451{
1452 return valGlue(varList, "", " \\\n\t\t", "");
1453}
1454
1455QStringList
1456MakefileGenerator::createObjectList(const QStringList &sources)
1457{
1458 QStringList ret;
1459 QString objdir;
1460 if(!project->values("OBJECTS_DIR").isEmpty())
1461 objdir = project->first("OBJECTS_DIR");
1462 for(QStringList::ConstIterator it = sources.begin(); it != sources.end(); ++it) {
1463 QFileInfo fi(fileInfo(Option::fixPathToLocalOS((*it))));
1464 QString dir;
1465 if(objdir.isEmpty() && project->isActiveConfig("object_with_source")) {
1466 QString fName = Option::fixPathToTargetOS((*it), false);
1467 int dl = fName.lastIndexOf(Option::dir_sep);
1468 if(dl != -1)
1469 dir = fName.left(dl + 1);
1470 } else {
1471 dir = objdir;
1472 }
1473 ret.append(dir + fi.completeBaseName() + Option::obj_ext);
1474 }
1475 return ret;
1476}
1477
1478ReplaceExtraCompilerCacheKey::ReplaceExtraCompilerCacheKey(const QString &v, const QStringList &i, const QStringList &o)
1479{
1480 hash = 0;
1481 pwd = qmake_getpwd();
1482 var = v;
1483 {
1484 QStringList il = i;
1485 il.sort();
1486 in = il.join("::");
1487 }
1488 {
1489 QStringList ol = o;
1490 ol.sort();
1491 out = ol.join("::");
1492 }
1493}
1494
1495bool ReplaceExtraCompilerCacheKey::operator==(const ReplaceExtraCompilerCacheKey &f) const
1496{
1497 return (hashCode() == f.hashCode() &&
1498 f.in == in &&
1499 f.out == out &&
1500 f.var == var &&
1501 f.pwd == pwd);
1502}
1503
1504
1505QString
1506MakefileGenerator::replaceExtraCompilerVariables(const QString &orig_var, const QStringList &in, const QStringList &out)
1507{
1508 //lazy cache
1509 ReplaceExtraCompilerCacheKey cacheKey(orig_var, in, out);
1510 QString cacheVal = extraCompilerVariablesCache.value(cacheKey);
1511 if(!cacheVal.isNull())
1512 return cacheVal;
1513
1514 //do the work
1515 QString ret = orig_var;
1516 QRegExp reg_var("\\$\\{.*\\}");
1517 reg_var.setMinimal(true);
1518 for(int rep = 0; (rep = reg_var.indexIn(ret, rep)) != -1; ) {
1519 QStringList val;
1520 const QString var = ret.mid(rep + 2, reg_var.matchedLength() - 3);
1521 bool filePath = false;
1522 if(val.isEmpty() && var.startsWith(QLatin1String("QMAKE_VAR_"))) {
1523 const QString varname = var.mid(10);
1524 val += project->values(varname);
1525 }
1526 if(val.isEmpty() && var.startsWith(QLatin1String("QMAKE_VAR_FIRST_"))) {
1527 const QString varname = var.mid(16);
1528 val += project->first(varname);
1529 }
1530
1531 if(val.isEmpty() && !in.isEmpty()) {
1532 if(var.startsWith(QLatin1String("QMAKE_FUNC_FILE_IN_"))) {
1533 filePath = true;
1534 const QString funcname = var.mid(19);
1535 val += project->expand(funcname, QList<QStringList>() << in);
1536 } else if(var == QLatin1String("QMAKE_FILE_BASE") || var == QLatin1String("QMAKE_FILE_IN_BASE")) {
1537 //filePath = true;
1538 for(int i = 0; i < in.size(); ++i) {
1539 QFileInfo fi(fileInfo(Option::fixPathToLocalOS(in.at(i))));
1540 QString base = fi.completeBaseName();
1541 if(base.isNull())
1542 base = fi.fileName();
1543 val += base;
1544 }
1545 } else if(var == QLatin1String("QMAKE_FILE_PATH") || var == QLatin1String("QMAKE_FILE_IN_PATH")) {
1546 filePath = true;
1547 for(int i = 0; i < in.size(); ++i)
1548 val += fileInfo(Option::fixPathToLocalOS(in.at(i))).path();
1549 } else if(var == QLatin1String("QMAKE_FILE_NAME") || var == QLatin1String("QMAKE_FILE_IN")) {
1550 filePath = true;
1551 for(int i = 0; i < in.size(); ++i)
1552 val += fileInfo(Option::fixPathToLocalOS(in.at(i))).filePath();
1553
1554 }
1555 }
1556 if(val.isEmpty() && !out.isEmpty()) {
1557 if(var.startsWith(QLatin1String("QMAKE_FUNC_FILE_OUT_"))) {
1558 filePath = true;
1559 const QString funcname = var.mid(20);
1560 val += project->expand(funcname, QList<QStringList>() << out);
1561 } else if(var == QLatin1String("QMAKE_FILE_OUT")) {
1562 filePath = true;
1563 for(int i = 0; i < out.size(); ++i)
1564 val += fileInfo(Option::fixPathToLocalOS(out.at(i))).filePath();
1565 } else if(var == QLatin1String("QMAKE_FILE_OUT_BASE")) {
1566 //filePath = true;
1567 for(int i = 0; i < out.size(); ++i) {
1568 QFileInfo fi(fileInfo(Option::fixPathToLocalOS(out.at(i))));
1569 QString base = fi.completeBaseName();
1570 if(base.isNull())
1571 base = fi.fileName();
1572 val += base;
1573 }
1574 }
1575 }
1576 if(val.isEmpty() && var.startsWith(QLatin1String("QMAKE_FUNC_"))) {
1577 const QString funcname = var.mid(11);
1578 val += project->expand(funcname, QList<QStringList>() << in << out);
1579 }
1580
1581 if(!val.isEmpty()) {
1582 QString fullVal;
1583 if(filePath) {
1584 for(int i = 0; i < val.size(); ++i) {
1585 const QString file = Option::fixPathToTargetOS(unescapeFilePath(val.at(i)), false);
1586 if(!fullVal.isEmpty())
1587 fullVal += " ";
1588 fullVal += escapeFilePath(file);
1589 }
1590 } else {
1591 fullVal = val.join(" ");
1592 }
1593 ret.replace(rep, reg_var.matchedLength(), fullVal);
1594 rep += fullVal.length();
1595 } else {
1596 rep += reg_var.matchedLength();
1597 }
1598 }
1599
1600 //cache the value
1601 extraCompilerVariablesCache.insert(cacheKey, ret);
1602 return ret;
1603}
1604
1605bool
1606MakefileGenerator::verifyExtraCompiler(const QString &comp, const QString &file_unfixed)
1607{
1608 if(noIO())
1609 return false;
1610 const QString file = Option::fixPathToLocalOS(file_unfixed);
1611
1612 if(project->values(comp + ".CONFIG").indexOf("moc_verify") != -1) {
1613 if(!file.isNull()) {
1614 QMakeSourceFileInfo::addSourceFile(file, QMakeSourceFileInfo::SEEK_MOCS);
1615 if(!mocable(file)) {
1616 return false;
1617 } else {
1618 project->values("MOCABLES").append(file);
1619 }
1620 }
1621 } else if(project->values(comp + ".CONFIG").indexOf("function_verify") != -1) {
1622 QString tmp_out = project->values(comp + ".output").first();
1623 if(tmp_out.isEmpty())
1624 return false;
1625 QStringList verify_function = project->values(comp + ".verify_function");
1626 if(verify_function.isEmpty())
1627 return false;
1628
1629 for(int i = 0; i < verify_function.size(); ++i) {
1630 bool invert = false;
1631 QString verify = verify_function.at(i);
1632 if(verify.at(0) == QLatin1Char('!')) {
1633 invert = true;
1634 verify = verify.mid(1);
1635 }
1636
1637 if(project->values(comp + ".CONFIG").indexOf("combine") != -1) {
1638 bool pass = project->test(verify, QList<QStringList>() << QStringList(tmp_out) << QStringList(file));
1639 if(invert)
1640 pass = !pass;
1641 if(!pass)
1642 return false;
1643 } else {
1644 QStringList &tmp = project->values(comp + ".input");
1645 for(QStringList::Iterator it = tmp.begin(); it != tmp.end(); ++it) {
1646 QStringList &inputs = project->values((*it));
1647 for(QStringList::Iterator input = inputs.begin(); input != inputs.end(); ++input) {
1648 if((*input).isEmpty())
1649 continue;
1650 QString in = fileFixify(Option::fixPathToTargetOS((*input), false));
1651 if(in == file) {
1652 bool pass = project->test(verify,
1653 QList<QStringList>() << QStringList(replaceExtraCompilerVariables(tmp_out, (*input), QString())) <<
1654 QStringList(file));
1655 if(invert)
1656 pass = !pass;
1657 if(!pass)
1658 return false;
1659 break;
1660 }
1661 }
1662 }
1663 }
1664 }
1665 } else if(project->values(comp + ".CONFIG").indexOf("verify") != -1) {
1666 QString tmp_out = project->values(comp + ".output").first();
1667 if(tmp_out.isEmpty())
1668 return false;
1669 QString tmp_cmd;
1670 if(!project->isEmpty(comp + ".commands")) {
1671 int argv0 = -1;
1672 QStringList cmdline = project->values(comp + ".commands");
1673 for(int i = 0; i < cmdline.count(); ++i) {
1674 if(!cmdline.at(i).contains('=')) {
1675 argv0 = i;
1676 break;
1677 }
1678 }
1679 if(argv0 != -1) {
1680 cmdline[argv0] = Option::fixPathToTargetOS(cmdline.at(argv0), false);
1681 tmp_cmd = cmdline.join(" ");
1682 }
1683 }
1684
1685 if(project->values(comp + ".CONFIG").indexOf("combine") != -1) {
1686 QString cmd = replaceExtraCompilerVariables(tmp_cmd, QString(), tmp_out);
1687 if(system(cmd.toLatin1().constData()))
1688 return false;
1689 } else {
1690 QStringList &tmp = project->values(comp + ".input");
1691 for(QStringList::Iterator it = tmp.begin(); it != tmp.end(); ++it) {
1692 QStringList &inputs = project->values((*it));
1693 for(QStringList::Iterator input = inputs.begin(); input != inputs.end(); ++input) {
1694 if((*input).isEmpty())
1695 continue;
1696 QString in = fileFixify(Option::fixPathToTargetOS((*input), false));
1697 if(in == file) {
1698 QString out = replaceExtraCompilerVariables(tmp_out, (*input), QString());
1699 QString cmd = replaceExtraCompilerVariables(tmp_cmd, in, out);
1700 if(system(cmd.toLatin1().constData()))
1701 return false;
1702 break;
1703 }
1704 }
1705 }
1706 }
1707 }
1708 return true;
1709}
1710
1711void
1712MakefileGenerator::writeExtraTargets(QTextStream &t)
1713{
1714 QStringList &qut = project->values("QMAKE_EXTRA_TARGETS");
1715 for(QStringList::Iterator it = qut.begin(); it != qut.end(); ++it) {
1716 QString targ = var((*it) + ".target"),
1717 cmd = var((*it) + ".commands"), deps;
1718 if(targ.isEmpty())
1719 targ = (*it);
1720 QStringList &deplist = project->values((*it) + ".depends");
1721 for(QStringList::Iterator dep_it = deplist.begin(); dep_it != deplist.end(); ++dep_it) {
1722 QString dep = var((*dep_it) + ".target");
1723 if(dep.isEmpty())
1724 dep = (*dep_it);
1725 deps += " " + escapeDependencyPath(dep);
1726 }
1727 if(project->values((*it) + ".CONFIG").indexOf("fix_target") != -1)
1728 targ = fileFixify(targ);
1729 if(project->isEmpty("QMAKE_NOFORCE") &&
1730 project->values((*it) + ".CONFIG").indexOf("phony") != -1)
1731 deps += QString(" ") + "FORCE";
1732 t << escapeDependencyPath(targ) << ":" << deps;
1733 if(!cmd.isEmpty())
1734 t << "\n\t" << cmd;
1735 t << endl << endl;
1736
1737 project->values(QLatin1String("QMAKE_INTERNAL_ET_PARSED_TARGETS.") + (*it)) << escapeDependencyPath(targ);
1738 project->values(QLatin1String("QMAKE_INTERNAL_ET_PARSED_DEPS.") + (*it) + escapeDependencyPath(targ)) << deps.split(" ", QString::SkipEmptyParts);
1739 project->values(QLatin1String("QMAKE_INTERNAL_ET_PARSED_CMD.") + (*it) + escapeDependencyPath(targ)) << cmd;
1740 }
1741}
1742
1743void
1744MakefileGenerator::writeExtraCompilerTargets(QTextStream &t)
1745{
1746 QString clean_targets;
1747 const QStringList &quc = project->values("QMAKE_EXTRA_COMPILERS");
1748 for(QStringList::ConstIterator it = quc.begin(); it != quc.end(); ++it) {
1749 QString tmp_out = fileFixify(project->values((*it) + ".output").first(),
1750 Option::output_dir, Option::output_dir);
1751 QString tmp_cmd;
1752 if(!project->isEmpty((*it) + ".commands")) {
1753 QStringList cmdline = project->values((*it) + ".commands");
1754 int argv0 = findExecutable(cmdline);
1755 if(argv0 != -1) {
1756 cmdline[argv0] = escapeFilePath(Option::fixPathToTargetOS(cmdline.at(argv0), false));
1757 tmp_cmd = cmdline.join(" ");
1758 }
1759 }
1760 QStringList tmp_dep = project->values((*it) + ".depends");
1761 QString tmp_dep_cmd;
1762 if(!project->isEmpty((*it) + ".depend_command")) {
1763 int argv0 = -1;
1764 QStringList cmdline = project->values((*it) + ".depend_command");
1765 for(int i = 0; i < cmdline.count(); ++i) {
1766 if(!cmdline.at(i).contains('=')) {
1767 argv0 = i;
1768 break;
1769 }
1770 }
1771 if(argv0 != -1) {
1772 const QString c = Option::fixPathToLocalOS(cmdline.at(argv0), true);
1773 if(exists(c)) {
1774 cmdline[argv0] = escapeFilePath(Option::fixPathToLocalOS(cmdline.at(argv0), false));
1775 tmp_dep_cmd = cmdline.join(" ");
1776 } else {
1777 cmdline[argv0] = escapeFilePath(cmdline.at(argv0));
1778 }
1779 }
1780 }
1781 QStringList &vars = project->values((*it) + ".variables");
1782 if(tmp_out.isEmpty() || tmp_cmd.isEmpty())
1783 continue;
1784 QStringList tmp_inputs;
1785 {
1786 const QStringList &comp_inputs = project->values((*it) + ".input");
1787 for(QStringList::ConstIterator it2 = comp_inputs.begin(); it2 != comp_inputs.end(); ++it2) {
1788 const QStringList &tmp = project->values((*it2));
1789 for(QStringList::ConstIterator input = tmp.begin(); input != tmp.end(); ++input) {
1790 QString in = Option::fixPathToTargetOS((*input), false);
1791 if(verifyExtraCompiler((*it), in))
1792 tmp_inputs.append((*input));
1793 }
1794 }
1795 }
1796
1797 t << "compiler_" << (*it) << "_make_all:";
1798 if(project->values((*it) + ".CONFIG").indexOf("combine") != -1) {
1799 // compilers with a combined input only have one output
1800 QString input = project->values((*it) + ".output").first();
1801 t << " " << escapeDependencyPath(replaceExtraCompilerVariables(tmp_out, input, QString()));
1802 } else {
1803 for(QStringList::ConstIterator input = tmp_inputs.begin(); input != tmp_inputs.end(); ++input) {
1804 QString in = Option::fixPathToTargetOS((*input), false);
1805 t << " " << escapeDependencyPath(replaceExtraCompilerVariables(tmp_out, (*input), QString()));
1806 }
1807 }
1808 t << endl;
1809
1810 if(project->values((*it) + ".CONFIG").indexOf("no_clean") == -1) {
1811 QString tmp_clean = project->values((*it) + ".clean").join(" ");
1812 QString tmp_clean_cmds = project->values((*it) + ".clean_commands").join(" ");
1813 if(!tmp_inputs.isEmpty())
1814 clean_targets += QString("compiler_" + (*it) + "_clean ");
1815 t << "compiler_" << (*it) << "_clean:";
1816 bool wrote_clean_cmds = false, wrote_clean = false;
1817 if(tmp_clean_cmds.isEmpty()) {
1818 wrote_clean_cmds = true;
1819 } else if(tmp_clean_cmds.indexOf("${QMAKE_") == -1) {
1820 t << "\n\t" << tmp_clean_cmds;
1821 wrote_clean_cmds = true;
1822 }
1823 if(tmp_clean.isEmpty())
1824 tmp_clean = tmp_out;
1825
1826 const QString del_statement("-$(DEL_FILE)");
1827 const QString del_suffix =
1828 Option::target_mode == Option::TARG_OS2_MODE ?
1829 QString(" >nul 2>&1") : // reduce noise
1830 isForSymbian() ?
1831 QString(" 2> NUL") :
1832 QString::null;
1833
1834 if(tmp_clean.indexOf("${QMAKE_") == -1) {
1835 t << "\n\t" << del_statement << " " << tmp_clean << del_suffix;
1836 if (isForSymbian())
1837 t << " 2> NUL"; // Eliminate unnecessary warnings
1838 wrote_clean = true;
1839 }
1840 if(!wrote_clean_cmds || !wrote_clean) {
1841 QStringList cleans;
1842 if(!wrote_clean) {
1843 if(project->isActiveConfig("no_delete_multiple_files")) {
1844 for(QStringList::ConstIterator input = tmp_inputs.begin(); input != tmp_inputs.end(); ++input)
1845 cleans.append(" " + replaceExtraCompilerVariables(tmp_clean, (*input),
1846 replaceExtraCompilerVariables(tmp_out, (*input), QString())));
1847 } else {
1848 QString files, file;
1849 const int commandlineLimit =
1850 Option::target_mode == Option::TARG_OS2_MODE ?
1851 1000: // OS/2 CMD.EXE limit (1024 - suffix - reserve)
1852 2047; // NT limit, expanded
1853 for(int input = 0; input < tmp_inputs.size(); ++input) {
1854 file = " " + replaceExtraCompilerVariables(tmp_clean, tmp_inputs.at(input),
1855 replaceExtraCompilerVariables(tmp_out, tmp_inputs.at(input), QString()));
1856 if(del_statement.length() + files.length() +
1857 qMax(fixEnvVariables(file).length(), file.length()) > commandlineLimit) {
1858 cleans.append(files);
1859 files.clear();
1860 }
1861 files += file;
1862 }
1863 if(!files.isEmpty())
1864 cleans.append(files);
1865 }
1866 }
1867 if(!cleans.isEmpty())
1868 t << valGlue(cleans, "\n\t" + del_statement, del_suffix + "\n\t" + del_statement, del_suffix);
1869 if(!wrote_clean_cmds) {
1870 for(QStringList::ConstIterator input = tmp_inputs.begin(); input != tmp_inputs.end(); ++input) {
1871 t << "\n\t" << replaceExtraCompilerVariables(tmp_clean_cmds, (*input),
1872 replaceExtraCompilerVariables(tmp_out, (*input), QString()));
1873 }
1874 }
1875 }
1876 t << endl;
1877 }
1878 if(project->values((*it) + ".CONFIG").indexOf("combine") != -1) {
1879 if(tmp_out.indexOf("${QMAKE_") != -1) {
1880 warn_msg(WarnLogic, "QMAKE_EXTRA_COMPILERS(%s) with combine has variable output.",
1881 (*it).toLatin1().constData());
1882 continue;
1883 }
1884 QStringList deps, inputs;
1885 if(!tmp_dep.isEmpty())
1886 deps += fileFixify(tmp_dep, Option::output_dir, Option::output_dir);
1887 for(QStringList::ConstIterator input = tmp_inputs.begin(); input != tmp_inputs.end(); ++input) {
1888 deps += findDependencies((*input));
1889 inputs += Option::fixPathToTargetOS((*input), false);
1890 if(!tmp_dep_cmd.isEmpty() && doDepends()) {
1891 char buff[256];
1892 QString dep_cmd = replaceExtraCompilerVariables(tmp_dep_cmd, (*input),
1893 tmp_out);
1894 dep_cmd = fixEnvVariables(dep_cmd);
1895 if(FILE *proc = QT_POPEN(dep_cmd.toLatin1().constData(), "r")) {
1896 QString indeps;
1897 while(!feof(proc)) {
1898 int read_in = (int)fread(buff, 1, 255, proc);
1899 if(!read_in)
1900 break;
1901 indeps += QByteArray(buff, read_in);
1902 }
1903 QT_PCLOSE(proc);
1904 if(!indeps.isEmpty()) {
1905 QStringList dep_cmd_deps = indeps.replace('\n', ' ').simplified().split(' ');
1906 for(int i = 0; i < dep_cmd_deps.count(); ++i) {
1907 QString &file = dep_cmd_deps[i];
1908 if(!exists(file)) {
1909 QString localFile;
1910 QList<QMakeLocalFileName> depdirs = QMakeSourceFileInfo::dependencyPaths();
1911 for(QList<QMakeLocalFileName>::Iterator it = depdirs.begin();
1912 it != depdirs.end(); ++it) {
1913 if(exists((*it).real() + Option::dir_sep + file)) {
1914 localFile = (*it).local() + Option::dir_sep + file;
1915 break;
1916 }
1917 }
1918 file = localFile;
1919 }
1920 if(!file.isEmpty())
1921 file = fileFixify(file);
1922 }
1923 deps += dep_cmd_deps;
1924 }
1925 }
1926 }
1927 }
1928 for(int i = 0; i < inputs.size(); ) {
1929 if(tmp_out == inputs.at(i))
1930 inputs.removeAt(i);
1931 else
1932 ++i;
1933 }
1934 for(int i = 0; i < deps.size(); ) {
1935 if(tmp_out == deps.at(i))
1936 deps.removeAt(i);
1937 else
1938 ++i;
1939 }
1940 if (inputs.isEmpty())
1941 continue;
1942
1943 QString cmd;
1944 if (isForSymbianSbsv2()) {
1945 // In sbsv2 the command inputs and outputs need to use absolute paths
1946 cmd = replaceExtraCompilerVariables(tmp_cmd,
1947 fileFixify(escapeFilePaths(inputs), FileFixifyAbsolute),
1948 fileFixify(QStringList(tmp_out), FileFixifyAbsolute));
1949 } else {
1950 cmd = replaceExtraCompilerVariables(tmp_cmd, escapeFilePaths(inputs), QStringList(tmp_out));
1951 }
1952
1953 t << escapeDependencyPath(tmp_out) << ":";
1954 project->values(QLatin1String("QMAKE_INTERNAL_ET_PARSED_TARGETS.") + (*it)) << escapeDependencyPath(tmp_out);
1955 // compiler.CONFIG+=explicit_dependencies means that ONLY compiler.depends gets to cause Makefile dependencies
1956 if(project->values((*it) + ".CONFIG").indexOf("explicit_dependencies") != -1) {
1957 t << " " << valList(escapeDependencyPaths(fileFixify(tmp_dep, Option::output_dir, Option::output_dir)));
1958 project->values(QLatin1String("QMAKE_INTERNAL_ET_PARSED_DEPS.") + (*it) + escapeDependencyPath(tmp_out)) << tmp_dep;
1959 } else {
1960 t << " " << valList(escapeDependencyPaths(inputs)) << " " << valList(escapeDependencyPaths(deps));
1961 project->values(QLatin1String("QMAKE_INTERNAL_ET_PARSED_DEPS.") + (*it) + escapeDependencyPath(tmp_out)) << inputs << deps;
1962 }
1963 t << "\n\t" << cmd << endl << endl;
1964 project->values(QLatin1String("QMAKE_INTERNAL_ET_PARSED_CMD.") + (*it) + escapeDependencyPath(tmp_out)) << cmd;
1965 continue;
1966 }
1967 for(QStringList::ConstIterator input = tmp_inputs.begin(); input != tmp_inputs.end(); ++input) {
1968 QString in = Option::fixPathToTargetOS((*input), false);
1969 QStringList deps = findDependencies((*input));
1970 deps += escapeDependencyPath(in);
1971 QString out = replaceExtraCompilerVariables(tmp_out, (*input), QString());
1972 if(!tmp_dep.isEmpty()) {
1973 QStringList pre_deps = fileFixify(tmp_dep, Option::output_dir, Option::output_dir);
1974 for(int i = 0; i < pre_deps.size(); ++i)
1975 deps += replaceExtraCompilerVariables(pre_deps.at(i), (*input), out);
1976 }
1977 QString cmd = replaceExtraCompilerVariables(tmp_cmd, (*input), out);
1978 // NOTE: The var -> QMAKE_COMP_var replace feature is unsupported, do not use!
1979 if (isForSymbianSbsv2()) {
1980 // In sbsv2 the command inputs and outputs need to use absolute paths
1981 cmd = replaceExtraCompilerVariables(tmp_cmd,
1982 fileFixify((*input), FileFixifyAbsolute),
1983 fileFixify(out, FileFixifyAbsolute));
1984 } else {
1985 cmd = replaceExtraCompilerVariables(tmp_cmd, (*input), out);
1986 }
1987 for(QStringList::ConstIterator it3 = vars.constBegin(); it3 != vars.constEnd(); ++it3)
1988 cmd.replace("$(" + (*it3) + ")", "$(QMAKE_COMP_" + (*it3)+")");
1989 if(!tmp_dep_cmd.isEmpty() && doDepends()) {
1990 char buff[256];
1991 QString dep_cmd = replaceExtraCompilerVariables(tmp_dep_cmd, (*input), out);
1992 dep_cmd = fixEnvVariables(dep_cmd);
1993 if(FILE *proc = QT_POPEN(dep_cmd.toLatin1().constData(), "r")) {
1994 QString indeps;
1995 while(!feof(proc)) {
1996 int read_in = (int)fread(buff, 1, 255, proc);
1997 if(!read_in)
1998 break;
1999 indeps += QByteArray(buff, read_in);
2000 }
2001 QT_PCLOSE(proc);
2002 if(!indeps.isEmpty()) {
2003 QStringList dep_cmd_deps = indeps.replace('\n', ' ').simplified().split(' ');
2004 for(int i = 0; i < dep_cmd_deps.count(); ++i) {
2005 QString &file = dep_cmd_deps[i];
2006 if(!exists(file)) {
2007 QString localFile;
2008 QList<QMakeLocalFileName> depdirs = QMakeSourceFileInfo::dependencyPaths();
2009 for(QList<QMakeLocalFileName>::Iterator it = depdirs.begin();
2010 it != depdirs.end(); ++it) {
2011 if(exists((*it).real() + Option::dir_sep + file)) {
2012 localFile = (*it).local() + Option::dir_sep + file;
2013 break;
2014 }
2015 }
2016 file = localFile;
2017 }
2018 if(!file.isEmpty())
2019 file = fileFixify(file);
2020 }
2021 deps += dep_cmd_deps;
2022 }
2023 }
2024 //use the depend system to find includes of these included files
2025 QStringList inc_deps;
2026 for(int i = 0; i < deps.size(); ++i) {
2027 const QString dep = deps.at(i);
2028 if(QFile::exists(dep)) {
2029 SourceFileType type = TYPE_UNKNOWN;
2030 if(type == TYPE_UNKNOWN) {
2031 for(QStringList::Iterator cit = Option::c_ext.begin();
2032 cit != Option::c_ext.end(); ++cit) {
2033 if(dep.endsWith((*cit))) {
2034 type = TYPE_C;
2035 break;
2036 }
2037 }
2038 }
2039 if(type == TYPE_UNKNOWN) {
2040 for(QStringList::Iterator cppit = Option::cpp_ext.begin();
2041 cppit != Option::cpp_ext.end(); ++cppit) {
2042 if(dep.endsWith((*cppit))) {
2043 type = TYPE_C;
2044 break;
2045 }
2046 }
2047 }
2048 if(type == TYPE_UNKNOWN) {
2049 for(QStringList::Iterator hit = Option::h_ext.begin();
2050 type == TYPE_UNKNOWN && hit != Option::h_ext.end(); ++hit) {
2051 if(dep.endsWith((*hit))) {
2052 type = TYPE_C;
2053 break;
2054 }
2055 }
2056 }
2057 if(type != TYPE_UNKNOWN) {
2058 if(!QMakeSourceFileInfo::containsSourceFile(dep, type))
2059 QMakeSourceFileInfo::addSourceFile(dep, type);
2060 inc_deps += QMakeSourceFileInfo::dependencies(dep);
2061 }
2062 }
2063 }
2064 deps += inc_deps;
2065 }
2066 for(int i = 0; i < deps.size(); ) {
2067 QString &dep = deps[i];
2068 dep = Option::fixPathToTargetOS(unescapeFilePath(dep), false);
2069 if(out == dep)
2070 deps.removeAt(i);
2071 else
2072 ++i;
2073 }
2074 t << escapeDependencyPath(out) << ": " << valList(escapeDependencyPaths(deps)) << "\n\t"
2075 << cmd << endl << endl;
2076 project->values(QLatin1String("QMAKE_INTERNAL_ET_PARSED_TARGETS.") + (*it)) << escapeDependencyPath(out);
2077 project->values(QLatin1String("QMAKE_INTERNAL_ET_PARSED_DEPS.") + (*it) + escapeDependencyPath(out)) << deps;
2078 project->values(QLatin1String("QMAKE_INTERNAL_ET_PARSED_CMD.") + (*it) + escapeDependencyPath(out)) << cmd;
2079 }
2080 }
2081 t << "compiler_clean: " << clean_targets << endl << endl;
2082}
2083
2084void
2085MakefileGenerator::writeExtraCompilerVariables(QTextStream &t)
2086{
2087 bool first = true;
2088 const QStringList &quc = project->values("QMAKE_EXTRA_COMPILERS");
2089 for(QStringList::ConstIterator it = quc.begin(); it != quc.end(); ++it) {
2090 const QStringList &vars = project->values((*it) + ".variables");
2091 for(QStringList::ConstIterator varit = vars.begin(); varit != vars.end(); ++varit) {
2092 if(first) {
2093 t << "\n####### Custom Compiler Variables" << endl;
2094 first = false;
2095 }
2096 t << "QMAKE_COMP_" << (*varit) << " = "
2097 << valList(project->values((*varit))) << endl;
2098 }
2099 }
2100 if(!first)
2101 t << endl;
2102}
2103
2104void
2105MakefileGenerator::writeExtraVariables(QTextStream &t)
2106{
2107 bool first = true;
2108 QMap<QString, QStringList> &vars = project->variables();
2109 QStringList &exports = project->values("QMAKE_EXTRA_VARIABLES");
2110 for(QMap<QString, QStringList>::Iterator it = vars.begin(); it != vars.end(); ++it) {
2111 for(QStringList::Iterator exp_it = exports.begin(); exp_it != exports.end(); ++exp_it) {
2112 QRegExp rx((*exp_it), Qt::CaseInsensitive, QRegExp::Wildcard);
2113 if(rx.exactMatch(it.key())) {
2114 if(first) {
2115 t << "\n####### Custom Variables" << endl;
2116 first = false;
2117 }
2118 t << "EXPORT_" << it.key() << " = " << it.value().join(" ") << endl;
2119 }
2120 }
2121 }
2122 if(!first)
2123 t << endl;
2124}
2125
2126bool
2127MakefileGenerator::writeStubMakefile(QTextStream &t)
2128{
2129 t << "QMAKE = " << (project->isEmpty("QMAKE_QMAKE") ? QString("qmake") : var("QMAKE_QMAKE")) << endl;
2130 QStringList &qut = project->values("QMAKE_EXTRA_TARGETS");
2131 for(QStringList::ConstIterator it = qut.begin(); it != qut.end(); ++it)
2132 t << *it << " ";
2133 //const QString ofile = Option::fixPathToTargetOS(fileFixify(Option::output.fileName()));
2134 t << "first all clean install distclean uninstall: " << "qmake" << endl
2135 << "qmake_all:" << endl;
2136 writeMakeQmake(t);
2137 if(project->isEmpty("QMAKE_NOFORCE"))
2138 t << "FORCE:" << endl << endl;
2139 return true;
2140}
2141
2142bool
2143MakefileGenerator::writeMakefile(QTextStream &t)
2144{
2145 t << "####### Compile" << endl << endl;
2146 writeObj(t, "SOURCES");
2147 writeObj(t, "GENERATED_SOURCES");
2148
2149 t << "####### Install" << endl << endl;
2150 writeInstalls(t, "INSTALLS");
2151
2152 if(project->isEmpty("QMAKE_NOFORCE"))
2153 t << "FORCE:" << endl << endl;
2154 return true;
2155}
2156
2157QString MakefileGenerator::buildArgs(const QString &outdir)
2158{
2159 QString ret;
2160 //special variables
2161 if(!project->isEmpty("QMAKE_ABSOLUTE_SOURCE_PATH"))
2162 ret += " QMAKE_ABSOLUTE_SOURCE_PATH=" + escapeFilePath(project->first("QMAKE_ABSOLUTE_SOURCE_PATH"));
2163
2164 //warnings
2165 else if(Option::warn_level == WarnNone)
2166 ret += " -Wnone";
2167 else if(Option::warn_level == WarnAll)
2168 ret += " -Wall";
2169 else if(Option::warn_level & WarnParser)
2170 ret += " -Wparser";
2171 //other options
2172 if(!Option::user_template.isEmpty())
2173 ret += " -t " + Option::user_template;
2174 if(!Option::user_template_prefix.isEmpty())
2175 ret += " -tp " + Option::user_template_prefix;
2176 if(!Option::mkfile::do_cache)
2177 ret += " -nocache";
2178 if(!Option::mkfile::do_deps)
2179 ret += " -nodepend";
2180 if(!Option::mkfile::do_dep_heuristics)
2181 ret += " -nodependheuristics";
2182 if(!Option::mkfile::qmakespec_commandline.isEmpty())
2183 ret += " -spec " + specdir(outdir);
2184 if(Option::target_mode == Option::TARG_MAC9_MODE)
2185 ret += " -mac9";
2186 else if(Option::target_mode == Option::TARG_MACX_MODE)
2187 ret += " -macx";
2188 else if(Option::target_mode == Option::TARG_UNIX_MODE)
2189 ret += " -unix";
2190 else if(Option::target_mode == Option::TARG_WIN_MODE)
2191 ret += " -win32";
2192 else if(Option::target_mode == Option::TARG_OS2_MODE)
2193 ret += " -os2";
2194
2195 //configs
2196 for(QStringList::Iterator it = Option::user_configs.begin();
2197 it != Option::user_configs.end(); ++it)
2198 ret += " -config " + (*it);
2199 //arguments
2200 for(QStringList::Iterator it = Option::before_user_vars.begin();
2201 it != Option::before_user_vars.end(); ++it) {
2202 if((*it).left(qstrlen("QMAKE_ABSOLUTE_SOURCE_PATH")) != "QMAKE_ABSOLUTE_SOURCE_PATH")
2203 ret += " " + escapeFilePath((*it));
2204 }
2205 if(Option::after_user_vars.count()) {
2206 ret += " -after ";
2207 for(QStringList::Iterator it = Option::after_user_vars.begin();
2208 it != Option::after_user_vars.end(); ++it) {
2209 if((*it).left(qstrlen("QMAKE_ABSOLUTE_SOURCE_PATH")) != "QMAKE_ABSOLUTE_SOURCE_PATH")
2210 ret += " " + escapeFilePath((*it));
2211 }
2212 }
2213 return ret;
2214}
2215
2216//could get stored argv, but then it would have more options than are
2217//probably necesary this will try to guess the bare minimum..
2218QString MakefileGenerator::build_args(const QString &outdir)
2219{
2220 QString ret = "$(QMAKE)";
2221
2222 // general options and arguments
2223 ret += buildArgs(outdir);
2224
2225 //output
2226 QString ofile = Option::fixPathToTargetOS(fileFixify(Option::output.fileName()));
2227 if(!ofile.isEmpty() && ofile != project->first("QMAKE_MAKEFILE"))
2228 ret += " -o " + escapeFilePath(ofile);
2229
2230 //inputs
2231 ret += " " + escapeFilePath(fileFixify(project->projectFile(), outdir));
2232
2233 return ret;
2234}
2235
2236void
2237MakefileGenerator::writeHeader(QTextStream &t)
2238{
2239 t << "#############################################################################" << endl;
2240 t << "# Makefile for building: " << escapeFilePath(var("TARGET")) << endl;
2241 t << "# Generated by qmake (" << qmake_version() << ") (Qt " << QT_VERSION_STR << ") on: ";
2242 t << QDateTime::currentDateTime().toString() << endl;
2243 t << "# Project: " << fileFixify(project->projectFile()) << endl;
2244 t << "# Template: " << var("TEMPLATE") << endl;
2245 if(!project->isActiveConfig("build_pass"))
2246 t << "# Command: " << build_args().replace("$(QMAKE)",
2247 (project->isEmpty("QMAKE_QMAKE") ? QString("qmake") : var("QMAKE_QMAKE"))) << endl;
2248 t << "#############################################################################" << endl;
2249 t << endl;
2250}
2251
2252QList<MakefileGenerator::SubTarget*>
2253MakefileGenerator::findSubDirsSubTargets() const
2254{
2255 QList<SubTarget*> targets;
2256 {
2257 const QStringList subdirs = project->values("SUBDIRS");
2258 for(int subdir = 0; subdir < subdirs.size(); ++subdir) {
2259 QString fixedSubdir = subdirs[subdir];
2260 fixedSubdir = fixedSubdir.replace(QRegExp("[^a-zA-Z0-9_]"),"-");
2261
2262 SubTarget *st = new SubTarget;
2263 st->name = subdirs[subdir];
2264 targets.append(st);
2265
2266 bool fromFile = false;
2267 QString file = subdirs[subdir];
2268 if(!project->isEmpty(fixedSubdir + ".file")) {
2269 if(!project->isEmpty(fixedSubdir + ".subdir"))
2270 warn_msg(WarnLogic, "Cannot assign both file and subdir for subdir %s",
2271 subdirs[subdir].toLatin1().constData());
2272 file = project->first(fixedSubdir + ".file");
2273 fromFile = true;
2274 } else if(!project->isEmpty(fixedSubdir + ".subdir")) {
2275 file = project->first(fixedSubdir + ".subdir");
2276 fromFile = false;
2277 } else {
2278 fromFile = file.endsWith(Option::pro_ext);
2279 }
2280 file = Option::fixPathToTargetOS(file);
2281
2282 if(fromFile) {
2283 int slsh = file.lastIndexOf(Option::dir_sep);
2284 if(slsh != -1) {
2285 st->in_directory = file.left(slsh+1);
2286 st->profile = file.mid(slsh+1);
2287 } else {
2288 st->profile = file;
2289 }
2290 } else {
2291 if(!file.isEmpty() && !project->isActiveConfig("subdir_first_pro"))
2292 st->profile = file.section(Option::dir_sep, -1) + Option::pro_ext;
2293 st->in_directory = file;
2294 }
2295 while(st->in_directory.endsWith(Option::dir_sep))
2296 st->in_directory.chop(1);
2297 if(fileInfo(st->in_directory).isRelative())
2298 st->out_directory = st->in_directory;
2299 else
2300 st->out_directory = fileFixify(st->in_directory, qmake_getpwd(), Option::output_dir);
2301 if(!project->isEmpty(fixedSubdir + ".makefile")) {
2302 st->makefile = project->first(fixedSubdir + ".makefile");
2303 } else {
2304 st->makefile = "$(MAKEFILE)";
2305 if(!st->profile.isEmpty()) {
2306 QString basename = st->in_directory;
2307 int new_slsh = basename.lastIndexOf(Option::dir_sep);
2308 if(new_slsh != -1)
2309 basename = basename.mid(new_slsh+1);
2310 if(st->profile != basename + Option::pro_ext)
2311 st->makefile += "." + st->profile.left(st->profile.length() - Option::pro_ext.length());
2312 }
2313 }
2314 if(!project->isEmpty(fixedSubdir + ".depends")) {
2315 const QStringList depends = project->values(fixedSubdir + ".depends");
2316 for(int depend = 0; depend < depends.size(); ++depend) {
2317 bool found = false;
2318 for(int subDep = 0; subDep < subdirs.size(); ++subDep) {
2319 if(subdirs[subDep] == depends.at(depend)) {
2320 QString fixedSubDep = subdirs[subDep];
2321 fixedSubDep = fixedSubDep.replace(QRegExp("[^a-zA-Z0-9_]"),"-");
2322 if(!project->isEmpty(fixedSubDep + ".target")) {
2323 st->depends += project->first(fixedSubDep + ".target");
2324 } else {
2325 QString d = Option::fixPathToLocalOS(subdirs[subDep]);
2326 if(!project->isEmpty(fixedSubDep + ".file"))
2327 d = project->first(fixedSubDep + ".file");
2328 else if(!project->isEmpty(fixedSubDep + ".subdir"))
2329 d = project->first(fixedSubDep + ".subdir");
2330 st->depends += "sub-" + d.replace(QRegExp("[^a-zA-Z0-9_]"),"-");
2331 }
2332 found = true;
2333 break;
2334 }
2335 }
2336 if(!found) {
2337 QString depend_str = depends.at(depend);
2338 st->depends += depend_str.replace(QRegExp("[^a-zA-Z0-9_]"),"-");
2339 }
2340 }
2341 }
2342 if(!project->isEmpty(fixedSubdir + ".target")) {
2343 st->target = project->first(fixedSubdir + ".target");
2344 } else {
2345 st->target = "sub-" + file;
2346 st->target = st->target.replace(QRegExp("[^a-zA-Z0-9_]"),"-");
2347 }
2348 }
2349 }
2350 return targets;
2351}
2352
2353void
2354MakefileGenerator::writeSubDirs(QTextStream &t)
2355{
2356 QList<SubTarget*> targets = findSubDirsSubTargets();
2357 t << "first: make_default" << endl;
2358 int flags = SubTargetInstalls;
2359 if(project->isActiveConfig("ordered"))
2360 flags |= SubTargetOrdered;
2361 writeSubTargets(t, targets, flags);
2362 qDeleteAll(targets);
2363}
2364
2365void
2366MakefileGenerator::writeSubTargets(QTextStream &t, QList<MakefileGenerator::SubTarget*> targets, int flags)
2367{
2368 // blasted includes
2369 QStringList &qeui = project->values("QMAKE_EXTRA_INCLUDES");
2370 for(QStringList::Iterator qeui_it = qeui.begin(); qeui_it != qeui.end(); ++qeui_it)
2371 t << "include " << (*qeui_it) << endl;
2372
2373 if (!(flags & SubTargetSkipDefaultVariables)) {
2374 QString ofile = Option::fixPathToTargetOS(Option::output.fileName());
2375 if(ofile.lastIndexOf(Option::dir_sep) != -1)
2376 ofile.remove(0, ofile.lastIndexOf(Option::dir_sep) +1);
2377 t << "MAKEFILE = " << ofile << endl;
2378 /* Calling Option::fixPathToTargetOS() is necessary for MinGW/MSYS, which requires
2379 * back-slashes to be turned into slashes. */
2380 t << "QMAKE = " << Option::fixPathToTargetOS(var("QMAKE_QMAKE")) << endl;
2381 t << "DEL_FILE = " << var("QMAKE_DEL_FILE") << endl;
2382 t << "CHK_DIR_EXISTS= " << var("QMAKE_CHK_DIR_EXISTS") << endl;
2383 t << "MKDIR = " << var("QMAKE_MKDIR") << endl;
2384 t << "COPY = " << var("QMAKE_COPY") << endl;
2385 t << "COPY_FILE = " << var("QMAKE_COPY_FILE") << endl;
2386 t << "COPY_DIR = " << var("QMAKE_COPY_DIR") << endl;
2387 t << "INSTALL_FILE = " << var("QMAKE_INSTALL_FILE") << endl;
2388 t << "INSTALL_PROGRAM = " << var("QMAKE_INSTALL_PROGRAM") << endl;
2389 t << "INSTALL_DIR = " << var("QMAKE_INSTALL_DIR") << endl;
2390 t << "DEL_FILE = " << var("QMAKE_DEL_FILE") << endl;
2391 t << "SYMLINK = " << var("QMAKE_SYMBOLIC_LINK") << endl;
2392 t << "DEL_DIR = " << var("QMAKE_DEL_DIR") << endl;
2393 t << "MOVE = " << var("QMAKE_MOVE") << endl;
2394 t << "CHK_DIR_EXISTS= " << var("QMAKE_CHK_DIR_EXISTS") << endl;
2395 t << "MKDIR = " << var("QMAKE_MKDIR") << endl;
2396 t << "SUBTARGETS = "; // subtargets are sub-directory
2397 for(int target = 0; target < targets.size(); ++target)
2398 t << " \\\n\t\t" << targets.at(target)->target;
2399 t << endl << endl;
2400 }
2401 writeExtraVariables(t);
2402
2403 QStringList targetSuffixes;
2404 const QString abs_source_path = project->first("QMAKE_ABSOLUTE_SOURCE_PATH");
2405 if (!(flags & SubTargetSkipDefaultTargets)) {
2406 targetSuffixes << "make_default" << "make_first" << "all" << "clean" << "distclean"
2407 << QString((flags & SubTargetInstalls) ? "install_subtargets" : "install")
2408 << QString((flags & SubTargetInstalls) ? "uninstall_subtargets" : "uninstall");
2409 }
2410
2411 // generate target rules
2412 for(int target = 0; target < targets.size(); ++target) {
2413 SubTarget *subtarget = targets.at(target);
2414 QString in_directory = subtarget->in_directory;
2415 if(!in_directory.isEmpty() && !in_directory.endsWith(Option::dir_sep))
2416 in_directory += Option::dir_sep;
2417 QString out_directory = subtarget->out_directory;
2418 if(!out_directory.isEmpty() && !out_directory.endsWith(Option::dir_sep))
2419 out_directory += Option::dir_sep;
2420 if(!abs_source_path.isEmpty() && out_directory.startsWith(abs_source_path))
2421 out_directory = Option::output_dir + out_directory.mid(abs_source_path.length());
2422
2423 QString mkfile = subtarget->makefile;
2424 if(!in_directory.isEmpty())
2425 mkfile.prepend(out_directory);
2426
2427 QString in_directory_cdin, in_directory_cdout, out_directory_cdin, out_directory_cdout;
2428#define MAKE_CD_IN_AND_OUT(directory) \
2429 if(!directory.isEmpty()) { \
2430 if(project->isActiveConfig("cd_change_global")) { \
2431 directory ## _cdin = "\n\tcd " + directory + "\n\t"; \
2432 QDir pwd(Option::output_dir); \
2433 QStringList in = directory.split(Option::dir_sep), out; \
2434 for(int i = 0; i < in.size(); i++) { \
2435 if(in.at(i) == "..") \
2436 out.prepend(fileInfo(pwd.path()).fileName()); \
2437 else if(in.at(i) != ".") \
2438 out.prepend(".."); \
2439 pwd.cd(in.at(i)); \
2440 } \
2441 directory ## _cdout = "\n\t@cd " + escapeFilePath(out.join(Option::dir_sep)); \
2442 } else { \
2443 directory ## _cdin = "\n\tcd " + escapeFilePath(directory) + " && "; \
2444 } \
2445 } else { \
2446 directory ## _cdin = "\n\t"; \
2447 }
2448 MAKE_CD_IN_AND_OUT(in_directory);
2449 MAKE_CD_IN_AND_OUT(out_directory);
2450
2451 //qmake it
2452 if(!subtarget->profile.isEmpty()) {
2453 QString out = out_directory + subtarget->makefile,
2454 in = fileFixify(in_directory + subtarget->profile, in_directory);
2455 if(in.startsWith(in_directory))
2456 in = in.mid(in_directory.length());
2457 if(out.startsWith(out_directory))
2458 out = out.mid(out_directory.length());
2459 t << mkfile << ": " << "\n\t";
2460 if(!in_directory.isEmpty()) {
2461 t << mkdir_p_asstring(in_directory)
2462 << in_directory_cdin
2463 << "$(QMAKE) " << in << buildArgs(in_directory) << " -o " << out
2464 << in_directory_cdout << endl;
2465 } else {
2466 t << "$(QMAKE) " << in << buildArgs(in_directory) << " -o " << out << endl;
2467 }
2468 t << subtarget->target << "-qmake_all: ";
2469 if(project->isEmpty("QMAKE_NOFORCE"))
2470 t << " FORCE";
2471 t << "\n\t";
2472 if(!in_directory.isEmpty()) {
2473 t << mkdir_p_asstring(in_directory)
2474 << in_directory_cdin
2475 << "$(QMAKE) " << in << buildArgs(in_directory) << " -o " << out
2476 << in_directory_cdout << endl;
2477 } else {
2478 t << "$(QMAKE) " << in << buildArgs(in_directory) << " -o " << out << endl;
2479 }
2480 }
2481
2482 QString makefilein = " -f " + subtarget->makefile;
2483
2484 { //actually compile
2485 t << subtarget->target << ": " << mkfile;
2486 if(!subtarget->depends.isEmpty())
2487 t << " " << valList(subtarget->depends);
2488 if(project->isEmpty("QMAKE_NOFORCE"))
2489 t << " FORCE";
2490 t << out_directory_cdin
2491 << "$(MAKE)" << makefilein
2492 << out_directory_cdout << endl;
2493 }
2494
2495 for(int suffix = 0; suffix < targetSuffixes.size(); ++suffix) {
2496 QString s = targetSuffixes.at(suffix);
2497 if(s == "install_subtargets")
2498 s = "install";
2499 else if(s == "uninstall_subtargets")
2500 s = "uninstall";
2501 else if(s == "make_first")
2502 s = "first";
2503 else if(s == "make_default")
2504 s = QString();
2505
2506 if(flags & SubTargetOrdered) {
2507 t << subtarget->target << "-" << targetSuffixes.at(suffix) << "-ordered: " << mkfile;
2508 if(target)
2509 t << " " << targets.at(target-1)->target << "-" << targetSuffixes.at(suffix) << "-ordered ";
2510 if(project->isEmpty("QMAKE_NOFORCE"))
2511 t << " FORCE";
2512 t << out_directory_cdin
2513 << "$(MAKE)" << makefilein << " " << s
2514 << out_directory_cdout << endl;
2515 }
2516 t << subtarget->target << "-" << targetSuffixes.at(suffix) << ": " << mkfile;
2517 if(!subtarget->depends.isEmpty())
2518 t << " " << valGlue(subtarget->depends, QString(), "-" + targetSuffixes.at(suffix) + " ",
2519 "-"+targetSuffixes.at(suffix));
2520 if(project->isEmpty("QMAKE_NOFORCE"))
2521 t << " FORCE";
2522 t << out_directory_cdin
2523 << "$(MAKE)" << makefilein << " " << s
2524 << out_directory_cdout << endl;
2525 }
2526 }
2527 t << endl;
2528
2529 if (!(flags & SubTargetSkipDefaultTargets)) {
2530 if(project->values("QMAKE_INTERNAL_QMAKE_DEPS").indexOf("qmake_all") == -1)
2531 project->values("QMAKE_INTERNAL_QMAKE_DEPS").append("qmake_all");
2532
2533 writeMakeQmake(t);
2534
2535 t << "qmake_all:";
2536 if(!targets.isEmpty()) {
2537 for(QList<SubTarget*>::Iterator it = targets.begin(); it != targets.end(); ++it) {
2538 if(!(*it)->profile.isEmpty())
2539 t << " " << (*it)->target << "-" << "qmake_all";
2540 }
2541 }
2542 if(project->isEmpty("QMAKE_NOFORCE"))
2543 t << " FORCE";
2544 if(project->isActiveConfig("no_empty_targets"))
2545 t << "\n\t" << "@cd .";
2546 t << endl << endl;
2547 }
2548
2549 for(int s = 0; s < targetSuffixes.size(); ++s) {
2550 QString suffix = targetSuffixes.at(s);
2551 if(!(flags & SubTargetInstalls) && suffix.endsWith("install"))
2552 continue;
2553
2554 t << suffix << ":";
2555 for(int target = 0; target < targets.size(); ++target) {
2556 SubTarget *subTarget = targets.at(target);
2557 if((suffix == "make_first" || suffix == "make_default")
2558 && project->values(subTarget->name + ".CONFIG").indexOf("no_default_target") != -1) {
2559 continue;
2560 }
2561 QString targetRule = subTarget->target + "-" + suffix;
2562 if(flags & SubTargetOrdered)
2563 targetRule += "-ordered";
2564 t << " " << targetRule;
2565 }
2566 if(suffix == "all" || suffix == "make_first")
2567 t << varGlue("ALL_DEPS"," "," ","");
2568 if(suffix == "clean")
2569 t << varGlue("CLEAN_DEPS"," "," ","");
2570 if(project->isEmpty("QMAKE_NOFORCE"))
2571 t << " FORCE";
2572 t << endl;
2573 const QString del_suffix =
2574 Option::target_mode == Option::TARG_OS2_MODE ?
2575 QString(" >nul 2>&1"): // reduce noise
2576 QString::null;
2577 if(suffix == "clean") {
2578 t << varGlue("QMAKE_CLEAN","\t-$(DEL_FILE) ",del_suffix+"\n\t-$(DEL_FILE) ", del_suffix) << endl;
2579 } else if(suffix == "distclean") {
2580 QString ofile = Option::fixPathToTargetOS(fileFixify(Option::output.fileName()));
2581 if(!ofile.isEmpty())
2582 t << "\t-$(DEL_FILE) " << ofile << del_suffix << endl;
2583 } else if(project->isActiveConfig("no_empty_targets")) {
2584 t << "\t" << "@cd ." << endl;
2585 }
2586 }
2587
2588 // user defined targets
2589 QStringList &qut = project->values("QMAKE_EXTRA_TARGETS");
2590 for(QStringList::Iterator qut_it = qut.begin(); qut_it != qut.end(); ++qut_it) {
2591 QString targ = var((*qut_it) + ".target"),
2592 cmd = var((*qut_it) + ".commands"), deps;
2593 if(targ.isEmpty())
2594 targ = (*qut_it);
2595 t << endl;
2596
2597 QStringList &deplist = project->values((*qut_it) + ".depends");
2598 for(QStringList::Iterator dep_it = deplist.begin(); dep_it != deplist.end(); ++dep_it) {
2599 QString dep = var((*dep_it) + ".target");
2600 if(dep.isEmpty())
2601 dep = Option::fixPathToTargetOS(*dep_it, false);
2602 deps += " " + dep;
2603 }
2604 if(project->values((*qut_it) + ".CONFIG").indexOf("recursive") != -1) {
2605 QSet<QString> recurse;
2606 if(project->isSet((*qut_it) + ".recurse")) {
2607 recurse = project->values((*qut_it) + ".recurse").toSet();
2608 } else {
2609 for(int target = 0; target < targets.size(); ++target)
2610 recurse.insert(targets.at(target)->name);
2611 }
2612 for(int target = 0; target < targets.size(); ++target) {
2613 SubTarget *subtarget = targets.at(target);
2614 QString in_directory = subtarget->in_directory;
2615 if(!in_directory.isEmpty() && !in_directory.endsWith(Option::dir_sep))
2616 in_directory += Option::dir_sep;
2617 QString out_directory = subtarget->out_directory;
2618 if(!out_directory.isEmpty() && !out_directory.endsWith(Option::dir_sep))
2619 out_directory += Option::dir_sep;
2620 if(!abs_source_path.isEmpty() && out_directory.startsWith(abs_source_path))
2621 out_directory = Option::output_dir + out_directory.mid(abs_source_path.length());
2622
2623 if(!recurse.contains(subtarget->name))
2624 continue;
2625 QString mkfile = subtarget->makefile;
2626 if(!in_directory.isEmpty()) {
2627 if(!out_directory.endsWith(Option::dir_sep))
2628 mkfile.prepend(out_directory + Option::dir_sep);
2629 else
2630 mkfile.prepend(out_directory);
2631 }
2632 QString out_directory_cdin, out_directory_cdout;
2633 MAKE_CD_IN_AND_OUT(out_directory);
2634
2635 // note that we always pass the makefile as argument since it's
2636 // hard to tell if it matches the platform make's default
2637 // file name or not (and it can be also specified indirectly
2638 // through $(MAKEFILE))
2639 QString makefilein = " -f " + subtarget->makefile;
2640
2641 //write the rule/depends
2642 if(flags & SubTargetOrdered) {
2643 const QString dep = subtarget->target + "-" + (*qut_it) + "_ordered";
2644 t << dep << ": " << mkfile;
2645 if(target)
2646 t << " " << targets.at(target-1)->target << "-" << (*qut_it) << "_ordered ";
2647 deps += " " + dep;
2648 } else {
2649 const QString dep = subtarget->target + "-" + (*qut_it);
2650 t << dep << ": " << mkfile;
2651 if(!subtarget->depends.isEmpty())
2652 t << " " << valGlue(subtarget->depends, QString(), "-" + (*qut_it) + " ", "-" + (*qut_it));
2653 deps += " " + dep;
2654 }
2655
2656 QString sub_targ = targ;
2657 if(project->isSet((*qut_it) + ".recurse_target"))
2658 sub_targ = project->first((*qut_it) + ".recurse_target");
2659
2660 //write the commands
2661 if(!out_directory.isEmpty()) {
2662 t << out_directory_cdin
2663 << "$(MAKE)" << makefilein << " " << sub_targ
2664 << out_directory_cdout << endl;
2665 } else {
2666 t << "\n\t"
2667 << "$(MAKE)" << makefilein << " " << sub_targ << endl;
2668 }
2669 }
2670 }
2671 if(project->isEmpty("QMAKE_NOFORCE") &&
2672 project->values((*qut_it) + ".CONFIG").indexOf("phony") != -1)
2673 deps += " FORCE";
2674 t << targ << ":" << deps << "\n";
2675 if(!cmd.isEmpty())
2676 t << "\t" << cmd << endl;
2677 }
2678
2679 if(flags & SubTargetInstalls) {
2680 project->values("INSTALLDEPS") += "install_subtargets";
2681 project->values("UNINSTALLDEPS") += "uninstall_subtargets";
2682 writeInstalls(t, "INSTALLS", true);
2683 }
2684
2685 if(project->isEmpty("QMAKE_NOFORCE"))
2686 t << "FORCE:" << endl << endl;
2687}
2688
2689void
2690MakefileGenerator::writeMakeQmake(QTextStream &t)
2691{
2692 QString ofile = Option::fixPathToTargetOS(fileFixify(Option::output.fileName()));
2693 if(project->isEmpty("QMAKE_FAILED_REQUIREMENTS") && !project->isEmpty("QMAKE_INTERNAL_PRL_FILE")) {
2694 QStringList files = fileFixify(Option::mkfile::project_files);
2695 t << escapeDependencyPath(project->first("QMAKE_INTERNAL_PRL_FILE")) << ": " << "\n\t"
2696 << "@$(QMAKE) -prl " << buildArgs() << " " << files.join(" ") << endl;
2697 }
2698
2699 QString pfile = project->projectFile();
2700 if(pfile != "(stdin)") {
2701 QString qmake = build_args();
2702 if(!ofile.isEmpty() && !project->isActiveConfig("no_autoqmake")) {
2703 t << escapeFilePath(ofile) << ": " << escapeDependencyPath(fileFixify(pfile)) << " ";
2704 if(Option::mkfile::do_cache)
2705 t << escapeDependencyPath(fileFixify(Option::mkfile::cachefile)) << " ";
2706 if(!specdir().isEmpty()) {
2707 if(exists(Option::fixPathToLocalOS(specdir()+QDir::separator()+"qmake.conf")))
2708 t << escapeDependencyPath(specdir() + Option::dir_sep + "qmake.conf") << " ";
2709 else if(exists(Option::fixPathToLocalOS(specdir()+QDir::separator()+"tmake.conf")))
2710 t << escapeDependencyPath(specdir() + Option::dir_sep + "tmake.conf") << " ";
2711 }
2712 const QStringList &included = project->values("QMAKE_INTERNAL_INCLUDED_FILES");
2713 t << escapeDependencyPaths(included).join(" \\\n\t\t") << "\n\t"
2714 << qmake << endl;
2715 for(int include = 0; include < included.size(); ++include) {
2716 const QString i(included.at(include));
2717 if(!i.isEmpty())
2718 t << i << ":" << endl;
2719 }
2720 }
2721 if(project->first("QMAKE_ORIG_TARGET") != "qmake") {
2722 t << "qmake: " <<
2723 project->values("QMAKE_INTERNAL_QMAKE_DEPS").join(" \\\n\t\t");
2724 if(project->isEmpty("QMAKE_NOFORCE"))
2725 t << " FORCE";
2726 t << "\n\t" << "@" << qmake << endl << endl;
2727 }
2728 }
2729}
2730
2731QFileInfo
2732MakefileGenerator::fileInfo(QString file) const
2733{
2734 static QHash<FileInfoCacheKey, QFileInfo> *cache = 0;
2735 static QFileInfo noInfo = QFileInfo();
2736 if(!cache) {
2737 cache = new QHash<FileInfoCacheKey, QFileInfo>;
2738 qmakeAddCacheClear(qmakeDeleteCacheClear_QHashFileInfoCacheKeyQFileInfo, (void**)&cache);
2739 }
2740 FileInfoCacheKey cacheKey(file);
2741 QFileInfo value = cache->value(cacheKey, noInfo);
2742 if (value != noInfo)
2743 return value;
2744
2745 QFileInfo fi(file);
2746 if (fi.exists())
2747 cache->insert(cacheKey, fi);
2748 return fi;
2749}
2750
2751QString
2752MakefileGenerator::unescapeFilePath(const QString &path) const
2753{
2754 QString ret = path;
2755 if(!ret.isEmpty()) {
2756 if(ret.contains(QLatin1String("\\ ")))
2757 ret.replace(QLatin1String("\\ "), QLatin1String(" "));
2758 if(ret.contains(QLatin1Char('\"')))
2759 ret.remove(QLatin1Char('\"'));
2760 }
2761 return ret;
2762}
2763
2764QStringList
2765MakefileGenerator::escapeFilePaths(const QStringList &paths) const
2766{
2767 QStringList ret;
2768 for(int i = 0; i < paths.size(); ++i)
2769 ret.append(escapeFilePath(paths.at(i)));
2770 return ret;
2771}
2772
2773QStringList
2774MakefileGenerator::escapeDependencyPaths(const QStringList &paths) const
2775{
2776 QStringList ret;
2777 for(int i = 0; i < paths.size(); ++i)
2778 ret.append(escapeDependencyPath(paths.at(i)));
2779 return ret;
2780}
2781
2782QStringList
2783MakefileGenerator::unescapeFilePaths(const QStringList &paths) const
2784{
2785 QStringList ret;
2786 for(int i = 0; i < paths.size(); ++i)
2787 ret.append(unescapeFilePath(paths.at(i)));
2788 return ret;
2789}
2790
2791QStringList
2792MakefileGenerator::fileFixify(const QStringList& files, const QString &out_dir, const QString &in_dir,
2793 FileFixifyType fix, bool canon) const
2794{
2795 if(files.isEmpty())
2796 return files;
2797 QStringList ret;
2798 for(QStringList::ConstIterator it = files.begin(); it != files.end(); ++it) {
2799 if(!(*it).isEmpty())
2800 ret << fileFixify((*it), out_dir, in_dir, fix, canon);
2801 }
2802 return ret;
2803}
2804
2805QString
2806MakefileGenerator::fileFixify(const QString& file, const QString &out_d, const QString &in_d,
2807 FileFixifyType fix, bool canon) const
2808{
2809 if(file.isEmpty())
2810 return file;
2811 QString ret = unescapeFilePath(file);
2812
2813 //setup the cache
2814 static QHash<FileFixifyCacheKey, QString> *cache = 0;
2815 if(!cache) {
2816 cache = new QHash<FileFixifyCacheKey, QString>;
2817 qmakeAddCacheClear(qmakeDeleteCacheClear_QHashFileFixifyCacheKeyQString, (void**)&cache);
2818 }
2819 FileFixifyCacheKey cacheKey(ret, out_d, in_d, fix, canon);
2820 QString cacheVal = cache->value(cacheKey);
2821 if(!cacheVal.isNull())
2822 return cacheVal;
2823
2824 //do the fixin'
2825 QString pwd = qmake_getpwd();
2826 if (!pwd.endsWith('/'))
2827 pwd += '/';
2828 QString orig_file = ret;
2829 if(ret.startsWith(QLatin1Char('~'))) {
2830 if(ret.startsWith(QLatin1String("~/")))
2831 ret = QDir::homePath() + ret.mid(1);
2832 else
2833 warn_msg(WarnLogic, "Unable to expand ~ in %s", ret.toLatin1().constData());
2834 }
2835 if(fix == FileFixifyAbsolute || (fix == FileFixifyDefault && project->isActiveConfig("no_fixpath"))) {
2836 if(fix == FileFixifyAbsolute && QDir::isRelativePath(ret)) //already absolute
2837 ret.prepend(pwd);
2838 ret = Option::fixPathToTargetOS(ret, false, canon);
2839 } else { //fix it..
2840 QString out_dir = QDir(Option::output_dir).absoluteFilePath(out_d);
2841 QString in_dir = QDir(pwd).absoluteFilePath(in_d);
2842 {
2843 QFileInfo in_fi(fileInfo(in_dir));
2844 if(in_fi.exists())
2845 in_dir = in_fi.canonicalFilePath();
2846 QFileInfo out_fi(fileInfo(out_dir));
2847 if(out_fi.exists())
2848 out_dir = out_fi.canonicalFilePath();
2849 }
2850
2851 QString qfile(Option::fixPathToLocalOS(ret, true, canon));
2852 QFileInfo qfileinfo(fileInfo(qfile));
2853 if(out_dir != in_dir || !qfileinfo.isRelative()) {
2854 if(qfileinfo.isRelative()) {
2855 ret = in_dir + "/" + qfile;
2856 qfileinfo.setFile(ret);
2857 }
2858 ret = Option::fixPathToTargetOS(ret, false, canon);
2859 if(canon && qfileinfo.exists() &&
2860 file == Option::fixPathToTargetOS(ret, true, canon))
2861 ret = Option::fixPathToTargetOS(qfileinfo.canonicalFilePath());
2862 QString match_dir = Option::fixPathToTargetOS(out_dir, false, canon);
2863 if(ret == match_dir) {
2864 ret = "";
2865 } else if(ret.startsWith(match_dir + Option::dir_sep)) {
2866 ret = ret.mid(match_dir.length() + Option::dir_sep.length());
2867 } else {
2868 //figure out the depth
2869 int depth = 4;
2870 if(Option::qmake_mode == Option::QMAKE_GENERATE_MAKEFILE ||
2871 Option::qmake_mode == Option::QMAKE_GENERATE_PRL) {
2872 if(project && !project->isEmpty("QMAKE_PROJECT_DEPTH"))
2873 depth = project->first("QMAKE_PROJECT_DEPTH").toInt();
2874 else if(Option::mkfile::cachefile_depth != -1)
2875 depth = Option::mkfile::cachefile_depth;
2876 }
2877 //calculate how much can be removed
2878 QString dot_prefix;
2879 for(int i = 1; i <= depth; i++) {
2880 int sl = match_dir.lastIndexOf(Option::dir_sep);
2881 if(sl == -1)
2882 break;
2883 match_dir = match_dir.left(sl);
2884 if(match_dir.isEmpty())
2885 break;
2886 if(ret.startsWith(match_dir + Option::dir_sep)) {
2887 //concat
2888 int remlen = ret.length() - (match_dir.length() + 1);
2889 if(remlen < 0)
2890 remlen = 0;
2891 ret = ret.right(remlen);
2892 //prepend
2893 for(int o = 0; o < i; o++)
2894 dot_prefix += ".." + Option::dir_sep;
2895 }
2896 }
2897 ret.prepend(dot_prefix);
2898 }
2899 } else {
2900 ret = Option::fixPathToTargetOS(ret, false, canon);
2901 }
2902 }
2903 if(ret.isEmpty())
2904 ret = ".";
2905 debug_msg(3, "Fixed[%d,%d] %s :: to :: %s [%s::%s] [%s::%s]", fix, canon, orig_file.toLatin1().constData(),
2906 ret.toLatin1().constData(), in_d.toLatin1().constData(), out_d.toLatin1().constData(),
2907 pwd.toLatin1().constData(), Option::output_dir.toLatin1().constData());
2908 cache->insert(cacheKey, ret);
2909 return ret;
2910}
2911
2912void
2913MakefileGenerator::checkMultipleDefinition(const QString &f, const QString &w)
2914{
2915 if(!(Option::warn_level & WarnLogic))
2916 return;
2917 QString file = f;
2918 int slsh = f.lastIndexOf(Option::dir_sep);
2919 if(slsh != -1)
2920 file.remove(0, slsh + 1);
2921 QStringList &l = project->values(w);
2922 for(QStringList::Iterator val_it = l.begin(); val_it != l.end(); ++val_it) {
2923 QString file2((*val_it));
2924 slsh = file2.lastIndexOf(Option::dir_sep);
2925 if(slsh != -1)
2926 file2.remove(0, slsh + 1);
2927 if(file2 == file) {
2928 warn_msg(WarnLogic, "Found potential symbol conflict of %s (%s) in %s",
2929 file.toLatin1().constData(), (*val_it).toLatin1().constData(), w.toLatin1().constData());
2930 break;
2931 }
2932 }
2933}
2934
2935QMakeLocalFileName
2936MakefileGenerator::fixPathForFile(const QMakeLocalFileName &file, bool forOpen)
2937{
2938 if(forOpen)
2939 return QMakeLocalFileName(fileFixify(file.real(), qmake_getpwd(), Option::output_dir));
2940 return QMakeLocalFileName(fileFixify(file.real()));
2941}
2942
2943QFileInfo
2944MakefileGenerator::findFileInfo(const QMakeLocalFileName &file)
2945{
2946 return fileInfo(file.local());
2947}
2948
2949QMakeLocalFileName
2950MakefileGenerator::findFileForDep(const QMakeLocalFileName &dep, const QMakeLocalFileName &file)
2951{
2952 QMakeLocalFileName ret;
2953 if(!project->isEmpty("SKIP_DEPENDS")) {
2954 bool found = false;
2955 QStringList &nodeplist = project->values("SKIP_DEPENDS");
2956 for(QStringList::Iterator it = nodeplist.begin();
2957 it != nodeplist.end(); ++it) {
2958 QRegExp regx((*it));
2959 if(regx.indexIn(dep.local()) != -1) {
2960 found = true;
2961 break;
2962 }
2963 }
2964 if(found)
2965 return ret;
2966 }
2967
2968 ret = QMakeSourceFileInfo::findFileForDep(dep, file);
2969 if(!ret.isNull())
2970 return ret;
2971
2972 //these are some "hacky" heuristics it will try to do on an include
2973 //however these can be turned off at runtime, I'm not sure how
2974 //reliable these will be, most likely when problems arise turn it off
2975 //and see if they go away..
2976 if(Option::mkfile::do_dep_heuristics) {
2977 if(depHeuristicsCache.contains(dep.real()))
2978 return depHeuristicsCache[dep.real()];
2979
2980 if(Option::output_dir != qmake_getpwd()
2981 && QDir::isRelativePath(dep.real())) { //is it from the shadow tree
2982 QList<QMakeLocalFileName> depdirs = QMakeSourceFileInfo::dependencyPaths();
2983 depdirs.prepend(fileInfo(file.real()).absoluteDir().path());
2984 QString pwd = qmake_getpwd();
2985 if(pwd.at(pwd.length()-1) != '/')
2986 pwd += '/';
2987 for(int i = 0; i < depdirs.count(); i++) {
2988 QString dir = depdirs.at(i).real();
2989 if(!QDir::isRelativePath(dir) && dir.startsWith(pwd))
2990 dir = dir.mid(pwd.length());
2991 if(QDir::isRelativePath(dir)) {
2992 if(!dir.endsWith(Option::dir_sep))
2993 dir += Option::dir_sep;
2994 QString shadow = fileFixify(dir + dep.local(), pwd, Option::output_dir);
2995 if(exists(shadow)) {
2996 ret = QMakeLocalFileName(shadow);
2997 goto found_dep_from_heuristic;
2998 }
2999 }
3000 }
3001 }
3002 { //is it from an EXTRA_TARGET
3003 const QString dep_basename = dep.local().section(Option::dir_sep, -1);
3004 QStringList &qut = project->values("QMAKE_EXTRA_TARGETS");
3005 for(QStringList::Iterator it = qut.begin(); it != qut.end(); ++it) {
3006 QString targ = var((*it) + ".target");
3007 if(targ.isEmpty())
3008 targ = (*it);
3009 QString out = Option::fixPathToTargetOS(targ);
3010 if(out == dep.real() || out.section(Option::dir_sep, -1) == dep_basename) {
3011 ret = QMakeLocalFileName(out);
3012 goto found_dep_from_heuristic;
3013 }
3014 }
3015 }
3016 { //is it from an EXTRA_COMPILER
3017 const QString dep_basename = dep.local().section(Option::dir_sep, -1);
3018 const QStringList &quc = project->values("QMAKE_EXTRA_COMPILERS");
3019 for(QStringList::ConstIterator it = quc.begin(); it != quc.end(); ++it) {
3020 QString tmp_out = project->values((*it) + ".output").first();
3021 if(tmp_out.isEmpty())
3022 continue;
3023 QStringList &tmp = project->values((*it) + ".input");
3024 for(QStringList::Iterator it2 = tmp.begin(); it2 != tmp.end(); ++it2) {
3025 QStringList &inputs = project->values((*it2));
3026 for(QStringList::Iterator input = inputs.begin(); input != inputs.end(); ++input) {
3027 QString out = Option::fixPathToTargetOS(unescapeFilePath(replaceExtraCompilerVariables(tmp_out, (*input), QString())));
3028 if(out == dep.real() || out.section(Option::dir_sep, -1) == dep_basename) {
3029 ret = QMakeLocalFileName(fileFixify(out, qmake_getpwd(), Option::output_dir));
3030 goto found_dep_from_heuristic;
3031 }
3032 }
3033 }
3034 }
3035 }
3036 found_dep_from_heuristic:
3037 depHeuristicsCache.insert(dep.real(), ret);
3038 }
3039 return ret;
3040}
3041
3042QStringList
3043&MakefileGenerator::findDependencies(const QString &file)
3044{
3045 const QString fixedFile = fileFixify(file);
3046 if(!dependsCache.contains(fixedFile)) {
3047#if 1
3048 QStringList deps = QMakeSourceFileInfo::dependencies(file);
3049 if(file != fixedFile)
3050 deps += QMakeSourceFileInfo::dependencies(fixedFile);
3051#else
3052 QStringList deps = QMakeSourceFileInfo::dependencies(fixedFile);
3053#endif
3054 dependsCache.insert(fixedFile, deps);
3055 }
3056 return dependsCache[fixedFile];
3057}
3058
3059QString
3060MakefileGenerator::specdir(const QString &outdir)
3061{
3062#if 0
3063 if(!spec.isEmpty())
3064 return spec;
3065#endif
3066 spec = fileFixify(Option::mkfile::qmakespec, outdir);
3067 return spec;
3068}
3069
3070bool
3071MakefileGenerator::openOutput(QFile &file, const QString &build) const
3072{
3073 {
3074 QString outdir;
3075 if(!file.fileName().isEmpty()) {
3076 if(QDir::isRelativePath(file.fileName()))
3077 file.setFileName(Option::output_dir + "/" + file.fileName()); //pwd when qmake was run
3078 QFileInfo fi(fileInfo(file.fileName()));
3079 if(fi.isDir())
3080 outdir = file.fileName() + '/';
3081 }
3082 if(!outdir.isEmpty() || file.fileName().isEmpty()) {
3083 QString fname = "Makefile";
3084 if(!project->isEmpty("MAKEFILE"))
3085 fname = project->first("MAKEFILE");
3086 file.setFileName(outdir + fname);
3087 }
3088 }
3089 if(QDir::isRelativePath(file.fileName())) {
3090 QString fname = Option::output_dir; //pwd when qmake was run
3091 if(!fname.endsWith("/"))
3092 fname += "/";
3093 fname += file.fileName();
3094 file.setFileName(fname);
3095 }
3096 if(!build.isEmpty())
3097 file.setFileName(file.fileName() + "." + build);
3098 if(project->isEmpty("QMAKE_MAKEFILE"))
3099 project->values("QMAKE_MAKEFILE").append(file.fileName());
3100 int slsh = file.fileName().lastIndexOf('/');
3101 if(slsh != -1)
3102 mkdir(file.fileName().left(slsh));
3103 if(file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate)) {
3104 QFileInfo fi(fileInfo(Option::output.fileName()));
3105 QString od;
3106 if(fi.isSymLink())
3107 od = fileInfo(fi.readLink()).absolutePath();
3108 else
3109 od = fi.path();
3110 od = QDir::fromNativeSeparators(od);
3111 if(QDir::isRelativePath(od)) {
3112 QString dir = Option::output_dir;
3113 if (!dir.endsWith('/') && !od.isEmpty())
3114 dir += '/';
3115 od.prepend(dir);
3116 }
3117 Option::output_dir = od;
3118 return true;
3119 }
3120 return false;
3121}
3122
3123QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.