Skip to content

Commit 03e42b9

Browse files
committed
fix os.hostarch to return the actual architecture of running system
1 parent c8c60f8 commit 03e42b9

2 files changed

Lines changed: 121 additions & 5 deletions

File tree

src/host/os_hostarch.c

Lines changed: 121 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,129 @@
55
*/
66

77
#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+
8128

9129
int os_hostarch(lua_State* L)
10130
{
11-
lua_pushstring(L, PLATFORM_ARCHITECTURE);
131+
lua_pushstring(L, os_hostarch_detect());
12132
return 1;
13133
}

website/docs/os/os.hostarch.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ None.
1212

1313
An architecture identifier; see [architecture()](architecture.md) for a complete list of identifiers.
1414

15-
:::warning
16-
Currently, this function actually returns the architecture of the system that Premake was compiled for. This means that if you run the Win32 version of Premake on a 64-bit Windows system, this function will return 'x86' instead of 'x86_64'.
17-
:::
18-
1915
### Availability ###
2016

2117
Premake 5.0.0 beta 3 or later.

0 commit comments

Comments
 (0)