cmTryRunCommand.cxx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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 "cmTryRunCommand.h"
  11. #include "cmTryCompileCommand.h"
  12. #include <cmsys/FStream.hxx>
  13. // cmTryRunCommand
  14. bool cmTryRunCommand
  15. ::InitialPass(std::vector<std::string> const& argv, cmExecutionStatus &)
  16. {
  17. if(argv.size() < 4)
  18. {
  19. return false;
  20. }
  21. if(this->Makefile->GetCMakeInstance()->GetWorkingMode() ==
  22. cmake::FIND_PACKAGE_MODE)
  23. {
  24. this->Makefile->IssueMessage(cmake::FATAL_ERROR,
  25. "The TRY_RUN() command is not supported in --find-package mode.");
  26. return false;
  27. }
  28. // build an arg list for TryCompile and extract the runArgs,
  29. std::vector<std::string> tryCompile;
  30. this->CompileResultVariable = "";
  31. this->RunResultVariable = "";
  32. this->OutputVariable = "";
  33. this->RunOutputVariable = "";
  34. this->CompileOutputVariable = "";
  35. std::string runArgs;
  36. unsigned int i;
  37. for (i = 1; i < argv.size(); ++i)
  38. {
  39. if (argv[i] == "ARGS")
  40. {
  41. ++i;
  42. while (i < argv.size() && argv[i] != "COMPILE_DEFINITIONS" &&
  43. argv[i] != "CMAKE_FLAGS" &&
  44. argv[i] != "LINK_LIBRARIES")
  45. {
  46. runArgs += " ";
  47. runArgs += argv[i];
  48. ++i;
  49. }
  50. if (i < argv.size())
  51. {
  52. tryCompile.push_back(argv[i]);
  53. }
  54. }
  55. else
  56. {
  57. if (argv[i] == "OUTPUT_VARIABLE")
  58. {
  59. if ( argv.size() <= (i+1) )
  60. {
  61. cmSystemTools::Error(
  62. "OUTPUT_VARIABLE specified but there is no variable");
  63. return false;
  64. }
  65. i++;
  66. this->OutputVariable = argv[i];
  67. }
  68. else if (argv[i] == "RUN_OUTPUT_VARIABLE")
  69. {
  70. if (argv.size() <= (i + 1))
  71. {
  72. cmSystemTools::Error(
  73. "RUN_OUTPUT_VARIABLE specified but there is no variable");
  74. return false;
  75. }
  76. i++;
  77. this->RunOutputVariable = argv[i];
  78. }
  79. else if (argv[i] == "COMPILE_OUTPUT_VARIABLE")
  80. {
  81. if (argv.size() <= (i + 1))
  82. {
  83. cmSystemTools::Error(
  84. "COMPILE_OUTPUT_VARIABLE specified but there is no variable");
  85. return false;
  86. }
  87. i++;
  88. this->CompileOutputVariable = argv[i];
  89. }
  90. else
  91. {
  92. tryCompile.push_back(argv[i]);
  93. }
  94. }
  95. }
  96. // although they could be used together, don't allow it, because
  97. // using OUTPUT_VARIABLE makes crosscompiling harder
  98. if (this->OutputVariable.size()
  99. && (!this->RunOutputVariable.empty()
  100. || !this->CompileOutputVariable.empty()))
  101. {
  102. cmSystemTools::Error(
  103. "You cannot use OUTPUT_VARIABLE together with COMPILE_OUTPUT_VARIABLE "
  104. "or RUN_OUTPUT_VARIABLE. Please use only COMPILE_OUTPUT_VARIABLE and/or "
  105. "RUN_OUTPUT_VARIABLE.");
  106. return false;
  107. }
  108. bool captureRunOutput = false;
  109. if (!this->OutputVariable.empty())
  110. {
  111. captureRunOutput = true;
  112. tryCompile.push_back("OUTPUT_VARIABLE");
  113. tryCompile.push_back(this->OutputVariable);
  114. }
  115. if (!this->CompileOutputVariable.empty())
  116. {
  117. tryCompile.push_back("OUTPUT_VARIABLE");
  118. tryCompile.push_back(this->CompileOutputVariable);
  119. }
  120. if (!this->RunOutputVariable.empty())
  121. {
  122. captureRunOutput = true;
  123. }
  124. this->RunResultVariable = argv[0];
  125. this->CompileResultVariable = argv[1];
  126. // do the try compile
  127. int res = this->TryCompileCode(tryCompile, true);
  128. // now try running the command if it compiled
  129. if (!res)
  130. {
  131. if (this->OutputFile.empty())
  132. {
  133. cmSystemTools::Error(this->FindErrorMessage.c_str());
  134. }
  135. else
  136. {
  137. // "run" it and capture the output
  138. std::string runOutputContents;
  139. if (this->Makefile->IsOn("CMAKE_CROSSCOMPILING") &&
  140. !this->Makefile->IsDefinitionSet("CMAKE_CROSSCOMPILING_EMULATOR"))
  141. {
  142. this->DoNotRunExecutable(runArgs,
  143. argv[3],
  144. captureRunOutput ? &runOutputContents : 0);
  145. }
  146. else
  147. {
  148. this->RunExecutable(runArgs, &runOutputContents);
  149. }
  150. // now put the output into the variables
  151. if(!this->RunOutputVariable.empty())
  152. {
  153. this->Makefile->AddDefinition(this->RunOutputVariable,
  154. runOutputContents.c_str());
  155. }
  156. if(!this->OutputVariable.empty())
  157. {
  158. // if the TryCompileCore saved output in this outputVariable then
  159. // prepend that output to this output
  160. const char* compileOutput
  161. = this->Makefile->GetDefinition(this->OutputVariable);
  162. if (compileOutput)
  163. {
  164. runOutputContents = std::string(compileOutput) + runOutputContents;
  165. }
  166. this->Makefile->AddDefinition(this->OutputVariable,
  167. runOutputContents.c_str());
  168. }
  169. }
  170. }
  171. // if we created a directory etc, then cleanup after ourselves
  172. if(!this->Makefile->GetCMakeInstance()->GetDebugTryCompile())
  173. {
  174. this->CleanupFiles(this->BinaryDirectory.c_str());
  175. }
  176. return true;
  177. }
  178. void cmTryRunCommand::RunExecutable(const std::string& runArgs,
  179. std::string* out)
  180. {
  181. int retVal = -1;
  182. std::string finalCommand;
  183. const std::string emulator =
  184. this->Makefile->GetSafeDefinition("CMAKE_CROSSCOMPILING_EMULATOR");
  185. if (!emulator.empty())
  186. {
  187. std::vector<std::string> emulatorWithArgs;
  188. cmSystemTools::ExpandListArgument(emulator, emulatorWithArgs);
  189. finalCommand += cmSystemTools::ConvertToRunCommandPath(
  190. emulatorWithArgs[0].c_str());
  191. finalCommand += " ";
  192. for (std::vector<std::string>::const_iterator ei =
  193. emulatorWithArgs.begin()+1;
  194. ei != emulatorWithArgs.end(); ++ei)
  195. {
  196. finalCommand += "\"";
  197. finalCommand += *ei;
  198. finalCommand += "\"";
  199. finalCommand += " ";
  200. }
  201. }
  202. finalCommand += cmSystemTools::ConvertToRunCommandPath(
  203. this->OutputFile.c_str());
  204. if (!runArgs.empty())
  205. {
  206. finalCommand += runArgs;
  207. }
  208. int timeout = 0;
  209. bool worked = cmSystemTools::RunSingleCommand(finalCommand.c_str(),
  210. out, out, &retVal,
  211. 0, cmSystemTools::OUTPUT_NONE, timeout);
  212. // set the run var
  213. char retChar[1000];
  214. if (worked)
  215. {
  216. sprintf(retChar, "%i", retVal);
  217. }
  218. else
  219. {
  220. strcpy(retChar, "FAILED_TO_RUN");
  221. }
  222. this->Makefile->AddCacheDefinition(this->RunResultVariable, retChar,
  223. "Result of TRY_RUN",
  224. cmState::INTERNAL);
  225. }
  226. /* This is only used when cross compiling. Instead of running the
  227. executable, two cache variables are created which will hold the results
  228. the executable would have produced.
  229. */
  230. void cmTryRunCommand::DoNotRunExecutable(const std::string& runArgs,
  231. const std::string& srcFile,
  232. std::string* out
  233. )
  234. {
  235. // copy the executable out of the CMakeFiles/ directory, so it is not
  236. // removed at the end of TRY_RUN and the user can run it manually
  237. // on the target platform.
  238. std::string copyDest = this->Makefile->GetHomeOutputDirectory();
  239. copyDest += cmake::GetCMakeFilesDirectory();
  240. copyDest += "/";
  241. copyDest += cmSystemTools::GetFilenameWithoutExtension(
  242. this->OutputFile);
  243. copyDest += "-";
  244. copyDest += this->RunResultVariable;
  245. copyDest += cmSystemTools::GetFilenameExtension(this->OutputFile);
  246. cmSystemTools::CopyFileAlways(this->OutputFile, copyDest);
  247. std::string resultFileName = this->Makefile->GetHomeOutputDirectory();
  248. resultFileName += "/TryRunResults.cmake";
  249. std::string detailsString = "For details see ";
  250. detailsString += resultFileName;
  251. std::string internalRunOutputName=this->RunResultVariable+"__TRYRUN_OUTPUT";
  252. bool error = false;
  253. if (this->Makefile->GetDefinition(this->RunResultVariable) == 0)
  254. {
  255. // if the variables doesn't exist, create it with a helpful error text
  256. // and mark it as advanced
  257. std::string comment;
  258. comment += "Run result of TRY_RUN(), indicates whether the executable "
  259. "would have been able to run on its target platform.\n";
  260. comment += detailsString;
  261. this->Makefile->AddCacheDefinition(this->RunResultVariable,
  262. "PLEASE_FILL_OUT-FAILED_TO_RUN",
  263. comment.c_str(),
  264. cmState::STRING);
  265. cmState* state = this->Makefile->GetState();
  266. const char* existingValue
  267. = state->GetCacheEntryValue(this->RunResultVariable);
  268. if (existingValue)
  269. {
  270. state->SetCacheEntryProperty(this->RunResultVariable, "ADVANCED", "1");
  271. }
  272. error = true;
  273. }
  274. // is the output from the executable used ?
  275. if (out!=0)
  276. {
  277. if (this->Makefile->GetDefinition(internalRunOutputName) == 0)
  278. {
  279. // if the variables doesn't exist, create it with a helpful error text
  280. // and mark it as advanced
  281. std::string comment;
  282. comment+="Output of TRY_RUN(), contains the text, which the executable "
  283. "would have printed on stdout and stderr on its target platform.\n";
  284. comment += detailsString;
  285. this->Makefile->AddCacheDefinition(internalRunOutputName,
  286. "PLEASE_FILL_OUT-NOTFOUND",
  287. comment.c_str(),
  288. cmState::STRING);
  289. cmState* state = this->Makefile->GetState();
  290. const char* existing =
  291. state->GetCacheEntryValue(internalRunOutputName);
  292. if (existing)
  293. {
  294. state->SetCacheEntryProperty(internalRunOutputName,
  295. "ADVANCED", "1");
  296. }
  297. error = true;
  298. }
  299. }
  300. if (error)
  301. {
  302. static bool firstTryRun = true;
  303. cmsys::ofstream file(resultFileName.c_str(),
  304. firstTryRun ? std::ios::out : std::ios::app);
  305. if ( file )
  306. {
  307. if (firstTryRun)
  308. {
  309. /* clang-format off */
  310. file << "# This file was generated by CMake because it detected "
  311. "TRY_RUN() commands\n"
  312. "# in crosscompiling mode. It will be overwritten by the next "
  313. "CMake run.\n"
  314. "# Copy it to a safe location, set the variables to "
  315. "appropriate values\n"
  316. "# and use it then to preset the CMake cache (using -C).\n\n";
  317. /* clang-format on */
  318. }
  319. std::string comment ="\n";
  320. comment += this->RunResultVariable;
  321. comment += "\n indicates whether the executable would have been able "
  322. "to run on its\n"
  323. " target platform. If so, set ";
  324. comment += this->RunResultVariable;
  325. comment += " to\n"
  326. " the exit code (in many cases 0 for success), otherwise "
  327. "enter \"FAILED_TO_RUN\".\n";
  328. if (out!=0)
  329. {
  330. comment += internalRunOutputName;
  331. comment += "\n contains the text the executable "
  332. "would have printed on stdout and stderr.\n"
  333. " If the executable would not have been able to run, set ";
  334. comment += internalRunOutputName;
  335. comment += " empty.\n"
  336. " Otherwise check if the output is evaluated by the "
  337. "calling CMake code. If so,\n"
  338. " check what the source file would have printed when "
  339. "called with the given arguments.\n";
  340. }
  341. comment += "The ";
  342. comment += this->CompileResultVariable;
  343. comment += " variable holds the build result for this TRY_RUN().\n\n"
  344. "Source file : ";
  345. comment += srcFile + "\n";
  346. comment += "Executable : ";
  347. comment += copyDest + "\n";
  348. comment += "Run arguments : ";
  349. comment += runArgs;
  350. comment += "\n";
  351. comment += " Called from: " + this->Makefile->FormatListFileStack();
  352. cmsys::SystemTools::ReplaceString(comment, "\n", "\n# ");
  353. file << comment << "\n\n";
  354. file << "set( " << this->RunResultVariable << " \n \""
  355. << this->Makefile->GetDefinition(this->RunResultVariable)
  356. << "\"\n CACHE STRING \"Result from TRY_RUN\" FORCE)\n\n";
  357. if (out!=0)
  358. {
  359. file << "set( " << internalRunOutputName << " \n \""
  360. << this->Makefile->GetDefinition(internalRunOutputName)
  361. << "\"\n CACHE STRING \"Output from TRY_RUN\" FORCE)\n\n";
  362. }
  363. file.close();
  364. }
  365. firstTryRun = false;
  366. std::string errorMessage = "TRY_RUN() invoked in cross-compiling mode, "
  367. "please set the following cache variables "
  368. "appropriately:\n";
  369. errorMessage += " " + this->RunResultVariable + " (advanced)\n";
  370. if (out!=0)
  371. {
  372. errorMessage += " " + internalRunOutputName + " (advanced)\n";
  373. }
  374. errorMessage += detailsString;
  375. cmSystemTools::Error(errorMessage.c_str());
  376. return;
  377. }
  378. if (out!=0)
  379. {
  380. (*out) = this->Makefile->GetDefinition(internalRunOutputName);
  381. }
  382. }