1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the qmake application of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:GPL-EXCEPT$
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 The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21 ** included in the packaging of this file. Please review the following
22 ** information to ensure the GNU General Public License requirements will
23 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24 **
25 ** $QT_END_LICENSE$
26 **
27 ****************************************************************************/
28 
29 #include "property.h"
30 #include "option.h"
31 
32 #include <qdir.h>
33 #include <qsettings.h>
34 #include <qlibraryinfo.h>
35 #include <qstringlist.h>
36 #include <stdio.h>
37 
38 QT_BEGIN_NAMESPACE
39 
40 static const struct {
41     const char *name;
42     QLibraryInfo::LibraryLocation loc;
43     bool raw;
44     bool singular;
45 } propList[] = {
46     { "QT_SYSROOT", QLibraryInfo::SysrootPath, true, true },
47     { "QT_INSTALL_PREFIX", QLibraryInfo::PrefixPath, false, false },
48     { "QT_INSTALL_ARCHDATA", QLibraryInfo::ArchDataPath, false, false },
49     { "QT_INSTALL_DATA", QLibraryInfo::DataPath, false, false },
50     { "QT_INSTALL_DOCS", QLibraryInfo::DocumentationPath, false, false },
51     { "QT_INSTALL_HEADERS", QLibraryInfo::HeadersPath, false, false },
52     { "QT_INSTALL_LIBS", QLibraryInfo::LibrariesPath, false, false },
53     { "QT_INSTALL_LIBEXECS", QLibraryInfo::LibraryExecutablesPath, false, false },
54     { "QT_INSTALL_BINS", QLibraryInfo::BinariesPath, false, false },
55     { "QT_INSTALL_TESTS", QLibraryInfo::TestsPath, false, false },
56     { "QT_INSTALL_PLUGINS", QLibraryInfo::PluginsPath, false, false },
57     { "QT_INSTALL_IMPORTS", QLibraryInfo::ImportsPath, false, false },
58     { "QT_INSTALL_QML", QLibraryInfo::Qml2ImportsPath, false, false },
59     { "QT_INSTALL_TRANSLATIONS", QLibraryInfo::TranslationsPath, false, false },
60     { "QT_INSTALL_CONFIGURATION", QLibraryInfo::SettingsPath, false, false },
61     { "QT_INSTALL_EXAMPLES", QLibraryInfo::ExamplesPath, false, false },
62     { "QT_INSTALL_DEMOS", QLibraryInfo::ExamplesPath, false, false }, // Just backwards compat
63     { "QT_HOST_PREFIX", QLibraryInfo::HostPrefixPath, true, false },
64     { "QT_HOST_DATA", QLibraryInfo::HostDataPath, true, false },
65     { "QT_HOST_BINS", QLibraryInfo::HostBinariesPath, true, false },
66     { "QT_HOST_LIBS", QLibraryInfo::HostLibrariesPath, true, false },
67     { "QMAKE_SPEC", QLibraryInfo::HostSpecPath, true, true },
68     { "QMAKE_XSPEC", QLibraryInfo::TargetSpecPath, true, true },
69 };
70 
QMakeProperty()71 QMakeProperty::QMakeProperty() : settings(nullptr)
72 {
73     reload();
74 }
75 
reload()76 void QMakeProperty::reload()
77 {
78     QLibraryInfo::reload();
79     for (unsigned i = 0; i < sizeof(propList)/sizeof(propList[0]); i++) {
80         QString name = QString::fromLatin1(propList[i].name);
81         if (!propList[i].singular) {
82             m_values[ProKey(name + "/src")] = QLibraryInfo::rawLocation(propList[i].loc, QLibraryInfo::EffectiveSourcePaths);
83             m_values[ProKey(name + "/get")] = QLibraryInfo::rawLocation(propList[i].loc, QLibraryInfo::EffectivePaths);
84         }
85         QString val = QLibraryInfo::rawLocation(propList[i].loc, QLibraryInfo::FinalPaths);
86         if (!propList[i].raw) {
87             m_values[ProKey(name + "/dev")] = QLibraryInfo::rawLocation(propList[i].loc, QLibraryInfo::DevicePaths);
88             m_values[ProKey(name)] = QLibraryInfo::location(propList[i].loc);
89             name += "/raw";
90         }
91         m_values[ProKey(name)] = val;
92     }
93     m_values["QMAKE_VERSION"] = ProString(QMAKE_VERSION_STR);
94 #ifdef QT_VERSION_STR
95     m_values["QT_VERSION"] = ProString(QT_VERSION_STR);
96 #endif
97 }
98 
~QMakeProperty()99 QMakeProperty::~QMakeProperty()
100 {
101     delete settings;
102     settings = nullptr;
103 }
104 
initSettings()105 void QMakeProperty::initSettings()
106 {
107     if(!settings) {
108         settings = new QSettings(QSettings::UserScope, "QtProject", "QMake");
109         settings->setFallbacksEnabled(false);
110     }
111 }
112 
113 ProString
value(const ProKey & vk)114 QMakeProperty::value(const ProKey &vk)
115 {
116     ProString val = m_values.value(vk);
117     if (!val.isNull())
118         return val;
119 
120     initSettings();
121     return settings->value(vk.toQString()).toString();
122 }
123 
124 bool
hasValue(const ProKey & v)125 QMakeProperty::hasValue(const ProKey &v)
126 {
127     return !value(v).isNull();
128 }
129 
130 void
setValue(QString var,const QString & val)131 QMakeProperty::setValue(QString var, const QString &val)
132 {
133     initSettings();
134     settings->setValue(var, val);
135 }
136 
137 void
remove(const QString & var)138 QMakeProperty::remove(const QString &var)
139 {
140     initSettings();
141     settings->remove(var);
142 }
143 
144 bool
exec()145 QMakeProperty::exec()
146 {
147     bool ret = true;
148     if(Option::qmake_mode == Option::QMAKE_QUERY_PROPERTY) {
149         if(Option::prop::properties.isEmpty()) {
150             initSettings();
151             const auto keys = settings->childKeys();
152             for (const QString &key : keys) {
153                 QString val = settings->value(key).toString();
154                 fprintf(stdout, "%s:%s\n", qPrintable(key), qPrintable(val));
155             }
156             QStringList specialProps;
157             for (unsigned i = 0; i < sizeof(propList)/sizeof(propList[0]); i++)
158                 specialProps.append(QString::fromLatin1(propList[i].name));
159             specialProps.append("QMAKE_VERSION");
160 #ifdef QT_VERSION_STR
161             specialProps.append("QT_VERSION");
162 #endif
163             for (const QString &prop : qAsConst(specialProps)) {
164                 ProString val = value(ProKey(prop));
165                 ProString pval = value(ProKey(prop + "/raw"));
166                 ProString gval = value(ProKey(prop + "/get"));
167                 ProString sval = value(ProKey(prop + "/src"));
168                 ProString dval = value(ProKey(prop + "/dev"));
169                 fprintf(stdout, "%s:%s\n", prop.toLatin1().constData(), val.toLatin1().constData());
170                 if (!pval.isEmpty() && pval != val)
171                     fprintf(stdout, "%s/raw:%s\n", prop.toLatin1().constData(), pval.toLatin1().constData());
172                 if (!gval.isEmpty() && gval != (pval.isEmpty() ? val : pval))
173                     fprintf(stdout, "%s/get:%s\n", prop.toLatin1().constData(), gval.toLatin1().constData());
174                 if (!sval.isEmpty() && sval != gval)
175                     fprintf(stdout, "%s/src:%s\n", prop.toLatin1().constData(), sval.toLatin1().constData());
176                 if (!dval.isEmpty() && dval != pval)
177                     fprintf(stdout, "%s/dev:%s\n", prop.toLatin1().constData(), dval.toLatin1().constData());
178             }
179             return true;
180         }
181         for (QStringList::ConstIterator it = Option::prop::properties.cbegin();
182             it != Option::prop::properties.cend(); it++) {
183             if(Option::prop::properties.count() > 1)
184                 fprintf(stdout, "%s:", (*it).toLatin1().constData());
185             const ProKey pkey(*it);
186             if (!hasValue(pkey)) {
187                 ret = false;
188                 fprintf(stdout, "**Unknown**\n");
189             } else {
190                 fprintf(stdout, "%s\n", value(pkey).toLatin1().constData());
191             }
192         }
193     } else if(Option::qmake_mode == Option::QMAKE_SET_PROPERTY) {
194         for (QStringList::ConstIterator it = Option::prop::properties.cbegin();
195             it != Option::prop::properties.cend(); it++) {
196             QString var = (*it);
197             it++;
198             if (it == Option::prop::properties.cend()) {
199                 ret = false;
200                 break;
201             }
202             if(!var.startsWith("."))
203                 setValue(var, (*it));
204         }
205     } else if(Option::qmake_mode == Option::QMAKE_UNSET_PROPERTY) {
206         for (QStringList::ConstIterator it = Option::prop::properties.cbegin();
207             it != Option::prop::properties.cend(); it++) {
208             QString var = (*it);
209             if(!var.startsWith("."))
210                 remove(var);
211         }
212     }
213     return ret;
214 }
215 
216 QT_END_NAMESPACE
217