Revert "move finding the makespec to Option"
[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         QString val = QLibraryInfo::rawLocation(propList[i].loc);
83         if (!propList[i].raw) {
84             m_values[name] = QLibraryInfo::location(propList[i].loc);
85             name += "/raw";
86         }
87         m_values[name] = val;
88     }
89 }
90
91 QMakeProperty::~QMakeProperty()
92 {
93     delete settings;
94     settings = 0;
95 }
96
97 void QMakeProperty::initSettings()
98 {
99     if(!settings) {
100         settings = new QSettings(QSettings::UserScope, "Trolltech", "QMake");
101         settings->setFallbacksEnabled(false);
102     }
103 }
104
105 QString
106 QMakeProperty::keyBase(bool version) const
107 {
108     if (version)
109         return QString(qmake_version()) + "/";
110     return QString();
111 }
112
113 QString
114 QMakeProperty::value(QString v, bool just_check)
115 {
116     QString val = m_values.value(v);
117     if (!val.isNull())
118         return val;
119     else if(v == "QMAKE_MKSPECS")
120         return qmake_mkspec_paths().join(Option::dirlist_sep);
121     else if(v == "QMAKE_VERSION")
122         return qmake_version();
123 #ifdef QT_VERSION_STR
124     else if(v == "QT_VERSION")
125         return QT_VERSION_STR;
126 #endif
127
128     initSettings();
129     int slash = v.lastIndexOf('/');
130     QVariant var = settings->value(keyBase(slash == -1) + v);
131     bool ok = var.isValid();
132     QString ret = var.toString();
133     if(!ok) {
134         QString version = qmake_version();
135         if(slash != -1) {
136             version = v.left(slash-1);
137             v = v.mid(slash+1);
138         }
139         settings->beginGroup(keyBase(false));
140         QStringList subs = settings->childGroups();
141         settings->endGroup();
142         subs.sort();
143         for (int x = subs.count() - 1; x >= 0; x--) {
144             QString s = subs[x];
145             if(s.isEmpty() || s > version)
146                 continue;
147             var = settings->value(keyBase(false) + s + "/" + v);
148             ok = var.isValid();
149             ret = var.toString();
150             if (ok) {
151                 if(!just_check)
152                     debug_msg(1, "Fell back from %s -> %s for '%s'.", version.toLatin1().constData(),
153                               s.toLatin1().constData(), v.toLatin1().constData());
154                 return ret;
155             }
156         }
157     }
158     return ok ? ret : QString();
159 }
160
161 bool
162 QMakeProperty::hasValue(QString v)
163 {
164     return !value(v, true).isNull();
165 }
166
167 void
168 QMakeProperty::setValue(QString var, const QString &val)
169 {
170     initSettings();
171     settings->setValue(keyBase() + var, val);
172 }
173
174 void
175 QMakeProperty::remove(const QString &var)
176 {
177     initSettings();
178     settings->remove(keyBase() + var);
179 }
180
181 bool
182 QMakeProperty::exec()
183 {
184     bool ret = true;
185     if(Option::qmake_mode == Option::QMAKE_QUERY_PROPERTY) {
186         if(Option::prop::properties.isEmpty()) {
187             initSettings();
188             settings->beginGroup(keyBase(false));
189             QStringList subs = settings->childGroups();
190             settings->endGroup();
191             subs.sort();
192             for(int x = subs.count() - 1; x >= 0; x--) {
193                 QString s = subs[x];
194                 if(s.isEmpty())
195                     continue;
196                 settings->beginGroup(keyBase(false) + s);
197                 QStringList keys = settings->childKeys();
198                 settings->endGroup();
199                 for(QStringList::ConstIterator it2 = keys.begin(); it2 != keys.end(); it2++) {
200                     QString ret = settings->value(keyBase(false) + s + "/" + (*it2)).toString();
201                     if(s != qmake_version())
202                         fprintf(stdout, "%s/", s.toLatin1().constData());
203                     fprintf(stdout, "%s:%s\n", (*it2).toLatin1().constData(), ret.toLatin1().constData());
204                 }
205             }
206             QStringList specialProps;
207             for (int i = 0; i < sizeof(propList)/sizeof(propList[0]); i++)
208                 specialProps.append(QString::fromLatin1(propList[i].name));
209             specialProps.append("QMAKE_MKSPECS");
210             specialProps.append("QMAKE_VERSION");
211 #ifdef QT_VERSION_STR
212             specialProps.append("QT_VERSION");
213 #endif
214             foreach (QString prop, specialProps) {
215                 QString val = value(prop);
216                 QString pval = value(prop + "/raw");
217                 fprintf(stdout, "%s:%s\n", prop.toLatin1().constData(), val.toLatin1().constData());
218                 if (!pval.isEmpty() && pval != val)
219                     fprintf(stdout, "%s/raw:%s\n", prop.toLatin1().constData(), pval.toLatin1().constData());
220             }
221             return true;
222         }
223         for(QStringList::ConstIterator it = Option::prop::properties.begin();
224             it != Option::prop::properties.end(); it++) {
225             if(Option::prop::properties.count() > 1)
226                 fprintf(stdout, "%s:", (*it).toLatin1().constData());
227             if(!hasValue((*it))) {
228                 ret = false;
229                 fprintf(stdout, "**Unknown**\n");
230             } else {
231                 fprintf(stdout, "%s\n", value((*it)).toLatin1().constData());
232             }
233         }
234     } else if(Option::qmake_mode == Option::QMAKE_SET_PROPERTY) {
235         for(QStringList::ConstIterator it = Option::prop::properties.begin();
236             it != Option::prop::properties.end(); it++) {
237             QString var = (*it);
238             it++;
239             if(it == Option::prop::properties.end()) {
240                 ret = false;
241                 break;
242             }
243             if(!var.startsWith("."))
244                 setValue(var, (*it));
245         }
246     } else if(Option::qmake_mode == Option::QMAKE_UNSET_PROPERTY) {
247         for(QStringList::ConstIterator it = Option::prop::properties.begin();
248             it != Option::prop::properties.end(); it++) {
249             QString var = (*it);
250             if(!var.startsWith("."))
251                 remove(var);
252         }
253     }
254     return ret;
255 }
256
257 QT_END_NAMESPACE