cmTryCompileCommand.cxx 6.2 KB

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