cmTryCompileCommand.cxx 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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] + "/CMakeFiles/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(
  110. "Attempt at a recursive or nested TRY_COMPILE in directory ",
  111. binaryDirectory);
  112. return -1;
  113. }
  114. std::string outFileName = tmpString + "/CMakeLists.txt";
  115. // which signature are we using? If we are using var srcfile bindir
  116. if (srcFileSignature)
  117. {
  118. // remove any CMakeCache.txt files so we will have a clean test
  119. std::string ccFile = tmpString + "/CMakeCache.txt";
  120. cmSystemTools::RemoveFile(ccFile.c_str());
  121. // we need to create a directory and CMakeList file etc...
  122. // first create the directories
  123. sourceDirectory = binaryDirectory;
  124. // now create a CMakeList.txt file in that directory
  125. FILE *fout = fopen(outFileName.c_str(),"w");
  126. if (!fout)
  127. {
  128. cmSystemTools::Error("Failed to create CMakeList file for ",
  129. outFileName.c_str());
  130. cmSystemTools::ReportLastSystemError("");
  131. return -1;
  132. }
  133. std::string source = argv[2];
  134. std::string ext = cmSystemTools::GetFilenameExtension(source);
  135. const char* lang = (mf->GetCMakeInstance()->GetGlobalGenerator()
  136. ->GetLanguageFromExtension(ext.c_str()));
  137. const char* def = mf->GetDefinition("CMAKE_MODULE_PATH");
  138. if(def)
  139. {
  140. fprintf(fout, "SET(CMAKE_MODULE_PATH %s)\n", def);
  141. }
  142. if(lang)
  143. {
  144. fprintf(fout, "PROJECT(CMAKE_TRY_COMPILE %s)\n", lang);
  145. }
  146. else
  147. {
  148. cmOStringStream err;
  149. err << "Unknown extension \"" << ext << "\" for file \""
  150. << source << "\". TRY_COMPILE only works for enabled languages.\n"
  151. << "Currently enabled languages are:";
  152. std::vector<std::string> langs;
  153. mf->GetCMakeInstance()->GetGlobalGenerator()->GetEnabledLanguages(langs);
  154. for(std::vector<std::string>::iterator l = langs.begin();
  155. l != langs.end(); ++l)
  156. {
  157. err << " " << *l;
  158. }
  159. err << "\nSee PROJECT command for help enabling other languages.";
  160. cmSystemTools::Error(err.str().c_str());
  161. return -1;
  162. }
  163. std::string langFlags = "CMAKE_";
  164. langFlags += lang;
  165. langFlags += "_FLAGS";
  166. fprintf(fout, "SET(CMAKE_VERBOSE_MAKEFILE 1)\n");
  167. fprintf(fout, "SET(CMAKE_%s_FLAGS \"", lang);
  168. const char* flags = mf->GetDefinition(langFlags.c_str());
  169. if(flags)
  170. {
  171. fprintf(fout, " %s ", flags);
  172. }
  173. fprintf(fout, " ${COMPILE_DEFINITIONS}\")\n");
  174. fprintf(fout, "INCLUDE_DIRECTORIES(${INCLUDE_DIRECTORIES})\n");
  175. fprintf(fout, "LINK_DIRECTORIES(${LINK_DIRECTORIES})\n");
  176. // handle any compile flags we need to pass on
  177. if (compileFlags.size())
  178. {
  179. fprintf(fout, "ADD_DEFINITIONS( ");
  180. for (i = 0; i < compileFlags.size(); ++i)
  181. {
  182. fprintf(fout,"%s ",compileFlags[i].c_str());
  183. }
  184. fprintf(fout, ")\n");
  185. }
  186. fprintf(fout, "ADD_EXECUTABLE(cmTryCompileExec \"%s\")\n",source.c_str());
  187. fprintf(fout,
  188. "TARGET_LINK_LIBRARIES(cmTryCompileExec ${LINK_LIBRARIES})\n");
  189. fclose(fout);
  190. projectName = "CMAKE_TRY_COMPILE";
  191. targetName = "cmTryCompileExec";
  192. // if the source is not in CMakeTmp
  193. if(source.find("CMakeTmp") == source.npos)
  194. {
  195. mf->AddCMakeDependFile(source.c_str());
  196. }
  197. }
  198. // else the srcdir bindir project target signature
  199. else
  200. {
  201. projectName = argv[3].c_str();
  202. if (argv.size() - extraArgs == 5)
  203. {
  204. targetName = argv[4].c_str();
  205. }
  206. }
  207. bool erroroc = cmSystemTools::GetErrorOccuredFlag();
  208. cmSystemTools::ResetErrorOccuredFlag();
  209. std::string output;
  210. // actually do the try compile now that everything is setup
  211. int res = mf->TryCompile(sourceDirectory, binaryDirectory,
  212. projectName, targetName, &cmakeFlags, &output);
  213. // for the xcode generator
  214. if(strcmp(mf->GetCMakeInstance()->GetGlobalGenerator()->GetName() ,
  215. "Xcode") == 0)
  216. {
  217. int numTrys = 0;
  218. while(output.find("/bin/sh: bad interpreter: Text file busy")
  219. != output.npos && numTrys < 4)
  220. {
  221. output = "";
  222. res = mf->TryCompile(sourceDirectory, binaryDirectory,
  223. projectName, targetName, &cmakeFlags, &output);
  224. numTrys++;
  225. }
  226. }
  227. if ( erroroc )
  228. {
  229. cmSystemTools::SetErrorOccured();
  230. }
  231. // set the result var to the return value to indicate success or failure
  232. mf->AddCacheDefinition(argv[0].c_str(), (res == 0 ? "TRUE" : "FALSE"),
  233. "Result of TRY_COMPILE",
  234. cmCacheManager::INTERNAL);
  235. if ( outputVariable.size() > 0 )
  236. {
  237. mf->AddDefinition(outputVariable.c_str(), output.c_str());
  238. }
  239. // if They specified clean then we clean up what we can
  240. if (srcFileSignature && clean)
  241. {
  242. if(!mf->GetCMakeInstance()->GetDebugTryCompile())
  243. {
  244. cmTryCompileCommand::CleanupFiles(binaryDirectory);
  245. }
  246. }
  247. return res;
  248. }
  249. // cmExecutableCommand
  250. bool cmTryCompileCommand::InitialPass(std::vector<std::string> const& argv)
  251. {
  252. if(argv.size() < 3)
  253. {
  254. return false;
  255. }
  256. cmTryCompileCommand::CoreTryCompileCode(this->Makefile,argv,true);
  257. return true;
  258. }
  259. void cmTryCompileCommand::CleanupFiles(const char* binDir)
  260. {
  261. if ( !binDir )
  262. {
  263. return;
  264. }
  265. std::string bdir = binDir;
  266. if(bdir.find("CMakeTmp") == std::string::npos)
  267. {
  268. cmSystemTools::Error(
  269. "TRY_COMPILE attempt to remove -rf directory that does not contain "
  270. "CMakeTmp:", binDir);
  271. return;
  272. }
  273. cmsys::Directory dir;
  274. dir.Load(binDir);
  275. size_t fileNum;
  276. std::set<cmStdString> deletedFiles;
  277. for (fileNum = 0; fileNum < dir.GetNumberOfFiles(); ++fileNum)
  278. {
  279. if (strcmp(dir.GetFile(static_cast<unsigned long>(fileNum)),".") &&
  280. strcmp(dir.GetFile(static_cast<unsigned long>(fileNum)),".."))
  281. {
  282. if(deletedFiles.find( dir.GetFile(static_cast<unsigned long>(fileNum)))
  283. == deletedFiles.end())
  284. {
  285. deletedFiles.insert(dir.GetFile(static_cast<unsigned long>(fileNum)));
  286. std::string fullPath = binDir;
  287. fullPath += "/";
  288. fullPath += dir.GetFile(static_cast<unsigned long>(fileNum));
  289. if(cmSystemTools::FileIsDirectory(fullPath.c_str()))
  290. {
  291. cmTryCompileCommand::CleanupFiles(fullPath.c_str());
  292. }
  293. else
  294. {
  295. if(!cmSystemTools::RemoveFile(fullPath.c_str()))
  296. {
  297. std::string m = "Remove failed on file: ";
  298. m += fullPath;
  299. cmSystemTools::ReportLastSystemError(m.c_str());
  300. }
  301. }
  302. }
  303. }
  304. }
  305. }