cmTryCompileCommand.cxx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html 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. std::string source = argv[2];
  122. cmSystemTools::e_FileFormat format =
  123. cmSystemTools::GetFileFormat(
  124. cmSystemTools::GetFilenameExtension(source).c_str());
  125. if ( format == cmSystemTools::C_FILE_FORMAT )
  126. {
  127. fprintf(fout, "PROJECT(CMAKE_TRY_COMPILE C)\n");
  128. }
  129. else if ( format == cmSystemTools::CXX_FILE_FORMAT )
  130. {
  131. fprintf(fout, "PROJECT(CMAKE_TRY_COMPILE CXX)\n");
  132. }
  133. else
  134. {
  135. cmSystemTools::Error("Unknown file format for file: ", source.c_str(),
  136. "; TRY_COMPILE only works for C and CXX files");
  137. return -1;
  138. }
  139. if ( format == cmSystemTools::CXX_FILE_FORMAT )
  140. {
  141. fprintf(fout, "IF (CMAKE_ANSI_CXXFLAGS)\n");
  142. fprintf(fout, " SET(CMAKE_CXX_FLAGS \"${CMAKE_CXX_FLAGS}"
  143. " ${CMAKE_ANSI_CXXFLAGS}\")\n");
  144. fprintf(fout, "ENDIF (CMAKE_ANSI_CXXFLAGS)\n");
  145. }
  146. if ( format == cmSystemTools::C_FILE_FORMAT )
  147. {
  148. fprintf(fout, "IF (CMAKE_ANSI_CFLAGS)\n");
  149. fprintf(fout, " SET(CMAKE_C_FLAGS \"${CMAKE_C_FLAGS} ${CMAKE_ANSI_CFLAGS}\")\n");
  150. fprintf(fout, "ENDIF (CMAKE_ANSI_CFLAGS)\n");
  151. }
  152. fprintf(fout, "ADD_DEFINITIONS(${COMPILE_DEFINITIONS})\n");
  153. fprintf(fout, "INCLUDE_DIRECTORIES(${INCLUDE_DIRECTORIES})\n");
  154. fprintf(fout, "LINK_DIRECTORIES(${LINK_DIRECTORIES})\n");
  155. // handle any compile flags we need to pass on
  156. if (compileFlags.size())
  157. {
  158. fprintf(fout, "ADD_DEFINITIONS( ");
  159. for (i = 0; i < compileFlags.size(); ++i)
  160. {
  161. fprintf(fout,"%s ",compileFlags[i].c_str());
  162. }
  163. fprintf(fout, ")\n");
  164. }
  165. fprintf(fout, "ADD_EXECUTABLE(cmTryCompileExec \"%s\")\n",source.c_str());
  166. fprintf(fout, "TARGET_LINK_LIBRARIES(cmTryCompileExec ${LINK_LIBRARIES})\n");
  167. fclose(fout);
  168. projectName = "CMAKE_TRY_COMPILE";
  169. targetName = "cmTryCompileExec";
  170. }
  171. // else the srcdir bindir project target signature
  172. else
  173. {
  174. projectName = argv[3].c_str();
  175. if (argv.size() == 5)
  176. {
  177. targetName = argv[4].c_str();
  178. }
  179. }
  180. std::string output;
  181. // actually do the try compile now that everything is setup
  182. int res = mf->TryCompile(sourceDirectory, binaryDirectory,
  183. projectName, targetName, &cmakeFlags, &output);
  184. // set the result var to the return value to indicate success or failure
  185. mf->AddCacheDefinition(argv[0].c_str(), (res == 0 ? "TRUE" : "FALSE"),
  186. "Result of TRY_COMPILE",
  187. cmCacheManager::INTERNAL);
  188. if ( outputVariable.size() > 0 )
  189. {
  190. mf->AddDefinition(outputVariable.c_str(), output.c_str());
  191. }
  192. // if They specified clean then we clean up what we can
  193. if (srcFileSignature && clean)
  194. {
  195. cmListFileCache::GetInstance()->FlushCache(outFileName.c_str());
  196. cmTryCompileCommand::CleanupFiles(binaryDirectory);
  197. }
  198. return res;
  199. }
  200. // cmExecutableCommand
  201. bool cmTryCompileCommand::InitialPass(std::vector<std::string> const& argv)
  202. {
  203. if(argv.size() < 3)
  204. {
  205. return false;
  206. }
  207. if ( m_Makefile->GetLocal() )
  208. {
  209. return true;
  210. }
  211. cmTryCompileCommand::CoreTryCompileCode(m_Makefile,argv,true);
  212. return true;
  213. }
  214. static void cmTryCompileCommandNotUsed(bool){}
  215. void cmTryCompileCommand::CleanupFiles(const char* binDir, bool recursive)
  216. {
  217. if ( !binDir )
  218. {
  219. return;
  220. }
  221. cmTryCompileCommandNotUsed(recursive);
  222. #ifdef WIN32
  223. if ( recursive )
  224. {
  225. std::string bdir = binDir;
  226. bdir += "/Debug";
  227. cmTryCompileCommand::CleanupFiles(bdir.c_str(), false);
  228. }
  229. #endif
  230. cmDirectory dir;
  231. dir.Load(binDir);
  232. size_t fileNum;
  233. for (fileNum = 0; fileNum < dir.GetNumberOfFiles(); ++fileNum)
  234. {
  235. if (strcmp(dir.GetFile(fileNum),".") &&
  236. strcmp(dir.GetFile(fileNum),".."))
  237. {
  238. std::string fullPath = binDir;
  239. fullPath += "/";
  240. fullPath += dir.GetFile(fileNum);
  241. cmSystemTools::RemoveFile(fullPath.c_str());
  242. }
  243. }
  244. }