Skip to content

Commit

Permalink
Add a os.hostarch() function to get the host system architecture.
Browse files Browse the repository at this point in the history
  • Loading branch information
tritao committed Sep 21, 2024
1 parent 9a70afe commit b9adb26
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/host/os_hostarch.c
Original file line number Diff line number Diff line change
@@ -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;
}
1 change: 1 addition & 0 deletions src/host/premake.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down
1 change: 1 addition & 0 deletions src/host/premake.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,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);
Expand Down
10 changes: 10 additions & 0 deletions tests/base/test_os.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions website/docs/Lua-Library-Additions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
31 changes: 31 additions & 0 deletions website/docs/os.hostarch.md
Original file line number Diff line number Diff line change
@@ -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)
1 change: 1 addition & 0 deletions website/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ module.exports = {
'os.getpass',
'os.getversion',
'os.host',
'os.hostarch',
'os.is',
'os.is64bit',
'os.isdir',
Expand Down

0 comments on commit b9adb26

Please sign in to comment.