cmDependsC.cxx 16.4 KB
Newer Older
1
2
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file Copyright.txt or https://cmake.org/licensing for details.  */
3
4
#include "cmDependsC.h"

5
6
7
#include <cmsys/FStream.hxx>
#include <utility>

8
#include "cmAlgorithms.h"
9
#include "cmFileTimeComparison.h"
10
#include "cmLocalGenerator.h"
11
#include "cmMakefile.h"
12
#include "cmSystemTools.h"
13

14
#define INCLUDE_REGEX_LINE                                                    \
Brad King's avatar
Brad King committed
15
  "^[ \t]*#[ \t]*(include|import)[ \t]*[<\"]([^\">]+)([\">])"
16
17
18
19

#define INCLUDE_REGEX_LINE_MARKER "#IncludeRegexLine: "
#define INCLUDE_REGEX_SCAN_MARKER "#IncludeRegexScan: "
#define INCLUDE_REGEX_COMPLAIN_MARKER "#IncludeRegexComplain: "
20
#define INCLUDE_REGEX_TRANSFORM_MARKER "#IncludeRegexTransform: "
21

22
cmDependsC::cmDependsC()
Daniel Pfeifer's avatar
Daniel Pfeifer committed
23
  : ValidDeps(CM_NULLPTR)
24
25
{
}
26

27
28
29
30
31
cmDependsC::cmDependsC(
  cmLocalGenerator* lg, const char* targetDir, const std::string& lang,
  const std::map<std::string, DependencyVector>* validDeps)
  : cmDepends(lg, targetDir)
  , ValidDeps(validDeps)
32
{
33
34
35
36
37
38
39
40
41
  cmMakefile* mf = lg->GetMakefile();

  // Configure the include file search path.
  this->SetIncludePathFromLanguage(lang);

  // Configure regular expressions.
  std::string scanRegex = "^.*$";
  std::string complainRegex = "^$";
  {
42
43
44
45
46
    std::string scanRegexVar = "CMAKE_";
    scanRegexVar += lang;
    scanRegexVar += "_INCLUDE_REGEX_SCAN";
    if (const char* sr = mf->GetDefinition(scanRegexVar)) {
      scanRegex = sr;
47
    }
48
49
50
51
52
    std::string complainRegexVar = "CMAKE_";
    complainRegexVar += lang;
    complainRegexVar += "_INCLUDE_REGEX_COMPLAIN";
    if (const char* cr = mf->GetDefinition(complainRegexVar)) {
      complainRegex = cr;
53
54
55
56
57
58
59
60
61
62
63
64
    }
  }

  this->IncludeRegexLine.compile(INCLUDE_REGEX_LINE);
  this->IncludeRegexScan.compile(scanRegex.c_str());
  this->IncludeRegexComplain.compile(complainRegex.c_str());
  this->IncludeRegexLineString = INCLUDE_REGEX_LINE_MARKER INCLUDE_REGEX_LINE;
  this->IncludeRegexScanString = INCLUDE_REGEX_SCAN_MARKER;
  this->IncludeRegexScanString += scanRegex;
  this->IncludeRegexComplainString = INCLUDE_REGEX_COMPLAIN_MARKER;
  this->IncludeRegexComplainString += complainRegex;

65
66
  this->SetupTransforms();

67
68
69
70
71
  this->CacheFileName = this->TargetDirectory;
  this->CacheFileName += "/";
  this->CacheFileName += lang;
  this->CacheFileName += ".includecache";

72
  this->ReadCacheFile();
73
74
75
76
}

cmDependsC::~cmDependsC()
{
77
  this->WriteCacheFile();
78
  cmDeleteAll(this->FileCache);
79
80
}

81
82
83
84
bool cmDependsC::WriteDependencies(const std::set<std::string>& sources,
                                   const std::string& obj,
                                   std::ostream& makeDepends,
                                   std::ostream& internalDepends)
