1/*
2 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
3 SPDX-FileCopyrightText: 2021 Harald Sitter <sitter@kde.org>
4*/
5
6#ifndef KLIBEXEC_H
7#define KLIBEXEC_H
8
9#include <kcoreaddons_export.h>
10
11#include <QString>
12#include <QStringList>
13
14/**
15 * @brief Utility functions around libexec.
16 */
17namespace KLibexec
18{
19
20#ifndef K_DOXYGEN
21// Internal helpers. Do not use these but the inline variants.
22KCOREADDONS_EXPORT QString pathFromAddress(const QString &relativePath, void *address);
23KCOREADDONS_EXPORT QStringList pathCandidates(const QString &relativePath);
24#endif
25
26/**
27 * @brief Absolute libexec path resolved in relative relation to the current shared object.
28 *
29 * This function helps locate the absolute libexec path relative to the caller's binary artifact.
30 *
31 * For example:
32 *
33 * - Your source gets built with prefix /usr
34 * - Your binary artifact's presumed absolute path will be `/usr/lib/libfoobar.so`
35 * - You call `KLibexec::path("libexec/foobar")`
36 *
37 * Scenario 1 - The binaries are actually installed in /usr:
38 * - The function's output is `/usr/lib/libexec/foobar/` (resolved relatively from `/usr/lib/libfoobar.so`)
39 *
40 * Scenario 2 - The **same** binaries are installed in /opt (or moved there):
41 * - The function's output is `/opt/lib/libexec/foobar/` (resolved relatively from `/opt/lib/libfoobar.so`)
42 *
43 * @param relativePath relative element to append (e.g. "libexec/foobar" resulting in /usr/lib/libexec/foobar/ as output)
44 * when called with an empty string you effectively get the directory of your binary artifact.
45 * @return QString absolute libexec path or empty string if it cannot be resolved
46 * @since 5.91
47 */
48inline QString path(const QString &relativePath)
49{
50 // this MUST be inline so that the marker address is in the calling object!
51 static int marker = 0;
52 return pathFromAddress(relativePath, address: &marker);
53}
54
55/**
56 * @brief default paths list for KDE Frameworks
57 *
58 * This function returns a fairly opinionated list of paths you can feed into QStandardPaths. The list includes
59 * various standard locations for Qt and KDE Frameworks and should generally be sensible for most use cases.
60 * You may wish to append the absolute installation path as final fallback.
61 *
62 * @warning The precise content and order of the list is an implementation detail and not expected to remain stable!
63 *
64 * @param relativePath see path() - not all paths get this appended!
65 * @return QStringList list of search paths
66 * @since 5.91
67 */
68inline QStringList kdeFrameworksPaths(const QString &relativePath)
69{
70 // intentionally inline because path must be inline
71 return pathCandidates(relativePath: path(relativePath));
72}
73
74} // namespace KLibexec
75
76#endif // KLIBEXEC_H
77

source code of kcoreaddons/src/lib/util/klibexec.h