_hx_os_info.lua 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. -- credit : https://gist.github.com/soulik
  2. local function _hx_os_info()
  3. local raw_os_name, raw_arch_name = '', ''
  4. -- LuaJIT shortcut
  5. if jit and jit.os and jit.arch then
  6. raw_os_name = jit.os
  7. raw_arch_name = jit.arch
  8. else
  9. -- is popen supported?
  10. local popen_status, popen_result = pcall(io.popen, "")
  11. if popen_status then
  12. popen_result:close()
  13. -- Unix-based OS
  14. raw_os_name = io.popen('uname -s','r'):read('*l')
  15. raw_arch_name = io.popen('uname -m','r'):read('*l')
  16. else
  17. -- Windows
  18. local env_OS = os.getenv('OS')
  19. local env_ARCH = os.getenv('PROCESSOR_ARCHITECTURE')
  20. if env_OS and env_ARCH then
  21. raw_os_name, raw_arch_name = env_OS, env_ARCH
  22. end
  23. end
  24. end
  25. raw_os_name = (raw_os_name):lower()
  26. raw_arch_name = (raw_arch_name):lower()
  27. local os_patterns = {
  28. ['windows'] = 'Windows',
  29. ['linux'] = 'Linux',
  30. ['mac'] = 'Mac',
  31. ['darwin'] = 'Mac',
  32. ['^mingw'] = 'Windows',
  33. ['^cygwin'] = 'Windows',
  34. ['bsd$'] = 'BSD',
  35. ['SunOS'] = 'Solaris',
  36. }
  37. local arch_patterns = {
  38. ['^x86$'] = 'x86',
  39. ['i[%d]86'] = 'x86',
  40. ['amd64'] = 'x86_64',
  41. ['x86_64'] = 'x86_64',
  42. ['Power Macintosh'] = 'powerpc',
  43. ['^arm'] = 'arm',
  44. ['^mips'] = 'mips',
  45. }
  46. local os_name, arch_name = 'unknown', 'unknown'
  47. for pattern, name in pairs(os_patterns) do
  48. if raw_os_name:match(pattern) then
  49. os_name = name
  50. break
  51. end
  52. end
  53. for pattern, name in pairs(arch_patterns) do
  54. if raw_arch_name:match(pattern) then
  55. arch_name = name
  56. break
  57. end
  58. end
  59. return {os_name, arch_name}
  60. end