cmLoadCommandCommand.cxx 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 <utility>
  10. #include "cm_memory.hxx"
  11. #include "cmCPluginAPI.cxx"
  12. #include "cmCPluginAPI.h"
  13. #include "cmDynamicLoader.h"
  14. #include "cmMakefile.h"
  15. #include "cmState.h"
  16. #include "cmSystemTools.h"
  17. class cmExecutionStatus;
  18. #ifdef __QNX__
  19. # include <malloc.h> /* for malloc/free on QNX */
  20. #endif
  21. namespace {
  22. const char* LastName = nullptr;
  23. extern "C" void TrapsForSignals(int sig)
  24. {
  25. fprintf(stderr, "CMake loaded command %s crashed with signal: %d.\n",
  26. LastName, sig);
  27. }
  28. struct SignalHandlerGuard
  29. {
  30. explicit SignalHandlerGuard(const char* name)
  31. {
  32. LastName = name != nullptr ? name : "????";
  33. signal(SIGSEGV, TrapsForSignals);
  34. #ifdef SIGBUS
  35. signal(SIGBUS, TrapsForSignals);
  36. #endif
  37. signal(SIGILL, TrapsForSignals);
  38. }
  39. ~SignalHandlerGuard()
  40. {
  41. signal(SIGSEGV, nullptr);
  42. #ifdef SIGBUS
  43. signal(SIGBUS, nullptr);
  44. #endif
  45. signal(SIGILL, nullptr);
  46. }
  47. SignalHandlerGuard(SignalHandlerGuard const&) = delete;
  48. SignalHandlerGuard& operator=(SignalHandlerGuard const&) = delete;
  49. };
  50. struct LoadedCommandImpl : cmLoadedCommandInfo
  51. {
  52. explicit LoadedCommandImpl(CM_INIT_FUNCTION init)
  53. : cmLoadedCommandInfo{ 0, 0, &cmStaticCAPI, 0,
  54. nullptr, nullptr, nullptr, nullptr,
  55. nullptr, nullptr, nullptr, nullptr }
  56. {
  57. init(this);
  58. }
  59. ~LoadedCommandImpl()
  60. {
  61. if (this->Destructor) {
  62. SignalHandlerGuard guard(this->Name);
  63. this->Destructor(this);
  64. }
  65. if (this->Error != nullptr) {
  66. free(this->Error);
  67. }
  68. }
  69. LoadedCommandImpl(LoadedCommandImpl const&) = delete;
  70. LoadedCommandImpl& operator=(LoadedCommandImpl const&) = delete;
  71. int DoInitialPass(cmMakefile* mf, int argc, char* argv[])
  72. {
  73. SignalHandlerGuard guard(this->Name);
  74. return this->InitialPass(this, mf, argc, argv);
  75. }
  76. void DoFinalPass(cmMakefile* mf)
  77. {
  78. SignalHandlerGuard guard(this->Name);
  79. this->FinalPass(this, mf);
  80. }
  81. };
  82. // a class for loadabple commands
  83. class cmLoadedCommand : public cmCommand
  84. {
  85. public:
  86. cmLoadedCommand() = default;
  87. explicit cmLoadedCommand(CM_INIT_FUNCTION init)
  88. : Impl(std::make_shared<LoadedCommandImpl>(init))
  89. {
  90. }
  91. /**
  92. * This is a virtual constructor for the command.
  93. */
  94. std::unique_ptr<cmCommand> Clone() override
  95. {
  96. auto newC = cm::make_unique<cmLoadedCommand>();
  97. // we must copy when we clone
  98. newC->Impl = this->Impl;
  99. return std::unique_ptr<cmCommand>(std::move(newC));
  100. }
  101. /**
  102. * This is called when the command is first encountered in
  103. * the CMakeLists.txt file.
  104. */
  105. bool InitialPass(std::vector<std::string> const& args,
  106. cmExecutionStatus&) override;
  107. private:
  108. std::shared_ptr<LoadedCommandImpl> Impl;
  109. };
  110. bool cmLoadedCommand::InitialPass(std::vector<std::string> const& args,
  111. cmExecutionStatus&)
  112. {
  113. if (!this->Impl->InitialPass) {
  114. return true;
  115. }
  116. // clear the error string
  117. if (this->Impl->Error) {
  118. free(this->Impl->Error);
  119. }
  120. // create argc and argv and then invoke the command
  121. int argc = static_cast<int>(args.size());
  122. char** argv = nullptr;
  123. if (argc) {
  124. argv = static_cast<char**>(malloc(argc * sizeof(char*)));
  125. }
  126. int i;
  127. for (i = 0; i < argc; ++i) {
  128. argv[i] = strdup(args[i].c_str());
  129. }
  130. int result = this->Impl->DoInitialPass(this->Makefile, argc, argv);
  131. cmFreeArguments(argc, argv);
  132. if (result) {
  133. if (this->Impl->FinalPass) {
  134. auto impl = this->Impl;
  135. this->Makefile->AddFinalAction(
  136. [impl](cmMakefile& makefile) { impl->DoFinalPass(&makefile); });
  137. }
  138. return true;
  139. }
  140. /* Initial Pass must have failed so set the error string */
  141. if (this->Impl->Error) {
  142. this->SetError(this->Impl->Error);
  143. }
  144. return false;
  145. }
  146. } // namespace
  147. // cmLoadCommandCommand
  148. bool cmLoadCommandCommand::InitialPass(std::vector<std::string> const& args,
  149. cmExecutionStatus&)
  150. {
  151. if (args.empty()) {
  152. return true;
  153. }
  154. // Construct a variable to report what file was loaded, if any.
  155. // Start by removing the definition in case of failure.
  156. std::string reportVar = "CMAKE_LOADED_COMMAND_";
  157. reportVar += args[0];
  158. this->Makefile->RemoveDefinition(reportVar);
  159. // the file must exist
  160. std::string moduleName =
  161. this->Makefile->GetRequiredDefinition("CMAKE_SHARED_MODULE_PREFIX");
  162. moduleName += "cm" + args[0];
  163. moduleName +=
  164. this->Makefile->GetRequiredDefinition("CMAKE_SHARED_MODULE_SUFFIX");
  165. // search for the file
  166. std::vector<std::string> path;
  167. for (unsigned int j = 1; j < args.size(); j++) {
  168. // expand variables
  169. std::string exp = args[j];
  170. cmSystemTools::ExpandRegistryValues(exp);
  171. // Glob the entry in case of wildcards.
  172. cmSystemTools::GlobDirs(exp, path);
  173. }
  174. // Try to find the program.
  175. std::string fullPath = cmSystemTools::FindFile(moduleName, path);
  176. if (fullPath.empty()) {
  177. std::ostringstream e;
  178. e << "Attempt to load command failed from file \"" << moduleName << "\"";
  179. this->SetError(e.str());
  180. return false;
  181. }
  182. // try loading the shared library / dll
  183. cmsys::DynamicLoader::LibraryHandle lib =
  184. cmDynamicLoader::OpenLibrary(fullPath.c_str());
  185. if (!lib) {
  186. std::string err = "Attempt to load the library ";
  187. err += fullPath + " failed.";
  188. const char* error = cmsys::DynamicLoader::LastError();
  189. if (error) {
  190. err += " Additional error info is:\n";
  191. err += error;
  192. }
  193. this->SetError(err);
  194. return false;
  195. }
  196. // Report what file was loaded for this command.
  197. this->Makefile->AddDefinition(reportVar, fullPath);
  198. // find the init function
  199. std::string initFuncName = args[0] + "Init";
  200. CM_INIT_FUNCTION initFunction = reinterpret_cast<CM_INIT_FUNCTION>(
  201. cmsys::DynamicLoader::GetSymbolAddress(lib, initFuncName));
  202. if (!initFunction) {
  203. initFuncName = "_";
  204. initFuncName += args[0];
  205. initFuncName += "Init";
  206. initFunction = reinterpret_cast<CM_INIT_FUNCTION>(
  207. cmsys::DynamicLoader::GetSymbolAddress(lib, initFuncName));
  208. }
  209. // if the symbol is found call it to set the name on the
  210. // function blocker
  211. if (initFunction) {
  212. this->Makefile->GetState()->AddScriptedCommand(
  213. args[0],
  214. cmLegacyCommandWrapper(cm::make_unique<cmLoadedCommand>(initFunction)));
  215. return true;
  216. }
  217. this->SetError("Attempt to load command failed. "
  218. "No init function found.");
  219. return false;
  220. }