cmTryRunCommand.cxx 14 KB

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