I designed a powershell script that can take a path from CLI and add it to user path. I can run the script from a powershell or a cmd terminal but not from CMake.
I tried:
execute_process(COMMAND "powershell -File <path to script>/AddUserPath.ps1 -NewPath NewPath")
execute_process(COMMAND powershell "-File <path to script>/AddUserPath.ps1 -NewPath NewPath")
execute_process(COMMAND powershell -File <path to script>/AddUserPath.ps1 -NewPath NewPath)
or without the -File
I get only errors or nothing (for the first case).
the errors can be -File or /AddUserPath.ps1 not recognised as an applet, function or whatever.
Use quotes around the path to your script. CMake will then ensure the shell sees it as a single argument.
execute_process(
COMMAND powershell
"Myself/my path/Scripts/Test.ps1"
"-NewPath NewPathFromCMake2" # This looks suspicious, should it really be a single argument?
OUTPUT_VARIABLE FOO
RESULT_VARIABLE FOOR
ERROR_VARIABLE FOO_ER
)
Note that the quotes you use are not passed through to the shell command. They are interpreted by CMake as part of parsing the call to execute_process(). When you quote something like -NewPath NewPathFromCMake2, you are telling CMake to keep that together as a single argument on the command line. CMake will then apply quoting for you if needed, such as when there’s an embedded space or other special shell character.
I’m sorry but I still does not work. I tried to be a bit more exhaustive in my tests:
execute_process(COMMAND powershell C:/Users/Myself/mypath/Scripts/Test.ps1 "-NewPath NewPath" OUTPUT_VARIABLE FOO RESULT_VARIABLE FOOR ERROR_VARIABLE FOO_ER) SUCCESS
execute_process(COMMAND powershell C:/Users/Myself/mypath/Scripts/Test.ps1 -NewPath NewPath OUTPUT_VARIABLE FOO RESULT_VARIABLE FOOR ERROR_VARIABLE FOO_ER) SUCCESS
execute_process(COMMAND powershell "C:/Users/Myself/mypath/Scripts/Test.ps1" "-NewPath NewPath" OUTPUT_VARIABLE FOO RESULT_VARIABLE FOOR ERROR_VARIABLE FOO_ER) SUCCESS
execute_process(COMMAND powershell "C:/Users/Myself/mypath/Scripts/Test.ps1" -NewPath NewPath OUTPUT_VARIABLE FOO RESULT_VARIABLE FOOR ERROR_VARIABLE FOO_ER) SUCCESS
execute_process(COMMAND powershell "\"C:/Users/Myself/mypath/Scripts/Test.ps1\"" "-NewPath NewPath" OUTPUT_VARIABLE FOO RESULT_VARIABLE FOOR ERROR_VARIABLE FOO_ER) ERROR unexpected token -NewPath
execute_process(COMMAND powershell "\"C:/Users/Myself/mypath/Scripts/Test.ps1\"" -NewPath NewPath OUTPUT_VARIABLE FOO RESULT_VARIABLE FOOR ERROR_VARIABLE FOO_ER) ERROR unexpected token -NewPath
execute_process(COMMAND powershell "C:/Users/Myself/mypath/Scripts/Test.ps1 -NewPath NewPath" OUTPUT_VARIABLE FOO RESULT_VARIABLE FOOR ERROR_VARIABLE FOO_ER) SUCCESS
execute_process(COMMAND powershell "\"C:/Users/Myself/mypath/Scripts/Test.ps1\" -NewPath NewPath" OUTPUT_VARIABLE FOO RESULT_VARIABLE FOOR ERROR_VARIABLE FOO_ER) ERROR unexpected token -NewPath
execute_process(COMMAND powershell C:/Users/Myself/my path/Scripts/Test.ps1 "-NewPath NewPath" OUTPUT_VARIABLE FOO RESULT_VARIABLE FOOR ERROR_VARIABLE FOO_ER) ERROR C:/Users/Myself/my is not recognised as a command applet
execute_process(COMMAND powershell C:/Users/Myself/my path/Scripts/Test.ps1 -NewPath NewPath OUTPUT_VARIABLE FOO RESULT_VARIABLE FOOR ERROR_VARIABLE FOO_ER) ERROR C:/Users/Myself/my is not recognised as a command applet
execute_process(COMMAND powershell "C:/Users/Myself/my path/Scripts/Test.ps1" "-NewPath NewPath" OUTPUT_VARIABLE FOO RESULT_VARIABLE FOOR ERROR_VARIABLE FOO_ER) ERROR C:/Users/Myself/my is not recognised as a command applet
execute_process(COMMAND powershell "C:/Users/Myself/my path/Scripts/Test.ps1" -NewPath NewPath OUTPUT_VARIABLE FOO RESULT_VARIABLE FOOR ERROR_VARIABLE FOO_ER) ERROR C:/Users/Myself/my is not recognised as a command applet
execute_process(COMMAND powershell "\"C:/Users/Myself/my path/Scripts/Test.ps1\"" "-NewPath NewPath" OUTPUT_VARIABLE FOO RESULT_VARIABLE FOOR ERROR_VARIABLE FOO_ER) ERROR "C:/Users/Myself/my;path/Scripts/Test.ps1" unexpected token -NewPath: note the appearance of the semi-column within the path
execute_process(COMMAND powershell "\"C:/Users/Myself/my path/Scripts/Test.ps1\"" -NewPath NewPath OUTPUT_VARIABLE FOO RESULT_VARIABLE FOOR ERROR_VARIABLE FOO_ER) ERROR "C:/Users/Myself/my;path/Scripts/Test.ps1" unexpected token -NewPath: note the appearance of the semi-column within the path
execute_process(COMMAND powershell "C:/Users/Myself/my path/Scripts/Test.ps1 -NewPath NewPath" OUTPUT_VARIABLE FOO RESULT_VARIABLE FOOR ERROR_VARIABLE FOO_ER) ERROR C:/Users/Myself/my is not recognised as a command applet
execute_process(COMMAND powershell "\"C:/Users/Myself/my path/Scripts/Test.ps1\" -NewPath NewPath" OUTPUT_VARIABLE FOO RESULT_VARIABLE FOOR ERROR_VARIABLE FOO_ER) ERROR "C:/Users/Myself/my;path/Scripts/Test.ps1" unexpected token -NewPath: note the appearance of the semi-column within the path
Yet I made experiments directly within windows shell and there is multiple issues.
the right way to call a script seems to depend on the calling shell, and a ` must be used in order to escape the whitespace:
in native shell (cmd) I found these syntaxes:
The easy part will be, with string(replace…) to change all space by `space.
Nevertheless, I don’t know in what shell my command is launch from CMake. I suspect that both situation may arise but I don’t know how to determine in what shell the command will be launched.
How can I do?
Or is there another syntax that will works whatever the shell?
By the way, I have another question. My script returns with EXIT and a value.
Nethertheless the returned value retrieve by execute process is always 0 when return is 0 but 1 whatever the returned value if its not 0. Is it a normal behavior?
It seems like there’s probably some cmd /c getting in the way here (of quoting and return codes)? I don’t see it in the source though. I wonder if VERBATIM might help here too?
Which allows you to run ‘inline’ scripts which would allow you to also run .ps1 files, too. I use it - for example - for xml parsing files on Windows, like this (taken from here):
# Read the PLATFORM_REFERENCES from the WINDOWS_KITS_PLATFORM_PATH file.
execute_powershell("
$ErrorActionPreference = 'Stop'
[xml]$Platform = Get-Content \"${WINDOWS_KITS_PLATFORM_PATH}\"
$Platform.ApplicationPlatform.ContainedApiContracts.ApiContract |
ForEach-Object { $_.name, $_.version }
"
OUTPUT_VARIABLE PLATFORM_REFERENCES
RESULT_VARIABLE POWERSHELL_RESULT
)