cmLoadCommandCommand.cxx 6.6 KB

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