cmParseArgumentsCommand.cxx 6.08 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*============================================================================
  CMake - Cross Platform Makefile Generator
  Copyright 2015 Matthias Maennich <matthias@maennich.net>
  Copyright 2010 Alexander Neundorf <neundorf@kde.org>

  Distributed under the OSI-approved BSD License (the "License");
  see accompanying file Copyright.txt for details.

  This software is distributed WITHOUT ANY WARRANTY; without even the
  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  See the License for more information.
============================================================================*/

#include "cmParseArgumentsCommand.h"
#include "cmAlgorithms.h"

//----------------------------------------------------------------------------
bool cmParseArgumentsCommand
::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
{
  // cmake_parse_arguments(prefix options single multi <ARGN>)
  //                         1       2      3      4
  if (args.size() < 4)
    {
    this->SetError("must be called with at least 4 arguments.");
    return false;
    }

  std::vector<std::string>::const_iterator argIter = args.begin(),
                                           argEnd  = args.end();
  // the first argument is the prefix
  const std::string prefix = (*argIter++) + "_";

  // define the result maps holding key/value pairs for
  // options, single values and multi values
  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;
  options_map options;
  single_map single;
  multi_map multi;

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

46
47
48
49
  // remember already defined keywords
  std::set<std::string> used_keywords;
  const std::string dup_warning = "keyword defined more than once: ";

50
51
52
53
54
55
56
  // the second argument is a (cmake) list of options without argument
  std::vector<std::string> list;
  cmSystemTools::ExpandListArgument(*argIter++, list);
  for (std::vector<std::string>::const_iterator iter  = list.begin(),
                                                end   = list.end();
                                                iter != end; ++iter)
    {
57
58
59
60
    if (!used_keywords.insert(*iter).second)
      {
      this->GetMakefile()->IssueMessage(cmake::WARNING, dup_warning + *iter);
      }
61
62
63
64
65
66
67
68
69
70
    options[*iter]; // default initialize
    }

  // the third argument is a (cmake) list of single argument options
  list.clear();
  cmSystemTools::ExpandListArgument(*argIter++, list);
  for (std::vector<std::string>::const_iterator iter  = list.begin(),
                                                end   = list.end();
                                                iter != end; ++iter)
    {
71
72
73
74
    if (!used_keywords.insert(*iter).second)
      {
      this->GetMakefile()->IssueMessage(cmake::WARNING, dup_warning + *iter);
      }
75
76
77
78
79
80
81
82
83
84
    single[*iter]; // default initialize
    }

  // the fourth argument is a (cmake) list of multi argument options
  list.clear();
  cmSystemTools::ExpandListArgument(*argIter++, list);
  for (std::vector<std::string>::const_iterator iter  = list.begin(),
                                                end   = list.end();
                                                iter != end; ++iter)
    {
85
86
87
88
    if (!used_keywords.insert(*iter).second)
      {
      this->GetMakefile()->IssueMessage(cmake::WARNING, dup_warning + *iter);
      }
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
    multi[*iter]; // default initialize
    }

  enum insideValues
  {
    NONE,
    SINGLE,
    MULTI
  } insideValues = NONE;
  std::string currentArgName;

  // now iterate over the remaining arguments
  // and fill in the values where applicable
  for(; argIter != argEnd; ++argIter)
    {
    const options_map::iterator optIter = options.find(*argIter);
    if (optIter != options.end())
      {
      insideValues = NONE;
      optIter->second = true;
      continue;
      }

    const single_map::iterator singleIter = single.find(*argIter);
    if (singleIter != single.end())
      {
      insideValues = SINGLE;
      currentArgName = *argIter;
      continue;
      }

    const multi_map::iterator multiIter = multi.find(*argIter);
    if (multiIter != multi.end())
      {
      insideValues = MULTI;
      currentArgName = *argIter;
      continue;
      }

    switch(insideValues)
      {
      case SINGLE:
        single[currentArgName] = *argIter;
        insideValues = NONE;
        break;
      case MULTI:
        multi[currentArgName].push_back(*argIter);
        break;
      default:
        unparsed.push_back(*argIter);
        break;
      }
    }

  // now iterate over the collected values and update their definition
  // within the current scope. undefine if necessary.

  for (options_map::const_iterator iter = options.begin(), end = options.end();
                                   iter != end; ++iter)
    {
    this->Makefile->AddDefinition(prefix + iter->first,
                                  iter->second? "TRUE": "FALSE");
    }
  for (single_map::const_iterator iter = single.begin(), end = single.end();
                                  iter != end; ++iter)
    {
    if (!iter->second.empty())
      {
      this->Makefile->AddDefinition(prefix + iter->first,
                                    iter->second.c_str());
      }
    else
      {
      this->Makefile->RemoveDefinition(prefix + iter->first);
      }
    }

  for (multi_map::const_iterator iter = multi.begin(), end = multi.end();
                                  iter != end; ++iter)
    {
    if (!iter->second.empty())
      {
      this->Makefile->AddDefinition(prefix + iter->first,
                                    cmJoin(cmMakeRange(iter->second), ";")
                                      .c_str());
      }
    else
      {
      this->Makefile->RemoveDefinition(prefix + iter->first);
      }
    }

  if (!unparsed.empty())
    {
    this->Makefile->AddDefinition(prefix + "UNPARSED_ARGUMENTS",
                                  cmJoin(cmMakeRange(unparsed), ";").c_str());
    }
  else
    {
    this->Makefile->RemoveDefinition(prefix + "UNPARSED_ARGUMENTS");
    }

  return true;
}