Merge "Merge remote-tracking branch 'origin/api_changes'" into refs/staging/master
[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 static const struct {
53     const char *name;
54     QLibraryInfo::LibraryLocation loc;
55     bool raw;
56 } propList[] = {
57     { "QT_SYSROOT", QLibraryInfo::SysrootPath, true },
58     { "QT_INSTALL_PREFIX", QLibraryInfo::PrefixPath, false },
59     { "QT_INSTALL_DATA", QLibraryInfo::DataPath, false },
60     { "QT_INSTALL_DOCS", QLibraryInfo::DocumentationPath, false },
61     { "QT_INSTALL_HEADERS", QLibraryInfo::HeadersPath, false },
62     { "QT_INSTALL_LIBS", QLibraryInfo::LibrariesPath, false },
63     { "QT_INSTALL_BINS", QLibraryInfo::BinariesPath, false },
64     { "QT_INSTALL_TESTS", QLibraryInfo::TestsPath, false },
65     { "QT_INSTALL_PLUGINS", QLibraryInfo::PluginsPath, false },
66     { "QT_INSTALL_IMPORTS", QLibraryInfo::ImportsPath, false },
67     { "QT_INSTALL_TRANSLATIONS", QLibraryInfo::TranslationsPath, false },
68     { "QT_INSTALL_CONFIGURATION", QLibraryInfo::SettingsPath, false },
69     { "QT_INSTALL_EXAMPLES", QLibraryInfo::ExamplesPath, false },
70     { "QT_INSTALL_DEMOS", QLibraryInfo::ExamplesPath, false }, // Just backwards compat
71     { "QT_RAW_INSTALL_PREFIX", QLibraryInfo::PrefixPath, true },
72     { "QT_RAW_INSTALL_DATA", QLibraryInfo::DataPath, true },
73     { "QT_RAW_INSTALL_DOCS", QLibraryInfo::DocumentationPath, true },
74     { "QT_RAW_INSTALL_HEADERS", QLibraryInfo::HeadersPath, true },
75     { "QT_RAW_INSTALL_LIBS", QLibraryInfo::LibrariesPath, true },
76     { "QT_RAW_INSTALL_BINS", QLibraryInfo::BinariesPath, true },
77     { "QT_RAW_INSTALL_TESTS", QLibraryInfo::TestsPath, true },
78     { "QT_RAW_INSTALL_PLUGINS", QLibraryInfo::PluginsPath, true },
79     { "QT_RAW_INSTALL_IMPORTS", QLibraryInfo::ImportsPath, true },
80     { "QT_RAW_INSTALL_TRANSLATIONS", QLibraryInfo::TranslationsPath, true },
81     { "QT_RAW_INSTALL_CONFIGURATION", QLibraryInfo::SettingsPath, true },
82     { "QT_RAW_INSTALL_EXAMPLES", QLibraryInfo::ExamplesPath, true },
83     { "QT_HOST_PREFIX", QLibraryInfo::HostPrefixPath, true },
84     { "QT_HOST_DATA", QLibraryInfo::HostDataPath, true },
85     { "QT_HOST_BINS", QLibraryInfo::HostBinariesPath, true },
86 };
87
88 QMakeProperty::QMakeProperty() : settings(0)
89 {
90     for (int i = 0; i < sizeof(propList)/sizeof(propList[0]); i++)
91         m_values[QString::fromLatin1(propList[i].name)] = propList[i].raw
92                 ? QLibraryInfo::rawLocation(propList[i].loc)
93                 : QLibraryInfo::location(propList[i].loc);
94 }
95
96 QMakeProperty::~QMakeProperty()
97 {
98     delete settings;
99     settings = 0;
100 }
101
102 void QMakeProperty::initSettings()
103 {
104     if(!settings) {
105         settings = new QSettings(QSettings::UserScope, "Trolltech", "QMake");
106         settings->setFallbacksEnabled(false);
107     }
108 }
109
110 QString
111 QMakeProperty::keyBase(bool version) const
112 {
113     if (version)
114         return QString(qmake_version()) + "/";
115     return QString();
116 }
117
118 QString
119 QMakeProperty::value(QString v, bool just_check)
120 {
121     QString val = m_values.value(v);
122     if (!val.isNull())
123         return val;
124     else if(v == "QMAKE_MKSPECS")
125         return Option::mkspecPaths().join(Option::dirlist_sep);
126     else if(v == "QMAKE_VERSION")
127         return qmake_version();
128 #ifdef QT_VERSION_STR
129     else if(v == "QT_VERSION")
130         return QT_VERSION_STR;
131 #endif
132
133     initSettings();
134     int slash = v.lastIndexOf('/');
135     QVariant var = settings->value(keyBase(slash == -1) + v);
136     bool ok = var.isValid();
137     QString ret = var.toString();
138     if(!ok) {
139         QString version = qmake_version();
140         if(slash != -1) {
141             version = v.left(slash-1);
142             v = v.mid(slash+1);
143         }
144         settings->beginGroup(keyBase(false));
145         QStringList subs = settings->childGroups();
146         settings->endGroup();
147         subs.sort();
148         for (int x = subs.count() - 1; x >= 0; x--) {
149             QString s = subs[x];
150             if(s.isEmpty() || s > version)
151                 continue;
152             var = settings->value(keyBase(false) + s + "/" + v);
153             ok = var.isValid();
154             ret = var.toString();
155             if (ok) {
156                 if(!just_check)
157                     debug_msg(1, "Fell back from %s -> %s for '%s'.", version.toLatin1().constData(),
158                               s.toLatin1().constData(), v.toLatin1().constData());
159                 return ret;
160             }
161         }
162     }
163     return ok ? ret : QString();
164 }
165
166 bool
167 QMakeProperty::hasValue(QString v)
168 {
169     return !value(v, true).isNull();
170 }
171
172 void
173 QMakeProperty::setValue(QString var, const QString &val)
174 {
175     initSettings();
176     settings->setValue(keyBase() + var, val);
177 }
178
179 void
180 QMakeProperty::remove(const QString &var)
181 {
182     initSettings();
183     settings->remove(keyBase() + var);
184 }
185
186 bool
187 QMakeProperty::exec()
188 {
189     bool ret = true;
190     if(Option::qmake_mode == Option::QMAKE_QUERY_PROPERTY) {
191         if(Option::prop::properties.isEmpty()) {
192             initSettings();
193             settings->beginGroup(keyBase(false));
194             QStringList subs = settings->childGroups();
195             settings->endGroup();
196             subs.sort();
197             for(int x = subs.count() - 1; x >= 0; x--) {
198                 QString s = subs[x];
199                 if(s.isEmpty())
200                     continue;
201                 settings->beginGroup(keyBase(false) + s);
202                 QStringList keys = settings->childKeys();
203                 settings->endGroup();
204                 for(QStringList::ConstIterator it2 = keys.begin(); it2 != keys.end(); it2++) {
205                     QString ret = settings->value(keyBase(false) + s + "/" + (*it2)).toString();
206                     if(s != qmake_version())
207                         fprintf(stdout, "%s/", s.toLatin1().constData());
208                     fprintf(stdout, "%s:%s\n", (*it2).toLatin1().constData(), ret.toLatin1().constData());
209                 }
210             }
211             QStringList specialProps;
212             for (int i = 0; i < sizeof(propList)/sizeof(propList[0]); i++)
213                 specialProps.append(QString::fromLatin1(propList[i].name));
214             specialProps.append("QMAKE_MKSPECS");
215             specialProps.append("QMAKE_VERSION");
216 #ifdef QT_VERSION_STR
217             specialProps.append("QT_VERSION");
218 #endif
219             foreach (QString prop, specialProps)
220                 fprintf(stdout, "%s:%s\n", prop.toLatin1().constData(), value(prop).toLatin1().constData());
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