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