diff --git a/Source/cmSystemTools.cxx b/Source/cmSystemTools.cxx index 54104dbc79..13e3245427 100644 --- a/Source/cmSystemTools.cxx +++ b/Source/cmSystemTools.cxx @@ -787,11 +787,13 @@ std::size_t cmSystemTools::CalculateCommandLineLengthLimit() return sz; } -void cmSystemTools::MaybePrependCmdExe(std::vector& cmdLine) +cmsys::Status cmSystemTools::MaybePrependCmdExe( + std::vector& cmdLine) { #if defined(_WIN32) && !defined(__CYGWIN__) + cmsys::Status status; if (!cmdLine.empty()) { - auto const& applicationName = cmdLine.at(0); + std::string& applicationName = cmdLine.at(0); static cmsys::RegularExpression const winCmdRegex( "\\.([Bb][Aa][Tt]|[Cc][Mm][Dd])$"); cmsys::RegularExpressionMatch winCmdMatch; @@ -800,22 +802,21 @@ void cmSystemTools::MaybePrependCmdExe(std::vector& cmdLine) output.reserve(cmdLine.size() + 2); output.emplace_back(cmSystemTools::GetComspec()); output.emplace_back("/c"); - std::string tmpShortPath; - if (applicationName.find(' ') != std::string::npos && - cmSystemTools::GetShortPath(applicationName, tmpShortPath)) { - // If the batch file name contains spaces convert it to the windows - // short path. Otherwise it might cause issue when running cmd.exe. - output.emplace_back(tmpShortPath); - } else { - output.push_back(applicationName); + if (applicationName.find(' ') != std::string::npos) { + // Convert the batch file path to a short path to avoid spaces. + // Otherwise, cmd.exe may not handle arguments with spaces. + status = cmSystemTools::GetShortPath(applicationName, applicationName); } + output.push_back(applicationName); std::move(cmdLine.begin() + 1, cmdLine.end(), std::back_inserter(output)); cmdLine = std::move(output); } } + return status; #else static_cast(cmdLine); + return cmsys::Status::Success(); #endif } diff --git a/Source/cmSystemTools.h b/Source/cmSystemTools.h index 052c917758..60a70953db 100644 --- a/Source/cmSystemTools.h +++ b/Source/cmSystemTools.h @@ -246,12 +246,12 @@ public: * attempting to execute the batch files. * * Also cmd.exe is unable to parse batch file names correctly if they - * contain spaces. This function uses cmSystemTools::GetShortPath conversion - * to suppress this behavior. + * contain spaces. This function uses cmSystemTools::GetShortPath + * conversion to suppress this behavior, and returns its status. * * The function is noop on platforms different from the pure WIN32 one. */ - static void MaybePrependCmdExe(std::vector& cmdLine); + static cmsys::Status MaybePrependCmdExe(std::vector& cmdLine); /** * Run a single executable command