1
0

cmConfigureFileCommand.cxx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 <sstream>
  6. #include <cm/string_view>
  7. #include <cmext/string_view>
  8. #include <sys/types.h>
  9. #include "cmExecutionStatus.h"
  10. #include "cmFSPermissions.h"
  11. #include "cmMakefile.h"
  12. #include "cmMessageType.h"
  13. #include "cmNewLineStyle.h"
  14. #include "cmStringAlgorithms.h"
  15. #include "cmSystemTools.h"
  16. // cmConfigureFileCommand
  17. bool cmConfigureFileCommand(std::vector<std::string> const& args,
  18. cmExecutionStatus& status)
  19. {
  20. if (args.size() < 2) {
  21. status.SetError("called with incorrect number of arguments, expected 2");
  22. return false;
  23. }
  24. std::string const& inFile = args[0];
  25. const std::string inputFile = cmSystemTools::CollapseFullPath(
  26. inFile, status.GetMakefile().GetCurrentSourceDirectory());
  27. // If the input location is a directory, error out.
  28. if (cmSystemTools::FileIsDirectory(inputFile)) {
  29. status.SetError(cmStrCat("input location\n ", inputFile,
  30. "\n"
  31. "is a directory but a file was expected."));
  32. return false;
  33. }
  34. std::string const& outFile = args[1];
  35. std::string outputFile = cmSystemTools::CollapseFullPath(
  36. outFile, status.GetMakefile().GetCurrentBinaryDirectory());
  37. // If the output location is already a directory put the file in it.
  38. if (cmSystemTools::FileIsDirectory(outputFile)) {
  39. outputFile += "/";
  40. outputFile += cmSystemTools::GetFilenameName(inFile);
  41. }
  42. if (!status.GetMakefile().CanIWriteThisFile(outputFile)) {
  43. std::string e = "attempted to configure a file: " + outputFile +
  44. " into a source directory.";
  45. status.SetError(e);
  46. cmSystemTools::SetFatalErrorOccured();
  47. return false;
  48. }
  49. std::string errorMessage;
  50. cmNewLineStyle newLineStyle;
  51. if (!newLineStyle.ReadFromArguments(args, errorMessage)) {
  52. status.SetError(errorMessage);
  53. return false;
  54. }
  55. bool copyOnly = false;
  56. bool escapeQuotes = false;
  57. bool useSourcePermissions = false;
  58. bool noSourcePermissions = false;
  59. bool filePermissions = false;
  60. std::vector<std::string> filePermissionOptions;
  61. enum class Doing
  62. {
  63. DoingNone,
  64. DoingFilePermissions,
  65. DoneFilePermissions
  66. };
  67. Doing doing = Doing::DoingNone;
  68. static std::set<cm::string_view> noopOptions = {
  69. /* Legacy. */
  70. "IMMEDIATE"_s,
  71. /* Handled by NewLineStyle member. */
  72. "NEWLINE_STYLE"_s,
  73. "LF"_s,
  74. "UNIX"_s,
  75. "CRLF"_s,
  76. "WIN32"_s,
  77. "DOS"_s,
  78. };
  79. std::string unknown_args;
  80. bool atOnly = false;
  81. for (unsigned int i = 2; i < args.size(); ++i) {
  82. if (args[i] == "COPYONLY") {
  83. if (doing == Doing::DoingFilePermissions) {
  84. doing = Doing::DoneFilePermissions;
  85. }
  86. copyOnly = true;
  87. if (newLineStyle.IsValid()) {
  88. status.SetError("COPYONLY could not be used in combination "
  89. "with NEWLINE_STYLE");
  90. return false;
  91. }
  92. } else if (args[i] == "ESCAPE_QUOTES") {
  93. if (doing == Doing::DoingFilePermissions) {
  94. doing = Doing::DoneFilePermissions;
  95. }
  96. escapeQuotes = true;
  97. } else if (args[i] == "@ONLY") {
  98. if (doing == Doing::DoingFilePermissions) {
  99. doing = Doing::DoneFilePermissions;
  100. }
  101. atOnly = true;
  102. } else if (args[i] == "NO_SOURCE_PERMISSIONS") {
  103. if (doing == Doing::DoingFilePermissions) {
  104. status.SetError(" given both FILE_PERMISSIONS and "
  105. "NO_SOURCE_PERMISSIONS. Only one option allowed.");
  106. return false;
  107. }
  108. noSourcePermissions = true;
  109. } else if (args[i] == "USE_SOURCE_PERMISSIONS") {
  110. if (doing == Doing::DoingFilePermissions) {
  111. status.SetError(" given both FILE_PERMISSIONS and "
  112. "USE_SOURCE_PERMISSIONS. Only one option allowed.");
  113. return false;
  114. }
  115. useSourcePermissions = true;
  116. } else if (args[i] == "FILE_PERMISSIONS") {
  117. if (useSourcePermissions) {
  118. status.SetError(" given both FILE_PERMISSIONS and "
  119. "USE_SOURCE_PERMISSIONS. Only one option allowed.");
  120. return false;
  121. }
  122. if (noSourcePermissions) {
  123. status.SetError(" given both FILE_PERMISSIONS and "
  124. "NO_SOURCE_PERMISSIONS. Only one option allowed.");
  125. return false;
  126. }
  127. if (doing == Doing::DoingNone) {
  128. doing = Doing::DoingFilePermissions;
  129. filePermissions = true;
  130. }
  131. } else if (noopOptions.find(args[i]) != noopOptions.end()) {
  132. /* Ignore no-op options. */
  133. } else if (doing == Doing::DoingFilePermissions) {
  134. filePermissionOptions.push_back(args[i]);
  135. } else {
  136. unknown_args += " ";
  137. unknown_args += args[i];
  138. unknown_args += "\n";
  139. }
  140. }
  141. if (!unknown_args.empty()) {
  142. std::string msg = cmStrCat(
  143. "configure_file called with unknown argument(s):\n", unknown_args);
  144. status.GetMakefile().IssueMessage(MessageType::AUTHOR_WARNING, msg);
  145. }
  146. if (useSourcePermissions && noSourcePermissions) {
  147. status.SetError(" given both USE_SOURCE_PERMISSIONS and "
  148. "NO_SOURCE_PERMISSIONS. Only one option allowed.");
  149. return false;
  150. }
  151. mode_t permissions = 0;
  152. if (filePermissions) {
  153. if (filePermissionOptions.empty()) {
  154. status.SetError(" given FILE_PERMISSIONS without any options.");
  155. return false;
  156. }
  157. std::vector<std::string> invalidOptions;
  158. for (auto const& e : filePermissionOptions) {
  159. if (!cmFSPermissions::stringToModeT(e, permissions)) {
  160. invalidOptions.push_back(e);
  161. }
  162. }
  163. if (!invalidOptions.empty()) {
  164. std::ostringstream oss;
  165. oss << " given invalid permission ";
  166. for (auto i = 0u; i < invalidOptions.size(); i++) {
  167. if (i == 0u) {
  168. oss << "\"" << invalidOptions[i] << "\"";
  169. } else {
  170. oss << ",\"" << invalidOptions[i] << "\"";
  171. }
  172. }
  173. oss << ".";
  174. status.SetError(oss.str());
  175. return false;
  176. }
  177. }
  178. if (noSourcePermissions) {
  179. permissions |= cmFSPermissions::mode_owner_read;
  180. permissions |= cmFSPermissions::mode_owner_write;
  181. permissions |= cmFSPermissions::mode_group_read;
  182. permissions |= cmFSPermissions::mode_world_read;
  183. }
  184. if (!status.GetMakefile().ConfigureFile(inputFile, outputFile, copyOnly,
  185. atOnly, escapeQuotes, permissions,
  186. newLineStyle)) {
  187. status.SetError("Problem configuring file");
  188. return false;
  189. }
  190. return true;
  191. }