Add a new location for QML 2.x imports
[profile/ivi/qtbase.git] / qmake / property.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the qmake application of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.  For licensing terms and
14 ** conditions see http://qt.digia.com/licensing.  For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights.  These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file.  Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
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_ARCHDATA", QLibraryInfo::ArchDataPath, false },
60     { "QT_INSTALL_DATA", QLibraryInfo::DataPath, false },
61     { "QT_INSTALL_DOCS", QLibraryInfo::DocumentationPath, false },
62     { "QT_INSTALL_HEADERS", QLibraryInfo::HeadersPath, false },
63     { "QT_INSTALL_LIBS", QLibraryInfo::LibrariesPath, false },
64     { "QT_INSTALL_BINS", QLibraryInfo::BinariesPath, false },
65     { "QT_INSTALL_TESTS", QLibraryInfo::TestsPath, false },
66     { "QT_INSTALL_PLUGINS", QLibraryInfo::PluginsPath, false },
67     { "QT_INSTALL_IMPORTS", QLibraryInfo::ImportsPath, false },
68     { "QT_INSTALL_QML", QLibraryInfo::Qml2ImportsPath, 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     { "QMAKE_SPEC", QLibraryInfo::HostSpecPath, true },
77     { "QMAKE_XSPEC", QLibraryInfo::TargetSpecPath, true },
78 };
79
80 QMakeProperty::QMakeProperty() : settings(0)
81 {
82     for (int i = 0; i < sizeof(propList)/sizeof(propList[0]); i++) {
83         QString name = QString::fromLatin1(propList[i].name);
84         m_values[ProKey(name + "/get")] = QLibraryInfo::rawLocation(propList[i].loc, QLibraryInfo::EffectivePaths);
85         QString val = QLibraryInfo::rawLocation(propList[i].loc, QLibraryInfo::FinalPaths);
86         if (!propList[i].raw) {
87             m_values[ProKey(name)] = QLibraryInfo::location(propList[i].loc);
88             name += "/raw";
89         }
90         m_values[ProKey(name)] = val;
91     }
92     m_values["QMAKE_VERSION"] = ProString(QMAKE_VERSION_STR);
93 #ifdef QT_VERSION_STR
94     m_values["QT_VERSION"] = ProString(QT_VERSION_STR);
95 #endif
96 }
97
98 QMakeProperty::~QMakeProperty()
99 {
100     delete settings;
101     settings = 0;
102 }
103
104 void QMakeProperty::initSettings()
105 {
106     if(!settings) {
107         settings = new QSettings(QSettings::UserScope, "Trolltech", "QMake");
108         settings->setFallbacksEnabled(false);
109     }
110 }
111
112 ProString
113 QMakeProperty::value(const ProKey &vk)
114 {
115     ProString val = m_values.value(vk);
116     if (!val.isNull())
117         return val;
118
119     initSettings();
120     QString v = vk.toQString();
121     if (!settings->contains(v))
122         return settings->value("2.01a/" + v).toString(); // Backwards compat
123     return settings->value(v).toString();
124 }
125
126 bool
127 QMakeProperty::hasValue(const ProKey &v)
128 {
129     return !value(v).isNull();
130 }
131
132 void
133 QMakeProperty::setValue(QString var, const QString &val)
134 {
135     initSettings();
136     settings->setValue(var, val);
137     settings->remove("2.01a/" + var); // Backwards compat
138 }
139
140 void
141 QMakeProperty::remove(const QString &var)
142 {
143     initSettings();
144     settings->remove(var);
145     settings->remove("2.01a/" + var); // Backwards compat
146 }
147
148 bool
149 QMakeProperty::exec()
150 {
151     bool ret = true;
152     if(Option::qmake_mode == Option::QMAKE_QUERY_PROPERTY) {
153         if(Option::prop::properties.isEmpty()) {
154             initSettings();
155             QStringList keys = settings->childKeys();
156             settings->beginGroup("2.01a");
157             keys += settings->childKeys();
158             settings->endGroup();
159             keys.removeDuplicates();
160             foreach (const QString &key, keys) {
161                 QString val = settings->value(settings->contains(key) ? key : "2.01a/" + key).toString();
162                 fprintf(stdout, "%s:%s\n", qPrintable(key), qPrintable(val));
163             }
164             QStringList specialProps;
165             for (int i = 0; i < sizeof(propList)/sizeof(propList[0]); i++)
166                 specialProps.append(QString::fromLatin1(propList[i].name));
167             specialProps.append("QMAKE_VERSION");
168 #ifdef QT_VERSION_STR
169             specialProps.append("QT_VERSION");
170 #endif
171             foreach (QString prop, specialProps) {
172                 ProString val = value(ProKey(prop));
173                 ProString pval = value(ProKey(prop + "/raw"));
174                 ProString gval = value(ProKey(prop + "/get"));
175                 fprintf(stdout, "%s:%s\n", prop.toLatin1().constData(), val.toLatin1().constData());
176                 if (!pval.isEmpty() && pval != val)
177                     fprintf(stdout, "%s/raw:%s\n", prop.toLatin1().constData(), pval.toLatin1().constData());
178                 if (!gval.isEmpty() && gval != (pval.isEmpty() ? val : pval))
179                     fprintf(stdout, "%s/get:%s\n", prop.toLatin1().constData(), gval.toLatin1().constData());
180             }
181             return true;
182         }
183         for(QStringList::ConstIterator it = Option::prop::properties.begin();
184             it != Option::prop::properties.end(); it++) {
185             if(Option::prop::properties.count() > 1)
186                 fprintf(stdout, "%s:", (*it).toLatin1().constData());
187             const ProKey pkey(*it);
188             if (!hasValue(pkey)) {
189                 ret = false;
190                 fprintf(stdout, "**Unknown**\n");
191             } else {
192                 fprintf(stdout, "%s\n", value(pkey).toLatin1().constData());
193             }
194         }
195     } else if(Option::qmake_mode == Option::QMAKE_SET_PROPERTY) {
196         for(QStringList::ConstIterator it = Option::prop::properties.begin();
197             it != Option::prop::properties.end(); it++) {
198             QString var = (*it);
199             it++;
200             if(it == Option::prop::properties.end()) {
201                 ret = false;
202                 break;
203             }
204             if(!var.startsWith("."))
205                 setValue(var, (*it));
206         }
207     } else if(Option::qmake_mode == Option::QMAKE_UNSET_PROPERTY) {
208         for(QStringList::ConstIterator it = Option::prop::properties.begin();
209             it != Option::prop::properties.end(); it++) {
210             QString var = (*it);
211             if(!var.startsWith("."))
212                 remove(var);
213         }
214     }
215     return ret;
216 }
217
218 QT_END_NAMESPACE