Skip to content

Commit

Permalink
os.translateCommandsAndPaths (#2082)
Browse files Browse the repository at this point in the history
* Fix `os.translateCommandsAndPaths`.
Fix msvc rule with `%[path]`.
Fix xcode4 with `%[path]`.

* Complete and fix documentation about token replacements.
  • Loading branch information
Jarod42 authored Feb 28, 2024
1 parent c22f3db commit 5d6d7ff
Show file tree
Hide file tree
Showing 14 changed files with 48 additions and 34 deletions.
2 changes: 1 addition & 1 deletion modules/vstudio/vs2010_rules_props.lua
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@

-- write out the result.
if buildcommands and #buildcommands > 0 then
local cmds = os.translateCommands(buildcommands, p.WINDOWS)
local cmds = os.translateCommandsAndPaths(buildcommands, r.basedir, r.location, p.WINDOWS)
cmds = table.concat(cmds, p.eol())
p.x('<CommandLineTemplate>%s</CommandLineTemplate>', cmds)
end
Expand Down
2 changes: 1 addition & 1 deletion modules/xcode/xcode_common.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1061,7 +1061,7 @@

if #commands > 0 then
table.insert(commands, 1, 'set -e') -- Tells the shell to exit when any command fails
commands = os.translateCommands(commands, p.MACOSX)
commands = os.translateCommandsAndPaths(commands, tr.project.basedir, tr.project.location, p.MACOSX)
if not wrapperWritten then
_p('/* Begin PBXShellScriptBuildPhase section */')
wrapperWritten = true
Expand Down
6 changes: 3 additions & 3 deletions src/base/os.lua
Original file line number Diff line number Diff line change
Expand Up @@ -735,12 +735,12 @@
-- Translate decorated command paths into their OS equivalents.
---
function os.translateCommandsAndPaths(cmds, basedir, location, map)
local translatedBaseDir = path.getrelative(location, basedir)

map = map or os.target()
location = path.getabsolute(location)
basedir = path.getabsolute(basedir)

local translateFunction = function(value)
local result = path.join(translatedBaseDir, value)
local result = path.getrelative(location, path.join(basedir, value))
result = os.translateCommandAndPath(result, map)
if value:endswith('/') or value:endswith('\\') or -- if original path ends with a slash then ensure the same
value:endswith('/"') or value:endswith('\\"') then
Expand Down
3 changes: 3 additions & 0 deletions tests/base/test_os.lua
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,9 @@
test.isequal('cmdtool "../foo/path1" "../foo/path2/"', os.translateCommandsAndPaths("cmdtool %[path1] %[path2/]", '../foo', '.', 'osx'))
end

function suite.translateCommandsAndPaths_RelativePath()
test.isequal('cmdtool "path1" "../bar/path2/"', os.translateCommandsAndPaths("cmdtool %[../foo/path1] %[path2/]", './bar', './foo', 'osx'))
end

--
-- Helpers
Expand Down
6 changes: 3 additions & 3 deletions website/docs/Custom-Build-Commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ These are the simplest to setup and use: pass one or more command lines to the [
```lua
-- copy a file from the objects directory to the target directory
postbuildcommands {
"{COPY} %{cfg.objdir}/output.map %{cfg.targetdir}"
"{COPYFILE} %[%{!cfg.objdir}/output.map] %[%{!cfg.targetdir}]"
}
```

Expand All @@ -29,11 +29,11 @@ Here is an example which compiles all Lua files in a project to C:
```lua
filter 'files:**.lua'
-- A message to display while this build step is running (optional)
buildmessage 'Compiling %{file.relpath}'
buildmessage 'Compiling %[%{file.relpath}]'

-- One or more commands to run (required)
buildcommands {
'luac -o "%{cfg.objdir}/%{file.basename}.out" "%{file.relpath}"'
'luac -o "%[%{!cfg.objdir}/%{file.basename}.out]" "%[%{!file.relpath}]"'
}

-- One or more outputs resulting from the build (required)
Expand Down
2 changes: 1 addition & 1 deletion website/docs/Custom-Rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ rule "MyCustomRule"
}
```

Properties may then be used in the rule commands by enclosing the name in square brackets. This, again, is a Visual Studio convention; we may switch it up if support for additional exporters becomes available.
Properties may then be used in the rule commands by enclosing the name in square brackets. This, again, is a Visual Studio convention; we may switch it up if support for additional exporters becomes available. You might find in [Tokens](Tokens.md) some portable tokens for replacement.

```lua
buildcommand 'MyCustomCC.exe -c "%(FullPath)" -o "%(IntDir)/%(Filename).obj" [StripDebugInfo]
Expand Down
20 changes: 18 additions & 2 deletions website/docs/Tokens.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Value tokens are expressions wrapped in a `%{}` sequence. Tokens have access to
%{cfg.targetdir}
```

The contents of the %{} are run through loadstring() and executed at token-replacement time, so more complex replacements can be used. You can access any global value.
The contents of the %{} are run through `loadstring()` and executed at token-replacement time, so more complex replacements can be used. You can access any global value.

```lua
%{wks.name:gsub(' ', '_')}
Expand Down Expand Up @@ -70,13 +70,15 @@ file.extension -- (including '.'; eg ".cpp")
[target].suffix
```

The paths are expanded relative to premake script, to obtain absolute paths, you have to add `!` as in `%{!file.path}`.

## Command Tokens

Command tokens represent a system level command in a platform-neutral way.

```lua
postbuildcommands {
"{COPYFILE} file1.txt file2.txt"
"{COPYFILE} %[file1.txt] %[file2.txt]"
}
```

Expand Down Expand Up @@ -116,6 +118,20 @@ The following tokens are deprecated:
|------------|---------------------------------------------|-----------------|-------------------------------------|
| {COPY} | xcopy /Q /E /Y /I {args} | cp -rf {args} | Use {COPYDIR} or {COPYFILE} instead |

### Path in commands

Paths in Premake should be relative to premake script in which they appears.

When you specify a path inside a commands, you have to wrap path insice `%[]` to allow correct trnasformation for the generator.

i.e.

```lua
buildcommands {
"{COPYFILE} %[%{!file.abspath}] %[%{!sln.location}/%{file.basename}]"
}
```

## Tokens and Filters

Tokens are not expanded in filters. See [issue 1306](https://github.com/premake/premake-core/issues/1036#issuecomment-379685035) for some illustrative examples.
5 changes: 3 additions & 2 deletions website/docs/buildcommands.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ Use [per-file custom build commands](Custom-Build-Commands.md) to compile all Lu
```lua
filter 'files:**.lua'
-- A message to display while this build step is running (optional)
buildmessage 'Compiling %{file.relpath}'
buildmessage 'Compiling %[%{file.relpath}]'

-- One or more commands to run (required)
buildcommands {
'luac -o "%{cfg.objdir}/%{file.basename}.out" "%{file.relpath}"'
'luac -o "%[%{!cfg.objdir}/%{file.basename}.out]" "%[%{file.relpath}]"'
}

-- One or more outputs resulting from the build (required)
Expand Down Expand Up @@ -56,6 +56,7 @@ project "MyProject"

## See Also ##

* [Tokens](Tokens.md)
* [Custom Build Commands](Custom-Build-Commands.md)
* [Makefile Projects](Makefile-Projects.md)
* [buildinputs](buildinputs.md)
Expand Down
9 changes: 3 additions & 6 deletions website/docs/postbuildcommands.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ postbuildcommands { "commands" }

### Parameters ###

`commands` is one or more shell commands. These commands will be passed to the shell exactly as entered, including path separators and the like.
`commands` is one or more shell commands.

### Applies To ###

Expand All @@ -19,14 +19,11 @@ Premake 4.4 or later.
### Examples ###

```lua
filter { "system:windows" }
postbuildcommands { "copy default.config bin\\project.config" }

filter { "not system:windows" }
postbuildcommands { "cp default.config bin/project.config" }
postbuildcommands { "{COPYFILE} %[default.config] %[bin/project.config]" }
```

### See Also ###
* [Tokens](Tokens.md)
* [Custom Build Commands](Custom-Build-Commands.md)
* [Tokens](Tokens.md)
* [prebuildcommands](prebuildcommands.md)
Expand Down
3 changes: 2 additions & 1 deletion website/docs/postbuildmessage.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ Premake 5.0 or later.

```lua
project "MyProject"
postbuildcommands { "copy dependencies/*.lib bin" }
postbuildcommands { "{COPYFILE} %[dependencies/*.lib] %[bin]" }
postbuildmessage "Copying dependencies..."
```

### See Also ###

* [Tokens](Tokens.md)
* [postbuildcommands](postbuildcommands.md)
* [prebuildmessage](prebuildmessage.md)
* [prelinkmessage](prelinkmessage.md)
9 changes: 3 additions & 6 deletions website/docs/prebuildcommands.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ prebuildcommands { "commands" }

### Parameters ###

`commands` is one or more shell commands. These commands will be passed to the shell exactly as entered, including path separators and the like.
`commands` is one or more shell commands.

### Applies To ###

Expand All @@ -19,14 +19,11 @@ Premake 4.4 or later.
### Examples ###

```lua
filter { "system:windows" }
prebuildcommands { "copy default.config bin\\project.config" }

filter { "not system:windows" }
prebuildcommands { "cp default.config bin/project.config" }
prebuildcommands { "{COPYFILE} %[default.config] %[bin/project.config]" }
```

### See Also ###

* [Tokens](Tokens.md)
* [postbuildcommands](postbuildcommands.md)
* [prelinkcommands](prelinkcommands.md)
3 changes: 2 additions & 1 deletion website/docs/prebuildmessage.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ Premake 4.4 or later.

```lua
project "MyProject"
prebuildcommands { "copy dependencies/*.lib bin" }
prebuildcommands { "{COPYFILE} %[dependencies/*.lib] %[bin]" }
prebuildmessage "Copying dependencies..."
```

### See Also ###

* [Tokens](Tokens.md)
* [prebuildcommands](prebuildcommands.md)
* [postbuildmessage](postbuildmessage.md)
* [prelinkmessage](prelinkmessage.md)
9 changes: 3 additions & 6 deletions website/docs/prelinkcommands.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ prelinkcommands { "commands" }

### Parameters ###

`commands` is one or more shell commands. These commands will be passed to the shell exactly as entered, including path separators and the like.
`commands` is one or more shell commands.

### Applies To ###

Expand All @@ -19,14 +19,11 @@ Premake 4.4 or later.
### Examples ###

```lua
filter { "system:windows" }
prelinkcommands { "copy default.config bin\\project.config" }

filter { "not system:windows" }
prelinkcommands { "cp default.config bin/project.config" }
prelinkcommands { "{COPY} %[default.config] %[bin/project.config]" }
```

### See Also ###

* [Tokens](Tokens.md)
* [prebuildcommands](prebuildcommands.md)
* [postbuildcommands](postbuildcommands.md)
3 changes: 2 additions & 1 deletion website/docs/prelinkmessage.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ Premake 4.4 or later.

```lua
project "MyProject"
prelinkcommands { "copy dependencies/*.lib bin" }
prelinkcommands { "{COPYFILE} %[dependencies/*.lib] %[bin]" }
prelinkmessage "Copying dependencies..."
```

## See Also ##

* [Tokens](Tokens.md)
* [prelinkcommands](prelinkcommands.md)
* [prebuildmessage](prebuildmessage.md)
* [postbuildmessage](postbuildmessage.md)

0 comments on commit 5d6d7ff

Please sign in to comment.