Skip to content

Commit d4622b8

Browse files
committed
os.is64bit() support on POSIX platform
Use iname() to get host architecture name and compare it against a known 64-bit architecture name list.
1 parent 63f1c86 commit d4622b8

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

src/host/os_is64bit.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,19 @@
55
*/
66

77
#include "premake.h"
8+
#if defined(PLATFORM_MACOSX) || defined(PLATFORM_BSD) || defined(PLATFORM_LINUX) || defined(PLATFORM_SOLARIS) || defined(PLATFORM_HURD) || defined(PLATFORM_HAIKU) || defined(PLATFORM_COSMO)
9+
10+
#if !defined(HAVE_UNAME)
11+
#define HAVE_UNAME 1
12+
#endif
13+
#include <sys/utsname.h>
14+
#include <string.h>
15+
16+
#else
17+
18+
#define HAVE_UNAME 0
19+
20+
#endif
821

922
int os_is64bit(lua_State* L)
1023
{
@@ -24,6 +37,30 @@ int os_is64bit(lua_State* L)
2437
}
2538
}
2639
#endif
40+
#if HAVE_UNAME
41+
struct utsname data;
42+
if (uname(&data) >= 0)
43+
{
44+
// Non-exhaustive list of 64bit architectures reported by uname -m
45+
static const char *knownArchitectures[] = {
46+
"x86_64", "adm64",
47+
"arm64", "aarch64",
48+
"ppc64", "ppc64le",
49+
"s390x",
50+
"mips64", "mips64el",
51+
"riscv64",
52+
"longarch64"
53+
};
54+
for (size_t a = 0; a < (sizeof(knownArchitectures) / sizeof(const char *)); ++a)
55+
{
56+
if (strcmp(data.machine, knownArchitectures[a]) == 0)
57+
{
58+
lua_pushboolean(L, 1);
59+
return 1;
60+
}
61+
}
62+
}
63+
#endif
2764

2865
lua_pushboolean(L, 0);
2966
return 1;

0 commit comments

Comments
 (0)