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 | * \namespace KLibexec |
16 | * \inmodule KCoreAddons |
17 | * \brief Utility functions around libexec. |
18 | */ |
19 | namespace KLibexec |
20 | { |
21 | |
22 | // Internal helpers. Do not use these but the inline variants. |
23 | KCOREADDONS_EXPORT QString pathFromAddress(const QString &relativePath, void *address); |
24 | KCOREADDONS_EXPORT QStringList pathCandidates(const QString &relativePath); |
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 | * \list |
33 | * \li Your source gets built with prefix /usr |
34 | * \li Your binary artifact's presumed absolute path will be /usr/lib/libfoobar.so |
35 | * \li You call KLibexec::path("libexec/foobar") |
36 | * \endlist |
37 | * |
38 | * Scenario 1: The binaries are actually installed in /usr: |
39 | * \list |
40 | * \li The function's output is /usr/lib/libexec/foobar/ (resolved relatively from /usr/lib/libfoobar.so) |
41 | * \endlist |
42 | * |
43 | * Scenario 2: The same binaries are installed in /opt (or moved there): |
44 | * \list |
45 | * \li The function's output is /opt/lib/libexec/foobar/ (resolved relatively from /opt/lib/libfoobar.so) |
46 | * \endlist |
47 | * |
48 | * \a relativePath relative element to append (e.g. "libexec/foobar" resulting in /usr/lib/libexec/foobar/ as output) |
49 | * when called with an empty string you effectively get the directory of your binary artifact. |
50 | * |
51 | * Returns the absolute libexec path or empty string if it cannot be resolved |
52 | * \since 5.91 |
53 | */ |
54 | inline QString path(const QString &relativePath) |
55 | { |
56 | // this MUST be inline so that the marker address is in the calling object! |
57 | static int marker = 0; |
58 | return pathFromAddress(relativePath, address: &marker); |
59 | } |
60 | |
61 | /*! |
62 | * \brief default paths list for KDE Frameworks |
63 | * |
64 | * This function returns a fairly opinionated list of paths you can feed into QStandardPaths. The list includes |
65 | * various standard locations for Qt and KDE Frameworks and should generally be sensible for most use cases. |
66 | * You may wish to append the absolute installation path as final fallback. |
67 | * |
68 | * \warning The precise content and order of the list is an implementation detail and not expected to remain stable! |
69 | * |
70 | * \a relativePath see path() - not all paths get this appended! |
71 | * |
72 | * Returns QStringList list of search paths |
73 | * \since 5.91 |
74 | */ |
75 | inline QStringList kdeFrameworksPaths(const QString &relativePath) |
76 | { |
77 | // intentionally inline because path must be inline |
78 | return pathCandidates(relativePath: path(relativePath)); |
79 | } |
80 | |
81 | } // namespace KLibexec |
82 | |
83 | #endif // KLIBEXEC_H |
84 | |