cmConfigureFileCommand.cxx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 "cmExecutionStatus.h"
  6. #include "cmMakefile.h"
  7. #include "cmMessageType.h"
  8. #include "cmNewLineStyle.h"
  9. #include "cmStringAlgorithms.h"
  10. #include "cmSystemTools.h"
  11. // cmConfigureFileCommand
  12. bool cmConfigureFileCommand(std::vector<std::string> const& args,
  13. cmExecutionStatus& status)
  14. {
  15. if (args.size() < 2) {
  16. status.SetError("called with incorrect number of arguments, expected 2");
  17. return false;
  18. }
  19. std::string const& inFile = args[0];
  20. const std::string inputFile = cmSystemTools::CollapseFullPath(
  21. inFile, status.GetMakefile().GetCurrentSourceDirectory());
  22. // If the input location is a directory, error out.
  23. if (cmSystemTools::FileIsDirectory(inputFile)) {
  24. std::ostringstream e;
  25. /* clang-format off */
  26. e << "input location\n"
  27. << " " << inputFile << "\n"
  28. << "is a directory but a file was expected.";
  29. /* clang-format on */
  30. status.SetError(e.str());
  31. return false;
  32. }
  33. std::string const& outFile = args[1];
  34. std::string outputFile = cmSystemTools::CollapseFullPath(
  35. outFile, status.GetMakefile().GetCurrentBinaryDirectory());
  36. // If the output location is already a directory put the file in it.
  37. if (cmSystemTools::FileIsDirectory(outputFile)) {
  38. outputFile += "/";
  39. outputFile += cmSystemTools::GetFilenameName(inFile);
  40. }
  41. if (!status.GetMakefile().CanIWriteThisFile(outputFile)) {
  42. std::string e = "attempted to configure a file: " + outputFile +
  43. " into a source directory.";
  44. status.SetError(e);
  45. cmSystemTools::SetFatalErrorOccured();
  46. return false;
  47. }
  48. std::string errorMessage;
  49. cmNewLineStyle newLineStyle;
  50. if (!newLineStyle.ReadFromArguments(args, errorMessage)) {
  51. status.SetError(errorMessage);
  52. return false;
  53. }
  54. bool copyOnly = false;
  55. bool escapeQuotes = false;
  56. std::string unknown_args;
  57. bool atOnly = false;
  58. for (unsigned int i = 2; i < args.size(); ++i) {
  59. if (args[i] == "COPYONLY") {
  60. copyOnly = true;
  61. if (newLineStyle.IsValid()) {
  62. status.SetError("COPYONLY could not be used in combination "
  63. "with NEWLINE_STYLE");
  64. return false;
  65. }
  66. } else if (args[i] == "ESCAPE_QUOTES") {
  67. escapeQuotes = true;
  68. } else if (args[i] == "@ONLY") {
  69. 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 = cmStrCat(
  84. "configure_file called with unknown argument(s):\n", unknown_args);
  85. status.GetMakefile().IssueMessage(MessageType::AUTHOR_WARNING, msg);
  86. }
  87. if (!status.GetMakefile().ConfigureFile(
  88. inputFile, outputFile, copyOnly, atOnly, escapeQuotes, newLineStyle)) {
  89. status.SetError("Problem configuring file");
  90. return false;
  91. }
  92. return true;
  93. }