Skip to content

Adds a new os.targetarch() function. #2263

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 28, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions modules/self-test/test_runner.lua
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@
hooks.action = _ACTION
hooks.options = _OPTIONS
hooks.targetOs = _TARGET_OS
hooks.targetArch = _TARGET_ARCH

hooks.io_open = io.open
hooks.io_output = io.output
Expand Down Expand Up @@ -217,6 +218,7 @@
p.action.set(hooks.action)
_OPTIONS = hooks.options
_TARGET_OS = hooks.targetOs
_TARGET_ARCH = hooks.targetArch

io.open = hooks.io_open
io.output = hooks.io_output
Expand Down
5 changes: 2 additions & 3 deletions premake5.lua
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,9 @@
--
{ "Win32", "Same as x86" },
{ "x64", "Same as x86_64" },
--
{ "default", "Generates default platforms for targets, x86 and x86_64 projects for Windows." }
},
default = "default",
-- "Generates default platforms for targets, x86 and x86_64 projects for Windows." }
default = nil,
}

--
Expand Down
21 changes: 21 additions & 0 deletions src/_premake_init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1876,6 +1876,24 @@
}
}

local function getArchs()
local keys={}
for key,_ in pairs(premake.field.get("architecture").allowed) do
if type(key) ~= "number" then
table.insert(keys, { key, "" })
end
end
return keys
end

newoption
{
trigger = "arch",
value = "VALUE",
description = "Generate files for a different architecture",
allowed = getArchs()
}

