cmLoadCommandCommand.cxx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmLoadCommandCommand.h"
  4. #include <signal.h>
  5. #include <sstream>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include "cmCPluginAPI.cxx"
  10. #include "cmCPluginAPI.h"
  11. #include "cmDynamicLoader.h"
  12. #include "cmMakefile.h"
  13. #include "cmState.h"
  14. #include "cmSystemTools.h"
  15. class cmExecutionStatus;
  16. #ifdef __QNX__
  17. #include <malloc.h> /* for malloc/free on QNX */
  18. #endif
  19. extern "C" void TrapsForSignalsCFunction(int sig);
  20. // a class for loadabple commands
  21. class cmLoadedCommand : public cmCommand
  22. {
  23. public:
  24. cmLoadedCommand()
  25. {
  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() CM_OVERRIDE;
  31. /**
  32. * This is a virtual constructor for the command.
  33. */
  34. cmCommand* Clone() CM_OVERRIDE
  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. bool InitialPass(std::vector<std::string> const& args,
  46. cmExecutionStatus&) CM_OVERRIDE;
  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. void FinalPass() CM_OVERRIDE;
  54. bool HasFinalPass() const CM_OVERRIDE
  55. {
  56. return this->info.FinalPass != CM_NULLPTR;
  57. }
  58. /**
  59. * The name of the command as specified in CMakeList.txt.
  60. */
  61. std::string GetName() const CM_OVERRIDE { return info.Name; }
  62. static const char* LastName;
  63. static void TrapsForSignals(int sig)
  64. {
  65. fprintf(stderr, "CMake loaded command %s crashed with signal: %d.\n",
  66. cmLoadedCommand::LastName, sig);
  67. }
  68. static void InstallSignalHandlers(const char* name, int remove = 0)
  69. {
  70. cmLoadedCommand::LastName = name;
  71. if (!name) {
  72. cmLoadedCommand::LastName = "????";
  73. }
  74. if (!remove) {
  75. signal(SIGSEGV, TrapsForSignalsCFunction);
  76. #ifdef SIGBUS
  77. signal(SIGBUS, TrapsForSignalsCFunction);
  78. #endif
  79. signal(SIGILL, TrapsForSignalsCFunction);
  80. } else {
  81. signal(SIGSEGV, CM_NULLPTR);
  82. #ifdef SIGBUS
  83. signal(SIGBUS, CM_NULLPTR);
  84. #endif
  85. signal(SIGILL, CM_NULLPTR);
  86. }
  87. }
  88. cmLoadedCommandInfo info;
  89. };
  90. extern "C" void TrapsForSignalsCFunction(int sig)
  91. {
  92. cmLoadedCommand::TrapsForSignals(sig);
  93. }
  94. const char* cmLoadedCommand::LastName = CM_NULLPTR;
  95. bool cmLoadedCommand::InitialPass(std::vector<std::string> const& args,
  96. cmExecutionStatus&)
  97. {
  98. if (!info.InitialPass) {
  99. return true;
  100. }
  101. // clear the error string
  102. if (this->info.Error) {
  103. free(this->info.Error);
  104. }
  105. // create argc and argv and then invoke the command
  106. int argc = static_cast<int>(args.size());
  107. char** argv = CM_NULLPTR;
  108. if (argc) {
  109. argv = (char**)malloc(argc * sizeof(char*));
  110. }
  111. int i;
  112. for (i = 0; i < argc; ++i) {
  113. argv[i] = strdup(args[i].c_str());
  114. }
  115. cmLoadedCommand::InstallSignalHandlers(info.Name);
  116. int result =
  117. info.InitialPass((void*)&info, (void*)this->Makefile, argc, argv);
  118. cmLoadedCommand::InstallSignalHandlers(info.Name, 1);
  119. cmFreeArguments(argc, argv);
  120. if (result) {
  121. return true;
  122. }
  123. /* Initial Pass must have failed so set the error string */
  124. if (this->info.Error) {
  125. this->SetError(this->info.Error);
  126. }
  127. return false;
  128. }
  129. void cmLoadedCommand::FinalPass()
  130. {
  131. if (this->info.FinalPass) {
  132. cmLoadedCommand::InstallSignalHandlers(info.Name);
  133. this->info.FinalPass((void*)&this->info, (void*)this->Makefile);
  134. cmLoadedCommand::InstallSignalHandlers(info.Name, 1);
  135. }
  136. }
  137. cmLoadedCommand::~cmLoadedCommand()
  138. {
  139. if (this->info.Destructor) {
  140. cmLoadedCommand::InstallSignalHandlers(info.Name);
  141. this->info.Destructor((void*)&this->info);
  142. cmLoadedCommand::InstallSignalHandlers(info.Name, 1);
  143. }
  144. if (this->info.Error) {
  145. free(this->info.Error);
  146. }
  147. }
  148. // cmLoadCommandCommand
  149. bool cmLoadCommandCommand::InitialPass(std::vector<std::string> const& args,
  150. cmExecutionStatus&)
  151. {
  152. if (args.empty()) {
  153. return true;
  154. }
  155. // Construct a variable to report what file was loaded, if any.
  156. // Start by removing the definition in case of failure.
  157. std::string reportVar = "CMAKE_LOADED_COMMAND_";
  158. reportVar += args[0];
  159. this->Makefile->RemoveDefinition(reportVar);
  160. // the file must exist
  161. std::string moduleName =
  162. this->Makefile->GetRequiredDefinition("CMAKE_SHARED_MODULE_PREFIX");
  163. moduleName += "cm" + args[0];
  164. moduleName +=
  165. this->Makefile->GetRequiredDefinition("CMAKE_SHARED_MODULE_SUFFIX");
  166. // search for the file
  167. std::vector<std::string> path;
  168. for (unsigned int j = 1; j < args.size(); j++) {
  169. // expand variables
  170. std::string exp = args[j];
  171. cmSystemTools::ExpandRegistryValues(exp);
  172. // Glob the entry in case of wildcards.
  173. cmSystemTools::GlobDirs(exp, path);
  174. }
  175. // Try to find the program.
  176. std::string fullPath = cmSystemTools::FindFile(moduleName, path);
  177. if (fullPath == "") {
  178. std::ostringstream e;
  179. e << "Attempt to load command failed from file \"" << moduleName << "\"";
  180. this->SetError(e.str());
  181. return false;
  182. }
  183. // try loading the shared library / dll
  184. cmsys::DynamicLoader::LibraryHandle lib =
  185. cmDynamicLoader::OpenLibrary(fullPath.c_str());
  186. if (!lib) {
  187. std::string err = "Attempt to load the library ";
  188. err += fullPath + " failed.";
  189. const char* error = cmsys::DynamicLoader::LastError();
  190. if (error) {
  191. err += " Additional error info is:\n";
  192. err += error;
  193. }
  194. this->SetError(err);
  195. return false;
  196. }
  197. // Report what file was loaded for this command.
  198. this->Makefile->AddDefinition(reportVar, fullPath.c_str());
  199. // find the init function
  200. std::string initFuncName = args[0] + "Init";
  201. CM_INIT_FUNCTION initFunction =
  202. (CM_INIT_FUNCTION)cmsys::DynamicLoader::GetSymbolAddress(lib,
  203. initFuncName);
  204. if (!initFunction) {
  205. initFuncName = "_";
  206. initFuncName += args[0];
  207. initFuncName += "Init";
  208. initFunction = (CM_INIT_FUNCTION)(
  209. cmsys::DynamicLoader::GetSymbolAddress(lib, initFuncName));
  210. }
  211. // if the symbol is found call it to set the name on the
  212. // function blocker
  213. if (initFunction) {
  214. // create a function blocker and set it up
  215. cmLoadedCommand* f = new cmLoadedCommand();
  216. (*initFunction)(&f->info);
  217. this->Makefile->GetState()->AddScriptedCommand(args[0], f);
  218. return true;
  219. }
  220. this->SetError("Attempt to load command failed. "
  221. "No init function found.");
  222. return false;
  223. }