cmLoadCommandCommand.cxx 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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.cxx"
  12. #include "cmCPluginAPI.h"
  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. {
  27. memset(&this->info, 0, sizeof(this->info));
  28. this->info.CAPI = &cmStaticCAPI;
  29. }
  30. ///! clean up any memory allocated by the plugin
  31. ~cmLoadedCommand() CM_OVERRIDE;
  32. /**
  33. * This is a virtual constructor for the command.
  34. */
  35. cmCommand* Clone() CM_OVERRIDE
  36. {
  37. cmLoadedCommand* newC = new cmLoadedCommand;
  38. // we must copy when we clone
  39. memcpy(&newC->info, &this->info, sizeof(info));
  40. return newC;
  41. }
  42. /**
  43. * This is called when the command is first encountered in
  44. * the CMakeLists.txt file.
  45. */
  46. bool InitialPass(std::vector<std::string> const& args,
  47. cmExecutionStatus&) CM_OVERRIDE;
  48. /**
  49. * This is called at the end after all the information
  50. * specified by the command is accumulated. Most commands do
  51. * not implement this method. At this point, reading and
  52. * writing to the cache can be done.
  53. */
  54. void FinalPass() CM_OVERRIDE;
  55. bool HasFinalPass() const CM_OVERRIDE
  56. {
  57. return this->info.FinalPass ? true : false;
  58. }
  59. /**
  60. * The name of the command as specified in CMakeList.txt.
  61. */
  62. std::string GetName() const CM_OVERRIDE { return info.Name; }
  63. static const char* LastName;
  64. static void TrapsForSignals(int sig)
  65. {
  66. fprintf(stderr, "CMake loaded command %s crashed with signal: %d.\n",
  67. cmLoadedCommand::LastName, sig);
  68. }
  69. static void InstallSignalHandlers(const char* name, int remove = 0)
  70. {
  71. cmLoadedCommand::LastName = name;
  72. if (!name) {
  73. cmLoadedCommand::LastName = "????";
  74. }
  75. if (!remove) {
  76. signal(SIGSEGV, TrapsForSignalsCFunction);
  77. #ifdef SIGBUS
  78. signal(SIGBUS, TrapsForSignalsCFunction);
  79. #endif
  80. signal(SIGILL, TrapsForSignalsCFunction);
  81. } else {
  82. signal(SIGSEGV, CM_NULLPTR);
  83. #ifdef SIGBUS
  84. signal(SIGBUS, CM_NULLPTR);
  85. #endif
  86. signal(SIGILL, CM_NULLPTR);
  87. }
  88. }
  89. cmTypeMacro(cmLoadedCommand, cmCommand);
  90. cmLoadedCommandInfo info;
  91. };
  92. extern "C" void TrapsForSignalsCFunction(int sig)
  93. {
  94. cmLoadedCommand::TrapsForSignals(sig);
  95. }
  96. const char* cmLoadedCommand::LastName = CM_NULLPTR;
  97. bool cmLoadedCommand::InitialPass(std::vector<std::string> const& args,
  98. cmExecutionStatus&)
  99. {
  100. if (!info.InitialPass) {
  101. return true;
  102. }
  103. // clear the error string
  104. if (this->info.Error) {
  105. free(this->info.Error);
  106. }
  107. // create argc and argv and then invoke the command
  108. int argc = static_cast<int>(args.size());
  109. char** argv = CM_NULLPTR;
  110. if (argc) {
  111. argv = (char**)malloc(argc * sizeof(char*));
  112. }
  113. int i;
  114. for (i = 0; i < argc; ++i) {
  115. argv[i] = strdup(args[i].c_str());
  116. }
  117. cmLoadedCommand::InstallSignalHandlers(info.Name);
  118. int result =
  119. info.InitialPass((void*)&info, (void*)this->Makefile, argc, argv);
  120. cmLoadedCommand::InstallSignalHandlers(info.Name, 1);
  121. cmFreeArguments(argc, argv);
  122. if (result) {
  123. return true;
  124. }
  125. /* Initial Pass must have failed so set the error string */
  126. if (this->info.Error) {
  127. this->SetError(this->info.Error);
  128. }
  129. return false;
  130. }
  131. void cmLoadedCommand::FinalPass()
  132. {
  133. if (this->info.FinalPass) {
  134. cmLoadedCommand::InstallSignalHandlers(info.Name);
  135. this->info.FinalPass((void*)&this->info, (void*)this->Makefile);
  136. cmLoadedCommand::InstallSignalHandlers(info.Name, 1);
  137. }
  138. }
  139. cmLoadedCommand::~cmLoadedCommand()
  140. {
  141. if (this->info.Destructor) {
  142. cmLoadedCommand::InstallSignalHandlers(info.Name);
  143. this->info.Destructor((void*)&this->info);
  144. cmLoadedCommand::InstallSignalHandlers(info.Name, 1);
  145. }
  146. if (this->info.Error) {
  147. free(this->info.Error);
  148. }
  149. }
  150. // cmLoadCommandCommand
  151. bool cmLoadCommandCommand::InitialPass(std::vector<std::string> const& args,
  152. cmExecutionStatus&)
  153. {
  154. if (this->Disallowed(
  155. cmPolicies::CMP0031,
  156. "The load_command command should not be called; see CMP0031.")) {
  157. return true;
  158. }
  159. if (args.empty()) {
  160. return true;
  161. }
  162. // Construct a variable to report what file was loaded, if any.
  163. // Start by removing the definition in case of failure.
  164. std::string reportVar = "CMAKE_LOADED_COMMAND_";
  165. reportVar += args[0];
  166. this->Makefile->RemoveDefinition(reportVar);
  167. // the file must exist
  168. std::string moduleName =
  169. this->Makefile->GetRequiredDefinition("CMAKE_SHARED_MODULE_PREFIX");
  170. moduleName += "cm" + args[0];
  171. moduleName +=
  172. this->Makefile->GetRequiredDefinition("CMAKE_SHARED_MODULE_SUFFIX");
  173. // search for the file
  174. std::vector<std::string> path;
  175. for (unsigned int j = 1; j < args.size(); j++) {
  176. // expand variables
  177. std::string exp = args[j];
  178. cmSystemTools::ExpandRegistryValues(exp);
  179. // Glob the entry in case of wildcards.
  180. cmSystemTools::GlobDirs(exp, path);
  181. }
  182. // Try to find the program.
  183. std::string fullPath = cmSystemTools::FindFile(moduleName.c_str(), path);
  184. if (fullPath == "") {
  185. std::ostringstream e;
  186. e << "Attempt to load command failed from file \"" << moduleName << "\"";
  187. this->SetError(e.str());
  188. return false;
  189. }
  190. // try loading the shared library / dll
  191. cmsys::DynamicLoader::LibraryHandle lib =
  192. cmDynamicLoader::OpenLibrary(fullPath.c_str());
  193. if (!lib) {
  194. std::string err = "Attempt to load the library ";
  195. err += fullPath + " failed.";
  196. const char* error = cmsys::DynamicLoader::LastError();
  197. if (error) {
  198. err += " Additional error info is:\n";
  199. err += error;
  200. }
  201. this->SetError(err);
  202. return false;
  203. }
  204. // Report what file was loaded for this command.
  205. this->Makefile->AddDefinition(reportVar, fullPath.c_str());
  206. // find the init function
  207. std::string initFuncName = args[0] + "Init";
  208. CM_INIT_FUNCTION initFunction =
  209. (CM_INIT_FUNCTION)cmsys::DynamicLoader::GetSymbolAddress(
  210. lib, initFuncName.c_str());
  211. if (!initFunction) {
  212. initFuncName = "_";
  213. initFuncName += args[0];
  214. initFuncName += "Init";
  215. initFunction = (CM_INIT_FUNCTION)(
  216. cmsys::DynamicLoader::GetSymbolAddress(lib, initFuncName.c_str()));
  217. }
  218. // if the symbol is found call it to set the name on the
  219. // function blocker
  220. if (initFunction) {
  221. // create a function blocker and set it up
  222. cmLoadedCommand* f = new cmLoadedCommand();
  223. (*initFunction)(&f->info);
  224. this->Makefile->GetState()->AddCommand(f);
  225. return true;
  226. }
  227. this->SetError("Attempt to load command failed. "
  228. "No init function found.");
  229. return false;
  230. }