cmTryRunCommand.cxx 13 KB

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