jaulib v1.3.6
Jau Support Library (C++, Java, ..)
Loading...
Searching...
No Matches
os_support.cpp
Go to the documentation of this file.
1/*
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2020-2024 Gothel Software e.K.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25#include <cstring>
26
27#include <ctime>
28
29#include <jau/cpuid.hpp>
30#include <jau/string_util.hpp>
31
32#include <jau/debug.hpp>
33#include <jau/file_util.hpp>
34#include <jau/os/dyn_linker.hpp>
35#include <jau/os/os_support.hpp>
36#include <jau/os/user_info.hpp>
37
38#if !defined(_WIN32)
39 #include <sys/utsname.h>
40#endif
41
42using namespace jau;
43
44#define CASE_TO_STRING(U,V) case U::V: return #V;
45
47 #if !defined(_WIN32)
48 struct utsname uinfo;
49 if( 0 == ::uname(&uinfo) ) {
50 info.sysname = std::string(uinfo.sysname);
51 info.nodename = std::string(uinfo.nodename);
52 info.release = std::string(uinfo.release);
53 info.version = std::string(uinfo.version);
54 info.machine = std::string(uinfo.machine);
55 #if defined(_GNU_SOURCE)
56 info.domainname = std::string(uinfo.domainname);
57 #endif
58 return true;
59 }
60 #endif
61 return false;
62}
63
64std::string jau::os::get_os_and_arch(const os_type_t os, const jau::cpu::cpu_family_t cpu, const abi_type_t abi, const endian_t e) noexcept {
65 std::string os_;
66 std::string _and_arch_tmp, _and_arch_final;
67
68 switch( cpu ) {
70 if( abi_type_t::gnu_armhf == abi ) {
71 _and_arch_tmp = "armv6hf";
72 } else {
73 _and_arch_tmp = "armv6";
74 }
75 break;
77 _and_arch_tmp = "i586";
78 break;
80 _and_arch_tmp = "ppc";
81 break;
83 _and_arch_tmp = jau::is_little_endian(e) ? "mipsel" : "mips";
84 break;
86 _and_arch_tmp = "sparc";
87 break;
89 _and_arch_tmp = "superh";
90 break;
91
93 _and_arch_tmp = "aarch64";
94 break;
96 _and_arch_tmp = "amd64";
97 break;
99 _and_arch_tmp = jau::is_little_endian(e) ? "ppc64le" : "ppc64";
100 break;
102 _and_arch_tmp = "mips64";
103 break;
105 _and_arch_tmp = "ia64";
106 break;
108 _and_arch_tmp = "sparcv9";
109 break;
111 _and_arch_tmp = "superh64";
112 break;
114 _and_arch_tmp = "wasm32";
115 break;
117 _and_arch_tmp = "wasm64";
118 break;
119 default:
120 _and_arch_tmp = "undef_arch";
121 break;
122 }
123
124 switch( os ) {
126 os_ = "android";
127 _and_arch_final = _and_arch_tmp;
128 break;
130 os_ = "darwin";
131 _and_arch_final = "universal";
132 break;
134 os_ = "windows";
135 _and_arch_final = _and_arch_tmp;
136 break;
137 case os_type_t::Linux:
138 os_ = "linux";
139 _and_arch_final = _and_arch_tmp;
140 break;
142 os_ = "freebsd";
143 _and_arch_final = _and_arch_tmp;
144 break;
146 os_ = "qnxnto";
147 _and_arch_final = _and_arch_tmp;
148 break;
150 os_ = "webasm";
151 _and_arch_final = _and_arch_tmp;
152 break;
154 os_ = "emscripten";
155 _and_arch_final = _and_arch_tmp;
156 break;
157 default:
158 os_ = "undef_os";
159 _and_arch_final = _and_arch_tmp;
160 break;
161 }
162 return os_ + "-" + _and_arch_final;
163}
164
165
166std::string jau::os::get_platform_info(std::string& sb) noexcept {
170 bool rti_ok = jau::os::get_rt_os_info(rti);
172
173 sb.append( jau::format_string("Platform: %s %s, %s (",
174 jau::os::to_string(os).c_str(),
175 ( rti_ok ? rti.release.c_str() : ""),
176 jau::cpu::to_string(cpu.family).c_str() ) );
177 cpu.toString(sb, true);
178 sb.append( jau::format_string("), abi %s, ", jau::os::to_string(abi).c_str()) );
179 sb.append( jau::os::get_os_and_arch(os, cpu.family, abi, cpu.byte_order) );
180 if( rti_ok ) {
181 sb.append(", runtime: ").append(rti.to_string()).append("\n");
182 }
183 return sb;
184}
185
186static
187std::pair<std::string, bool>
188DynamicLinker_processCanonicalNameImpl(bool strip, const std::string& filename, const bool isBasename,
189 const bool caseInsensitive=jau::os::is_windows()) noexcept
190{
191 const std::string prefix = jau::os::DynamicLinker::getDefaultPrefix();
192 const std::string suffix = jau::os::DynamicLinker::getDefaultSuffix();
193 const std::string libBaseName = isBasename ? filename : jau::fs::basename(filename);
194 const std::string libBaseNameLC = caseInsensitive ? jau::toLower(libBaseName) : libBaseName;
195 const size_t pre_idx = libBaseNameLC.find(prefix);
196 if( 0 == pre_idx ) { // starts-with
197 const size_t sfx_idx = libBaseNameLC.rfind(suffix);
198 if( std::string::npos != sfx_idx ) {
199 // Check to see if everything after it is a Unix version number
200 bool ok = true;
201 for (size_t i = sfx_idx + suffix.length(); i < libBaseName.size(); i++) {
202 const char c = libBaseName[i];
203 if (!(c == '.' || (c >= '0' && c <= '9'))) {
204 ok = false;
205 break;
206 }
207 }
208 if (ok) {
209 std::string res;
210 if( strip ) {
211 const size_t sfx_len = libBaseName.size() - sfx_idx;
212 res = libBaseName.substr(prefix.size(), libBaseName.size() - prefix.size() - sfx_len);
213 }
214 return std::make_pair(res, true);
215 }
216 }
217 }
218 return std::make_pair(std::string(), false);
219}
220
221std::string jau::os::DynamicLinker::getBaseName(const std::string& filename, const bool isBasename, const bool caseInsensitive) noexcept {
222 return DynamicLinker_processCanonicalNameImpl(true, filename, isBasename, caseInsensitive).first;
223}
224
225bool jau::os::DynamicLinker::isCanonicalName(const std::string& filename, const bool isBasename, const bool caseInsensitive) noexcept {
226 return DynamicLinker_processCanonicalNameImpl(false, filename, isBasename, caseInsensitive).second;
227}
228
229static std::vector<std::string> DynamicLinker_buildNames(const std::string& libName) noexcept {
230 std::vector<std::string> res;
231
232 const std::string libBaseName = jau::fs::basename(libName);
233 if( jau::os::DynamicLinker::isCanonicalName(libBaseName, true) ) {
234 // basename is canonical, so use the original with leading path
235 res.push_back(libName);
236 return res;
237 }
238
239 res.push_back( jau::os::DynamicLinker::getCanonicalName(libBaseName, false) );
240 if ( jau::os::is_darwin() ) {
241 // Plain library-base-name in Framework folder
242 res.push_back(libBaseName);
243 }
244 return res;
245}
246
247static void DynamicLinker_addBasenames(const std::string& cause, const std::vector<std::string>& baseNames, std::vector<std::string>& paths) noexcept {
248 for (const std::string& baseName : baseNames) {
249 DBG_PRINT("NativeLibrary.enumerateLibraryPaths: %s: '%s'", cause.c_str(), baseName.c_str());
250 paths.push_back(baseName);
251 }
252}
253static void DynamicLinker_addAbsPaths(const std::string& cause, const std::string& abs_path, const std::vector<std::string>& baseNames, std::vector<std::string>& paths) noexcept {
254 for (const std::string& baseName : baseNames) {
255 std::string p(abs_path); p.append("/").append(baseName);
256 DBG_PRINT("NativeLibrary.enumerateLibraryPaths: %s: '%s', from path '%s'", cause.c_str(), p.c_str(), abs_path.c_str());
257 paths.push_back(p);
258 }
259}
260static void DynamicLinker_addSysPaths(const std::string& cause, const std::vector<std::string>& baseNames, std::vector<std::string>& paths) noexcept {
261 // First add just the library names to use the OS's search algorithm
262 DynamicLinker_addBasenames(cause, baseNames, paths);
263
264 // Second add full path for each sys-folder to overcome SONAME mismatch (OS's search algorithm)
265 std::vector<std::string> lib_paths = jau::os::DynamicLinker::getSystemEnvLibraryPaths();
266 for(const std::string& p : lib_paths) {
267 DynamicLinker_addAbsPaths(cause, p, baseNames, paths);
268 }
269}
270#if 0
271static void DynamicLinker_addRelPaths(const std::string& cause, const std::string& path, const std::vector<std::string>& baseNames, std::vector<std::string>& paths) noexcept {
272 std::string abs_path;
273 jau::fs::file_stats path_stats(path);
274 if( path_stats.exists() ) {
275 DynamicLinker_addAbsPaths(cause, path_stats.path(), baseNames, paths);
276 }
277}
278#endif
279
280std::vector<std::string> jau::os::DynamicLinker::enumerateLibraryPaths(const std::string& libName,
281 bool searchSystemPath,
282 bool searchSystemPathFirst) noexcept {
283 DBG_PRINT("DynamicLibrary.enumerateLibraryPaths: libName '%s'", libName.c_str());
284 std::vector<std::string> paths;
285 if ( 0 == libName.size() ) {
286 return paths;
287 }
288
289 // Allow user's full path specification to override our building of paths
290 if ( jau::fs::isAbsolute(libName) ) {
291 paths.push_back(libName);
292 DBG_PRINT("NativeLibrary.enumerateLibraryPaths: done, absolute path found '%s'", libName.c_str());
293 return paths;
294 }
295
296 std::vector<std::string> baseNames = DynamicLinker_buildNames(libName);
297 DBG_PRINT("NativeLibrary.enumerateLibraryPaths: baseNames: %s", jau::to_string(baseNames).c_str());
298
299 if( searchSystemPath && searchSystemPathFirst ) {
300 DynamicLinker_addSysPaths("add.ssp_1st", baseNames, paths);
301
302 // Add probable Mac OS X-specific paths
303 if ( jau::os::is_darwin() ) {
304 // Add historical location
305 DynamicLinker_addAbsPaths("add.ssp_1st_macos_old", "/Library/Frameworks/" + libName + ".framework", baseNames, paths);
306 // Add current location
307 DynamicLinker_addAbsPaths("add.ssp_1st_macos_cur", "/System/Library/Frameworks/" + libName + ".framework", baseNames, paths);
308 }
309 }
310
311 // Add current working directory
312 {
313 std::string cwd = jau::fs::get_cwd();
314 DynamicLinker_addAbsPaths("add.cwd", cwd, baseNames, paths);
315
316 // Add current working directory + natives/os-arch/ + library names (for unpacked archives, if exists)
317 const std::string cwd_bin = cwd+"/natives/"+jau::os::get_os_and_arch();
318 jau::fs::file_stats fstats(cwd_bin);
319 if( fstats.exists() ) {
320 DynamicLinker_addAbsPaths("add.cwd.natives.os_arch", cwd_bin, baseNames, paths);
321 }
322 }
323
324 // Add user directory
325 {
327 if( user.isValid() ) {
328 DynamicLinker_addAbsPaths("add.home.std", user.homedir(), baseNames, paths);
329
330 // Add current home + bin/os-arch/ + library names (if exists)
331 const std::string home_bin = user.homedir()+"/bin/"+jau::os::get_os_and_arch();
332 jau::fs::file_stats fstats(home_bin);
333 if( fstats.exists() ) {
334 DynamicLinker_addAbsPaths("add.home.bin.os_arch", home_bin, baseNames, paths);
335 }
336 }
337 }
338
339 if( searchSystemPath && !searchSystemPathFirst ) {
340 DynamicLinker_addSysPaths("add.ssp_lst", baseNames, paths);
341
342 // Add probable Mac OS X-specific paths
343 if ( jau::os::is_darwin() ) {
344 // Add historical location
345 DynamicLinker_addAbsPaths("add.ssp_lst_macos_old", "/Library/Frameworks/" + libName + ".framework", baseNames, paths);
346 // Add current location
347 DynamicLinker_addAbsPaths("add.ssp_lst_macos_cur", "/System/Library/Frameworks/" + libName + ".framework", baseNames, paths);
348 }
349 }
350 DBG_PRINT("NativeLibrary.enumerateLibraryPaths: done: %s", jau::to_string(paths).c_str());
351 return paths;
352}
353
Singleton CpuInfo caching all jau::cpu information.
Definition cpuid.hpp:179
static const CpuInfo & get() noexcept
Returns reference to const singleton instance.
Definition cpuid.hpp:219
Platform agnostic representation of POSIX ::lstat() and ::stat() for a given pathname.
constexpr bool exists() const noexcept
Returns true if entity does not exist, exclusive bit.
static std::string getBaseName(const std::string &filename, const bool isBasename=false, const bool caseInsensitive=jau::os::is_windows()) noexcept
Returns the library basename, i.e.
static std::vector< std::string > enumerateLibraryPaths(const std::string &libName, bool searchSystemPath=false, bool searchSystemPathFirst=false) noexcept
Returns list of potential absolute library filenames.
static constexpr_cxx20 std::string getDefaultPrefix() noexcept
Returns the native library prefix, e.g.
static constexpr_cxx20 std::string getDefaultSuffix() noexcept
Returns the native library suffix including the dot, e.g.
static constexpr_cxx20 std::string getCanonicalName(const std::string &basename, const bool checkIsCanonical=true) noexcept
Returns canonical library name for this system from given library-basename, e.g.
static bool isCanonicalName(const std::string &filename, const bool isBasename=false, const bool caseInsensitive=jau::os::is_windows()) noexcept
Returns true if the given filename contains the canonical prefix and suffix, otherwise returns false.
static std::vector< std::string > getSystemEnvLibraryPaths()
Returns a list of system paths, from the getSystemEnvLibraryPathVarname() variable.
User account information of the underlying OS.
Definition user_info.hpp:42
const std::string & homedir() const noexcept
Definition user_info.hpp:81
bool isValid() const noexcept
Definition user_info.hpp:77
#define DBG_PRINT(...)
Use for environment-variable environment::DEBUG conditional debug messages, prefix '[elapsed_time] De...
Definition debug.hpp:52
constexpr bool is_little_endian() noexcept
Evaluates true if platform is running in little endian mode, i.e.
endian_t
Endian identifier, indicating endianess of all scalar types.
std::string to_string(const endian_t v) noexcept
Return std::string representation of the given endian.
std::string basename(const std::string_view &path) noexcept
Return stripped leading directory components from given path separated by /.
std::string get_cwd() noexcept
Return the current working directory or empty on failure.
Definition file_util.cpp:84
bool isAbsolute(const std::string_view &path) noexcept
Returns true if first character is / or - in case of Windows - \\.
std::string get_os_and_arch() noexcept
Returns this hosts's common name, see get_os_and_arch()
std::string get_platform_info() noexcept
bool get_rt_os_info(RuntimeOSInfo &info) noexcept
constexpr bool is_darwin() noexcept
Evaluates true if platform os_type::native contains os_type::Darwin.
os_type_t
OS type bits and unique IDs.
std::string get_os_and_arch(const os_type_t os, const jau::cpu::cpu_family_t cpu, const abi_type_t abi, const endian_t e) noexcept
Returns the common name for the given os_type, jau::cpu::cpu_family, abi_type and endian.
constexpr bool is_windows() noexcept
Evaluates true if platform os_type::native contains os_type::Windows.
abi_type_t get_abi_type() noexcept
@ Emscripten
WebAssembly with Unix/Posix suport bit (emscripten)
@ native
Identifier for native OS type, one of the above.
@ FreeBSD
FreeBSD bit, includes: unix.
@ Darwin
Darwin (Apple OSX and iOS) bit, includes: unix.
@ GenWasm
Generic WebAssembly bit.
@ Windows
Windows bit.
@ QnxNTO
QNX NTO (>= 6) bit, includes: unix.
@ Android
Android bit, includes: linux and unix.
@ Linux
Linux bit, contained by: android; includes: unix.
@ gnu_armhf
ARM GNU-EABI ARMHF -mfloat-abi=hard.
std::string format_string(const char *format,...)
Returns a string according to printf() formatting rules and variable number of arguments following th...
std::string toLower(const std::string &s) noexcept
cpu_family_t
Definition cpuid.hpp:50
@ x86_64
AMD/Intel 64-bit.
Definition cpuid.hpp:62
@ arm64
ARM 64bit.
Definition cpuid.hpp:57
@ x86_32
AMD/Intel 32-bit.
Definition cpuid.hpp:60
@ sparc32
SPARC 32bit.
Definition cpuid.hpp:72
@ wasm64
WebAssembly 64-bit.
Definition cpuid.hpp:89
@ superh32
Hitachi SuperH 32bit.
Definition cpuid.hpp:82
@ superh64
Hitachi SuperH 64bit.
Definition cpuid.hpp:84
@ arm32
ARM 32bit.
Definition cpuid.hpp:55
@ wasm32
WebAssembly 32-bit.
Definition cpuid.hpp:87
@ ppc64
Power PC 32bit.
Definition cpuid.hpp:69
@ mips64
Mips 64bit.
Definition cpuid.hpp:79
@ sparc64
SPARC 32bit.
Definition cpuid.hpp:74
@ mips32
Mips 32bit.
Definition cpuid.hpp:77
@ ppc32
Power PC 32bit.
Definition cpuid.hpp:67
Author: Sven Gothel sgothel@jausoft.com Copyright (c) 2024 Gothel Software e.K.
__pack(...): Produces MSVC, clang and gcc compatible lead-in and -out macros.
Definition backtrace.hpp:32
static std::vector< std::string > DynamicLinker_buildNames(const std::string &libName) noexcept
static void DynamicLinker_addAbsPaths(const std::string &cause, const std::string &abs_path, const std::vector< std::string > &baseNames, std::vector< std::string > &paths) noexcept
static void DynamicLinker_addBasenames(const std::string &cause, const std::vector< std::string > &baseNames, std::vector< std::string > &paths) noexcept
static void DynamicLinker_addSysPaths(const std::string &cause, const std::vector< std::string > &baseNames, std::vector< std::string > &paths) noexcept
static std::pair< std::string, bool > DynamicLinker_processCanonicalNameImpl(bool strip, const std::string &filename, const bool isBasename, const bool caseInsensitive=jau::os::is_windows()) noexcept
std::string to_string() noexcept