File indexing completed on 2024-05-19 03:56:24

0001 /*
0002     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0003     SPDX-FileCopyrightText: 2021 Harald Sitter <sitter@kde.org>
0004 */
0005 
0006 #ifndef KLIBEXEC_H
0007 #define KLIBEXEC_H
0008 
0009 #include <kcoreaddons_export.h>
0010 
0011 #include <QString>
0012 #include <QStringList>
0013 
0014 /**
0015  * @brief Utility functions around libexec.
0016  */
0017 namespace KLibexec
0018 {
0019 
0020 #ifndef K_DOXYGEN
0021 // Internal helpers. Do not use these but the inline variants.
0022 KCOREADDONS_EXPORT QString pathFromAddress(const QString &relativePath, void *address);
0023 KCOREADDONS_EXPORT QStringList pathCandidates(const QString &relativePath);
0024 #endif
0025 
0026 /**
0027  * @brief Absolute libexec path resolved in relative relation to the current shared object.
0028  *
0029  * This function helps locate the absolute libexec path relative to the caller's binary artifact.
0030  *
0031  * For example:
0032  *
0033  * - Your source gets built with prefix /usr
0034  * - Your binary artifact's presumed absolute path will be `/usr/lib/libfoobar.so`
0035  * - You call `KLibexec::path("libexec/foobar")`
0036  *
0037  * Scenario 1 - The binaries are actually installed in /usr:
0038  * - The function's output is `/usr/lib/libexec/foobar/` (resolved relatively from `/usr/lib/libfoobar.so`)
0039  *
0040  * Scenario 2 - The **same** binaries are installed in /opt (or moved there):
0041  * - The function's output is `/opt/lib/libexec/foobar/` (resolved relatively from `/opt/lib/libfoobar.so`)
0042  *
0043  * @param relativePath relative element to append (e.g. "libexec/foobar" resulting in /usr/lib/libexec/foobar/ as output)
0044  *   when called with an empty string you effectively get the directory of your binary artifact.
0045  * @return QString absolute libexec path or empty string if it cannot be resolved
0046  * @since 5.91
0047  */
0048 inline QString path(const QString &relativePath)
0049 {
0050     // this MUST be inline so that the marker address is in the calling object!
0051     static int marker = 0;
0052     return pathFromAddress(relativePath, &marker);
0053 }
0054 
0055 /**
0056  * @brief default paths list for KDE Frameworks
0057  *
0058  * This function returns a fairly opinionated list of paths you can feed into QStandardPaths. The list includes
0059  * various standard locations for Qt and KDE Frameworks and should generally be sensible for most use cases.
0060  * You may wish to append the absolute installation path as final fallback.
0061  *
0062  * @warning The precise content and order of the list is an implementation detail and not expected to remain stable!
0063  *
0064  * @param relativePath see path() - not all paths get this appended!
0065  * @return QStringList list of search paths
0066  * @since 5.91
0067  */
0068 inline QStringList kdeFrameworksPaths(const QString &relativePath)
0069 {
0070     // intentionally inline because path must be inline
0071     return pathCandidates(path(relativePath));
0072 }
0073 
0074 } // namespace KLibexec
0075 
0076 #endif // KLIBEXEC_H