cmTryCompileCommand.cxx 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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 "cmake.h"
  15. #include "cmCacheManager.h"
  16. #include "cmListFileCache.h"
  17. #include <cmsys/Directory.hxx>
  18. int cmTryCompileCommand::CoreTryCompileCode(
  19. cmMakefile *mf, std::vector<std::string> const& argv, bool clean)
  20. {
  21. // which signature were we called with ?
  22. bool srcFileSignature = false;
  23. unsigned int i;
  24. // where will the binaries be stored
  25. const char* binaryDirectory = argv[1].c_str();
  26. const char* sourceDirectory = argv[2].c_str();
  27. const char* projectName = 0;
  28. const char* targetName = 0;
  29. std::string tmpString;
  30. int extraArgs = 0;
  31. // look for CMAKE_FLAGS and store them
  32. std::vector<std::string> cmakeFlags;
  33. for (i = 3; i < argv.size(); ++i)
  34. {
  35. if (argv[i] == "CMAKE_FLAGS")
  36. {
  37. // CMAKE_FLAGS is the first argument because we need an argv[0] that
  38. // is not used, so it matches regular command line parsing which has
  39. // the program name as arg 0
  40. for (; i < argv.size() && argv[i] != "COMPILE_DEFINITIONS" &&
  41. argv[i] != "OUTPUT_VARIABLE";
  42. ++i)
  43. {
  44. extraArgs++;
  45. cmakeFlags.push_back(argv[i]);
  46. }
  47. break;
  48. }
  49. }
  50. // look for OUTPUT_VARIABLE and store them
  51. std::string outputVariable;
  52. for (i = 3; i < argv.size(); ++i)
  53. {
  54. if (argv[i] == "OUTPUT_VARIABLE")
  55. {
  56. if ( argv.size() <= (i+1) )
  57. {
  58. cmSystemTools::Error(
  59. "OUTPUT_VARIABLE specified but there is no variable");
  60. return -1;
  61. }
  62. extraArgs += 2;
  63. outputVariable = argv[i+1];
  64. break;
  65. }
  66. }
  67. // look for COMPILE_DEFINITIONS and store them
  68. std::vector<std::string> compileFlags;
  69. for (i = 3; i < argv.size(); ++i)
  70. {
  71. if (argv[i] == "COMPILE_DEFINITIONS")
  72. {
  73. extraArgs++;
  74. for (i = i + 1; i < argv.size() && argv[i] != "CMAKE_FLAGS" &&
  75. argv[i] != "OUTPUT_VARIABLE";
  76. ++i)
  77. {
  78. extraArgs++;
  79. compileFlags.push_back(argv[i]);
  80. }
  81. break;
  82. }
  83. }
  84. // do we have a srcfile signature
  85. if (argv.size() - extraArgs == 3)
  86. {
  87. srcFileSignature = true;
  88. }
  89. // only valid for srcfile signatures
  90. if (!srcFileSignature && compileFlags.size())
  91. {
  92. cmSystemTools::Error(
  93. "COMPILE_FLAGS specified on a srcdir type TRY_COMPILE");
  94. return -1;
  95. }
  96. // compute the binary dir when TRY_COMPILE is called with a src file
  97. // signature
  98. if (srcFileSignature)
  99. {
  100. tmpString = argv[1] + "/CMakeTmp";
  101. binaryDirectory = tmpString.c_str();
  102. }
  103. // make sure the binary directory exists
  104. cmSystemTools::MakeDirectory(binaryDirectory);
  105. // do not allow recursive try Compiles
  106. if (!strcmp(binaryDirectory,mf->GetHomeOutputDirectory()))
  107. {
  108. cmSystemTools::Error("Attempt at a recursive or nested TRY_COMPILE in directory ",
  109. binaryDirectory);
  110. return -1;
  111. }
  112. std::string outFileName = tmpString + "/CMakeLists.txt";
  113. // which signature are we using? If we are using var srcfile bindir
  114. if (srcFileSignature)
  115. {
  116. // remove any CMakeCache.txt files so we will have a clean test
  117. std::string ccFile = tmpString + "/CMakeCache.txt";
  118. cmSystemTools::RemoveFile(ccFile.c_str());
  119. // we need to create a directory and CMakeList file etc...
  120. // first create the directories
  121. sourceDirectory = binaryDirectory;
  122. // now create a CMakeList.txt file in that directory
  123. FILE *fout = fopen(outFileName.c_str(),"w");
  124. if (!fout)
  125. {
  126. cmSystemTools::Error("Failed to create CMakeList file for ",
  127. outFileName.c_str());
  128. return -1;
  129. }
  130. std::string source = argv[2];
  131. cmSystemTools::FileFormat format =
  132. cmSystemTools::GetFileFormat(
  133. cmSystemTools::GetFilenameExtension(source).c_str());
  134. if ( format == cmSystemTools::C_FILE_FORMAT )
  135. {
  136. fprintf(fout, "PROJECT(CMAKE_TRY_COMPILE C)\n");
  137. }
  138. else if ( format == cmSystemTools::CXX_FILE_FORMAT )
  139. {
  140. fprintf(fout, "PROJECT(CMAKE_TRY_COMPILE CXX)\n");
  141. }
  142. else if ( format == cmSystemTools::FORTRAN_FILE_FORMAT )
  143. {
  144. fprintf(fout, "PROJECT(CMAKE_TRY_COMPILE FORTRAN)\n");
  145. }
  146. else
  147. {
  148. cmSystemTools::Error("Unknown file format for file: ", source.c_str(),
  149. "; TRY_COMPILE only works for C, CXX, and FORTRAN files");
  150. return -1;
  151. }
  152. const char* cflags = mf->GetDefinition("CMAKE_C_FLAGS");
  153. fprintf(fout, "SET(CMAKE_VERBOSE_MAKEFILE 1)\n");
  154. fprintf(fout, "SET(CMAKE_C_FLAGS \"${CMAKE_C_FLAGS}");
  155. if(cflags)
  156. {
  157. fprintf(fout, " %s ", cflags);
  158. }
  159. fprintf(fout, " ${COMPILE_DEFINITIONS}\")\n");
  160. // CXX specific flags
  161. if(format == cmSystemTools::CXX_FILE_FORMAT )
  162. {
  163. const char* cxxflags = mf->GetDefinition("CMAKE_CXX_FLAGS");
  164. fprintf(fout, "SET(CMAKE_CXX_FLAGS \"${CMAKE_CXX_FLAGS} ");
  165. if(cxxflags)
  166. {
  167. fprintf(fout, " %s ", cxxflags);
  168. }
  169. fprintf(fout, " ${COMPILE_DEFINITIONS}\")\n");
  170. }
  171. if(format == cmSystemTools::FORTRAN_FILE_FORMAT )
  172. {
  173. const char* fflags = mf->GetDefinition("CMAKE_FORTRAN_FLAGS");
  174. fprintf(fout, "SET(CMAKE_FORTRAN_FLAGS \"${CMAKE_FORTRAN_FLAGS} ");
  175. if(fflags)
  176. {
  177. fprintf(fout, " %s ", fflags);
  178. }
  179. fprintf(fout, " ${COMPILE_DEFINITIONS}\")\n");
  180. }
  181. fprintf(fout, "INCLUDE_DIRECTORIES(${INCLUDE_DIRECTORIES})\n");
  182. fprintf(fout, "LINK_DIRECTORIES(${LINK_DIRECTORIES})\n");
  183. // handle any compile flags we need to pass on
  184. if (compileFlags.size())
  185. {
  186. fprintf(fout, "ADD_DEFINITIONS( ");
  187. for (i = 0; i < compileFlags.size(); ++i)
  188. {
  189. fprintf(fout,"%s ",compileFlags[i].c_str());
  190. }
  191. fprintf(fout, ")\n");
  192. }
  193. fprintf(fout, "ADD_EXECUTABLE(cmTryCompileExec \"%s\")\n",source.c_str());
  194. fprintf(fout, "TARGET_LINK_LIBRARIES(cmTryCompileExec ${LINK_LIBRARIES})\n");
  195. fclose(fout);
  196. projectName = "CMAKE_TRY_COMPILE";
  197. targetName = "cmTryCompileExec";
  198. // if the source is not in CMakeTmp
  199. if(source.find(argv[1] + "/CMakeTmp") == source.npos)
  200. {
  201. mf->AddCMakeDependFile(source.c_str());
  202. }
  203. }
  204. // else the srcdir bindir project target signature
  205. else
  206. {
  207. projectName = argv[3].c_str();
  208. if (argv.size() - extraArgs == 5)
  209. {
  210. targetName = argv[4].c_str();
  211. }
  212. }
  213. bool erroroc = cmSystemTools::GetErrorOccuredFlag();
  214. cmSystemTools::ResetErrorOccuredFlag();
  215. std::string output;
  216. // actually do the try compile now that everything is setup
  217. int res = mf->TryCompile(sourceDirectory, binaryDirectory,
  218. projectName, targetName, &cmakeFlags, &output);
  219. if ( erroroc )
  220. {
  221. cmSystemTools::SetErrorOccured();
  222. }
  223. // set the result var to the return value to indicate success or failure
  224. mf->AddCacheDefinition(argv[0].c_str(), (res == 0 ? "TRUE" : "FALSE"),
  225. "Result of TRY_COMPILE",
  226. cmCacheManager::INTERNAL);
  227. if ( outputVariable.size() > 0 )
  228. {
  229. mf->AddDefinition(outputVariable.c_str(), output.c_str());
  230. }
  231. // if They specified clean then we clean up what we can
  232. if (srcFileSignature && clean)
  233. {
  234. cmListFileCache::GetInstance()->FlushCache(outFileName.c_str());
  235. if(!mf->GetCMakeInstance()->GetDebugTryCompile())
  236. {
  237. cmTryCompileCommand::CleanupFiles(binaryDirectory);
  238. }
  239. }
  240. return res;
  241. }
  242. // cmExecutableCommand
  243. bool cmTryCompileCommand::InitialPass(std::vector<std::string> const& argv)
  244. {
  245. if(argv.size() < 3)
  246. {
  247. return false;
  248. }
  249. if ( m_Makefile->GetLocal() )
  250. {
  251. return true;
  252. }
  253. cmTryCompileCommand::CoreTryCompileCode(m_Makefile,argv,true);
  254. return true;
  255. }
  256. void cmTryCompileCommand::CleanupFiles(const char* binDir)
  257. {
  258. if ( !binDir )
  259. {
  260. return;
  261. }
  262. std::string bdir = binDir;
  263. if(bdir.find("CMakeTmp") == std::string::npos)
  264. {
  265. cmSystemTools::Error("TRY_COMPILE attempt to remove -rf directory that does not contain CMakeTmp:", binDir);
  266. return;
  267. }
  268. cmsys::Directory dir;
  269. dir.Load(binDir);
  270. size_t fileNum;
  271. for (fileNum = 0; fileNum < dir.GetNumberOfFiles(); ++fileNum)
  272. {
  273. if (strcmp(dir.GetFile(static_cast<unsigned long>(fileNum)),".") &&
  274. strcmp(dir.GetFile(static_cast<unsigned long>(fileNum)),".."))
  275. {
  276. std::string fullPath = binDir;
  277. fullPath += "/";
  278. fullPath += dir.GetFile(static_cast<unsigned long>(fileNum));
  279. if(cmSystemTools::FileIsDirectory(fullPath.c_str()))
  280. {
  281. cmTryCompileCommand::CleanupFiles(fullPath.c_str());
  282. }
  283. else
  284. {
  285. if(!cmSystemTools::RemoveFile(fullPath.c_str()))
  286. {
  287. std::string m = "Remove failed on file: ";
  288. m += fullPath;
  289. cmSystemTools::ReportLastSystemError(m.c_str());
  290. }
  291. }
  292. }
  293. }
  294. }