cmFunctionCommand.cxx 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. // set the values for ARGV0 ARGV1 ...
  97. for (unsigned int t = 0; t < expandedArgs.size(); ++t)
  98. {
  99. cmOStringStream tmpStream;
  100. tmpStream << "ARGV" << t;
  101. this->Makefile->AddDefinition(tmpStream.str().c_str(),
  102. expandedArgs[t].c_str());
  103. }
  104. // define the formal arguments
  105. for (unsigned int j = 1; j < this->Args.size(); ++j)
  106. {
  107. this->Makefile->AddDefinition(this->Args[j].c_str(),
  108. expandedArgs[j-1].c_str());
  109. }
  110. // define ARGV and ARGN
  111. std::vector<std::string>::const_iterator eit;
  112. std::string argvDef;
  113. std::string argnDef;
  114. unsigned int cnt = 0;
  115. for ( eit = expandedArgs.begin(); eit != expandedArgs.end(); ++eit )
  116. {
  117. if ( argvDef.size() > 0 )
  118. {
  119. argvDef += ";";
  120. }
  121. argvDef += *eit;
  122. if ( cnt >= this->Args.size()-1 )
  123. {
  124. if ( argnDef.size() > 0 )
  125. {
  126. argnDef += ";";
  127. }
  128. argnDef += *eit;
  129. }
  130. cnt ++;
  131. }
  132. this->Makefile->AddDefinition("ARGV", argvDef.c_str());
  133. this->Makefile->AddDefinition("ARGN", argnDef.c_str());
  134. // Invoke all the functions that were collected in the block.
  135. // for each function
  136. for(unsigned int c = 0; c < this->Functions.size(); ++c)
  137. {
  138. cmExecutionStatus status;
  139. if (!this->Makefile->ExecuteCommand(this->Functions[c],status) ||
  140. status.GetNestedError())
  141. {
  142. // The error message should have already included the call stack
  143. // so we do not need to report an error here.
  144. lexScope.Quiet();
  145. polScope.Quiet();
  146. inStatus.SetNestedError(true);
  147. return false;
  148. }
  149. if (status.GetReturnInvoked())
  150. {
  151. return true;
  152. }
  153. }
  154. // pop scope on the makefile
  155. return true;
  156. }
  157. bool cmFunctionFunctionBlocker::
  158. IsFunctionBlocked(const cmListFileFunction& lff, cmMakefile &mf,
  159. cmExecutionStatus &)
  160. {
  161. // record commands until we hit the ENDFUNCTION
  162. // at the ENDFUNCTION call we shift gears and start looking for invocations
  163. if(!cmSystemTools::Strucmp(lff.Name.c_str(),"function"))
  164. {
  165. this->Depth++;
  166. }
  167. else if(!cmSystemTools::Strucmp(lff.Name.c_str(),"endfunction"))
  168. {
  169. // if this is the endfunction for this function then execute
  170. if (!this->Depth)
  171. {
  172. std::string name = this->Args[0];
  173. std::vector<std::string>::size_type cc;
  174. name += "(";
  175. for ( cc = 0; cc < this->Args.size(); cc ++ )
  176. {
  177. name += " " + this->Args[cc];
  178. }
  179. name += " )";
  180. // create a new command and add it to cmake
  181. cmFunctionHelperCommand *f = new cmFunctionHelperCommand();
  182. f->Args = this->Args;
  183. f->Functions = this->Functions;
  184. mf.RecordPolicies(f->Policies);
  185. // Set the FilePath on the arguments to match the function since it is
  186. // not stored and the original values may be freed
  187. for (unsigned int i = 0; i < f->Functions.size(); ++i)
  188. {
  189. for (unsigned int j = 0; j < f->Functions[i].Arguments.size(); ++j)
  190. {
  191. f->Functions[i].Arguments[j].FilePath =
  192. f->Functions[i].FilePath.c_str();
  193. }
  194. }
  195. std::string newName = "_" + this->Args[0];
  196. mf.GetCMakeInstance()->RenameCommand(this->Args[0].c_str(),
  197. newName.c_str());
  198. mf.AddCommand(f);
  199. // remove the function blocker now that the function is defined
  200. mf.RemoveFunctionBlocker(this, lff);
  201. return true;
  202. }
  203. else
  204. {
  205. // decrement for each nested function that ends
  206. this->Depth--;
  207. }
  208. }
  209. // if it wasn't an endfunction and we are not executing then we must be
  210. // recording
  211. this->Functions.push_back(lff);
  212. return true;
  213. }
  214. bool cmFunctionFunctionBlocker::
  215. ShouldRemove(const cmListFileFunction& lff, cmMakefile &mf)
  216. {
  217. if(!cmSystemTools::Strucmp(lff.Name.c_str(),"endfunction"))
  218. {
  219. std::vector<std::string> expandedArguments;
  220. mf.ExpandArguments(lff.Arguments, expandedArguments);
  221. // if the endfunction has arguments then make sure
  222. // they match the ones in the openeing function command
  223. if ((expandedArguments.empty() ||
  224. (expandedArguments[0] == this->Args[0])))
  225. {
  226. return true;
  227. }
  228. }
  229. return false;
  230. }
  231. bool cmFunctionCommand
  232. ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
  233. {
  234. if(args.size() < 1)
  235. {
  236. this->SetError("called with incorrect number of arguments");
  237. return false;
  238. }
  239. // create a function blocker
  240. cmFunctionFunctionBlocker *f = new cmFunctionFunctionBlocker();
  241. for(std::vector<std::string>::const_iterator j = args.begin();
  242. j != args.end(); ++j)
  243. {
  244. f->Args.push_back(*j);
  245. }
  246. this->Makefile->AddFunctionBlocker(f);
  247. return true;
  248. }