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
|
/*=========================================================================
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 "cmDependsC.h"
#include "cmSystemTools.h"
#include <ctype.h> // isspace
//----------------------------------------------------------------------------
cmDependsC::cmDependsC()
{
}
//----------------------------------------------------------------------------
// yummy look at all those constructor arguments
cmDependsC::cmDependsC(std::vector<std::string> const& includes,
const char* scanRegex, const char* complainRegex):
m_IncludePath(&includes),
m_IncludeRegexLine("^[ \t]*#[ \t]*include[ \t]*[<\"]([^\">]+)([\">])"),
m_IncludeRegexScan(scanRegex),
m_IncludeRegexComplain(complainRegex)
{
}
//----------------------------------------------------------------------------
cmDependsC::~cmDependsC()
{
}
//----------------------------------------------------------------------------
bool cmDependsC::WriteDependencies(const char *src,
const char *obj, std::ostream& os)
{
// Make sure this is a scanning instance.
if(!src || src[0] == '\0')
{
cmSystemTools::Error("Cannot scan dependencies without a source file.");
return false;
}
if(!obj || obj[0] == '\0')
{
cmSystemTools::Error("Cannot scan dependencies without an object file.");
return false;
}
if(!m_IncludePath)
{
cmSystemTools::Error("Cannot scan dependencies without an include path.");
return false;
}
// Walk the dependency graph starting with the source file.
bool first = true;
UnscannedEntry root;
root.FileName = src;
m_Unscanned.push(root);
m_Encountered.clear();
m_Encountered.insert(src);
std::set<cmStdString> dependencies;
std::set<cmStdString> scanned;
while(!m_Unscanned.empty())
{
// Get the next file to scan.
UnscannedEntry current = m_Unscanned.front();
m_Unscanned.pop();
// If not a full path, find the file in the include path.
std::string fullName;
if(first || cmSystemTools::FileIsFullPath(current.FileName.c_str()))
{
if(cmSystemTools::FileExists(current.FileName.c_str()))
{
fullName = current.FileName;
}
}
else if(!current.QuotedLocation.empty() &&
cmSystemTools::FileExists(current.QuotedLocation.c_str()))
{
// The include statement producing this entry was a double-quote
// include and the included file is present in the directory of
// the source containing the include statement.
fullName = current.QuotedLocation;
}
else
{
for(std::vector<std::string>::const_iterator i = m_IncludePath->begin();
i != m_IncludePath->end(); ++i)
{
// Construct the name of the file as if it were in the current
// include directory. Avoid using a leading "./".
std::string temp = *i;
if(temp == ".")
{
temp = "";
}
else
{
temp += "/";
}
temp += current.FileName;
// Look for the file in this location.
if(cmSystemTools::FileExists(temp.c_str()))
{
fullName = temp;
break;
}
}
}
// Complain if the file cannot be found and matches the complain
// regex.
if(fullName.empty() &&
m_IncludeRegexComplain.find(current.FileName.c_str()))
{
cmSystemTools::Error("Cannot find file \"",
current.FileName.c_str(), "\".");
return false;
}
// Scan the file if it was found and has not been scanned already.
if(!fullName.empty() && (scanned.find(fullName) == scanned.end()))
{
// Record scanned files.
scanned.insert(fullName);
// Try to scan the file. Just leave it out if we cannot find
// it.
std::ifstream fin(fullName.c_str());
if(fin)
{
// Add this file as a dependency.
dependencies.insert(fullName);
// Scan this file for new dependencies. Pass the directory
// containing the file to handle double-quote includes.
std::string dir = cmSystemTools::GetFilenamePath(fullName);
this->Scan(fin, dir.c_str());
}
}
first = false;
}
// Write the dependencies to the output stream.
for(std::set<cmStdString>::iterator i=dependencies.begin();
i != dependencies.end(); ++i)
{
os << obj << ": "
<< cmSystemTools::ConvertToOutputPath(i->c_str()).c_str()
<< std::endl;
}
os << std::endl;
return true;
}
//----------------------------------------------------------------------------
bool cmDependsC::CheckDependencies(std::istream& is)
{
// Parse dependencies from the stream. If any dependee is missing
// or newer than the depender then dependencies should be
// regenerated.
bool okay = true;
std::string line;
std::string depender;
std::string dependee;
while(cmSystemTools::GetLineFromStream(is, line))
{
// Parse the dependency line.
if(!this->ParseDependency(line.c_str(), depender, dependee))
{
continue;
}
// Dependencies must be regenerated if the dependee does not exist
// or if the depender exists and is older than the dependee.
bool regenerate = false;
if(!cmSystemTools::FileExists(dependee.c_str()))
{
// The dependee does not exist.
regenerate = true;
// Print verbose output.
if(m_Verbose)
{
cmOStringStream msg;
msg << "Dependee \"" << dependee
<< "\" does not exist for depender \""
<< depender << "\"." << std::endl;
cmSystemTools::Stdout(msg.str().c_str());
}
}
else if(cmSystemTools::FileExists(depender.c_str()))
{
// The dependee and depender both exist. Compare file times.
int result = 0;
if((!cmSystemTools::FileTimeCompare(depender.c_str(), dependee.c_str(),
&result) || result < 0))
{
// The depender is older than the dependee.
regenerate = true;
// Print verbose output.
if(m_Verbose)
{
cmOStringStream msg;
msg << "Dependee \"" << dependee
<< "\" is newer than depender \""
<< depender << "\"." << std::endl;
cmSystemTools::Stdout(msg.str().c_str());
}
}
}
if(regenerate)
{
// Dependencies must be regenerated.
okay = false;
// Remove the depender to be sure it is rebuilt.
cmSystemTools::RemoveFile(depender.c_str());
}
}
return okay;
}
//----------------------------------------------------------------------------
void cmDependsC::Scan(std::istream& is, const char* directory)
{
// Read one line at a time.
std::string line;
while(cmSystemTools::GetLineFromStream(is, line))
{
// Match include directives.
if(m_IncludeRegexLine.find(line.c_str()))
{
// Get the file being included.
UnscannedEntry entry;
entry.FileName = m_IncludeRegexLine.match(1);
if(m_IncludeRegexLine.match(2) == "\"")
{
// This was a double-quoted include. We must check for the
// file in the directory containing the file we are scanning.
entry.QuotedLocation = directory;
entry.QuotedLocation += "/";
entry.QuotedLocation += entry.FileName;
}
// Queue the file if it has not yet been encountered and it
// matches the regular expression for recursive scanning. Note
// that this check does not account for the possibility of two
// headers with the same name in different directories when one
// is included by double-quotes and the other by angle brackets.
// This kind of problem will be fixed when a more
// preprocessor-like implementation of this scanner is created.
if(m_Encountered.find(entry.FileName) == m_Encountered.end() &&
m_IncludeRegexScan.find(entry.FileName.c_str()))
{
m_Encountered.insert(entry.FileName);
m_Unscanned.push(entry);
}
}
}
}
//----------------------------------------------------------------------------
bool cmDependsC::ParseDependency(const char* line, std::string& depender,
std::string& dependee)
{
// Start with empty names.
depender = "";
dependee = "";
// Get the left-hand-side of the dependency.
const char* c = this->ParseFileName(line, depender);
// Skip the ':' separator.
for(;c && *c && isspace(*c);++c);
if(!c || !*c || *c != ':')
{
return false;
}
++c;
// Get the right-hand-side of the dependency.
return this->ParseFileName(c, dependee)?true:false;
}
//----------------------------------------------------------------------------
const char* cmDependsC::ParseFileName(const char* in, std::string& name)
{
// Skip leading whitespace.
const char* c = in;
for(;c && *c && isspace(*c);++c);
// If this is an empty line or a comment line return failure.
if(!c || !*c || *c == '#')
{
return 0;
}
// Parse the possibly quoted file name.
bool quoted = false;
char* buf = new char[strlen(in)+1];
char* pos = buf;
// for every character while we haven't hit the end of the string AND we
// are in a quoted string OR the current character isn't a : or the second
// character AND it isn't a space
for(;*c && (quoted ||
((*c != ':' || pos > buf) && !isspace(*c))); ++c)
{
if(*c == '"')
{
quoted = !quoted;
}
else if(!quoted && *c == '\\' && isspace(*(c+1)))
{
*pos = *(++c);
pos++;
}
else
{
*pos = *c;
pos++;
}
}
*pos =0;
name += pos;
delete [] buf;
// Return the ending position.
return c;
}
|