cmConfigureFileCommand.cxx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 "cmConfigureFileCommand.h"
  14. #include <cmsys/RegularExpression.hxx>
  15. // cmConfigureFileCommand
  16. bool cmConfigureFileCommand
  17. ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
  18. {
  19. if(args.size() < 2 )
  20. {
  21. this->SetError("called with incorrect number of arguments, expected 2");
  22. return false;
  23. }
  24. const char* inFile = args[0].c_str();
  25. if(!cmSystemTools::FileIsFullPath(inFile))
  26. {
  27. this->InputFile = this->Makefile->GetCurrentDirectory();
  28. this->InputFile += "/";
  29. }
  30. this->InputFile += inFile;
  31. // If the input location is a directory, error out.
  32. if(cmSystemTools::FileIsDirectory(this->InputFile.c_str()))
  33. {
  34. cmOStringStream e;
  35. e << "input location\n"
  36. << " " << this->InputFile << "\n"
  37. << "is a directory but a file was expected.";
  38. this->SetError(e.str().c_str());
  39. return false;
  40. }
  41. const char* outFile = args[1].c_str();
  42. if(!cmSystemTools::FileIsFullPath(outFile))
  43. {
  44. this->OutputFile = this->Makefile->GetCurrentOutputDirectory();
  45. this->OutputFile += "/";
  46. }
  47. this->OutputFile += outFile;
  48. // If the output location is already a directory put the file in it.
  49. if(cmSystemTools::FileIsDirectory(this->OutputFile.c_str()))
  50. {
  51. this->OutputFile += "/";
  52. this->OutputFile += cmSystemTools::GetFilenameName(inFile);
  53. }
  54. if ( !this->Makefile->CanIWriteThisFile(this->OutputFile.c_str()) )
  55. {
  56. std::string e = "attempted to configure a file: " + this->OutputFile
  57. + " into a source directory.";
  58. this->SetError(e.c_str());
  59. cmSystemTools::SetFatalErrorOccured();
  60. return false;
  61. }
  62. this->CopyOnly = false;
  63. this->EscapeQuotes = false;
  64. // for CMake 2.0 and earlier CONFIGURE_FILE defaults to the FinalPass,
  65. // after 2.0 it only does InitialPass
  66. this->Immediate = !this->Makefile->NeedBackwardsCompatibility(2,0);
  67. this->AtOnly = false;
  68. for(unsigned int i=2;i < args.size();++i)
  69. {
  70. if(args[i] == "COPYONLY")
  71. {
  72. this->CopyOnly = true;
  73. }
  74. else if(args[i] == "ESCAPE_QUOTES")
  75. {
  76. this->EscapeQuotes = true;
  77. }
  78. else if(args[i] == "@ONLY")
  79. {
  80. this->AtOnly = true;
  81. }
  82. else if(args[i] == "IMMEDIATE")
  83. {
  84. this->Immediate = true;
  85. }
  86. }
  87. // If we were told to copy the file immediately, then do it on the
  88. // first pass (now).
  89. if(this->Immediate)
  90. {
  91. if ( !this->ConfigureFile() )
  92. {
  93. this->SetError("Problem configuring file");
  94. return false;
  95. }
  96. }
  97. return true;
  98. }
  99. void cmConfigureFileCommand::FinalPass()
  100. {
  101. if(!this->Immediate)
  102. {
  103. this->ConfigureFile();
  104. }
  105. }
  106. int cmConfigureFileCommand::ConfigureFile()
  107. {
  108. return this->Makefile->ConfigureFile(
  109. this->InputFile.c_str(),
  110. this->OutputFile.c_str(),
  111. this->CopyOnly,
  112. this->AtOnly,
  113. this->EscapeQuotes);
  114. }