85
86
{
  // Make sure this is a scanning instance.
87
  if (sources.empty() || sources.begin()->empty()) {
88
89
    cmSystemTools::Error("Cannot scan dependencies without a source file.");
    return false;
90
91
  }
  if (obj.empty()) {
92
    cmSystemTools::Error("Cannot scan dependencies without an object file.");
93
    return false;
94
  }
95

96
  std::set<std::string> dependencies;
97
98
  bool haveDeps = false;

Daniel Pfeifer's avatar
Daniel Pfeifer committed
99
  if (this->ValidDeps != CM_NULLPTR) {
100
    std::map<std::string, DependencyVector>::const_iterator tmpIt =
101
102
      this->ValidDeps->find(obj);
    if (tmpIt != this->ValidDeps->end()) {
103
      dependencies.insert(tmpIt->second.begin(), tmpIt->second.end());
104
      haveDeps = true;
105
    }
106
  }
107

108
  if (!haveDeps) {
109
    // Walk the dependency graph starting with the source file.
110
    int srcFiles = (int)sources.size();
111
    this->Encountered.clear();
112

113
114
    for (std::set<std::string>::const_iterator srcIt = sources.begin();
         srcIt != sources.end(); ++srcIt) {
115
116
117
118
      UnscannedEntry root;
      root.FileName = *srcIt;
      this->Unscanned.push(root);
      this->Encountered.insert(*srcIt);
119
    }
120

121
    std::set<std::string> scanned;
122
123
124
125

    // Use reserve to allocate enough memory for tempPathStr
    // so that during the loops no memory is allocated or freed
    std::string tempPathStr;
126
    tempPathStr.reserve(4 * 1024);
127

128
    while (!this->Unscanned.empty()) {
129
130
131
132
133
134
      // Get the next file to scan.
      UnscannedEntry current = this->Unscanned.front();
      this->Unscanned.pop();

      // If not a full path, find the file in the include path.
      std::string fullName;
135
136
137
      if ((srcFiles > 0) ||
          cmSystemTools::FileIsFullPath(current.FileName.c_str())) {
        if (cmSystemTools::FileExists(current.FileName.c_str(), true)) {
138
          fullName = current.FileName;
139
        }
140
141
142
      } else if (!current.QuotedLocation.empty() &&
                 cmSystemTools::FileExists(current.QuotedLocation.c_str(),
                                           true)) {
143
144
145
146
        // 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;
147
148
149
150
151
      } else {
        std::map<std::string, std::string>::iterator headerLocationIt =
          this->HeaderLocationCache.find(current.FileName);
        if (headerLocationIt != this->HeaderLocationCache.end()) {
          fullName = headerLocationIt->second;
152
        } else {
153
154
155
156
157
158
159
160
161
162
163
164
165
166
          for (std::vector<std::string>::const_iterator i =
                 this->IncludePath.begin();
               i != this->IncludePath.end(); ++i) {
            // Construct the name of the file as if it were in the current
            // include directory.  Avoid using a leading "./".

            tempPathStr =
              cmSystemTools::CollapseCombinedPath(*i, current.FileName);

            // Look for the file in this location.
            if (cmSystemTools::FileExists(tempPathStr.c_str(), true)) {
              fullName = tempPathStr;
              HeaderLocationCache[current.FileName] = fullName;
              break;
167
            }
168
          }
169
        }
170
      }
171

172
173
      // Complain if the file cannot be found and matches the complain
      // regex.
174
175
176
177
      if (fullName.empty() &&
          this->IncludeRegexComplain.find(current.FileName.c_str())) {
        cmSystemTools::Error("Cannot find file \"", current.FileName.c_str(),
                             "\".");
178
        return false;
179
      }
180

181
      // Scan the file if it was found and has not been scanned already.
182
      if (!fullName.empty() && (scanned.find(fullName) == scanned.end())) {
183
184
185
186
        // Record scanned files.
        scanned.insert(fullName);

        // Check whether this file is already in the cache
187
        std::map<std::string, cmIncludeLines*>::iterator fileIt =
188
          this->FileCache.find(fullName);
189
190
        if (fileIt != this->FileCache.end()) {
          fileIt->second->Used = true;
191
          dependencies.insert(fullName);
192
193
194
          for (std::vector<UnscannedEntry>::const_iterator incIt =
                 fileIt->second->UnscannedEntries.begin();
               incIt != fileIt->second->UnscannedEntries.end(); ++incIt) {
195
            if (this->Encountered.find(incIt->FileName) ==
196
                this->Encountered.end()) {
197
198
              this->Encountered.insert(incIt->FileName);
              this->Unscanned.push(*incIt);
199
200
            }
          }
201
        } else {
Andy Cedilnik's avatar
Andy Cedilnik committed
202

203
204
          // Try to scan the file.  Just leave it out if we cannot find
          // it.
205
          cmsys::ifstream fin(fullName.c_str());
206
          if (fin) {
207
            cmsys::FStream::BOM bom = cmsys::FStream::ReadBOM(fin);
208
209
            if (bom == cmsys::FStream::BOM_None ||
                bom == cmsys::FStream::BOM_UTF8) {
210
211
              // Add this file as a dependency.
              dependencies.insert(fullName);
212

213
214
215
216
              // 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(), fullName);
217
            } else {
218
              // Skip file with encoding we do not implement.
219
            }
220
          }
221
        }
222
      }
Andy Cedilnik's avatar
Andy Cedilnik committed
223

224
      srcFiles--;
225
    }
226
  }
Andy Cedilnik's avatar
Andy Cedilnik committed
227

228
229
230
231
  // Write the dependencies to the output stream.  Makefile rules
  // written by the original local generator for this directory
  // convert the dependencies to paths relative to the home output
  // directory.  We must do the same here.
232
233
  std::string binDir = this->LocalGenerator->GetBinaryDirectory();
  std::string obj_i = this->LocalGenerator->ConvertToRelativePath(binDir, obj);
234
  std::string obj_m = cmSystemTools::ConvertToOutputPath(obj_i.c_str());
235
236
  internalDepends << obj_i << std::endl;

237
238
  for (std::set<std::string>::const_iterator i = dependencies.begin();
       i != dependencies.end(); ++i) {
239
240
241
242
243
    makeDepends
      << obj_m << ": "
      << cmSystemTools::ConvertToOutputPath(
           this->LocalGenerator->ConvertToRelativePath(binDir, *i).c_str())
      << std::endl;
244
    internalDepends << " " << *i << std::endl;
245
  }
246
  makeDepends << std::endl;
Andy Cedilnik's avatar
Andy Cedilnik committed
247

248
249
250
  return true;
}

