cmConfigureFileCommand.cxx 3.5 KB

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