1
0

cmLoadCommandCommand.cxx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Insight Consortium. All rights reserved.
  8. See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm 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. newC->m_commandName = this->m_commandName;
  35. memcpy(&newC->info,&this->info,sizeof(info));
  36. return newC;
  37. }
  38. /**
  39. * This is called when the command is first encountered in
  40. * the CMakeLists.txt file.
  41. */
  42. virtual bool InitialPass(std::vector<std::string> const& args);
  43. /**
  44. * This is called at the end after all the information
  45. * specified by the command is accumulated. Most commands do
  46. * not implement this method. At this point, reading and
  47. * writing to the cache can be done.
  48. */
  49. virtual void FinalPass();
  50. /**
  51. * This determines if the command gets propagated down
  52. * to makefiles located in subdirectories.
  53. */
  54. virtual bool IsInherited() {
  55. return (info.m_Inherited != 0 ? true : false);
  56. }
  57. /**
  58. * The name of the command as specified in CMakeList.txt.
  59. */
  60. virtual const char* GetName() {
  61. return m_commandName.c_str();
  62. }
  63. /**
  64. * Succinct documentation.
  65. */
  66. virtual const char* GetTerseDocumentation()
  67. {
  68. if (this->info.GetTerseDocumentation)
  69. {
  70. return info.GetTerseDocumentation();
  71. }
  72. else
  73. {
  74. return "LoadedCommand without any additional documentation";
  75. }
  76. }
  77. /**
  78. * More documentation.
  79. */
  80. virtual const char* GetFullDocumentation()
  81. {
  82. if (this->info.GetFullDocumentation)
  83. {
  84. return info.GetFullDocumentation();
  85. }
  86. else
  87. {
  88. return "LoadedCommand without any additional documentation";
  89. }
  90. }
  91. cmTypeMacro(cmLoadedCommand, cmCommand);
  92. cmLoadedCommandInfo info;
  93. std::string m_commandName;
  94. };
  95. bool cmLoadedCommand::InitialPass(std::vector<std::string> const& args)
  96. {
  97. if (!info.InitialPass)
  98. {
  99. return true;
  100. }
  101. // create argc and argv and then invoke the command
  102. int argc = static_cast<int> (args.size());
  103. char **argv = 0;
  104. if (argc)
  105. {
  106. argv = (char **)malloc(argc*sizeof(char *));
  107. }
  108. int i;
  109. for (i = 0; i < argc; ++i)
  110. {
  111. argv[i] = strdup(args[i].c_str());
  112. }
  113. int result = info.InitialPass((void *)&info,(void *)this->m_Makefile,argc,argv);
  114. cmFreeArguments(argc,argv);
  115. if (result)
  116. {
  117. return true;
  118. }
  119. return false;
  120. }
  121. void cmLoadedCommand::FinalPass()
  122. {
  123. if (this->info.FinalPass)
  124. {
  125. this->info.FinalPass((void *)&this->info,(void *)this->m_Makefile);
  126. }
  127. }
  128. cmLoadedCommand::~cmLoadedCommand()
  129. {
  130. if (this->info.Destructor)
  131. {
  132. this->info.Destructor((void *)&this->info);
  133. }
  134. }
  135. // cmLoadCommandCommand
  136. bool cmLoadCommandCommand::InitialPass(std::vector<std::string> const& argsIn)
  137. {
  138. if(argsIn.size() < 1 )
  139. {
  140. return true;
  141. }
  142. // the file must exist
  143. std::string fullPath = cmDynamicLoader::LibPrefix();
  144. fullPath += "cm" + argsIn[0] + cmDynamicLoader::LibExtension();
  145. std::vector<std::string> args;
  146. cmSystemTools::ExpandListArguments(argsIn, args);
  147. // search for the file
  148. std::vector<std::string> path;
  149. for (unsigned int j = 1; j < args.size(); j++)
  150. {
  151. // expand variables
  152. std::string exp = args[j];
  153. cmSystemTools::ExpandRegistryValues(exp);
  154. // Glob the entry in case of wildcards.
  155. cmSystemTools::GlobDirs(exp.c_str(), path);
  156. }
  157. // Try to find the program.
  158. fullPath = cmSystemTools::FindFile(fullPath.c_str(), path);
  159. if (fullPath == "")
  160. {
  161. this->SetError("Attempt to load command failed.");
  162. return false;
  163. }
  164. // try loading the shared library / dll
  165. cmLibHandle lib = cmDynamicLoader::OpenLibrary(fullPath.c_str());
  166. if(lib)
  167. {
  168. // Look for the symbol cmLoad, cmGetFactoryCompilerUsed,
  169. // and cmGetFactoryVersion in the library
  170. CM_NAME_FUNCTION nameFunction
  171. = (CM_NAME_FUNCTION)
  172. cmDynamicLoader::GetSymbolAddress(lib, "cmGetName");
  173. if ( !nameFunction )
  174. {
  175. nameFunction =
  176. (CM_NAME_FUNCTION)(
  177. cmDynamicLoader::GetSymbolAddress(lib, "_cmGetName"));
  178. }
  179. CM_INIT_FUNCTION initFunction
  180. = (CM_INIT_FUNCTION)
  181. cmDynamicLoader::GetSymbolAddress(lib, "cmInitializeCommand");
  182. if ( !initFunction )
  183. {
  184. initFunction =
  185. (CM_INIT_FUNCTION)(
  186. cmDynamicLoader::GetSymbolAddress(lib, "_cmInitializeCommand"));
  187. }
  188. // if the symbol is found call it to set the name on the
  189. // function blocker
  190. if(nameFunction)
  191. {
  192. // create a function blocker and set it up
  193. cmLoadedCommand *f = new cmLoadedCommand();
  194. f->m_commandName = (*nameFunction)();
  195. if (!initFunction)
  196. {
  197. this->SetError("Attempt to load command failed. "
  198. "No init function found.");
  199. return false;
  200. }
  201. (*initFunction)(&f->info);
  202. m_Makefile->AddCommand(f);
  203. return true;
  204. }
  205. }
  206. return false;
  207. }