cmConfigureFileCommand.cxx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 <set>
  5. #include <cm/string_view>
  6. #include <cmext/string_view>
  7. #include "cmExecutionStatus.h"
  8. #include "cmMakefile.h"
  9. #include "cmMessageType.h"
  10. #include "cmNewLineStyle.h"
  11. #include "cmStringAlgorithms.h"
  12. #include "cmSystemTools.h"
  13. // cmConfigureFileCommand
  14. bool cmConfigureFileCommand(std::vector<std::string> const& args,
  15. cmExecutionStatus& status)
  16. {
  17. if (args.size() < 2) {
  18. status.SetError("called with incorrect number of arguments, expected 2");
  19. return false;
  20. }
  21. std::string const& inFile = args[0];
  22. const std::string inputFile = cmSystemTools::CollapseFullPath(
  23. inFile, status.GetMakefile().GetCurrentSourceDirectory());
  24. // If the input location is a directory, error out.
  25. if (cmSystemTools::FileIsDirectory(inputFile)) {
  26. status.SetError(cmStrCat("input location\n ", inputFile,
  27. "\n"
  28. "is a directory but a file was expected."));
  29. return false;
  30. }
  31. std::string const& outFile = args[1];
  32. std::string outputFile = cmSystemTools::CollapseFullPath(
  33. outFile, status.GetMakefile().GetCurrentBinaryDirectory());
  34. // If the output location is already a directory put the file in it.
  35. if (cmSystemTools::FileIsDirectory(outputFile)) {
  36. outputFile += "/";
  37. outputFile += cmSystemTools::GetFilenameName(inFile);
  38. }
  39. if (!status.GetMakefile().CanIWriteThisFile(outputFile)) {
  40. std::string e = "attempted to configure a file: " + outputFile +
  41. " into a source directory.";
  42. status.SetError(e);
  43. cmSystemTools::SetFatalErrorOccured();
  44. return false;
  45. }
  46. std::string errorMessage;
  47. cmNewLineStyle newLineStyle;
  48. if (!newLineStyle.ReadFromArguments(args, errorMessage)) {
  49. status.SetError(errorMessage);
  50. return false;
  51. }
  52. bool copyOnly = false;
  53. bool escapeQuotes = false;
  54. bool use_source_permissions = true;
  55. static std::set<cm::string_view> noopOptions = {
  56. /* Legacy. */
  57. "IMMEDIATE"_s,
  58. /* Handled by NewLineStyle member. */
  59. "NEWLINE_STYLE"_s,
  60. "LF"_s,
  61. "UNIX"_s,
  62. "CRLF"_s,
  63. "WIN32"_s,
  64. "DOS"_s,
  65. };
  66. std::string unknown_args;
  67. bool atOnly = false;
  68. for (unsigned int i = 2; i < args.size(); ++i) {
  69. if (args[i] == "COPYONLY") {
  70. copyOnly = true;
  71. if (newLineStyle.IsValid()) {
  72. status.SetError("COPYONLY could not be used in combination "
  73. "with NEWLINE_STYLE");
  74. return false;
  75. }
  76. } else if (args[i] == "ESCAPE_QUOTES") {
  77. escapeQuotes = true;
  78. } else if (args[i] == "@ONLY") {
  79. atOnly = true;
  80. } else if (args[i] == "NO_SOURCE_PERMISSIONS") {
  81. use_source_permissions = false;
  82. } else if (noopOptions.find(args[i]) != noopOptions.end()) {
  83. /* Ignore no-op options. */
  84. } else {
  85. unknown_args += " ";
  86. unknown_args += args[i];
  87. unknown_args += "\n";
  88. }
  89. }
  90. if (!unknown_args.empty()) {
  91. std::string msg = cmStrCat(
  92. "configure_file called with unknown argument(s):\n", unknown_args);
  93. status.GetMakefile().IssueMessage(MessageType::AUTHOR_WARNING, msg);
  94. }
  95. if (!status.GetMakefile().ConfigureFile(
  96. inputFile, outputFile, copyOnly, atOnly, escapeQuotes,
  97. use_source_permissions, newLineStyle)) {
  98. status.SetError("Problem configuring file");
  99. return false;
  100. }
  101. return true;
  102. }