251
void cmDependsC::ReadCacheFile()
252
{
253
  if (this->CacheFileName.empty()) {
254
    return;
255
  }
256
  cmsys::ifstream fin(this->CacheFileName.c_str());
257
  if (!fin) {
258
    return;
259
  }
Andy Cedilnik's avatar
Andy Cedilnik committed
260

261
  std::string line;
Daniel Pfeifer's avatar
Daniel Pfeifer committed
262
  cmIncludeLines* cacheEntry = CM_NULLPTR;
263
  bool haveFileName = false;
Andy Cedilnik's avatar
Andy Cedilnik committed
264

265
266
  while (cmSystemTools::GetLineFromStream(fin, line)) {
    if (line.empty()) {
Daniel Pfeifer's avatar
Daniel Pfeifer committed
267
      cacheEntry = CM_NULLPTR;
268
      haveFileName = false;
269
      continue;
270
271
    }
    // the first line after an empty line is the name of the parsed file
Daniel Pfeifer's avatar
Daniel Pfeifer committed
272
    if (!haveFileName) {
273
274
      haveFileName = true;
      int newer = 0;
275
      cmFileTimeComparison comp;
276
277
      bool res = comp.FileTimeCompare(this->CacheFileName.c_str(),
                                      line.c_str(), &newer);
278

Daniel Pfeifer's avatar
Daniel Pfeifer committed
279
      if (res && newer == 1) // cache is newer than the parsed file
280
281
282
283
      {
        cacheEntry = new cmIncludeLines;
        this->FileCache[line] = cacheEntry;
      }
Brad King's avatar
Brad King committed
284
285
      // file doesn't exist, check that the regular expressions
      // haven't changed
Daniel Pfeifer's avatar
Daniel Pfeifer committed
286
      else if (!res) {
287
288
        if (line.find(INCLUDE_REGEX_LINE_MARKER) == 0) {
          if (line != this->IncludeRegexLineString) {
289
290
            return;
          }
291
292
        } else if (line.find(INCLUDE_REGEX_SCAN_MARKER) == 0) {
          if (line != this->IncludeRegexScanString) {
293
294
            return;
          }
295
296
        } else if (line.find(INCLUDE_REGEX_COMPLAIN_MARKER) == 0) {
          if (line != this->IncludeRegexComplainString) {
297
298
            return;
          }
299
300
        } else if (line.find(INCLUDE_REGEX_TRANSFORM_MARKER) == 0) {
          if (line != this->IncludeRegexTransformString) {
301
302
            return;
          }
303
        }
304
      }
Daniel Pfeifer's avatar
Daniel Pfeifer committed
305
    } else if (cacheEntry != CM_NULLPTR) {
306
307
      UnscannedEntry entry;
      entry.FileName = line;
308
309
310
      if (cmSystemTools::GetLineFromStream(fin, line)) {
        if (line != "-") {
          entry.QuotedLocation = line;
311
        }
312
        cacheEntry->UnscannedEntries.push_back(entry);
313
314
      }
    }
315
  }
316
317
318
319
}

