cmTryRunCommand.cxx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmTryRunCommand.h"
  14. #include "cmCacheManager.h"
  15. #include "cmTryCompileCommand.h"
  16. // cmTryRunCommand
  17. bool cmTryRunCommand::InitialPass(std::vector<std::string> const& argv)
  18. {
  19. if(argv.size() < 4)
  20. {
  21. return false;
  22. }
  23. if (this->Makefile->IsOn("CMAKE_CROSSCOMPILING"))
  24. {
  25. this->SetError("doesn't work when crosscompiling.");
  26. cmSystemTools::SetFatalErrorOccured();
  27. return false;
  28. }
  29. // build an arg list for TryCompile and extract the runArgs
  30. std::vector<std::string> tryCompile;
  31. std::string outputVariable;
  32. std::string runArgs;
  33. unsigned int i;
  34. for (i = 1; i < argv.size(); ++i)
  35. {
  36. if (argv[i] == "ARGS")
  37. {
  38. ++i;
  39. while (i < argv.size() && argv[i] != "COMPILE_DEFINITIONS" &&
  40. argv[i] != "CMAKE_FLAGS")
  41. {
  42. runArgs += " ";
  43. runArgs += argv[i];
  44. ++i;
  45. }
  46. if (i < argv.size())
  47. {
  48. tryCompile.push_back(argv[i]);
  49. }
  50. }
  51. else
  52. {
  53. tryCompile.push_back(argv[i]);
  54. if (argv[i] == "OUTPUT_VARIABLE")
  55. {
  56. if ( argv.size() <= (i+1) )
  57. {
  58. cmSystemTools::Error(
  59. "OUTPUT_VARIABLE specified but there is no variable");
  60. return false;
  61. }
  62. outputVariable = argv[i+1];
  63. }
  64. }
  65. }
  66. // do the try compile
  67. int res = cmTryCompileCommand::CoreTryCompileCode(this->Makefile,
  68. tryCompile, false);
  69. // now try running the command if it compiled
  70. std::string binaryDirectory = argv[2];
  71. binaryDirectory += cmake::GetCMakeFilesDirectory();
  72. binaryDirectory += "/CMakeTmp";
  73. if (!res)
  74. {
  75. int retVal = -1;
  76. std::string output;
  77. std::string executableSuffix=this->Makefile->GetDefinition("CMAKE_EXECUTABLE_SUFFIX");
  78. std::string command1 = binaryDirectory;
  79. std::vector<std::string> attemptedPaths;
  80. command1 += "/cmTryCompileExec";
  81. command1 += executableSuffix;
  82. std::string fullPath;
  83. if(cmSystemTools::FileExists(command1.c_str()))
  84. {
  85. fullPath = cmSystemTools::CollapseFullPath(command1.c_str());
  86. }
  87. attemptedPaths.push_back(command1);
  88. command1 = binaryDirectory;
  89. // try CMAKE_TRY_COMPILE_CONFIGURATION if it is set
  90. if (fullPath.empty())
  91. {
  92. const char* config =
  93. this->Makefile->GetDefinition("CMAKE_TRY_COMPILE_CONFIGURATION");
  94. // if a config was specified try that first
  95. if (config && config[0])
  96. {
  97. command1 += "/";
  98. command1 += config;
  99. command1 += "/cmTryCompileExec";
  100. command1 += executableSuffix;
  101. if(cmSystemTools::FileExists(command1.c_str()))
  102. {
  103. fullPath = cmSystemTools::CollapseFullPath(command1.c_str());
  104. }
  105. attemptedPaths.push_back(command1);
  106. }
  107. }
  108. // try Debug if still not found
  109. if (fullPath.empty())
  110. {
  111. command1 = binaryDirectory;
  112. command1 += "/Debug/cmTryCompileExec";
  113. command1 += executableSuffix;
  114. if(cmSystemTools::FileExists(command1.c_str()))
  115. {
  116. fullPath = cmSystemTools::CollapseFullPath(command1.c_str());
  117. }
  118. attemptedPaths.push_back(command1);
  119. }
  120. // try Deployment if still not found
  121. if (fullPath.empty())
  122. {
  123. command1 = binaryDirectory;
  124. command1 += "/Development/cmTryCompileExec";
  125. command1 += executableSuffix;
  126. if(cmSystemTools::FileExists(command1.c_str()))
  127. {
  128. fullPath = cmSystemTools::CollapseFullPath(command1.c_str());
  129. }
  130. attemptedPaths.push_back(command1);
  131. }
  132. if (fullPath.empty())
  133. {
  134. cmOStringStream emsg;
  135. emsg << "Unable to find executable for TRY_RUN: tried \"";
  136. for (i = 0; i < attemptedPaths.size(); ++i)
  137. {
  138. emsg << attemptedPaths[i];
  139. if (i < attemptedPaths.size() - 1)
  140. {
  141. emsg << "\" and \"";
  142. }
  143. else
  144. {
  145. emsg << "\".";
  146. }
  147. }
  148. cmSystemTools::Error(emsg.str().c_str());
  149. }
  150. if (fullPath.size() > 1)
  151. {
  152. std::string finalCommand = fullPath;
  153. finalCommand = cmSystemTools::ConvertToRunCommandPath(fullPath.c_str());
  154. if(runArgs.size())
  155. {
  156. finalCommand += runArgs;
  157. }
  158. int timeout = 0;
  159. bool worked = cmSystemTools::RunSingleCommand(finalCommand.c_str(),
  160. &output, &retVal,
  161. 0, false, timeout);
  162. if(outputVariable.size())
  163. {
  164. // if the TryCompileCore saved output in this outputVariable then
  165. // prepend that output to this output
  166. const char* compileOutput
  167. = this->Makefile->GetDefinition(outputVariable.c_str());
  168. if(compileOutput)
  169. {
  170. output = std::string(compileOutput) + output;
  171. }
  172. this->Makefile->AddDefinition(outputVariable.c_str(), output.c_str());
  173. }
  174. // set the run var
  175. char retChar[1000];
  176. if(worked)
  177. {
  178. sprintf(retChar,"%i",retVal);
  179. }
  180. else
  181. {
  182. strcpy(retChar, "FAILED_TO_RUN");
  183. }
  184. this->Makefile->AddCacheDefinition(argv[0].c_str(), retChar,
  185. "Result of TRY_RUN",
  186. cmCacheManager::INTERNAL);
  187. }
  188. }
  189. // if we created a directory etc, then cleanup after ourselves
  190. std::string cacheFile = binaryDirectory;
  191. cacheFile += "/CMakeLists.txt";
  192. if(!this->Makefile->GetCMakeInstance()->GetDebugTryCompile())
  193. {
  194. cmTryCompileCommand::CleanupFiles(binaryDirectory.c_str());
  195. }
  196. return true;
  197. }