newoption
{
trigger = "shell",
Expand Down Expand Up @@ -1989,6 +2007,9 @@
filter { "system:darwin" }
toolset "clang"

filter { "platforms:Win32" }
architecture "x86"

filter { "platforms:Win64" }
architecture "x86_64"

Expand Down
3 changes: 2 additions & 1 deletion src/base/action.lua
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,11 @@
function action.set(name)
_ACTION = name

-- Some actions imply a particular operating system
-- Some actions imply a particular operating system or architecture
local act = action.get(name)
if act then
_TARGET_OS = act.targetos or _TARGET_OS
_TARGET_ARCH = act.targetarch or _TARGET_ARCH
end

-- Some are implemented in standalone modules
Expand Down
8 changes: 8 additions & 0 deletions src/base/os.lua
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,14 @@
return _OPTIONS.os or _TARGET_OS
end

--
-- Retrieve the current target architecture ID string.
--

function os.targetarch()
return _OPTIONS.arch or _TARGET_ARCH
end

function os.get()
local caller = filelineinfo(2)
premake.warnOnce(caller, "os.get() is deprecated, use 'os.target()' or 'os.host()'.\n @%s\n", caller)
Expand Down
2 changes: 1 addition & 1 deletion src/base/oven.lua
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@
-- external Visual Studio project files.

local system = os.target()
local architecture = nil
local architecture = os.targetarch()
local toolset = p.action.current().toolset

if platform then
Expand Down
2 changes: 1 addition & 1 deletion src/host/os_getversion.c
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ int getversion(struct OsVersionInfo* info)
if (uname(&u))
{
// error
info->description = PLATFORM_STRING;
info->description = PLATFORM_OS;
return 0;
}

Expand Down
2 changes: 1 addition & 1 deletion src/host/os_host.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@

int os_host(lua_State* L)
{
lua_pushstring(L, PLATFORM_STRING);
lua_pushstring(L, PLATFORM_OS);
return 1;
}
8 changes: 6 additions & 2 deletions src/host/premake.c
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,14 @@ int premake_init(lua_State* L)
lua_pushstring(L, PREMAKE_PROJECT_URL);
lua_setglobal(L, "_PREMAKE_URL");

/* set the OS platform variable */
lua_pushstring(L, PLATFORM_STRING);
/* set the target OS platform variable */
lua_pushstring(L, PLATFORM_OS);
lua_setglobal(L, "_TARGET_OS");

/* set the target arch platform variable */
lua_pushnil(L);
lua_setglobal(L, "_TARGET_ARCH");

/* find the user's home directory */
value = getenv("HOME");
if (!value) value = getenv("USERPROFILE");
Expand Down
16 changes: 8 additions & 8 deletions src/host/premake.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,28 @@
* Windows but since it is the most common I use it as the default */
#if defined(__linux__)
#define PLATFORM_LINUX (1)
#define PLATFORM_STRING "linux"
#define PLATFORM_OS "linux"
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
#define PLATFORM_BSD (1)
#define PLATFORM_STRING "bsd"
#define PLATFORM_OS "bsd"
#elif defined(__APPLE__) && defined(__MACH__)
#define PLATFORM_MACOSX (1)
#define PLATFORM_STRING "macosx"
#define PLATFORM_OS "macosx"
#elif defined(__sun__) && defined(__svr4__)
#define PLATFORM_SOLARIS (1)
#define PLATFORM_STRING "solaris"
#define PLATFORM_OS "solaris"
#elif defined(__HAIKU__)
#define PLATFORM_HAIKU (1)
#define PLATFORM_STRING "haiku"
#define PLATFORM_OS "haiku"
#elif defined (_AIX)
#define PLATFORM_AIX (1)
#define PLATFORM_STRING "aix"
#define PLATFORM_OS "aix"
#elif defined (__GNU__)
#define PLATFORM_HURD (1)
#define PLATFORM_STRING "hurd"
#define PLATFORM_OS "hurd"
#else
#define PLATFORM_WINDOWS (1)
#define PLATFORM_STRING "windows"
#define PLATFORM_OS "windows"
#endif

#define PLATFORM_POSIX (PLATFORM_LINUX || PLATFORM_BSD || PLATFORM_MACOSX || PLATFORM_SOLARIS || PLATFORM_HAIKU)
Expand Down
17 changes: 17 additions & 0 deletions tests/base/test_os.lua
Original file line number Diff line number Diff line change
Expand Up @@ -508,3 +508,20 @@
local arch = os.hostarch()
test.istrue(string.len(arch) > 0)
end


--
-- os.targetarch() tests.
--

function suite.targetarch()
-- nil by default for backwards compatibility
test.isequal(nil, os.targetarch())

_TARGET_ARCH = "x64"
test.isequal(_TARGET_ARCH, os.targetarch())

-- --arch has priority over _TARGET_ARCH
_OPTIONS["arch"] = "arm64"
test.isequal(_OPTIONS["arch"], os.targetarch())
end
45 changes: 45 additions & 0 deletions website/docs/os.targetarch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
Returns the id of the architecture currently being targeted.
See [architecture](architecture.md) for a complete list of architecture identifiers.

```lua
id = os.targetarch()
```

This will return `nil` by default instead of returning the architecture for the current running
system due to backwards compatibility.

A target architecture can be set either via setting [_TARGET_ARCH](premake_TARGET_ARCH.md) or
by passing an architecture via the `--arch` command line option (which has the most priority).


### 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 generating files for, which is not necessarily the same as the architecture for the OS that Premake is currently running on.


### Availability ###

Premake 5.0.0 beta 3 or later.


### Examples ###

```lua
print(os.targetarch())
-- "x86_64"
end
```


### See Also ###

* [_TARGET_ARCH](premake_TARGET_ARCH.md)
* [os.hostarch](os.hostarch.md)
* [architecture](architecture.md)
5 changes: 5 additions & 0 deletions website/docs/premake_OS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
id: _OS
---

:::caution_
**This has been deprecated in Premake 5.0 alpha 12.** Use the new [_TARGET_OS](premake_TARGET_OS.md) instead.
:::

Stores the name of the operating system currently being targeted; see [system()](system.md) for a complete list of OS identifiers.

The current OS may be overridden on the command line with the `--os` option.
Expand All @@ -17,4 +21,5 @@ Premake 4.0 or later.

## See Also ##

* [_TARGET_OS](premake_TARGET_OS.md)
* [Using Premake](Using-Premake.md)
21 changes: 21 additions & 0 deletions website/docs/premake_TARGET_ARCH.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
id: _TARGET_ARCH
---

Stores the name of the architecture currently being targeted; see [architecture()](architecture.md) for a complete list of architecture identifiers.

The current architecture may be overridden on the command line with the `--arch` option.

```
$ premake5 --arch=x86 gmake
```

### Availability ###

Premake 5.0 beta 3 or later.


## See Also ##

* [os.targetarch](os.targetarch.md)
* [_TARGET_OS](premake_TARGET_OS.md)
20 changes: 20 additions & 0 deletions website/docs/premake_TARGET_OS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
id: _TARGET_OS
---

Stores the name of the operating system currently being targeted; see [system()](system.md) for a complete list of OS identifiers.

The current OS may be overridden on the command line with the `--os` option.

```
$ premake5 --os=linux gmake
```

### Availability ###

Premake 5.0 alpha 12 or later.


## See Also ##

* [_TARGET_ARCH](premake_TARGET_ARCH.md)
4 changes: 3 additions & 1 deletion website/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,8 @@ module.exports = {
'_PREMAKE_COMMAND',
'_PREMAKE_DIR',
'_PREMAKE_VERSION',
'_WORKING_DIR',
'_TARGET_ARCH',
'_TARGET_OS',
'iif',
'include',
'includeexternal',
Expand Down Expand Up @@ -406,6 +407,7 @@ module.exports = {
'os.rmdir',
'os.stat',
'os.target',
'os.targetarch',
'os.touchfile',
'os.translateCommands',
'os.uuid',
Expand Down