remove library version matching from qt.conf
[profile/ivi/qtbase.git] / src / corelib / global / qlibraryinfo.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 QtCore module 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 "qdir.h"
43 #include "qfile.h"
44 #include "qconfig.h"
45 #include "qsettings.h"
46 #include "qlibraryinfo.h"
47 #include "qscopedpointer.h"
48
49 #if defined(QT_BUILD_QMAKE) || defined(QT_BOOTSTRAPPED)
50 # define BOOTSTRAPPING
51 #endif
52
53 #ifdef BOOTSTRAPPING
54 QT_BEGIN_NAMESPACE
55 extern QString qmake_libraryInfoFile();
56 QT_END_NAMESPACE
57 #else
58 # include "qcoreapplication.h"
59 #endif
60
61 #ifdef Q_OS_MAC
62 #  include "private/qcore_mac_p.h"
63 #endif
64
65 #include "qconfig.cpp"
66
67 QT_BEGIN_NAMESPACE
68
69 extern void qDumpCPUFeatures(); // in qsimd.cpp
70
71 #ifndef QT_NO_SETTINGS
72
73 struct QLibrarySettings
74 {
75     QLibrarySettings();
76     QScopedPointer<QSettings> settings;
77 };
78 Q_GLOBAL_STATIC(QLibrarySettings, qt_library_settings)
79
80 class QLibraryInfoPrivate
81 {
82 public:
83     static QSettings *findConfiguration();
84     static void cleanup()
85     {
86         QLibrarySettings *ls = qt_library_settings();
87         if (ls)
88             ls->settings.reset(0);
89     }
90     static QSettings *configuration()
91     {
92         QLibrarySettings *ls = qt_library_settings();
93         return ls ? ls->settings.data() : 0;
94     }
95 };
96
97 QLibrarySettings::QLibrarySettings()
98     : settings(QLibraryInfoPrivate::findConfiguration())
99 {
100 #ifndef BOOTSTRAPPING
101     qAddPostRoutine(QLibraryInfoPrivate::cleanup);
102 #endif
103 }
104
105 QSettings *QLibraryInfoPrivate::findConfiguration()
106 {
107     QString qtconfig = QLatin1String(":/qt/etc/qt.conf");
108 #ifdef BOOTSTRAPPING
109     if(!QFile::exists(qtconfig))
110         qtconfig = qmake_libraryInfoFile();
111 #else
112     if (!QFile::exists(qtconfig) && QCoreApplication::instance()) {
113 #ifdef Q_OS_MAC
114         CFBundleRef bundleRef = CFBundleGetMainBundle();
115         if (bundleRef) {
116             QCFType<CFURLRef> urlRef = CFBundleCopyResourceURL(bundleRef,
117                                                                QCFString(QLatin1String("qt.conf")),
118                                                                0,
119                                                                0);
120             if (urlRef) {
121                 QCFString path = CFURLCopyFileSystemPath(urlRef, kCFURLPOSIXPathStyle);
122                 qtconfig = QDir::cleanPath(path);
123             }
124         }
125         if (qtconfig.isEmpty())
126 #endif
127             {
128                 QDir pwd(QCoreApplication::applicationDirPath());
129                 qtconfig = pwd.filePath(QLatin1String("qt.conf"));
130             }
131     }
132 #endif
133     if (QFile::exists(qtconfig))
134         return new QSettings(qtconfig, QSettings::IniFormat);
135     return 0;     //no luck
136 }
137
138 /*!
139     \class QLibraryInfo
140     \brief The QLibraryInfo class provides information about the Qt library.
141
142     Many pieces of information are established when Qt is configured and built.
143     This class provides an abstraction for accessing that information.
144     By using the static functions of this class, an application can obtain
145     information about the instance of the Qt library which the application
146     is using at run-time.
147
148     You can also use a \c qt.conf file to override the hard-coded paths
149     that are compiled into the Qt library. For more information, see
150     the \l {Using qt.conf} documentation.
151
152     \sa QSysInfo, {Using qt.conf}
153 */
154
155 /*! \internal
156
157    You cannot create a QLibraryInfo, instead only the static functions are available to query
158    information.
159 */
160
161 QLibraryInfo::QLibraryInfo()
162 { }
163
164 /*!
165   Returns the person to whom this build of Qt is licensed.
166
167   \sa licensedProducts()
168 */
169
170 QString
171 QLibraryInfo::licensee()
172 {
173     const char *str = QT_CONFIGURE_LICENSEE;
174     return QString::fromLocal8Bit(str);
175 }
176
177 /*!
178   Returns the products that the license for this build of Qt has access to.
179
180   \sa licensee()
181 */
182
183 QString
184 QLibraryInfo::licensedProducts()
185 {
186     const char *str = QT_CONFIGURE_LICENSED_PRODUCTS;
187     return QString::fromLatin1(str);
188 }
189
190 /*!
191     \since 4.6
192     Returns the installation date for this build of Qt. The install date will
193     usually be the last time that Qt sources were configured.
194 */
195 #ifndef QT_NO_DATESTRING
196 QDate
197 QLibraryInfo::buildDate()
198 {
199     return QDate::fromString(QString::fromLatin1(qt_configure_installation + 12), Qt::ISODate);
200 }
201 #endif //QT_NO_DATESTRING
202
203 /*!
204     \since 5.0
205     Returns true if this build of Qt was built with debugging enabled, or
206     false if it was built in release mode.
207 */
208 bool
209 QLibraryInfo::isDebugBuild()
210 {
211 #ifdef QT_DEBUG
212     return true;
213 #endif
214     return false;
215 }
216
217 /*!
218   Returns the location specified by \a loc.
219
220 */
221
222 QString
223 QLibraryInfo::location(LibraryLocation loc)
224 {
225     QString ret;
226     if(!QLibraryInfoPrivate::configuration()) {
227         const char *path = 0;
228         switch (loc) {
229 #ifdef QT_CONFIGURE_PREFIX_PATH
230         case PrefixPath:
231             path = QT_CONFIGURE_PREFIX_PATH;
232             break;
233 #endif
234 #ifdef QT_CONFIGURE_DOCUMENTATION_PATH
235         case DocumentationPath:
236             path = QT_CONFIGURE_DOCUMENTATION_PATH;
237             break;
238 #endif
239 #ifdef QT_CONFIGURE_HEADERS_PATH
240         case HeadersPath:
241             path = QT_CONFIGURE_HEADERS_PATH;
242             break;
243 #endif
244 #ifdef QT_CONFIGURE_LIBRARIES_PATH
245         case LibrariesPath:
246             path = QT_CONFIGURE_LIBRARIES_PATH;
247             break;
248 #endif
249 #ifdef QT_CONFIGURE_BINARIES_PATH
250         case BinariesPath:
251             path = QT_CONFIGURE_BINARIES_PATH;
252             break;
253 #endif
254 #ifdef QT_CONFIGURE_PLUGINS_PATH
255         case PluginsPath:
256             path = QT_CONFIGURE_PLUGINS_PATH;
257             break;
258 #endif
259 #ifdef QT_CONFIGURE_IMPORTS_PATH
260         case ImportsPath:
261             path = QT_CONFIGURE_IMPORTS_PATH;
262             break;
263 #endif
264 #ifdef QT_CONFIGURE_DATA_PATH
265         case DataPath:
266             path = QT_CONFIGURE_DATA_PATH;
267             break;
268 #endif
269 #ifdef QT_CONFIGURE_TRANSLATIONS_PATH
270         case TranslationsPath:
271             path = QT_CONFIGURE_TRANSLATIONS_PATH;
272             break;
273 #endif
274 #ifdef QT_CONFIGURE_SETTINGS_PATH
275         case SettingsPath:
276             path = QT_CONFIGURE_SETTINGS_PATH;
277             break;
278 #endif
279 #ifdef QT_CONFIGURE_EXAMPLES_PATH
280         case ExamplesPath:
281             path = QT_CONFIGURE_EXAMPLES_PATH;
282             break;
283 #endif
284 #ifdef QT_CONFIGURE_TESTS_PATH
285         case TestsPath:
286             path = QT_CONFIGURE_TESTS_PATH;
287             break;
288 #endif
289         default:
290             break;
291         }
292
293         if (path)
294             ret = QString::fromLocal8Bit(path);
295     } else {
296         QString key;
297         QString defaultValue;
298         switch(loc) {
299         case PrefixPath:
300             key = QLatin1String("Prefix");
301             break;
302         case DocumentationPath:
303             key = QLatin1String("Documentation");
304             defaultValue = QLatin1String("doc");
305             break;
306         case HeadersPath:
307             key = QLatin1String("Headers");
308             defaultValue = QLatin1String("include");
309             break;
310         case LibrariesPath:
311             key = QLatin1String("Libraries");
312             defaultValue = QLatin1String("lib");
313             break;
314         case BinariesPath:
315             key = QLatin1String("Binaries");
316             defaultValue = QLatin1String("bin");
317             break;
318         case PluginsPath:
319             key = QLatin1String("Plugins");
320             defaultValue = QLatin1String("plugins");
321             break;
322         case ImportsPath:
323             key = QLatin1String("Imports");
324             defaultValue = QLatin1String("imports");
325             break;
326         case DataPath:
327             key = QLatin1String("Data");
328             break;
329         case TranslationsPath:
330             key = QLatin1String("Translations");
331             defaultValue = QLatin1String("translations");
332             break;
333         case SettingsPath:
334             key = QLatin1String("Settings");
335             break;
336         case ExamplesPath:
337             key = QLatin1String("Examples");
338             break;
339         case TestsPath:
340             key = QLatin1String("Tests");
341             defaultValue = QLatin1String("tests");
342             break;
343         default:
344             break;
345         }
346
347         if(!key.isNull()) {
348             QSettings *config = QLibraryInfoPrivate::configuration();
349             config->beginGroup(QLatin1String("Paths"));
350
351             ret = config->value(key, defaultValue).toString();
352
353             // expand environment variables in the form $(ENVVAR)
354             int rep;
355             QRegExp reg_var(QLatin1String("\\$\\(.*\\)"));
356             reg_var.setMinimal(true);
357             while((rep = reg_var.indexIn(ret)) != -1) {
358                 ret.replace(rep, reg_var.matchedLength(),
359                             QString::fromLocal8Bit(qgetenv(ret.mid(rep + 2,
360                                 reg_var.matchedLength() - 3).toLatin1().constData()).constData()));
361             }
362
363             config->endGroup();
364         }
365     }
366
367     if (QDir::isRelativePath(ret)) {
368         QString baseDir;
369         if (loc == PrefixPath) {
370             // we make the prefix path absolute to the executable's directory
371 #ifdef BOOTSTRAPPING
372             baseDir = QFileInfo(qmake_libraryInfoFile()).absolutePath();
373 #else
374             if (QCoreApplication::instance()) {
375 #ifdef Q_OS_MAC
376                 CFBundleRef bundleRef = CFBundleGetMainBundle();
377                 if (bundleRef) {
378                     QCFType<CFURLRef> urlRef = CFBundleCopyBundleURL(bundleRef);
379                     if (urlRef) {
380                         QCFString path = CFURLCopyFileSystemPath(urlRef, kCFURLPOSIXPathStyle);
381                         return QDir::cleanPath(QString(path) + QLatin1String("/Contents/") + ret);
382                     }
383                 }
384 #endif
385                 baseDir = QCoreApplication::applicationDirPath();
386             } else {
387                 baseDir = QDir::currentPath();
388             }
389 #endif
390         } else {
391             // we make any other path absolute to the prefix directory
392             baseDir = location(PrefixPath);
393         }
394         ret = QDir::cleanPath(baseDir + QLatin1Char('/') + ret);
395     }
396     return ret;
397 }
398
399 /*!
400     \enum QLibraryInfo::LibraryLocation
401
402     \keyword library location
403
404     This enum type is used to specify a specific location
405     specifier:
406
407     \value PrefixPath The default prefix for all paths.
408     \value DocumentationPath The location for documentation upon install.
409     \value HeadersPath The location for all headers.
410     \value LibrariesPath The location of installed libraries.
411     \value BinariesPath The location of installed Qt binaries (tools and applications).
412     \value PluginsPath The location of installed Qt plugins.
413     \value ImportsPath The location of installed QML extensions to import.
414     \value DataPath The location of general Qt data.
415     \value TranslationsPath The location of translation information for Qt strings.
416     \value SettingsPath The location for Qt settings.
417     \value ExamplesPath The location for examples upon install.
418     \value TestsPath The location of installed Qt testcases.
419
420     \sa location()
421 */
422
423 #endif // QT_NO_SETTINGS
424
425 QT_END_NAMESPACE
426
427 #if defined(Q_CC_GNU) && defined(ELF_INTERPRETER)
428 #  include <stdio.h>
429 #  include <stdlib.h>
430
431 extern const char qt_core_interpreter[] __attribute__((section(".interp")))
432     = ELF_INTERPRETER;
433
434 extern "C" void qt_core_boilerplate();
435 void qt_core_boilerplate()
436 {
437     printf("This is the QtCore library version " QT_VERSION_STR "\n"
438            "Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).\n"
439            "Contact: http://www.qt-project.org/\n"
440            "\n"
441            "Build date:          %s\n"
442            "Installation prefix: %s\n"
443            "Library path:        %s\n"
444            "Include path:        %s\n",
445            qt_configure_installation + 12,
446            qt_configure_prefix_path_str + 12,
447            qt_configure_libraries_path_str + 12,
448            qt_configure_headers_path_str + 12);
449
450     QT_PREPEND_NAMESPACE(qDumpCPUFeatures)();
451
452 #ifdef QT_EVAL
453     extern void qt_core_eval_init(uint);
454     qt_core_eval_init(1);
455 #endif
456
457     exit(0);
458 }
459
460 #endif