cmTryCompileCommand.cxx 8.5 KB

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