cmParseArgumentsCommand.cxx 7.08 KB
Newer Older
1 2
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file Copyright.txt or https://cmake.org/licensing for details.  */
3
#include "cmParseArgumentsCommand.h"
4

5 6 7 8 9
#include <map>
#include <set>
#include <sstream>
#include <utility>

10
#include "cmAlgorithms.h"
11
#include "cmArgumentParser.h"
12
#include "cmMakefile.h"
13
#include "cmMessageType.h"
14
#include "cmRange.h"
15
#include "cmSystemTools.h"
16
#include "cm_string_view.hxx"
17 18

class cmExecutionStatus;
19

20
static std::string EscapeArg(const std::string& arg)
21 22 23
{
  // replace ";" with "\;" so output argument lists will split correctly
  std::string escapedArg;
24 25
  for (char i : arg) {
    if (i == ';') {
26 27
      escapedArg += '\\';
    }
28
    escapedArg += i;
29 30 31 32
  }
  return escapedArg;
}

33
static std::string JoinList(std::vector<std::string> const& arg, bool escape)
34
{
35 36 37 38 39
  return escape ? cmJoin(cmMakeRange(arg).transform(EscapeArg), ";")
                : cmJoin(cmMakeRange(arg), ";");
}

namespace {
40 41 42 43

typedef std::map<std::string, bool> options_map;
typedef std::map<std::string, std::string> single_map;
typedef std::map<std::string, std::vector<std::string>> multi_map;
44 45
typedef std::set<std::string> options_set;

46
struct UserArgumentParser : public cmArgumentParser<void>
47
{
48 49 50 51 52 53 54 55 56 57 58 59
  template <typename T, typename H>
  void Bind(std::vector<std::string> const& names,
            std::map<std::string, T>& ref, H duplicateKey)
  {
    for (std::string const& key : names) {
      auto const it = ref.emplace(key, T{}).first;
      bool const inserted = this->cmArgumentParser<void>::Bind(
        cm::string_view(it->first), it->second);
      if (!inserted) {
        duplicateKey(key);
      }
    }
60
  }
61
};
62

63
} // namespace
64

65 66 67 68 69
static void PassParsedArguments(
  const std::string& prefix, cmMakefile& makefile, const options_map& options,
  const single_map& singleValArgs, const multi_map& multiValArgs,
  const std::vector<std::string>& unparsed,
  const options_set& keywordsMissingValues, bool parseFromArgV)
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
{
  for (auto const& iter : options) {
    makefile.AddDefinition(prefix + iter.first,
                           iter.second ? "TRUE" : "FALSE");
  }

  for (auto const& iter : singleValArgs) {
    if (!iter.second.empty()) {
      makefile.AddDefinition(prefix + iter.first, iter.second.c_str());
    } else {
      makefile.RemoveDefinition(prefix + iter.first);
    }
  }

  for (auto const& iter : multiValArgs) {
    if (!iter.second.empty()) {
      makefile.AddDefinition(prefix + iter.first,
87
                             JoinList(iter.second, parseFromArgV).c_str());
88 89 90 91 92 93 94
    } else {
      makefile.RemoveDefinition(prefix + iter.first);
    }
  }

  if (!unparsed.empty()) {
    makefile.AddDefinition(prefix + "UNPARSED_ARGUMENTS",
95
                           JoinList(unparsed, parseFromArgV).c_str());
96 97 98
  } else {
    makefile.RemoveDefinition(prefix + "UNPARSED_ARGUMENTS");
  }
99 100 101 102 103 104 105 106

  if (!keywordsMissingValues.empty()) {
    makefile.AddDefinition(
      prefix + "KEYWORDS_MISSING_VALUES",
      cmJoin(cmMakeRange(keywordsMissingValues), ";").c_str());
  } else {
    makefile.RemoveDefinition(prefix + "KEYWORDS_MISSING_VALUES");
  }
107 108
}

109 110
bool cmParseArgumentsCommand::InitialPass(std::vector<std::string> const& args,
                                          cmExecutionStatus&)
