cmTryCompileCommand.cxx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Insight Consortium. All rights reserved.
  8. See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm 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. // cmExecutableCommand
  16. bool cmTryCompileCommand::InitialPass(std::vector<std::string> const& argv)
  17. {
  18. if(argv.size() < 3)
  19. {
  20. return false;
  21. }
  22. // where will the binaries be stored
  23. const char* binaryDirectory = argv[2].c_str();
  24. const char* sourceDirectory = argv[1].c_str();
  25. const char* projectName = 0;
  26. const char* targetName = 0;
  27. std::string tmpString;
  28. // compute the binary dir when TRY_COMPILE is called with a src file
  29. // signature
  30. if (argv.size() == 3)
  31. {
  32. tmpString = argv[2] + "/CMakeTmp";
  33. binaryDirectory = tmpString.c_str();
  34. }
  35. // do not allow recursive try Compiles
  36. if (!strcmp(binaryDirectory,m_Makefile->GetHomeOutputDirectory()))
  37. {
  38. cmSystemTools::Error("Attempt at a recursive or nested TRY_COMPILE",
  39. binaryDirectory);
  40. return false;
  41. }
  42. // which signature are we using? If we are using var srcfile bindir
  43. if (argv.size() == 3)
  44. {
  45. // remove any CMakeCache.txt files so we will have a clean test
  46. std::string ccFile = tmpString + "/CMakeCache.txt";
  47. cmSystemTools::RemoveFile(ccFile.c_str());
  48. // we need to create a directory and CMakeList file etc...
  49. // first create the directories
  50. sourceDirectory = binaryDirectory;
  51. cmSystemTools::MakeDirectory(binaryDirectory);
  52. // now create a CMakeList.txt file in that directory
  53. std::string outFileName = tmpString + "/CMakeLists.txt";
  54. FILE *fout = fopen(outFileName.c_str(),"w");
  55. if (!fout)
  56. {
  57. cmSystemTools::Error("Failed to create CMakeList file for ",
  58. outFileName.c_str());
  59. return false;
  60. }
  61. fprintf(fout,"PROJECT(CMAKE_TRY_COMPILE)\n");
  62. fprintf(fout,"ADD_EXECUTABLE(cmTryCompileExec \"%s\")\n",argv[1].c_str());
  63. fclose(fout);
  64. projectName = "CMAKE_TRY_COMPILE";
  65. targetName = "cmTryCompileExec";
  66. }
  67. // else the srcdir bindir project target signature
  68. else
  69. {
  70. projectName = argv[3].c_str();
  71. if (argv.size() == 5)
  72. {
  73. targetName = argv[4].c_str();
  74. }
  75. }
  76. // actually do the try compile now that everything is setup
  77. int res = m_Makefile->TryCompile(sourceDirectory, binaryDirectory,
  78. projectName, targetName);
  79. // set the result var to the return value to indicate success or failure
  80. m_Makefile->AddDefinition(argv[0].c_str(), (res == 0 ? "TRUE" : "FALSE"));
  81. // if we created a directory etc, then cleanup after ourselves
  82. if (argv.size() == 3)
  83. {
  84. cmDirectory dir;
  85. dir.Load(binaryDirectory);
  86. size_t fileNum;
  87. for (fileNum = 0; fileNum < dir.GetNumberOfFiles(); ++fileNum)
  88. {
  89. if (strcmp(dir.GetFile(fileNum),".") &&
  90. strcmp(dir.GetFile(fileNum),".."))
  91. {
  92. std::string fullPath = binaryDirectory;
  93. fullPath += "/";
  94. fullPath += dir.GetFile(fileNum);
  95. cmSystemTools::RemoveFile(fullPath.c_str());
  96. }
  97. }
  98. }
  99. return true;
  100. }