cmTryCompileCommand.cxx 9.0 KB

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