cmLoadCommandCommand.cxx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. #include <cmsys/DynamicLoader.hxx>
  18. #include <signal.h>
  19. extern "C" void TrapsForSignalsCFunction(int sig);
  20. // a class for loadabple commands
  21. class cmLoadedCommand : public cmCommand
  22. {
  23. public:
  24. cmLoadedCommand() {
  25. memset(&this->info,0,sizeof(this->info));
  26. this->info.CAPI = &cmStaticCAPI;
  27. }
  28. ///! clean up any memory allocated by the plugin
  29. ~cmLoadedCommand();
  30. /**
  31. * This is a virtual constructor for the command.
  32. */
  33. virtual cmCommand* Clone()
  34. {
  35. cmLoadedCommand *newC = new cmLoadedCommand;
  36. // we must copy when we clone
  37. memcpy(&newC->info,&this->info,sizeof(info));
  38. return newC;
  39. }
  40. /**
  41. * This is called when the command is first encountered in
  42. * the CMakeLists.txt file.
  43. */
  44. virtual bool InitialPass(std::vector<std::string> const& args);
  45. /**
  46. * This is called at the end after all the information
  47. * specified by the command is accumulated. Most commands do
  48. * not implement this method. At this point, reading and
  49. * writing to the cache can be done.
  50. */
  51. virtual void FinalPass();
  52. /**
  53. * The name of the command as specified in CMakeList.txt.
  54. */
  55. virtual const char* GetName() { return info.Name; }
  56. /**
  57. * Succinct documentation.
  58. */
  59. virtual const char* GetTerseDocumentation()
  60. {
  61. if (this->info.GetTerseDocumentation)
  62. {
  63. cmLoadedCommand::InstallSignalHandlers(info.Name);
  64. const char* ret = info.GetTerseDocumentation();
  65. cmLoadedCommand::InstallSignalHandlers(info.Name, 1);
  66. return ret;
  67. }
  68. else
  69. {
  70. return "LoadedCommand without any additional documentation";
  71. }
  72. }
  73. static const char* LastName;
  74. static void TrapsForSignals(int sig)
  75. {
  76. fprintf(stderr, "CMake loaded command %s crashed with signal: %d.\n",
  77. cmLoadedCommand::LastName, sig);
  78. }
  79. static void InstallSignalHandlers(const char* name, int remove = 0)
  80. {
  81. cmLoadedCommand::LastName = name;
  82. if(!name)
  83. {
  84. cmLoadedCommand::LastName = "????";
  85. }
  86. if(!remove)
  87. {
  88. signal(SIGSEGV, TrapsForSignalsCFunction);
  89. #ifdef SIGBUS
  90. signal(SIGBUS, TrapsForSignalsCFunction);
  91. #endif
  92. signal(SIGILL, TrapsForSignalsCFunction);
  93. }
  94. else
  95. {
  96. signal(SIGSEGV, 0);
  97. #ifdef SIGBUS
  98. signal(SIGBUS, 0);
  99. #endif
  100. signal(SIGILL, 0);
  101. }
  102. }
  103. /**
  104. * More documentation.
  105. */
  106. virtual const char* GetFullDocumentation()
  107. {
  108. if (this->info.GetFullDocumentation)
  109. {
  110. cmLoadedCommand::InstallSignalHandlers(info.Name);
  111. const char* ret = info.GetFullDocumentation();
  112. cmLoadedCommand::InstallSignalHandlers(info.Name, 1);
  113. return ret;
  114. }
  115. else
  116. {
  117. return "LoadedCommand without any additional documentation";
  118. }
  119. }
  120. cmTypeMacro(cmLoadedCommand, cmCommand);
  121. cmLoadedCommandInfo info;
  122. };
  123. extern "C" void TrapsForSignalsCFunction(int sig)
  124. {
  125. cmLoadedCommand::TrapsForSignals(sig);
  126. }
  127. const char* cmLoadedCommand::LastName = 0;
  128. bool cmLoadedCommand::InitialPass(std::vector<std::string> const& args)
  129. {
  130. if (!info.InitialPass)
  131. {
  132. return true;
  133. }
  134. // clear the error string
  135. if (this->info.Error)
  136. {
  137. free(this->info.Error);
  138. }
  139. // create argc and argv and then invoke the command
  140. int argc = static_cast<int> (args.size());
  141. char **argv = 0;
  142. if (argc)
  143. {
  144. argv = (char **)malloc(argc*sizeof(char *));
  145. }
  146. int i;
  147. for (i = 0; i < argc; ++i)
  148. {
  149. argv[i] = strdup(args[i].c_str());
  150. }
  151. cmLoadedCommand::InstallSignalHandlers(info.Name);
  152. int result = info.InitialPass((void *)&info,
  153. (void *)this->Makefile,argc,argv);
  154. cmLoadedCommand::InstallSignalHandlers(info.Name, 1);
  155. cmFreeArguments(argc,argv);
  156. if (result)
  157. {
  158. return true;
  159. }
  160. /* Initial Pass must have failed so set the error string */
  161. if (this->info.Error)
  162. {
  163. this->SetError(this->info.Error);
  164. }
  165. return false;
  166. }
  167. void cmLoadedCommand::FinalPass()
  168. {
  169. if (this->info.FinalPass)
  170. {
  171. cmLoadedCommand::InstallSignalHandlers(info.Name);
  172. this->info.FinalPass((void *)&this->info,(void *)this->Makefile);
  173. cmLoadedCommand::InstallSignalHandlers(info.Name, 1);
  174. }
  175. }
  176. cmLoadedCommand::~cmLoadedCommand()
  177. {
  178. if (this->info.Destructor)
  179. {
  180. cmLoadedCommand::InstallSignalHandlers(info.Name);
  181. this->info.Destructor((void *)&this->info);
  182. cmLoadedCommand::InstallSignalHandlers(info.Name, 1);
  183. }
  184. if (this->info.Error)
  185. {
  186. free(this->info.Error);
  187. }
  188. }
  189. // cmLoadCommandCommand
  190. bool cmLoadCommandCommand::InitialPass(std::vector<std::string> const& args)
  191. {
  192. if(args.size() < 1 )
  193. {
  194. return true;
  195. }
  196. // Construct a variable to report what file was loaded, if any.
  197. // Start by removing the definition in case of failure.
  198. std::string reportVar = "CMAKE_LOADED_COMMAND_";
  199. reportVar += args[0];
  200. this->Makefile->RemoveDefinition(reportVar.c_str());
  201. // the file must exist
  202. std::string fullPath = cmDynamicLoader::LibPrefix();
  203. fullPath += "cm" + args[0] + cmDynamicLoader::LibExtension();
  204. // search for the file
  205. std::vector<std::string> path;
  206. for (unsigned int j = 1; j < args.size(); j++)
  207. {
  208. // expand variables
  209. std::string exp = args[j];
  210. cmSystemTools::ExpandRegistryValues(exp);
  211. // Glob the entry in case of wildcards.
  212. cmSystemTools::GlobDirs(exp.c_str(), path);
  213. }
  214. // Try to find the program.
  215. fullPath = cmSystemTools::FindFile(fullPath.c_str(), path);
  216. if (fullPath == "")
  217. {
  218. fullPath = "Attempt to load command failed from file : ";
  219. fullPath += cmDynamicLoader::LibPrefix();
  220. fullPath += "cm" + args[0] + cmDynamicLoader::LibExtension();
  221. this->SetError(fullPath.c_str());
  222. return false;
  223. }
  224. // try loading the shared library / dll
  225. cmsys::DynamicLoader::LibraryHandle lib
  226. = cmDynamicLoader::OpenLibrary(fullPath.c_str());
  227. if(!lib)
  228. {
  229. std::string err = "Attempt to load the library ";
  230. err += fullPath + " failed.";
  231. const char* error = cmsys::DynamicLoader::LastError();
  232. if ( error )
  233. {
  234. err += " Additional error info is:\n";
  235. err += error;
  236. }
  237. this->SetError(err.c_str());
  238. return false;
  239. }
  240. // Report what file was loaded for this command.
  241. this->Makefile->AddDefinition(reportVar.c_str(), fullPath.c_str());
  242. // find the init function
  243. std::string initFuncName = args[0] + "Init";
  244. CM_INIT_FUNCTION initFunction
  245. = (CM_INIT_FUNCTION)
  246. cmsys::DynamicLoader::GetSymbolAddress(lib, initFuncName.c_str());
  247. if ( !initFunction )
  248. {
  249. initFuncName = "_";
  250. initFuncName += args[0];
  251. initFuncName += "Init";
  252. initFunction = (CM_INIT_FUNCTION)(
  253. cmsys::DynamicLoader::GetSymbolAddress(lib, initFuncName.c_str()));
  254. }
  255. // if the symbol is found call it to set the name on the
  256. // function blocker
  257. if(initFunction)
  258. {
  259. // create a function blocker and set it up
  260. cmLoadedCommand *f = new cmLoadedCommand();
  261. (*initFunction)(&f->info);
  262. this->Makefile->AddCommand(f);
  263. return true;
  264. }
  265. this->SetError("Attempt to load command failed. "
  266. "No init function found.");
  267. return false;
  268. }