cmTryRunCommand.cxx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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(m_Makefile, tryCompile, false);
  62. // now try running the command if it compiled
  63. std::string binaryDirectory = argv[2] + "/CMakeFiles/CMakeTmp";
  64. if (!res)
  65. {
  66. int retVal = -1;
  67. std::string output;
  68. std::string command1 = binaryDirectory;
  69. command1 += "/cmTryCompileExec";
  70. command1 += cmSystemTools::GetExecutableExtension();
  71. std::string fullPath;
  72. if(cmSystemTools::FileExists(command1.c_str()))
  73. {
  74. fullPath = cmSystemTools::CollapseFullPath(command1.c_str());
  75. }
  76. else
  77. {
  78. std::string command2 = binaryDirectory;
  79. command2 += "/Debug/cmTryCompileExec";
  80. command2 += cmSystemTools::GetExecutableExtension();
  81. if(cmSystemTools::FileExists(command2.c_str()))
  82. {
  83. fullPath = cmSystemTools::CollapseFullPath(command2.c_str());
  84. }
  85. else
  86. {
  87. std::string command3 = binaryDirectory;
  88. command3 += "/Development/cmTryCompileExec";
  89. command3 += cmSystemTools::GetExecutableExtension();
  90. if(cmSystemTools::FileExists(command3.c_str()))
  91. {
  92. fullPath = cmSystemTools::CollapseFullPath(command3.c_str());
  93. }
  94. else
  95. {
  96. cmOStringStream emsg;
  97. emsg << "Unable to find executable for TRY_RUN: tried \""
  98. << command1 << "\" and \""
  99. << command2 << "\" and \""
  100. << command3 << "\".";
  101. cmSystemTools::Error(emsg.str().c_str());
  102. }
  103. }
  104. }
  105. if (fullPath.size() > 1)
  106. {
  107. std::string finalCommand = fullPath;
  108. finalCommand = cmSystemTools::ConvertToRunCommandPath(fullPath.c_str());
  109. if(runArgs.size())
  110. {
  111. finalCommand += runArgs;
  112. }
  113. int timeout = 0;
  114. bool worked = cmSystemTools::RunSingleCommand(finalCommand.c_str(),
  115. &output, &retVal,
  116. 0, false, timeout);
  117. if(outputVariable.size())
  118. {
  119. // if the TryCompileCore saved output in this outputVariable then
  120. // prepend that output to this output
  121. const char* compileOutput
  122. = m_Makefile->GetDefinition(outputVariable.c_str());
  123. if(compileOutput)
  124. {
  125. output = std::string(compileOutput) + output;
  126. }
  127. m_Makefile->AddDefinition(outputVariable.c_str(), output.c_str());
  128. }
  129. // set the run var
  130. char retChar[1000];
  131. if(worked)
  132. {
  133. sprintf(retChar,"%i",retVal);
  134. }
  135. else
  136. {
  137. strcpy(retChar, "FAILED_TO_RUN");
  138. }
  139. m_Makefile->AddCacheDefinition(argv[0].c_str(), retChar,
  140. "Result of TRY_RUN",
  141. cmCacheManager::INTERNAL);
  142. }
  143. }
  144. // if we created a directory etc, then cleanup after ourselves
  145. std::string cacheFile = binaryDirectory;
  146. cacheFile += "/CMakeLists.txt";
  147. cmListFileCache::GetInstance()->FlushCache(cacheFile.c_str());
  148. if(!m_Makefile->GetCMakeInstance()->GetDebugTryCompile())
  149. {
  150. cmTryCompileCommand::CleanupFiles(binaryDirectory.c_str());
  151. }
  152. return true;
  153. }