cmLoadCommandCommand.cxx 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. * The name of the command as specified in CMakeList.txt.
  53. */
  54. virtual const char* GetName() { return info.Name; }
  55. /**
  56. * Succinct documentation.
  57. */
  58. virtual const char* GetTerseDocumentation()
  59. {
  60. if (this->info.GetTerseDocumentation)
  61. {
  62. cmLoadedCommand::InstallSignalHandlers(info.Name);
  63. const char* ret = info.GetTerseDocumentation();
  64. cmLoadedCommand::InstallSignalHandlers(info.Name, 1);
  65. return ret;
  66. }
  67. else
  68. {
  69. return "LoadedCommand without any additional documentation";
  70. }
  71. }
  72. static const char* LastName;
  73. static void TrapsForSignals(int sig)
  74. {
  75. fprintf(stderr, "CMake loaded command %s crashed with signal: %d.\n",
  76. cmLoadedCommand::LastName, sig);
  77. }
  78. static void InstallSignalHandlers(const char* name, int remove = 0)
  79. {
  80. cmLoadedCommand::LastName = name;
  81. if(!name)
  82. {
  83. cmLoadedCommand::LastName = "????";
  84. }
  85. if(!remove)
  86. {
  87. signal(SIGSEGV, TrapsForSignalsCFunction);
  88. #ifdef SIGBUS
  89. signal(SIGBUS, TrapsForSignalsCFunction);
  90. #endif
  91. signal(SIGILL, TrapsForSignalsCFunction);
  92. }
  93. else
  94. {
  95. signal(SIGSEGV, 0);
  96. #ifdef SIGBUS
  97. signal(SIGBUS, 0);
  98. #endif
  99. signal(SIGILL, 0);
  100. }
  101. }
  102. /**
  103. * More documentation.
  104. */
  105. virtual const char* GetFullDocumentation()
  106. {
  107. if (this->info.GetFullDocumentation)
  108. {
  109. cmLoadedCommand::InstallSignalHandlers(info.Name);
  110. const char* ret = info.GetFullDocumentation();
  111. cmLoadedCommand::InstallSignalHandlers(info.Name, 1);
  112. return ret;
  113. }
  114. else
  115. {
  116. return "LoadedCommand without any additional documentation";
  117. }
  118. }
  119. cmTypeMacro(cmLoadedCommand, cmCommand);
  120. cmLoadedCommandInfo info;
  121. };
  122. extern "C" void TrapsForSignalsCFunction(int sig)
  123. {
  124. cmLoadedCommand::TrapsForSignals(sig);
  125. }
  126. const char* cmLoadedCommand::LastName = 0;
  127. bool cmLoadedCommand::InitialPass(std::vector<std::string> const& args)
  128. {
  129. if (!info.InitialPass)
  130. {
  131. return true;
  132. }
  133. // clear the error string
  134. if (this->info.Error)
  135. {
  136. free(this->info.Error);
  137. }
  138. // create argc and argv and then invoke the command
  139. int argc = static_cast<int> (args.size());
  140. char **argv = 0;
  141. if (argc)
  142. {
  143. argv = (char **)malloc(argc*sizeof(char *));
  144. }
  145. int i;
  146. for (i = 0; i < argc; ++i)
  147. {
  148. argv[i] = strdup(args[i].c_str());
  149. }
  150. cmLoadedCommand::InstallSignalHandlers(info.Name);
  151. int result = info.InitialPass((void *)&info,(void *)this->m_Makefile,argc,argv);
  152. cmLoadedCommand::InstallSignalHandlers(info.Name, 1);
  153. cmFreeArguments(argc,argv);
  154. if (result)
  155. {
  156. return true;
  157. }
  158. /* Initial Pass must have failed so set the error string */
  159. if (this->info.Error)
  160. {
  161. this->SetError(this->info.Error);
  162. }
  163. return false;
  164. }
  165. void cmLoadedCommand::FinalPass()
  166. {
  167. if (this->info.FinalPass)
  168. {
  169. cmLoadedCommand::InstallSignalHandlers(info.Name);
  170. this->info.FinalPass((void *)&this->info,(void *)this->m_Makefile);
  171. cmLoadedCommand::InstallSignalHandlers(info.Name, 1);
  172. }
  173. }
  174. cmLoadedCommand::~cmLoadedCommand()
  175. {
  176. if (this->info.Destructor)
  177. {
  178. cmLoadedCommand::InstallSignalHandlers(info.Name);
  179. this->info.Destructor((void *)&this->info);
  180. cmLoadedCommand::InstallSignalHandlers(info.Name, 1);
  181. }
  182. if (this->info.Error)
  183. {
  184. free(this->info.Error);
  185. }
  186. }
  187. // cmLoadCommandCommand
  188. bool cmLoadCommandCommand::InitialPass(std::vector<std::string> const& args)
  189. {
  190. if(args.size() < 1 )
  191. {
  192. return true;
  193. }
  194. // Construct a variable to report what file was loaded, if any.
  195. // Start by removing the definition in case of failure.
  196. std::string reportVar = "CMAKE_LOADED_COMMAND_";
  197. reportVar += args[0];
  198. m_Makefile->RemoveDefinition(reportVar.c_str());
  199. // the file must exist
  200. std::string fullPath = cmDynamicLoader::LibPrefix();
  201. fullPath += "cm" + args[0] + cmDynamicLoader::LibExtension();
  202. // search for the file
  203. std::vector<std::string> path;
  204. for (unsigned int j = 1; j < args.size(); j++)
  205. {
  206. // expand variables
  207. std::string exp = args[j];
  208. cmSystemTools::ExpandRegistryValues(exp);
  209. // Glob the entry in case of wildcards.
  210. cmSystemTools::GlobDirs(exp.c_str(), path);
  211. }
  212. // Try to find the program.
  213. fullPath = cmSystemTools::FindFile(fullPath.c_str(), path);
  214. if (fullPath == "")
  215. {
  216. fullPath = "Attempt to load command failed from file : ";
  217. fullPath += cmDynamicLoader::LibPrefix();
  218. fullPath += "cm" + args[0] + cmDynamicLoader::LibExtension();
  219. this->SetError(fullPath.c_str());
  220. return false;
  221. }
  222. // try loading the shared library / dll
  223. cmLibHandle lib = cmDynamicLoader::OpenLibrary(fullPath.c_str());
  224. if(!lib)
  225. {
  226. std::string err = "Attempt to load the library ";
  227. err += fullPath + " failed.";
  228. const char* error = cmDynamicLoader::LastError();
  229. if ( error )
  230. {
  231. err += " Additional error info is:\n";
  232. err += error;
  233. }
  234. this->SetError(err.c_str());
  235. return false;
  236. }
  237. // Report what file was loaded for this command.
  238. m_Makefile->AddDefinition(reportVar.c_str(), fullPath.c_str());
  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. }