cmTryCompileCommand.cxx 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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::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. const char* cflags = mf->GetDefinition("CMAKE_C_FLAGS");
  140. fprintf(fout, "SET(CMAKE_VERBOSE_MAKEFILE 1)\n");
  141. fprintf(fout, "SET(CMAKE_C_FLAGS \"${CMAKE_C_FLAGS}");
  142. if(cflags)
  143. {
  144. fprintf(fout, " %s ", cflags);
  145. }
  146. fprintf(fout, " ${COMPILE_DEFINITIONS}\")\n");
  147. // CXX specific flags
  148. if(format == cmSystemTools::CXX_FILE_FORMAT )
  149. {
  150. const char* cxxflags = mf->GetDefinition("CMAKE_CXX_FLAGS");
  151. fprintf(fout, "SET(CMAKE_CXX_FLAGS \"${CMAKE_CXX_FLAGS} ");
  152. if(cxxflags)
  153. {
  154. fprintf(fout, " %s ", cxxflags);
  155. }
  156. fprintf(fout, " ${COMPILE_DEFINITIONS}\")\n");
  157. }
  158. fprintf(fout, "INCLUDE_DIRECTORIES(${INCLUDE_DIRECTORIES})\n");
  159. fprintf(fout, "LINK_DIRECTORIES(${LINK_DIRECTORIES})\n");
  160. // handle any compile flags we need to pass on
  161. if (compileFlags.size())
  162. {
  163. fprintf(fout, "ADD_DEFINITIONS( ");
  164. for (i = 0; i < compileFlags.size(); ++i)
  165. {
  166. fprintf(fout,"%s ",compileFlags[i].c_str());
  167. }
  168. fprintf(fout, ")\n");
  169. }
  170. fprintf(fout, "ADD_EXECUTABLE(cmTryCompileExec \"%s\")\n",source.c_str());
  171. fprintf(fout, "TARGET_LINK_LIBRARIES(cmTryCompileExec ${LINK_LIBRARIES})\n");
  172. fclose(fout);
  173. projectName = "CMAKE_TRY_COMPILE";
  174. targetName = "cmTryCompileExec";
  175. // if the source is not in CMakeTmp
  176. if(source.find(argv[1] + "/CMakeTmp") == source.npos)
  177. {
  178. mf->AddCMakeDependFile(source.c_str());
  179. }
  180. }
  181. // else the srcdir bindir project target signature
  182. else
  183. {
  184. projectName = argv[3].c_str();
  185. if (argv.size() == 5)
  186. {
  187. targetName = argv[4].c_str();
  188. }
  189. }
  190. std::string output;
  191. // actually do the try compile now that everything is setup
  192. int res = mf->TryCompile(sourceDirectory, binaryDirectory,
  193. projectName, targetName, &cmakeFlags, &output);
  194. // set the result var to the return value to indicate success or failure
  195. mf->AddCacheDefinition(argv[0].c_str(), (res == 0 ? "TRUE" : "FALSE"),
  196. "Result of TRY_COMPILE",
  197. cmCacheManager::INTERNAL);
  198. if ( outputVariable.size() > 0 )
  199. {
  200. mf->AddDefinition(outputVariable.c_str(), output.c_str());
  201. }
  202. // if They specified clean then we clean up what we can
  203. if (srcFileSignature && clean)
  204. {
  205. cmListFileCache::GetInstance()->FlushCache(outFileName.c_str());
  206. cmTryCompileCommand::CleanupFiles(binaryDirectory);
  207. }
  208. return res;
  209. }
  210. // cmExecutableCommand
  211. bool cmTryCompileCommand::InitialPass(std::vector<std::string> const& argv)
  212. {
  213. if(argv.size() < 3)
  214. {
  215. return false;
  216. }
  217. if ( m_Makefile->GetLocal() )
  218. {
  219. return true;
  220. }
  221. cmTryCompileCommand::CoreTryCompileCode(m_Makefile,argv,true);
  222. return true;
  223. }
  224. void cmTryCompileCommand::CleanupFiles(const char* binDir)
  225. {
  226. if ( !binDir )
  227. {
  228. return;
  229. }
  230. std::string bdir = binDir;
  231. if(bdir.find("CMakeTmp") == std::string::npos)
  232. {
  233. cmSystemTools::Error("TRY_COMPILE attempt to remove -rf directory that does not contain CMakeTmp:", binDir);
  234. return;
  235. }
  236. cmDirectory dir;
  237. dir.Load(binDir);
  238. size_t fileNum;
  239. for (fileNum = 0; fileNum < dir.GetNumberOfFiles(); ++fileNum)
  240. {
  241. if (strcmp(dir.GetFile(fileNum),".") &&
  242. strcmp(dir.GetFile(fileNum),".."))
  243. {
  244. std::string fullPath = binDir;
  245. fullPath += "/";
  246. fullPath += dir.GetFile(fileNum);
  247. if(cmSystemTools::FileIsDirectory(fullPath.c_str()))
  248. {
  249. cmTryCompileCommand::CleanupFiles(fullPath.c_str());
  250. }
  251. else
  252. {
  253. if(!cmSystemTools::RemoveFile(fullPath.c_str()))
  254. {
  255. std::string m = "Remove failed on file: ";
  256. m += fullPath;
  257. cmSystemTools::ReportLastSystemError(m.c_str());
  258. }
  259. }
  260. }
  261. }
  262. }