cmVariableWatchCommand.cxx 4.7 KB

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