cmTryCompileCommand.cxx 9.5 KB

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