cmWorkingDirectory: use the new class

These functions just need to change the directory for a block of code
and then go back to the caller's expected location. Use
cmWorkingDirectory to ensure that all return paths are handled.
This commit is contained in:
Ben Boeckel authored and Brad King committed 2017-03-06 10:38:58 -05:00
1 parent 047a5e4d66
commit aba92ffd92
14 files changed
+86 -146

No files matched your search

+3 -7
View File
@@ -7,6 +7,7 @@
#include "cmCPackLog.h" #include "cmCPackLog.h"
#include "cmGeneratedFileStream.h" #include "cmGeneratedFileStream.h"
#include "cmSystemTools.h" #include "cmSystemTools.h"
#include "cmWorkingDirectory.h"
#include <map> #include <map>
#include <ostream> #include <ostream>
@@ -37,9 +38,8 @@ int cmCPackArchiveGenerator::addOneComponentToArchive(
// Add the files of this component to the archive // Add the files of this component to the archive
std::string localToplevel(this->GetOption("CPACK_TEMPORARY_DIRECTORY")); std::string localToplevel(this->GetOption("CPACK_TEMPORARY_DIRECTORY"));
localToplevel += "/" + component->Name; localToplevel += "/" + component->Name;
std::string dir = cmSystemTools::GetCurrentWorkingDirectory();
// Change to local toplevel // Change to local toplevel
cmSystemTools::ChangeDirectory(localToplevel); cmWorkingDirectory workdir(localToplevel);
std::string filePrefix; std::string filePrefix;
if (this->IsOn("CPACK_COMPONENT_INCLUDE_TOPLEVEL_DIRECTORY")) { if (this->IsOn("CPACK_COMPONENT_INCLUDE_TOPLEVEL_DIRECTORY")) {
filePrefix = this->GetOption("CPACK_PACKAGE_FILE_NAME"); filePrefix = this->GetOption("CPACK_PACKAGE_FILE_NAME");
@@ -64,8 +64,6 @@ int cmCPackArchiveGenerator::addOneComponentToArchive(
return 0; return 0;
} }
} }
// Go back to previous dir
cmSystemTools::ChangeDirectory(dir);
return 1; return 1;
} }
@@ -227,8 +225,7 @@ int cmCPackArchiveGenerator::PackageFiles()
// CASE 3 : NON COMPONENT package. // CASE 3 : NON COMPONENT package.
DECLARE_AND_OPEN_ARCHIVE(packageFileNames[0], archive); DECLARE_AND_OPEN_ARCHIVE(packageFileNames[0], archive);
std::vector<std::string>::const_iterator fileIt; std::vector<std::string>::const_iterator fileIt;
std::string dir = cmSystemTools::GetCurrentWorkingDirectory(); cmWorkingDirectory workdir(toplevel);
cmSystemTools::ChangeDirectory(toplevel);
for (fileIt = files.begin(); fileIt != files.end(); ++fileIt) { for (fileIt = files.begin(); fileIt != files.end(); ++fileIt) {
// Get the relative path to the file // Get the relative path to the file
std::string rp = std::string rp =
@@ -241,7 +238,6 @@ int cmCPackArchiveGenerator::PackageFiles()
return 0; return 0;
} }
} }
cmSystemTools::ChangeDirectory(dir);
// The destructor of cmArchiveWrite will close and finish the write // The destructor of cmArchiveWrite will close and finish the write
return 1; return 1;
} }
+2 -2
View File
@@ -16,6 +16,7 @@
#include "cmGlobalGenerator.h" #include "cmGlobalGenerator.h"
#include "cmMakefile.h" #include "cmMakefile.h"
#include "cmStateSnapshot.h" #include "cmStateSnapshot.h"
#include "cmWorkingDirectory.h"
#include "cmXMLSafe.h" #include "cmXMLSafe.h"
#include "cm_auto_ptr.hxx" #include "cm_auto_ptr.hxx"
#include "cmake.h" #include "cmake.h"
@@ -383,7 +384,7 @@ int cmCPackGenerator::InstallProjectViaInstalledDirectories(
goToDir += "/" + subdir; goToDir += "/" + subdir;
cmCPackLogger(cmCPackLog::LOG_DEBUG, "Change dir to: " << goToDir cmCPackLogger(cmCPackLog::LOG_DEBUG, "Change dir to: " << goToDir
<< std::endl); << std::endl);
cmSystemTools::ChangeDirectory(goToDir); cmWorkingDirectory workdir(goToDir);
for (symlinkedIt = symlinkedFiles.begin(); for (symlinkedIt = symlinkedFiles.begin();
symlinkedIt != symlinkedFiles.end(); ++symlinkedIt) { symlinkedIt != symlinkedFiles.end(); ++symlinkedIt) {
cmCPackLogger(cmCPackLog::LOG_DEBUG, "Will create a symlink: " cmCPackLogger(cmCPackLog::LOG_DEBUG, "Will create a symlink: "
@@ -408,7 +409,6 @@ int cmCPackGenerator::InstallProjectViaInstalledDirectories(
} }
cmCPackLogger(cmCPackLog::LOG_DEBUG, "Going back to: " << curDir cmCPackLogger(cmCPackLog::LOG_DEBUG, "Going back to: " << curDir
<< std::endl); << std::endl);
cmSystemTools::ChangeDirectory(curDir);
} }
} }
} }
+4 -10
View File
@@ -6,6 +6,7 @@
#include "cmCTestTestHandler.h" #include "cmCTestTestHandler.h"
#include "cmGlobalGenerator.h" #include "cmGlobalGenerator.h"
#include "cmSystemTools.h" #include "cmSystemTools.h"
#include "cmWorkingDirectory.h"
#include "cmake.h" #include "cmake.h"
#include <cmsys/Process.h> #include <cmsys/Process.h>
@@ -42,7 +43,7 @@ int cmCTestBuildAndTestHandler::ProcessHandler()
int cmCTestBuildAndTestHandler::RunCMake(std::string* outstring, int cmCTestBuildAndTestHandler::RunCMake(std::string* outstring,
std::ostringstream& out, std::ostringstream& out,
std::string& cmakeOutString, std::string& cmakeOutString,
std::string& cwd, cmake* cm) cmake* cm)
{ {
unsigned int k; unsigned int k;
std::vector<std::string> args; std::vector<std::string> args;
@@ -85,8 +86,6 @@ int cmCTestBuildAndTestHandler::RunCMake(std::string* outstring,
if (cm->Run(args) != 0) { if (cm->Run(args) != 0) {
out << "Error: cmake execution failed\n"; out << "Error: cmake execution failed\n";
out << cmakeOutString << "\n"; out << cmakeOutString << "\n";
// return to the original directory
cmSystemTools::ChangeDirectory(cwd);
if (outstring) { if (outstring) {
*outstring = out.str(); *outstring = out.str();
} else { } else {
@@ -99,8 +98,6 @@ int cmCTestBuildAndTestHandler::RunCMake(std::string* outstring,
if (cm->Run(args) != 0) { if (cm->Run(args) != 0) {
out << "Error: cmake execution failed\n"; out << "Error: cmake execution failed\n";
out << cmakeOutString << "\n"; out << cmakeOutString << "\n";
// return to the original directory
cmSystemTools::ChangeDirectory(cwd);
if (outstring) { if (outstring) {
*outstring = out.str(); *outstring = out.str();
} else { } else {
@@ -199,13 +196,12 @@ int cmCTestBuildAndTestHandler::RunCMakeAndTest(std::string* outstring)
double clock_start = cmSystemTools::GetTime(); double clock_start = cmSystemTools::GetTime();
// make sure the binary dir is there // make sure the binary dir is there
std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
out << "Internal cmake changing into directory: " << this->BinaryDir out << "Internal cmake changing into directory: " << this->BinaryDir
<< std::endl; << std::endl;
if (!cmSystemTools::FileIsDirectory(this->BinaryDir)) { if (!cmSystemTools::FileIsDirectory(this->BinaryDir)) {
cmSystemTools::MakeDirectory(this->BinaryDir.c_str()); cmSystemTools::MakeDirectory(this->BinaryDir.c_str());
} }
cmSystemTools::ChangeDirectory(this->BinaryDir); cmWorkingDirectory workdir(this->BinaryDir);
if (this->BuildNoCMake) { if (this->BuildNoCMake) {
// Make the generator available for the Build call below. // Make the generator available for the Build call below.
@@ -217,7 +213,7 @@ int cmCTestBuildAndTestHandler::RunCMakeAndTest(std::string* outstring)
cm.LoadCache(this->BinaryDir); cm.LoadCache(this->BinaryDir);
} else { } else {
// do the cmake step, no timeout here since it is not a sub process // do the cmake step, no timeout here since it is not a sub process
if (this->RunCMake(outstring, out, cmakeOutString, cwd, &cm)) { if (this->RunCMake(outstring, out, cmakeOutString, &cm)) {
return 1; return 1;
} }
} }
@@ -304,8 +300,6 @@ int cmCTestBuildAndTestHandler::RunCMakeAndTest(std::string* outstring)
} else { } else {
cmCTestLog(this->CTest, ERROR_MESSAGE, out.str()); cmCTestLog(this->CTest, ERROR_MESSAGE, out.str());
} }
// return to the original directory
cmSystemTools::ChangeDirectory(cwd);
return 1; return 1;
} }
+1 -1
View File
@@ -46,7 +46,7 @@ protected:
///! Run CMake and build a test and then run it as a single test. ///! Run CMake and build a test and then run it as a single test.
int RunCMakeAndTest(std::string* output); int RunCMakeAndTest(std::string* output);
int RunCMake(std::string* outstring, std::ostringstream& out, int RunCMake(std::string* outstring, std::ostringstream& out,
std::string& cmakeOutString, std::string& cwd, cmake* cm); std::string& cmakeOutString, cmake* cm);
std::string Output; std::string Output;
+5 -15
View File
@@ -12,6 +12,7 @@
#include "cmParseJacocoCoverage.h" #include "cmParseJacocoCoverage.h"
#include "cmParsePHPCoverage.h" #include "cmParsePHPCoverage.h"
#include "cmSystemTools.h" #include "cmSystemTools.h"
#include "cmWorkingDirectory.h"
#include "cmXMLWriter.h" #include "cmXMLWriter.h"
#include "cmake.h" #include "cmake.h"
@@ -969,9 +970,8 @@ int cmCTestCoverageHandler::HandleGCovCoverage(
std::string testingDir = this->CTest->GetBinaryDir() + "/Testing"; std::string testingDir = this->CTest->GetBinaryDir() + "/Testing";
std::string tempDir = testingDir + "/CoverageInfo"; std::string tempDir = testingDir + "/CoverageInfo";
std::string currentDirectory = cmSystemTools::GetCurrentWorkingDirectory();
cmSystemTools::MakeDirectory(tempDir.c_str()); cmSystemTools::MakeDirectory(tempDir.c_str());
cmSystemTools::ChangeDirectory(tempDir); cmWorkingDirectory workdir(tempDir);
int gcovStyle = 0; int gcovStyle = 0;
@@ -1294,7 +1294,6 @@ int cmCTestCoverageHandler::HandleGCovCoverage(
} }
} }
cmSystemTools::ChangeDirectory(currentDirectory);
return file_count; return file_count;
} }
@@ -1340,7 +1339,6 @@ int cmCTestCoverageHandler::HandleLCovCoverage(
return 0; return 0;
} }
std::string testingDir = this->CTest->GetBinaryDir(); std::string testingDir = this->CTest->GetBinaryDir();
std::string currentDirectory = cmSystemTools::GetCurrentWorkingDirectory();
std::set<std::string> missingFiles; std::set<std::string> missingFiles;
@@ -1362,7 +1360,7 @@ int cmCTestCoverageHandler::HandleLCovCoverage(
cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, "." << std::flush, cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, "." << std::flush,
this->Quiet); this->Quiet);
std::string fileDir = cmSystemTools::GetFilenamePath(*it); std::string fileDir = cmSystemTools::GetFilenamePath(*it);
cmSystemTools::ChangeDirectory(fileDir); cmWorkingDirectory workdir(fileDir);
std::string command = "\"" + lcovCommand + "\" " + lcovExtraFlags + " "; std::string command = "\"" + lcovCommand + "\" " + lcovExtraFlags + " ";
cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT, cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
@@ -1552,7 +1550,6 @@ int cmCTestCoverageHandler::HandleLCovCoverage(
} }
} }
cmSystemTools::ChangeDirectory(currentDirectory);
return file_count; return file_count;
} }
@@ -1591,13 +1588,8 @@ bool cmCTestCoverageHandler::FindLCovFiles(std::vector<std::string>& files)
gl.RecurseOff(); // No need of recurse if -prof_dir${BUILD_DIR} flag is gl.RecurseOff(); // No need of recurse if -prof_dir${BUILD_DIR} flag is
// used while compiling. // used while compiling.
gl.RecurseThroughSymlinksOff(); gl.RecurseThroughSymlinksOff();
std::string prevBinaryDir;
std::string buildDir = this->CTest->GetCTestConfiguration("BuildDirectory"); std::string buildDir = this->CTest->GetCTestConfiguration("BuildDirectory");
if (cmSystemTools::ChangeDirectory(buildDir)) { cmWorkingDirectory workdir(buildDir);
cmCTestLog(this->CTest, ERROR_MESSAGE, "Error changing directory to "
<< buildDir << std::endl);
return false;
}
// Run profmerge to merge all *.dyn files into dpi files // Run profmerge to merge all *.dyn files into dpi files
if (!cmSystemTools::RunSingleCommand("profmerge")) { if (!cmSystemTools::RunSingleCommand("profmerge")) {
@@ -1605,11 +1597,9 @@ bool cmCTestCoverageHandler::FindLCovFiles(std::vector<std::string>& files)
return false; return false;
} }
prevBinaryDir = cmSystemTools::GetCurrentWorkingDirectory();
// DPI file should appear in build directory // DPI file should appear in build directory
std::string daGlob; std::string daGlob;
daGlob = prevBinaryDir; daGlob = buildDir;
daGlob += "/*.dpi"; daGlob += "/*.dpi";
cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT, cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
" looking for dpi files in: " << daGlob << std::endl, " looking for dpi files in: " << daGlob << std::endl,
+2 -3
View File
@@ -6,6 +6,7 @@
#include "cmCTestGenericHandler.h" #include "cmCTestGenericHandler.h"
#include "cmMakefile.h" #include "cmMakefile.h"
#include "cmSystemTools.h" #include "cmSystemTools.h"
#include "cmWorkingDirectory.h"
#include "cmake.h" #include "cmake.h"
#include <sstream> #include <sstream>
@@ -216,8 +217,7 @@ bool cmCTestHandlerCommand::InitialPass(std::vector<std::string> const& args,
handler->SetSubmitIndex(atoi(this->Values[ct_SUBMIT_INDEX])); handler->SetSubmitIndex(atoi(this->Values[ct_SUBMIT_INDEX]));
} }
} }
std::string current_dir = cmSystemTools::GetCurrentWorkingDirectory(); cmWorkingDirectory workdir(
cmSystemTools::ChangeDirectory(
this->CTest->GetCTestConfiguration("BuildDirectory")); this->CTest->GetCTestConfiguration("BuildDirectory"));
int res = handler->ProcessHandler(); int res = handler->ProcessHandler();
if (this->Values[ct_RETURN_VALUE] && *this->Values[ct_RETURN_VALUE]) { if (this->Values[ct_RETURN_VALUE] && *this->Values[ct_RETURN_VALUE]) {
@@ -243,7 +243,6 @@ bool cmCTestHandlerCommand::InitialPass(std::vector<std::string> const& args,
this->Makefile->AddDefinition(this->Values[ct_CAPTURE_CMAKE_ERROR], this->Makefile->AddDefinition(this->Values[ct_CAPTURE_CMAKE_ERROR],
returnString); returnString);
} }
cmSystemTools::ChangeDirectory(current_dir);
return true; return true;
} }
+3 -8
View File
@@ -7,6 +7,7 @@
#include "cmCTestScriptHandler.h" #include "cmCTestScriptHandler.h"
#include "cmCTestTestHandler.h" #include "cmCTestTestHandler.h"
#include "cmSystemTools.h" #include "cmSystemTools.h"
#include "cmWorkingDirectory.h"
#include <algorithm> #include <algorithm>
#include <cmsys/FStream.hxx> #include <cmsys/FStream.hxx>
@@ -138,8 +139,7 @@ void cmCTestMultiProcessHandler::StartTestProcess(int test)
} }
} }
std::string current_dir = cmSystemTools::GetCurrentWorkingDirectory(); cmWorkingDirectory workdir(this->Properties[test]->Directory);
cmSystemTools::ChangeDirectory(this->Properties[test]->Directory);
// Lock the resources we'll be using // Lock the resources we'll be using
this->LockResources(test); this->LockResources(test);
@@ -166,7 +166,6 @@ void cmCTestMultiProcessHandler::StartTestProcess(int test)
this->Failed->push_back(this->Properties[test]->Name); this->Failed->push_back(this->Properties[test]->Name);
delete testRun; delete testRun;
} }
cmSystemTools::ChangeDirectory(current_dir);
} }
void cmCTestMultiProcessHandler::LockResources(int index) void cmCTestMultiProcessHandler::LockResources(int index)
@@ -683,9 +682,7 @@ void cmCTestMultiProcessHandler::PrintTestList()
count++; count++;
cmCTestTestHandler::cmCTestTestProperties& p = *it->second; cmCTestTestHandler::cmCTestTestProperties& p = *it->second;
// push working dir cmWorkingDirectory workdir(p.Directory);
std::string current_dir = cmSystemTools::GetCurrentWorkingDirectory();
cmSystemTools::ChangeDirectory(p.Directory);
cmCTestRunTest testRun(this->TestHandler); cmCTestRunTest testRun(this->TestHandler);
testRun.SetIndex(p.Index); testRun.SetIndex(p.Index);
@@ -724,8 +721,6 @@ void cmCTestMultiProcessHandler::PrintTestList()
cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, " ", this->Quiet); cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, " ", this->Quiet);
cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, p.Name << std::endl, cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, p.Name << std::endl,
this->Quiet); this->Quiet);
// pop working dir
cmSystemTools::ChangeDirectory(current_dir);
} }
cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, std::endl cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, std::endl
+7 -12
View File
@@ -7,6 +7,7 @@
#include "cmCTestTestHandler.h" #include "cmCTestTestHandler.h"
#include "cmProcess.h" #include "cmProcess.h"
#include "cmSystemTools.h" #include "cmSystemTools.h"
#include "cmWorkingDirectory.h"
#include <cmConfigure.h> #include <cmConfigure.h>
#include <cm_curl.h> #include <cm_curl.h>
@@ -270,14 +271,11 @@ bool cmCTestRunTest::EndTest(size_t completed, size_t total, bool started)
*this->TestHandler->LogFile << "Test time = " << buf << std::endl; *this->TestHandler->LogFile << "Test time = " << buf << std::endl;
} }
// Set the working directory to the tests directory // Set the working directory to the tests directory to process Dart files.
std::string oldpath = cmSystemTools::GetCurrentWorkingDirectory(); {
cmSystemTools::ChangeDirectory(this->TestProperties->Directory); cmWorkingDirectory workdir(this->TestProperties->Directory);
this->DartProcessing();
this->DartProcessing(); }
// restore working directory
cmSystemTools::ChangeDirectory(oldpath);
// if this is doing MemCheck then all the output needs to be put into // if this is doing MemCheck then all the output needs to be put into
// Output since that is what is parsed by cmCTestMemCheckHandler // Output since that is what is parsed by cmCTestMemCheckHandler
@@ -356,11 +354,8 @@ bool cmCTestRunTest::StartAgain()
} }
this->RunAgain = false; // reset this->RunAgain = false; // reset
// change to tests directory // change to tests directory
std::string current_dir = cmSystemTools::GetCurrentWorkingDirectory(); cmWorkingDirectory workdir(this->TestProperties->Directory);
cmSystemTools::ChangeDirectory(this->TestProperties->Directory);
this->StartTest(this->TotalNumberOfTests); this->StartTest(this->TotalNumberOfTests);
// change back
cmSystemTools::ChangeDirectory(current_dir);
return true; return true;
} }
+3 -10
View File
@@ -19,6 +19,7 @@
#include "cmState.h" #include "cmState.h"
#include "cmSystemTools.h" #include "cmSystemTools.h"
#include "cmThirdParty.h" #include "cmThirdParty.h"
#include "cmWorkingDirectory.h"
#include "cmXMLParser.h" #include "cmXMLParser.h"
#include "cmake.h" #include "cmake.h"
@@ -1519,7 +1520,6 @@ int cmCTestSubmitHandler::ProcessHandler()
#endif #endif
} else if (dropMethod == "scp") { } else if (dropMethod == "scp") {
std::string url; std::string url;
std::string oldWorkingDirectory;
if (!this->CTest->GetCTestConfiguration("DropSiteUser").empty()) { if (!this->CTest->GetCTestConfiguration("DropSiteUser").empty()) {
url += this->CTest->GetCTestConfiguration("DropSiteUser") + "@"; url += this->CTest->GetCTestConfiguration("DropSiteUser") + "@";
} }
@@ -1528,19 +1528,16 @@ int cmCTestSubmitHandler::ProcessHandler()
// change to the build directory so that we can uses a relative path // change to the build directory so that we can uses a relative path
// on windows since scp dosn't support "c:" a drive in the path // on windows since scp dosn't support "c:" a drive in the path
oldWorkingDirectory = cmSystemTools::GetCurrentWorkingDirectory(); cmWorkingDirectory workdir(buildDirectory);
cmSystemTools::ChangeDirectory(buildDirectory);
if (!this->SubmitUsingSCP(this->CTest->GetCTestConfiguration("ScpCommand"), if (!this->SubmitUsingSCP(this->CTest->GetCTestConfiguration("ScpCommand"),
"Testing/" + this->CTest->GetCurrentTag(), files, "Testing/" + this->CTest->GetCurrentTag(), files,
prefix, url)) { prefix, url)) {
cmSystemTools::ChangeDirectory(oldWorkingDirectory);
cmCTestLog(this->CTest, ERROR_MESSAGE, cmCTestLog(this->CTest, ERROR_MESSAGE,
" Problems when submitting via SCP" << std::endl); " Problems when submitting via SCP" << std::endl);
ofs << " Problems when submitting via SCP" << std::endl; ofs << " Problems when submitting via SCP" << std::endl;
return -1; return -1;
} }
cmSystemTools::ChangeDirectory(oldWorkingDirectory);
cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
" Submission successful" << std::endl, this->Quiet); " Submission successful" << std::endl, this->Quiet);
ofs << " Submission successful" << std::endl; ofs << " Submission successful" << std::endl;
@@ -1550,22 +1547,18 @@ int cmCTestSubmitHandler::ProcessHandler()
// change to the build directory so that we can uses a relative path // change to the build directory so that we can uses a relative path
// on windows since scp dosn't support "c:" a drive in the path // on windows since scp dosn't support "c:" a drive in the path
std::string oldWorkingDirectory = cmWorkingDirectory workdir(buildDirectory);
cmSystemTools::GetCurrentWorkingDirectory();
cmSystemTools::ChangeDirectory(buildDirectory);
cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT, cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
" Change directory: " << buildDirectory << std::endl, " Change directory: " << buildDirectory << std::endl,
this->Quiet); this->Quiet);
if (!this->SubmitUsingCP("Testing/" + this->CTest->GetCurrentTag(), files, if (!this->SubmitUsingCP("Testing/" + this->CTest->GetCurrentTag(), files,
prefix, location)) { prefix, location)) {
cmSystemTools::ChangeDirectory(oldWorkingDirectory);
cmCTestLog(this->CTest, ERROR_MESSAGE, cmCTestLog(this->CTest, ERROR_MESSAGE,
" Problems when submitting via CP" << std::endl); " Problems when submitting via CP" << std::endl);
ofs << " Problems when submitting via cp" << std::endl; ofs << " Problems when submitting via cp" << std::endl;
return -1; return -1;
} }
cmSystemTools::ChangeDirectory(oldWorkingDirectory);
cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
" Submission successful" << std::endl, this->Quiet); " Submission successful" << std::endl, this->Quiet);
ofs << " Submission successful" << std::endl; ofs << " Submission successful" << std::endl;
+35 -35
View File
@@ -27,6 +27,7 @@
#include "cmState.h" #include "cmState.h"
#include "cmStateSnapshot.h" #include "cmStateSnapshot.h"
#include "cmSystemTools.h" #include "cmSystemTools.h"
#include "cmWorkingDirectory.h"
#include "cmXMLWriter.h" #include "cmXMLWriter.h"
#include "cm_auto_ptr.hxx" #include "cm_auto_ptr.hxx"
#include "cm_utf8.h" #include "cm_utf8.h"
@@ -86,22 +87,24 @@ bool cmCTestSubdirCommand::InitialPass(std::vector<std::string> const& args,
// No subdirectory? So what... // No subdirectory? So what...
continue; continue;
} }
cmSystemTools::ChangeDirectory(fname); bool readit = false;
const char* testFilename; {
if (cmSystemTools::FileExists("CTestTestfile.cmake")) { cmWorkingDirectory workdir(fname);
// does the CTestTestfile.cmake exist ? const char* testFilename;
testFilename = "CTestTestfile.cmake"; if (cmSystemTools::FileExists("CTestTestfile.cmake")) {
} else if (cmSystemTools::FileExists("DartTestfile.txt")) { // does the CTestTestfile.cmake exist ?
// does the DartTestfile.txt exist ? testFilename = "CTestTestfile.cmake";
testFilename = "DartTestfile.txt"; } else if (cmSystemTools::FileExists("DartTestfile.txt")) {
} else { // does the DartTestfile.txt exist ?
// No CTestTestfile? Who cares... testFilename = "DartTestfile.txt";
continue; } else {
// No CTestTestfile? Who cares...
continue;
}
fname += "/";
fname += testFilename;
readit = this->Makefile->ReadDependentFile(fname.c_str());
} }
fname += "/";
fname += testFilename;
bool readit = this->Makefile->ReadDependentFile(fname.c_str());
cmSystemTools::ChangeDirectory(cwd);
if (!readit) { if (!readit) {
std::string m = "Could not find include file: "; std::string m = "Could not find include file: ";
m += fname; m += fname;
@@ -109,7 +112,6 @@ bool cmCTestSubdirCommand::InitialPass(std::vector<std::string> const& args,
return false; return false;
} }
} }
cmSystemTools::ChangeDirectory(cwd);
return true; return true;
} }
@@ -149,9 +151,7 @@ bool cmCTestAddSubdirectoryCommand::InitialPass(
return false; return false;
} }
std::string cwd = cmSystemTools::GetCurrentWorkingDirectory(); std::string fname = cmSystemTools::GetCurrentWorkingDirectory();
cmSystemTools::ChangeDirectory(cwd);
std::string fname = cwd;
fname += "/"; fname += "/";
fname += args[0]; fname += args[0];
@@ -159,23 +159,23 @@ bool cmCTestAddSubdirectoryCommand::InitialPass(
// No subdirectory? So what... // No subdirectory? So what...
return true; return true;
} }
cmSystemTools::ChangeDirectory(fname); bool readit = false;
const char* testFilename; {
if (cmSystemTools::FileExists("CTestTestfile.cmake")) { const char* testFilename;
// does the CTestTestfile.cmake exist ? if (cmSystemTools::FileExists("CTestTestfile.cmake")) {
testFilename = "CTestTestfile.cmake"; // does the CTestTestfile.cmake exist ?
} else if (cmSystemTools::FileExists("DartTestfile.txt")) { testFilename = "CTestTestfile.cmake";
// does the DartTestfile.txt exist ? } else if (cmSystemTools::FileExists("DartTestfile.txt")) {
testFilename = "DartTestfile.txt"; // does the DartTestfile.txt exist ?
} else { testFilename = "DartTestfile.txt";
// No CTestTestfile? Who cares... } else {
cmSystemTools::ChangeDirectory(cwd); // No CTestTestfile? Who cares...
return true; return true;
}
fname += "/";
fname += testFilename;
readit = this->Makefile->ReadDependentFile(fname.c_str());
} }
fname += "/";
fname += testFilename;
bool readit = this->Makefile->ReadDependentFile(fname.c_str());
cmSystemTools::ChangeDirectory(cwd);
if (!readit) { if (!readit) {
std::string m = "Could not find include file: "; std::string m = "Could not find include file: ";
m += fname; m += fname;
+2 -12
View File
@@ -7,6 +7,7 @@
#include "cmLocalGenerator.h" #include "cmLocalGenerator.h"
#include "cmMakefile.h" #include "cmMakefile.h"
#include "cmSystemTools.h" #include "cmSystemTools.h"
#include "cmWorkingDirectory.h"
#include <cmsys/FStream.hxx> #include <cmsys/FStream.hxx>
#include <sstream> #include <sstream>
@@ -75,13 +76,7 @@ bool cmDepends::Check(const char* makeFile, const char* internalFile,
std::map<std::string, DependencyVector>& validDeps) std::map<std::string, DependencyVector>& validDeps)
{ {
// Dependency checks must be done in proper working directory. // Dependency checks must be done in proper working directory.
std::string oldcwd = "."; cmWorkingDirectory workdir(this->CompileDirectory);
if (this->CompileDirectory != ".") {
// Get the CWD but do not call CollapseFullPath because
// we only need it to cd back, and the form does not matter
oldcwd = cmSystemTools::GetCurrentWorkingDirectory(false);
cmSystemTools::ChangeDirectory(this->CompileDirectory);
}
// Check whether dependencies must be regenerated. // Check whether dependencies must be regenerated.
bool okay = true; bool okay = true;
@@ -93,11 +88,6 @@ bool cmDepends::Check(const char* makeFile, const char* internalFile,
okay = false; okay = false;
} }
// Restore working directory.
if (oldcwd != ".") {
cmSystemTools::ChangeDirectory(oldcwd);
}
return okay; return okay;
} }
+2 -7
View File
@@ -42,6 +42,7 @@
#include "cmStateDirectory.h" #include "cmStateDirectory.h"
#include "cmStateTypes.h" #include "cmStateTypes.h"
#include "cmVersion.h" #include "cmVersion.h"
#include "cmWorkingDirectory.h"
#include "cmake.h" #include "cmake.h"
#if defined(CMAKE_BUILD_WITH_CMAKE) #if defined(CMAKE_BUILD_WITH_CMAKE)
@@ -1763,8 +1764,7 @@ int cmGlobalGenerator::Build(const std::string& /*unused*/,
/** /**
* Run an executable command and put the stdout in output. * Run an executable command and put the stdout in output.
*/ */
std::string cwd = cmSystemTools::GetCurrentWorkingDirectory(); cmWorkingDirectory workdir(bindir);
cmSystemTools::ChangeDirectory(bindir);
output += "Change Dir: "; output += "Change Dir: ";
output += bindir; output += bindir;
output += "\n"; output += "\n";
@@ -1804,8 +1804,6 @@ int cmGlobalGenerator::Build(const std::string& /*unused*/,
output += *outputPtr; output += *outputPtr;
output += "\nGenerator: execution of make clean failed.\n"; output += "\nGenerator: execution of make clean failed.\n";
// return to the original directory
cmSystemTools::ChangeDirectory(cwd);
return 1; return 1;
} }
output += *outputPtr; output += *outputPtr;
@@ -1828,8 +1826,6 @@ int cmGlobalGenerator::Build(const std::string& /*unused*/,
output += "\nGenerator: execution of make failed. Make command was: " + output += "\nGenerator: execution of make failed. Make command was: " +
makeCommandStr + "\n"; makeCommandStr + "\n";
// return to the original directory
cmSystemTools::ChangeDirectory(cwd);
return 1; return 1;
} }
output += *outputPtr; output += *outputPtr;
@@ -1842,7 +1838,6 @@ int cmGlobalGenerator::Build(const std::string& /*unused*/,
retVal = 1; retVal = 1;
} }
cmSystemTools::ChangeDirectory(cwd);
return retVal; return retVal;
} }
+2 -9
View File
@@ -36,6 +36,7 @@
#include "cmTest.h" #include "cmTest.h"
#include "cmTestGenerator.h" // IWYU pragma: keep #include "cmTestGenerator.h" // IWYU pragma: keep
#include "cmVersion.h" #include "cmVersion.h"
#include "cmWorkingDirectory.h"
#include "cm_auto_ptr.hxx" #include "cm_auto_ptr.hxx"
#include "cmake.h" #include "cmake.h"
@@ -3147,8 +3148,7 @@ int cmMakefile::TryCompile(const std::string& srcdir,
// change to the tests directory and run cmake // change to the tests directory and run cmake
// use the cmake object instead of calling cmake // use the cmake object instead of calling cmake
std::string cwd = cmSystemTools::GetCurrentWorkingDirectory(); cmWorkingDirectory workdir(bindir);
cmSystemTools::ChangeDirectory(bindir);
// make sure the same generator is used // make sure the same generator is used
// use this program as the cmake to be run, it should not // use this program as the cmake to be run, it should not
@@ -3162,8 +3162,6 @@ int cmMakefile::TryCompile(const std::string& srcdir,
this->GetGlobalGenerator()->GetName() + this->GetGlobalGenerator()->GetName() +
"' could not be created."); "' could not be created.");
cmSystemTools::SetFatalErrorOccured(); cmSystemTools::SetFatalErrorOccured();
// return to the original directory
cmSystemTools::ChangeDirectory(cwd);
this->IsSourceFileTryCompile = false; this->IsSourceFileTryCompile = false;
return 1; return 1;
} }
@@ -3227,8 +3225,6 @@ int cmMakefile::TryCompile(const std::string& srcdir,
this->IssueMessage(cmake::FATAL_ERROR, this->IssueMessage(cmake::FATAL_ERROR,
"Failed to configure test project build system."); "Failed to configure test project build system.");
cmSystemTools::SetFatalErrorOccured(); cmSystemTools::SetFatalErrorOccured();
// return to the original directory
cmSystemTools::ChangeDirectory(cwd);
this->IsSourceFileTryCompile = false; this->IsSourceFileTryCompile = false;
return 1; return 1;
} }
@@ -3237,8 +3233,6 @@ int cmMakefile::TryCompile(const std::string& srcdir,
this->IssueMessage(cmake::FATAL_ERROR, this->IssueMessage(cmake::FATAL_ERROR,
"Failed to generate test project build system."); "Failed to generate test project build system.");
cmSystemTools::SetFatalErrorOccured(); cmSystemTools::SetFatalErrorOccured();
// return to the original directory
cmSystemTools::ChangeDirectory(cwd);
this->IsSourceFileTryCompile = false; this->IsSourceFileTryCompile = false;
return 1; return 1;
} }
@@ -3247,7 +3241,6 @@ int cmMakefile::TryCompile(const std::string& srcdir,
int ret = this->GetGlobalGenerator()->TryCompile( int ret = this->GetGlobalGenerator()->TryCompile(
srcdir, bindir, projectName, targetName, fast, output, this); srcdir, bindir, projectName, targetName, fast, output, this);
cmSystemTools::ChangeDirectory(cwd);
this->IsSourceFileTryCompile = false; this->IsSourceFileTryCompile = false;
return ret; return ret;
} }
+15 -15
View File
@@ -23,6 +23,7 @@
#include "cmTargetLinkLibraryType.h" #include "cmTargetLinkLibraryType.h"
#include "cmUtils.hxx" #include "cmUtils.hxx"
#include "cmVersionConfig.h" #include "cmVersionConfig.h"
#include "cmWorkingDirectory.h"
#include "cm_auto_ptr.hxx" #include "cm_auto_ptr.hxx"
#if defined(CMAKE_BUILD_WITH_CMAKE) #if defined(CMAKE_BUILD_WITH_CMAKE)
@@ -2199,24 +2200,23 @@ int cmake::GetSystemInformation(std::vector<std::string>& args)
resultFile += "/__cmake_systeminformation/results.txt"; resultFile += "/__cmake_systeminformation/results.txt";
} }
// now run cmake on the CMakeLists file {
cmSystemTools::ChangeDirectory(destPath); // now run cmake on the CMakeLists file
std::vector<std::string> args2; cmWorkingDirectory workdir(destPath);
args2.push_back(args[0]); std::vector<std::string> args2;
args2.push_back(destPath); args2.push_back(args[0]);
std::string resultArg = "-DRESULT_FILE="; args2.push_back(destPath);
resultArg += resultFile; std::string resultArg = "-DRESULT_FILE=";
args2.push_back(resultArg); resultArg += resultFile;
int res = this->Run(args2, false); args2.push_back(resultArg);
int res = this->Run(args2, false);
if (res != 0) { if (res != 0) {
std::cerr << "Error: --system-information failed on internal CMake!\n"; std::cerr << "Error: --system-information failed on internal CMake!\n";
return res; return res;
}
} }
// change back to the original directory
cmSystemTools::ChangeDirectory(cwd);
// echo results to stdout if needed // echo results to stdout if needed
if (writeToStdout) { if (writeToStdout) {
FILE* fin = cmsys::SystemTools::Fopen(resultFile, "r"); FILE* fin = cmsys::SystemTools::Fopen(resultFile, "r");