cmConfigureFileCommand.cxx 3.2 KB

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