cmFunctionCommand.cxx 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 "cmFunctionCommand.h"
  14. #include "cmake.h"
  15. // define the class for function commands
  16. class cmFunctionHelperCommand : public cmCommand
  17. {
  18. public:
  19. cmFunctionHelperCommand() {}
  20. ///! clean up any memory allocated by the function
  21. ~cmFunctionHelperCommand() {};
  22. /**
  23. * This is a virtual constructor for the command.
  24. */
  25. virtual cmCommand* Clone()
  26. {
  27. cmFunctionHelperCommand *newC = new cmFunctionHelperCommand;
  28. // we must copy when we clone
  29. newC->Args = this->Args;
  30. newC->Functions = this->Functions;
  31. return newC;
  32. }
  33. /**
  34. * This determines if the command is invoked when in script mode.
  35. */
  36. virtual bool IsScriptable() { return true; }
  37. /**
  38. * This is called when the command is first encountered in
  39. * the CMakeLists.txt file.
  40. */
  41. virtual bool InvokeInitialPass(const std::vector<cmListFileArgument>& args);
  42. virtual bool InitialPass(std::vector<std::string> const&) { return false; };
  43. /**
  44. * The name of the command as specified in CMakeList.txt.
  45. */
  46. virtual const char* GetName() { return this->Args[0].c_str(); }
  47. /**
  48. * Succinct documentation.
  49. */
  50. virtual const char* GetTerseDocumentation()
  51. {
  52. std::string docs = "Function named: ";
  53. docs += this->GetName();
  54. return docs.c_str();
  55. }
  56. /**
  57. * More documentation.
  58. */
  59. virtual const char* GetFullDocumentation()
  60. {
  61. return this->GetTerseDocumentation();
  62. }
  63. cmTypeMacro(cmFunctionHelperCommand, cmCommand);
  64. std::vector<std::string> Args;
  65. std::vector<cmListFileFunction> Functions;
  66. };
  67. bool cmFunctionHelperCommand::InvokeInitialPass
  68. (const std::vector<cmListFileArgument>& args)
  69. {
  70. // Expand the argument list to the function.
  71. std::vector<std::string> expandedArgs;
  72. this->Makefile->ExpandArguments(args, expandedArgs);
  73. // make sure the number of arguments passed is at least the number
  74. // required by the signature
  75. if (expandedArgs.size() < this->Args.size() - 1)
  76. {
  77. std::string errorMsg =
  78. "Function invoked with incorrect arguments for function named: ";
  79. errorMsg += this->Args[0];
  80. this->SetError(errorMsg.c_str());
  81. return false;
  82. }
  83. // we push a scope on the makefile
  84. this->Makefile->PushScope();
  85. // set the value of argc
  86. cmOStringStream strStream;
  87. strStream << expandedArgs.size();
  88. this->Makefile->AddDefinition("ARGC",strStream.str().c_str());
  89. // set the values for ARGV0 ARGV1 ...
  90. for (unsigned int t = 0; t < expandedArgs.size(); ++t)
  91. {
  92. cmOStringStream tmpStream;
  93. tmpStream << "ARGV" << t;
  94. this->Makefile->AddDefinition(tmpStream.str().c_str(),
  95. expandedArgs[t].c_str());
  96. }
  97. // define the formal arguments
  98. for (unsigned int j = 1; j < this->Args.size(); ++j)
  99. {
  100. this->Makefile->AddDefinition(this->Args[j].c_str(),
  101. expandedArgs[j-1].c_str());
  102. }
  103. // define ARGV and ARGN
  104. std::vector<std::string>::const_iterator eit;
  105. std::string argvDef;
  106. std::string argnDef;
  107. unsigned int cnt = 0;
  108. for ( eit = expandedArgs.begin(); eit != expandedArgs.end(); ++eit )
  109. {
  110. if ( argvDef.size() > 0 )
  111. {
  112. argvDef += ";";
  113. }
  114. argvDef += *eit;
  115. if ( cnt >= this->Args.size()-1 )
  116. {
  117. if ( argnDef.size() > 0 )
  118. {
  119. argnDef += ";";
  120. }
  121. argnDef += *eit;
  122. }
  123. cnt ++;
  124. }
  125. this->Makefile->AddDefinition("ARGV", argvDef.c_str());
  126. this->Makefile->AddDefinition("ARGN", argnDef.c_str());
  127. // Invoke all the functions that were collected in the block.
  128. cmListFileFunction newLFF;
  129. // for each function
  130. for(unsigned int c = 0; c < this->Functions.size(); ++c)
  131. {
  132. if (!this->Makefile->ExecuteCommand(this->Functions[c]))
  133. {
  134. cmOStringStream error;
  135. error << "Error in cmake code at\n"
  136. << this->Functions[c].FilePath << ":"
  137. << this->Functions[c].Line << ":\n"
  138. << "A command failed during the invocation of function \""
  139. << this->Args[0].c_str() << "\".";
  140. cmSystemTools::Error(error.str().c_str());
  141. // pop scope on the makefile and return
  142. this->Makefile->PopScope();
  143. return false;
  144. }
  145. }
  146. // pop scope on the makefile
  147. this->Makefile->PopScope();
  148. return true;
  149. }
  150. bool cmFunctionFunctionBlocker::
  151. IsFunctionBlocked(const cmListFileFunction& lff, cmMakefile &mf)
  152. {
  153. // record commands until we hit the ENDFUNCTION
  154. // at the ENDFUNCTION call we shift gears and start looking for invocations
  155. if(!cmSystemTools::Strucmp(lff.Name.c_str(),"function"))
  156. {
  157. this->Depth++;
  158. }
  159. else if(!cmSystemTools::Strucmp(lff.Name.c_str(),"endfunction"))
  160. {
  161. // if this is the endfunction for this function then execute
  162. if (!this->Depth)
  163. {
  164. std::string name = this->Args[0];
  165. std::vector<std::string>::size_type cc;
  166. name += "(";
  167. for ( cc = 0; cc < this->Args.size(); cc ++ )
  168. {
  169. name += " " + this->Args[cc];
  170. }
  171. name += " )";
  172. // create a new command and add it to cmake
  173. cmFunctionHelperCommand *f = new cmFunctionHelperCommand();
  174. f->Args = this->Args;
  175. f->Functions = this->Functions;
  176. std::string newName = "_" + this->Args[0];
  177. mf.GetCMakeInstance()->RenameCommand(this->Args[0].c_str(),
  178. newName.c_str());
  179. mf.AddCommand(f);
  180. // remove the function blocker now that the function is defined
  181. mf.RemoveFunctionBlocker(lff);
  182. return true;
  183. }
  184. else
  185. {
  186. // decrement for each nested function that ends
  187. this->Depth--;
  188. }
  189. }
  190. // if it wasn't an endfunction and we are not executing then we must be
  191. // recording
  192. this->Functions.push_back(lff);
  193. return true;
  194. }
  195. bool cmFunctionFunctionBlocker::
  196. ShouldRemove(const cmListFileFunction& lff, cmMakefile &mf)
  197. {
  198. if(!cmSystemTools::Strucmp(lff.Name.c_str(),"endfunction"))
  199. {
  200. std::vector<std::string> expandedArguments;
  201. mf.ExpandArguments(lff.Arguments, expandedArguments);
  202. if ((!expandedArguments.empty() &&
  203. (expandedArguments[0] == this->Args[0]))
  204. || cmSystemTools::IsOn
  205. (mf.GetPropertyOrDefinition("CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS")))
  206. {
  207. return true;
  208. }
  209. }
  210. return false;
  211. }
  212. void cmFunctionFunctionBlocker::
  213. ScopeEnded(cmMakefile &mf)
  214. {
  215. // functions should end with an EndFunction
  216. cmSystemTools::Error(
  217. "The end of a CMakeLists file was reached with a FUNCTION statement that "
  218. "was not closed properly. Within the directory: ",
  219. mf.GetCurrentDirectory(), " with function ",
  220. this->Args[0].c_str());
  221. }
  222. bool cmFunctionCommand::InitialPass(std::vector<std::string> const& args)
  223. {
  224. if(args.size() < 1)
  225. {
  226. this->SetError("called with incorrect number of arguments");
  227. return false;
  228. }
  229. // create a function blocker
  230. cmFunctionFunctionBlocker *f = new cmFunctionFunctionBlocker();
  231. for(std::vector<std::string>::const_iterator j = args.begin();
  232. j != args.end(); ++j)
  233. {
  234. f->Args.push_back(*j);
  235. }
  236. this->Makefile->AddFunctionBlocker(f);
  237. return true;
  238. }