summaryrefslogtreecommitdiffstats
path: root/Source/cmCableDefineSetCommand.cxx
blob: 7d5a35c694104ab40ea32b971f9141cf83f39c0a (plain)
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*=========================================================================

  Program:   Insight Segmentation & Registration Toolkit
  Module:    $RCSfile$
  Language:  C++
  Date:      $Date$
  Version:   $Revision$

Copyright (c) 2001 Insight Consortium
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

 * Redistributions of source code must retain the above copyright notice,
   this list of conditions and the following disclaimer.

 * Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

 * The name of the Insight Consortium, nor the names of any consortium members,
   nor of any contributors, may be used to endorse or promote products derived
   from this software without specific prior written permission.

  * Modified source versions must be plainly marked as such, and must not be
    misrepresented as being the original software.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS ``AS IS''
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

=========================================================================*/
#include "cmCableDefineSetCommand.h"
#include "cmCacheManager.h"

#include "cmRegularExpression.h"


// cmCableDefineSetCommand
bool cmCableDefineSetCommand::Invoke(std::vector<std::string>& args)
{
  if(args.size() < 2)
    {
    this->SetError("called with incorrect number of arguments");
    return false;
    }
  
  // This command needs access to the Cable data.
  this->SetupCableData();
  
  std::vector<std::string>::const_iterator arg = args.begin();
  
  // The first argument is the name of the set.
  m_SetName = *arg++;
  
  // All arguments until a "SOURCE_FILES" are the elements to be placed in
  // the set.
  for(; (arg != args.end()) && (*arg != "SOURCE_FILES"); ++arg)
    {
    // If the element cannot be added, return an error.
    // This can occur when a tag is not specified and can't be generated.
    if(!this->AddElement(*arg))
      { return false; }
    }

  // If we are not at the end, the "SOURCE_FILES" keyword has been
  // encountered.
  if(arg != args.end())
    {
    // The rest of the arguments are source files to be included in
    // any package which references the set.
    for(++arg; arg != args.end(); ++arg)
      {
      if(!this->AddSourceFile(*arg))
        { return false; }
      }
    }
  
  // Write this command's configuration output.
  this->WriteConfiguration();
  
  return true;
}


/**
 * Write the CABLE configuration code to define this Set.
 */
void cmCableDefineSetCommand::WriteConfiguration() const
{
  cmRegularExpression needCdataBlock("[&<>]");
  
  // Get the ouptut information from the cmCableData.
  std::ostream& os = m_CableData->GetOutputStream();
  cmCableData::Indentation indent = m_CableData->GetIndentation();
  
  // Output the code.
  os << indent << "<Set name=\"" << m_SetName.c_str() << "\">" << std::endl;
  for(std::vector<std::string>::const_iterator e = m_SourceHeaders.begin();
      e != m_SourceHeaders.end(); ++e)
    {
    os << indent << "  <File name=\"" << e->c_str() << "\"/>" << std::endl;
    }
  for(std::vector<std::string>::const_iterator e = m_InstantiationSources.begin();
      e != m_InstantiationSources.end(); ++e)
    {
    os << indent << "  <File name=\"" << e->c_str()
       << "\" purpose=\"instantiate\"/>" << std::endl;
    }
  for(Elements::const_iterator e = m_Elements.begin();
      e != m_Elements.end(); ++e)
    {
    os << indent << "  <Element";
    // Only output the tag if it is not the empty string.
    if(e->first.length() > 0)
      {
      os << " tag=\"" << e->first.c_str() << "\"";
      }
    os << ">";
    if(needCdataBlock.find(e->second.c_str()))
      {
      os << "<![CDATA[" << e->second.c_str() << "]]>";
      }
    else
      {
      os << e->second.c_str();
      }
    os << "</Element>" << std::endl;
    }
  os << indent << "</Set>" << std::endl;
}


/**
 * Add an element to the set.  The given string is the argument to the
 * command describing the element.  There are two formats allowed:
 *  "code" = The code describing the element to CABLE is simply given.
 *           The GenerateTag() method will guess at a good tag for the
 *           code.
 *  "tag:code" = The left side of a single colon is text describing the tag.
 *               GenerateTag() will not be called.
 */
bool cmCableDefineSetCommand::AddElement(const std::string& arg)
{
  // A regular expression to match the tagged element specification.
  cmRegularExpression tagGiven("^([A-Za-z_0-9]*)[ \t]*:[ \t]*([^:].*|::.*)$");
  
  std::string tag;
  std::string code;

  if(tagGiven.find(arg.c_str()))
    {
    // A tag was given.  Use it.
    tag = tagGiven.match(1);
    code = tagGiven.match(2);
    }
  else
    {
    // No tag was given.  Try to generate one.
    if(!this->GenerateTag(arg, tag))
      { return false; }
    code = arg;
    }
  
  // Add an element with the given tag and code.
  m_Elements.push_back(Element(tag, code));
  return true;
}


