cmLoadCommandCommand.cxx 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmLoadCommandCommand.h"
  11. #include "cmCPluginAPI.h"
  12. #include "cmCPluginAPI.cxx"
  13. #include "cmDynamicLoader.h"
  14. #include <cmsys/DynamicLoader.hxx>
  15. #include <stdlib.h>
  16. #ifdef __QNX__
  17. # include <malloc.h> /* for malloc/free on QNX */
  18. #endif
  19. #include <signal.h>
  20. extern "C" void TrapsForSignalsCFunction(int sig);
  21. // a class for loadabple commands
  22. class cmLoadedCommand : public cmCommand
  23. {
  24. public:
  25. cmLoadedCommand() {
  26. memset(&this->info,0,sizeof(this->info));
  27. this->info.CAPI = &cmStaticCAPI;
  28. }
  29. ///! clean up any memory allocated by the plugin
  30. ~cmLoadedCommand();
  31. /**
  32. * This is a virtual constructor for the command.
  33. */
  34. virtual cmCommand* Clone()
  35. {
  36. cmLoadedCommand *newC = new cmLoadedCommand;
  37. // we must copy when we clone
  38. memcpy(&newC->info,&this->info,sizeof(info));
  39. return newC;
  40. }
  41. /**
  42. * This is called when the command is first encountered in
  43. * the CMakeLists.txt file.
  44. */
  45. virtual bool InitialPass(std::vector<std::string> const& args,
  46. cmExecutionStatus &);
  47. /**
  48. * This is called at the end after all the information
  49. * specified by the command is accumulated. Most commands do
  50. * not implement this method. At this point, reading and
  51. * writing to the cache can be done.
  52. */
  53. virtual void FinalPass();
  54. virtual bool HasFinalPass() const
  55. { return this->info.FinalPass? true:false; }
  56. /**
  57. * The name of the command as specified in CMakeList.txt.
  58. */
  59. virtual const char* GetName() { return info.Name; }
  60. /**
  61. * Succinct documentation.
  62. */
  63. virtual const char* GetTerseDocumentation()
  64. {
  65. if (this->info.GetTerseDocumentation)
  66. {
  67. cmLoadedCommand::InstallSignalHandlers(info.Name);
  68. const char* ret = info.GetTerseDocumentation();
  69. cmLoadedCommand::InstallSignalHandlers(info.Name, 1);
  70. return ret;
  71. }
  72. else
  73. {
  74. return "LoadedCommand without any additional documentation";
  75. }
  76. }
  77. static const char* LastName;
  78. static void TrapsForSignals(int sig)
  79. {
  80. fprintf(stderr, "CMake loaded command %s crashed with signal: %d.\n",
  81. cmLoadedCommand::LastName, sig);
  82. }
  83. static void InstallSignalHandlers(const char* name, int remove = 0)
  84. {
  85. cmLoadedCommand::LastName = name;
  86. if(!name)
  87. {
  88. cmLoadedCommand::LastName = "????";
  89. }
  90. if(!remove)
  91. {
  92. signal(SIGSEGV, TrapsForSignalsCFunction);
  93. #ifdef SIGBUS
  94. signal(SIGBUS, TrapsForSignalsCFunction);
  95. #endif
  96. signal(SIGILL, TrapsForSignalsCFunction);
  97. }
  98. else
  99. {
  100. signal(SIGSEGV, 0);
  101. #ifdef SIGBUS
  102. signal(SIGBUS, 0);
  103. #endif
  104. signal(SIGILL, 0);
  105. }
  106. }
  107. /**
  108. * More documentation.
  109. */
  110. virtual const char* GetFullDocumentation()
  111. {
  112. if (this->info.GetFullDocumentation)
  113. {
  114. cmLoadedCommand::InstallSignalHandlers(info.Name);
  115. const char* ret = info.GetFullDocumentation();
  116. cmLoadedCommand::InstallSignalHandlers(info.Name, 1);
  117. return ret;
  118. }
  119. else
  120. {
  121. return "LoadedCommand without any additional documentation";
  122. }
  123. }
  124. cmTypeMacro(cmLoadedCommand, cmCommand);
  125. cmLoadedCommandInfo info;
  126. };
  127. extern "C" void TrapsForSignalsCFunction(int sig)
  128. {
  129. cmLoadedCommand::TrapsForSignals(sig);
  130. }
  131. const char* cmLoadedCommand::LastName = 0;
  132. bool cmLoadedCommand::InitialPass(std::vector<std::string> const& args,
  133. cmExecutionStatus &)
  134. {
  135. if (!info.InitialPass)
  136. {
  137. return true;
  138. }
  139. // clear the error string
  140. if (this->info.Error)
  141. {
  142. free(this->info.Error);
  143. }
  144. // create argc and argv and then invoke the command
  145. int argc = static_cast<int> (args.size());
  146. char **argv = 0;
  147. if (argc)
  148. {
  149. argv = (char **)malloc(argc*sizeof(char *));
  150. }
  151. int i;
  152. for (i = 0; i < argc; ++i)
  153. {
  154. argv[i] = strdup(args[i].c_str());
  155. }
  156. cmLoadedCommand::InstallSignalHandlers(info.Name);
  157. int result = info.InitialPass((void *)&info,
  158. (void *)this->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->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
  196. ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
  197. {
  198. if(args.size() < 1 )
  199. {
  200. return true;
  201. }
  202. // Construct a variable to report what file was loaded, if any.
  203. // Start by removing the definition in case of failure.
  204. std::string reportVar = "CMAKE_LOADED_COMMAND_";
  205. reportVar += args[0];
  206. this->Makefile->RemoveDefinition(reportVar.c_str());
  207. // the file must exist
  208. std::string moduleName =
  209. this->Makefile->GetRequiredDefinition("CMAKE_SHARED_MODULE_PREFIX");
  210. moduleName += "cm" + args[0];
  211. moduleName +=
  212. this->Makefile->GetRequiredDefinition("CMAKE_SHARED_MODULE_SUFFIX");
  213. // search for the file
  214. std::vector<std::string> path;
  215. for (unsigned int j = 1; j < args.size(); j++)
  216. {
  217. // expand variables
  218. std::string exp = args[j];
  219. cmSystemTools::ExpandRegistryValues(exp);
  220. // Glob the entry in case of wildcards.
  221. cmSystemTools::GlobDirs(exp.c_str(), path);
  222. }
  223. // Try to find the program.
  224. std::string fullPath = cmSystemTools::FindFile(moduleName.c_str(), path);
  225. if (fullPath == "")
  226. {
  227. cmOStringStream e;
  228. e << "Attempt to load command failed from file \""
  229. << moduleName << "\"";
  230. this->SetError(e.str().c_str());
  231. return false;
  232. }
  233. // try loading the shared library / dll
  234. cmsys::DynamicLoader::LibraryHandle lib
  235. = cmDynamicLoader::OpenLibrary(fullPath.c_str());
  236. if(!lib)
  237. {
  238. std::string err = "Attempt to load the library ";
  239. err += fullPath + " failed.";
  240. const char* error = cmsys::DynamicLoader::LastError();
  241. if ( error )
  242. {
  243. err += " Additional error info is:\n";
  244. err += error;
  245. }
  246. this->SetError(err.c_str());
  247. return false;
  248. }
  249. // Report what file was loaded for this command.
  250. this->Makefile->AddDefinition(reportVar.c_str(), fullPath.c_str());
  251. // find the init function
  252. std::string initFuncName = args[0] + "Init";
  253. CM_INIT_FUNCTION initFunction
  254. = (CM_INIT_FUNCTION)
  255. cmsys::DynamicLoader::GetSymbolAddress(lib, initFuncName.c_str());
  256. if ( !initFunction )
  257. {
  258. initFuncName = "_";
  259. initFuncName += args[0];
  260. initFuncName += "Init";
  261. initFunction = (CM_INIT_FUNCTION)(
  262. cmsys::DynamicLoader::GetSymbolAddress(lib, initFuncName.c_str()));
  263. }
  264. // if the symbol is found call it to set the name on the
  265. // function blocker
  266. if(initFunction)
  267. {
  268. // create a function blocker and set it up
  269. cmLoadedCommand *f = new cmLoadedCommand();
  270. (*initFunction)(&f->info);
  271. this->Makefile->AddCommand(f);
  272. return true;
  273. }
  274. this->SetError("Attempt to load command failed. "
  275. "No init function found.");
  276. return false;
  277. }