cmTryRunCommand.cxx 13 KB

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