cmConfigureFileCommand.cxx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmConfigureFileCommand.h"
  4. #include "cmSystemTools.h"
  5. #include <cmsys/RegularExpression.hxx>
  6. // cmConfigureFileCommand
  7. bool cmConfigureFileCommand::InitialPass(std::vector<std::string> const& args,
  8. cmExecutionStatus&)
  9. {
  10. if (args.size() < 2) {
  11. this->SetError("called with incorrect number of arguments, expected 2");
  12. return false;
  13. }
  14. const char* inFile = args[0].c_str();
  15. if (!cmSystemTools::FileIsFullPath(inFile)) {
  16. this->InputFile = this->Makefile->GetCurrentSourceDirectory();
  17. this->InputFile += "/";
  18. }
  19. this->InputFile += inFile;
  20. // If the input location is a directory, error out.
  21. if (cmSystemTools::FileIsDirectory(this->InputFile)) {
  22. std::ostringstream e;
  23. /* clang-format off */
  24. e << "input location\n"
  25. << " " << this->InputFile << "\n"
  26. << "is a directory but a file was expected.";
  27. /* clang-format on */
  28. this->SetError(e.str());
  29. return false;
  30. }
  31. const char* outFile = args[1].c_str();
  32. if (!cmSystemTools::FileIsFullPath(outFile)) {
  33. this->OutputFile = this->Makefile->GetCurrentBinaryDirectory();
  34. this->OutputFile += "/";
  35. }
  36. this->OutputFile += outFile;
  37. // If the output location is already a directory put the file in it.
  38. if (cmSystemTools::FileIsDirectory(this->OutputFile)) {
  39. this->OutputFile += "/";
  40. this->OutputFile += cmSystemTools::GetFilenameName(inFile);
  41. }
  42. if (!this->Makefile->CanIWriteThisFile(this->OutputFile.c_str())) {
  43. std::string e = "attempted to configure a file: " + this->OutputFile +
  44. " into a source directory.";
  45. this->SetError(e);
  46. cmSystemTools::SetFatalErrorOccured();
  47. return false;
  48. }
  49. std::string errorMessage;
  50. if (!this->NewLineStyle.ReadFromArguments(args, errorMessage)) {
  51. this->SetError(errorMessage);
  52. return false;
  53. }
  54. this->CopyOnly = false;
  55. this->EscapeQuotes = false;
  56. std::string unknown_args;
  57. this->AtOnly = false;
  58. for (unsigned int i = 2; i < args.size(); ++i) {
  59. if (args[i] == "COPYONLY") {
  60. this->CopyOnly = true;
  61. if (this->NewLineStyle.IsValid()) {
  62. this->SetError("COPYONLY could not be used in combination "
  63. "with NEWLINE_STYLE");
  64. return false;
  65. }
  66. } else if (args[i] == "ESCAPE_QUOTES") {
  67. this->EscapeQuotes = true;
  68. } else if (args[i] == "@ONLY") {
  69. this->AtOnly = true;
  70. } else if (args[i] == "IMMEDIATE") {
  71. /* Ignore legacy option. */
  72. } else if (args[i] == "NEWLINE_STYLE" || args[i] == "LF" ||
  73. args[i] == "UNIX" || args[i] == "CRLF" || args[i] == "WIN32" ||
  74. args[i] == "DOS") {
  75. /* Options handled by NewLineStyle member above. */
  76. } else {
  77. unknown_args += " ";
  78. unknown_args += args[i];
  79. unknown_args += "\n";
  80. }
  81. }
  82. if (!unknown_args.empty()) {
  83. std::string msg = "configure_file called with unknown argument(s):\n";
  84. msg += unknown_args;
  85. this->Makefile->IssueMessage(cmake::AUTHOR_WARNING, msg);
  86. }
  87. if (!this->ConfigureFile()) {
  88. this->SetError("Problem configuring file");
  89. return false;
  90. }
  91. return true;
  92. }
  93. int cmConfigureFileCommand::ConfigureFile()
  94. {
  95. return this->Makefile->ConfigureFile(
  96. this->InputFile.c_str(), this->OutputFile.c_str(), this->CopyOnly,
  97. this->AtOnly, this->EscapeQuotes, this->NewLineStyle);
  98. }