make QMakeMetaInfo a little less inefficient with libtool .la files
[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_HOST_PREFIX", QLibraryInfo::HostPrefixPath, true },
72     { "QT_HOST_DATA", QLibraryInfo::HostDataPath, true },
73     { "QT_HOST_BINS", QLibraryInfo::HostBinariesPath, true },
74 };
75
76 QMakeProperty::QMakeProperty() : settings(0)
77 {
78     for (int i = 0; i < sizeof(propList)/sizeof(propList[0]); i++) {
79         QString name = QString::fromLatin1(propList[i].name);
80         m_values[name + "/get"] = QLibraryInfo::rawLocation(propList[i].loc, QLibraryInfo::EffectivePaths);
81         QString val = QLibraryInfo::rawLocation(propList[i].loc, QLibraryInfo::FinalPaths);
82         if (!propList[i].raw) {
83             m_values[name] = QLibraryInfo::location(propList[i].loc);
84             name += "/raw";
85         }
86         m_values[name] = val;
87     }
88     m_values["QMAKE_VERSION"] = qmake_version();
89 #ifdef QT_VERSION_STR
90     m_values["QT_VERSION"] = QT_VERSION_STR;
91 #endif
92 }
93
94 QMakeProperty::~QMakeProperty()
95 {
96     delete settings;
97     settings = 0;
98 }
99
100 void QMakeProperty::initSettings()
101 {
102     if(!settings) {
103         settings = new QSettings(QSettings::UserScope, "Trolltech", "QMake");
104         settings->setFallbacksEnabled(false);
105     }
106 }
107
108 QString
109 QMakeProperty::value(const QString &v)
110 {
111     QString val = m_values.value(v);
112     if (!val.isNull())
113         return val;
114
115     initSettings();
116     if (!settings->contains(v))
117         return settings->value("2.01a/" + v).toString(); // Backwards compat
118     return settings->value(v).toString();
119 }
120
121 bool
122 QMakeProperty::hasValue(QString v)
123 {
124     return !value(v).isNull();
125 }
126
127 void
128 QMakeProperty::setValue(QString var, const QString &val)
129 {
130     initSettings();
131     settings->setValue(var, val);
132     settings->remove("2.01a/" + var); // Backwards compat
133 }
134
135 void
136 QMakeProperty::remove(const QString &var)
137 {
138     initSettings();
139     settings->remove(var);
140     settings->remove("2.01a/" + var); // Backwards compat
141 }
142
143 bool
144 QMakeProperty::exec()
145 {
146     bool ret = true;
147     if(Option::qmake_mode == Option::QMAKE_QUERY_PROPERTY) {
148         if(Option::prop::properties.isEmpty()) {
149             initSettings();
150             QStringList keys = settings->childKeys();
151             settings->beginGroup("2.01a");
152             keys += settings->childKeys();
153             settings->endGroup();
154             keys.removeDuplicates();
155             foreach (const QString &key, keys) {
156                 QString val = settings->value(settings->contains(key) ? key : "2.01a/" + key).toString();
157                 fprintf(stdout, "%s:%s\n", qPrintable(key), qPrintable(val));
158             }
159             QStringList specialProps;
160             for (int i = 0; i < sizeof(propList)/sizeof(propList[0]); i++)
161                 specialProps.append(QString::fromLatin1(propList[i].name));
162             specialProps.append("QMAKE_VERSION");
163 #ifdef QT_VERSION_STR
164             specialProps.append("QT_VERSION");
165 #endif
166             foreach (QString prop, specialProps) {
167                 QString val = value(prop);
168                 QString pval = value(prop + "/raw");
169                 QString gval = value(prop + "/get");
170                 fprintf(stdout, "%s:%s\n", prop.toLatin1().constData(), val.toLatin1().constData());
171                 if (!pval.isEmpty() && pval != val)
172                     fprintf(stdout, "%s/raw:%s\n", prop.toLatin1().constData(), pval.toLatin1().constData());
173                 if (!gval.isEmpty() && gval != (pval.isEmpty() ? val : pval))
174                     fprintf(stdout, "%s/get:%s\n", prop.toLatin1().constData(), gval.toLatin1().constData());
175             }
176             return true;
177         }
178         for(QStringList::ConstIterator it = Option::prop::properties.begin();
179             it != Option::prop::properties.end(); it++) {
180             if(Option::prop::properties.count() > 1)
181                 fprintf(stdout, "%s:", (*it).toLatin1().constData());
182             if(!hasValue((*it))) {
183                 ret = false;
184                 fprintf(stdout, "**Unknown**\n");
185             } else {
186                 fprintf(stdout, "%s\n", value((*it)).toLatin1().constData());
187             }
188         }
189     } else if(Option::qmake_mode == Option::QMAKE_SET_PROPERTY) {
190         for(QStringList::ConstIterator it = Option::prop::properties.begin();
191             it != Option::prop::properties.end(); it++) {
192             QString var = (*it);
193             it++;
194             if(it == Option::prop::properties.end()) {
195                 ret = false;
196                 break;
197             }
198             if(!var.startsWith("."))
199                 setValue(var, (*it));
200         }
201     } else if(Option::qmake_mode == Option::QMAKE_UNSET_PROPERTY) {
202         for(QStringList::ConstIterator it = Option::prop::properties.begin();
203             it != Option::prop::properties.end(); it++) {
204             QString var = (*it);
205             if(!var.startsWith("."))
206                 remove(var);
207         }
208     }
209     return ret;
210 }
211
212 QT_END_NAMESPACE