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"
10 #include <cm/string_view>
12 #include "cmArgumentParser.h"
13 #include "cmExecutionStatus.h"
14 #include "cmMakefile.h"
15 #include "cmMessageType.h"
17 #include "cmStringAlgorithms.h"
18 #include "cmSystemTools.h"
21 static std::string EscapeArg(const std::string& arg)
23 // replace ";" with "\;" so output argument lists will split correctly
24 std::string escapedArg;
34 static std::string JoinList(std::vector<std::string> const& arg, bool escape)
36 return escape ? cmJoin(cmMakeRange(arg).transform(EscapeArg), ";")
37 : cmJoin(cmMakeRange(arg), ";");
42 using options_map = std::map<std::string, bool>;
43 using single_map = std::map<std::string, std::string>;
44 using multi_map = std::map<std::string, std::vector<std::string>>;
45 using options_set = std::set<std::string>;
47 struct UserArgumentParser : public cmArgumentParser<void>
49 template <typename T, typename H>
50 void Bind(std::vector<std::string> const& names,
51 std::map<std::string, T>& ref, H duplicateKey)
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);
66 static void PassParsedArguments(
67 const std::string& prefix, cmMakefile& makefile, const options_map& options,
68 const single_map& singleValArgs, const multi_map& multiValArgs,
69 const std::vector<std::string>& unparsed,
70 const options_set& keywordsMissingValues, bool parseFromArgV)
72 for (auto const& iter : options) {
73 makefile.AddDefinition(prefix + iter.first,
74 iter.second ? "TRUE" : "FALSE");
77 for (auto const& iter : singleValArgs) {
78 if (!iter.second.empty()) {
79 makefile.AddDefinition(prefix + iter.first, iter.second);
81 makefile.RemoveDefinition(prefix + iter.first);
85 for (auto const& iter : multiValArgs) {
86 if (!iter.second.empty()) {
87 makefile.AddDefinition(prefix + iter.first,
88 JoinList(iter.second, parseFromArgV));
90 makefile.RemoveDefinition(prefix + iter.first);
94 if (!unparsed.empty()) {
95 makefile.AddDefinition(prefix + "UNPARSED_ARGUMENTS",
96 JoinList(unparsed, parseFromArgV));
98 makefile.RemoveDefinition(prefix + "UNPARSED_ARGUMENTS");
101 if (!keywordsMissingValues.empty()) {
102 makefile.AddDefinition(prefix + "KEYWORDS_MISSING_VALUES",
103 cmJoin(cmMakeRange(keywordsMissingValues), ";"));
105 makefile.RemoveDefinition(prefix + "KEYWORDS_MISSING_VALUES");
109 bool cmParseArgumentsCommand(std::vector<std::string> const& args,
110 cmExecutionStatus& status)
112 // cmake_parse_arguments(prefix options single multi <ARGN>)
115 // cmake_parse_arguments(PARSE_ARGV N prefix options single multi)
116 if (args.size() < 4) {
117 status.SetError("must be called with at least 4 arguments.");
121 auto argIter = args.begin();
122 auto argEnd = args.end();
123 bool parseFromArgV = false;
124 unsigned long argvStart = 0;
125 if (*argIter == "PARSE_ARGV") {
126 if (args.size() != 6) {
127 status.GetMakefile().IssueMessage(
128 MessageType::FATAL_ERROR,
129 "PARSE_ARGV must be called with exactly 6 arguments.");
130 cmSystemTools::SetFatalErrorOccurred();
133 parseFromArgV = true;
134 argIter++; // move past PARSE_ARGV
135 if (!cmStrToULong(*argIter, &argvStart)) {
136 status.GetMakefile().IssueMessage(MessageType::FATAL_ERROR,
137 "PARSE_ARGV index '" + *argIter +
138 "' is not an unsigned integer");
139 cmSystemTools::SetFatalErrorOccurred();
142 argIter++; // move past N
144 // the first argument is the prefix
145 const std::string prefix = (*argIter++) + "_";
147 UserArgumentParser parser;
149 // define the result maps holding key/value pairs for
150 // options, single values and multi values
152 single_map singleValArgs;
153 multi_map multiValArgs;
155 // anything else is put into a vector of unparsed strings
156 std::vector<std::string> unparsed;
158 auto const duplicateKey = [&status](std::string const& key) {
159 status.GetMakefile().IssueMessage(
160 MessageType::WARNING, "keyword defined more than once: " + key);
163 // the second argument is a (cmake) list of options without argument
164 std::vector<std::string> list = cmExpandedList(*argIter++);
165 parser.Bind(list, options, duplicateKey);
167 // the third argument is a (cmake) list of single argument options
169 cmExpandList(*argIter++, list);
170 parser.Bind(list, singleValArgs, duplicateKey);
172 // the fourth argument is a (cmake) list of multi argument options
174 cmExpandList(*argIter++, list);
175 parser.Bind(list, multiValArgs, duplicateKey);
178 if (!parseFromArgV) {
179 // Flatten ;-lists in the arguments into a single list as was done
180 // by the original function(CMAKE_PARSE_ARGUMENTS).
181 for (; argIter != argEnd; ++argIter) {
182 cmExpandList(*argIter, list);
185 // in the PARSE_ARGV move read the arguments from ARGC and ARGV#
186 std::string argc = status.GetMakefile().GetSafeDefinition("ARGC");
188 if (!cmStrToULong(argc, &count)) {
189 status.GetMakefile().IssueMessage(MessageType::FATAL_ERROR,
190 "PARSE_ARGV called with ARGC='" +
192 "' that is not an unsigned integer");
193 cmSystemTools::SetFatalErrorOccurred();
196 for (unsigned long i = argvStart; i < count; ++i) {
197 std::ostringstream argName;
198 argName << "ARGV" << i;
199 cmValue arg = status.GetMakefile().GetDefinition(argName.str());
201 status.GetMakefile().IssueMessage(MessageType::FATAL_ERROR,
202 "PARSE_ARGV called with " +
203 argName.str() + " not set");
204 cmSystemTools::SetFatalErrorOccurred();
207 list.emplace_back(*arg);
211 std::vector<std::string> keywordsMissingValues;
213 parser.Parse(list, &unparsed, &keywordsMissingValues);
216 prefix, status.GetMakefile(), options, singleValArgs, multiValArgs,
218 options_set(keywordsMissingValues.begin(), keywordsMissingValues.end()),