cmTryCompileCommand.cxx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 "cmTryCompileCommand.h"
  14. #include "cmCacheManager.h"
  15. // cmExecutableCommand
  16. bool cmTryCompileCommand::InitialPass(std::vector<std::string> const& argv)
  17. {
  18. if(argv.size() < 3)
  19. {
  20. return false;
  21. }
  22. // which signature were we called with ?
  23. bool srcFileSignature = true;
  24. // look for CMAKE_FLAGS and store them
  25. std::vector<std::string> cmakeFlags;
  26. int i;
  27. for (i = 3; i < argv.size(); ++i)
  28. {
  29. if (argv[i] == "CMAKE_FLAGS")
  30. {
  31. for (; i < argv.size(); ++i)
  32. {
  33. cmakeFlags.push_back(argv[i]);
  34. }
  35. }
  36. else
  37. {
  38. srcFileSignature = false;
  39. }
  40. }
  41. // where will the binaries be stored
  42. const char* binaryDirectory = argv[1].c_str();
  43. const char* sourceDirectory = argv[2].c_str();
  44. const char* projectName = 0;
  45. const char* targetName = 0;
  46. std::string tmpString;
  47. // compute the binary dir when TRY_COMPILE is called with a src file
  48. // signature
  49. if (srcFileSignature)
  50. {
  51. tmpString = argv[1] + "/CMakeTmp";
  52. binaryDirectory = tmpString.c_str();
  53. }
  54. // make sure the binary directory exists
  55. cmSystemTools::MakeDirectory(binaryDirectory);
  56. // do not allow recursive try Compiles
  57. if (!strcmp(binaryDirectory,m_Makefile->GetHomeOutputDirectory()))
  58. {
  59. cmSystemTools::Error("Attempt at a recursive or nested TRY_COMPILE",
  60. binaryDirectory);
  61. return false;
  62. }
  63. // which signature are we using? If we are using var srcfile bindir
  64. if (srcFileSignature)
  65. {
  66. // remove any CMakeCache.txt files so we will have a clean test
  67. std::string ccFile = tmpString + "/CMakeCache.txt";
  68. cmSystemTools::RemoveFile(ccFile.c_str());
  69. // we need to create a directory and CMakeList file etc...
  70. // first create the directories
  71. sourceDirectory = binaryDirectory;
  72. // now create a CMakeList.txt file in that directory
  73. std::string outFileName = tmpString + "/CMakeLists.txt";
  74. FILE *fout = fopen(outFileName.c_str(),"w");
  75. if (!fout)
  76. {
  77. cmSystemTools::Error("Failed to create CMakeList file for ",
  78. outFileName.c_str());
  79. return false;
  80. }
  81. fprintf(fout,"PROJECT(CMAKE_TRY_COMPILE)\n");
  82. fprintf(fout, "IF (CMAKE_ANSI_CXXFLAGS)\n");
  83. fprintf(fout, " SET(CMAKE_CXX_FLAGS \"${CMAKE_CXX_FLAGS} ${CMAKE_ANSI_CXXFLAGS}\")\n");
  84. fprintf(fout, "ENDIF (CMAKE_ANSI_CXXFLAGS)\n");
  85. fprintf(fout,"ADD_EXECUTABLE(cmTryCompileExec \"%s\")\n",argv[2].c_str());
  86. fclose(fout);
  87. projectName = "CMAKE_TRY_COMPILE";
  88. targetName = "cmTryCompileExec";
  89. }
  90. // else the srcdir bindir project target signature
  91. else
  92. {
  93. projectName = argv[3].c_str();
  94. if (argv.size() == 5)
  95. {
  96. targetName = argv[4].c_str();
  97. }
  98. }
  99. // actually do the try compile now that everything is setup
  100. int res = m_Makefile->TryCompile(sourceDirectory, binaryDirectory,
  101. projectName, targetName, &cmakeFlags);
  102. // set the result var to the return value to indicate success or failure
  103. m_Makefile->AddDefinition(argv[0].c_str(), (res == 0 ? "TRUE" : "FALSE"));
  104. // if we created a directory etc, then cleanup after ourselves
  105. if (srcFileSignature)
  106. {
  107. cmDirectory dir;
  108. dir.Load(binaryDirectory);
  109. size_t fileNum;
  110. for (fileNum = 0; fileNum < dir.GetNumberOfFiles(); ++fileNum)
  111. {
  112. if (strcmp(dir.GetFile(fileNum),".") &&
  113. strcmp(dir.GetFile(fileNum),".."))
  114. {
  115. std::string fullPath = binaryDirectory;
  116. fullPath += "/";
  117. fullPath += dir.GetFile(fileNum);
  118. cmSystemTools::RemoveFile(fullPath.c_str());
  119. }
  120. }
  121. }
  122. return true;
  123. }