cmTryRunCommand.cxx 12 KB

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