cmConfigureFileCommand.cxx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmConfigureFileCommand.h"
  11. #include <cmsys/RegularExpression.hxx>
  12. // cmConfigureFileCommand
  13. bool cmConfigureFileCommand
  14. ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
  15. {
  16. if(args.size() < 2 )
  17. {
  18. this->SetError("called with incorrect number of arguments, expected 2");
  19. return false;
  20. }
  21. const char* inFile = args[0].c_str();
  22. if(!cmSystemTools::FileIsFullPath(inFile))
  23. {
  24. this->InputFile = this->Makefile->GetCurrentDirectory();
  25. this->InputFile += "/";
  26. }
  27. this->InputFile += inFile;
  28. // If the input location is a directory, error out.
  29. if(cmSystemTools::FileIsDirectory(this->InputFile))
  30. {
  31. std::ostringstream e;
  32. e << "input location\n"
  33. << " " << this->InputFile << "\n"
  34. << "is a directory but a file was expected.";
  35. this->SetError(e.str());
  36. return false;
  37. }
  38. const char* outFile = args[1].c_str();
  39. if(!cmSystemTools::FileIsFullPath(outFile))
  40. {
  41. this->OutputFile = this->Makefile->GetCurrentOutputDirectory();
  42. this->OutputFile += "/";
  43. }
  44. this->OutputFile += outFile;
  45. // If the output location is already a directory put the file in it.
  46. if(cmSystemTools::FileIsDirectory(this->OutputFile))
  47. {
  48. this->OutputFile += "/";
  49. this->OutputFile += cmSystemTools::GetFilenameName(inFile);
  50. }
  51. if ( !this->Makefile->CanIWriteThisFile(this->OutputFile.c_str()) )
  52. {
  53. std::string e = "attempted to configure a file: " + this->OutputFile
  54. + " into a source directory.";
  55. this->SetError(e);
  56. cmSystemTools::SetFatalErrorOccured();
  57. return false;
  58. }
  59. std::string errorMessage;
  60. if (!this->NewLineStyle.ReadFromArguments(args, errorMessage))
  61. {
  62. this->SetError(errorMessage);
  63. return false;
  64. }
  65. this->CopyOnly = false;
  66. this->EscapeQuotes = false;
  67. std::string unknown_args;
  68. this->AtOnly = false;
  69. for(unsigned int i=2;i < args.size();++i)
  70. {
  71. if(args[i] == "COPYONLY")
  72. {
  73. this->CopyOnly = true;
  74. if (this->NewLineStyle.IsValid())
  75. {
  76. this->SetError("COPYONLY could not be used in combination "
  77. "with NEWLINE_STYLE");
  78. return false;
  79. }
  80. }
  81. else if(args[i] == "ESCAPE_QUOTES")
  82. {
  83. this->EscapeQuotes = true;
  84. }
  85. else if(args[i] == "@ONLY")
  86. {
  87. this->AtOnly = true;
  88. }
  89. else if(args[i] == "IMMEDIATE")
  90. {
  91. /* Ignore legacy option. */
  92. }
  93. else
  94. {
  95. unknown_args += " ";
  96. unknown_args += args[i];
  97. unknown_args += "\n";
  98. }
  99. }
  100. if (!unknown_args.empty())
  101. {
  102. std::string msg = "configure_file called with unknown argument(s):\n";
  103. msg += unknown_args;
  104. this->Makefile->IssueMessage(cmake::AUTHOR_WARNING, msg);
  105. }
  106. if ( !this->ConfigureFile() )
  107. {
  108. this->SetError("Problem configuring file");
  109. return false;
  110. }
  111. return true;
  112. }
  113. int cmConfigureFileCommand::ConfigureFile()
  114. {
  115. return this->Makefile->ConfigureFile(
  116. this->InputFile.c_str(),
  117. this->OutputFile.c_str(),
  118. this->CopyOnly,
  119. this->AtOnly,
  120. this->EscapeQuotes,
  121. this->NewLineStyle);
  122. }