cmLoadCommandCommand.cxx 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. // Construct a variable to report what file was loaded, if any.
  202. // Start by removing the definition in case of failure.
  203. std::string reportVar = "CMAKE_LOADED_COMMAND_";
  204. reportVar += args[0];
  205. m_Makefile->RemoveDefinition(reportVar.c_str());
  206. // the file must exist
  207. std::string fullPath = cmDynamicLoader::LibPrefix();
  208. fullPath += "cm" + args[0] + cmDynamicLoader::LibExtension();
  209. // search for the file
  210. std::vector<std::string> path;
  211. for (unsigned int j = 1; j < args.size(); j++)
  212. {
  213. // expand variables
  214. std::string exp = args[j];
  215. cmSystemTools::ExpandRegistryValues(exp);
  216. // Glob the entry in case of wildcards.
  217. cmSystemTools::GlobDirs(exp.c_str(), path);
  218. }
  219. // Try to find the program.
  220. fullPath = cmSystemTools::FindFile(fullPath.c_str(), path);
  221. if (fullPath == "")
  222. {
  223. fullPath = "Attempt to load command failed from file : ";
  224. fullPath += cmDynamicLoader::LibPrefix();
  225. fullPath += "cm" + args[0] + cmDynamicLoader::LibExtension();
  226. this->SetError(fullPath.c_str());
  227. return false;
  228. }
  229. // try loading the shared library / dll
  230. cmLibHandle lib = cmDynamicLoader::OpenLibrary(fullPath.c_str());
  231. if(!lib)
  232. {
  233. std::string err = "Attempt to load the library ";
  234. err += fullPath + " failed.";
  235. const char* error = cmDynamicLoader::LastError();
  236. if ( error )
  237. {
  238. err += " Additional error info is:\n";
  239. err += error;
  240. }
  241. this->SetError(err.c_str());
  242. return false;
  243. }
  244. // Report what file was loaded for this command.
  245. m_Makefile->AddDefinition(reportVar.c_str(), fullPath.c_str());
  246. // find the init function
  247. std::string initFuncName = args[0] + "Init";
  248. CM_INIT_FUNCTION initFunction
  249. = (CM_INIT_FUNCTION)
  250. cmDynamicLoader::GetSymbolAddress(lib, initFuncName.c_str());
  251. if ( !initFunction )
  252. {
  253. initFuncName = "_";
  254. initFuncName += args[0];
  255. initFuncName += "Init";
  256. initFunction = (CM_INIT_FUNCTION)(
  257. cmDynamicLoader::GetSymbolAddress(lib, initFuncName.c_str()));
  258. }
  259. // if the symbol is found call it to set the name on the
  260. // function blocker
  261. if(initFunction)
  262. {
  263. // create a function blocker and set it up
  264. cmLoadedCommand *f = new cmLoadedCommand();
  265. (*initFunction)(&f->info);
  266. m_Makefile->AddCommand(f);
  267. return true;
  268. }
  269. this->SetError("Attempt to load command failed. "
  270. "No init function found.");
  271. return false;
  272. }