You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

256 lines
8.5 KiB

Simplify CMake per-source license notices Per-source copyright/license notice headers that spell out copyright holder names and years are hard to maintain and often out-of-date or plain wrong. Precise contributor information is already maintained automatically by the version control tool. Ultimately it is the receiver of a file who is responsible for determining its licensing status, and per-source notices are merely a convenience. Therefore it is simpler and more accurate for each source to have a generic notice of the license name and references to more detailed information on copyright holders and full license terms. Our `Copyright.txt` file now contains a list of Contributors whose names appeared source-level copyright notices. It also references version control history for more precise information. Therefore we no longer need to spell out the list of Contributors in each source file notice. Replace CMake per-source copyright/license notice headers with a short description of the license and links to `Copyright.txt` and online information available from "https://cmake.org/licensing". The online URL also handles cases of modules being copied out of our source into other projects, so we can drop our notices about replacing links with full license text. Run the `Utilities/Scripts/filter-notices.bash` script to perform the majority of the replacements mechanically. Manually fix up shebang lines and trailing newlines in a few files. Manually update the notices in a few files that the script does not handle.
9 years ago
  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 "cmParseArgumentsCommand.h"
  4. #include <map>
  5. #include <set>
  6. #include <utility>
  7. #include <cm/string_view>
  8. #include "cmArgumentParser.h"
  9. #include "cmArgumentParserTypes.h"
  10. #include "cmExecutionStatus.h"
  11. #include "cmList.h"
  12. #include "cmMakefile.h"
  13. #include "cmMessageType.h"
  14. #include "cmPolicies.h"
  15. #include "cmRange.h"
  16. #include "cmStringAlgorithms.h"
  17. #include "cmSystemTools.h"
  18. #include "cmValue.h"
  19. namespace {
  20. std::string EscapeArg(const std::string& arg)
  21. {
  22. // replace ";" with "\;" so output argument lists will split correctly
  23. std::string escapedArg;
  24. for (char i : arg) {
  25. if (i == ';') {
  26. escapedArg += '\\';
  27. }
  28. escapedArg += i;
  29. }
  30. return escapedArg;
  31. }
  32. std::string JoinList(std::vector<std::string> const& arg, bool escape)
  33. {
  34. return escape ? cmList::to_string(cmMakeRange(arg).transform(EscapeArg))
  35. : cmList::to_string(cmMakeRange(arg));
  36. }
  37. using options_map = std::map<std::string, bool>;
  38. using single_map = std::map<std::string, std::string>;
  39. using multi_map =
  40. std::map<std::string, ArgumentParser::NonEmpty<std::vector<std::string>>>;
  41. using options_set = std::set<cm::string_view>;
  42. struct UserArgumentParser : public cmArgumentParser<void>
  43. {
  44. void BindKeywordsMissingValue(std::vector<cm::string_view>& ref)
  45. {
  46. this->cmArgumentParser<void>::BindKeywordMissingValue(
  47. [&ref](Instance&, cm::string_view arg) { ref.emplace_back(arg); });
  48. }
  49. template <typename T, typename H>
  50. void Bind(std::vector<std::string> const& names,
  51. std::map<std::string, T>& ref, H duplicateKey)
  52. {
  53. for (std::string const& key : names) {
  54. auto const it = ref.emplace(key, T{}).first;
  55. bool const inserted = this->cmArgumentParser<void>::Bind(
  56. cm::string_view(it->first), it->second);
  57. if (!inserted) {
  58. duplicateKey(key);
  59. }
  60. }
  61. }
  62. };
  63. } // namespace
  64. static void PassParsedArguments(
  65. const std::string& prefix, cmMakefile& makefile, const options_map& options,
  66. const single_map& singleValArgs, const multi_map& multiValArgs,
  67. const std::vector<std::string>& unparsed, const options_set& keywordsSeen,
  68. const options_set& keywordsMissingValues, bool parseFromArgV)
  69. {
  70. for (auto const& iter : options) {
  71. makefile.AddDefinition(cmStrCat(prefix, iter.first),
  72. iter.second ? "TRUE" : "FALSE");
  73. }
  74. const cmPolicies::PolicyStatus cmp0174 =
  75. makefile.GetPolicyStatus(cmPolicies::CMP0174);
  76. for (auto const& iter : singleValArgs) {
  77. if (keywordsSeen.find(iter.first) == keywordsSeen.end()) {
  78. makefile.RemoveDefinition(cmStrCat(prefix, iter.first));
  79. } else if ((parseFromArgV && cmp0174 == cmPolicies::NEW) ||
  80. !iter.second.empty()) {
  81. makefile.AddDefinition(cmStrCat(prefix, iter.first), iter.second);
  82. } else {
  83. // The OLD policy behavior doesn't define a variable for an empty or
  84. // missing value, and we can't differentiate between those two cases.
  85. if (parseFromArgV && (cmp0174 == cmPolicies::WARN)) {
  86. makefile.IssueMessage(
  87. MessageType::AUTHOR_WARNING,
  88. cmStrCat("The ", iter.first,
  89. " keyword was followed by an empty string or no value at "
  90. "all. Policy CMP0174 is not set, so "
  91. "cmake_parse_arguments() will unset the ",
  92. prefix, iter.first,
  93. " variable rather than setting it to an empty string."));
  94. }
  95. makefile.RemoveDefinition(cmStrCat(prefix, iter.first));
  96. }
  97. }
  98. for (auto const& iter : multiValArgs) {
  99. if (!iter.second.empty()) {
  100. makefile.AddDefinition(cmStrCat(prefix, iter.first),
  101. JoinList(iter.second, parseFromArgV));
  102. } else {
  103. makefile.RemoveDefinition(cmStrCat(prefix, iter.first));
  104. }
  105. }
  106. if (!unparsed.empty()) {
  107. makefile.AddDefinition(cmStrCat(prefix, "UNPARSED_ARGUMENTS"),
  108. JoinList(unparsed, parseFromArgV));
  109. } else {
  110. makefile.RemoveDefinition(cmStrCat(prefix, "UNPARSED_ARGUMENTS"));
  111. }
  112. if (!keywordsMissingValues.empty()) {
  113. makefile.AddDefinition(
  114. cmStrCat(prefix, "KEYWORDS_MISSING_VALUES"),
  115. cmList::to_string(cmMakeRange(keywordsMissingValues)));
  116. } else {
  117. makefile.RemoveDefinition(cmStrCat(prefix, "KEYWORDS_MISSING_VALUES"));
  118. }
  119. }
  120. bool cmParseArgumentsCommand(std::vector<std::string> const& args,
  121. cmExecutionStatus& status)
  122. {
  123. // cmake_parse_arguments(prefix options single multi <ARGN>)
  124. // 1 2 3 4
  125. // or
  126. // cmake_parse_arguments(PARSE_ARGV N prefix options single multi)
  127. if (args.size() < 4) {
  128. status.SetError("must be called with at least 4 arguments.");
  129. return false;
  130. }
  131. auto argIter = args.begin();
  132. auto argEnd = args.end();
  133. bool parseFromArgV = false;
  134. unsigned long argvStart = 0;
  135. if (*argIter == "PARSE_ARGV") {
  136. if (args.size() != 6) {
  137. status.GetMakefile().IssueMessage(
  138. MessageType::FATAL_ERROR,
  139. "PARSE_ARGV must be called with exactly 6 arguments.");
  140. cmSystemTools::SetFatalErrorOccurred();
  141. return true;
  142. }
  143. parseFromArgV = true;
  144. argIter++; // move past PARSE_ARGV
  145. if (!cmStrToULong(*argIter, &argvStart)) {
  146. status.GetMakefile().IssueMessage(
  147. MessageType::FATAL_ERROR,
  148. cmStrCat("PARSE_ARGV index '", *argIter,
  149. "' is not an unsigned integer"));
  150. cmSystemTools::SetFatalErrorOccurred();
  151. return true;
  152. }
  153. argIter++; // move past N
  154. }
  155. // the first argument is the prefix
  156. const std::string prefix = (*argIter++) + "_";
  157. UserArgumentParser parser;
  158. // define the result maps holding key/value pairs for
  159. // options, single values and multi values
  160. options_map options;
  161. single_map singleValArgs;
  162. multi_map multiValArgs;
  163. // anything else is put into a vector of unparsed strings
  164. std::vector<std::string> unparsed;
  165. auto const duplicateKey = [&status](std::string const& key) {
  166. status.GetMakefile().IssueMessage(
  167. MessageType::WARNING, cmStrCat("keyword defined more than once: ", key));
  168. };
  169. // the second argument is a (cmake) list of options without argument
  170. cmList list{ *argIter++ };
  171. parser.Bind(list, options, duplicateKey);
  172. // the third argument is a (cmake) list of single argument options
  173. list.assign(*argIter++);
  174. parser.Bind(list, singleValArgs, duplicateKey);
  175. // the fourth argument is a (cmake) list of multi argument options
  176. list.assign(*argIter++);
  177. parser.Bind(list, multiValArgs, duplicateKey);
  178. list.clear();
  179. if (!parseFromArgV) {
  180. // Flatten ;-lists in the arguments into a single list as was done
  181. // by the original function(CMAKE_PARSE_ARGUMENTS).
  182. for (; argIter != argEnd; ++argIter) {
  183. list.append(*argIter);
  184. }
  185. } else {
  186. // in the PARSE_ARGV move read the arguments from ARGC and ARGV#
  187. std::string argc = status.GetMakefile().GetSafeDefinition("ARGC");
  188. unsigned long count;
  189. if (!cmStrToULong(argc, &count)) {
  190. status.GetMakefile().IssueMessage(
  191. MessageType::FATAL_ERROR,
  192. cmStrCat("PARSE_ARGV called with ARGC='", argc,
  193. "' that is not an unsigned integer"));
  194. cmSystemTools::SetFatalErrorOccurred();
  195. return true;
  196. }
  197. for (unsigned long i = argvStart; i < count; ++i) {
  198. const std::string argName{ cmStrCat("ARGV", i) };
  199. cmValue arg = status.GetMakefile().GetDefinition(argName);
  200. if (!arg) {
  201. status.GetMakefile().IssueMessage(
  202. MessageType::FATAL_ERROR,
  203. cmStrCat("PARSE_ARGV called with ", argName, " not set"));
  204. cmSystemTools::SetFatalErrorOccurred();
  205. return true;
  206. }
  207. list.emplace_back(*arg);
  208. }
  209. }
  210. std::vector<cm::string_view> keywordsSeen;
  211. parser.BindParsedKeywords(keywordsSeen);
  212. // For single-value keywords, only the last instance matters, since it
  213. // determines the value stored. But if a keyword is repeated, it will be
  214. // added to this vector if _any_ instance is missing a value. If one of the
  215. // earlier instances is missing a value but the last one isn't, its presence
  216. // in this vector will be misleading.
  217. std::vector<cm::string_view> keywordsMissingValues;
  218. parser.BindKeywordsMissingValue(keywordsMissingValues);
  219. parser.Parse(list, &unparsed);
  220. PassParsedArguments(
  221. prefix, status.GetMakefile(), options, singleValArgs, multiValArgs,
  222. unparsed, options_set(keywordsSeen.begin(), keywordsSeen.end()),
  223. options_set(keywordsMissingValues.begin(), keywordsMissingValues.end()),
  224. parseFromArgV);
  225. return true;
  226. }