cmTryRunCommand.cxx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. // cmExecutableCommand
  17. bool cmTryRunCommand::InitialPass(std::vector<std::string> const& argv)
  18. {
  19. if(argv.size() < 4)
  20. {
  21. return false;
  22. }
  23. // build an arg list for TryCompile and extract the runArgs
  24. std::vector<std::string> tryCompile;
  25. std::string outputVariable;
  26. std::string runArgs;
  27. unsigned int i;
  28. for (i = 1; i < argv.size(); ++i)
  29. {
  30. if (argv[i] == "ARGS")
  31. {
  32. ++i;
  33. while (i < argv.size() && argv[i] != "COMPILE_DEFINITIONS" &&
  34. argv[i] != "CMAKE_FLAGS")
  35. {
  36. runArgs += " ";
  37. runArgs += argv[i];
  38. ++i;
  39. }
  40. if (i < argv.size())
  41. {
  42. tryCompile.push_back(argv[i]);
  43. }
  44. }
  45. else
  46. {
  47. tryCompile.push_back(argv[i]);
  48. if (argv[i] == "OUTPUT_VARIABLE")
  49. {
  50. if ( argv.size() <= (i+1) )
  51. {
  52. cmSystemTools::Error(
  53. "OUTPUT_VARIABLE specified but there is no variable");
  54. return false;
  55. }
  56. outputVariable = argv[i+1];
  57. }
  58. }
  59. }
  60. // do the try compile
  61. int res = cmTryCompileCommand::CoreTryCompileCode(this->Makefile,
  62. tryCompile, false);
  63. // now try running the command if it compiled
  64. std::string binaryDirectory = argv[2];
  65. binaryDirectory += cmake::GetCMakeFilesDirectory();
  66. binaryDirectory += "/CMakeTmp";
  67. if (!res)
  68. {
  69. int retVal = -1;
  70. std::string output;
  71. std::string command1 = binaryDirectory;
  72. std::vector<std::string> attemptedPaths;
  73. command1 += "/cmTryCompileExec";
  74. command1 += cmSystemTools::GetExecutableExtension();
  75. std::string fullPath;
  76. if(cmSystemTools::FileExists(command1.c_str()))
  77. {
  78. fullPath = cmSystemTools::CollapseFullPath(command1.c_str());
  79. }
  80. attemptedPaths.push_back(command1);
  81. command1 = binaryDirectory;
  82. // try CMAKE_TRY_COMPILE_CONFIGURATION if it is set
  83. if (fullPath.empty())
  84. {
  85. const char* config =
  86. this->Makefile->GetDefinition("CMAKE_TRY_COMPILE_CONFIGURATION");
  87. // if a config was specified try that first
  88. if (config && config[0])
  89. {
  90. command1 += "/";
  91. command1 += config;
  92. command1 += "/cmTryCompileExec";
  93. command1 += cmSystemTools::GetExecutableExtension();
  94. if(cmSystemTools::FileExists(command1.c_str()))
  95. {
  96. fullPath = cmSystemTools::CollapseFullPath(command1.c_str());
  97. }
  98. attemptedPaths.push_back(command1);
  99. }
  100. }
  101. // try Debug if still not found
  102. if (fullPath.empty())
  103. {
  104. command1 = binaryDirectory;
  105. command1 += "/Debug/cmTryCompileExec";
  106. command1 += cmSystemTools::GetExecutableExtension();
  107. if(cmSystemTools::FileExists(command1.c_str()))
  108. {
  109. fullPath = cmSystemTools::CollapseFullPath(command1.c_str());
  110. }
  111. attemptedPaths.push_back(command1);
  112. }
  113. // try Deployment if still not found
  114. if (fullPath.empty())
  115. {
  116. command1 = binaryDirectory;
  117. command1 += "/Development/cmTryCompileExec";
  118. command1 += cmSystemTools::GetExecutableExtension();
  119. if(cmSystemTools::FileExists(command1.c_str()))
  120. {
  121. fullPath = cmSystemTools::CollapseFullPath(command1.c_str());
  122. }
  123. attemptedPaths.push_back(command1);
  124. }
  125. if (fullPath.empty())
  126. {
  127. cmOStringStream emsg;
  128. emsg << "Unable to find executable for TRY_RUN: tried \"";
  129. for (i = 0; i < attemptedPaths.size(); ++i)
  130. {
  131. emsg << attemptedPaths[i];
  132. if (i < attemptedPaths.size() - 1)
  133. {
  134. emsg << "\" and \"";
  135. }
  136. else
  137. {
  138. emsg << "\".";
  139. }
  140. }
  141. cmSystemTools::Error(emsg.str().c_str());
  142. }
  143. if (fullPath.size() > 1)
  144. {
  145. std::string finalCommand = fullPath;
  146. finalCommand = cmSystemTools::ConvertToRunCommandPath(fullPath.c_str());
  147. if(runArgs.size())
  148. {
  149. finalCommand += runArgs;
  150. }
  151. int timeout = 0;
  152. bool worked = cmSystemTools::RunSingleCommand(finalCommand.c_str(),
  153. &output, &retVal,
  154. 0, false, timeout);
  155. if(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(outputVariable.c_str());
  161. if(compileOutput)
  162. {
  163. output = std::string(compileOutput) + output;
  164. }
  165. this->Makefile->AddDefinition(outputVariable.c_str(), output.c_str());
  166. }
  167. // set the run var
  168. char retChar[1000];
  169. if(worked)
  170. {
  171. sprintf(retChar,"%i",retVal);
  172. }
  173. else
  174. {
  175. strcpy(retChar, "FAILED_TO_RUN");
  176. }
  177. this->Makefile->AddCacheDefinition(argv[0].c_str(), retChar,
  178. "Result of TRY_RUN",
  179. cmCacheManager::INTERNAL);
  180. }
  181. }
  182. // if we created a directory etc, then cleanup after ourselves
  183. std::string cacheFile = binaryDirectory;
  184. cacheFile += "/CMakeLists.txt";
  185. if(!this->Makefile->GetCMakeInstance()->GetDebugTryCompile())
  186. {
  187. cmTryCompileCommand::CleanupFiles(binaryDirectory.c_str());
  188. }
  189. return true;
  190. }