cmTryCompileCommand.cxx 5.3 KB

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