VS: Use setlocal/endlocal only in VS 10 custom commands

The setlocal/endlocal and errorlevel pattern added by commit 06fcbc47
(VS10: Fix working directory of consecutive custom commands, 2011-04-08)
does not work well in VS 7.1.  Restore the original behavior for VS
versions that do not need the new behavior.
This commit is contained in:
Brad King 2011-04-11 10:07:40 -04:00
parent 06fcbc4757
commit b98fdd5284
3 changed files with 39 additions and 13 deletions

View File

@ -39,6 +39,7 @@ public:
protected: protected:
virtual const char* ReportErrorLabel() const; virtual const char* ReportErrorLabel() const;
virtual bool CustomCommandUseLocal() const { return true; }
private: private:
}; };

View File

@ -170,29 +170,47 @@ std::string
cmLocalVisualStudioGenerator cmLocalVisualStudioGenerator
::ConstructScript(cmCustomCommand const& cc, ::ConstructScript(cmCustomCommand const& cc,
const char* configName, const char* configName,
const char* newline) const char* newline_text)
{ {
bool useLocal = this->CustomCommandUseLocal();
const cmCustomCommandLines& commandLines = cc.GetCommandLines(); const cmCustomCommandLines& commandLines = cc.GetCommandLines();
const char* workingDirectory = cc.GetWorkingDirectory(); const char* workingDirectory = cc.GetWorkingDirectory();
cmCustomCommandGenerator ccg(cc, configName, this->Makefile); cmCustomCommandGenerator ccg(cc, configName, this->Makefile);
RelativeRoot relativeRoot = workingDirectory? NONE : START_OUTPUT; RelativeRoot relativeRoot = workingDirectory? NONE : START_OUTPUT;
// Avoid leading or trailing newlines.
const char* newline = "";
// Line to check for error between commands. // Line to check for error between commands.
std::string check_error = newline; std::string check_error = newline_text;
if(useLocal)
{
check_error += "if %errorlevel% neq 0 goto :cmEnd"; check_error += "if %errorlevel% neq 0 goto :cmEnd";
}
else
{
check_error += "if errorlevel 1 goto ";
check_error += this->GetReportErrorLabel();
}
// Store the script in a string. // Store the script in a string.
std::string script; std::string script;
// Open a local context. // Open a local context.
if(useLocal)
{
script += newline;
newline = newline_text;
script += "set errlev="; script += "set errlev=";
script += newline; script += newline;
script += "setlocal"; script += "setlocal";
}
if(workingDirectory) if(workingDirectory)
{ {
// Change the working directory. // Change the working directory.
script += newline; script += newline;
newline = newline_text;
script += "cd "; script += "cd ";
script += this->Convert(workingDirectory, FULL, SHELL); script += this->Convert(workingDirectory, FULL, SHELL);
script += check_error; script += check_error;
@ -201,6 +219,7 @@ cmLocalVisualStudioGenerator
if(workingDirectory[0] && workingDirectory[1] == ':') if(workingDirectory[0] && workingDirectory[1] == ':')
{ {
script += newline; script += newline;
newline = newline_text;
script += workingDirectory[0]; script += workingDirectory[0];
script += workingDirectory[1]; script += workingDirectory[1];
script += check_error; script += check_error;
@ -216,6 +235,7 @@ cmLocalVisualStudioGenerator
if(extraPath) if(extraPath)
{ {
script += newline; script += newline;
newline = newline_text;
script += "set PATH="; script += "set PATH=";
script += extraPath; script += extraPath;
script += ";%PATH%"; script += ";%PATH%";
@ -227,6 +247,7 @@ cmLocalVisualStudioGenerator
{ {
// Start a new line. // Start a new line.
script += newline; script += newline;
newline = newline_text;
// Add this command line. // Add this command line.
std::string cmd = ccg.GetCommand(c); std::string cmd = ccg.GetCommand(c);
@ -241,6 +262,8 @@ cmLocalVisualStudioGenerator
} }
// Close the local context. // Close the local context.
if(useLocal)
{
script += newline; script += newline;
script += ":cmEnd"; script += ":cmEnd";
script += newline; script += newline;
@ -248,6 +271,7 @@ cmLocalVisualStudioGenerator
script += newline; script += newline;
script += "if %errlev% neq 0 goto "; script += "if %errlev% neq 0 goto ";
script += this->GetReportErrorLabel(); script += this->GetReportErrorLabel();
}
return script; return script;
} }

View File

@ -43,6 +43,7 @@ public:
protected: protected:
virtual const char* ReportErrorLabel() const; virtual const char* ReportErrorLabel() const;
virtual bool CustomCommandUseLocal() const { return false; }
/** Construct a custom command to make exe import lib dir. */ /** Construct a custom command to make exe import lib dir. */
cmsys::auto_ptr<cmCustomCommand> cmsys::auto_ptr<cmCustomCommand>