1 #include "Platform.hpp" 2 3 #include <boost/log/trivial.hpp> 4 #include <boost/filesystem/operations.hpp> 5 6 namespace Slic3r { 7 8 static auto s_platform = Platform::Uninitialized; 9 static auto s_platform_flavor = PlatformFlavor::Uninitialized; 10 detect_platform()11void detect_platform() 12 { 13 #if defined(_WIN32) 14 BOOST_LOG_TRIVIAL(info) << "Platform: Windows"; 15 s_platform = Platform::Windows; 16 s_platform_flavor = PlatformFlavor::Generic; 17 #elif defined(__APPLE__) 18 BOOST_LOG_TRIVIAL(info) << "Platform: OSX"; 19 s_platform = Platform::OSX; 20 s_platform_flavor = PlatformFlavor::Generic; 21 #elif defined(__linux__) 22 BOOST_LOG_TRIVIAL(info) << "Platform: Linux"; 23 s_platform = Platform::Linux; 24 s_platform_flavor = PlatformFlavor::GenericLinux; 25 // Test for Chromium. 26 { 27 FILE *f = ::fopen("/proc/version", "rt"); 28 if (f) { 29 char buf[4096]; 30 // Read the 1st line. 31 if (::fgets(buf, 4096, f)) { 32 if (strstr(buf, "Chromium OS") != nullptr) { 33 s_platform_flavor = PlatformFlavor::LinuxOnChromium; 34 BOOST_LOG_TRIVIAL(info) << "Platform flavor: LinuxOnChromium"; 35 } else if (strstr(buf, "microsoft") != nullptr || strstr(buf, "Microsoft") != nullptr) { 36 if (boost::filesystem::exists("/run/WSL") && getenv("WSL_INTEROP") != nullptr) { 37 BOOST_LOG_TRIVIAL(info) << "Platform flavor: WSL2"; 38 s_platform_flavor = PlatformFlavor::WSL2; 39 } else { 40 BOOST_LOG_TRIVIAL(info) << "Platform flavor: WSL"; 41 s_platform_flavor = PlatformFlavor::WSL; 42 } 43 } 44 } 45 ::fclose(f); 46 } 47 } 48 #elif defined(__OpenBSD__) 49 BOOST_LOG_TRIVIAL(info) << "Platform: OpenBSD"; 50 s_platform = Platform::BSDUnix; 51 s_platform_flavor = PlatformFlavor::OpenBSD; 52 #elif defined(__FreeBSD__) 53 BOOST_LOG_TRIVIAL(info) << "Platform: FreeBSD"; 54 s_platform = Platform::BSDUnix; 55 s_platform_flavor = PlatformFlavor::FreeBSD; 56 #else 57 // This should not happen. 58 BOOST_LOG_TRIVIAL(info) << "Platform: Unknown"; 59 static_assert(false, "Unknown platform detected"); 60 s_platform = Platform::Unknown; 61 s_platform_flavor = PlatformFlavor::Unknown; 62 #endif 63 } 64 platform()65Platform platform() 66 { 67 return s_platform; 68 } 69 platform_flavor()70PlatformFlavor platform_flavor() 71 { 72 return s_platform_flavor; 73 } 74 75 } // namespace Slic3r 76