void cmDependsC::WriteCacheFile() const
{
320
  if (this->CacheFileName.empty()) {
321
    return;
322
  }
323
  cmsys::ofstream cacheOut(this->CacheFileName.c_str());
324
  if (!cacheOut) {
325
    return;
326
  }
327

328
329
330
  cacheOut << this->IncludeRegexLineString << "\n\n";
  cacheOut << this->IncludeRegexScanString << "\n\n";
  cacheOut << this->IncludeRegexComplainString << "\n\n";
331
  cacheOut << this->IncludeRegexTransformString << "\n\n";
332

333
  for (std::map<std::string, cmIncludeLines*>::const_iterator fileIt =
334
         this->FileCache.begin();
335
336
       fileIt != this->FileCache.end(); ++fileIt) {
    if (fileIt->second->Used) {
337
      cacheOut << fileIt->first << std::endl;
338
339
340
341

      for (std::vector<UnscannedEntry>::const_iterator incIt =
             fileIt->second->UnscannedEntries.begin();
           incIt != fileIt->second->UnscannedEntries.end(); ++incIt) {
342
        cacheOut << incIt->FileName << std::endl;
343
344
345
        if (incIt->QuotedLocation.empty()) {
          cacheOut << "-" << std::endl;
        } else {
346
          cacheOut << incIt->QuotedLocation << std::endl;
347
348
        }
      }
349
350
351
      cacheOut << std::endl;
    }
  }
352
353
}

Andy Cedilnik's avatar
Andy Cedilnik committed
354
void cmDependsC::Scan(std::istream& is, const char* directory,
355
                      const std::string& fullName)
356
{
357
358
359
  cmIncludeLines* newCacheEntry = new cmIncludeLines;
  newCacheEntry->Used = true;
  this->FileCache[fullName] = newCacheEntry;
360

361
362
  // Read one line at a time.
  std::string line;
363
  while (cmSystemTools::GetLineFromStream(is, line)) {
364
    // Transform the line content first.
365
    if (!this->TransformRules.empty()) {
366
      this->TransformLine(line);
367
    }
368

369
    // Match include directives.
370
    if (this->IncludeRegexLine.find(line.c_str())) {
371
      // Get the file being included.
372
      UnscannedEntry entry;
373
      entry.FileName = this->IncludeRegexLine.match(2);
374
      cmSystemTools::ConvertToUnixSlashes(entry.FileName);
375
376
      if (this->IncludeRegexLine.match(3) == "\"" &&
          !cmSystemTools::FileIsFullPath(entry.FileName.c_str())) {
377
378
379
        // This was a double-quoted include with a relative path.  We
        // must check for the file in the directory containing the
        // file we are scanning.
380
381
        entry.QuotedLocation =
          cmSystemTools::CollapseCombinedPath(directory, entry.FileName);
382
      }
383

384
      // Queue the file if it has not yet been encountered and it
385
386
387
388
      // 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.
389
390
391
      // It also does not work properly if two header files with the same
      // name exist in different directories, and both are included from a
      // file their own directory by simply using "filename.h" (#12619)
392
393
      // This kind of problem will be fixed when a more
      // preprocessor-like implementation of this scanner is created.
394
      if (this->IncludeRegexScan.find(entry.FileName.c_str())) {
Ken Martin's avatar
Ken Martin committed
395
        newCacheEntry->UnscannedEntries.push_back(entry);
396
397
        if (this->Encountered.find(entry.FileName) ==
            this->Encountered.end()) {
Ken Martin's avatar
Ken Martin committed
398
399
          this->Encountered.insert(entry.FileName);
          this->Unscanned.push(entry);
400
401
402
        }
      }
    }
403
  }
404
}
405
406
407
408
409
410

