1
0

cmFunctionCommand.cxx 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmFunctionCommand.h"
  11. #include "cmake.h"
  12. // define the class for function commands
  13. class cmFunctionHelperCommand : public cmCommand
  14. {
  15. public:
  16. cmFunctionHelperCommand() {}
  17. ///! clean up any memory allocated by the function
  18. ~cmFunctionHelperCommand() {};
  19. /**
  20. * This is a virtual constructor for the command.
  21. */
  22. virtual cmCommand* Clone()
  23. {
  24. cmFunctionHelperCommand *newC = new cmFunctionHelperCommand;
  25. // we must copy when we clone
  26. newC->Args = this->Args;
  27. newC->Functions = this->Functions;
  28. newC->Policies = this->Policies;
  29. return newC;
  30. }
  31. /**
  32. * This determines if the command is invoked when in script mode.
  33. */
  34. virtual bool IsScriptable() { return true; }
  35. /**
  36. * This is called when the command is first encountered in
  37. * the CMakeLists.txt file.
  38. */
  39. virtual bool InvokeInitialPass(const std::vector<cmListFileArgument>& args,
  40. cmExecutionStatus &);
  41. virtual bool InitialPass(std::vector<std::string> const&,
  42. cmExecutionStatus &) { 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. cmPolicies::PolicyMap Policies;
  67. };
  68. bool cmFunctionHelperCommand::InvokeInitialPass
  69. (const std::vector<cmListFileArgument>& args,
  70. cmExecutionStatus & inStatus)
  71. {
  72. // Expand the argument list to the function.
  73. std::vector<std::string> expandedArgs;
  74. this->Makefile->ExpandArguments(args, expandedArgs);
  75. // make sure the number of arguments passed is at least the number
  76. // required by the signature
  77. if (expandedArgs.size() < this->Args.size() - 1)
  78. {
  79. std::string errorMsg =
  80. "Function invoked with incorrect arguments for function named: ";
  81. errorMsg += this->Args[0];
  82. this->SetError(errorMsg.c_str());
  83. return false;
  84. }
  85. // we push a scope on the makefile
  86. cmMakefile::LexicalPushPop lexScope(this->Makefile);
  87. cmMakefile::ScopePushPop varScope(this->Makefile);
  88. static_cast<void>(varScope);
  89. // Push a weak policy scope which restores the policies recorded at
  90. // function creation.
  91. cmMakefile::PolicyPushPop polScope(this->Makefile, true, this->Policies);
  92. // set the value of argc
  93. cmOStringStream strStream;
  94. strStream << expandedArgs.size();
  95. this->Makefile->AddDefinition("ARGC",strStream.str().c_str());
  96. this->Makefile->MarkVariableAsUsed("ARGC");
  97. // set the values for ARGV0 ARGV1 ...
  98. for (unsigned int t = 0; t < expandedArgs.size(); ++t)
  99. {
  100. cmOStringStream tmpStream;
  101. tmpStream << "ARGV" << t;
  102. this->Makefile->AddDefinition(tmpStream.str().c_str(),
  103. expandedArgs[t].c_str());
  104. this->Makefile->MarkVariableAsUsed(tmpStream.str().c_str());
  105. }
  106. // define the formal arguments
  107. for (unsigned int j = 1; j < this->Args.size(); ++j)
  108. {
  109. this->Makefile->AddDefinition(this->Args[j].c_str(),
  110. expandedArgs[j-1].c_str());
  111. }
  112. // define ARGV and ARGN
  113. std::vector<std::string>::const_iterator eit;
  114. std::string argvDef;
  115. std::string argnDef;
  116. unsigned int cnt = 0;
  117. for ( eit = expandedArgs.begin(); eit != expandedArgs.end(); ++eit )
  118. {
  119. if ( argvDef.size() > 0 )
  120. {
  121. argvDef += ";";
  122. }
  123. argvDef += *eit;
  124. if ( cnt >= this->Args.size()-1 )
  125. {
  126. if ( argnDef.size() > 0 )
  127. {
  128. argnDef += ";";
  129. }
  130. argnDef += *eit;
  131. }
  132. cnt ++;
  133. }
  134. this->Makefile->AddDefinition("ARGV", argvDef.c_str());
  135. this->Makefile->MarkVariableAsUsed("ARGV");
  136. this->Makefile->AddDefinition("ARGN", argnDef.c_str());
  137. this->Makefile->MarkVariableAsUsed("ARGN");
  138. // Invoke all the functions that were collected in the block.
  139. // for each function
  140. for(unsigned int c = 0; c < this->Functions.size(); ++c)
  141. {
  142. cmExecutionStatus status;
  143. if (!this->Makefile->ExecuteCommand(this->Functions[c],status) ||
  144. status.GetNestedError())
  145. {
  146. // The error message should have already included the call stack
  147. // so we do not need to report an error here.
  148. lexScope.Quiet();
  149. polScope.Quiet();
  150. inStatus.SetNestedError(true);
  151. return false;
  152. }
  153. if (status.GetReturnInvoked())
  154. {
  155. return true;
  156. }
  157. }
  158. // pop scope on the makefile
  159. return true;
  160. }
  161. bool cmFunctionFunctionBlocker::
  162. IsFunctionBlocked(const cmListFileFunction& lff, cmMakefile &mf,
  163. cmExecutionStatus &)
  164. {
  165. // record commands until we hit the ENDFUNCTION
  166. // at the ENDFUNCTION call we shift gears and start looking for invocations
  167. if(!cmSystemTools::Strucmp(lff.Name.c_str(),"function"))
  168. {
  169. this->Depth++;
  170. }
  171. else if(!cmSystemTools::Strucmp(lff.Name.c_str(),"endfunction"))
  172. {
  173. // if this is the endfunction for this function then execute
  174. if (!this->Depth)
  175. {
  176. std::string name = this->Args[0];
  177. std::vector<std::string>::size_type cc;
  178. name += "(";
  179. for ( cc = 0; cc < this->Args.size(); cc ++ )
  180. {
  181. name += " " + this->Args[cc];
  182. }
  183. name += " )";
  184. // create a new command and add it to cmake
  185. cmFunctionHelperCommand *f = new cmFunctionHelperCommand();
  186. f->Args = this->Args;
  187. f->Functions = this->Functions;
  188. mf.RecordPolicies(f->Policies);
  189. // Set the FilePath on the arguments to match the function since it is
  190. // not stored and the original values may be freed
  191. for (unsigned int i = 0; i < f->Functions.size(); ++i)
  192. {
  193. for (unsigned int j = 0; j < f->Functions[i].Arguments.size(); ++j)
  194. {
  195. f->Functions[i].Arguments[j].FilePath =
  196. f->Functions[i].FilePath.c_str();
  197. }
  198. }
  199. std::string newName = "_" + this->Args[0];
  200. mf.GetCMakeInstance()->RenameCommand(this->Args[0].c_str(),
  201. newName.c_str());
  202. mf.AddCommand(f);
  203. // remove the function blocker now that the function is defined
  204. mf.RemoveFunctionBlocker(this, lff);
  205. return true;
  206. }
  207. else
  208. {
  209. // decrement for each nested function that ends
  210. this->Depth--;
  211. }
  212. }
  213. // if it wasn't an endfunction and we are not executing then we must be
  214. // recording
  215. this->Functions.push_back(lff);
  216. return true;
  217. }
  218. bool cmFunctionFunctionBlocker::
  219. ShouldRemove(const cmListFileFunction& lff, cmMakefile &mf)
  220. {
  221. if(!cmSystemTools::Strucmp(lff.Name.c_str(),"endfunction"))
  222. {
  223. std::vector<std::string> expandedArguments;
  224. mf.ExpandArguments(lff.Arguments, expandedArguments);
  225. // if the endfunction has arguments then make sure
  226. // they match the ones in the openeing function command
  227. if ((expandedArguments.empty() ||
  228. (expandedArguments[0] == this->Args[0])))
  229. {
  230. return true;
  231. }
  232. }
  233. return false;
  234. }
  235. bool cmFunctionCommand
  236. ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
  237. {
  238. if(args.size() < 1)
  239. {
  240. this->SetError("called with incorrect number of arguments");
  241. return false;
  242. }
  243. // create a function blocker
  244. cmFunctionFunctionBlocker *f = new cmFunctionFunctionBlocker();
  245. for(std::vector<std::string>::const_iterator j = args.begin();
  246. j != args.end(); ++j)
  247. {
  248. f->Args.push_back(*j);
  249. }
  250. this->Makefile->AddFunctionBlocker(f);
  251. return true;
  252. }