cmTryCompileCommand.cxx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. #include "cmListFileCache.h"
  16. int cmTryCompileCommand::CoreTryCompileCode(
  17. cmMakefile *mf, std::vector<std::string> const& argv, bool clean)
  18. {
  19. // which signature were we called with ?
  20. bool srcFileSignature = false;
  21. unsigned int i;
  22. // where will the binaries be stored
  23. const char* binaryDirectory = argv[1].c_str();
  24. const char* sourceDirectory = argv[2].c_str();
  25. const char* projectName = 0;
  26. const char* targetName = 0;
  27. std::string tmpString;
  28. // do we have a srcfile signature
  29. if (argv.size() == 3 || argv[3] == "CMAKE_FLAGS" || argv[3] == "COMPILE_DEFINITIONS")
  30. {
  31. srcFileSignature = true;
  32. }
  33. // look for CMAKE_FLAGS and store them
  34. std::vector<std::string> cmakeFlags;
  35. for (i = 3; i < argv.size(); ++i)
  36. {
  37. if (argv[i] == "CMAKE_FLAGS")
  38. {
  39. for (; i < argv.size() && argv[i] != "COMPILE_DEFINITIONS";
  40. ++i)
  41. {
  42. cmakeFlags.push_back(argv[i]);
  43. }
  44. break;
  45. }
  46. }
  47. // look for COMPILE_DEFINITIONS and store them
  48. std::vector<std::string> compileFlags;
  49. for (i = 3; i < argv.size(); ++i)
  50. {
  51. if (argv[i] == "COMPILE_DEFINITIONS")
  52. {
  53. // only valid for srcfile signatures
  54. if (!srcFileSignature)
  55. {
  56. cmSystemTools::Error(
  57. "COMPILE_FLAGS specified on a srcdir type TRY_COMPILE");
  58. return -1;
  59. }
  60. for (i = i + 1; i < argv.size() && argv[i] != "CMAKE_FLAGS";
  61. ++i)
  62. {
  63. compileFlags.push_back(argv[i]);
  64. }
  65. break;
  66. }
  67. }
  68. // compute the binary dir when TRY_COMPILE is called with a src file
  69. // signature
  70. if (srcFileSignature)
  71. {
  72. tmpString = argv[1] + "/CMakeTmp";
  73. binaryDirectory = tmpString.c_str();
  74. }
  75. // make sure the binary directory exists
  76. cmSystemTools::MakeDirectory(binaryDirectory);
  77. // do not allow recursive try Compiles
  78. if (!strcmp(binaryDirectory,mf->GetHomeOutputDirectory()))
  79. {
  80. cmSystemTools::Error("Attempt at a recursive or nested TRY_COMPILE",
  81. binaryDirectory);
  82. return -1;
  83. }
  84. std::string outFileName = tmpString + "/CMakeLists.txt";
  85. // which signature are we using? If we are using var srcfile bindir
  86. if (srcFileSignature)
  87. {
  88. // remove any CMakeCache.txt files so we will have a clean test
  89. std::string ccFile = tmpString + "/CMakeCache.txt";
  90. cmSystemTools::RemoveFile(ccFile.c_str());
  91. // we need to create a directory and CMakeList file etc...
  92. // first create the directories
  93. sourceDirectory = binaryDirectory;
  94. // now create a CMakeList.txt file in that directory
  95. FILE *fout = fopen(outFileName.c_str(),"w");
  96. if (!fout)
  97. {
  98. cmSystemTools::Error("Failed to create CMakeList file for ",
  99. outFileName.c_str());
  100. return -1;
  101. }
  102. fprintf(fout, "PROJECT(CMAKE_TRY_COMPILE)\n");
  103. fprintf(fout, "IF (CMAKE_ANSI_CXXFLAGS)\n");
  104. fprintf(fout, " SET(CMAKE_CXX_FLAGS \"${CMAKE_CXX_FLAGS} ${CMAKE_ANSI_CXXFLAGS}\")\n");
  105. fprintf(fout, "ENDIF (CMAKE_ANSI_CXXFLAGS)\n");
  106. // handle any compile flags we need to pass on
  107. if (compileFlags.size())
  108. {
  109. fprintf(fout, "ADD_DEFINITIONS( ");
  110. for (i = 0; i < compileFlags.size(); ++i)
  111. {
  112. fprintf(fout,"%s ",compileFlags[i].c_str());
  113. }
  114. fprintf(fout, ")\n");
  115. }
  116. fprintf(fout, "ADD_EXECUTABLE(cmTryCompileExec \"%s\")\n",argv[2].c_str());
  117. fclose(fout);
  118. projectName = "CMAKE_TRY_COMPILE";
  119. targetName = "cmTryCompileExec";
  120. }
  121. // else the srcdir bindir project target signature
  122. else
  123. {
  124. projectName = argv[3].c_str();
  125. if (argv.size() == 5)
  126. {
  127. targetName = argv[4].c_str();
  128. }
  129. }
  130. // actually do the try compile now that everything is setup
  131. int res = mf->TryCompile(sourceDirectory, binaryDirectory,
  132. projectName, targetName, &cmakeFlags);
  133. // set the result var to the return value to indicate success or failure
  134. mf->AddDefinition(argv[0].c_str(), (res == 0 ? "TRUE" : "FALSE"));
  135. // if They specified clean then we clean up what we can
  136. if (srcFileSignature && clean)
  137. {
  138. cmDirectory dir;
  139. dir.Load(binaryDirectory);
  140. size_t fileNum;
  141. for (fileNum = 0; fileNum < dir.GetNumberOfFiles(); ++fileNum)
  142. {
  143. if (strcmp(dir.GetFile(fileNum),".") &&
  144. strcmp(dir.GetFile(fileNum),".."))
  145. {
  146. std::string fullPath = binaryDirectory;
  147. fullPath += "/";
  148. fullPath += dir.GetFile(fileNum);
  149. cmSystemTools::RemoveFile(fullPath.c_str());
  150. }
  151. }
  152. cmListFileCache::GetInstance()->FlushCache(outFileName.c_str());
  153. }
  154. return res;
  155. }
  156. // cmExecutableCommand
  157. bool cmTryCompileCommand::InitialPass(std::vector<std::string> const& argv)
  158. {
  159. if(argv.size() < 3)
  160. {
  161. return false;
  162. }
  163. cmTryCompileCommand::CoreTryCompileCode(m_Makefile,argv,true);
  164. return true;
  165. }