Platform.cpp 2.0 KB

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