1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the qmake application of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
43 #include "cachekeys.h"
48 #include <qsettings.h>
55 const char *Option::application_argv0 = 0;
56 QString Option::prf_ext;
57 QString Option::prl_ext;
58 QString Option::libtool_ext;
59 QString Option::pkgcfg_ext;
60 QString Option::ui_ext;
61 QStringList Option::h_ext;
62 QString Option::cpp_moc_ext;
63 QStringList Option::cpp_ext;
64 QStringList Option::c_ext;
65 QString Option::obj_ext;
66 QString Option::lex_ext;
67 QString Option::yacc_ext;
68 QString Option::pro_ext;
69 QString Option::dir_sep;
70 QString Option::dirlist_sep;
71 QString Option::h_moc_mod;
72 QString Option::yacc_mod;
73 QString Option::lex_mod;
74 QString Option::res_ext;
75 char Option::field_sep;
78 Option::QMAKE_MODE Option::qmake_mode = Option::QMAKE_GENERATE_NOTHING;
81 QString Option::qmake_abslocation;
82 QStringList Option::qmake_args;
83 int Option::warn_level = WarnLogic | WarnDeprecated;
84 int Option::debug_level = 0;
86 QString Option::output_dir;
87 bool Option::recursive = false;
88 QStringList Option::before_user_vars;
89 QStringList Option::after_user_vars;
90 QString Option::user_template;
91 QString Option::user_template_prefix;
93 //QMAKE_*_PROPERTY stuff
94 QStringList Option::prop::properties;
96 //QMAKE_GENERATE_PROJECT stuff
97 bool Option::projfile::do_pwd = true;
98 QStringList Option::projfile::project_dirs;
100 //QMAKE_GENERATE_MAKEFILE stuff
101 QString Option::mkfile::qmakespec;
102 QString Option::mkfile::xqmakespec;
103 int Option::mkfile::cachefile_depth = -1;
104 bool Option::mkfile::do_deps = true;
105 bool Option::mkfile::do_mocs = true;
106 bool Option::mkfile::do_dep_heuristics = true;
107 bool Option::mkfile::do_preprocess = false;
108 bool Option::mkfile::do_stub_makefile = false;
109 bool Option::mkfile::do_cache = true;
110 QString Option::mkfile::source_root;
111 QString Option::mkfile::build_root;
112 QString Option::mkfile::cachefile;
113 QStringList Option::mkfile::project_files;
115 static Option::QMAKE_MODE default_mode(QString progname)
117 int s = progname.lastIndexOf(QDir::separator());
119 progname = progname.right(progname.length() - (s + 1));
120 if(progname == "qmakegen")
121 return Option::QMAKE_GENERATE_PROJECT;
122 else if(progname == "qt-config")
123 return Option::QMAKE_QUERY_PROPERTY;
124 return Option::QMAKE_GENERATE_MAKEFILE;
127 static QString detectProjectFile(const QString &path)
131 if(dir.exists(dir.dirName() + Option::pro_ext)) {
132 ret = dir.filePath(dir.dirName()) + Option::pro_ext;
133 } else { //last try..
134 QStringList profiles = dir.entryList(QStringList("*" + Option::pro_ext));
135 if(profiles.count() == 1)
136 ret = dir.filePath(profiles.at(0));
141 static QString cleanSpec(const QString &spec)
143 QString ret = QDir::cleanPath(spec);
144 if (ret.contains('/')) {
145 const QFileInfo specDirInfo(ret);
146 if (specDirInfo.exists() && specDirInfo.isDir())
147 ret = QDir::cleanPath(specDirInfo.absoluteFilePath());
152 QString project_builtin_regx();
153 bool usage(const char *a0)
155 fprintf(stdout, "Usage: %s [mode] [options] [files]\n"
157 "QMake has two modes, one mode for generating project files based on\n"
158 "some heuristics, and the other for generating makefiles. Normally you\n"
159 "shouldn't need to specify a mode, as makefile generation is the default\n"
160 "mode for qmake, but you may use this to test qmake on an existing project\n"
163 " -project Put qmake into project file generation mode%s\n"
164 " In this mode qmake interprets files as files to\n"
167 " Note: The created .pro file probably will \n"
168 " need to be edited. For example add the QT variable to \n"
169 " specify what modules are required.\n"
170 " -makefile Put qmake into makefile generation mode%s\n"
171 " In this mode qmake interprets files as project files to\n"
172 " be processed, if skipped qmake will try to find a project\n"
173 " file in your current working directory\n"
175 "Warnings Options:\n"
176 " -Wnone Turn off all warnings; specific ones may be re-enabled by\n"
177 " later -W options\n"
178 " -Wall Turn on all warnings\n"
179 " -Wparser Turn on parser warnings\n"
180 " -Wlogic Turn on logic warnings (on by default)\n"
181 " -Wdeprecated Turn on deprecation warnings (on by default)\n"
184 " * You can place any variable assignment in options and it will be *\n"
185 " * processed as if it was in [files]. These assignments will be parsed *\n"
186 " * before [files]. *\n"
187 " -o file Write output to file\n"
188 " -d Increase debug level\n"
189 " -t templ Overrides TEMPLATE as templ\n"
190 " -tp prefix Overrides TEMPLATE so that prefix is prefixed into the value\n"
192 " -v Version information\n"
193 " -after All variable assignments after this will be\n"
194 " parsed after [files]\n"
195 " -norecursive Don't do a recursive search\n"
196 " -recursive Do a recursive search\n"
197 " -set <prop> <value> Set persistent property\n"
198 " -unset <prop> Unset persistent property\n"
199 " -query <prop> Query persistent property. Show all if <prop> is empty.\n"
200 " -cache file Use file as cache [makefile mode only]\n"
201 " -spec spec Use spec as QMAKESPEC [makefile mode only]\n"
202 " -nocache Don't use a cache file [makefile mode only]\n"
203 " -nodepend Don't generate dependencies [makefile mode only]\n"
204 " -nomoc Don't generate moc targets [makefile mode only]\n"
205 " -nopwd Don't look for files in pwd [project mode only]\n"
207 default_mode(a0) == Option::QMAKE_GENERATE_PROJECT ? " (default)" : "", project_builtin_regx().toLatin1().constData(),
208 default_mode(a0) == Option::QMAKE_GENERATE_MAKEFILE ? " (default)" : ""
214 Option::parseCommandLine(QStringList &args)
216 QStringList user_configs;
219 args << QString(); // Avoid bounds checking for options which take an argument
220 for (int x = 0; x < args.size() - 1; ) {
221 QString arg = args.at(x);
222 if (arg.size() > 1 && arg.startsWith('-')) { /* options */
223 QString opt = arg.mid(1);
224 if(opt == "o" || opt == "output") {
225 Option::output.setFileName(args.at(x + 1));
226 args.erase(args.begin() + x, args.begin() + x + 2);
228 } else if(opt == "after") {
230 } else if(opt == "t" || opt == "template") {
231 Option::user_template = args.at(++x);
232 } else if(opt == "tp" || opt == "template_prefix") {
233 Option::user_template_prefix = args.at(++x);
234 } else if(opt == "unix") {
235 Option::dir_sep = "/";
236 } else if(opt == "win32") {
237 Option::dir_sep = "\\";
238 } else if(opt == "d") {
239 Option::debug_level++;
240 } else if(opt == "version" || opt == "v" || opt == "-version") {
243 "Using Qt version %s in %s\n",
244 qmake_version(), QT_VERSION_STR,
245 QLibraryInfo::location(QLibraryInfo::LibrariesPath).toLatin1().constData());
246 #ifdef QMAKE_OPENSOURCE_VERSION
247 fprintf(stdout, "QMake is Open Source software from Nokia Corporation and/or its subsidiary(-ies).\n");
249 return Option::QMAKE_CMDLINE_BAIL;
250 } else if(opt == "h" || opt == "help") {
251 return Option::QMAKE_CMDLINE_SHOW_USAGE;
252 } else if(opt == "Wall") {
253 Option::warn_level |= WarnAll;
254 } else if(opt == "Wparser") {
255 Option::warn_level |= WarnParser;
256 } else if(opt == "Wlogic") {
257 Option::warn_level |= WarnLogic;
258 } else if(opt == "Wdeprecated") {
259 Option::warn_level |= WarnDeprecated;
260 } else if(opt == "Wnone") {
261 Option::warn_level = WarnNone;
262 } else if(opt == "r" || opt == "recursive") {
263 Option::recursive = true;
266 } else if(opt == "nr" || opt == "norecursive") {
267 Option::recursive = false;
270 } else if(opt == "config") {
271 user_configs += args.at(++x);
273 if(Option::qmake_mode == Option::QMAKE_GENERATE_MAKEFILE ||
274 Option::qmake_mode == Option::QMAKE_GENERATE_PRL) {
275 if(opt == "nodepend" || opt == "nodepends") {
276 Option::mkfile::do_deps = false;
277 } else if(opt == "nomoc") {
278 Option::mkfile::do_mocs = false;
279 } else if(opt == "nocache") {
280 Option::mkfile::do_cache = false;
281 } else if(opt == "createstub") {
282 Option::mkfile::do_stub_makefile = true;
283 } else if(opt == "nodependheuristics") {
284 Option::mkfile::do_dep_heuristics = false;
285 } else if(opt == "E") {
286 Option::mkfile::do_preprocess = true;
287 } else if(opt == "cache") {
288 Option::mkfile::cachefile = args.at(++x);
289 } else if(opt == "platform" || opt == "spec") {
290 Option::mkfile::qmakespec = args[x] = cleanSpec(args.at(++x));
291 } else if (opt == "xplatform" || opt == "xspec") {
292 Option::mkfile::xqmakespec = args[x] = cleanSpec(args.at(x));
294 fprintf(stderr, "***Unknown option -%s\n", opt.toLatin1().constData());
295 return Option::QMAKE_CMDLINE_SHOW_USAGE | Option::QMAKE_CMDLINE_ERROR;
297 } else if(Option::qmake_mode == Option::QMAKE_GENERATE_PROJECT) {
299 Option::projfile::do_pwd = false;
301 fprintf(stderr, "***Unknown option -%s\n", opt.toLatin1().constData());
302 return Option::QMAKE_CMDLINE_SHOW_USAGE | Option::QMAKE_CMDLINE_ERROR;
307 if(arg.indexOf('=') != -1) {
309 Option::before_user_vars.append(arg);
311 Option::after_user_vars.append(arg);
314 if(Option::qmake_mode == Option::QMAKE_QUERY_PROPERTY ||
315 Option::qmake_mode == Option::QMAKE_SET_PROPERTY ||
316 Option::qmake_mode == Option::QMAKE_UNSET_PROPERTY) {
317 Option::prop::properties.append(arg);
320 if(!fi.makeAbsolute()) //strange
322 if(Option::qmake_mode == Option::QMAKE_GENERATE_MAKEFILE ||
323 Option::qmake_mode == Option::QMAKE_GENERATE_PRL) {
325 QString proj = detectProjectFile(arg);
329 Option::mkfile::project_files.append(arg);
330 } else if(Option::qmake_mode == Option::QMAKE_GENERATE_PROJECT) {
331 Option::projfile::project_dirs.append(arg);
337 return Option::QMAKE_CMDLINE_SHOW_USAGE | Option::QMAKE_CMDLINE_ERROR;
346 if (!user_configs.isEmpty())
347 Option::before_user_vars += "CONFIG += " + user_configs.join(" ");
349 if (Option::mkfile::xqmakespec.isEmpty())
350 Option::mkfile::xqmakespec = Option::mkfile::qmakespec;
353 return Option::QMAKE_CMDLINE_SUCCESS;
357 Option::init(int argc, char **argv)
359 Option::application_argv0 = 0;
360 Option::prf_ext = ".prf";
361 Option::pro_ext = ".pro";
363 Option::dir_sep = "\\";
364 Option::dirlist_sep = ";";
366 Option::dir_sep = "/";
367 Option::dirlist_sep = ":";
369 Option::field_sep = ' ';
372 Option::application_argv0 = argv[0];
373 QString argv0 = argv[0];
374 if(Option::qmake_mode == Option::QMAKE_GENERATE_NOTHING)
375 Option::qmake_mode = default_mode(argv0);
376 if(!argv0.isEmpty() && !QFileInfo(argv0).isRelative()) {
377 Option::qmake_abslocation = argv0;
378 } else if (argv0.contains(QLatin1Char('/'))
380 || argv0.contains(QLatin1Char('\\'))
383 Option::qmake_abslocation = QDir::current().absoluteFilePath(argv0);
384 } else { //in the PATH
385 QByteArray pEnv = qgetenv("PATH");
386 QDir currentDir = QDir::current();
388 QStringList paths = QString::fromLocal8Bit(pEnv).split(QLatin1String(";"));
390 QStringList paths = QString::fromLocal8Bit(pEnv).split(QLatin1String(":"));
392 for (QStringList::const_iterator p = paths.constBegin(); p != paths.constEnd(); ++p) {
395 QString candidate = currentDir.absoluteFilePath(*p + QLatin1Char('/') + argv0);
399 if (QFile::exists(candidate)) {
400 Option::qmake_abslocation = candidate;
405 if(!Option::qmake_abslocation.isNull())
406 Option::qmake_abslocation = QDir::cleanPath(Option::qmake_abslocation);
407 else // This is rather unlikely to ever happen on a modern system ...
408 Option::qmake_abslocation = QLibraryInfo::rawLocation(QLibraryInfo::HostBinariesPath,
409 QLibraryInfo::EffectivePaths) +
416 Option::qmake_mode = Option::QMAKE_GENERATE_MAKEFILE;
419 const QByteArray envflags = qgetenv("QMAKEFLAGS");
420 if (!envflags.isNull()) {
424 bool hasWord = false;
425 for (int i = 0; i < envflags.size(); ++i) {
426 char c = envflags.at(i);
427 if (!quote && (c == '\'' || c == '"')) {
429 } else if (c == quote) {
431 } else if (!quote && c == ' ') {
433 args << QString::fromLocal8Bit(buf);
443 args << QString::fromLocal8Bit(buf);
444 parseCommandLine(args);
448 for (int i = 1; i < argc; i++)
449 args << QString::fromLocal8Bit(argv[i]);
451 while (!args.isEmpty()) {
452 QString opt = args.at(0);
453 if (opt == "-project") {
454 Option::recursive = true;
455 Option::qmake_mode = Option::QMAKE_GENERATE_PROJECT;
456 } else if (opt == "-prl") {
457 Option::mkfile::do_deps = false;
458 Option::mkfile::do_mocs = false;
459 Option::qmake_mode = Option::QMAKE_GENERATE_PRL;
460 } else if (opt == "-set") {
461 Option::qmake_mode = Option::QMAKE_SET_PROPERTY;
462 } else if (opt == "-unset") {
463 Option::qmake_mode = Option::QMAKE_UNSET_PROPERTY;
464 } else if (opt == "-query") {
465 Option::qmake_mode = Option::QMAKE_QUERY_PROPERTY;
466 } else if (opt == "-makefile") {
467 Option::qmake_mode = Option::QMAKE_GENERATE_MAKEFILE;
475 int ret = parseCommandLine(args);
476 if(ret != Option::QMAKE_CMDLINE_SUCCESS) {
477 if ((ret & Option::QMAKE_CMDLINE_SHOW_USAGE) != 0)
480 //return ret == QMAKE_CMDLINE_SHOW_USAGE ? usage(argv[0]) : false;
482 Option::qmake_args = args;
485 //last chance for defaults
486 if(Option::qmake_mode == Option::QMAKE_GENERATE_MAKEFILE ||
487 Option::qmake_mode == Option::QMAKE_GENERATE_PRL) {
488 if (Option::mkfile::xqmakespec.isEmpty())
489 Option::mkfile::xqmakespec = QString::fromLocal8Bit(qgetenv("XQMAKESPEC").constData());
490 if (Option::mkfile::qmakespec.isEmpty()) {
491 Option::mkfile::qmakespec = QString::fromLocal8Bit(qgetenv("QMAKESPEC").constData());
492 if (Option::mkfile::xqmakespec.isEmpty())
493 Option::mkfile::xqmakespec = Option::mkfile::qmakespec;
496 //try REALLY hard to do it for them, lazy..
497 if(Option::mkfile::project_files.isEmpty()) {
498 QString proj = detectProjectFile(qmake_getpwd());
500 Option::mkfile::project_files.append(proj);
501 #ifndef QT_BUILD_QMAKE_LIBRARY
502 if(Option::mkfile::project_files.isEmpty()) {
504 return Option::QMAKE_CMDLINE_ERROR;
510 return QMAKE_CMDLINE_SUCCESS;
513 void Option::prepareProject(const QString &pfile)
515 QString srcpath = (pfile != "-")
516 ? QDir::cleanPath(QFileInfo(pfile).absolutePath()) : qmake_getpwd();
517 if (srcpath != output_dir) {
518 if (!srcpath.endsWith(QLatin1Char('/')))
519 srcpath += QLatin1Char('/');
520 QString dstpath = output_dir;
521 if (!dstpath.endsWith(QLatin1Char('/')))
522 dstpath += QLatin1Char('/');
523 int srcLen = srcpath.length();
524 int dstLen = dstpath.length();
526 while (++lastSl, srcpath.at(--srcLen) == dstpath.at(--dstLen))
527 if (srcpath.at(srcLen) == QLatin1Char('/'))
529 mkfile::source_root = srcpath.left(srcLen + lastSl);
530 mkfile::build_root = dstpath.left(dstLen + lastSl);
532 mkfile::source_root.clear();
536 bool Option::postProcessProject(QMakeProject *project)
538 Option::cpp_ext = project->values("QMAKE_EXT_CPP");
539 Option::h_ext = project->values("QMAKE_EXT_H");
540 Option::c_ext = project->values("QMAKE_EXT_C");
541 Option::res_ext = project->first("QMAKE_EXT_RES");
542 Option::pkgcfg_ext = project->first("QMAKE_EXT_PKGCONFIG");
543 Option::libtool_ext = project->first("QMAKE_EXT_LIBTOOL");
544 Option::prl_ext = project->first("QMAKE_EXT_PRL");
545 Option::ui_ext = project->first("QMAKE_EXT_UI");
546 Option::cpp_moc_ext = project->first("QMAKE_EXT_CPP_MOC");
547 Option::lex_ext = project->first("QMAKE_EXT_LEX");
548 Option::yacc_ext = project->first("QMAKE_EXT_YACC");
549 Option::obj_ext = project->first("QMAKE_EXT_OBJ");
550 Option::h_moc_mod = project->first("QMAKE_H_MOD_MOC");
551 Option::lex_mod = project->first("QMAKE_MOD_LEX");
552 Option::yacc_mod = project->first("QMAKE_MOD_YACC");
554 if (Option::output_dir.startsWith(project->buildRoot()))
555 Option::mkfile::cachefile_depth =
556 Option::output_dir.mid(project->buildRoot().length()).count('/');
562 Option::fixString(QString string, uchar flags)
564 //const QString orig_string = string;
565 static QHash<FixStringCacheKey, QString> *cache = 0;
567 cache = new QHash<FixStringCacheKey, QString>;
568 qmakeAddCacheClear(qmakeDeleteCacheClear<QHash<FixStringCacheKey, QString> >, (void**)&cache);
570 FixStringCacheKey cacheKey(string, flags);
572 QHash<FixStringCacheKey, QString>::const_iterator it = cache->constFind(cacheKey);
574 if (it != cache->constEnd()) {
575 //qDebug() << "Fix (cached) " << orig_string << "->" << it.value();
579 //fix the environment variables
580 if(flags & Option::FixEnvVars) {
582 static QRegExp reg_var("\\$\\(.*\\)");
583 reg_var.setMinimal(true);
584 while((rep = reg_var.indexIn(string)) != -1)
585 string.replace(rep, reg_var.matchedLength(),
586 QString::fromLocal8Bit(qgetenv(string.mid(rep + 2, reg_var.matchedLength() - 3).toLatin1().constData()).constData()));
589 //canonicalize it (and treat as a path)
590 if(flags & Option::FixPathCanonicalize) {
592 string = QFileInfo(string).canonicalFilePath();
594 string = QDir::cleanPath(string);
597 bool localSep = (flags & Option::FixPathToLocalSeparators) != 0;
598 bool targetSep = (flags & Option::FixPathToTargetSeparators) != 0;
599 bool normalSep = (flags & Option::FixPathToNormalSeparators) != 0;
601 // either none or only one active flag
602 Q_ASSERT(localSep + targetSep + normalSep <= 1);
604 if (flags & Option::FixPathToNormalSeparators) {
605 string = string.replace('\\', '/');
606 } else if (flags & Option::FixPathToLocalSeparators) {
607 #if defined(Q_OS_WIN32)
608 string = string.replace('/', '\\');
610 string = string.replace('\\', '/');
612 } else if(flags & Option::FixPathToTargetSeparators) {
613 string = string.replace('/', Option::dir_sep).replace('\\', Option::dir_sep);
616 if ((string.startsWith("\"") && string.endsWith("\"")) ||
617 (string.startsWith("\'") && string.endsWith("\'")))
618 string = string.mid(1, string.length()-2);
621 //qDebug() << "Fix" << orig_string << "->" << string;
622 cache->insert(cacheKey, string);
626 const char *qmake_version()
628 static char *ret = NULL;
631 ret = (char *)malloc(15);
632 qmakeAddCacheClear(qmakeFreeCacheClear, (void**)&ret);
633 #if defined(_MSC_VER) && _MSC_VER >= 1400
634 sprintf_s(ret, 15, "%d.%02d%c", QMAKE_VERSION_MAJOR, QMAKE_VERSION_MINOR, 'a' + QMAKE_VERSION_PATCH);
636 sprintf(ret, "%d.%02d%c", QMAKE_VERSION_MAJOR, QMAKE_VERSION_MINOR, 'a' + QMAKE_VERSION_PATCH);
641 void debug_msg_internal(int level, const char *fmt, ...)
643 if(Option::debug_level < level)
645 fprintf(stderr, "DEBUG %d: ", level);
649 vfprintf(stderr, fmt, ap);
652 fprintf(stderr, "\n");
655 void warn_msg(QMakeWarn type, const char *fmt, ...)
657 if(!(Option::warn_level & type))
659 fprintf(stderr, "WARNING: ");
663 vfprintf(stderr, fmt, ap);
666 fprintf(stderr, "\n");
669 class QMakeCacheClearItem {
671 qmakeCacheClearFunc func;
674 QMakeCacheClearItem(qmakeCacheClearFunc f, void **d) : func(f), data(d) { }
675 ~QMakeCacheClearItem() {
680 static QList<QMakeCacheClearItem*> cache_items;
685 qDeleteAll(cache_items);
690 qmakeAddCacheClear(qmakeCacheClearFunc func, void **data)
692 cache_items.append(new QMakeCacheClearItem(func, data));
695 QString qmake_libraryInfoFile()
697 if(!Option::qmake_abslocation.isEmpty())
698 return QDir(QFileInfo(Option::qmake_abslocation).absolutePath()).filePath("qt.conf");