cmConfigureFileCommand.cxx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 "cmMessageType.h"
  7. #include "cmSystemTools.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. this->InputFile = cmSystemTools::CollapseFullPath(
  19. inFile, this->Makefile->GetCurrentSourceDirectory());
  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. std::string const& outFile = args[1];
  32. this->OutputFile = cmSystemTools::CollapseFullPath(
  33. outFile, this->Makefile->GetCurrentBinaryDirectory());
  34. // If the output location is already a directory put the file in it.
  35. if (cmSystemTools::FileIsDirectory(this->OutputFile)) {
  36. this->OutputFile += "/";
  37. this->OutputFile += cmSystemTools::GetFilenameName(inFile);
  38. }
  39. if (!this->Makefile->CanIWriteThisFile(this->OutputFile)) {
  40. std::string e = "attempted to configure a file: " + this->OutputFile +
  41. " into a source directory.";
  42. this->SetError(e);
  43. cmSystemTools::SetFatalErrorOccured();
  44. return false;
  45. }
  46. std::string errorMessage;
  47. if (!this->NewLineStyle.ReadFromArguments(args, errorMessage)) {
  48. this->SetError(errorMessage);
  49. return false;
  50. }
  51. this->CopyOnly = false;
  52. this->EscapeQuotes = false;
  53. std::string unknown_args;
  54. this->AtOnly = false;
  55. for (unsigned int i = 2; i < args.size(); ++i) {
  56. if (args[i] == "COPYONLY") {
  57. this->CopyOnly = true;
  58. if (this->NewLineStyle.IsValid()) {
  59. this->SetError("COPYONLY could not be used in combination "
  60. "with NEWLINE_STYLE");
  61. return false;
  62. }
  63. } else if (args[i] == "ESCAPE_QUOTES") {
  64. this->EscapeQuotes = true;
  65. } else if (args[i] == "@ONLY") {
  66. this->AtOnly = true;
  67. } else if (args[i] == "IMMEDIATE") {
  68. /* Ignore legacy option. */
  69. } else if (args[i] == "NEWLINE_STYLE" || args[i] == "LF" ||
  70. args[i] == "UNIX" || args[i] == "CRLF" || args[i] == "WIN32" ||
  71. args[i] == "DOS") {
  72. /* Options handled by NewLineStyle member above. */
  73. } else {
  74. unknown_args += " ";
  75. unknown_args += args[i];
  76. unknown_args += "\n";
  77. }
  78. }
  79. if (!unknown_args.empty()) {
  80. std::string msg = "configure_file called with unknown argument(s):\n";
  81. msg += unknown_args;
  82. this->Makefile->IssueMessage(MessageType::AUTHOR_WARNING, msg);
  83. }
  84. if (!this->ConfigureFile()) {
  85. this->SetError("Problem configuring file");
  86. return false;
  87. }
  88. return true;
  89. }
  90. int cmConfigureFileCommand::ConfigureFile()
  91. {
  92. return this->Makefile->ConfigureFile(this->InputFile, this->OutputFile,
  93. this->CopyOnly, this->AtOnly,
  94. this->EscapeQuotes, this->NewLineStyle);
  95. }