cmTryRunCommand.cxx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Insight Consortium. All rights reserved.
  8. See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm 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 runArgs;
  26. int i;
  27. for (i = 1; i < argv.size(); ++i)
  28. {
  29. if (argv[i] == "ARGS")
  30. {
  31. ++i;
  32. while (i < argv.size() && argv[i] != "COMPILE_DEFINITIONS" &&
  33. argv[i] != "CMAKE_FLAGS")
  34. {
  35. runArgs += " ";
  36. runArgs += argv[i];
  37. ++i;
  38. }
  39. if (i < argv.size())
  40. {
  41. tryCompile.push_back(argv[i]);
  42. }
  43. }
  44. else
  45. {
  46. tryCompile.push_back(argv[i]);
  47. }
  48. }
  49. // do the try compile
  50. int res = cmTryCompileCommand::CoreTryCompileCode(m_Makefile, tryCompile, false);
  51. // now try running the command if it compiled
  52. std::string binaryDirectory = argv[2] + "/CMakeTmp";
  53. if (!res)
  54. {
  55. int retVal;
  56. std::string output;
  57. std::string command;
  58. command = binaryDirectory;
  59. command += "/cmTryCompileExec";
  60. command += cmSystemTools::GetExecutableExtension();
  61. std::string fullPath;
  62. if(cmSystemTools::FileExists(command.c_str()))
  63. {
  64. fullPath = cmSystemTools::CollapseFullPath(command.c_str());
  65. }
  66. else
  67. {
  68. command = binaryDirectory;
  69. command += "/Debug/cmTryCompileExec";
  70. command += cmSystemTools::GetExecutableExtension();
  71. if(cmSystemTools::FileExists(command.c_str()))
  72. {
  73. fullPath = cmSystemTools::CollapseFullPath(command.c_str());
  74. }
  75. else
  76. {
  77. cmSystemTools::Error("Unable to find executable for TRY_RUN",
  78. command.c_str());
  79. }
  80. }
  81. if (fullPath.size() > 1)
  82. {
  83. std::string finalCommand = fullPath;
  84. if(runArgs.size())
  85. {
  86. finalCommand = cmSystemTools::ConvertToOutputPath(fullPath.c_str());
  87. finalCommand += runArgs;
  88. }
  89. cmSystemTools::RunCommand(finalCommand.c_str(), output, retVal, 0, false);
  90. // set the run var
  91. char retChar[1000];
  92. sprintf(retChar,"%i",retVal);
  93. m_Makefile->AddDefinition(argv[0].c_str(), retChar);
  94. }
  95. }
  96. // if we created a directory etc, then cleanup after ourselves
  97. cmDirectory dir;
  98. dir.Load(binaryDirectory.c_str());
  99. size_t fileNum;
  100. for (fileNum = 0; fileNum < dir.GetNumberOfFiles(); ++fileNum)
  101. {
  102. if (strcmp(dir.GetFile(fileNum),".") &&
  103. strcmp(dir.GetFile(fileNum),".."))
  104. {
  105. std::string fullPath = binaryDirectory;
  106. fullPath += "/";
  107. fullPath += dir.GetFile(fileNum);
  108. cmSystemTools::RemoveFile(fullPath.c_str());
  109. }
  110. }
  111. std::string cacheFile = binaryDirectory;
  112. cacheFile += "/CMakeLists.txt";
  113. cmListFileCache::GetInstance()->FlushCache(cacheFile.c_str());
  114. return true;
  115. }