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

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

qmake: Don't prepend install commands with '-' in OS/2 mode to have install errors reported (copy command can silently overwrite the target and doesn't return an error). Add suppressing del command output in uninstall targets.

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