jaulib v1.3.6
Jau Support Library (C++, Java, ..)
Loading...
Searching...
No Matches
os_support.hpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2020 Gothel Software e.K.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *
23 */
24
25#ifndef JAU_OS_SUPPORT_HPP_
26#define JAU_OS_SUPPORT_HPP_
27
28#include <cstdint>
29
30#include <jau/byte_util.hpp>
31#include <jau/int_types.hpp>
32#include "jau/cpp_lang_util.hpp"
33#include "jau/enum_util.hpp"
34#include "jau/cpuid.hpp"
35
36namespace jau::os {
37
38 /** @defgroup OSSup OS Support
39 * OS Support Functionality
40 *
41 * Available predefined macros denoting the [Operating Systems](https://sourceforge.net/p/predef/wiki/OperatingSystems/)
42 * - `__FreeBSD__` : FreeBSD
43 * - `__linux__` : Linux, w/o Android: `__linux__ && !__ANDROID__`
44 * - `__ANDROID__` : Android, implies `__linux__`
45 * - `_WIN32` : Windows
46 * - `_WIN64` : Windows 64 bit, implies `_WIN32`
47 * - `__APPLE__` : Darwin, i.e. MacOS or iOS
48 * - `__ros__` : Akaros
49 * - `__native_client__`: NaCL
50 * - `__asmjs__` : AsmJS
51 * - `__EMSCRIPTEN__` : emscripten for asm.js and WebAssembly
52 * - `__Fuchsia__` : Fuchsia
53 *
54 * Further infos:
55 * - [Unix standards](https://sourceforge.net/p/predef/wiki/Standards/)
56 * - [GNU glibc](https://sourceforge.net/p/predef/wiki/Libraries/)
57 * - [glibc 1.3.4 Feature Test Macros](https://www.gnu.org/software/libc/manual/html_node/Feature-Test-Macros.html)
58 * - [Architectures](https://sourceforge.net/p/predef/wiki/Architectures/)
59 *
60 * @{
61 */
62
63 namespace impl {
64 constexpr uint32_t get_host_os_id() noexcept {
65 #if defined(__EMSCRIPTEN__)
66 #define JAU_OS_TYPE_UNIX 1
67 #define JAU_OS_TYPE_WASM 1
68 #if defined(__EMSCRIPTEN_PTHREADS__)
69 #define JAU_OS_HAS_PTHREAD 1
70 #else
71 #define JAU_OS_HAS_PTHREAD 0
72 #endif
73 return 0b00000001000000000000000000000001U; // Emscripten
74 #elif defined(__QNXNTO__)
75 #define JAU_OS_TYPE_UNIX 1
76 #define JAU_OS_TYPE_QNXNTO 1
77 #define JAU_OS_HAS_PTHREAD 1
78 return 0b00000000000000000001000000000001U; // QnxNTO
79 #elif defined(__APPLE__) && defined(__MACH__)
80 #define JAU_OS_TYPE_UNIX 1
81 #define JAU_OS_TYPE_DARWIN 1
82 #define JAU_OS_HAS_PTHREAD 1
83 return 0b00000000000000000000100000000001U; // Darwin
84 #elif defined(__FreeBSD__)
85 #define JAU_OS_TYPE_UNIX 1
86 #define JAU_OS_TYPE_FREEBSD 1
87 #define JAU_OS_HAS_PTHREAD 1
88 return 0b00000000000000000000010000000001U; // FreeBSD
89 #elif defined(__ANDROID__)
90 #define JAU_OS_TYPE_UNIX 1
91 #define JAU_OS_TYPE_ANDROID 1
92 #define JAU_OS_HAS_PTHREAD 1
93 return 0b00000000000000000000001100000001U; // Android
94 #elif defined(__linux__)
95 #define JAU_OS_TYPE_UNIX 1
96 #define JAU_OS_TYPE_LINUX 1
97 #define JAU_OS_HAS_PTHREAD 1
98 return 0b00000000000000000000000100000001U; // Linux
99 #elif defined(_WIN32)
100 #define JAU_OS_TYPE_WINDOWS 1
101 #define JAU_OS_HAS_PTHREAD 1
102 return 0b00000000000000000000000000000010U; // Windows
103 #else
104 #define JAU_OS_TYPE_UNIX 1
105 #define JAU_OS_HAS_PTHREAD 1
106 return 0b00000000000000000000000000000001U; // Unix
107 #endif
108 }
109 }
110
111 using namespace jau::enums;
112
113 /** OS type bits and unique IDs */
114 enum class os_type_t : uint32_t {
115 /** Unix bit, contained by: linux, android, freebsd, darwin. */
116 Unix = 0b00000000000000000000000000000001U,
117 /** Windows bit */
118 Windows = 0b00000000000000000000000000000010U,
119 /** Linux bit, contained by: android; includes: unix */
120 Linux = 0b00000000000000000000000100000001U,
121 /** Android bit, includes: linux and unix */
122 Android = 0b00000000000000000000001100000001U,
123 /** FreeBSD bit, includes: unix */
124 FreeBSD = 0b00000000000000000000010000000001U,
125 /** Darwin (Apple OSX and iOS) bit, includes: unix */
126 Darwin = 0b00000000000000000000100000000001U,
127 /** QNX NTO (>= 6) bit, includes: unix */
128 QnxNTO = 0b00000000000000000001000000000001U,
129 /** Generic WebAssembly bit */
130 GenWasm = 0b00000001000000000000000000000000U,
131 /** WebAssembly with Unix/Posix suport bit (emscripten) */
132 Emscripten = 0b00000001000000000000000000000001U,
133 /** Identifier for native OS type, one of the above. */
135 };
137
138 /**
139 * Evaluates `true` if the given \ref os_type is defined,
140 * i.e. `Unix`, `Windows`, `Linux`, `Android`, ...
141 */
142 constexpr bool is_defined_os_type(const os_type_t v) noexcept {
143 switch(v) {
144 case os_type_t::Unix:
145 [[fallthrough]];
147 [[fallthrough]];
148 case os_type_t::Linux:
149 [[fallthrough]];
151 [[fallthrough]];
153 [[fallthrough]];
155 [[fallthrough]];
157 [[fallthrough]];
159 return true;
161 return true;
162 default:
163 return false;
164 }
165 }
166
167 // one static_assert is sufficient for whole compilation unit
168 static_assert( is_defined_os_type(os_type_t::native) ); // Enhance os_type to match your platform!
169
170 /** Evaluates `true` if platform os_type::native contains os_type::Unix */
171 constexpr bool is_unix() noexcept { return is_set(os_type_t::native, os_type_t::Unix); }
172
173 /** Evaluates `true` if platform os_type::native contains os_type::Windows */
174 constexpr bool is_windows() noexcept { return is_set(os_type_t::native, os_type_t::Windows); }
175
176 /** Evaluates `true` if platform os_type::native contains os_type::Linux */
177 constexpr bool is_linux() noexcept { return is_set(os_type_t::native, os_type_t::Linux); }
178
179 /** Evaluates `true` if platform os_type::native contains os_type::Android */
180 constexpr bool is_android() noexcept { return is_set(os_type_t::native, os_type_t::Android); }
181
182 /** Evaluates `true` if platform os_type::native contains os_type::FreeBSD */
183 constexpr bool is_freebsd() noexcept { return is_set(os_type_t::native, os_type_t::FreeBSD); }
184
185 /** Evaluates `true` if platform os_type::native contains os_type::Darwin */
186 constexpr bool is_darwin() noexcept { return is_set(os_type_t::native, os_type_t::Darwin); }
187
188 /** Evaluates `true` if platform os_type::native contains os_type::QnxNTO */
189 constexpr bool is_qnxnto() noexcept { return is_set(os_type_t::native, os_type_t::QnxNTO); }
190
191 /** Evaluates `true` if platform os_type::native contains os_type::GenWasm */
192 constexpr bool is_generic_wasm() noexcept { return is_set(os_type_t::native, os_type_t::GenWasm); }
193
194 /** Evaluates `true` if platform os_type::native contains os_type::Emscripten */
195 constexpr bool is_emscripten() noexcept { return is_set(os_type_t::native, os_type_t::Emscripten); }
196
197 /** Evaluates `true` if platform supports posix compatible threading. */
198 constexpr bool has_pthread() noexcept {
199 #if JAU_OS_HAS_PTHREAD
200 return true;
201 #else
202 return false;
203 #endif
204 }
205
207 std::string sysname;
208 std::string nodename;
209 std::string release;
210 std::string version;
211 std::string machine;
212 std::string domainname;
213 std::string to_string() noexcept {
214 std::string sb = sysname+" "+release+", "+machine;
215 if( nodename.length() > 0 ) {
216 sb.append(", node ").append(nodename);
217 }
218 if( domainname.length() > 0 ) {
219 sb.append(", domain ").append(domainname);
220 }
221 sb.append(", ").append(version);
222 return sb;
223 }
224 };
225 bool get_rt_os_info(RuntimeOSInfo& info) noexcept;
226
227 enum class abi_type_t : uint16_t {
228 generic = 0x00,
229 /** ARM GNU-EABI ARMEL -mfloat-abi=softfp */
230 gnu_armel = 0x01,
231 /** ARM GNU-EABI ARMHF -mfloat-abi=hard */
232 gnu_armhf = 0x02,
233 /** ARM EABI AARCH64 (64bit) */
234 aarch64 = 0x03,
235 /** WASM Generic (32bit) */
237 /** WASM Emscripten (32bit) */
239 /** WASM Generic (64bit) */
241 /** WASM Emscripten (64bit) */
243 };
246 return abi_type_t::aarch64;
247 } else if ( jau::cpu::cpu_family_t::arm32 == cpu ) {
248 return abi_type_t::gnu_armhf; // FIXME?
249 } else if ( jau::cpu::cpu_family_t::wasm32 == cpu ) {
250 #if defined(__EMSCRIPTEN__)
252 #else
254 #endif
255 } else if ( jau::cpu::cpu_family_t::wasm64 == cpu ) {
256 #if defined(__EMSCRIPTEN__)
258 #else
260 #endif
261 }
262 return abi_type_t::generic;
263 }
264 inline abi_type_t get_abi_type() noexcept {
265 return get_abi_type( jau::cpu::CpuInfo::get().family );
266 }
268
269 /**
270 * Returns the common name for the given
271 * os_type, jau::cpu::cpu_family, abi_type and endian.
272 *
273 * An excerpt of supported <code>os.and.arch</code> strings:
274 * <ul>
275 * <li>android-armv6</li>
276 * <li>android-aarch64</li>
277 * <li>android-x86</li>
278 * <li>linux-armv6</li>
279 * <li>linux-armv6hf</li>
280 * <li>linux-i586</li>
281 * <li>linux-ppc</li>
282 * <li>linux-mips</li>
283 * <li>linux-mipsel</li>
284 * <li>linux-superh</li>
285 * <li>linux-sparc</li>
286 * <li>linux-aarch64</li>
287 * <li>linux-amd64</li>
288 * <li>linux-ppc64</li>
289 * <li>linux-ppc64le</li>
290 * <li>linux-mips64</li>
291 * <li>linux-ia64</li>
292 * <li>linux-sparcv9</li>
293 * <li>linux-risc2.0</li>
294 * <li>freebsd-i586</li>
295 * <li>freebsd-amd64</li>
296 * <li>darwin-universal</li>
297 * <li>windows-amd64</li>
298 * <li>windows-i586</li>
299 * </ul>
300 * @return The <i>os.and.arch</i> value.
301 */
302 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;
303
304 /** Returns this hosts's common name, see get_os_and_arch() */
305 inline std::string get_os_and_arch() noexcept {
307 }
308
309 /** Returns the OS's path separator character, e.g. `;` for Windows and `:` for Unix (rest of the world) */
310 constexpr char path_separator_char() noexcept {
311 if constexpr (jau::os::is_windows()) {
312 return ';';
313 } else {
314 return ':';
315 }
316 }
317 /** Returns the OS's path separator as a string, e.g. `;` for Windows and `:` for Unix (rest of the world) */
318 constexpr_cxx20 std::string path_separator() noexcept {
319 return std::string(1, path_separator_char());
320 }
321
322 /** Returns the OS's path separator character, e.g. `\\` for Windows and `/` for Unix (rest of the world) */
323 constexpr char dir_separator_char() noexcept {
324 if constexpr (jau::os::is_windows()) {
325 return '\\';
326 } else {
327 return '/';
328 }
329 }
330
331 /** Returns the OS's path separator as a string, e.g. `\\` for Windows and `/` for Unix (rest of the world) */
332 constexpr_cxx20 std::string dir_separator() noexcept {
333 return std::string(1, dir_separator_char());
334 }
335
336 std::string get_platform_info(std::string& sb) noexcept;
337 inline std::string get_platform_info() noexcept {
338 std::string sb; get_platform_info(sb); return sb;
339 }
340
341 /**@}*/
342
343} // namespace jau::os
344
345#endif /* JAU_OS_SUPPORT_HPP_ */
static const CpuInfo & get() noexcept
Returns reference to const singleton instance.
Definition cpuid.hpp:219
endian_t
Endian identifier, indicating endianess of all scalar types.
@ native
Identifier for native platform type, one of the above.
#define JAU_MAKE_ENUM_STRING(type,...)
#define constexpr_cxx20
constexpr qualifier replacement for C++20 constexpr.
#define JAU_MAKE_BITFIELD_ENUM_STRING(type,...)
constexpr bool is_set(const E mask, const E bits) noexcept
constexpr_cxx20 std::string dir_separator() noexcept
Returns the OS's path separator as a string, e.g.
std::string get_os_and_arch() noexcept
Returns this hosts's common name, see get_os_and_arch()
constexpr bool is_defined_os_type(const os_type_t v) noexcept
Evaluates true if the given os_type is defined, i.e.
std::string get_platform_info() noexcept
constexpr bool is_freebsd() noexcept
Evaluates true if platform os_type::native contains os_type::FreeBSD.
bool get_rt_os_info(RuntimeOSInfo &info) noexcept
constexpr char path_separator_char() noexcept
Returns the OS's path separator character, e.g.
constexpr bool is_darwin() noexcept
Evaluates true if platform os_type::native contains os_type::Darwin.
constexpr bool is_emscripten() noexcept
Evaluates true if platform os_type::native contains os_type::Emscripten.
constexpr bool has_pthread() noexcept
Evaluates true if platform supports posix compatible threading.
os_type_t
OS type bits and unique IDs.
constexpr bool is_unix() noexcept
Evaluates true if platform os_type::native contains os_type::Unix.
constexpr bool is_linux() noexcept
Evaluates true if platform os_type::native contains os_type::Linux.
constexpr_cxx20 std::string path_separator() noexcept
Returns the OS's path separator as a string, e.g.
constexpr bool is_qnxnto() noexcept
Evaluates true if platform os_type::native contains os_type::QnxNTO.
constexpr bool is_android() noexcept
Evaluates true if platform os_type::native contains os_type::Android.
constexpr bool is_windows() noexcept
Evaluates true if platform os_type::native contains os_type::Windows.
constexpr char dir_separator_char() noexcept
Returns the OS's path separator character, e.g.
abi_type_t get_abi_type() noexcept
constexpr bool is_generic_wasm() noexcept
Evaluates true if platform os_type::native contains os_type::GenWasm.
@ Emscripten
WebAssembly with Unix/Posix suport bit (emscripten)
@ Unix
Unix bit, contained by: linux, android, freebsd, darwin.
@ 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.
@ wasm32_gen
WASM Generic (32bit)
@ wasm64_gen
WASM Generic (64bit)
@ gnu_armel
ARM GNU-EABI ARMEL -mfloat-abi=softfp.
@ wasm32_ems
WASM Emscripten (32bit)
@ wasm64_ems
WASM Emscripten (64bit)
@ aarch64
ARM EABI AARCH64 (64bit)
@ gnu_armhf
ARM GNU-EABI ARMHF -mfloat-abi=hard.
cpu_family_t
Definition cpuid.hpp:50
@ arm64
ARM 64bit.
Definition cpuid.hpp:57
@ wasm64
WebAssembly 64-bit.
Definition cpuid.hpp:89
@ arm32
ARM 32bit.
Definition cpuid.hpp:55
@ wasm32
WebAssembly 32-bit.
Definition cpuid.hpp:87
Author: Sven Gothel sgothel@jausoft.com Copyright Gothel Software e.K.
Definition enum_util.hpp:65
constexpr uint32_t get_host_os_id() noexcept
Author: Sven Gothel sgothel@jausoft.com Copyright (c) 2024 Gothel Software e.K.
std::string to_string() noexcept