cmLoadCommandCommand.cxx 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 std::string 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(this->Disallowed(cmPolicies::CMP0031,
  165. "The load_command command should not be called; see CMP0031."))
  166. { return true; }
  167. if(args.size() < 1 )
  168. {
  169. return true;
  170. }
  171. // Construct a variable to report what file was loaded, if any.
  172. // Start by removing the definition in case of failure.
  173. std::string reportVar = "CMAKE_LOADED_COMMAND_";
  174. reportVar += args[0];
  175. this->Makefile->RemoveDefinition(reportVar);
  176. // the file must exist
  177. std::string moduleName =
  178. this->Makefile->GetRequiredDefinition("CMAKE_SHARED_MODULE_PREFIX");
  179. moduleName += "cm" + args[0];
  180. moduleName +=
  181. this->Makefile->GetRequiredDefinition("CMAKE_SHARED_MODULE_SUFFIX");
  182. // search for the file
  183. std::vector<std::string> path;
  184. for (unsigned int j = 1; j < args.size(); j++)
  185. {
  186. // expand variables
  187. std::string exp = args[j];
  188. cmSystemTools::ExpandRegistryValues(exp);
  189. // Glob the entry in case of wildcards.
  190. cmSystemTools::GlobDirs(exp, path);
  191. }
  192. // Try to find the program.
  193. std::string fullPath = cmSystemTools::FindFile(moduleName.c_str(), path);
  194. if (fullPath == "")
  195. {
  196. std::ostringstream e;
  197. e << "Attempt to load command failed from file \""
  198. << moduleName << "\"";
  199. this->SetError(e.str());
  200. return false;
  201. }
  202. // try loading the shared library / dll
  203. cmsys::DynamicLoader::LibraryHandle lib
  204. = cmDynamicLoader::OpenLibrary(fullPath.c_str());
  205. if(!lib)
  206. {
  207. std::string err = "Attempt to load the library ";
  208. err += fullPath + " failed.";
  209. const char* error = cmsys::DynamicLoader::LastError();
  210. if ( error )
  211. {
  212. err += " Additional error info is:\n";
  213. err += error;
  214. }
  215. this->SetError(err);
  216. return false;
  217. }
  218. // Report what file was loaded for this command.
  219. this->Makefile->AddDefinition(reportVar, fullPath.c_str());
  220. // find the init function
  221. std::string initFuncName = args[0] + "Init";
  222. CM_INIT_FUNCTION initFunction
  223. = (CM_INIT_FUNCTION)
  224. cmsys::DynamicLoader::GetSymbolAddress(lib, initFuncName.c_str());
  225. if ( !initFunction )
  226. {
  227. initFuncName = "_";
  228. initFuncName += args[0];
  229. initFuncName += "Init";
  230. initFunction = (CM_INIT_FUNCTION)(
  231. cmsys::DynamicLoader::GetSymbolAddress(lib, initFuncName.c_str()));
  232. }
  233. // if the symbol is found call it to set the name on the
  234. // function blocker
  235. if(initFunction)
  236. {
  237. // create a function blocker and set it up
  238. cmLoadedCommand *f = new cmLoadedCommand();
  239. (*initFunction)(&f->info);
  240. this->Makefile->AddCommand(f);
  241. return true;
  242. }
  243. this->SetError("Attempt to load command failed. "
  244. "No init function found.");
  245. return false;
  246. }