cmLoadCommandCommand.cxx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. /**
  26. * This is a virtual constructor for the command.
  27. */
  28. virtual cmCommand* Clone()
  29. {
  30. cmLoadedCommand *newC = new cmLoadedCommand;
  31. // we must copy when we clone
  32. newC->m_commandName = this->m_commandName;
  33. memcpy(&newC->info,&this->info,sizeof(info));
  34. return newC;
  35. }
  36. /**
  37. * This is called when the command is first encountered in
  38. * the CMakeLists.txt file.
  39. */
  40. virtual bool InitialPass(std::vector<std::string> const& args);
  41. /**
  42. * This is called at the end after all the information
  43. * specified by the command is accumulated. Most commands do
  44. * not implement this method. At this point, reading and
  45. * writing to the cache can be done.
  46. */
  47. virtual void FinalPass();
  48. /**
  49. * This determines if the command gets propagated down
  50. * to makefiles located in subdirectories.
  51. */
  52. virtual bool IsInherited() {
  53. return (info.m_Inherited != 0 ? true : false);
  54. }
  55. /**
  56. * The name of the command as specified in CMakeList.txt.
  57. */
  58. virtual const char* GetName() {
  59. return m_commandName.c_str();
  60. }
  61. /**
  62. * Succinct documentation.
  63. */
  64. virtual const char* GetTerseDocumentation()
  65. {
  66. if (this->info.GetTerseDocumentation)
  67. {
  68. return info.GetTerseDocumentation();
  69. }
  70. else
  71. {
  72. return "LoadedCommand without any additional documentation";
  73. }
  74. }
  75. /**
  76. * More documentation.
  77. */
  78. virtual const char* GetFullDocumentation()
  79. {
  80. if (this->info.GetFullDocumentation)
  81. {
  82. return info.GetFullDocumentation();
  83. }
  84. else
  85. {
  86. return "LoadedCommand without any additional documentation";
  87. }
  88. }
  89. cmTypeMacro(cmLoadedCommand, cmCommand);
  90. cmLoadedCommandInfo info;
  91. std::string m_commandName;
  92. };
  93. bool cmLoadedCommand::InitialPass(std::vector<std::string> const& args)
  94. {
  95. if (!info.InitialPass)
  96. {
  97. return true;
  98. }
  99. // create argc and argv and then invoke the command
  100. int argc = static_cast<int> (args.size());
  101. char **argv = 0;
  102. if (argc)
  103. {
  104. argv = (char **)malloc(argc*sizeof(char *));
  105. }
  106. int i;
  107. for (i = 0; i < argc; ++i)
  108. {
  109. argv[i] = strdup(args[i].c_str());
  110. }
  111. int result = info.InitialPass((void *)&info,(void *)this->m_Makefile,argc,argv);
  112. cmFreeArguments(argc,argv);
  113. if (result)
  114. {
  115. return true;
  116. }
  117. return false;
  118. }
  119. void cmLoadedCommand::FinalPass()
  120. {
  121. if (this->info.FinalPass)
  122. {
  123. this->info.FinalPass((void *)&this->info,(void *)this->m_Makefile);
  124. }
  125. }
  126. // cmLoadCommandCommand
  127. bool cmLoadCommandCommand::InitialPass(std::vector<std::string> const& argsIn)
  128. {
  129. if(argsIn.size() < 1 )
  130. {
  131. return true;
  132. }
  133. // the file must exist
  134. std::string fullPath = cmDynamicLoader::LibPrefix();
  135. fullPath += "cm" + argsIn[0] + cmDynamicLoader::LibExtension();
  136. std::vector<std::string> args;
  137. cmSystemTools::ExpandListArguments(argsIn, args);
  138. // search for the file
  139. std::vector<std::string> path;
  140. for (unsigned int j = 1; j < args.size(); j++)
  141. {
  142. // expand variables
  143. std::string exp = args[j];
  144. cmSystemTools::ExpandRegistryValues(exp);
  145. // Glob the entry in case of wildcards.
  146. cmSystemTools::GlobDirs(exp.c_str(), path);
  147. }
  148. // Try to find the program.
  149. fullPath = cmSystemTools::FindFile(fullPath.c_str(), path);
  150. if (fullPath == "")
  151. {
  152. this->SetError("Attempt to load command failed.");
  153. return false;
  154. }
  155. // try loading the shared library / dll
  156. cmLibHandle lib = cmDynamicLoader::OpenLibrary(fullPath.c_str());
  157. if(lib)
  158. {
  159. // Look for the symbol cmLoad, cmGetFactoryCompilerUsed,
  160. // and cmGetFactoryVersion in the library
  161. CM_NAME_FUNCTION nameFunction
  162. = (CM_NAME_FUNCTION)
  163. cmDynamicLoader::GetSymbolAddress(lib, "cmGetName");
  164. CM_INIT_FUNCTION initFunction
  165. = (CM_INIT_FUNCTION)
  166. cmDynamicLoader::GetSymbolAddress(lib, "cmInitializeCommand");
  167. // if the symbol is found call it to set the name on the
  168. // function blocker
  169. if(nameFunction)
  170. {
  171. // create a function blocker and set it up
  172. cmLoadedCommand *f = new cmLoadedCommand();
  173. f->m_commandName = (*nameFunction)();
  174. (*initFunction)(&f->info);
  175. m_Makefile->AddCommand(f);
  176. }
  177. }
  178. return true;
  179. }