From b4d65e35b324ad5e767d7e58d772db344872d7c3 Mon Sep 17 00:00:00 2001 From: Joris Dauphin Date: Fri, 15 Mar 2024 19:29:45 +0100 Subject: [PATCH] Allow to select shell from command line for token replacement. (#2077) --- src/_premake_init.lua | 11 +++++++++++ src/base/os.lua | 24 +++++++++++++++++------- website/docs/Tokens.md | 6 +++--- 3 files changed, 31 insertions(+), 10 deletions(-) diff --git a/src/_premake_init.lua b/src/_premake_init.lua index 88d6090bc4..421b9f8f57 100644 --- a/src/_premake_init.lua +++ b/src/_premake_init.lua @@ -1876,6 +1876,17 @@ } } + newoption + { + trigger = "shell", + value = "VALUE", + description = "Select shell (for command token substitution)", + allowed = { + { "cmd", "Windows command shell" }, + { "posix", "For posix shells" }, + } + } + newoption { trigger = "scripts", diff --git a/src/base/os.lua b/src/base/os.lua index 90323711a1..e815ed6e41 100644 --- a/src/base/os.lua +++ b/src/base/os.lua @@ -258,6 +258,13 @@ return table.contains(tags, id:lower()) end +-- +-- Retrieve the current target shell ID string. +-- + + function os.shell() + return _OPTIONS.shell or iif(os.target() == "windows", "cmd", "posix") + end --- -- Determine if a directory exists on the file system, and that it is a @@ -599,7 +606,7 @@ --- os.commandTokens = { - _ = { + posix = { chdir = function(v) return "cd " .. path.normalize(v) end, @@ -631,7 +638,7 @@ return "touch " .. path.normalize(v) end, }, - windows = { + cmd = { chdir = function(v) return "chdir " .. path.translate(path.normalize(v)) end, @@ -681,9 +688,12 @@ } function os.translateCommands(cmd, map) - map = map or os.target() + map = map or os.shell() if type(map) == "string" then - map = os.commandTokens[map] or os.commandTokens["_"] + if map == "windows" then -- For retro compatibility + map = "cmd" + end + map = os.commandTokens[map] or os.commandTokens["posix"] end local processOne = function(cmd) @@ -697,7 +707,7 @@ local token = cmd:sub(i + 1, j - 1):lower() local args = cmd:sub(j + 2) - local func = map[token] or os.commandTokens["_"][token] + local func = map[token] or os.commandTokens["posix"][token] if func then cmd = cmd:sub(1, i -1) .. func(args) end @@ -725,7 +735,7 @@ -- Apply os slashes for decorated command paths. --- function os.translateCommandAndPath(dir, map) - if map == 'windows' then + if map == 'windows' or map == 'cmd' then return path.translate(dir) end return dir @@ -735,7 +745,7 @@ -- Translate decorated command paths into their OS equivalents. --- function os.translateCommandsAndPaths(cmds, basedir, location, map) - map = map or os.target() + map = map or os.shell() location = path.getabsolute(location) basedir = path.getabsolute(basedir) diff --git a/website/docs/Tokens.md b/website/docs/Tokens.md index 32a9da607f..caa3a86bb4 100644 --- a/website/docs/Tokens.md +++ b/website/docs/Tokens.md @@ -74,7 +74,7 @@ The paths are expanded relative to premake script, to obtain absolute paths, you ## Command Tokens -Command tokens represent a system level command in a platform-neutral way. +Command tokens represent a system level command in a shell-neutral way. ```lua postbuildcommands { @@ -94,11 +94,11 @@ You can use command tokens anywhere you specify a command line, including: * [prelinkcommands](prelinkcommands.md) * [rebuildcommands](rebuildcommands.md) -Command tokens are replaced with an appropriate command for the target platform. For Windows, path separators in the commmand arguments are converted to backslashes. +Command tokens are replaced with an appropriate command for the target shell. For Windows, path separators in the commmand arguments are converted to backslashes. The available tokens, and their replacements: -| Token | DOS | Posix | +| Token | DOS/cmd | Posix | |------------|---------------------------------------------|-----------------| | {CHDIR} | chdir {args} | cd {args} | | {COPYFILE} | copy /B /Y {args} | cp -f {args} |