Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Source / cmVariableWatchCommand.cxx
blob6f912cd88a73f43d98cf57fa0d04ac3b44f14919
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmVariableWatchCommand.cxx,v $
5 Language: C++
6 Date: $Date: 2008/01/23 15:27:59 $
7 Version: $Revision: 1.3 $
9 Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
10 See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
12 This software is distributed WITHOUT ANY WARRANTY; without even
13 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 PURPOSE. See the above copyright notices for more information.
16 =========================================================================*/
17 #include "cmVariableWatchCommand.h"
19 #include "cmVariableWatch.h"
21 //----------------------------------------------------------------------------
22 static void cmVariableWatchCommandVariableAccessed(
23 const std::string& variable, int access_type, void* client_data,
24 const char* newValue, const cmMakefile* mf)
26 cmVariableWatchCommand* command
27 = static_cast<cmVariableWatchCommand*>(client_data);
28 command->VariableAccessed(variable, access_type, newValue, mf);
31 //----------------------------------------------------------------------------
32 cmVariableWatchCommand::cmVariableWatchCommand()
34 this->InCallback = false;
37 //----------------------------------------------------------------------------
38 bool cmVariableWatchCommand
39 ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
41 if ( args.size() < 1 )
43 this->SetError("must be called with at least one argument.");
44 return false;
46 std::string variable = args[0];
47 if ( args.size() > 1 )
49 std::string command = args[1];
50 this->Handlers[variable].Commands.push_back(args[1]);
52 if ( variable == "CMAKE_CURRENT_LIST_FILE" )
54 cmOStringStream ostr;
55 ostr << "cannot be set on the variable: " << variable.c_str();
56 this->SetError(ostr.str().c_str());
57 return false;
60 this->Makefile->GetCMakeInstance()->GetVariableWatch()->AddWatch(
61 variable, cmVariableWatchCommandVariableAccessed, this);
63 return true;
66 //----------------------------------------------------------------------------
67 void cmVariableWatchCommand::VariableAccessed(const std::string& variable,
68 int access_type, const char* newValue, const cmMakefile* mf)
70 if ( this->InCallback )
72 return;
74 this->InCallback = true;
76 cmListFileFunction newLFF;
77 cmVariableWatchCommandHandler *handler = &this->Handlers[variable];
78 cmVariableWatchCommandHandler::VectorOfCommands::iterator it;
79 cmListFileArgument arg;
80 bool processed = false;
81 const char* accessString = cmVariableWatch::GetAccessAsString(access_type);
82 const char* currentListFile = mf->GetDefinition("CMAKE_CURRENT_LIST_FILE");
84 /// Ultra bad!!
85 cmMakefile* makefile = const_cast<cmMakefile*>(mf);
87 std::string stack = makefile->GetProperty("LISTFILE_STACK");
88 for ( it = handler->Commands.begin(); it != handler->Commands.end();
89 ++ it )
91 std::string command = *it;
92 newLFF.Arguments.clear();
93 newLFF.Arguments.push_back(
94 cmListFileArgument(variable, true, "unknown", 9999));
95 newLFF.Arguments.push_back(
96 cmListFileArgument(accessString, true, "unknown", 9999));
97 newLFF.Arguments.push_back(
98 cmListFileArgument(newValue?newValue:"", true, "unknown", 9999));
99 newLFF.Arguments.push_back(
100 cmListFileArgument(currentListFile, true, "unknown", 9999));
101 newLFF.Arguments.push_back(
102 cmListFileArgument(stack, true, "unknown", 9999));
103 newLFF.Name = command;
104 newLFF.FilePath = "Some weird path";
105 newLFF.Line = 9999;
106 cmExecutionStatus status;
107 if(!makefile->ExecuteCommand(newLFF,status))
109 arg.FilePath = "Unknown";
110 arg.Line = 0;
111 cmOStringStream error;
112 error << "Error in cmake code at\n"
113 << arg.FilePath << ":" << arg.Line << ":\n"
114 << "A command failed during the invocation of callback\""
115 << command << "\".";
116 cmSystemTools::Error(error.str().c_str());
117 this->InCallback = false;
118 return;
120 processed = true;
122 if ( !processed )
124 cmOStringStream msg;
125 msg << "* Variable \"" << variable.c_str() << "\" was accessed using "
126 << accessString << " in: " << currentListFile << std::endl;
127 msg << " The value of the variable: \"" << newValue << "\"" << std::endl;
128 msg << " The list file stack: " << stack.c_str();
129 cmSystemTools::Message(msg.str().c_str());
130 std::vector<std::string> vars = makefile->GetDefinitions();
131 cmOStringStream msg2;
132 size_t cc;
133 for ( cc = 0; cc < vars.size(); cc ++ )
135 if ( vars[cc] == variable )
137 continue;
139 msg2 << vars[cc] << " = \""
140 << makefile->GetDefinition(vars[cc].c_str()) << "\"" << std::endl;
142 //cmSystemTools::Message(msg2.str().c_str());
144 this->InCallback = false;