cmTryRunCommand.cxx 4.4 KB

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