cmLoadCommandCommand.cxx 6.6 KB

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