cmLoadCommandCommand.cxx 7.2 KB

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