From d42055b283496e3259a89db9aa9074f4169bcf34 Mon Sep 17 00:00:00 2001 From: Bill Hoffman Date: Wed, 3 Apr 2002 16:14:06 -0500 Subject: ENH: fix more space problems, you can add args to the ExecProgram command separatly now --- Source/cmExecProgramCommand.cxx | 35 +++++++++++++++++-- Source/cmExecProgramCommand.h | 6 +++- Source/cmNMakeMakefileGenerator.cxx | 31 +++++++---------- Source/cmSystemTools.cxx | 56 +++++++++++++++++++++++++------ Source/cmSystemTools.h | 3 ++ Source/cmaketest.cxx | 16 ++------- Tests/Complex/CMakeLists.txt | 6 ++-- Tests/ComplexOneConfig/CMakeLists.txt | 6 ++-- Tests/ComplexRelativePaths/CMakeLists.txt | 6 ++-- 9 files changed, 109 insertions(+), 56 deletions(-) diff --git a/Source/cmExecProgramCommand.cxx b/Source/cmExecProgramCommand.cxx index aea6950..7e1ad76 100644 --- a/Source/cmExecProgramCommand.cxx +++ b/Source/cmExecProgramCommand.cxx @@ -25,16 +25,45 @@ bool cmExecProgramCommand::InitialPass(std::vector const& args) this->SetError("called with incorrect number of arguments"); return false; } + std::string arguments; + bool doingargs = false; + int count = 0; + for(int i=0; i < args.size(); ++i) + { + if(doingargs) + { + arguments += args[i]; + arguments += " "; + count++; + } + else if(args[i] == "ARGS") + { + count++; + doingargs = true; + } + } + + std::string command; + if(arguments.size()) + { + command = cmSystemTools::ConvertToOutputPath(args[0].c_str()); + command += " "; + command += arguments; + } + else + { + command = args[0]; + } std::string output; - if(args.size() == 2) + if(args.size() - count == 2) { cmSystemTools::MakeDirectory(args[1].c_str()); - cmSystemTools::RunCommand(args[0].c_str(), output, + cmSystemTools::RunCommand(command.c_str(), output, cmSystemTools::ConvertToOutputPath(args[1].c_str()).c_str()); } else { - cmSystemTools::RunCommand(args[0].c_str(), output); + cmSystemTools::RunCommand(command.c_str(), output); } return true; } diff --git a/Source/cmExecProgramCommand.h b/Source/cmExecProgramCommand.h index 75f083e..01acc1b 100644 --- a/Source/cmExecProgramCommand.h +++ b/Source/cmExecProgramCommand.h @@ -64,7 +64,11 @@ public: virtual const char* GetFullDocumentation() { return - "EXEC_PROGRAM(Executable [Directory to run in])"; + "EXEC_PROGRAM(Executable [Directory to run in] [ARGS arguments to executable])" + "The executable is run in the optionally specified Directory. The executable " + "can include arguments if it is double quoted, but it is better to use the " + "optional ARGS argument to specify arguments to the program. This is because " + "cmake will then be able to escape spaces in the Executable path."; } cmTypeMacro(cmExecProgramCommand, cmCommand); diff --git a/Source/cmNMakeMakefileGenerator.cxx b/Source/cmNMakeMakefileGenerator.cxx index f88b430..9e55a03 100644 --- a/Source/cmNMakeMakefileGenerator.cxx +++ b/Source/cmNMakeMakefileGenerator.cxx @@ -52,19 +52,14 @@ std::string cmNMakeMakefileGenerator::ShortPath(const char* path) } // if there are spaces then call GetShortPathName to get rid of them - char *buffer = new char[strlen(path)+1]; - if(GetShortPathName(path, buffer, - static_cast(strlen(path)+1)) != 0) - { - ret = buffer; - } - else + if(!cmSystemTools::GetShortPath(path, ret)) { // if GetShortPathName failed for some reason use // ConvertToOutputPath instead which will at least escape the spaces ret = this->ConvertToOutputPath(path); + return ret; } - delete [] buffer; + ret = this->ConvertToOutputPath(ret.c_str()); return ret; } @@ -161,37 +156,37 @@ void cmNMakeMakefileGenerator::OutputMakeVariables(std::ostream& fout) m_Makefile->ExpandVariablesInString(replaceVars); fout << replaceVars.c_str(); std::string ccommand = m_Makefile->GetDefinition("CCOMMAND_COMMAND"); - fout << "RM = " << this->ConvertToOutputPath(ccommand.c_str()) << " remove -f\n"; + fout << "RM = " << this->ShortPath(ccommand.c_str()) << " remove -f\n"; std::string ccompiler = m_Makefile->GetDefinition("CMAKE_C_COMPILER"); fout << "CMAKE_C_COMPILER = " - << this->ConvertToOutputPath(ccompiler.c_str()) << "\n"; + << this->ShortPath(ccompiler.c_str()) << "\n"; std::string cxxcompiler = m_Makefile->GetDefinition("CMAKE_CXX_COMPILER"); fout << "CMAKE_CXX_COMPILER = " - << this->ConvertToOutputPath(cxxcompiler.c_str()) << "\n"; + << this->ShortPath(cxxcompiler.c_str()) << "\n"; std::string linker = m_Makefile->GetDefinition("CMAKE_LINKER"); fout << "CMAKE_LINKER = " << - this->ConvertToOutputPath(linker.c_str()) << "\n"; + this->ShortPath(linker.c_str()) << "\n"; std::string lib_manager = m_Makefile->GetDefinition("CMAKE_LIBRARY_MANAGER"); fout << "CMAKE_LIBRARY_MANAGER = " - << this->ConvertToOutputPath(lib_manager.c_str()) << "\n"; + << this->ShortPath(lib_manager.c_str()) << "\n"; std::string cmakecommand = m_Makefile->GetDefinition("CMAKE_COMMAND"); fout << "CMAKE_COMMAND = " - << this->ConvertToOutputPath(cmakecommand.c_str()) << "\n"; + << this->ShortPath(cmakecommand.c_str()) << "\n"; fout << "CMAKE_CURRENT_SOURCE = " - << ShortPath(m_Makefile->GetStartDirectory() ) + << this->ShortPath(m_Makefile->GetStartDirectory() ) << "\n"; fout << "CMAKE_CURRENT_BINARY = " - << ShortPath(m_Makefile->GetStartOutputDirectory()) + << this->ShortPath(m_Makefile->GetStartOutputDirectory()) << "\n"; fout << "CMAKE_SOURCE_DIR = " - << ShortPath(m_Makefile->GetHomeDirectory()) << "\n"; + << this->ShortPath(m_Makefile->GetHomeDirectory()) << "\n"; fout << "CMAKE_BINARY_DIR = " - << ShortPath(m_Makefile->GetHomeOutputDirectory() ) + << this->ShortPath(m_Makefile->GetHomeOutputDirectory() ) << "\n"; // Output Include paths diff --git a/Source/cmSystemTools.cxx b/Source/cmSystemTools.cxx index 6a2a56b..0b05373 100644 --- a/Source/cmSystemTools.cxx +++ b/Source/cmSystemTools.cxx @@ -1275,20 +1275,13 @@ bool cmSystemTools::RunCommand(const char* command, std::string shortCmd; std::string cmd = quoted.match(1); std::string args = quoted.match(2); - char *buffer = new char[cmd.size()+1]; - if(GetShortPathName(cmd.c_str(), buffer, - int(cmd.size())) != 0) + if(!cmSystemTools::GetShortPath(cmd.c_str(), shortCmd)) { - shortCmd = buffer; - shortCmd += " "; - shortCmd += args; - } - else - { - cmSystemTools::Error("Could not get GetShortPathName for ", - cmd.c_str()); + cmSystemTools::Error("GetShortPath failed for " , cmd.c_str()); return false; } + shortCmd += " "; + shortCmd += args; return RunCommandViaSystem(shortCmd.c_str(), dir, output, retVal, verbose); } @@ -1845,3 +1838,44 @@ void cmSystemTools::ExpandListArguments(std::vector const& argument } } } + +bool cmSystemTools::GetShortPath(const char* path, std::string& shortPath) +{ +#if defined(WIN32) && !defined(__CYGWIN__) + const int size = int(strlen(path)) +1; // size of return + char *buffer = new char[size]; // create a buffer + buffer[0] = 0; + int ret = GetShortPathName(path, buffer, size); + if(buffer[0] == 0 || ret > size) + { + if(ret < size) + { + LPVOID lpMsgBuf; + FormatMessage( + FORMAT_MESSAGE_ALLOCATE_BUFFER | + FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, + GetLastError(), + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language + (LPTSTR) &lpMsgBuf, + 0, + NULL + ); + cmSystemTools::Error((LPCTSTR)lpMsgBuf); + LocalFree( lpMsgBuf ); + } + cmSystemTools::Error("Unable to get a short path: ", path); + return false; + } + else + { + shortPath = buffer; + delete [] buffer; + return true; + } +#else + shortPath = path; + return true; +#endif +} diff --git a/Source/cmSystemTools.h b/Source/cmSystemTools.h index 04ee977..62d5568 100644 --- a/Source/cmSystemTools.h +++ b/Source/cmSystemTools.h @@ -267,6 +267,9 @@ public: static bool RunCommand(const char* command, std::string& output, int &retVal, const char* directory = 0, bool verbose = true); + ///! for windows return the short path for the given path, unix just a pass through + static bool GetShortPath(const char* path, std::string& result); + ///! change directory the the directory specified static int ChangeDirectory(const char* dir); diff --git a/Source/cmaketest.cxx b/Source/cmaketest.cxx index 37a3d98..4f35bcf 100644 --- a/Source/cmaketest.cxx +++ b/Source/cmaketest.cxx @@ -157,13 +157,7 @@ int main (int argc, char **argv) #if defined(_WIN32) && !defined(__CYGWIN__) if(makeCommand.find(' ') != std::string::npos) { - char *buffer = new char[makeCommand.size()+1]; - if(GetShortPathName(makeCommand.c_str(), buffer, - int(makeCommand.size()+1)) != 0) - { - makeCommand = buffer; - } - delete [] buffer;\ + cmSystemTools::GetShortPath(makeCommand.c_str(), makeCommand); } #endif makeCommand += " "; @@ -177,13 +171,7 @@ int main (int argc, char **argv) #if defined(_WIN32) && !defined(__CYGWIN__) if(makeCommand.find(' ') != std::string::npos) { - char *buffer = new char[makeCommand.size()+1]; - if(GetShortPathName(makeCommand.c_str(), buffer, - int(makeCommand.size()+1)) != 0) - { - makeCommand = buffer; - } - delete [] buffer;\ + cmSystemTools::GetShortPath(makeCommand.c_str(), makeCommand); } #endif makeCommand += " "; diff --git a/Tests/Complex/CMakeLists.txt b/Tests/Complex/CMakeLists.txt index d5863fa..d4b4a06 100644 --- a/Tests/Complex/CMakeLists.txt +++ b/Tests/Complex/CMakeLists.txt @@ -124,12 +124,12 @@ IF (WIN32) ${Complex_SOURCE_DIR}/Library/dummy "${dir}/${file}" COPYONLY IMMEDIATE) - EXEC_PROGRAM("${CCOMMAND_COMMAND} write_regv \"${hkey}\" \"${dir}\"") + EXEC_PROGRAM(${CCOMMAND_COMMAND} ARGS "write_regv \"${hkey}\" \"${dir}\"") FIND_PATH(REGISTRY_TEST_PATH ${file} "[${hkey}]") - EXEC_PROGRAM("${CCOMMAND_COMMAND} delete_regv \"${hkey}\"") - EXEC_PROGRAM("${CCOMMAND_COMMAND} remove \"${dir}/${file}\"") + EXEC_PROGRAM(${CCOMMAND_COMMAND} ARGS "delete_regv \"${hkey}\"") + EXEC_PROGRAM(${CCOMMAND_COMMAND} ARGS "remove \"${dir}/${file}\"") ENDIF (NOT UNIX) ENDIF (WIN32) diff --git a/Tests/ComplexOneConfig/CMakeLists.txt b/Tests/ComplexOneConfig/CMakeLists.txt index d5863fa..d4b4a06 100644 --- a/Tests/ComplexOneConfig/CMakeLists.txt +++ b/Tests/ComplexOneConfig/CMakeLists.txt @@ -124,12 +124,12 @@ IF (WIN32) ${Complex_SOURCE_DIR}/Library/dummy "${dir}/${file}" COPYONLY IMMEDIATE) - EXEC_PROGRAM("${CCOMMAND_COMMAND} write_regv \"${hkey}\" \"${dir}\"") + EXEC_PROGRAM(${CCOMMAND_COMMAND} ARGS "write_regv \"${hkey}\" \"${dir}\"") FIND_PATH(REGISTRY_TEST_PATH ${file} "[${hkey}]") - EXEC_PROGRAM("${CCOMMAND_COMMAND} delete_regv \"${hkey}\"") - EXEC_PROGRAM("${CCOMMAND_COMMAND} remove \"${dir}/${file}\"") + EXEC_PROGRAM(${CCOMMAND_COMMAND} ARGS "delete_regv \"${hkey}\"") + EXEC_PROGRAM(${CCOMMAND_COMMAND} ARGS "remove \"${dir}/${file}\"") ENDIF (NOT UNIX) ENDIF (WIN32) diff --git a/Tests/ComplexRelativePaths/CMakeLists.txt b/Tests/ComplexRelativePaths/CMakeLists.txt index d5863fa..d4b4a06 100644 --- a/Tests/ComplexRelativePaths/CMakeLists.txt +++ b/Tests/ComplexRelativePaths/CMakeLists.txt @@ -124,12 +124,12 @@ IF (WIN32) ${Complex_SOURCE_DIR}/Library/dummy "${dir}/${file}" COPYONLY IMMEDIATE) - EXEC_PROGRAM("${CCOMMAND_COMMAND} write_regv \"${hkey}\" \"${dir}\"") + EXEC_PROGRAM(${CCOMMAND_COMMAND} ARGS "write_regv \"${hkey}\" \"${dir}\"") FIND_PATH(REGISTRY_TEST_PATH ${file} "[${hkey}]") - EXEC_PROGRAM("${CCOMMAND_COMMAND} delete_regv \"${hkey}\"") - EXEC_PROGRAM("${CCOMMAND_COMMAND} remove \"${dir}/${file}\"") + EXEC_PROGRAM(${CCOMMAND_COMMAND} ARGS "delete_regv \"${hkey}\"") + EXEC_PROGRAM(${CCOMMAND_COMMAND} ARGS "remove \"${dir}/${file}\"") ENDIF (NOT UNIX) ENDIF (WIN32) -- cgit v0.12