cmLoadCommandCommand.cxx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. 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. // create argc and argv and then invoke the command
  98. int argc = static_cast<int> (args.size());
  99. char **argv = 0;
  100. if (argc)
  101. {
  102. argv = (char **)malloc(argc*sizeof(char *));
  103. }
  104. int i;
  105. for (i = 0; i < argc; ++i)
  106. {
  107. argv[i] = strdup(args[i].c_str());
  108. }
  109. int result = info.InitialPass((void *)&info,(void *)this->m_Makefile,argc,argv);
  110. cmFreeArguments(argc,argv);
  111. if (result)
  112. {
  113. return true;
  114. }
  115. return false;
  116. }
  117. void cmLoadedCommand::FinalPass()
  118. {
  119. if (this->info.FinalPass)
  120. {
  121. this->info.FinalPass((void *)&this->info,(void *)this->m_Makefile);
  122. }
  123. }
  124. cmLoadedCommand::~cmLoadedCommand()
  125. {
  126. if (this->info.Destructor)
  127. {
  128. this->info.Destructor((void *)&this->info);
  129. }
  130. }
  131. // cmLoadCommandCommand
  132. bool cmLoadCommandCommand::InitialPass(std::vector<std::string> const& argsIn)
  133. {
  134. if(argsIn.size() < 1 )
  135. {
  136. return true;
  137. }
  138. // the file must exist
  139. std::string fullPath = cmDynamicLoader::LibPrefix();
  140. fullPath += "cm" + argsIn[0] + cmDynamicLoader::LibExtension();
  141. std::vector<std::string> args;
  142. cmSystemTools::ExpandListArguments(argsIn, args);
  143. // search for the file
  144. std::vector<std::string> path;
  145. for (unsigned int j = 1; j < args.size(); j++)
  146. {
  147. // expand variables
  148. std::string exp = args[j];
  149. cmSystemTools::ExpandRegistryValues(exp);
  150. // Glob the entry in case of wildcards.
  151. cmSystemTools::GlobDirs(exp.c_str(), path);
  152. }
  153. // Try to find the program.
  154. fullPath = cmSystemTools::FindFile(fullPath.c_str(), path);
  155. if (fullPath == "")
  156. {
  157. this->SetError("Attempt to load command failed.");
  158. return false;
  159. }
  160. // try loading the shared library / dll
  161. cmLibHandle lib = cmDynamicLoader::OpenLibrary(fullPath.c_str());
  162. if(lib)
  163. {
  164. // Look for the symbol cmLoad, cmGetFactoryCompilerUsed,
  165. // and cmGetFactoryVersion in the library
  166. CM_INIT_FUNCTION initFunction
  167. = (CM_INIT_FUNCTION)
  168. cmDynamicLoader::GetSymbolAddress(lib, "cmInitializeCommand");
  169. if ( !initFunction )
  170. {
  171. initFunction =
  172. (CM_INIT_FUNCTION)(
  173. cmDynamicLoader::GetSymbolAddress(lib, "_cmInitializeCommand"));
  174. }
  175. // if the symbol is found call it to set the name on the
  176. // function blocker
  177. if(initFunction)
  178. {
  179. // create a function blocker and set it up
  180. cmLoadedCommand *f = new cmLoadedCommand();
  181. (*initFunction)(&f->info);
  182. m_Makefile->AddCommand(f);
  183. return true;
  184. }
  185. this->SetError("Attempt to load command failed. "
  186. "No init function found.");
  187. }
  188. return false;
  189. }