cmLoadCommandCommand.cxx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 "cmLoadCommandCommand.h"
  14. #include "cmCPluginAPI.h"
  15. #include "cmCPluginAPI.cxx"
  16. #include "cmDynamicLoader.h"
  17. // a class for loadabple commands
  18. class cmLoadedCommand : public cmCommand
  19. {
  20. public:
  21. cmLoadedCommand() {
  22. memset(&this->info,0,sizeof(this->info));
  23. this->info.CAPI = &cmStaticCAPI;
  24. }
  25. ///! clean up any memory allocated by the plugin
  26. ~cmLoadedCommand();
  27. /**
  28. * This is a virtual constructor for the command.
  29. */
  30. virtual cmCommand* Clone()
  31. {
  32. cmLoadedCommand *newC = new cmLoadedCommand;
  33. // we must copy when we clone
  34. memcpy(&newC->info,&this->info,sizeof(info));
  35. return newC;
  36. }
  37. /**
  38. * This is called when the command is first encountered in
  39. * the CMakeLists.txt file.
  40. */
  41. virtual bool InitialPass(std::vector<std::string> const& args);
  42. /**
  43. * This is called at the end after all the information
  44. * specified by the command is accumulated. Most commands do
  45. * not implement this method. At this point, reading and
  46. * writing to the cache can be done.
  47. */
  48. virtual void FinalPass();
  49. /**
  50. * This determines if the command gets propagated down
  51. * to makefiles located in subdirectories.
  52. */
  53. virtual bool IsInherited() {
  54. return (info.m_Inherited != 0 ? true : false);
  55. }
  56. /**
  57. * The name of the command as specified in CMakeList.txt.
  58. */
  59. virtual const char* GetName() { return info.Name; }
  60. /**
  61. * Succinct documentation.
  62. */
  63. virtual const char* GetTerseDocumentation()
  64. {
  65. if (this->info.GetTerseDocumentation)
  66. {
  67. return info.GetTerseDocumentation();
  68. }
  69. else
  70. {
  71. return "LoadedCommand without any additional documentation";
  72. }
  73. }
  74. /**
  75. * More documentation.
  76. */
  77. virtual const char* GetFullDocumentation()
  78. {
  79. if (this->info.GetFullDocumentation)
  80. {
  81. return info.GetFullDocumentation();
  82. }
  83. else
  84. {
  85. return "LoadedCommand without any additional documentation";
  86. }
  87. }
  88. cmTypeMacro(cmLoadedCommand, cmCommand);
  89. cmLoadedCommandInfo info;
  90. };
  91. bool cmLoadedCommand::InitialPass(std::vector<std::string> const& args)
  92. {
  93. if (!info.InitialPass)
  94. {
  95. return true;
  96. }
  97. // clear the error string
  98. if (this->info.Error)
  99. {
  100. free(this->info.Error);
  101. }
  102. // create argc and argv and then invoke the command
  103. int argc = static_cast<int> (args.size());
  104. char **argv = 0;
  105. if (argc)
  106. {
  107. argv = (char **)malloc(argc*sizeof(char *));
  108. }
  109. int i;
  110. for (i = 0; i < argc; ++i)
  111. {
  112. argv[i] = strdup(args[i].c_str());
  113. }
  114. int result = info.InitialPass((void *)&info,(void *)this->m_Makefile,argc,argv);
  115. cmFreeArguments(argc,argv);
  116. if (result)
  117. {
  118. return true;
  119. }
  120. /* Initial Pass must have failed so set the error string */
  121. if (this->info.Error)
  122. {
  123. this->SetError(this->info.Error);
  124. }
  125. return false;
  126. }
  127. void cmLoadedCommand::FinalPass()
  128. {
  129. if (this->info.FinalPass)
  130. {
  131. this->info.FinalPass((void *)&this->info,(void *)this->m_Makefile);
  132. }
  133. }
  134. cmLoadedCommand::~cmLoadedCommand()
  135. {
  136. if (this->info.Destructor)
  137. {
  138. this->info.Destructor((void *)&this->info);
  139. }
  140. if (this->info.Error)
  141. {
  142. free(this->info.Error);
  143. }
  144. }
  145. // cmLoadCommandCommand
  146. bool cmLoadCommandCommand::InitialPass(std::vector<std::string> const& args)
  147. {
  148. if(args.size() < 1 )
  149. {
  150. return true;
  151. }
  152. // the file must exist
  153. std::string fullPath = cmDynamicLoader::LibPrefix();
  154. fullPath += "cm" + args[0] + cmDynamicLoader::LibExtension();
  155. // search for the file
  156. std::vector<std::string> path;
  157. for (unsigned int j = 1; j < args.size(); j++)
  158. {
  159. // expand variables
  160. std::string exp = args[j];
  161. cmSystemTools::ExpandRegistryValues(exp);
  162. // Glob the entry in case of wildcards.
  163. cmSystemTools::GlobDirs(exp.c_str(), path);
  164. }
  165. // Try to find the program.
  166. fullPath = cmSystemTools::FindFile(fullPath.c_str(), path);
  167. if (fullPath == "")
  168. {
  169. fullPath = "Attempt to load command failed from file : ";
  170. fullPath += cmDynamicLoader::LibPrefix();
  171. fullPath += "cm" + args[0] + cmDynamicLoader::LibExtension();
  172. this->SetError(fullPath.c_str());
  173. return false;
  174. }
  175. // try loading the shared library / dll
  176. cmLibHandle lib = cmDynamicLoader::OpenLibrary(fullPath.c_str());
  177. if(!lib)
  178. {
  179. std::string err = "Attempt to load the library ";
  180. err += fullPath + " failed. Additional error info is:\n";
  181. err += cmDynamicLoader::LastError();
  182. this->SetError(err.c_str());
  183. return false;
  184. }
  185. // find the init function
  186. std::string initFuncName = args[0] + "Init";
  187. CM_INIT_FUNCTION initFunction
  188. = (CM_INIT_FUNCTION)
  189. cmDynamicLoader::GetSymbolAddress(lib, initFuncName.c_str());
  190. if ( !initFunction )
  191. {
  192. initFuncName = "_";
  193. initFuncName += args[0];
  194. initFuncName += "Init";
  195. initFunction = (CM_INIT_FUNCTION)(
  196. cmDynamicLoader::GetSymbolAddress(lib, initFuncName.c_str()));
  197. }
  198. // if the symbol is found call it to set the name on the
  199. // function blocker
  200. if(initFunction)
  201. {
  202. // create a function blocker and set it up
  203. cmLoadedCommand *f = new cmLoadedCommand();
  204. (*initFunction)(&f->info);
  205. m_Makefile->AddCommand(f);
  206. return true;
  207. }
  208. this->SetError("Attempt to load command failed. "
  209. "No init function found.");
  210. return false;
  211. }