cmLoadCommandCommand.cxx 7.6 KB

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