summaryrefslogtreecommitdiffstats
path: root/Source/cmOrderLinkDirectories.cxx
blob: 7697aaa84539fb1a8161eb2f43fefc6e80b0bd0e (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
#include "cmOrderLinkDirectories.h"
#include "cmSystemTools.h"
#include "cmsys/RegularExpression.hxx"


inline void printv(std::vector<cmStdString>& v)
{
  for(unsigned int i = 0; i < v.size(); ++i)
    {
    std::cerr << "[" << v[i] << "]" << " ";
    }
  std::cerr << "\n";
}


//-------------------------------------------------------------------
bool cmOrderLinkDirectories::LibraryInDirectory(const char* dir, 
                                                const char* lib)
{
  cmStdString path = dir;
  path += "/";
  path += lib;
  // first look for the library as given
  if(cmSystemTools::FileExists(path.c_str()))
    {
    return true;
    }
  // next remove the extension (.a, .so ) and look for the library
  // under a different name as the linker can do either
  if(m_RemoveLibraryExtension.find(lib))
    {
    cmStdString lib = m_RemoveLibraryExtension.match(1);
    cmStdString ext = m_RemoveLibraryExtension.match(2);
    for(std::vector<cmStdString>::iterator i = m_LinkExtensions.begin();
        i != m_LinkExtensions.end(); ++i)
      {
      if(ext != *i)
        {
        path = dir;
        path += "/";
        path += lib + *i;
        if(cmSystemTools::FileExists(path.c_str()))
          {
          return true;
          } 
        }
      }
    }
  return false;
}

//-------------------------------------------------------------------
void cmOrderLinkDirectories::FindLibrariesInSeachPaths()
{
  for(std::set<cmStdString>::iterator dir = m_LinkPathSet.begin();
      dir != m_LinkPathSet.end(); ++dir)
    {
    for(std::map<cmStdString, Library>::iterator lib
          = m_FullPathLibraries.begin();
        lib != m_FullPathLibraries.end(); ++lib)
      {
      if(lib->second.Path != *dir)
        {
        if(LibraryInDirectory(dir->c_str(), lib->second.File.c_str()))
          {
          m_LibraryToDirectories[lib->second.FullPath].push_back(*dir);
          }
        }
      }
    }
}
                 
//-------------------------------------------------------------------
void cmOrderLinkDirectories::FindIndividualLibraryOrders()
{
  for(std::vector<Library>::iterator lib = m_MultiDirectoryLibraries.begin();
      lib != m_MultiDirectoryLibraries.end(); ++lib)
    {
    std::vector<cmStdString>& dirs = m_LibraryToDirectories[lib->FullPath];
    m_DirectoryToAfterList[lib->Path] = dirs;
    }
}


//-------------------------------------------------------------------
void
cmOrderLinkDirectories::PrintMap(const char* name,
                       std::map<cmStdString, std::vector<cmStdString> >& m)
{
  std::cerr << name << "\n";
  for(std::map<cmStdString, std::vector<cmStdString> >::iterator i =
        m.begin(); i != m.end();
      ++i)
    {
    std::cerr << i->first << ":  ";
    for(std::vector<cmStdString>::iterator l = i->second.begin();
        l != i->second.end(); ++l)
      {
      std::cerr << *l << " ";
      }
    std::cerr << "\n";
    }
}

//-------------------------------------------------------------------
void cmOrderLinkDirectories::CreateRegularExpressions()
{
  cmStdString libext = "(";
  bool first = true;
  for(std::vector<cmStdString>::iterator i = m_LinkExtensions.begin();
      i != m_LinkExtensions.end(); ++i)
    {
    if(!first)
      {
      libext += "|";
      }
    first = false;
    libext += "\\";
    libext += *i;
    }
  libext += ").*";
  cmStdString reg("(.*)");
  reg += libext;
  m_RemoveLibraryExtension.compile(reg.c_str());
  reg = "^lib([^/]*)";
  reg += libext;
  m_ExtractBaseLibraryName.compile(reg.c_str());
  reg = "([^/]*)";
  reg += libext;
  m_ExtractBaseLibraryNameNoPrefix.compile(reg.c_str());
}


//-------------------------------------------------------------------
void cmOrderLinkDirectories::PrepareLinkTargets()
{
  for(std::vector<cmStdString>::iterator i = m_LinkItems.begin();
      i != m_LinkItems.end(); ++i)
    {
    // separate the library name from libfoo.a or foo.a
    if(m_ExtractBaseLibraryName.find(*i))
      {
      *i = m_ExtractBaseLibraryName.match(1);
      }
    else if(m_ExtractBaseLibraryNameNoPrefix.find(*i))
      {
      *i = m_ExtractBaseLibraryNameNoPrefix.match(1);
      }
    }
}

//-------------------------------------------------------------------
bool cmOrderLinkDirectories::CanBeBefore(const cmStdString& d1,
                                         const cmStdString& d2)
{
  if(m_DirectoryToAfterList.count(d2) == 0)
    {
    return true;
    }
  std::vector<cmStdString>& d2dirs = m_DirectoryToAfterList[d2];
  // is d1 in the d2's list of directories that d2 must be before
  // if so, then d1 can not come before d2
  for(std::vector<cmStdString>::iterator i = d2dirs.begin();
      i != d2dirs.end(); ++i)
    {
    if(*i == d1)
      {
      return false;
      }
    }
  return true;
}

// This is a stl function object used to sort
// the vector of library paths.  It returns true
// if left directory can be before right directory (no swap).
// It also checks for the impossible case of two libraries and
// two directories that have both libraries.
struct cmOrderLinkDirectoriesCompare
  : public std::binary_function <cmStdString, cmStdString, bool> 
{
  cmOrderLinkDirectoriesCompare() 
  {
    This = 0;
  }
  bool operator()(
                  const cmStdString& left, 
                  const cmStdString& right
                  ) const
  {
    bool ret = This->CanBeBefore(left, right);
    if(!ret)
      {
      // check for the case when both libraries have to come
      // before each other 
      if(!This->CanBeBefore(right, left))
        {
        This->AddImpossible(right, left);
        }
      }
    return ret;
  }
  cmOrderLinkDirectories* This;
};

//-------------------------------------------------------------------
void cmOrderLinkDirectories::AddImpossible(const cmStdString& d1,
                                           const cmStdString& d2)
{
  m_ImposibleDirectories.insert(d1);
  m_ImposibleDirectories.insert(d2);
}

//-------------------------------------------------------------------
void cmOrderLinkDirectories::OrderPaths(std::vector<cmStdString>&
                                        orderedPaths)
{
  cmOrderLinkDirectoriesCompare comp;
  comp.This = this;
  std::sort(orderedPaths.begin(), orderedPaths.end(), comp);
}

//-------------------------------------------------------------------
void cmOrderLinkDirectories::SetLinkInformation(const cmTarget& target,
                                                cmTarget::LinkLibraryType 
                                                linktype,
                                                const char* targetLibrary)
{
    // collect the search paths from the target into paths set
  const std::vector<std::string>& searchPaths = target.GetLinkDirectories();
  for(std::vector<std::string>::const_iterator p = searchPaths.begin();
      p != searchPaths.end(); ++p)
    {
    m_LinkPathSet.insert(*p);
    }
  // collect the link items from the target and put it into libs
  const cmTarget::LinkLibraries& tlibs = target.GetLinkLibraries();
  std::vector<cmStdString>  libs;
  for(cmTarget::LinkLibraries::const_iterator lib = tlibs.begin();
      lib != tlibs.end(); ++lib)
    {
    // skip zero size library entries, this may happen
    // if a variable expands to nothing.
    if (lib->first.size() == 0)
      {
      continue;
      }
    // Don't link the library against itself!
    if(targetLibrary && (lib->first == targetLibrary))
      {
      continue;
      }  
    // use the correct lib for the current configuration
    if (lib->second == cmTarget::DEBUG && linktype != cmTarget::DEBUG)
      {
      continue;
      }
    if (lib->second == cmTarget::OPTIMIZED && 
        linktype != cmTarget::OPTIMIZED)
      {
      continue;
      }
    m_RawLinkItems.push_back(lib->first);
    }
}

//-------------------------------------------------------------------
bool cmOrderLinkDirectories::DetermineLibraryPathOrder()
{
  // set up all the regular expressions
  this->CreateRegularExpressions();
  std::vector<cmStdString> finalOrderPaths;
  // find all libs that are full paths
  Library aLib;
  cmStdString dir;
  cmStdString file;
  for(unsigned int i=0; i < m_RawLinkItems.size(); ++i)
    {
    if(cmSystemTools::FileIsFullPath(m_RawLinkItems[i].c_str()))
      {
      cmSystemTools::SplitProgramPath(m_RawLinkItems[i].c_str(),
                                      dir, file);
      m_LinkPathSet.insert(dir);
      aLib.FullPath = m_RawLinkItems[i];
      aLib.File = file;
      aLib.Path = dir;
      m_FullPathLibraries[aLib.FullPath] = aLib;
      m_LinkItems.push_back(file);
      }
    else
      {
      m_LinkItems.push_back(m_RawLinkItems[i]);
      }
    }
  this->FindLibrariesInSeachPaths();
  for(std::map<cmStdString, std::vector<cmStdString> >::iterator lib =
        m_LibraryToDirectories.begin(); lib!= m_LibraryToDirectories.end(); 
      ++lib)
    {
    if(lib->second.size() > 0)
      {
      m_MultiDirectoryLibraries.push_back(m_FullPathLibraries[lib->first]);
      }
    else
      {
      m_SingleDirectoryLibraries.push_back(m_FullPathLibraries[lib->first]);
      }
    }
  this->FindIndividualLibraryOrders();
  m_SortedSearchPaths.clear();
  for(std::set<cmStdString>::iterator i = m_LinkPathSet.begin();
      i != m_LinkPathSet.end(); ++i)
    {
    m_SortedSearchPaths.push_back(*i);
    }
  
  this->OrderPaths(m_SortedSearchPaths);
  // now turn libfoo.a into foo and foo.a into foo
  // This will prepare the link items for -litem 
  this->PrepareLinkTargets();
  //  this->PrintMap("m_DirectoryToAfterList", m_DirectoryToAfterList);
  //this->PrintMap("m_LibraryToDirectories", m_LibraryToDirectories);
  //std::cerr << "link objects: ";
  //printv(m_LinkItems);
  if(m_ImposibleDirectories.size())
    {
    return false;
    }
  return true;
}

std::string cmOrderLinkDirectories::GetWarnings()
{
  std::string warning = "It is impossible to order the linker search path in such a way that libraries specified as full paths will be picked by the linker.\nDirectories and libraries involvied are:\n";
  for(std::set<cmStdString>::iterator i = m_ImposibleDirectories.begin();
      i != m_ImposibleDirectories.end(); ++i)
    {
    warning += "Directory: ";
    warning += *i;
    warning += " contains ";
    std::map<cmStdString, std::vector<cmStdString> >::iterator j;
    for(j = m_LibraryToDirectories.begin(); 
        j != m_LibraryToDirectories.end(); ++j)
      {
      if(std::find(j->second.begin(), j->second.end(), *i)
         != j->second.end())
        {
        warning += "Library: ";
        warning += j->first;
        warning += "\n";
        }
      }
    }
  warning += "\n";
  return warning;
}