void cmDependsC::SetupTransforms()
{
  // Get the transformation rules.
  std::vector<std::string> transformRules;
  cmMakefile* mf = this->LocalGenerator->GetMakefile();
411
  if (const char* xform = mf->GetDefinition("CMAKE_INCLUDE_TRANSFORMS")) {
412
    cmSystemTools::ExpandListArgument(xform, transformRules, true);
413
414
415
  }
  for (std::vector<std::string>::const_iterator tri = transformRules.begin();
       tri != transformRules.end(); ++tri) {
416
    this->ParseTransform(*tri);
417
  }
418
419

  this->IncludeRegexTransformString = INCLUDE_REGEX_TRANSFORM_MARKER;
420
  if (!this->TransformRules.empty()) {
421
422
423
424
    // Construct the regular expression to match lines to be
    // transformed.
    std::string xform = "^([ \t]*#[ \t]*(include|import)[ \t]*)(";
    const char* sep = "";
425
426
    for (TransformRulesType::const_iterator tri = this->TransformRules.begin();
         tri != this->TransformRules.end(); ++tri) {
427
428
429
      xform += sep;
      xform += tri->first;
      sep = "|";
430
    }
431
432
433
434
435
436
    xform += ")[ \t]*\\(([^),]*)\\)";
    this->IncludeRegexTransform.compile(xform.c_str());

    // Build a string that encodes all transformation rules and will
    // change when rules are changed.
    this->IncludeRegexTransformString += xform;
437
438
    for (TransformRulesType::const_iterator tri = this->TransformRules.begin();
         tri != this->TransformRules.end(); ++tri) {
439
440
441
442
443
      this->IncludeRegexTransformString += " ";
      this->IncludeRegexTransformString += tri->first;
      this->IncludeRegexTransformString += "(%)=";
      this->IncludeRegexTransformString += tri->second;
    }
444
  }
445
446
447
448
449
450
451
}

void cmDependsC::ParseTransform(std::string const& xform)
{
  // A transform rule is of the form SOME_MACRO(%)=value-with-%
  // We can simply separate with "(%)=".
  std::string::size_type pos = xform.find("(%)=");
452
  if (pos == xform.npos || pos == 0) {
453
    return;
454
  }
455
  std::string name = xform.substr(0, pos);
456
  std::string value = xform.substr(pos + 4, xform.npos);
457
458
459
460
461
462
  this->TransformRules[name] = value;
}

void cmDependsC::TransformLine(std::string& line)
{
  // Check for a transform rule match.  Return if none.
463
  if (!this->IncludeRegexTransform.find(line.c_str())) {
464
    return;
465
  }
466
467
  TransformRulesType::const_iterator tri =
    this->TransformRules.find(this->IncludeRegexTransform.match(3));
468
  if (tri == this->TransformRules.end()) {
469
    return;
470
  }
471
472
473
474

  // Construct the transformed line.
  std::string newline = this->IncludeRegexTransform.match(1);
  std::string arg = this->IncludeRegexTransform.match(4);
475
476
  for (const char* c = tri->second.c_str(); *c; ++c) {
    if (*c == '%') {
477
      newline += arg;
478
    } else {
479
480
      newline += *c;
    }
481
  }
482
483
484
485

  // Return the transformed line.
  line = newline;
}