cmLoadCommandCommand.cxx 5.1 KB

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