1 /*============================================================================
2 CMake - Cross Platform Makefile Generator
3 Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
5 Distributed under the OSI-approved BSD License (the "License");
6 see accompanying file Copyright.txt for details.
8 This software is distributed WITHOUT ANY WARRANTY; without even the
9 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the License for more information.
11 ============================================================================*/
12 #include "cmVariableWatchCommand.h"
14 #include "cmVariableWatch.h"
16 //----------------------------------------------------------------------------
17 static void cmVariableWatchCommandVariableAccessed(
18 const std::string& variable, int access_type, void* client_data,
19 const char* newValue, const cmMakefile* mf)
21 cmVariableWatchCommand* command
22 = static_cast<cmVariableWatchCommand*>(client_data);
23 command->VariableAccessed(variable, access_type, newValue, mf);
26 //----------------------------------------------------------------------------
27 cmVariableWatchCommand::cmVariableWatchCommand()
29 this->InCallback = false;
32 //----------------------------------------------------------------------------
33 bool cmVariableWatchCommand
34 ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
36 if ( args.size() < 1 )
38 this->SetError("must be called with at least one argument.");
41 std::string variable = args[0];
42 if ( args.size() > 1 )
44 std::string command = args[1];
45 this->Handlers[variable].Commands.push_back(args[1]);
47 if ( variable == "CMAKE_CURRENT_LIST_FILE" )
50 ostr << "cannot be set on the variable: " << variable.c_str();
51 this->SetError(ostr.str().c_str());
55 this->Makefile->GetCMakeInstance()->GetVariableWatch()->AddWatch(
56 variable, cmVariableWatchCommandVariableAccessed, this);
61 //----------------------------------------------------------------------------
62 void cmVariableWatchCommand::VariableAccessed(const std::string& variable,
63 int access_type, const char* newValue, const cmMakefile* mf)
65 if ( this->InCallback )
69 this->InCallback = true;
71 cmListFileFunction newLFF;
72 cmVariableWatchCommandHandler *handler = &this->Handlers[variable];
73 cmVariableWatchCommandHandler::VectorOfCommands::iterator it;
74 cmListFileArgument arg;
75 bool processed = false;
76 const char* accessString = cmVariableWatch::GetAccessAsString(access_type);
77 const char* currentListFile = mf->GetDefinition("CMAKE_CURRENT_LIST_FILE");
80 cmMakefile* makefile = const_cast<cmMakefile*>(mf);
82 std::string stack = makefile->GetProperty("LISTFILE_STACK");
83 for ( it = handler->Commands.begin(); it != handler->Commands.end();
86 std::string command = *it;
87 newLFF.Arguments.clear();
88 newLFF.Arguments.push_back(
89 cmListFileArgument(variable, true, "unknown", 9999));
90 newLFF.Arguments.push_back(
91 cmListFileArgument(accessString, true, "unknown", 9999));
92 newLFF.Arguments.push_back(
93 cmListFileArgument(newValue?newValue:"", true, "unknown", 9999));
94 newLFF.Arguments.push_back(
95 cmListFileArgument(currentListFile, true, "unknown", 9999));
96 newLFF.Arguments.push_back(
97 cmListFileArgument(stack, true, "unknown", 9999));
98 newLFF.Name = command;
99 newLFF.FilePath = "Some weird path";
101 cmExecutionStatus status;
102 if(!makefile->ExecuteCommand(newLFF,status))
104 arg.FilePath = "Unknown";
106 cmOStringStream error;
107 error << "Error in cmake code at\n"
108 << arg.FilePath << ":" << arg.Line << ":\n"
109 << "A command failed during the invocation of callback\""
111 cmSystemTools::Error(error.str().c_str());
112 this->InCallback = false;
120 msg << "* Variable \"" << variable.c_str() << "\" was accessed using "
121 << accessString << " in: " << currentListFile << std::endl;
122 msg << " The value of the variable: \"" << newValue << "\"" << std::endl;
123 msg << " The list file stack: " << stack.c_str();
124 cmSystemTools::Message(msg.str().c_str());
125 std::vector<std::string> vars = makefile->GetDefinitions();
126 cmOStringStream msg2;
128 for ( cc = 0; cc < vars.size(); cc ++ )
130 if ( vars[cc] == variable )
134 msg2 << vars[cc] << " = \""
135 << makefile->GetDefinition(vars[cc].c_str()) << "\"" << std::endl;
137 //cmSystemTools::Message(msg2.str().c_str());
139 this->InCallback = false;