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

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

qmake: More OS/2-specific fixes. Enabled GNUMakefileGenerator (turned on by MAKEFILE_GENERATOR=GNUMAKE).

File size: 125.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 t << "QMAKE_PRL_TARGET = " << target << endl;
953 if(!project->isEmpty("PRL_EXPORT_DEFINES"))
954 t << "QMAKE_PRL_DEFINES = " << project->values("PRL_EXPORT_DEFINES").join(" ") << endl;
955 if(!project->isEmpty("PRL_EXPORT_CFLAGS"))
956 t << "QMAKE_PRL_CFLAGS = " << project->values("PRL_EXPORT_CFLAGS").join(" ") << endl;
957 if(!project->isEmpty("PRL_EXPORT_CXXFLAGS"))
958 t << "QMAKE_PRL_CXXFLAGS = " << project->values("PRL_EXPORT_CXXFLAGS").join(" ") << endl;
959 if(!project->isEmpty("CONFIG"))
960 t << "QMAKE_PRL_CONFIG = " << project->values("CONFIG").join(" ") << endl;
961 if(!project->isEmpty("TARGET_VERSION_EXT"))
962 t << "QMAKE_PRL_VERSION = " << project->first("TARGET_VERSION_EXT") << endl;
963 else if(!project->isEmpty("VERSION"))
964 t << "QMAKE_PRL_VERSION = " << project->first("VERSION") << endl;
965 if(project->isActiveConfig("staticlib") || project->isActiveConfig("explicitlib")) {
966 QStringList libs;
967 if(!project->isEmpty("QMAKE_INTERNAL_PRL_LIBS"))
968 libs = project->values("QMAKE_INTERNAL_PRL_LIBS");
969 else
970 libs << "QMAKE_LIBS"; //obvious one
971 t << "QMAKE_PRL_LIBS = ";
972 for(QStringList::Iterator it = libs.begin(); it != libs.end(); ++it)
973 t << project->values((*it)).join(" ") << " ";
974 t << endl;
975 }
976}
977
978bool
979MakefileGenerator::writeProjectMakefile()
980{
981 usePlatformDir();
982 QTextStream t(&Option::output);
983
984 //header
985 writeHeader(t);
986
987 QList<SubTarget*> targets;
988 {
989 QStringList builds = project->values("BUILDS");
990 for(QStringList::Iterator it = builds.begin(); it != builds.end(); ++it) {
991 SubTarget *st = new SubTarget;
992 targets.append(st);
993 st->makefile = "$(MAKEFILE)." + (*it);
994 st->name = (*it);
995 st->target = project->isEmpty((*it) + ".target") ? (*it) : project->first((*it) + ".target");
996 }
997 }
998 if(project->isActiveConfig("build_all")) {
999 t << "first: all" << endl;
1000 QList<SubTarget*>::Iterator it;
1001
1002 //install
1003 t << "install: ";
1004 for(it = targets.begin(); it != targets.end(); ++it)
1005 t << (*it)->target << "-install ";
1006 t << endl;
1007
1008 //uninstall
1009 t << "uninstall: ";
1010 for(it = targets.begin(); it != targets.end(); ++it)
1011 t << (*it)->target << "-uninstall ";
1012 t << endl;
1013 } else {
1014 t << "first: " << targets.first()->target << endl
1015 << "install: " << targets.first()->target << "-install" << endl
1016 << "uninstall: " << targets.first()->target << "-uninstall" << endl;
1017 }
1018
1019 writeSubTargets(t, targets, SubTargetsNoFlags);
1020 if(!project->isActiveConfig("no_autoqmake")) {
1021 for(QList<SubTarget*>::Iterator it = targets.begin(); it != targets.end(); ++it)
1022 t << (*it)->makefile << ": " <<
1023 Option::fixPathToTargetOS(fileFixify(Option::output.fileName())) << endl;
1024 }
1025 qDeleteAll(targets);
1026 return true;
1027}
1028
1029bool
1030MakefileGenerator::write()
1031{
1032 if(!project)
1033 return false;
1034 writePrlFile();
1035 if(Option::qmake_mode == Option::QMAKE_GENERATE_MAKEFILE || //write makefile
1036 Option::qmake_mode == Option::QMAKE_GENERATE_PROJECT) {
1037 QTextStream t(&Option::output);
1038 if(!writeMakefile(t)) {
1039#if 1
1040 warn_msg(WarnLogic, "Unable to generate output for: %s [TEMPLATE %s]",
1041 Option::output.fileName().toLatin1().constData(),
1042 project->first("TEMPLATE").toLatin1().constData());
1043 if(Option::output.exists())
1044 Option::output.remove();
1045#endif
1046 }
1047 }
1048 return true;
1049}
1050
1051QString
1052MakefileGenerator::prlFileName(bool fixify)
1053{
1054 QString ret = project->first("TARGET_PRL");;
1055 if(ret.isEmpty())
1056 ret = project->first("TARGET");
1057 int slsh = ret.lastIndexOf(Option::dir_sep);
1058 if(slsh != -1)
1059 ret = ret.right(ret.length() - slsh);
1060 if(!ret.endsWith(Option::prl_ext)) {
1061 int dot = ret.indexOf('.');
1062 if(dot != -1)
1063 ret = ret.left(dot);
1064 ret += Option::prl_ext;
1065 }
1066 if(!project->isEmpty("QMAKE_BUNDLE"))
1067 ret.prepend(project->first("QMAKE_BUNDLE") + Option::dir_sep);
1068 if(fixify) {
1069 if(!project->isEmpty("DESTDIR"))
1070 ret.prepend(project->first("DESTDIR"));
1071 ret = Option::fixPathToLocalOS(fileFixify(ret, qmake_getpwd(), Option::output_dir));
1072 }
1073 return ret;
1074}
1075
1076void
1077MakefileGenerator::writePrlFile()
1078{
1079 if((Option::qmake_mode == Option::QMAKE_GENERATE_MAKEFILE ||
1080 Option::qmake_mode == Option::QMAKE_GENERATE_PRL)
1081 && project->values("QMAKE_FAILED_REQUIREMENTS").isEmpty()
1082 && project->isActiveConfig("create_prl")
1083 && (project->first("TEMPLATE") == "lib"
1084 || project->first("TEMPLATE") == "vclib")
1085 && !project->isActiveConfig("plugin")) { //write prl file
1086 QString local_prl = prlFileName();
1087 QString prl = fileFixify(local_prl);
1088 mkdir(fileInfo(local_prl).path());
1089 QFile ft(local_prl);
1090 if(ft.open(QIODevice::WriteOnly)) {
1091 project->values("ALL_DEPS").append(prl);
1092 project->values("QMAKE_INTERNAL_PRL_FILE").append(prl);
1093 QTextStream t(&ft);
1094 writePrlFile(t);
1095 }
1096 }
1097}
1098
1099// Manipulate directories, so it's possible to build
1100// several cross-platform targets concurrently
1101void
1102MakefileGenerator::usePlatformDir()
1103{
1104 QString pltDir(project->first("QMAKE_PLATFORM_DIR"));
1105 if(pltDir.isEmpty())
1106 return;
1107 QChar sep = QDir::separator();
1108 QString slashPltDir = sep + pltDir;
1109
1110 QString dirs[] = { QString("OBJECTS_DIR"), QString("DESTDIR"), QString("QMAKE_PKGCONFIG_DESTDIR"),
1111 QString("SUBLIBS_DIR"), QString("DLLDESTDIR"), QString("QMAKE_LIBTOOL_DESTDIR"),
1112 QString("PRECOMPILED_DIR"), QString("QMAKE_LIBDIR_QT"), QString() };
1113 for(int i = 0; !dirs[i].isEmpty(); ++i) {
1114 QString filePath = project->first(dirs[i]);
1115 project->values(dirs[i]) = QStringList(filePath + (filePath.isEmpty() ? pltDir : slashPltDir));
1116 }
1117
1118 QString libs[] = { QString("QMAKE_LIBS_QT"), QString("QMAKE_LIBS_QT_THREAD"), QString("QMAKE_LIBS_QT_ENTRY"), QString() };
1119 for(int i = 0; !libs[i].isEmpty(); ++i) {
1120 QString filePath = project->first(libs[i]);
1121 int fpi = filePath.lastIndexOf(sep);
1122 if(fpi == -1)
1123 project->values(libs[i]).prepend(pltDir + sep);
1124 else
1125 project->values(libs[i]) = QStringList(filePath.left(fpi) + slashPltDir + filePath.mid(fpi));
1126 }
1127}
1128
1129void
1130MakefileGenerator::writeObj(QTextStream &t, const QString &src)
1131{
1132 QStringList &srcl = project->values(src);
1133 QStringList objl = createObjectList(srcl);
1134
1135 QStringList::Iterator oit = objl.begin();
1136 QStringList::Iterator sit = srcl.begin();
1137 QString stringSrc("$src");
1138 QString stringObj("$obj");
1139 for(;sit != srcl.end() && oit != objl.end(); ++oit, ++sit) {
1140 if((*sit).isEmpty())
1141 continue;
1142
1143 t << escapeDependencyPath((*oit)) << ": " << escapeDependencyPath((*sit)) << " " << escapeDependencyPaths(findDependencies((*sit))).join(" \\\n\t\t");
1144
1145 QString comp, cimp;
1146 for(QStringList::Iterator cppit = Option::cpp_ext.begin(); cppit != Option::cpp_ext.end(); ++cppit) {
1147 if((*sit).endsWith((*cppit))) {
1148 comp = "QMAKE_RUN_CXX";
1149 cimp = "QMAKE_RUN_CXX_IMP";
1150 break;
1151 }
1152 }
1153 if(comp.isEmpty()) {
1154 comp = "QMAKE_RUN_CC";
1155 cimp = "QMAKE_RUN_CC_IMP";
1156 }
1157 bool use_implicit_rule = !project->isEmpty(cimp);
1158 use_implicit_rule = false;
1159 if(use_implicit_rule) {
1160 if(!project->isEmpty("OBJECTS_DIR")) {
1161 use_implicit_rule = false;
1162 } else {
1163 int dot = (*sit).lastIndexOf('.');
1164 if(dot == -1 || ((*sit).left(dot) + Option::obj_ext != (*oit)))
1165 use_implicit_rule = false;
1166 }
1167 }
1168 if (!use_implicit_rule && !project->isEmpty(comp)) {
1169 QString p = var(comp), srcf(*sit);
1170 p.replace(stringSrc, escapeFilePath(srcf));
1171 p.replace(stringObj, escapeFilePath((*oit)));
1172 t << "\n\t" << p;
1173 }
1174 t << endl << endl;
1175 }
1176}
1177
1178QString
1179MakefileGenerator::filePrefixRoot(const QString &root, const QString &path)
1180{
1181 QString ret(root + path);
1182 if(path.length() > 2 && path[1] == ':') //c:\foo
1183 ret = QString(path.mid(0, 2) + root + path.mid(2));
1184 while(ret.endsWith("\\"))
1185 ret = ret.left(ret.length()-1);
1186 return ret;
1187}
1188
1189void
1190MakefileGenerator::writeInstalls(QTextStream &t, const QString &installs, bool noBuild)
1191{
1192 QString rm_dir_contents("-$(DEL_FILE)");
1193 if (!isDosLikeShell()) //ick
1194 rm_dir_contents = "-$(DEL_FILE) -r";
1195
1196 QString all_installs, all_uninstalls;
1197 QStringList &l = project->values(installs);
1198 for(QStringList::Iterator it = l.begin(); it != l.end(); ++it) {
1199 QString pvar = (*it) + ".path";
1200 if(project->values((*it) + ".CONFIG").indexOf("no_path") == -1 &&
1201 project->values((*it) + ".CONFIG").indexOf("dummy_install") == -1 &&
1202 project->values(pvar).isEmpty()) {
1203 warn_msg(WarnLogic, "%s is not defined: install target not created\n", pvar.toLatin1().constData());
1204 continue;
1205 }
1206
1207 bool do_default = true;
1208 const QString root = "$(INSTALL_ROOT)";
1209 QString target, dst;
1210 if(project->values((*it) + ".CONFIG").indexOf("no_path") == -1 &&
1211 project->values((*it) + ".CONFIG").indexOf("dummy_install") == -1) {
1212 dst = fileFixify(unescapeFilePath(project->values(pvar).first()), FileFixifyAbsolute, false);
1213 if(dst.right(1) != Option::dir_sep)
1214 dst += Option::dir_sep;
1215 }
1216 dst = escapeFilePath(dst);
1217
1218 QStringList tmp, uninst = project->values((*it) + ".uninstall");
1219 //other
1220 tmp = project->values((*it) + ".extra");
1221 if(tmp.isEmpty())
1222 tmp = project->values((*it) + ".commands"); //to allow compatible name
1223 if(!tmp.isEmpty()) {
1224 do_default = false;
1225 if(!target.isEmpty())
1226 target += "\n\t";
1227 target += tmp.join(" ");
1228 }
1229 //masks
1230 tmp = findFilesInVPATH(project->values((*it) + ".files"), VPATH_NoFixify);
1231 tmp = fileFixify(tmp, FileFixifyAbsolute);
1232 if(!tmp.isEmpty()) {
1233 if(!target.isEmpty())
1234 target += "\n";
1235 do_default = false;
1236 for(QStringList::Iterator wild_it = tmp.begin(); wild_it != tmp.end(); ++wild_it) {
1237 QString wild = Option::fixPathToLocalOS((*wild_it), false, false);
1238 QString dirstr = qmake_getpwd(), filestr = wild;
1239 int slsh = filestr.lastIndexOf(Option::dir_sep);
1240 if(slsh != -1) {
1241 dirstr = filestr.left(slsh+1);
1242 filestr = filestr.right(filestr.length() - slsh - 1);
1243 }
1244 if(dirstr.right(Option::dir_sep.length()) != Option::dir_sep)
1245 dirstr += Option::dir_sep;
1246 if(exists(wild)) { //real file
1247 QString file = wild;
1248 QFileInfo fi(fileInfo(wild));
1249 if(!target.isEmpty())
1250 target += "\t";
1251 QString dst_file = filePrefixRoot(root, dst);
1252 if(fi.isDir() && project->isActiveConfig("copy_dir_files")) {
1253 if(!dst_file.endsWith(Option::dir_sep))
1254 dst_file += Option::dir_sep;
1255 dst_file += fi.fileName();
1256 }
1257 QString cmd;
1258 if (fi.isDir())
1259 cmd = "-$(INSTALL_DIR)";
1260 else if (fi.isExecutable())
1261 cmd = "-$(INSTALL_PROGRAM)";
1262 else
1263 cmd = "-$(INSTALL_FILE)";
1264 cmd += " " + escapeFilePath(wild) + " " + dst_file + "\n";
1265 target += cmd;
1266 if(!project->isActiveConfig("debug") && !project->isActiveConfig("nostrip") &&
1267 !fi.isDir() && fi.isExecutable() && !project->isEmpty("QMAKE_STRIP"))
1268 target += QString("\t-") + var("QMAKE_STRIP") + " " +
1269 filePrefixRoot(root, fileFixify(dst + filestr, FileFixifyAbsolute, false)) + "\n";
1270 if(!uninst.isEmpty())
1271 uninst.append("\n\t");
1272 uninst.append(rm_dir_contents + " " + filePrefixRoot(root, fileFixify(dst + filestr, FileFixifyAbsolute, false)));
1273 continue;
1274 }
1275 QString local_dirstr = Option::fixPathToLocalOS(dirstr, true);
1276 QStringList files = QDir(local_dirstr).entryList(QStringList(filestr));
1277 if(project->values((*it) + ".CONFIG").indexOf("no_check_exist") != -1 && files.isEmpty()) {
1278 if(!target.isEmpty())
1279 target += "\t";
1280 QString dst_file = filePrefixRoot(root, dst);
1281 QFileInfo fi(fileInfo(wild));
1282 QString cmd = QString(fi.isExecutable() ? "-$(INSTALL_PROGRAM)" : "-$(INSTALL_FILE)") + " " +
1283 wild + " " + dst_file + "\n";
1284 target += cmd;
1285 if(!uninst.isEmpty())
1286 uninst.append("\n\t");
1287 uninst.append(rm_dir_contents + " " + filePrefixRoot(root, fileFixify(dst + filestr, FileFixifyAbsolute, false)));
1288 }
1289 for(int x = 0; x < files.count(); x++) {
1290 QString file = files[x];
1291 if(file == "." || file == "..") //blah
1292 continue;
1293 if(!uninst.isEmpty())
1294 uninst.append("\n\t");
1295 uninst.append(rm_dir_contents + " " + filePrefixRoot(root, fileFixify(dst + file, FileFixifyAbsolute, false)));
1296 QFileInfo fi(fileInfo(dirstr + file));
1297 if(!target.isEmpty())
1298 target += "\t";
1299 QString dst_file = filePrefixRoot(root, fileFixify(dst, FileFixifyAbsolute, false));
1300 if(fi.isDir() && project->isActiveConfig("copy_dir_files")) {
1301 if(!dst_file.endsWith(Option::dir_sep))
1302 dst_file += Option::dir_sep;
1303 dst_file += fi.fileName();
1304 }
1305 QString cmd = QString(fi.isDir() ? "-$(INSTALL_DIR)" : "-$(INSTALL_FILE)") + " " +
1306 dirstr + file + " " + dst_file + "\n";
1307 target += cmd;
1308 if(!project->isActiveConfig("debug") && !project->isActiveConfig("nostrip") &&
1309 !fi.isDir() && fi.isExecutable() && !project->isEmpty("QMAKE_STRIP"))
1310 target += QString("\t-") + var("QMAKE_STRIP") + " " +
1311 filePrefixRoot(root, fileFixify(dst + file, FileFixifyAbsolute, false)) +
1312 "\n";
1313 }
1314 }
1315 }
1316 //default?
1317 if(do_default) {
1318 target = defaultInstall((*it));
1319 uninst = project->values((*it) + ".uninstall");
1320 }
1321
1322 if(!target.isEmpty() || project->values((*it) + ".CONFIG").indexOf("dummy_install") != -1) {
1323 if(noBuild || project->values((*it) + ".CONFIG").indexOf("no_build") != -1)
1324 t << "install_" << (*it) << ":";
1325 else if(project->isActiveConfig("build_all"))
1326 t << "install_" << (*it) << ": all";
1327 else
1328 t << "install_" << (*it) << ": first";
1329 const QStringList &deps = project->values((*it) + ".depends");
1330 if(!deps.isEmpty()) {
1331 for(QStringList::ConstIterator dep_it = deps.begin(); dep_it != deps.end(); ++dep_it) {
1332 QString targ = var((*dep_it) + ".target");
1333 if(targ.isEmpty())
1334 targ = (*dep_it);
1335 t << " " << escapeDependencyPath(targ);
1336 }
1337 }
1338 if(project->isEmpty("QMAKE_NOFORCE"))
1339 t << " FORCE";
1340 t << "\n\t";
1341 const QStringList &dirs = project->values(pvar);
1342 for(QStringList::ConstIterator pit = dirs.begin(); pit != dirs.end(); ++pit) {
1343 QString tmp_dst = fileFixify((*pit), FileFixifyAbsolute, false);
1344 if (!isDosLikeShell() && tmp_dst.right(1) != Option::dir_sep)
1345 tmp_dst += Option::dir_sep;
1346 t << mkdir_p_asstring(filePrefixRoot(root, tmp_dst)) << "\n\t";
1347 }
1348 t << target << endl << endl;
1349 if(!uninst.isEmpty()) {
1350 t << "uninstall_" << (*it) << ": ";
1351 if(project->isEmpty("QMAKE_NOFORCE"))
1352 t << " FORCE";
1353 t << "\n\t"
1354 << uninst.join(" ") << "\n\t"
1355 << "-$(DEL_DIR) " << filePrefixRoot(root, dst) << " " << endl << endl;
1356 }
1357 t << endl;
1358
1359 if(project->values((*it) + ".CONFIG").indexOf("no_default_install") == -1) {
1360 all_installs += QString("install_") + (*it) + " ";
1361 if(!uninst.isEmpty())
1362 all_uninstalls += "uninstall_" + (*it) + " ";
1363 }
1364 } else {
1365 debug_msg(1, "no definition for install %s: install target not created",(*it).toLatin1().constData());
1366 }
1367 }
1368 t << "install: " << var("INSTALLDEPS") << " " << all_installs;
1369 if(project->isEmpty("QMAKE_NOFORCE"))
1370 t << " FORCE";
1371 t << "\n\n";
1372 t << "uninstall: " << all_uninstalls << " " << var("UNINSTALLDEPS");
1373 if(project->isEmpty("QMAKE_NOFORCE"))
1374 t << " FORCE";
1375 t << "\n\n";
1376}
1377
1378QString
1379MakefileGenerator::var(const QString &var)
1380{
1381 return val(project->values(var));
1382}
1383
1384QString
1385MakefileGenerator::val(const QStringList &varList)
1386{
1387 return valGlue(varList, "", " ", "");
1388}
1389
1390QString
1391MakefileGenerator::varGlue(const QString &var, const QString &before, const QString &glue, const QString &after)
1392{
1393 return valGlue(project->values(var), before, glue, after);
1394}
1395
1396QString
1397MakefileGenerator::valGlue(const QStringList &varList, const QString &before, const QString &glue, const QString &after)
1398{
1399 QString ret;
1400 for(QStringList::ConstIterator it = varList.begin(); it != varList.end(); ++it) {
1401 if(!(*it).isEmpty()) {
1402 if(!ret.isEmpty())
1403 ret += glue;
1404 ret += (*it);
1405 }
1406 }
1407 return ret.isEmpty() ? QString("") : before + ret + after;
1408}
1409
1410
1411QString
1412MakefileGenerator::varList(const QString &var)
1413{
1414 return valList(project->values(var));
1415}
1416
1417QString
1418MakefileGenerator::valList(const QStringList &varList)
1419{
1420 return valGlue(varList, "", " \\\n\t\t", "");
1421}
1422
1423QStringList
1424MakefileGenerator::createObjectList(const QStringList &sources)
1425{
1426 QStringList ret;
1427 QString objdir;
1428 if(!project->values("OBJECTS_DIR").isEmpty())
1429 objdir = project->first("OBJECTS_DIR");
1430 for(QStringList::ConstIterator it = sources.begin(); it != sources.end(); ++it) {
1431 QFileInfo fi(fileInfo(Option::fixPathToLocalOS((*it))));
1432 QString dir;
1433 if(objdir.isEmpty() && project->isActiveConfig("object_with_source")) {
1434 QString fName = Option::fixPathToTargetOS((*it), false);
1435 int dl = fName.lastIndexOf(Option::dir_sep);
1436 if(dl != -1)
1437 dir = fName.left(dl + 1);
1438 } else {
1439 dir = objdir;
1440 }
1441 ret.append(dir + fi.completeBaseName() + Option::obj_ext);
1442 }
1443 return ret;
1444}
1445
1446ReplaceExtraCompilerCacheKey::ReplaceExtraCompilerCacheKey(const QString &v, const QStringList &i, const QStringList &o)
1447{
1448 hash = 0;
1449 pwd = qmake_getpwd();
1450 var = v;
1451 {
1452 QStringList il = i;
1453 il.sort();
1454 in = il.join("::");
1455 }
1456 {
1457 QStringList ol = o;
1458 ol.sort();
1459 out = ol.join("::");
1460 }
1461}
1462
1463bool ReplaceExtraCompilerCacheKey::operator==(const ReplaceExtraCompilerCacheKey &f) const
1464{
1465 return (hashCode() == f.hashCode() &&
1466 f.in == in &&
1467 f.out == out &&
1468 f.var == var &&
1469 f.pwd == pwd);
1470}
1471
1472
1473QString
1474MakefileGenerator::replaceExtraCompilerVariables(const QString &orig_var, const QStringList &in, const QStringList &out)
1475{
1476 //lazy cache
1477 ReplaceExtraCompilerCacheKey cacheKey(orig_var, in, out);
1478 QString cacheVal = extraCompilerVariablesCache.value(cacheKey);
1479 if(!cacheVal.isNull())
1480 return cacheVal;
1481
1482 //do the work
1483 QString ret = orig_var;
1484 QRegExp reg_var("\\$\\{.*\\}");
1485 reg_var.setMinimal(true);
1486 for(int rep = 0; (rep = reg_var.indexIn(ret, rep)) != -1; ) {
1487 QStringList val;
1488 const QString var = ret.mid(rep + 2, reg_var.matchedLength() - 3);
1489 bool filePath = false;
1490 if(val.isEmpty() && var.startsWith(QLatin1String("QMAKE_VAR_"))) {
1491 const QString varname = var.mid(10);
1492 val += project->values(varname);
1493 }
1494 if(val.isEmpty() && var.startsWith(QLatin1String("QMAKE_VAR_FIRST_"))) {
1495 const QString varname = var.mid(12);
1496 val += project->first(varname);
1497 }
1498
1499 if(val.isEmpty() && !in.isEmpty()) {
1500 if(var.startsWith(QLatin1String("QMAKE_FUNC_FILE_IN_"))) {
1501 filePath = true;
1502 const QString funcname = var.mid(19);
1503 val += project->expand(funcname, QList<QStringList>() << in);
1504 } else if(var == QLatin1String("QMAKE_FILE_BASE") || var == QLatin1String("QMAKE_FILE_IN_BASE")) {
1505 //filePath = true;
1506 for(int i = 0; i < in.size(); ++i) {
1507 QFileInfo fi(fileInfo(Option::fixPathToLocalOS(in.at(i))));
1508 QString base = fi.completeBaseName();
1509 if(base.isNull())
1510 base = fi.fileName();
1511 val += base;
1512 }
1513 } else if(var == QLatin1String("QMAKE_FILE_PATH") || var == QLatin1String("QMAKE_FILE_IN_PATH")) {
1514 filePath = true;
1515 for(int i = 0; i < in.size(); ++i)
1516 val += fileInfo(Option::fixPathToLocalOS(in.at(i))).path();
1517 } else if(var == QLatin1String("QMAKE_FILE_NAME") || var == QLatin1String("QMAKE_FILE_IN")) {
1518 filePath = true;
1519 for(int i = 0; i < in.size(); ++i)
1520 val += fileInfo(Option::fixPathToLocalOS(in.at(i))).filePath();
1521
1522 }
1523 }
1524 if(val.isEmpty() && !out.isEmpty()) {
1525 if(var.startsWith(QLatin1String("QMAKE_FUNC_FILE_OUT_"))) {
1526 filePath = true;
1527 const QString funcname = var.mid(20);
1528 val += project->expand(funcname, QList<QStringList>() << out);
1529 } else if(var == QLatin1String("QMAKE_FILE_OUT")) {
1530 filePath = true;
1531 for(int i = 0; i < out.size(); ++i)
1532 val += fileInfo(Option::fixPathToLocalOS(out.at(i))).filePath();
1533 } else if(var == QLatin1String("QMAKE_FILE_OUT_BASE")) {
1534 //filePath = true;
1535 for(int i = 0; i < out.size(); ++i) {
1536 QFileInfo fi(fileInfo(Option::fixPathToLocalOS(out.at(i))));
1537 QString base = fi.completeBaseName();
1538 if(base.isNull())
1539 base = fi.fileName();
1540 val += base;
1541 }
1542 }
1543 }
1544 if(val.isEmpty() && var.startsWith(QLatin1String("QMAKE_FUNC_"))) {
1545 const QString funcname = var.mid(11);
1546 val += project->expand(funcname, QList<QStringList>() << in << out);
1547 }
1548
1549 if(!val.isEmpty()) {
1550 QString fullVal;
1551 if(filePath) {
1552 for(int i = 0; i < val.size(); ++i) {
1553 const QString file = Option::fixPathToTargetOS(unescapeFilePath(val.at(i)), false);
1554 if(!fullVal.isEmpty())
1555 fullVal += " ";
1556 fullVal += escapeFilePath(file);
1557 }
1558 } else {
1559 fullVal = val.join(" ");
1560 }
1561 ret.replace(rep, reg_var.matchedLength(), fullVal);
1562 rep += fullVal.length();
1563 } else {
1564 rep += reg_var.matchedLength();
1565 }
1566 }
1567
1568 //cache the value
1569 extraCompilerVariablesCache.insert(cacheKey, ret);
1570 return ret;
1571}
1572
1573bool
1574MakefileGenerator::verifyExtraCompiler(const QString &comp, const QString &file_unfixed)
1575{
1576 if(noIO())
1577 return false;
1578 const QString file = Option::fixPathToLocalOS(file_unfixed);
1579
1580 if(project->values(comp + ".CONFIG").indexOf("moc_verify") != -1) {
1581 if(!file.isNull()) {
1582 QMakeSourceFileInfo::addSourceFile(file, QMakeSourceFileInfo::SEEK_MOCS);
1583 if(!mocable(file))
1584 return false;
1585 }
1586 } else if(project->values(comp + ".CONFIG").indexOf("function_verify") != -1) {
1587 QString tmp_out = project->values(comp + ".output").first();
1588 if(tmp_out.isEmpty())
1589 return false;
1590 QStringList verify_function = project->values(comp + ".verify_function");
1591 if(verify_function.isEmpty())
1592 return false;
1593
1594 for(int i = 0; i < verify_function.size(); ++i) {
1595 bool invert = false;
1596 QString verify = verify_function.at(i);
1597 if(verify.at(0) == QLatin1Char('!')) {
1598 invert = true;
1599 verify = verify.mid(1);
1600 }
1601
1602 if(project->values(comp + ".CONFIG").indexOf("combine") != -1) {
1603 bool pass = project->test(verify, QList<QStringList>() << QStringList(tmp_out) << QStringList(file));
1604 if(invert)
1605 pass = !pass;
1606 if(!pass)
1607 return false;
1608 } else {
1609 QStringList &tmp = project->values(comp + ".input");
1610 for(QStringList::Iterator it = tmp.begin(); it != tmp.end(); ++it) {
1611 QStringList &inputs = project->values((*it));
1612 for(QStringList::Iterator input = inputs.begin(); input != inputs.end(); ++input) {
1613 if((*input).isEmpty())
1614 continue;
1615 QString in = fileFixify(Option::fixPathToTargetOS((*input), false));
1616 if(in == file) {
1617 bool pass = project->test(verify,
1618 QList<QStringList>() << QStringList(replaceExtraCompilerVariables(tmp_out, (*input), QString())) <<
1619 QStringList(file));
1620 if(invert)
1621 pass = !pass;
1622 if(!pass)
1623 return false;
1624 break;
1625 }
1626 }
1627 }
1628 }
1629 }
1630 } else if(project->values(comp + ".CONFIG").indexOf("verify") != -1) {
1631 QString tmp_out = project->values(comp + ".output").first();
1632 if(tmp_out.isEmpty())
1633 return false;
1634 QString tmp_cmd;
1635 if(!project->isEmpty(comp + ".commands")) {
1636 int argv0 = -1;
1637 QStringList cmdline = project->values(comp + ".commands");
1638 for(int i = 0; i < cmdline.count(); ++i) {
1639 if(!cmdline.at(i).contains('=')) {
1640 argv0 = i;
1641 break;
1642 }
1643 }
1644 if(argv0 != -1) {
1645 cmdline[argv0] = Option::fixPathToTargetOS(cmdline.at(argv0), false);
1646 tmp_cmd = cmdline.join(" ");
1647 }
1648 }
1649
1650 if(project->values(comp + ".CONFIG").indexOf("combine") != -1) {
1651 QString cmd = replaceExtraCompilerVariables(tmp_cmd, QString(), tmp_out);
1652 if(system(cmd.toLatin1().constData()))
1653 return false;
1654 } else {
1655 QStringList &tmp = project->values(comp + ".input");
1656 for(QStringList::Iterator it = tmp.begin(); it != tmp.end(); ++it) {
1657 QStringList &inputs = project->values((*it));
1658 for(QStringList::Iterator input = inputs.begin(); input != inputs.end(); ++input) {
1659 if((*input).isEmpty())
1660 continue;
1661 QString in = fileFixify(Option::fixPathToTargetOS((*input), false));
1662 if(in == file) {
1663 QString out = replaceExtraCompilerVariables(tmp_out, (*input), QString());
1664 QString cmd = replaceExtraCompilerVariables(tmp_cmd, in, out);
1665 if(system(cmd.toLatin1().constData()))
1666 return false;
1667 break;
1668 }
1669 }
1670 }
1671 }
1672 }
1673 return true;
1674}
1675
1676void
1677MakefileGenerator::writeExtraTargets(QTextStream &t)
1678{
1679 QStringList &qut = project->values("QMAKE_EXTRA_TARGETS");
1680 for(QStringList::Iterator it = qut.begin(); it != qut.end(); ++it) {
1681 QString targ = var((*it) + ".target"),
1682 cmd = var((*it) + ".commands"), deps;
1683 if(targ.isEmpty())
1684 targ = (*it);
1685 QStringList &deplist = project->values((*it) + ".depends");
1686 for(QStringList::Iterator dep_it = deplist.begin(); dep_it != deplist.end(); ++dep_it) {
1687 QString dep = var((*dep_it) + ".target");
1688 if(dep.isEmpty())
1689 dep = (*dep_it);
1690 deps += " " + escapeDependencyPath(dep);
1691 }
1692 if(project->values((*it) + ".CONFIG").indexOf("fix_target") != -1)
1693 targ = fileFixify(targ);
1694 if(project->isEmpty("QMAKE_NOFORCE") &&
1695 project->values((*it) + ".CONFIG").indexOf("phony") != -1)
1696 deps += QString(" ") + "FORCE";
1697 t << escapeDependencyPath(targ) << ":" << deps;
1698 if(!cmd.isEmpty())
1699 t << "\n\t" << cmd;
1700 t << endl << endl;
1701 }
1702}
1703
1704void
1705MakefileGenerator::writeExtraCompilerTargets(QTextStream &t)
1706{
1707 QString clean_targets;
1708 const QStringList &quc = project->values("QMAKE_EXTRA_COMPILERS");
1709 for(QStringList::ConstIterator it = quc.begin(); it != quc.end(); ++it) {
1710 QString tmp_out = fileFixify(project->values((*it) + ".output").first(),
1711 Option::output_dir, Option::output_dir);
1712 QString tmp_cmd;
1713 if(!project->isEmpty((*it) + ".commands")) {
1714 QStringList cmdline = project->values((*it) + ".commands");
1715 int argv0 = findExecutable(cmdline);
1716 if(argv0 != -1) {
1717 cmdline[argv0] = escapeFilePath(Option::fixPathToTargetOS(cmdline.at(argv0), false));
1718 tmp_cmd = cmdline.join(" ");
1719 }
1720 }
1721 QStringList tmp_dep = project->values((*it) + ".depends");
1722 QString tmp_dep_cmd;
1723 if(!project->isEmpty((*it) + ".depend_command")) {
1724 int argv0 = -1;
1725 QStringList cmdline = project->values((*it) + ".depend_command");
1726 for(int i = 0; i < cmdline.count(); ++i) {
1727 if(!cmdline.at(i).contains('=')) {
1728 argv0 = i;
1729 break;
1730 }
1731 }
1732 if(argv0 != -1) {
1733 const QString c = Option::fixPathToLocalOS(cmdline.at(argv0), true);
1734 if(exists(c)) {
1735 cmdline[argv0] = escapeFilePath(Option::fixPathToLocalOS(cmdline.at(argv0), false));
1736 tmp_dep_cmd = cmdline.join(" ");
1737 } else {
1738 cmdline[argv0] = escapeFilePath(cmdline.at(argv0));
1739 }
1740 }
1741 }
1742 QStringList &vars = project->values((*it) + ".variables");
1743 if(tmp_out.isEmpty() || tmp_cmd.isEmpty())
1744 continue;
1745 QStringList tmp_inputs;
1746 {
1747 const QStringList &comp_inputs = project->values((*it) + ".input");
1748 for(QStringList::ConstIterator it2 = comp_inputs.begin(); it2 != comp_inputs.end(); ++it2) {
1749 const QStringList &tmp = project->values((*it2));
1750 for(QStringList::ConstIterator input = tmp.begin(); input != tmp.end(); ++input) {
1751 QString in = Option::fixPathToTargetOS((*input), false);
1752 if(verifyExtraCompiler((*it), in))
1753 tmp_inputs.append((*input));
1754 }
1755 }
1756 }
1757
1758 t << "compiler_" << (*it) << "_make_all:";
1759 if(project->values((*it) + ".CONFIG").indexOf("combine") != -1) {
1760 // compilers with a combined input only have one output
1761 QString input = project->values((*it) + ".output").first();
1762 t << " " << escapeDependencyPath(replaceExtraCompilerVariables(tmp_out, input, QString()));
1763 } else {
1764 for(QStringList::ConstIterator input = tmp_inputs.begin(); input != tmp_inputs.end(); ++input) {
1765 QString in = Option::fixPathToTargetOS((*input), false);
1766 t << " " << escapeDependencyPath(replaceExtraCompilerVariables(tmp_out, (*input), QString()));
1767 }
1768 }
1769 t << endl;
1770
1771 if(project->values((*it) + ".CONFIG").indexOf("no_clean") == -1) {
1772 QString tmp_clean = project->values((*it) + ".clean").join(" ");
1773 QString tmp_clean_cmds = project->values((*it) + ".clean_commands").join(" ");
1774 if(!tmp_inputs.isEmpty())
1775 clean_targets += QString("compiler_" + (*it) + "_clean ");
1776 t << "compiler_" << (*it) << "_clean:";
1777 bool wrote_clean_cmds = false, wrote_clean = false;
1778 if(tmp_clean_cmds.isEmpty()) {
1779 wrote_clean_cmds = true;
1780 } else if(tmp_clean_cmds.indexOf("${QMAKE_") == -1) {
1781 t << "\n\t" << tmp_clean_cmds;
1782 wrote_clean_cmds = true;
1783 }
1784 if(tmp_clean.isEmpty())
1785 tmp_clean = tmp_out;
1786 if(tmp_clean.indexOf("${QMAKE_") == -1) {
1787 t << "\n\t" << "-$(DEL_FILE) " << tmp_clean;
1788 wrote_clean = true;
1789 }
1790 if(!wrote_clean_cmds || !wrote_clean) {
1791 QStringList cleans;
1792 const QString del_statement("-$(DEL_FILE)");
1793 if(!wrote_clean) {
1794 if(project->isActiveConfig("no_delete_multiple_files")) {
1795 for(QStringList::ConstIterator input = tmp_inputs.begin(); input != tmp_inputs.end(); ++input)
1796 cleans.append(" " + replaceExtraCompilerVariables(tmp_clean, (*input),
1797 replaceExtraCompilerVariables(tmp_out, (*input), QString())));
1798 } else {
1799 QString files, file;
1800 const int commandlineLimit = 2047; // NT limit, expanded
1801 for(int input = 0; input < tmp_inputs.size(); ++input) {
1802 file = " " + replaceExtraCompilerVariables(tmp_clean, tmp_inputs.at(input),
1803 replaceExtraCompilerVariables(tmp_out, tmp_inputs.at(input), QString()));
1804 if(del_statement.length() + files.length() +
1805 qMax(fixEnvVariables(file).length(), file.length()) > commandlineLimit) {
1806 cleans.append(files);
1807 files.clear();
1808 }
1809 files += file;
1810 }
1811 if(!files.isEmpty())
1812 cleans.append(files);
1813 }
1814 }
1815 if(!cleans.isEmpty())
1816 t << valGlue(cleans, "\n\t" + del_statement, "\n\t" + del_statement, "");
1817 if(!wrote_clean_cmds) {
1818 for(QStringList::ConstIterator input = tmp_inputs.begin(); input != tmp_inputs.end(); ++input) {
1819 t << "\n\t" << replaceExtraCompilerVariables(tmp_clean_cmds, (*input),
1820 replaceExtraCompilerVariables(tmp_out, (*input), QString()));
1821 }
1822 }
1823 }
1824 t << endl;
1825 }
1826 if(project->values((*it) + ".CONFIG").indexOf("combine") != -1) {
1827 if(tmp_out.indexOf("${QMAKE_") != -1) {
1828 warn_msg(WarnLogic, "QMAKE_EXTRA_COMPILERS(%s) with combine has variable output.",
1829 (*it).toLatin1().constData());
1830 continue;
1831 }
1832 QStringList deps, inputs;
1833 if(!tmp_dep.isEmpty())
1834 deps += fileFixify(tmp_dep, Option::output_dir, Option::output_dir);
1835 for(QStringList::ConstIterator input = tmp_inputs.begin(); input != tmp_inputs.end(); ++input) {
1836 deps += findDependencies((*input));
1837 inputs += Option::fixPathToTargetOS((*input), false);
1838 if(!tmp_dep_cmd.isEmpty() && doDepends()) {
1839 char buff[256];
1840 QString dep_cmd = replaceExtraCompilerVariables(tmp_dep_cmd, (*input),
1841 tmp_out);
1842 dep_cmd = fixEnvVariables(dep_cmd);
1843 if(FILE *proc = QT_POPEN(dep_cmd.toLatin1().constData(), "r")) {
1844 QString indeps;
1845 while(!feof(proc)) {
1846 int read_in = (int)fread(buff, 1, 255, proc);
1847 if(!read_in)
1848 break;
1849 indeps += QByteArray(buff, read_in);
1850 }
1851 QT_PCLOSE(proc);
1852 if(!indeps.isEmpty()) {
1853 QStringList dep_cmd_deps = indeps.replace('\n', ' ').simplified().split(' ');
1854 for(int i = 0; i < dep_cmd_deps.count(); ++i) {
1855 QString &file = dep_cmd_deps[i];
1856 if(!exists(file)) {
1857 QString localFile;
1858 QList<QMakeLocalFileName> depdirs = QMakeSourceFileInfo::dependencyPaths();
1859 for(QList<QMakeLocalFileName>::Iterator it = depdirs.begin();
1860 it != depdirs.end(); ++it) {
1861 if(exists((*it).real() + Option::dir_sep + file)) {
1862 localFile = (*it).local() + Option::dir_sep + file;
1863 break;
1864 }
1865 }
1866 file = localFile;
1867 }
1868 if(!file.isEmpty())
1869 file = fileFixify(file);
1870 }
1871 deps += dep_cmd_deps;
1872 }
1873 }
1874 }
1875 }
1876 for(int i = 0; i < inputs.size(); ) {
1877 if(tmp_out == inputs.at(i))
1878 inputs.removeAt(i);
1879 else
1880 ++i;
1881 }
1882 for(int i = 0; i < deps.size(); ) {
1883 if(tmp_out == deps.at(i))
1884 deps.removeAt(i);
1885 else
1886 ++i;
1887 }
1888 if (inputs.isEmpty())
1889 continue;
1890
1891 QString cmd = replaceExtraCompilerVariables(tmp_cmd, escapeFilePaths(inputs), QStringList(tmp_out));
1892 t << escapeDependencyPath(tmp_out) << ":";
1893 // compiler.CONFIG+=explicit_dependencies means that ONLY compiler.depends gets to cause Makefile dependencies
1894 if(project->values((*it) + ".CONFIG").indexOf("explicit_dependencies") != -1) {
1895 t << " " << valList(escapeDependencyPaths(fileFixify(tmp_dep, Option::output_dir, Option::output_dir)));
1896 } else {
1897 t << " " << valList(escapeDependencyPaths(inputs)) << " " << valList(escapeDependencyPaths(deps));
1898 }
1899 t << "\n\t" << cmd << endl << endl;
1900 continue;
1901 }
1902 for(QStringList::ConstIterator input = tmp_inputs.begin(); input != tmp_inputs.end(); ++input) {
1903 QString in = Option::fixPathToTargetOS((*input), false);
1904 QStringList deps = findDependencies((*input));
1905 deps += escapeDependencyPath(in);
1906 QString out = replaceExtraCompilerVariables(tmp_out, (*input), QString());
1907 if(!tmp_dep.isEmpty()) {
1908 QStringList pre_deps = fileFixify(tmp_dep, Option::output_dir, Option::output_dir);
1909 for(int i = 0; i < pre_deps.size(); ++i)
1910 deps += replaceExtraCompilerVariables(pre_deps.at(i), (*input), out);
1911 }
1912 QString cmd = replaceExtraCompilerVariables(tmp_cmd, (*input), out);
1913 for(QStringList::ConstIterator it3 = vars.constBegin(); it3 != vars.constEnd(); ++it3)
1914 cmd.replace("$(" + (*it3) + ")", "$(QMAKE_COMP_" + (*it3)+")");
1915 if(!tmp_dep_cmd.isEmpty() && doDepends()) {
1916 char buff[256];
1917 QString dep_cmd = replaceExtraCompilerVariables(tmp_dep_cmd, (*input), out);
1918 dep_cmd = fixEnvVariables(dep_cmd);
1919 if(FILE *proc = QT_POPEN(dep_cmd.toLatin1().constData(), "r")) {
1920 QString indeps;
1921 while(!feof(proc)) {
1922 int read_in = (int)fread(buff, 1, 255, proc);
1923 if(!read_in)
1924 break;
1925 indeps += QByteArray(buff, read_in);
1926 }
1927 QT_PCLOSE(proc);
1928 if(!indeps.isEmpty()) {
1929 QStringList dep_cmd_deps = indeps.replace('\n', ' ').simplified().split(' ');
1930 for(int i = 0; i < dep_cmd_deps.count(); ++i) {
1931 QString &file = dep_cmd_deps[i];
1932 if(!exists(file)) {
1933 QString localFile;
1934 QList<QMakeLocalFileName> depdirs = QMakeSourceFileInfo::dependencyPaths();
1935 for(QList<QMakeLocalFileName>::Iterator it = depdirs.begin();
1936 it != depdirs.end(); ++it) {
1937 if(exists((*it).real() + Option::dir_sep + file)) {
1938 localFile = (*it).local() + Option::dir_sep + file;
1939 break;
1940 }
1941 }
1942 file = localFile;
1943 }
1944 if(!file.isEmpty())
1945 file = fileFixify(file);
1946 }
1947 deps += dep_cmd_deps;
1948 }
1949 }
1950 //use the depend system to find includes of these included files
1951 QStringList inc_deps;
1952 for(int i = 0; i < deps.size(); ++i) {
1953 const QString dep = deps.at(i);
1954 if(QFile::exists(dep)) {
1955 SourceFileType type = TYPE_UNKNOWN;
1956 if(type == TYPE_UNKNOWN) {
1957 for(QStringList::Iterator cit = Option::c_ext.begin();
1958 cit != Option::c_ext.end(); ++cit) {
1959 if(dep.endsWith((*cit))) {
1960 type = TYPE_C;
1961 break;
1962 }
1963 }
1964 }
1965 if(type == TYPE_UNKNOWN) {
1966 for(QStringList::Iterator cppit = Option::cpp_ext.begin();
1967 cppit != Option::cpp_ext.end(); ++cppit) {
1968 if(dep.endsWith((*cppit))) {
1969 type = TYPE_C;
1970 break;
1971 }
1972 }
1973 }
1974 if(type == TYPE_UNKNOWN) {
1975 for(QStringList::Iterator hit = Option::h_ext.begin();
1976 type == TYPE_UNKNOWN && hit != Option::h_ext.end(); ++hit) {
1977 if(dep.endsWith((*hit))) {
1978 type = TYPE_C;
1979 break;
1980 }
1981 }
1982 }
1983 if(type != TYPE_UNKNOWN) {
1984 if(!QMakeSourceFileInfo::containsSourceFile(dep, type))
1985 QMakeSourceFileInfo::addSourceFile(dep, type);
1986 inc_deps += QMakeSourceFileInfo::dependencies(dep);
1987 }
1988 }
1989 }
1990 deps += inc_deps;
1991 }
1992 for(int i = 0; i < deps.size(); ) {
1993 QString &dep = deps[i];
1994 dep = Option::fixPathToTargetOS(unescapeFilePath(dep), false);
1995 if(out == dep)
1996 deps.removeAt(i);
1997 else
1998 ++i;
1999 }
2000 t << escapeDependencyPath(out) << ": " << valList(escapeDependencyPaths(deps)) << "\n\t"
2001 << cmd << endl << endl;
2002 }
2003 }
2004 t << "compiler_clean: " << clean_targets << endl << endl;
2005}
2006
2007void
2008MakefileGenerator::writeExtraCompilerVariables(QTextStream &t)
2009{
2010 bool first = true;
2011 const QStringList &quc = project->values("QMAKE_EXTRA_COMPILERS");
2012 for(QStringList::ConstIterator it = quc.begin(); it != quc.end(); ++it) {
2013 const QStringList &vars = project->values((*it) + ".variables");
2014 for(QStringList::ConstIterator varit = vars.begin(); varit != vars.end(); ++varit) {
2015 if(first) {
2016 t << "\n####### Custom Compiler Variables" << endl;
2017 first = false;
2018 }
2019 t << "QMAKE_COMP_" << (*varit) << " = "
2020 << valList(project->values((*varit))) << endl;
2021 }
2022 }
2023 if(!first)
2024 t << endl;
2025}
2026
2027void
2028MakefileGenerator::writeExtraVariables(QTextStream &t)
2029{
2030 bool first = true;
2031 QMap<QString, QStringList> &vars = project->variables();
2032 QStringList &exports = project->values("QMAKE_EXTRA_VARIABLES");
2033 for(QMap<QString, QStringList>::Iterator it = vars.begin(); it != vars.end(); ++it) {
2034 for(QStringList::Iterator exp_it = exports.begin(); exp_it != exports.end(); ++exp_it) {
2035 QRegExp rx((*exp_it), Qt::CaseInsensitive, QRegExp::Wildcard);
2036 if(rx.exactMatch(it.key())) {
2037 if(first) {
2038 t << "\n####### Custom Variables" << endl;
2039 first = false;
2040 }
2041 t << "EXPORT_" << it.key() << " = " << it.value().join(" ") << endl;
2042 }
2043 }
2044 }
2045 if(!first)
2046 t << endl;
2047}
2048
2049bool
2050MakefileGenerator::writeStubMakefile(QTextStream &t)
2051{
2052 t << "QMAKE = " << (project->isEmpty("QMAKE_QMAKE") ? QString("qmake") : var("QMAKE_QMAKE")) << endl;
2053 QStringList &qut = project->values("QMAKE_EXTRA_TARGETS");
2054 for(QStringList::ConstIterator it = qut.begin(); it != qut.end(); ++it)
2055 t << *it << " ";
2056 //const QString ofile = Option::fixPathToTargetOS(fileFixify(Option::output.fileName()));
2057 t << "first all clean install distclean uninstall: " << "qmake" << endl
2058 << "qmake_all:" << endl;
2059 writeMakeQmake(t);
2060 if(project->isEmpty("QMAKE_NOFORCE"))
2061 t << "FORCE:" << endl << endl;
2062 return true;
2063}
2064
2065bool
2066MakefileGenerator::writeMakefile(QTextStream &t)
2067{
2068 t << "####### Compile" << endl << endl;
2069 writeObj(t, "SOURCES");
2070 writeObj(t, "GENERATED_SOURCES");
2071
2072 t << "####### Install" << endl << endl;
2073 writeInstalls(t, "INSTALLS");
2074
2075 if(project->isEmpty("QMAKE_NOFORCE"))
2076 t << "FORCE:" << endl << endl;
2077 return true;
2078}
2079
2080QString MakefileGenerator::buildArgs(const QString &outdir)
2081{
2082 QString ret;
2083 //special variables
2084 if(!project->isEmpty("QMAKE_ABSOLUTE_SOURCE_PATH"))
2085 ret += " QMAKE_ABSOLUTE_SOURCE_PATH=" + escapeFilePath(project->first("QMAKE_ABSOLUTE_SOURCE_PATH"));
2086
2087 //warnings
2088 else if(Option::warn_level == WarnNone)
2089 ret += " -Wnone";
2090 else if(Option::warn_level == WarnAll)
2091 ret += " -Wall";
2092 else if(Option::warn_level & WarnParser)
2093 ret += " -Wparser";
2094 //other options
2095 if(!Option::user_template.isEmpty())
2096 ret += " -t " + Option::user_template;
2097 if(!Option::user_template_prefix.isEmpty())
2098 ret += " -tp " + Option::user_template_prefix;
2099 if(!Option::mkfile::do_cache)
2100 ret += " -nocache";
2101 if(!Option::mkfile::do_deps)
2102 ret += " -nodepend";
2103 if(!Option::mkfile::do_dep_heuristics)
2104 ret += " -nodependheuristics";
2105 if(!Option::mkfile::qmakespec_commandline.isEmpty())
2106 ret += " -spec " + specdir(outdir);
2107 if(Option::target_mode == Option::TARG_MAC9_MODE)
2108 ret += " -mac9";
2109 else if(Option::target_mode == Option::TARG_MACX_MODE)
2110 ret += " -macx";
2111 else if(Option::target_mode == Option::TARG_UNIX_MODE)
2112 ret += " -unix";
2113 else if(Option::target_mode == Option::TARG_WIN_MODE)
2114 ret += " -win32";
2115 else if(Option::target_mode == Option::TARG_OS2_MODE)
2116 ret += " -os2";
2117 else if(Option::target_mode == Option::TARG_QNX6_MODE)
2118 ret += " -qnx6";
2119
2120 //configs
2121 for(QStringList::Iterator it = Option::user_configs.begin();
2122 it != Option::user_configs.end(); ++it)
2123 ret += " -config " + (*it);
2124 //arguments
2125 for(QStringList::Iterator it = Option::before_user_vars.begin();
2126 it != Option::before_user_vars.end(); ++it) {
2127 if((*it).left(qstrlen("QMAKE_ABSOLUTE_SOURCE_PATH")) != "QMAKE_ABSOLUTE_SOURCE_PATH")
2128 ret += " " + escapeFilePath((*it));
2129 }
2130 if(Option::after_user_vars.count()) {
2131 ret += " -after ";
2132 for(QStringList::Iterator it = Option::after_user_vars.begin();
2133 it != Option::after_user_vars.end(); ++it) {
2134 if((*it).left(qstrlen("QMAKE_ABSOLUTE_SOURCE_PATH")) != "QMAKE_ABSOLUTE_SOURCE_PATH")
2135 ret += " " + escapeFilePath((*it));
2136 }
2137 }
2138 return ret;
2139}
2140
2141//could get stored argv, but then it would have more options than are
2142//probably necesary this will try to guess the bare minimum..
2143QString MakefileGenerator::build_args(const QString &outdir)
2144{
2145 QString ret = "$(QMAKE)";
2146
2147 // general options and arguments
2148 ret += buildArgs(outdir);
2149
2150 //output
2151 QString ofile = Option::fixPathToTargetOS(fileFixify(Option::output.fileName()));
2152 if(!ofile.isEmpty() && ofile != project->first("QMAKE_MAKEFILE"))
2153 ret += " -o " + escapeFilePath(ofile);
2154
2155 //inputs
2156 ret += " " + escapeFilePath(fileFixify(project->projectFile(), outdir));
2157
2158 return ret;
2159}
2160
2161void
2162MakefileGenerator::writeHeader(QTextStream &t)
2163{
2164 t << "#############################################################################" << endl;
2165 t << "# Makefile for building: " << escapeFilePath(var("TARGET")) << endl;
2166 t << "# Generated by qmake (" << qmake_version() << ") (Qt " << QT_VERSION_STR << ") on: ";
2167 t << QDateTime::currentDateTime().toString() << endl;
2168 t << "# Project: " << fileFixify(project->projectFile()) << endl;
2169 t << "# Template: " << var("TEMPLATE") << endl;
2170 if(!project->isActiveConfig("build_pass"))
2171 t << "# Command: " << build_args().replace("$(QMAKE)",
2172 (project->isEmpty("QMAKE_QMAKE") ? QString("qmake") : var("QMAKE_QMAKE"))) << endl;
2173 t << "#############################################################################" << endl;
2174 t << endl;
2175}
2176
2177void
2178MakefileGenerator::writeSubDirs(QTextStream &t)
2179{
2180 QList<SubTarget*> targets;
2181 {
2182 const QStringList subdirs = project->values("SUBDIRS");
2183 for(int subdir = 0; subdir < subdirs.size(); ++subdir) {
2184 QString fixedSubdir = subdirs[subdir];
2185 fixedSubdir = fixedSubdir.replace(QRegExp("[^a-zA-Z0-9_]"),"-");
2186
2187 SubTarget *st = new SubTarget;
2188 st->name = subdirs[subdir];
2189 targets.append(st);
2190
2191 bool fromFile = false;
2192 QString file = subdirs[subdir];
2193 if(!project->isEmpty(fixedSubdir + ".file")) {
2194 if(!project->isEmpty(fixedSubdir + ".subdir"))
2195 warn_msg(WarnLogic, "Cannot assign both file and subdir for subdir %s",
2196 subdirs[subdir].toLatin1().constData());
2197 file = project->first(fixedSubdir + ".file");
2198 fromFile = true;
2199 } else if(!project->isEmpty(fixedSubdir + ".subdir")) {
2200 file = project->first(fixedSubdir + ".subdir");
2201 fromFile = false;
2202 } else {
2203 fromFile = file.endsWith(Option::pro_ext);
2204 }
2205 file = Option::fixPathToTargetOS(file);
2206
2207 if(fromFile) {
2208 int slsh = file.lastIndexOf(Option::dir_sep);
2209 if(slsh != -1) {
2210 st->in_directory = file.left(slsh+1);
2211 st->profile = file.mid(slsh+1);
2212 } else {
2213 st->profile = file;
2214 }
2215 } else {
2216 if(!file.isEmpty() && !project->isActiveConfig("subdir_first_pro"))
2217 st->profile = file.section(Option::dir_sep, -1) + Option::pro_ext;
2218 st->in_directory = file;
2219 }
2220 while(st->in_directory.right(1) == Option::dir_sep)
2221 st->in_directory = st->in_directory.left(st->in_directory.length() - 1);
2222 if(fileInfo(st->in_directory).isRelative())
2223 st->out_directory = st->in_directory;
2224 else
2225 st->out_directory = fileFixify(st->in_directory, qmake_getpwd(), Option::output_dir);
2226 if(!project->isEmpty(fixedSubdir + ".makefile")) {
2227 st->makefile = project->first(fixedSubdir + ".makefile");
2228 } else {
2229 st->makefile = "$(MAKEFILE)";
2230 if(!st->profile.isEmpty()) {
2231 QString basename = st->in_directory;
2232 int new_slsh = basename.lastIndexOf(Option::dir_sep);
2233 if(new_slsh != -1)
2234 basename = basename.mid(new_slsh+1);
2235 if(st->profile != basename + Option::pro_ext)
2236 st->makefile += "." + st->profile.left(st->profile.length() - Option::pro_ext.length());
2237 }
2238 }
2239 if(!project->isEmpty(fixedSubdir + ".depends")) {
2240 const QStringList depends = project->values(fixedSubdir + ".depends");
2241 for(int depend = 0; depend < depends.size(); ++depend) {
2242 bool found = false;
2243 for(int subDep = 0; subDep < subdirs.size(); ++subDep) {
2244 if(subdirs[subDep] == depends.at(depend)) {
2245 QString fixedSubDep = subdirs[subDep];
2246 fixedSubDep = fixedSubDep.replace(QRegExp("[^a-zA-Z0-9_]"),"-");
2247 if(!project->isEmpty(fixedSubDep + ".target")) {
2248 st->depends += project->first(fixedSubDep + ".target");
2249 } else {
2250 QString d = Option::fixPathToLocalOS(subdirs[subDep]);
2251 if(!project->isEmpty(fixedSubDep + ".file"))
2252 d = project->first(fixedSubDep + ".file");
2253 else if(!project->isEmpty(fixedSubDep + ".subdir"))
2254 d = project->first(fixedSubDep + ".subdir");
2255 st->depends += "sub-" + d.replace(QRegExp("[^a-zA-Z0-9_]"),"-");
2256 }
2257 found = true;
2258 break;
2259 }
2260 }
2261 if(!found) {
2262 QString depend_str = depends.at(depend);
2263 st->depends += depend_str.replace(QRegExp("[^a-zA-Z0-9_]"),"-");
2264 }
2265 }
2266 }
2267 if(!project->isEmpty(fixedSubdir + ".target")) {
2268 st->target = project->first(fixedSubdir + ".target");
2269 } else {
2270 st->target = "sub-" + file;
2271 st->target = st->target.replace(QRegExp("[^a-zA-Z0-9_]"),"-");
2272 }
2273 }
2274 }
2275 t << "first: make_default" << endl;
2276 int flags = SubTargetInstalls;
2277 if(project->isActiveConfig("ordered"))
2278 flags |= SubTargetOrdered;
2279 writeSubTargets(t, targets, flags);
2280 qDeleteAll(targets);
2281}
2282
2283void
2284MakefileGenerator::writeSubTargets(QTextStream &t, QList<MakefileGenerator::SubTarget*> targets, int flags)
2285{
2286 // blasted includes
2287 QStringList &qeui = project->values("QMAKE_EXTRA_INCLUDES");
2288 for(QStringList::Iterator qeui_it = qeui.begin(); qeui_it != qeui.end(); ++qeui_it)
2289 t << "include " << (*qeui_it) << endl;
2290
2291 QString ofile = Option::fixPathToTargetOS(Option::output.fileName());
2292 if(ofile.lastIndexOf(Option::dir_sep) != -1)
2293 ofile = ofile.right(ofile.length() - ofile.lastIndexOf(Option::dir_sep) -1);
2294 t << "MAKEFILE = " << ofile << endl;
2295 /* Calling Option::fixPathToTargetOS() is necessary for MinGW/MSYS, which requires
2296 * back-slashes to be turned into slashes. */
2297 t << "QMAKE = " << Option::fixPathToTargetOS(var("QMAKE_QMAKE")) << endl;
2298 t << "DEL_FILE = " << var("QMAKE_DEL_FILE") << endl;
2299 t << "CHK_DIR_EXISTS= " << var("QMAKE_CHK_DIR_EXISTS") << endl;
2300 t << "MKDIR = " << var("QMAKE_MKDIR") << endl;
2301 t << "COPY = " << var("QMAKE_COPY") << endl;
2302 t << "COPY_FILE = " << var("QMAKE_COPY_FILE") << endl;
2303 t << "COPY_DIR = " << var("QMAKE_COPY_DIR") << endl;
2304 t << "INSTALL_FILE = " << var("QMAKE_INSTALL_FILE") << endl;
2305 t << "INSTALL_PROGRAM = " << var("QMAKE_INSTALL_PROGRAM") << endl;
2306 t << "INSTALL_DIR = " << var("QMAKE_INSTALL_DIR") << endl;
2307 t << "DEL_FILE = " << var("QMAKE_DEL_FILE") << endl;
2308 t << "SYMLINK = " << var("QMAKE_SYMBOLIC_LINK") << endl;
2309 t << "DEL_DIR = " << var("QMAKE_DEL_DIR") << endl;
2310 t << "MOVE = " << var("QMAKE_MOVE") << endl;
2311 t << "CHK_DIR_EXISTS= " << var("QMAKE_CHK_DIR_EXISTS") << endl;
2312 t << "MKDIR = " << var("QMAKE_MKDIR") << endl;
2313 writeExtraVariables(t);
2314 t << "SUBTARGETS = "; // subtargets are sub-directory
2315 for(int target = 0; target < targets.size(); ++target)
2316 t << " \\\n\t\t" << targets.at(target)->target;
2317 t << endl << endl;
2318
2319 QStringList targetSuffixes;
2320 const QString abs_source_path = project->first("QMAKE_ABSOLUTE_SOURCE_PATH");
2321 targetSuffixes << "make_default" << "make_first" << "all" << "clean" << "distclean"
2322 << QString((flags & SubTargetInstalls) ? "install_subtargets" : "install")
2323 << QString((flags & SubTargetInstalls) ? "uninstall_subtargets" : "uninstall");
2324
2325 // generate target rules
2326 for(int target = 0; target < targets.size(); ++target) {
2327 SubTarget *subtarget = targets.at(target);
2328 QString in_directory = subtarget->in_directory;
2329 if(!in_directory.isEmpty() && !in_directory.endsWith(Option::dir_sep))
2330 in_directory += Option::dir_sep;
2331 QString out_directory = subtarget->out_directory;
2332 if(!out_directory.isEmpty() && !out_directory.endsWith(Option::dir_sep))
2333 out_directory += Option::dir_sep;
2334 if(!abs_source_path.isEmpty() && out_directory.startsWith(abs_source_path))
2335 out_directory = Option::output_dir + out_directory.mid(abs_source_path.length());
2336
2337 QString mkfile = subtarget->makefile;
2338 if(!in_directory.isEmpty())
2339 mkfile.prepend(out_directory);
2340
2341 QString in_directory_cdin, in_directory_cdout, out_directory_cdin, out_directory_cdout;
2342#define MAKE_CD_IN_AND_OUT(directory) \
2343 if(!directory.isEmpty()) { \
2344 if(project->isActiveConfig("cd_change_global")) { \
2345 directory ## _cdin = "\n\tcd " + directory + "\n\t"; \
2346 QDir pwd(Option::output_dir); \
2347 QStringList in = directory.split(Option::dir_sep), out; \
2348 for(int i = 0; i < in.size(); i++) { \
2349 if(in.at(i) == "..") \
2350 out.prepend(fileInfo(pwd.path()).fileName()); \
2351 else if(in.at(i) != ".") \
2352 out.prepend(".."); \
2353 pwd.cd(in.at(i)); \
2354 } \
2355 directory ## _cdout = "\n\t@cd " + out.join(Option::dir_sep); \
2356 } else { \
2357 directory ## _cdin = "\n\tcd " + directory + " && "; \
2358 } \
2359 } else { \
2360 directory ## _cdin = "\n\t"; \
2361 }
2362 MAKE_CD_IN_AND_OUT(in_directory);
2363 MAKE_CD_IN_AND_OUT(out_directory);
2364
2365 //qmake it
2366 if(!subtarget->profile.isEmpty()) {
2367 QString out = out_directory + subtarget->makefile,
2368 in = fileFixify(in_directory + subtarget->profile, in_directory);
2369 if(in.startsWith(in_directory))
2370 in = in.mid(in_directory.length());
2371 if(out.startsWith(in_directory))
2372 out = out.mid(in_directory.length());
2373 t << mkfile << ": " << "\n\t";
2374 if(!in_directory.isEmpty()) {
2375 t << mkdir_p_asstring(in_directory)
2376 << in_directory_cdin
2377 << "$(QMAKE) " << in << buildArgs(in_directory) << " -o " << out
2378 << in_directory_cdout << endl;
2379 } else {
2380 t << "$(QMAKE) " << in << buildArgs(in_directory) << " -o " << out << endl;
2381 }
2382 t << subtarget->target << "-qmake_all: ";
2383 if(project->isEmpty("QMAKE_NOFORCE"))
2384 t << " FORCE";
2385 t << "\n\t";
2386 if(!in_directory.isEmpty()) {
2387 t << mkdir_p_asstring(in_directory)
2388 << in_directory_cdin
2389 << "$(QMAKE) " << in << buildArgs(in_directory) << " -o " << out
2390 << in_directory_cdout << endl;
2391 } else {
2392 t << "$(QMAKE) " << in << buildArgs(in_directory) << " -o " << out << endl;
2393 }
2394 }
2395
2396 QString makefilein = " -f " + subtarget->makefile;
2397
2398 { //actually compile
2399 t << subtarget->target << ": " << mkfile;
2400 if(!subtarget->depends.isEmpty())
2401 t << " " << valList(subtarget->depends);
2402 if(project->isEmpty("QMAKE_NOFORCE"))
2403 t << " FORCE";
2404 t << out_directory_cdin
2405 << "$(MAKE)" << makefilein
2406 << out_directory_cdout << endl;
2407 }
2408
2409 for(int suffix = 0; suffix < targetSuffixes.size(); ++suffix) {
2410 QString s = targetSuffixes.at(suffix);
2411 if(s == "install_subtargets")
2412 s = "install";
2413 else if(s == "uninstall_subtargets")
2414 s = "uninstall";
2415 else if(s == "make_first")
2416 s = "first";
2417 else if(s == "make_default")
2418 s = QString();
2419
2420 if(flags & SubTargetOrdered) {
2421 t << subtarget->target << "-" << targetSuffixes.at(suffix) << "-ordered: " << mkfile;
2422 if(target)
2423 t << " " << targets.at(target-1)->target << "-" << targetSuffixes.at(suffix) << "-ordered ";
2424 if(project->isEmpty("QMAKE_NOFORCE"))
2425 t << " FORCE";
2426 t << out_directory_cdin
2427 << "$(MAKE)" << makefilein << " " << s
2428 << out_directory_cdout << endl;
2429 }
2430 t << subtarget->target << "-" << targetSuffixes.at(suffix) << ": " << mkfile;
2431 if(!subtarget->depends.isEmpty())
2432 t << " " << valGlue(subtarget->depends, QString(), "-" + targetSuffixes.at(suffix) + " ",
2433 "-"+targetSuffixes.at(suffix));
2434 if(project->isEmpty("QMAKE_NOFORCE"))
2435 t << " FORCE";
2436 t << out_directory_cdin
2437 << "$(MAKE)" << makefilein << " " << s
2438 << out_directory_cdout << endl;
2439 }
2440 }
2441 t << endl;
2442
2443 if(project->values("QMAKE_INTERNAL_QMAKE_DEPS").indexOf("qmake_all") == -1)
2444 project->values("QMAKE_INTERNAL_QMAKE_DEPS").append("qmake_all");
2445
2446 writeMakeQmake(t);
2447
2448 t << "qmake_all:";
2449 if(!targets.isEmpty()) {
2450 for(QList<SubTarget*>::Iterator it = targets.begin(); it != targets.end(); ++it) {
2451 if(!(*it)->profile.isEmpty())
2452 t << " " << (*it)->target << "-" << "qmake_all";
2453 }
2454 }
2455 if(project->isEmpty("QMAKE_NOFORCE"))
2456 t << " FORCE";
2457 if(project->isActiveConfig("no_empty_targets"))
2458 t << "\n\t" << "@cd .";
2459 t << endl << endl;
2460
2461 for(int s = 0; s < targetSuffixes.size(); ++s) {
2462 QString suffix = targetSuffixes.at(s);
2463 if(!(flags & SubTargetInstalls) && suffix.endsWith("install"))
2464 continue;
2465
2466 t << suffix << ":";
2467 for(int target = 0; target < targets.size(); ++target) {
2468 QString targetRule = targets.at(target)->target + "-" + suffix;
2469 if(flags & SubTargetOrdered)
2470 targetRule += "-ordered";
2471 t << " " << targetRule;
2472 }
2473 if(suffix == "all" || suffix == "make_first")
2474 t << varGlue("ALL_DEPS"," "," ","");
2475 if(suffix == "clean")
2476 t << varGlue("CLEAN_DEPS"," "," ","");
2477 if(project->isEmpty("QMAKE_NOFORCE"))
2478 t << " FORCE";
2479 t << endl;
2480 if(suffix == "clean") {
2481 t << varGlue("QMAKE_CLEAN","\t-$(DEL_FILE) ","\n\t-$(DEL_FILE) ", "\n");
2482 } else if(suffix == "distclean") {
2483 QString ofile = Option::fixPathToTargetOS(fileFixify(Option::output.fileName()));
2484 if(!ofile.isEmpty())
2485 t << "\t-$(DEL_FILE) " << ofile << endl;
2486 } else if(project->isActiveConfig("no_empty_targets")) {
2487 t << "\t" << "@cd ." << endl;
2488 }
2489 }
2490
2491 // user defined targets
2492 QStringList &qut = project->values("QMAKE_EXTRA_TARGETS");
2493 for(QStringList::Iterator qut_it = qut.begin(); qut_it != qut.end(); ++qut_it) {
2494 QString targ = var((*qut_it) + ".target"),
2495 cmd = var((*qut_it) + ".commands"), deps;
2496 if(targ.isEmpty())
2497 targ = (*qut_it);
2498 t << endl;
2499
2500 QStringList &deplist = project->values((*qut_it) + ".depends");
2501 for(QStringList::Iterator dep_it = deplist.begin(); dep_it != deplist.end(); ++dep_it) {
2502 QString dep = var((*dep_it) + ".target");
2503 if(dep.isEmpty())
2504 dep = Option::fixPathToTargetOS(*dep_it, false);
2505 deps += " " + dep;
2506 }
2507 if(project->values((*qut_it) + ".CONFIG").indexOf("recursive") != -1) {
2508 QSet<QString> recurse;
2509 if(project->isSet((*qut_it) + ".recurse")) {
2510 recurse = project->values((*qut_it) + ".recurse").toSet();
2511 } else {
2512 for(int target = 0; target < targets.size(); ++target)
2513 recurse.insert(targets.at(target)->name);
2514 }
2515 for(int target = 0; target < targets.size(); ++target) {
2516 SubTarget *subtarget = targets.at(target);
2517 QString in_directory = subtarget->in_directory;
2518 if(!in_directory.isEmpty() && !in_directory.endsWith(Option::dir_sep))
2519 in_directory += Option::dir_sep;
2520 QString out_directory = subtarget->out_directory;
2521 if(!out_directory.isEmpty() && !out_directory.endsWith(Option::dir_sep))
2522 out_directory += Option::dir_sep;
2523 if(!abs_source_path.isEmpty() && out_directory.startsWith(abs_source_path))
2524 out_directory = Option::output_dir + out_directory.mid(abs_source_path.length());
2525
2526 if(!recurse.contains(subtarget->name))
2527 continue;
2528 QString mkfile = subtarget->makefile;
2529 if(!in_directory.isEmpty()) {
2530 if(!out_directory.endsWith(Option::dir_sep))
2531 mkfile.prepend(out_directory + Option::dir_sep);
2532 else
2533 mkfile.prepend(out_directory);
2534 }
2535 QString out_directory_cdin, out_directory_cdout;
2536 MAKE_CD_IN_AND_OUT(out_directory);
2537
2538 //don't need the makefile arg if it isn't changed
2539 QString makefilein;
2540 if(subtarget->makefile != "$(MAKEFILE)")
2541 makefilein = " -f " + subtarget->makefile;
2542
2543 //write the rule/depends
2544 if(flags & SubTargetOrdered) {
2545 const QString dep = subtarget->target + "-" + (*qut_it) + "_ordered";
2546 t << dep << ": " << mkfile;
2547 if(target)
2548 t << " " << targets.at(target-1)->target << "-" << (*qut_it) << "_ordered ";
2549 deps += " " + dep;
2550 } else {
2551 const QString dep = subtarget->target + "-" + (*qut_it);
2552 t << dep << ": " << mkfile;
2553 if(!subtarget->depends.isEmpty())
2554 t << " " << valGlue(subtarget->depends, QString(), "-" + (*qut_it) + " ", "-" + (*qut_it));
2555 deps += " " + dep;
2556 }
2557
2558 QString sub_targ = targ;
2559 if(project->isSet((*qut_it) + ".recurse_target"))
2560 sub_targ = project->first((*qut_it) + ".recurse_target");
2561
2562 //write the commands
2563 if(!out_directory.isEmpty()) {
2564 t << out_directory_cdin
2565 << "$(MAKE)" << makefilein << " " << sub_targ
2566 << out_directory_cdout << endl;
2567 } else {
2568 t << "\n\t"
2569 << "$(MAKE)" << makefilein << " " << sub_targ << endl;
2570 }
2571 }
2572 }
2573 if(project->isEmpty("QMAKE_NOFORCE") &&
2574 project->values((*qut_it) + ".CONFIG").indexOf("phony") != -1)
2575 deps += " FORCE";
2576 t << targ << ":" << deps << "\n";
2577 if(!cmd.isEmpty())
2578 t << "\t" << cmd << endl;
2579 }
2580
2581 if(flags & SubTargetInstalls) {
2582 project->values("INSTALLDEPS") += "install_subtargets";
2583 project->values("UNINSTALLDEPS") += "uninstall_subtargets";
2584 writeInstalls(t, "INSTALLS", true);
2585 }
2586
2587 if(project->isEmpty("QMAKE_NOFORCE"))
2588 t << "FORCE:" << endl << endl;
2589}
2590
2591void
2592MakefileGenerator::writeMakeQmake(QTextStream &t)
2593{
2594 QString ofile = Option::fixPathToTargetOS(fileFixify(Option::output.fileName()));
2595 if(project->isEmpty("QMAKE_FAILED_REQUIREMENTS") && !project->isEmpty("QMAKE_INTERNAL_PRL_FILE")) {
2596 QStringList files = fileFixify(Option::mkfile::project_files);
2597 t << escapeDependencyPath(project->first("QMAKE_INTERNAL_PRL_FILE")) << ": " << "\n\t"
2598 << "@$(QMAKE) -prl " << buildArgs() << " " << files.join(" ") << endl;
2599 }
2600
2601 QString pfile = project->projectFile();
2602 if(pfile != "(stdin)") {
2603 QString qmake = build_args();
2604 if(!ofile.isEmpty() && !project->isActiveConfig("no_autoqmake")) {
2605 t << escapeFilePath(ofile) << ": " << escapeDependencyPath(fileFixify(pfile)) << " ";
2606 if(Option::mkfile::do_cache)
2607 t << escapeDependencyPath(fileFixify(Option::mkfile::cachefile)) << " ";
2608 if(!specdir().isEmpty()) {
2609 if(exists(Option::fixPathToLocalOS(specdir()+QDir::separator()+"qmake.conf")))
2610 t << escapeDependencyPath(specdir() + Option::dir_sep + "qmake.conf") << " ";
2611 else if(exists(Option::fixPathToLocalOS(specdir()+QDir::separator()+"tmake.conf")))
2612 t << escapeDependencyPath(specdir() + Option::dir_sep + "tmake.conf") << " ";
2613 }
2614 const QStringList &included = project->values("QMAKE_INTERNAL_INCLUDED_FILES");
2615 t << escapeDependencyPaths(included).join(" \\\n\t\t") << "\n\t"
2616 << qmake << endl;
2617 for(int include = 0; include < included.size(); ++include) {
2618 const QString i(included.at(include));
2619 if(!i.isEmpty())
2620 t << i << ":" << endl;
2621 }
2622 }
2623 if(project->first("QMAKE_ORIG_TARGET") != "qmake") {
2624 t << "qmake: " <<
2625 project->values("QMAKE_INTERNAL_QMAKE_DEPS").join(" \\\n\t\t");
2626 if(project->isEmpty("QMAKE_NOFORCE"))
2627 t << " FORCE";
2628 t << "\n\t" << "@" << qmake << endl << endl;
2629 }
2630 }
2631}
2632
2633QFileInfo
2634MakefileGenerator::fileInfo(QString file) const
2635{
2636 static QHash<FileInfoCacheKey, QFileInfo> *cache = 0;
2637 static QFileInfo noInfo = QFileInfo();
2638 if(!cache) {
2639 cache = new QHash<FileInfoCacheKey, QFileInfo>;
2640 qmakeAddCacheClear(qmakeDeleteCacheClear_QHashFileInfoCacheKeyQFileInfo, (void**)&cache);
2641 }
2642 FileInfoCacheKey cacheKey(file);
2643 QFileInfo value = cache->value(cacheKey, noInfo);
2644 if (value != noInfo)
2645 return value;
2646
2647 QFileInfo fi(file);
2648 if (fi.exists())
2649 cache->insert(cacheKey, fi);
2650 return fi;
2651}
2652
2653QString
2654MakefileGenerator::unescapeFilePath(const QString &path) const
2655{
2656 QString ret = path;
2657 if(!ret.isEmpty()) {
2658 if(ret.contains(QLatin1String("\\ ")))
2659 ret.replace(QLatin1String("\\ "), QLatin1String(" "));
2660 if(ret.contains(QLatin1Char('\"')))
2661 ret.remove(QLatin1Char('\"'));
2662 }
2663 return ret;
2664}
2665
2666QStringList
2667MakefileGenerator::escapeFilePaths(const QStringList &paths) const
2668{
2669 QStringList ret;
2670 for(int i = 0; i < paths.size(); ++i)
2671 ret.append(escapeFilePath(paths.at(i)));
2672 return ret;
2673}
2674
2675QStringList
2676MakefileGenerator::escapeDependencyPaths(const QStringList &paths) const
2677{
2678 QStringList ret;
2679 for(int i = 0; i < paths.size(); ++i)
2680 ret.append(escapeDependencyPath(paths.at(i)));
2681 return ret;
2682}
2683
2684QStringList
2685MakefileGenerator::unescapeFilePaths(const QStringList &paths) const
2686{
2687 QStringList ret;
2688 for(int i = 0; i < paths.size(); ++i)
2689 ret.append(unescapeFilePath(paths.at(i)));
2690 return ret;
2691}
2692
2693QStringList
2694MakefileGenerator::fileFixify(const QStringList& files, const QString &out_dir, const QString &in_dir,
2695 FileFixifyType fix, bool canon) const
2696{
2697 if(files.isEmpty())
2698 return files;
2699 QStringList ret;
2700 for(QStringList::ConstIterator it = files.begin(); it != files.end(); ++it) {
2701 if(!(*it).isEmpty())
2702 ret << fileFixify((*it), out_dir, in_dir, fix, canon);
2703 }
2704 return ret;
2705}
2706
2707QString
2708MakefileGenerator::fileFixify(const QString& file, const QString &out_d, const QString &in_d,
2709 FileFixifyType fix, bool canon) const
2710{
2711 if(file.isEmpty())
2712 return file;
2713 QString ret = unescapeFilePath(file);
2714
2715 //setup the cache
2716 static QHash<FileFixifyCacheKey, QString> *cache = 0;
2717 if(!cache) {
2718 cache = new QHash<FileFixifyCacheKey, QString>;
2719 qmakeAddCacheClear(qmakeDeleteCacheClear_QHashFileFixifyCacheKeyQString, (void**)&cache);
2720 }
2721 FileFixifyCacheKey cacheKey(ret, out_d, in_d, fix, canon);
2722 QString cacheVal = cache->value(cacheKey);
2723 if(!cacheVal.isNull())
2724 return cacheVal;
2725
2726 //do the fixin'
2727 const QString pwd = qmake_getpwd() + "/";
2728 QString orig_file = ret;
2729 if(ret.startsWith(QLatin1Char('~'))) {
2730 if(ret.startsWith(QLatin1String("~/")))
2731 ret = QDir::homePath() + Option::dir_sep + ret.mid(1);
2732 else
2733 warn_msg(WarnLogic, "Unable to expand ~ in %s", ret.toLatin1().constData());
2734 }
2735 if(fix == FileFixifyAbsolute || (fix == FileFixifyDefault && project->isActiveConfig("no_fixpath"))) {
2736 if(fix == FileFixifyAbsolute && QDir::isRelativePath(ret)) //already absolute
2737 ret.prepend(pwd);
2738 ret = Option::fixPathToTargetOS(ret, false, canon);
2739 } else { //fix it..
2740 QString out_dir = QDir(Option::output_dir).absoluteFilePath(out_d);
2741 QString in_dir = QDir(pwd).absoluteFilePath(in_d);
2742 {
2743 QFileInfo in_fi(fileInfo(in_dir));
2744 if(in_fi.exists())
2745 in_dir = in_fi.canonicalFilePath();
2746 QFileInfo out_fi(fileInfo(out_dir));
2747 if(out_fi.exists())
2748 out_dir = out_fi.canonicalFilePath();
2749 }
2750
2751 QString qfile(Option::fixPathToLocalOS(ret, true, canon));
2752 QFileInfo qfileinfo(fileInfo(qfile));
2753 if(out_dir != in_dir || !qfileinfo.isRelative()) {
2754 if(qfileinfo.isRelative()) {
2755 ret = in_dir + "/" + qfile;
2756 qfileinfo.setFile(ret);
2757 }
2758 ret = Option::fixPathToTargetOS(ret, false, canon);
2759 if(canon && qfileinfo.exists() &&
2760 file == Option::fixPathToTargetOS(ret, true, canon))
2761 ret = Option::fixPathToTargetOS(qfileinfo.canonicalFilePath());
2762 QString match_dir = Option::fixPathToTargetOS(out_dir, false, canon);
2763 if(ret == match_dir) {
2764 ret = "";
2765 } else if(ret.startsWith(match_dir + Option::dir_sep)) {
2766 ret = ret.mid(match_dir.length() + Option::dir_sep.length());
2767 } else {
2768 //figure out the depth
2769 int depth = 4;
2770 if(Option::qmake_mode == Option::QMAKE_GENERATE_MAKEFILE ||
2771 Option::qmake_mode == Option::QMAKE_GENERATE_PRL) {
2772 if(project && !project->isEmpty("QMAKE_PROJECT_DEPTH"))
2773 depth = project->first("QMAKE_PROJECT_DEPTH").toInt();
2774 else if(Option::mkfile::cachefile_depth != -1)
2775 depth = Option::mkfile::cachefile_depth;
2776 }
2777 //calculate how much can be removed
2778 QString dot_prefix;
2779 for(int i = 1; i <= depth; i++) {
2780 int sl = match_dir.lastIndexOf(Option::dir_sep);
2781 if(sl == -1)
2782 break;
2783 match_dir = match_dir.left(sl);
2784 if(match_dir.isEmpty())
2785 break;
2786 if(ret.startsWith(match_dir + Option::dir_sep)) {
2787 //concat
2788 int remlen = ret.length() - (match_dir.length() + 1);
2789 if(remlen < 0)
2790 remlen = 0;
2791 ret = ret.right(remlen);
2792 //prepend
2793 for(int o = 0; o < i; o++)
2794 dot_prefix += ".." + Option::dir_sep;
2795 }
2796 }
2797 ret.prepend(dot_prefix);
2798 }
2799 } else {
2800 ret = Option::fixPathToTargetOS(ret, false, canon);
2801 }
2802 }
2803 if(ret.isEmpty())
2804 ret = ".";
2805 debug_msg(3, "Fixed[%d,%d] %s :: to :: %s [%s::%s] [%s::%s]", fix, canon, orig_file.toLatin1().constData(),
2806 ret.toLatin1().constData(), in_d.toLatin1().constData(), out_d.toLatin1().constData(),
2807 pwd.toLatin1().constData(), Option::output_dir.toLatin1().constData());
2808 cache->insert(cacheKey, ret);
2809 return ret;
2810}
2811
2812void
2813MakefileGenerator::checkMultipleDefinition(const QString &f, const QString &w)
2814{
2815 if(!(Option::warn_level & WarnLogic))
2816 return;
2817 QString file = f;
2818 int slsh = f.lastIndexOf(Option::dir_sep);
2819 if(slsh != -1)
2820 file = file.right(file.length() - slsh - 1);
2821 QStringList &l = project->values(w);
2822 for(QStringList::Iterator val_it = l.begin(); val_it != l.end(); ++val_it) {
2823 QString file2((*val_it));
2824 slsh = file2.lastIndexOf(Option::dir_sep);
2825 if(slsh != -1)
2826 file2 = file2.right(file2.length() - slsh - 1);
2827 if(file2 == file) {
2828 warn_msg(WarnLogic, "Found potential symbol conflict of %s (%s) in %s",
2829 file.toLatin1().constData(), (*val_it).toLatin1().constData(), w.toLatin1().constData());
2830 break;
2831 }
2832 }
2833}
2834
2835QMakeLocalFileName
2836MakefileGenerator::fixPathForFile(const QMakeLocalFileName &file, bool forOpen)
2837{
2838 if(forOpen)
2839 return QMakeLocalFileName(fileFixify(file.real(), qmake_getpwd(), Option::output_dir));
2840 return QMakeLocalFileName(fileFixify(file.real()));
2841}
2842
2843QFileInfo
2844MakefileGenerator::findFileInfo(const QMakeLocalFileName &file)
2845{
2846 return fileInfo(file.local());
2847}
2848
2849QMakeLocalFileName
2850MakefileGenerator::findFileForDep(const QMakeLocalFileName &dep, const QMakeLocalFileName &file)
2851{
2852 QMakeLocalFileName ret;
2853 if(!project->isEmpty("SKIP_DEPENDS")) {
2854 bool found = false;
2855 QStringList &nodeplist = project->values("SKIP_DEPENDS");
2856 for(QStringList::Iterator it = nodeplist.begin();
2857 it != nodeplist.end(); ++it) {
2858 QRegExp regx((*it));
2859 if(regx.indexIn(dep.local()) != -1) {
2860 found = true;
2861 break;
2862 }
2863 }
2864 if(found)
2865 return ret;
2866 }
2867
2868 ret = QMakeSourceFileInfo::findFileForDep(dep, file);
2869 if(!ret.isNull())
2870 return ret;
2871
2872 //these are some "hacky" heuristics it will try to do on an include
2873 //however these can be turned off at runtime, I'm not sure how
2874 //reliable these will be, most likely when problems arise turn it off
2875 //and see if they go away..
2876 if(Option::mkfile::do_dep_heuristics) {
2877 if(depHeuristicsCache.contains(dep.real()))
2878 return depHeuristicsCache[dep.real()];
2879
2880 if(Option::output_dir != qmake_getpwd()
2881 && QDir::isRelativePath(dep.real())) { //is it from the shadow tree
2882 QList<QMakeLocalFileName> depdirs = QMakeSourceFileInfo::dependencyPaths();
2883 depdirs.prepend(fileInfo(file.real()).absoluteDir().path());
2884 QString pwd = qmake_getpwd();
2885 if(pwd.at(pwd.length()-1) != '/')
2886 pwd += '/';
2887 for(int i = 0; i < depdirs.count(); i++) {
2888 QString dir = depdirs.at(i).real();
2889 if(!QDir::isRelativePath(dir) && dir.startsWith(pwd))
2890 dir = dir.mid(pwd.length());
2891 if(QDir::isRelativePath(dir)) {
2892 if(!dir.endsWith(Option::dir_sep))
2893 dir += Option::dir_sep;
2894 QString shadow = fileFixify(dir + dep.local(), pwd, Option::output_dir);
2895 if(exists(shadow)) {
2896 ret = QMakeLocalFileName(shadow);
2897 goto found_dep_from_heuristic;
2898 }
2899 }
2900 }
2901 }
2902 { //is it from an EXTRA_TARGET
2903 const QString dep_basename = dep.local().section(Option::dir_sep, -1);
2904 QStringList &qut = project->values("QMAKE_EXTRA_TARGETS");
2905 for(QStringList::Iterator it = qut.begin(); it != qut.end(); ++it) {
2906 QString targ = var((*it) + ".target");
2907 if(targ.isEmpty())
2908 targ = (*it);
2909 QString out = Option::fixPathToTargetOS(targ);
2910 if(out == dep.real() || out.section(Option::dir_sep, -1) == dep_basename) {
2911 ret = QMakeLocalFileName(out);
2912 goto found_dep_from_heuristic;
2913 }
2914 }
2915 }
2916 { //is it from an EXTRA_COMPILER
2917 const QString dep_basename = dep.local().section(Option::dir_sep, -1);
2918 const QStringList &quc = project->values("QMAKE_EXTRA_COMPILERS");
2919 for(QStringList::ConstIterator it = quc.begin(); it != quc.end(); ++it) {
2920 QString tmp_out = project->values((*it) + ".output").first();
2921 if(tmp_out.isEmpty())
2922 continue;
2923 QStringList &tmp = project->values((*it) + ".input");
2924 for(QStringList::Iterator it2 = tmp.begin(); it2 != tmp.end(); ++it2) {
2925 QStringList &inputs = project->values((*it2));
2926 for(QStringList::Iterator input = inputs.begin(); input != inputs.end(); ++input) {
2927 QString out = Option::fixPathToTargetOS(unescapeFilePath(replaceExtraCompilerVariables(tmp_out, (*input), QString())));
2928 if(out == dep.real() || out.section(Option::dir_sep, -1) == dep_basename) {
2929 ret = QMakeLocalFileName(fileFixify(out, qmake_getpwd(), Option::output_dir));
2930 goto found_dep_from_heuristic;
2931 }
2932 }
2933 }
2934 }
2935 }
2936 found_dep_from_heuristic:
2937 depHeuristicsCache.insert(dep.real(), ret);
2938 }
2939 return ret;
2940}
2941
2942QStringList
2943&MakefileGenerator::findDependencies(const QString &file)
2944{
2945 const QString fixedFile = fileFixify(file);
2946 if(!dependsCache.contains(fixedFile)) {
2947#if 1
2948 QStringList deps = QMakeSourceFileInfo::dependencies(file);
2949 if(file != fixedFile)
2950 deps += QMakeSourceFileInfo::dependencies(fixedFile);
2951#else
2952 QStringList deps = QMakeSourceFileInfo::dependencies(fixedFile);
2953#endif
2954 dependsCache.insert(fixedFile, deps);
2955 }
2956 return dependsCache[fixedFile];
2957}
2958
2959QString
2960MakefileGenerator::specdir(const QString &outdir)
2961{
2962#if 0
2963 if(!spec.isEmpty())
2964 return spec;
2965#endif
2966 spec = fileFixify(Option::mkfile::qmakespec, outdir);
2967 return spec;
2968}
2969
2970bool
2971MakefileGenerator::openOutput(QFile &file, const QString &build) const
2972{
2973 {
2974 QString outdir;
2975 if(!file.fileName().isEmpty()) {
2976 if(QDir::isRelativePath(file.fileName()))
2977 file.setFileName(Option::output_dir + "/" + file.fileName()); //pwd when qmake was run
2978 QFileInfo fi(fileInfo(file.fileName()));
2979 if(fi.isDir())
2980 outdir = file.fileName() + QDir::separator();
2981 }
2982 if(!outdir.isEmpty() || file.fileName().isEmpty()) {
2983 QString fname = "Makefile";
2984 if(!project->isEmpty("MAKEFILE"))
2985 fname = project->first("MAKEFILE");
2986 file.setFileName(outdir + fname);
2987 }
2988 }
2989 if(QDir::isRelativePath(file.fileName())) {
2990 QString fname = Option::output_dir; //pwd when qmake was run
2991 if(!fname.endsWith("/"))
2992 fname += "/";
2993 fname += file.fileName();
2994 file.setFileName(fname);
2995 }
2996 if(!build.isEmpty())
2997 file.setFileName(file.fileName() + "." + build);
2998 if(project->isEmpty("QMAKE_MAKEFILE"))
2999 project->values("QMAKE_MAKEFILE").append(file.fileName());
3000 int slsh = file.fileName().lastIndexOf(Option::dir_sep);
3001 if(slsh != -1)
3002 mkdir(file.fileName().left(slsh));
3003 if(file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate)) {
3004 QFileInfo fi(fileInfo(Option::output.fileName()));
3005 QString od;
3006 if(fi.isSymLink())
3007 od = fileInfo(fi.readLink()).absolutePath();
3008 else
3009 od = fi.path();
3010 od = Option::fixPathToTargetOS(od);
3011 if(QDir::isRelativePath(od))
3012 od.prepend(Option::output_dir);
3013 Option::output_dir = od;
3014 return true;
3015 }
3016 return false;
3017}
3018
3019QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.