From 3f974a6e0ddea0111c0e4f231747ee7742c6a97c Mon Sep 17 00:00:00 2001 From: Joao Matos Date: Sat, 7 Sep 2024 11:21:08 +0100 Subject: [PATCH] Add a `os.hostarch()` function to get the host system architecture. --- src/host/os_hostarch.c | 26 ++++++++++++++++++++++ src/host/premake.c | 1 + src/host/premake.h | 2 +- tests/base/test_os.lua | 10 +++++++++ website/docs/Lua-Library-Additions.md | 1 + website/docs/os.hostarch.md | 31 +++++++++++++++++++++++++++ website/sidebars.js | 1 + 7 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 src/host/os_hostarch.c create mode 100644 website/docs/os.hostarch.md diff --git a/src/host/os_hostarch.c b/src/host/os_hostarch.c new file mode 100644 index 0000000000..1454e0a2f8 --- /dev/null +++ b/src/host/os_hostarch.c @@ -0,0 +1,26 @@ +/** + * \file os_hostarch.c + * \brief Get the architecture for the current host OS we're executing on. + * \author Copyright (c) 2014-2024 Jason Perkins and the Premake project + */ + +#include "premake.h" + +#if defined(__x86_64__) || defined(__x86_64) || defined(__amd64__) || defined(__amd64) || \ + defined(_M_X64) || defined(_M_AMD64) +#define PLATFORM_ARCHITECTURE "x86_64" +#elif defined(i386) || defined(__i386) || defined(__i386__) || defined(__i486__) || defined(__i586__) || \ + defined(__i686__) || defined(_M_IX86)|| defined(__X86__) || defined(_X86_) +#define PLATFORM_ARCHITECTURE "x86" +#elif defined(__aarch64__) || defined(_M_ARM64) || defined(__AARCH64EL__) || defined(__arm64) +#define PLATFORM_ARCHITECTURE "ARM64" +#elif defined(__arm__) || defined(__thumb__) || defined(__TARGET_ARCH_ARM) || defined(__TARGET_ARCH_THUMB) || \ + defined(__ARM) || defined(_M_ARM) || defined(_M_ARM_T) || defined(__ARM_ARCH) +#define PLATFORM_ARCHITECTURE "ARM" +#endif + +int os_hostarch(lua_State* L) +{ + lua_pushstring(L, PLATFORM_ARCHITECTURE); + return 1; +} diff --git a/src/host/premake.c b/src/host/premake.c index e304846223..9b3abba637 100644 --- a/src/host/premake.c +++ b/src/host/premake.c @@ -72,6 +72,7 @@ static const luaL_Reg os_functions[] = { { "listWindowsRegistry", os_listWindowsRegistry }, { "getversion", os_getversion }, { "host", os_host }, + { "hostarch", os_hostarch }, { "isfile", os_isfile }, { "islink", os_islink }, { "locate", os_locate }, diff --git a/src/host/premake.h b/src/host/premake.h index 8f49827379..b6b83878cb 100644 --- a/src/host/premake.h +++ b/src/host/premake.h @@ -45,7 +45,6 @@ #define PLATFORM_POSIX (PLATFORM_LINUX || PLATFORM_BSD || PLATFORM_MACOSX || PLATFORM_SOLARIS || PLATFORM_HAIKU) - /* Pull in platform-specific headers required by built-in functions */ #if PLATFORM_WINDOWS #define WIN32_LEAN_AND_MEAN @@ -127,6 +126,7 @@ int os_getWindowsRegistry(lua_State* L); int os_listWindowsRegistry(lua_State* L); int os_getversion(lua_State* L); int os_host(lua_State* L); +int os_hostarch(lua_State* L); int os_is64bit(lua_State* L); int os_isdir(lua_State* L); int os_isfile(lua_State* L); diff --git a/tests/base/test_os.lua b/tests/base/test_os.lua index dcb027a780..0225470adf 100644 --- a/tests/base/test_os.lua +++ b/tests/base/test_os.lua @@ -498,3 +498,13 @@ local numcpus = os.getnumcpus() test.istrue(numcpus > 0) end + + +-- +-- os.hostarch() tests. +-- + + function suite.hostarch() + local arch = os.hostarch() + test.istrue(string.len(arch) > 0) + end diff --git a/website/docs/Lua-Library-Additions.md b/website/docs/Lua-Library-Additions.md index c2c981d85e..2a6ee1b83e 100644 --- a/website/docs/Lua-Library-Additions.md +++ b/website/docs/Lua-Library-Additions.md @@ -39,6 +39,7 @@ * [os.getpass](os.getpass.md) * [os.getversion](os.getversion.md) * [os.host](os.host.md) +* [os.hostarch](os.hostarch.md) * [os.is](os.is.md) * [os.is64bit](os.is64bit.md) * [os.isdir](os.isdir.md) diff --git a/website/docs/os.hostarch.md b/website/docs/os.hostarch.md new file mode 100644 index 0000000000..2ce3728e4a --- /dev/null +++ b/website/docs/os.hostarch.md @@ -0,0 +1,31 @@ +Identify the architecture for the currently executing operating system. + +```lua +id = os.hostarch() +``` + +### Parameters ### + +None. + +### Return Value ### + +An architecture identifier; see [architecture()](architecture.md) for a complete list of identifiers. + +Note that this function returns the architecture for the OS that Premake is currently running on, which is not necessarily the same as the architecture that Premake is generating files for. + +### Availability ### + +Premake 5.0.0 beta 3 or later. + +### Examples ### + +```lua +if os.hostarch() == "x86_64" then + -- do something x64-specific +end +``` + +### See Also ### + +* [architecture](architecture.md) diff --git a/website/sidebars.js b/website/sidebars.js index b9e819693e..d5ff060565 100644 --- a/website/sidebars.js +++ b/website/sidebars.js @@ -387,6 +387,7 @@ module.exports = { 'os.getpass', 'os.getversion', 'os.host', + 'os.hostarch', 'os.is', 'os.is64bit', 'os.isdir',