1
0

cmTryRunCommand.cxx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. if ( m_Makefile->GetLocal() )
  24. {
  25. return true;
  26. }
  27. // build an arg list for TryCompile and extract the runArgs
  28. std::vector<std::string> tryCompile;
  29. std::string outputVariable;
  30. std::string runArgs;
  31. unsigned int i;
  32. for (i = 1; i < argv.size(); ++i)
  33. {
  34. if (argv[i] == "ARGS")
  35. {
  36. ++i;
  37. while (i < argv.size() && argv[i] != "COMPILE_DEFINITIONS" &&
  38. argv[i] != "CMAKE_FLAGS")
  39. {
  40. runArgs += " ";
  41. runArgs += argv[i];
  42. ++i;
  43. }
  44. if (i < argv.size())
  45. {
  46. tryCompile.push_back(argv[i]);
  47. }
  48. }
  49. else
  50. {
  51. tryCompile.push_back(argv[i]);
  52. if (argv[i] == "OUTPUT_VARIABLE")
  53. {
  54. if ( argv.size() <= (i+1) )
  55. {
  56. cmSystemTools::Error(
  57. "OUTPUT_VARIABLE specified but there is no variable");
  58. return false;
  59. }
  60. outputVariable = argv[i+1];
  61. }
  62. }
  63. }
  64. // do the try compile
  65. int res = cmTryCompileCommand::CoreTryCompileCode(m_Makefile, tryCompile, false);
  66. // now try running the command if it compiled
  67. std::string binaryDirectory = argv[2] + "/CMakeTmp";
  68. if (!res)
  69. {
  70. int retVal = -1;
  71. std::string output;
  72. std::string command;
  73. command = binaryDirectory;
  74. command += "/cmTryCompileExec";
  75. command += cmSystemTools::GetExecutableExtension();
  76. std::string fullPath;
  77. if(cmSystemTools::FileExists(command.c_str()))
  78. {
  79. fullPath = cmSystemTools::CollapseFullPath(command.c_str());
  80. }
  81. else
  82. {
  83. command = binaryDirectory;
  84. command += "/Debug/cmTryCompileExec";
  85. command += cmSystemTools::GetExecutableExtension();
  86. if(cmSystemTools::FileExists(command.c_str()))
  87. {
  88. fullPath = cmSystemTools::CollapseFullPath(command.c_str());
  89. }
  90. else
  91. {
  92. cmSystemTools::Error("Unable to find executable for TRY_RUN",
  93. command.c_str());
  94. }
  95. }
  96. if (fullPath.size() > 1)
  97. {
  98. std::string finalCommand = fullPath;
  99. finalCommand = cmSystemTools::ConvertToRunCommandPath(fullPath.c_str());
  100. if(runArgs.size())
  101. {
  102. finalCommand += runArgs;
  103. }
  104. int timeout = 0;
  105. bool worked = cmSystemTools::RunSingleCommand(finalCommand.c_str(),
  106. &output, &retVal,
  107. 0, false, timeout);
  108. if(outputVariable.size())
  109. {
  110. // if the TryCompileCore saved output in this outputVariable then
  111. // prepend that output to this output
  112. const char* compileOutput
  113. = m_Makefile->GetDefinition(outputVariable.c_str());
  114. if(compileOutput)
  115. {
  116. output = std::string(compileOutput) + output;
  117. }
  118. m_Makefile->AddDefinition(outputVariable.c_str(), output.c_str());
  119. }
  120. // set the run var
  121. char retChar[1000];
  122. if(worked)
  123. {
  124. sprintf(retChar,"%i",retVal);
  125. }
  126. else
  127. {
  128. strcpy(retChar, "FAILED_TO_RUN");
  129. }
  130. m_Makefile->AddCacheDefinition(argv[0].c_str(), retChar,
  131. "Result of TRY_RUN",
  132. cmCacheManager::INTERNAL);
  133. }
  134. }
  135. // if we created a directory etc, then cleanup after ourselves
  136. std::string cacheFile = binaryDirectory;
  137. cacheFile += "/CMakeLists.txt";
  138. cmListFileCache::GetInstance()->FlushCache(cacheFile.c_str());
  139. if(!m_Makefile->GetCMakeInstance()->GetDebugTryCompile())
  140. {
  141. cmTryCompileCommand::CleanupFiles(binaryDirectory.c_str());
  142. }
  143. return true;
  144. }