cmSystemTools: Teach MaybePrependCmdExe to return GetShortPathNameW failure

This commit is contained in:
Brad King 2025-02-02 07:35:50 -05:00
parent 611a3000f7
commit 817b3967f8
2 changed files with 14 additions and 13 deletions

View File

@ -787,11 +787,13 @@ std::size_t cmSystemTools::CalculateCommandLineLengthLimit()
return sz; return sz;
} }
void cmSystemTools::MaybePrependCmdExe(std::vector<std::string>& cmdLine) cmsys::Status cmSystemTools::MaybePrependCmdExe(
std::vector<std::string>& cmdLine)
{ {
#if defined(_WIN32) && !defined(__CYGWIN__) #if defined(_WIN32) && !defined(__CYGWIN__)
cmsys::Status status;
if (!cmdLine.empty()) { if (!cmdLine.empty()) {
auto const& applicationName = cmdLine.at(0); std::string& applicationName = cmdLine.at(0);
static cmsys::RegularExpression const winCmdRegex( static cmsys::RegularExpression const winCmdRegex(
"\\.([Bb][Aa][Tt]|[Cc][Mm][Dd])$"); "\\.([Bb][Aa][Tt]|[Cc][Mm][Dd])$");
cmsys::RegularExpressionMatch winCmdMatch; cmsys::RegularExpressionMatch winCmdMatch;
@ -800,22 +802,21 @@ void cmSystemTools::MaybePrependCmdExe(std::vector<std::string>& cmdLine)
output.reserve(cmdLine.size() + 2); output.reserve(cmdLine.size() + 2);
output.emplace_back(cmSystemTools::GetComspec()); output.emplace_back(cmSystemTools::GetComspec());
output.emplace_back("/c"); output.emplace_back("/c");
std::string tmpShortPath; if (applicationName.find(' ') != std::string::npos) {
if (applicationName.find(' ') != std::string::npos && // Convert the batch file path to a short path to avoid spaces.
cmSystemTools::GetShortPath(applicationName, tmpShortPath)) { // Otherwise, cmd.exe may not handle arguments with spaces.
// If the batch file name contains spaces convert it to the windows status = cmSystemTools::GetShortPath(applicationName, applicationName);
// short path. Otherwise it might cause issue when running cmd.exe.
output.emplace_back(tmpShortPath);
} else {
output.push_back(applicationName);
} }
output.push_back(applicationName);
std::move(cmdLine.begin() + 1, cmdLine.end(), std::move(cmdLine.begin() + 1, cmdLine.end(),
std::back_inserter(output)); std::back_inserter(output));
cmdLine = std::move(output); cmdLine = std::move(output);
} }
} }
return status;
#else #else
static_cast<void>(cmdLine); static_cast<void>(cmdLine);
return cmsys::Status::Success();
#endif #endif
} }

View File

@ -246,12 +246,12 @@ public:
* attempting to execute the batch files. * attempting to execute the batch files.
* *
* Also cmd.exe is unable to parse batch file names correctly if they * Also cmd.exe is unable to parse batch file names correctly if they
* contain spaces. This function uses cmSystemTools::GetShortPath conversion * contain spaces. This function uses cmSystemTools::GetShortPath
* to suppress this behavior. * conversion to suppress this behavior, and returns its status.
* *
* The function is noop on platforms different from the pure WIN32 one. * The function is noop on platforms different from the pure WIN32 one.
*/ */
static void MaybePrependCmdExe(std::vector<std::string>& cmdLine); static cmsys::Status MaybePrependCmdExe(std::vector<std::string>& cmdLine);
/** /**
* Run a single executable command * Run a single executable command