111 112 113
{
  // cmake_parse_arguments(prefix options single multi <ARGN>)
  //                         1       2      3      4
114 115
  // or
  // cmake_parse_arguments(PARSE_ARGV N prefix options single multi)
116
  if (args.size() < 4) {
117 118
    this->SetError("must be called with at least 4 arguments.");
    return false;
119
  }
120 121

  std::vector<std::string>::const_iterator argIter = args.begin(),
122
                                           argEnd = args.end();
123 124 125 126 127
  bool parseFromArgV = false;
  unsigned long argvStart = 0;
  if (*argIter == "PARSE_ARGV") {
    if (args.size() != 6) {
      this->Makefile->IssueMessage(
128
        MessageType::FATAL_ERROR,
129 130 131 132 133 134 135
        "PARSE_ARGV must be called with exactly 6 arguments.");
      cmSystemTools::SetFatalErrorOccured();
      return true;
    }
    parseFromArgV = true;
    argIter++; // move past PARSE_ARGV
    if (!cmSystemTools::StringToULong(argIter->c_str(), &argvStart)) {
136
      this->Makefile->IssueMessage(MessageType::FATAL_ERROR,
137
                                   "PARSE_ARGV index '" + *argIter +
138 139 140 141 142 143
                                     "' is not an unsigned integer");
      cmSystemTools::SetFatalErrorOccured();
      return true;
    }
    argIter++; // move past N
  }
144 145 146
  // the first argument is the prefix
  const std::string prefix = (*argIter++) + "_";

147 148
  UserArgumentParser parser;

149 150 151
  // define the result maps holding key/value pairs for
  // options, single values and multi values
  options_map options;
152 153
  single_map singleValArgs;
  multi_map multiValArgs;
154 155 156 157

  // anything else is put into a vector of unparsed strings
  std::vector<std::string> unparsed;

158 159 160 161
  auto const duplicateKey = [this](std::string const& key) {
    this->GetMakefile()->IssueMessage(
      MessageType::WARNING, "keyword defined more than once: " + key);
  };
162

163 164 165
  // the second argument is a (cmake) list of options without argument
  std::vector<std::string> list;
  cmSystemTools::ExpandListArgument(*argIter++, list);
166
  parser.Bind(list, options, duplicateKey);
167 168 169 170

  // the third argument is a (cmake) list of single argument options
  list.clear();
  cmSystemTools::ExpandListArgument(*argIter++, list);
171
  parser.Bind(list, singleValArgs, duplicateKey);
172 173 174 175

  // the fourth argument is a (cmake) list of multi argument options
  list.clear();
  cmSystemTools::ExpandListArgument(*argIter++, list);
176
  parser.Bind(list, multiValArgs, duplicateKey);
177

178
  list.clear();
179 180 181 182 183 184 185 186 187 188 189
  if (!parseFromArgV) {
    // Flatten ;-lists in the arguments into a single list as was done
    // by the original function(CMAKE_PARSE_ARGUMENTS).
    for (; argIter != argEnd; ++argIter) {
      cmSystemTools::ExpandListArgument(*argIter, list);
    }
  } else {
    // in the PARSE_ARGV move read the arguments from ARGC and ARGV#
    std::string argc = this->Makefile->GetSafeDefinition("ARGC");
    unsigned long count;
    if (!cmSystemTools::StringToULong(argc.c_str(), &count)) {
190
      this->Makefile->IssueMessage(MessageType::FATAL_ERROR,
191 192 193 194 195 196 197 198 199 200
                                   "PARSE_ARGV called with ARGC='" + argc +
                                     "' that is not an unsigned integer");
      cmSystemTools::SetFatalErrorOccured();
      return true;
    }
    for (unsigned long i = argvStart; i < count; ++i) {
      std::ostringstream argName;
      argName << "ARGV" << i;
      const char* arg = this->Makefile->GetDefinition(argName.str());
      if (!arg) {
201
        this->Makefile->IssueMessage(MessageType::FATAL_ERROR,
202 203 204 205 206
                                     "PARSE_ARGV called with " +
                                       argName.str() + " not set");
        cmSystemTools::SetFatalErrorOccured();
        return true;
      }
wahikihiki's avatar
wahikihiki committed
207
      list.emplace_back(arg);
208
    }
209
  }
210

211
  std::vector<std::string> keywordsMissingValues;
212

213
  parser.Parse(list, &unparsed, &keywordsMissingValues);
214

215 216 217 218
  PassParsedArguments(
    prefix, *this->Makefile, options, singleValArgs, multiValArgs, unparsed,
    options_set(keywordsMissingValues.begin(), keywordsMissingValues.end()),
    parseFromArgV);
219 220 221

  return true;
}