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