Ninja: Optionally use powershell scripts instead of batch files for long custom commands
Hi, I'm using the latest CMake and ninja on Windows.
I notice that when a custom command is longer than 8192 characters, rather than be written directly to the build.ninja file, it's written to a .bat file.
I'm experiencing an issue with .bat files on a particular version of Windows that I am not sure if Microsoft will fix expediently (see Motivation).
It would be nice to have an option to be able to change the behavior of long commands to write to a PS script instead of a .bat script.
Since my arguments have generator expressions, I can't use file(WRITE).
For now, my solution is to write a PS script incrementally using multiple COMMANDs, and then finally calling that PS script, but it isn't ideal.
If CMake could support alternate ways to invoke long commands out of the box, it would be helpful.
Motivation
When working with add_custom_command and add_custom_target, CMake writes commands that would exceed the command line limit to a .bat file.
However, on Windows Server 2016, when executing a .bat file that contains a command longer 8192, while no error is thrown, the 8192th character is deleted from the command arguments during .bat execution, and the invoked program receives corrupt arguments. In CMake + ninja builds, the consequence is that when passing a long list of files to a custom command, one of the arguments or file paths will become corrupt.
A simple way to see this is to write:
powershell.exe -Command "& { echo 123456789123456789 }" > output.txt
to a .bat file, and modify the string echoed ("123456789123456789") to be longer than 8192 characters.
If you run this, you will see that output.txt is missing a character from what should have been echoed.
This issue only happens on Windows Server 2016 (I haven't tried 2019) and does not happen on the latest Windows 10 Pro.
Issue tracking on Microsoft's Feedback Hub (upvote would be appreciated): https://aka.ms/AAi2pi7