repurpose deprecated -E switch
[profile/ivi/qtbase.git] / qmake / property.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the qmake application of the Qt Toolkit.
7 **
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.
16 **
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.
20 **
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.
28 **
29 ** Other Usage
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.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "property.h"
43 #include "option.h"
44
45 #include <qdir.h>
46 #include <qsettings.h>
47 #include <qstringlist.h>
48 #include <stdio.h>
49
50 QT_BEGIN_NAMESPACE
51
52 QStringList qmake_mkspec_paths(); //project.cpp
53
54 static const struct {
55     const char *name;
56     QLibraryInfo::LibraryLocation loc;
57     bool raw;
58 } propList[] = {
59     { "QT_SYSROOT", QLibraryInfo::SysrootPath, true },
60     { "QT_INSTALL_PREFIX", QLibraryInfo::PrefixPath, false },
61     { "QT_INSTALL_DATA", QLibraryInfo::DataPath, false },
62     { "QT_INSTALL_DOCS", QLibraryInfo::DocumentationPath, false },
63     { "QT_INSTALL_HEADERS", QLibraryInfo::HeadersPath, false },
64     { "QT_INSTALL_LIBS", QLibraryInfo::LibrariesPath, false },
65     { "QT_INSTALL_BINS", QLibraryInfo::BinariesPath, false },
66     { "QT_INSTALL_TESTS", QLibraryInfo::TestsPath, false },
67     { "QT_INSTALL_PLUGINS", QLibraryInfo::PluginsPath, false },
68     { "QT_INSTALL_IMPORTS", QLibraryInfo::ImportsPath, false },
69     { "QT_INSTALL_TRANSLATIONS", QLibraryInfo::TranslationsPath, false },
70     { "QT_INSTALL_CONFIGURATION", QLibraryInfo::SettingsPath, false },
71     { "QT_INSTALL_EXAMPLES", QLibraryInfo::ExamplesPath, false },
72     { "QT_INSTALL_DEMOS", QLibraryInfo::ExamplesPath, false }, // Just backwards compat
73     { "QT_HOST_PREFIX", QLibraryInfo::HostPrefixPath, true },
74     { "QT_HOST_DATA", QLibraryInfo::HostDataPath, true },
75     { "QT_HOST_BINS", QLibraryInfo::HostBinariesPath, true },
76 };
77
78 QMakeProperty::QMakeProperty() : settings(0)
79 {
80     for (int i = 0; i < sizeof(propList)/sizeof(propList[0]); i++) {
81         QString name = QString::fromLatin1(propList[i].name);
82         m_values[name + "/get"] = QLibraryInfo::rawLocation(propList[i].loc, QLibraryInfo::EffectivePaths);
83         QString val = QLibraryInfo::rawLocation(propList[i].loc, QLibraryInfo::FinalPaths);
84         if (!propList[i].raw) {
85             m_values[name] = QLibraryInfo::location(propList[i].loc);
86             name += "/raw";
87         }
88         m_values[name] = val;
89     }
90 }
91
92 QMakeProperty::~QMakeProperty()
93 {
94     delete settings;
95     settings = 0;
96 }
97
98 void QMakeProperty::initSettings()
99 {
100     if(!settings) {
101         settings = new QSettings(QSettings::UserScope, "Trolltech", "QMake");
102         settings->setFallbacksEnabled(false);
103     }
104 }
105
106 QString
107 QMakeProperty::keyBase(bool version) const
108 {
109     if (version)
110         return QString(qmake_version()) + "/";
111     return QString();
112 }
113
114 QString
115 QMakeProperty::value(QString v, bool just_check)
116 {
117     QString val = m_values.value(v);
118     if (!val.isNull())
119         return val;
120     else if(v == "QMAKE_MKSPECS")
121         return qmake_mkspec_paths().join(Option::dirlist_sep);
122     else if(v == "QMAKE_VERSION")
123         return qmake_version();
124 #ifdef QT_VERSION_STR
125     else if(v == "QT_VERSION")
126         return QT_VERSION_STR;
127 #endif
128
129     initSettings();
130     int slash = v.lastIndexOf('/');
131     QVariant var = settings->value(keyBase(slash == -1) + v);
132     bool ok = var.isValid();
133     QString ret = var.toString();
134     if(!ok) {
135         QString version = qmake_version();
136         if(slash != -1) {
137             version = v.left(slash-1);
138             v = v.mid(slash+1);
139         }
140         settings->beginGroup(keyBase(false));
141         QStringList subs = settings->childGroups();
142         settings->endGroup();
143         subs.sort();
144         for (int x = subs.count() - 1; x >= 0; x--) {
145             QString s = subs[x];
146             if(s.isEmpty() || s > version)
147                 continue;
148             var = settings->value(keyBase(false) + s + "/" + v);
149             ok = var.isValid();
150             ret = var.toString();
151             if (ok) {
152                 if(!just_check)
153                     debug_msg(1, "Fell back from %s -> %s for '%s'.", version.toLatin1().constData(),
154                               s.toLatin1().constData(), v.toLatin1().constData());
155                 return ret;
156             }
157         }
158     }
159     return ok ? ret : QString();
160 }
161
162 bool
163 QMakeProperty::hasValue(QString v)
164 {
165     return !value(v, true).isNull();
166 }
167
168 void
169 QMakeProperty::setValue(QString var, const QString &val)
170 {
171     initSettings();
172     settings->setValue(keyBase() + var, val);
173 }
174
175 void
176 QMakeProperty::remove(const QString &var)
177 {
178     initSettings();
179     settings->remove(keyBase() + var);
180 }
181
182 bool
183 QMakeProperty::exec()
184 {
185     bool ret = true;
186     if(Option::qmake_mode == Option::QMAKE_QUERY_PROPERTY) {
187         if(Option::prop::properties.isEmpty()) {
188             initSettings();
189             settings->beginGroup(keyBase(false));
190             QStringList subs = settings->childGroups();
191             settings->endGroup();
192             subs.sort();
193             for(int x = subs.count() - 1; x >= 0; x--) {
194                 QString s = subs[x];
195                 if(s.isEmpty())
196                     continue;
197                 settings->beginGroup(keyBase(false) + s);
198                 QStringList keys = settings->childKeys();
199                 settings->endGroup();
200                 for(QStringList::ConstIterator it2 = keys.begin(); it2 != keys.end(); it2++) {
201                     QString ret = settings->value(keyBase(false) + s + "/" + (*it2)).toString();
202                     if(s != qmake_version())
203                         fprintf(stdout, "%s/", s.toLatin1().constData());
204                     fprintf(stdout, "%s:%s\n", (*it2).toLatin1().constData(), ret.toLatin1().constData());
205                 }
206             }
207             QStringList specialProps;
208             for (int i = 0; i < sizeof(propList)/sizeof(propList[0]); i++)
209                 specialProps.append(QString::fromLatin1(propList[i].name));
210             specialProps.append("QMAKE_MKSPECS");
211             specialProps.append("QMAKE_VERSION");
212 #ifdef QT_VERSION_STR
213             specialProps.append("QT_VERSION");
214 #endif
215             foreach (QString prop, specialProps) {
216                 QString val = value(prop);
217                 QString pval = value(prop + "/raw");
218                 QString gval = value(prop + "/get");
219                 fprintf(stdout, "%s:%s\n", prop.toLatin1().constData(), val.toLatin1().constData());
220                 if (!pval.isEmpty() && pval != val)
221                     fprintf(stdout, "%s/raw:%s\n", prop.toLatin1().constData(), pval.toLatin1().constData());
222                 if (!gval.isEmpty() && gval != (pval.isEmpty() ? val : pval))
223                     fprintf(stdout, "%s/get:%s\n", prop.toLatin1().constData(), gval.toLatin1().constData());
224             }
225             return true;
226         }
227         for(QStringList::ConstIterator it = Option::prop::properties.begin();
228             it != Option::prop::properties.end(); it++) {
229             if(Option::prop::properties.count() > 1)
230                 fprintf(stdout, "%s:", (*it).toLatin1().constData());
231             if(!hasValue((*it))) {
232                 ret = false;
233                 fprintf(stdout, "**Unknown**\n");
234             } else {
235                 fprintf(stdout, "%s\n", value((*it)).toLatin1().constData());
236             }
237         }
238     } else if(Option::qmake_mode == Option::QMAKE_SET_PROPERTY) {
239         for(QStringList::ConstIterator it = Option::prop::properties.begin();
240             it != Option::prop::properties.end(); it++) {
241             QString var = (*it);
242             it++;
243             if(it == Option::prop::properties.end()) {
244                 ret = false;
245                 break;
246             }
247             if(!var.startsWith("."))
248                 setValue(var, (*it));
249         }
250     } else if(Option::qmake_mode == Option::QMAKE_UNSET_PROPERTY) {
251         for(QStringList::ConstIterator it = Option::prop::properties.begin();
252             it != Option::prop::properties.end(); it++) {
253             QString var = (*it);
254             if(!var.startsWith("."))
255                 remove(var);
256         }
257     }
258     return ret;
259 }
260
261 QT_END_NAMESPACE