cmTryCompileCommand.cxx 8.7 KB

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