cmTryRunCommand.cxx 13 KB

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