cmTryCompileCommand.cxx 8.8 KB

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