/**
 * Given the string representing a set element, automatically generate
 * the CABLE element tag for it.
 *
 * **This function determines how the output language of all
 * CABLE-generated wrappers will look!**
 */
bool
cmCableDefineSetCommand::GenerateTag(const std::string& element,
                                     std::string& tag)
{
  // Hold the regular expressions for matching against the element.
  cmRegularExpression regex;
  
  // If the element's code begins in a $, it is referring to a set name.
  // The set's elements have their own tags, so we don't need one.
  regex.compile("^[ \t]*\\$");
  if(regex.find(element))
    { tag = ""; return true; }
  
  // Test for simple integer
  regex.compile("^[ \t]*([0-9]*)[ \t]*$");
  if(regex.find(element))
    {
    tag = "_";
    tag.append(regex.match(1));
    return true;
    }

  // Test for basic integer type
  regex.compile("^[ \t]*(unsigned[ ]|signed[ ])?[ \t]*(char|short|int|long|long[ ]long)[ \t]*$");
  if(regex.find(element))
    {
    tag = "_";
    if(regex.match(1) == "unsigned ")
      { tag.append("u"); }
    if(regex.match(2) == "long long")
      { tag.append("llong"); }
    else
      { tag.append(regex.match(2)); }
    return true;
    }

  // Test for basic floating-point type
  regex.compile("^[ \t]*(long[ ])?[ \t]*(float|double)[ \t]*$");
  if(regex.find(element))
    {
    tag = "_";
    if(regex.match(1) == "long ")
      tag.append("l");
    tag.append(regex.match(2));
    return true;
    }

  // Test for basic wide-character type
  regex.compile("^[ \t]*(wchar_t)[ \t]*$");
  if(regex.find(element))
    {
    tag = "_wchar";
    return true;
    }

  // Test for plain type name (without template arguments).
  regex.compile("^[ \t]*([A-Za-z_][A-Za-z0-9_]*)[ \t]*$");
  if(regex.find(element))
    {
    // The tag is the same as the type.
    tag = regex.match(1);
    return true;
    }
  
  // Test for template class instance.
  regex.compile("^[ \t]*([A-Za-z_][A-Za-z0-9_]*)<.*[ \t]*$");
  if(regex.find(element))
    {
    // The tag is the type without arguments (the arguments may have
    // their own tags).
    tag = regex.match(1);
    return true;
    }
  
  // We can't generate a tag.
  std::string err =
    ("doesn't know how to generate tag for element \""+element+"\" in set \""
     +m_SetName+"\"\nPlease specify one with the \"tag:element\" syntax.");
  this->SetError(err.c_str());

  tag = "";
  
  return false;
}


/**
 * Add a source file associated with this set.  Any package referencing
 * this set will automatically include this source file.
 */
bool cmCableDefineSetCommand::AddSourceFile(const std::string& file)
{
  // We must locate the file in the include path so that we can detect
  // its extension, and whether there is more than one to find.
  std::string header = file+".h";
  std::string txx = file+".txx";
  m_Makefile->ExpandVariablesInString(header);
  m_Makefile->ExpandVariablesInString(txx);
      
  // See if the file just exists here.  The compiler's search path will
  // locate it.
  if(cmSystemTools::FileExists(header.c_str()))
    {
    m_SourceHeaders.push_back(header);
    // See if there is a matching .txx as well.
    if(cmSystemTools::FileExists(txx.c_str()))
      {
      m_InstantiationSources.push_back(txx);
      }
    return true;
    }
  
  // We must look for the file in the include search path.
  const std::vector<std::string>& includeDirectories =
    m_Makefile->GetIncludeDirectories();
  
  for(std::vector<std::string>::const_iterator dir = includeDirectories.begin();
      dir != includeDirectories.end(); ++dir)
    {
    std::string path = *dir + "/";
    m_Makefile->ExpandVariablesInString(path);
    if(cmSystemTools::FileExists((path+header).c_str()))
      {
      m_SourceHeaders.push_back(header);
      // See if there is a matching .txx as well.
      if(cmSystemTools::FileExists((path+txx).c_str()))
        {
        m_InstantiationSources.push_back(txx);
        }
      return true;
      }
    }

  // We couldn't locate the source file.  Report the error.
  std::string err = "couldn't find source file " + header;
  this->SetError(err.c_str());
  return false;
}