cmTryCompileCommand.cxx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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[1].c_str();
  24. const char* sourceDirectory = argv[2].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[1] + "/CMakeTmp";
  33. binaryDirectory = tmpString.c_str();
  34. }
  35. // make sure the binary directory exists
  36. cmSystemTools::MakeDirectory(binaryDirectory);
  37. // do not allow recursive try Compiles
  38. if (!strcmp(binaryDirectory,m_Makefile->GetHomeOutputDirectory()))
  39. {
  40. cmSystemTools::Error("Attempt at a recursive or nested TRY_COMPILE",
  41. binaryDirectory);
  42. return false;
  43. }
  44. // which signature are we using? If we are using var srcfile bindir
  45. if (argv.size() == 3)
  46. {
  47. // remove any CMakeCache.txt files so we will have a clean test
  48. std::string ccFile = tmpString + "/CMakeCache.txt";
  49. cmSystemTools::RemoveFile(ccFile.c_str());
  50. // we need to create a directory and CMakeList file etc...
  51. // first create the directories
  52. sourceDirectory = binaryDirectory;
  53. // now create a CMakeList.txt file in that directory
  54. std::string outFileName = tmpString + "/CMakeLists.txt";
  55. FILE *fout = fopen(outFileName.c_str(),"w");
  56. if (!fout)
  57. {
  58. cmSystemTools::Error("Failed to create CMakeList file for ",
  59. outFileName.c_str());
  60. return false;
  61. }
  62. fprintf(fout,"PROJECT(CMAKE_TRY_COMPILE)\n");
  63. fprintf(fout,"ADD_EXECUTABLE(cmTryCompileExec \"%s\")\n",argv[2].c_str());
  64. fclose(fout);
  65. projectName = "CMAKE_TRY_COMPILE";
  66. targetName = "cmTryCompileExec";
  67. }
  68. // else the srcdir bindir project target signature
  69. else
  70. {
  71. projectName = argv[3].c_str();
  72. if (argv.size() == 5)
  73. {
  74. targetName = argv[4].c_str();
  75. }
  76. }
  77. // actually do the try compile now that everything is setup
  78. int res = m_Makefile->TryCompile(sourceDirectory, binaryDirectory,
  79. projectName, targetName);
  80. // set the result var to the return value to indicate success or failure
  81. m_Makefile->AddDefinition(argv[0].c_str(), (res == 0 ? "TRUE" : "FALSE"));
  82. // if we created a directory etc, then cleanup after ourselves
  83. if (argv.size() == 3)
  84. {
  85. cmDirectory dir;
  86. dir.Load(binaryDirectory);
  87. size_t fileNum;
  88. for (fileNum = 0; fileNum < dir.GetNumberOfFiles(); ++fileNum)
  89. {
  90. if (strcmp(dir.GetFile(fileNum),".") &&
  91. strcmp(dir.GetFile(fileNum),".."))
  92. {
  93. std::string fullPath = binaryDirectory;
  94. fullPath += "/";
  95. fullPath += dir.GetFile(fileNum);
  96. cmSystemTools::RemoveFile(fullPath.c_str());
  97. }
  98. }
  99. }
  100. return true;
  101. }