| 1 | // LAF Base Library |
| 2 | // Copyright (c) 2021 Igara Studio S.A. |
| 3 | // |
| 4 | // This file is released under the terms of the MIT license. |
| 5 | // Read LICENSE.txt for more information. |
| 6 | |
| 7 | #ifndef BASE_PLATFORM_H_INCLUDED |
| 8 | #define BASE_PLATFORM_H_INCLUDED |
| 9 | #pragma once |
| 10 | |
| 11 | #include "base/version.h" |
| 12 | |
| 13 | #include <cstdint> |
| 14 | |
| 15 | #if LAF_LINUX |
| 16 | #include <map> |
| 17 | #include <string> |
| 18 | #endif |
| 19 | |
| 20 | namespace base { |
| 21 | |
| 22 | struct Platform { |
| 23 | enum class OS { |
| 24 | Unknown, |
| 25 | Windows, |
| 26 | macOS, |
| 27 | Linux, |
| 28 | }; |
| 29 | |
| 30 | enum class Arch { |
| 31 | x86, // Windows 32-bit, Linux 32-bit |
| 32 | x64, // Windows 64-bit, Mac Intel |
| 33 | arm64, // Mac Apple Silicon (M1, M1 Pro), Raspberry Pi? |
| 34 | }; |
| 35 | |
| 36 | static constexpr OS os = |
| 37 | #if LAF_WINDOWS |
| 38 | OS::Windows |
| 39 | #elif LAF_MACOS |
| 40 | OS::macOS |
| 41 | #else |
| 42 | OS::Linux |
| 43 | #endif |
| 44 | ; |
| 45 | |
| 46 | static constexpr Arch arch = |
| 47 | #if defined(__arm64__) || defined(__aarch64__) |
| 48 | Arch::arm64 |
| 49 | #elif defined(__x86_64__) || defined(_WIN64) |
| 50 | Arch::x64 |
| 51 | #else |
| 52 | Arch::x86 |
| 53 | #endif |
| 54 | ; |
| 55 | |
| 56 | static_assert((arch == Arch::x86 && sizeof(void*) == 4) || |
| 57 | ((arch == Arch::x64 || |
| 58 | arch == Arch::arm64) && sizeof(void*) == 8), |
| 59 | "Invalid identification of CPU architecture" ); |
| 60 | |
| 61 | Version osVer; |
| 62 | |
| 63 | #if LAF_WINDOWS |
| 64 | enum class WindowsType { Unknown, Server, NT }; |
| 65 | WindowsType windowsType = WindowsType::Unknown; |
| 66 | Version servicePack; // Service pack version |
| 67 | bool isWow64 = false; // 32-bit version running on 64-bit |
| 68 | const char* wineVer = nullptr; // Are we running on Wine emulator? |
| 69 | #elif LAF_APPLE |
| 70 | // Nothing extra (macOS version on "osVer" field) |
| 71 | #elif LAF_LINUX |
| 72 | std::string distroName; // Linux distribution name |
| 73 | std::string distroVer; // Distribution version |
| 74 | #endif |
| 75 | |
| 76 | }; |
| 77 | |
| 78 | Platform get_platform(); |
| 79 | |
| 80 | #if LAF_WINDOWS |
| 81 | |
| 82 | bool is_wow64(); |
| 83 | const char* get_wine_version(); |
| 84 | |
| 85 | #elif LAF_MACOS |
| 86 | |
| 87 | Version get_osx_version(); |
| 88 | |
| 89 | #elif LAF_LINUX |
| 90 | |
| 91 | // Reads all the information of the current Linux distro from |
| 92 | // /etc/os-release or /etc/lsb-release (you specify the filename) |
| 93 | std::map<std::string, std::string> get_linux_release_info(const std::string& fn); |
| 94 | |
| 95 | #endif |
| 96 | |
| 97 | } // namespace base |
| 98 | |
| 99 | #endif |
| 100 | |