|
5 | 5 | */ |
6 | 6 |
|
7 | 7 | #include "premake.h" |
| 8 | +#include <string.h> |
| 9 | + |
| 10 | +#if !PLATFORM_WINDOWS |
| 11 | +#include <sys/utsname.h> |
| 12 | +#endif |
| 13 | + |
| 14 | +#if PLATFORM_MACOSX |
| 15 | +#include <sys/sysctl.h> |
| 16 | +#endif |
| 17 | + |
| 18 | + |
| 19 | +#if PLATFORM_WINDOWS |
| 20 | + |
| 21 | +static const char* os_hostarch_detect(void) |
| 22 | +{ |
| 23 | + /* Try IsWow64Process2 first (Windows 10 1511+): gives true native machine type |
| 24 | + * even when running a 32-bit on a 64-bit host, or a 64-bit on a ARM64 host. */ |
| 25 | + typedef BOOL (WINAPI *PFN_ISWOW64PROCESS2)(HANDLE, USHORT*, USHORT*); |
| 26 | + PFN_ISWOW64PROCESS2 fnIsWow64Process2 = |
| 27 | + (PFN_ISWOW64PROCESS2)(void*)GetProcAddress( |
| 28 | + GetModuleHandleA("kernel32"), "IsWow64Process2"); |
| 29 | + |
| 30 | + if (fnIsWow64Process2) |
| 31 | + { |
| 32 | + USHORT processMachine = 0; |
| 33 | + USHORT nativeMachine = 0; |
| 34 | + if (fnIsWow64Process2(GetCurrentProcess(), &processMachine, &nativeMachine)) |
| 35 | + { |
| 36 | + switch (nativeMachine) |
| 37 | + { |
| 38 | + case 0x8664: return "x86_64"; /* IMAGE_FILE_MACHINE_AMD64 */ |
| 39 | + case 0x014c: return "x86"; /* IMAGE_FILE_MACHINE_I386 */ |
| 40 | + case 0xAA64: return "ARM64"; /* IMAGE_FILE_MACHINE_ARM64 */ |
| 41 | + case 0x01c4: return "ARM"; /* IMAGE_FILE_MACHINE_ARMNT */ |
| 42 | + default: break; |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + // Fall back to GetNativeSystemInfo (available since Windows XP) |
| 48 | + { |
| 49 | + SYSTEM_INFO si; |
| 50 | + GetNativeSystemInfo(&si); |
| 51 | + switch (si.wProcessorArchitecture) |
| 52 | + { |
| 53 | + case 9: return "x86_64"; // PROCESSOR_ARCHITECTURE_AMD64 |
| 54 | + case 0: return "x86"; // PROCESSOR_ARCHITECTURE_INTEL |
| 55 | + case 12: return "ARM64"; // PROCESSOR_ARCHITECTURE_ARM64 |
| 56 | + case 5: return "ARM"; // PROCESSOR_ARCHITECTURE_ARM |
| 57 | + default: break; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + return PLATFORM_ARCHITECTURE; |
| 62 | +} |
| 63 | + |
| 64 | +#elif PLATFORM_MACOSX |
| 65 | + |
| 66 | +static const char* os_hostarch_detect(void) |
| 67 | +{ |
| 68 | + // hw.optional.arm64 == 1 on Apple Silicon (including Rosetta 2 processes) |
| 69 | + int arm64 = 0; |
| 70 | + size_t size = sizeof(arm64); |
| 71 | + if (sysctlbyname("hw.optional.arm64", &arm64, &size, NULL, 0) == 0 && arm64) |
| 72 | + return "ARM64"; |
| 73 | + |
| 74 | + // hw.machine reports the native CPU type string |
| 75 | + char machine[64]; |
| 76 | + size = sizeof(machine) - 1; |
| 77 | + if (sysctlbyname("hw.machine", machine, &size, NULL, 0) == 0) |
| 78 | + { |
| 79 | + machine[size] = '\0'; |
| 80 | + if (strncmp(machine, "arm64", 5) == 0) return "ARM64"; |
| 81 | + if (strncmp(machine, "x86_64", 6) == 0) return "x86_64"; |
| 82 | + if (strncmp(machine, "i386", 4) == 0) return "x86"; |
| 83 | + // not sure whether the following are really working, but try to detect them just in case |
| 84 | + if (strncmp(machine, "ppc64", 5) == 0) return "ppc64"; |
| 85 | + if (strncmp(machine, "ppc", 3) == 0) return "ppc"; |
| 86 | + } |
| 87 | + |
| 88 | + /* uname reports the architecture of the current process, which may be running under |
| 89 | + * Rosetta 2 translation on Apple Silicon, but is better than nothing. */ |
| 90 | + { |
| 91 | + struct utsname u; |
| 92 | + if (uname(&u) == 0) |
| 93 | + { |
| 94 | + if (strcmp(u.machine, "arm64") == 0) return "ARM64"; |
| 95 | + if (strcmp(u.machine, "x86_64") == 0) return "x86_64"; |
| 96 | + if (strcmp(u.machine, "i386") == 0) return "x86"; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + return PLATFORM_ARCHITECTURE; |
| 101 | +} |
| 102 | + |
| 103 | +#else // All other POSIX platforms (Linux, BSD, Haiku, Solaris, AIX, …) |
| 104 | + |
| 105 | +static const char* os_hostarch_detect(void) |
| 106 | +{ |
| 107 | + struct utsname u; |
| 108 | + if (uname(&u) == 0) |
| 109 | + { |
| 110 | + const char* m = u.machine; |
| 111 | + if (strcmp(m, "x86_64") == 0 || strcmp(m, "amd64") == 0) return "x86_64"; |
| 112 | + if (strcmp(m, "i386") == 0 || strcmp(m, "i686") == 0) return "x86"; |
| 113 | + if (strcmp(m, "aarch64") == 0 || strcmp(m, "arm64") == 0) return "ARM64"; |
| 114 | + if (strcmp(m, "arm") == 0 || strncmp(m, "armv", 4) == 0) return "ARM"; |
| 115 | + if (strcmp(m, "riscv64") == 0) return "RISCV64"; |
| 116 | + if (strcmp(m, "loongarch64") == 0) return "loongarch64"; |
| 117 | + if (strcmp(m, "e2k") == 0) return "e2k"; |
| 118 | + if (strcmp(m, "ppc64le") == 0 || strcmp(m, "ppc64") == 0) return "ppc64"; |
| 119 | + if (strcmp(m, "ppc") == 0) return "ppc"; |
| 120 | + if (strcmp(m, "mips64el") == 0) return "mips64el"; |
| 121 | + } |
| 122 | + |
| 123 | + return PLATFORM_ARCHITECTURE; |
| 124 | +} |
| 125 | + |
| 126 | +#endif |
| 127 | + |
8 | 128 |
|
9 | 129 | int os_hostarch(lua_State* L) |
10 | 130 | { |
11 | | - lua_pushstring(L, PLATFORM_ARCHITECTURE); |
| 131 | + lua_pushstring(L, os_hostarch_detect()); |
12 | 132 | return 1; |
13 | 133 | } |
0 commit comments