cmTryRunCommand.cxx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 runArgs;
  30. unsigned int i;
  31. for (i = 1; i < argv.size(); ++i)
  32. {
  33. if (argv[i] == "ARGS")
  34. {
  35. ++i;
  36. while (i < argv.size() && argv[i] != "COMPILE_DEFINITIONS" &&
  37. argv[i] != "CMAKE_FLAGS")
  38. {
  39. runArgs += " ";
  40. runArgs += argv[i];
  41. ++i;
  42. }
  43. if (i < argv.size())
  44. {
  45. tryCompile.push_back(argv[i]);
  46. }
  47. }
  48. else
  49. {
  50. tryCompile.push_back(argv[i]);
  51. }
  52. }
  53. // do the try compile
  54. int res = cmTryCompileCommand::CoreTryCompileCode(m_Makefile, tryCompile, false);
  55. // now try running the command if it compiled
  56. std::string binaryDirectory = argv[2] + "/CMakeTmp";
  57. if (!res)
  58. {
  59. int retVal;
  60. std::string output;
  61. std::string command;
  62. command = binaryDirectory;
  63. command += "/cmTryCompileExec";
  64. command += cmSystemTools::GetExecutableExtension();
  65. std::string fullPath;
  66. if(cmSystemTools::FileExists(command.c_str()))
  67. {
  68. fullPath = cmSystemTools::CollapseFullPath(command.c_str());
  69. }
  70. else
  71. {
  72. command = binaryDirectory;
  73. command += "/Debug/cmTryCompileExec";
  74. command += cmSystemTools::GetExecutableExtension();
  75. if(cmSystemTools::FileExists(command.c_str()))
  76. {
  77. fullPath = cmSystemTools::CollapseFullPath(command.c_str());
  78. }
  79. else
  80. {
  81. cmSystemTools::Error("Unable to find executable for TRY_RUN",
  82. command.c_str());
  83. }
  84. }
  85. if (fullPath.size() > 1)
  86. {
  87. std::string finalCommand = fullPath;
  88. finalCommand = cmSystemTools::ConvertToOutputPath(fullPath.c_str());
  89. if(runArgs.size())
  90. {
  91. finalCommand += runArgs;
  92. }
  93. cmSystemTools::RunCommand(finalCommand.c_str(), output, retVal, 0, false);
  94. // set the run var
  95. char retChar[1000];
  96. sprintf(retChar,"%i",retVal);
  97. m_Makefile->AddCacheDefinition(argv[0].c_str(), retChar,
  98. "Result of TRY_RUN", cmCacheManager::INTERNAL);
  99. }
  100. }
  101. // if we created a directory etc, then cleanup after ourselves
  102. std::string cacheFile = binaryDirectory;
  103. cacheFile += "/CMakeLists.txt";
  104. cmListFileCache::GetInstance()->FlushCache(cacheFile.c_str());
  105. cmTryCompileCommand::CleanupFiles(binaryDirectory.c_str());
  106. return true;
  107. }