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

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

qmake: Don't assume that relative paths in all OSes look like in *nix [vendor bug].

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