summaryrefslogtreecommitdiffstats
path: root/Source/cmFindBase.cxx
blob: 952cd80271d67c7808766513a09558ec9f70317c (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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*=========================================================================

  Program:   CMake - Cross-Platform Makefile Generator
  Module:    $RCSfile$
  Language:  C++
  Date:      $Date$
  Version:   $Revision$

  Copyright (c) 2002 Kitware, Inc., Insight Consortium.  All rights reserved.
  See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.

     This software is distributed WITHOUT ANY WARRANTY; without even 
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/
#include "cmFindBase.h"
  
cmFindBase::cmFindBase()
{
  this->AlreadyInCache = false;
  this->NoSystemPath = false;
  this->NoCMakePath = false;
  this->NoCMakeEnvironmentPath = false;
  this->NoCMakeSystemPath = false;
  // default is to search frameworks first on apple
#if defined(__APPLE__)
  this->SearchFrameworkFirst = true;
#else
   this->SearchFrameworkFirst = false;
#endif
  this->SearchFrameworkOnly = false;
  this->SearchFrameworkLast = false;
}

bool cmFindBase::ParseArguments(std::vector<std::string> const& argsIn)
{
  if(argsIn.size() < 2 )
    {
    this->SetError("called with incorrect number of arguments");
    return false;
    }
  // copy argsIn into args so it can be modified,
  // in the process extract the DOC "documentation" 
  size_t size = argsIn.size();
  std::vector<std::string> args;
  bool foundDoc = false;
  for(unsigned int j = 0; j < size; ++j)
    {
    if(foundDoc  || argsIn[j] != "DOC" )
      {
      args.push_back(argsIn[j]);
      }
    else
      {
      if(j+1 < size)
        {
        foundDoc = true;
        this->VariableDocumentation = argsIn[j+1];
        j++;
        if(j >= size)
          {
          break;
          }
        }
      }
    }
  this->VariableName = args[0];
  if(this->CheckForVariableInCache())
    {
    this->AlreadyInCache = true;
    return true;
    }
  this->AlreadyInCache = false; 
  std::vector<std::string> userPaths;
  std::string doc;
  bool foundName = false;
  bool doingNames = false;
  bool foundPaths = false;
  bool doingPaths = false;
  bool doingPathSuf = false;
  bool newStyle = false;

  for (unsigned int j = 1; j < args.size(); ++j)
    {
    if(args[j] == "NAMES")
      {
      doingNames = true;
      newStyle = true;
      doingPathSuf = false;
      doingPaths = false;
      }
    else if (args[j] == "PATHS")
      {
      doingPaths = true;
      newStyle = true;
      doingNames = false;
      doingPathSuf = false;
      }
    else if (args[j] == "PATH_SUFFIXES")
      { 
      doingPathSuf = true;
      newStyle = true;
      doingNames = false;
      doingPaths = false;
      }
    else if (args[j] == "NO_SYSTEM_PATH")
      {
      this->NoSystemPath = true;
      }
    else if (args[j] == "NO_CMAKE_PATH")
      {
      this->NoCMakePath = true;
      }
    else if (args[j] == "NO_CMAKE_ENVIRONMENT_PATH")
      {
      this->NoCMakeEnvironmentPath = true;
      }
    else if (args[j] == "NO_CMAKE_SYSTEM_PATH")
      {
      this->NoCMakeSystemPath = true;
      }
    else
      {
      if(doingNames)
        {
        this->Names.push_back(args[j]);
        }
      else if(doingPaths)
        { 
        userPaths.push_back(args[j]);
        }
      else if(doingPathSuf)
        { 
        this->SearchPathSuffixes.push_back(args[j]);
        }
      }
    }
  if(this->VariableDocumentation.size() == 0)
    {
    this->VariableDocumentation = "Whare can ";
    if(this->Names.size() == 0)
      {
      this->VariableDocumentation += "the (unknown) library be found";
      }
    else if(this->Names.size() == 1)
      {
      this->VariableDocumentation += "the " + this->Names[0] + " library be found";
      }
    else
      { 
      this->VariableDocumentation += "one of the " + this->Names[0];
      for (unsigned int j = 1; j < this->Names.size() - 1; ++j)
        {
        this->VariableDocumentation += ", " + this->Names[j];
        }
      this->VariableDocumentation += " or " + this->Names[this->Names.size() - 1] + " libraries be found";
      }
    }

  // look for old style
  // FIND_*(VAR name path1 path2 ...)
  if(!newStyle)
    {
    std::cerr << "oldstyle\n";
    this->Names.push_back(args[1]);
    for(unsigned int j = 2; j < args.size(); ++j)
      {
      userPaths.push_back(args[j]);
      }
    }
  this->ExpandPaths(userPaths);
  return true;
}

void cmFindBase::ExpandPaths(std::vector<std::string> userPaths)
{
  // Add CMAKE_*_PATH environment variables
  if(!this->NoCMakeEnvironmentPath)
    {
    this->AddEnvironmentVairables();
    }
  // Add CMake varibles of the same name as the previous environment
  // varibles CMAKE_*_PATH to be used most of the time with -D
  // command line options
  if(!this->NoCMakePath)
    {
    this->AddCMakeVairables();
    }
  // add System environment PATH and (LIB or INCLUDE)
  if(!this->NoSystemPath)
    {
    this->AddSystemEnvironmentVairables();
    }
  // Add CMAKE_SYSTEM_*_PATH variables which are defined in platform files
  if(!this->NoCMakeSystemPath)
    {
    this->AddCMakeSystemVariables();
    }
  // add the paths specified in the FIND_* call 
  for(unsigned int i =0; i < userPaths.size(); ++i)
    {
    this->SearchPaths.push_back(userPaths[i]);
    }
  // clean things up
  this->ExpandRegistryAndCleanPath();
}

void cmFindBase::AddEnvironmentVairables()
{ 
  if(this->SearchFrameworkFirst || this->SearchFrameworkOnly)
    {
    cmSystemTools::GetPath(this->SearchPaths, "CMAKE_FRAMEWORK_PATH");
    }
  std::string var = "CMAKE_";
  var += this->CMakePathName;
  var += "_PATH";
  cmSystemTools::GetPath(this->SearchPaths, var.c_str());
  if(this->SearchFrameworkLast)
    {
    cmSystemTools::GetPath(this->SearchPaths, "CMAKE_FRAMEWORK_PATH");
    }

}

void cmFindBase::AddCMakeVairables()
{ 
  if(this->SearchFrameworkFirst || this->SearchFrameworkOnly)
    { 
    if(const char* path = m_Makefile->GetDefinition("CMAKE_FRAMEWORK_PATH"))
      {
      cmSystemTools::ExpandListArgument(path, this->SearchPaths);
      }
    }
  std::string var = "CMAKE_";
  var += this->CMakePathName;
  var += "_PATH";
  if(const char* path = m_Makefile->GetDefinition(var.c_str()))
    {
    cmSystemTools::ExpandListArgument(path, this->SearchPaths);
    } 
  if(this->SearchFrameworkLast)
    {
    if(const char* path = m_Makefile->GetDefinition("CMAKE_FRAMEWORK_PATH"))
      {
      cmSystemTools::ExpandListArgument(path, this->SearchPaths);
      }
    }
}

void cmFindBase::AddSystemEnvironmentVairables()
{
  // Add LIB or INCLUDE
  if(this->EnvironmentPath.size())
    {
    cmSystemTools::GetPath(this->SearchPaths, this->EnvironmentPath.c_str());
    }
  // Add PATH 
  cmSystemTools::GetPath(this->SearchPaths);
}

void cmFindBase::AddCMakeSystemVariables()
{  
  if(this->SearchFrameworkFirst || this->SearchFrameworkOnly)
    { 
    if(const char* path = m_Makefile->GetDefinition("CMAKE_SYSTEM_FRAMEWORK_PATH"))
      {
      cmSystemTools::ExpandListArgument(path, this->SearchPaths);
      }
    }
  std::string var = "CMAKE_SYSTEM_";
  var += this->CMakePathName;
  var += "_PATH";
  if(const char* path = m_Makefile->GetDefinition(var.c_str()))
    {
    cmSystemTools::ExpandListArgument(path, this->SearchPaths);
    }  
  if(this->SearchFrameworkLast)
    {
    if(const char* path = m_Makefile->GetDefinition("CMAKE_SYSTEM_FRAMEWORK_PATH"))
      {
      cmSystemTools::ExpandListArgument(path, this->SearchPaths);
      }
    }
}

void cmFindBase::ExpandRegistryAndCleanPath()
{
  std::vector<std::string> finalPath;
  std::vector<std::string>::iterator i;
  for(i = this->SearchPaths.begin();
      i != this->SearchPaths.end(); ++i)
    {
    cmSystemTools::ExpandRegistryValues(*i);
    cmSystemTools::GlobDirs(i->c_str(), finalPath);
    }
  this->SearchPaths.clear();
  // convert all paths to unix slashes
  for(i = finalPath.begin();
      i != finalPath.end(); ++i)
    {
    cmSystemTools::ConvertToUnixSlashes(*i);
    // copy each finalPath combined with SearchPathSuffixes
    // to the SearchPaths ivar
    for(std::vector<std::string>::iterator j = this->SearchPathSuffixes.begin();
        j != this->SearchPathSuffixes.end(); ++j)
      {
      std::string p = *i + std::string("/") + *j;
      if(cmSystemTools::FileIsDirectory(p.c_str()))
        {
        this->SearchPaths.push_back(p);
        }
      }
    }
  // now put the path without the path suffixes in the SearchPaths
  for(i = finalPath.begin();
      i != finalPath.end(); ++i)
    {
    if(cmSystemTools::FileIsDirectory(i->c_str()))
      {
      this->SearchPaths.push_back(*i);
      }
    }
}

void cmFindBase::PrintFindStuff()
{
  std::cerr << "VariableName " << this->VariableName << "\n";
  std::cerr << "VariableDocumentation " << this->VariableDocumentation << "\n";
  std::cerr << "NoSystemPath " << this->NoSystemPath << "\n";
  std::cerr << "NoCMakeEnvironmentPath " << this->NoCMakeEnvironmentPath << "\n";
  std::cerr << "NoCMakePath " << this->NoCMakePath << "\n";
  std::cerr << "NoCMakeSystemPath " << this->NoCMakeSystemPath << "\n";
  std::cerr << "EnvironmentPath " << this->EnvironmentPath << "\n";
  std::cerr << "CMakePathName " << this->CMakePathName << "\n";
  std::cerr << "Names  ";
  for(unsigned int i =0; i < this->Names.size(); ++i)
    {
    std::cerr << this->Names[i] << " ";
    }
  std::cerr << "\n";
  std::cerr << "\n";
  std::cerr << "SearchPathSuffixes  ";
  for(unsigned int i =0; i < this->SearchPathSuffixes.size(); ++i)
    {
    std::cerr << this->SearchPathSuffixes[i] << "\n";
    }
  std::cerr << "\n";
  std::cerr << "SearchPaths\n";
  for(unsigned int i =0; i < this->SearchPaths.size(); ++i)
    {
    std::cerr << "[" << this->SearchPaths[i] << "]\n";
    }
}

bool cmFindBase::CheckForVariableInCache()
{
  std::cerr << "CheckForVariableInCache " << this->VariableName << "\n";
  const char* cacheValue
    = m_Makefile->GetDefinition(this->VariableName.c_str());
  if(cacheValue && !cmSystemTools::IsNOTFOUND(cacheValue))
    {
    return true;
    }
  if(cacheValue)
    {
    std::cerr << "Cachevalue " << cacheValue << "\n";
    cmCacheManager::CacheIterator it = 
      m_Makefile->GetCacheManager()->GetCacheIterator(this->VariableName.c_str());
    if(!it.IsAtEnd())
      {
      const char* hs = it.GetProperty("HELPSTRING");
      this->VariableDocumentation = hs?hs:"(none)";
      }
    }
  return false;
}