Cbeam
Loading...
Searching...
No Matches
runtime.hpp
Go to the documentation of this file.
1/*
2Copyright (c) 2025 acrion innovations GmbH
3Authors: Stefan Zipproth, s.zipproth@acrion.ch
4
5This file is part of Cbeam, see https://github.com/acrion/cbeam and https://cbeam.org
6
7Cbeam is offered under a commercial and under the AGPL license.
8For commercial licensing, contact us at https://acrion.ch/sales. For AGPL licensing, see below.
9
10AGPL licensing:
11
12Cbeam is free software: you can redistribute it and/or modify
13it under the terms of the GNU Affero General Public License as published by
14the Free Software Foundation, either version 3 of the License, or
15(at your option) any later version.
16
17Cbeam is distributed in the hope that it will be useful,
18but WITHOUT ANY WARRANTY; without even the implied warranty of
19MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20GNU Affero General Public License for more details.
21
22You should have received a copy of the GNU Affero General Public License
23along with Cbeam. If not, see <https://www.gnu.org/licenses/>.
24*/
25
26#pragma once
27
28#include <cbeam/config.hpp> // for HAVE_DLFCN_H
29
30#ifdef _WIN32
31 #include "windows_config.hpp"
32#elif HAVE_DLFCN_H
33 #include <cstdlib> // for realpath
34 #include <dlfcn.h> // for dladdr, Dl_info
35 #include <limits.h> // for PATH_MAX
36#endif
37
38#include <filesystem> // for std::filesystem::path
39#include <stdexcept> // for std::runtime_error
40#include <string> // for std::allocator, std::operator+, std::char_traits, std::string
41
42namespace cbeam::platform
43{
67 inline std::filesystem::path get_path_to_runtime_binary(const void* symbol_inside_runtime_binary = nullptr)
68 {
69 if (symbol_inside_runtime_binary == nullptr)
70 {
71 static const char symbol_inside_this_runtime_binary{};
72 symbol_inside_runtime_binary = &symbol_inside_this_runtime_binary;
73 }
74#ifdef _WIN32
75 char path[MAX_PATH];
76 HMODULE hm = NULL;
77
78 if (!GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
79 (LPCSTR)symbol_inside_runtime_binary,
80 &hm)
81 || GetModuleFileName(hm, path, sizeof(path)) <= 0)
82 {
83 throw std::runtime_error("cbeam::platform::get_path_to_runtime_binary: Could not get path to current runtime binary: " + get_last_windows_error_message());
84 }
85 else
86 {
87 return std::filesystem::absolute(std::filesystem::path(path));
88 }
89#elif HAVE_DLFCN_H
90 Dl_info dl_info;
91 if (!dladdr(symbol_inside_runtime_binary, &dl_info))
92 {
93 throw std::runtime_error("cbeam::platform::get_path_to_runtime_binary: Could not resolve symbol");
94 }
95
96 if (dl_info.dli_fname)
97 {
98 char resolved_path[PATH_MAX];
99 if (realpath(dl_info.dli_fname, resolved_path) != nullptr)
100 {
101 return std::filesystem::path(resolved_path);
102 }
103 else
104 {
105 throw std::runtime_error("cbeam::platform::get_path_to_runtime_binary: Could not resolve symbolic link '" + std::string{dl_info.dli_fname} + "'");
106 }
107 }
108 else
109 {
110 throw std::runtime_error("cbeam::platform::get_path_to_runtime_binary: Symbol is not associated with a shared library or executable");
111 }
112#else
113 #error Unsupported platform
114#endif
115 throw std::runtime_error("cbeam::platform::get_path_to_runtime_binary: Could not get path to current runtime binary");
116 }
117}
Groups platform-specific helpers for Windows, Linux, and macOS. These utilities detect CPU architectu...
Definition clock_precision.hpp:42
std::string get_last_windows_error_message()
Retrieves a descriptive error message for the last Windows API error.
Definition windows_config.hpp:66
std::filesystem::path get_path_to_runtime_binary(const void *symbol_inside_runtime_binary=nullptr)
Retrieves the absolute path to the runtime binary (either an executable or a shared library) that con...
Definition runtime.hpp:67
Header file to manage inclusion of windows.h with specific settings.