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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
|
/*=========================================================================
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 "cmLocalUnixMakefileGenerator2.h"
#include "cmGeneratedFileStream.h"
#include "cmGlobalGenerator.h"
#include "cmMakefile.h"
#include "cmSourceFile.h"
#include <queue>
//----------------------------------------------------------------------------
cmLocalUnixMakefileGenerator2::cmLocalUnixMakefileGenerator2()
{
}
//----------------------------------------------------------------------------
cmLocalUnixMakefileGenerator2::~cmLocalUnixMakefileGenerator2()
{
}
//----------------------------------------------------------------------------
void cmLocalUnixMakefileGenerator2::Generate(bool fromTheTop)
{
// TODO: Account for control-c during Makefile generation.
// Generate old style for now.
this->cmLocalUnixMakefileGenerator::Generate(fromTheTop);
// Generate the rule files for each target.
const cmTargets& targets = m_Makefile->GetTargets();
for(cmTargets::const_iterator t = targets.begin(); t != targets.end(); ++t)
{
this->GenerateTargetRuleFile(t->second);
}
// Generate the main makefile.
this->GenerateMakefile();
// Generate the cmake file that keeps the makefile up to date.
this->GenerateCMakefile();
}
//----------------------------------------------------------------------------
void cmLocalUnixMakefileGenerator2::GenerateMakefile()
{
std::string makefileName = m_Makefile->GetStartOutputDirectory();
makefileName += "/Makefile2";
std::string cmakefileName = makefileName;
cmakefileName += ".cmake";
// Open the output files.
std::ofstream makefileStream(makefileName.c_str());
if(!makefileStream)
{
cmSystemTools::Error("Error can not open for write: ",
makefileName.c_str());
cmSystemTools::ReportLastSystemError("");
return;
}
// Write the do not edit header.
this->WriteDisclaimer(makefileStream);
// Write some rules to make things look nice.
makefileStream
<< "# Disable some common implicit rules to speed things up.\n"
<< ".SUFFIXES:\n"
<< ".SUFFIXES:.hpuxmakemusthaverule\n\n";
// Write standard variables to the makefile.
this->OutputMakeVariables(makefileStream);
// Build command to run CMake to check if anything needs regenerating.
std::string runRule =
"@$(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)";
runRule += " --check-rerun ";
runRule += this->ConvertToRelativeOutputPath(cmakefileName.c_str());
// Most unix makes will pass the command line flags to make down to
// sub-invoked makes via an environment variable. However, some
// makes do not support that, so you have to pass the flags
// explicitly.
const char* depRule = "$(MAKE) -f Makefile2 $(MAKESILENT) all.depends";
const char* allRule = "$(MAKE) -f Makefile2 $(MAKESILENT) all";
if(m_PassMakeflags)
{
depRule = "$(MAKE) -f Makefile2 $(MAKESILENT) -$(MAKEFLAGS) all.depends";
allRule = "$(MAKE) -f Makefile2 $(MAKESILENT) -$(MAKEFLAGS) all";
}
// Write the main entry point target. This must be the VERY first
// target so that make with no arguments will run it.
{
std::vector<std::string> depends;
std::vector<std::string> commands;
commands.push_back(runRule);
commands.push_back(depRule);
commands.push_back(allRule);
this->OutputMakeRule(
makefileStream,
"Default target executed when no arguments are given to make.",
"default_target",
depends,
commands);
}
// Write special target to silence make output. This must be after
// the default target in case VERBOSE is set (which changes the name).
if(!m_Makefile->IsOn("CMAKE_VERBOSE_MAKEFILE"))
{
makefileStream
<< "# Suppress display of executed commands.\n"
<< "$(VERBOSE).SILENT:\n\n";
}
// Get the set of targets.
const cmTargets& targets = m_Makefile->GetTargets();
// Output top level dependency rule.
{
std::vector<std::string> depends;
std::vector<std::string> commands;
for(cmTargets::const_iterator t = targets.begin(); t != targets.end(); ++t)
{
if(t->second.IsInAll())
{
std::string dep = this->GetTargetDirectory(t->second);
dep += "/";
dep += t->first;
dep += ".depends";
depends.push_back(dep);
}
}
this->OutputMakeRule(makefileStream, "all dependencies", "all.depends",
depends, commands);
}
// Output top level build rule.
{
std::vector<std::string> depends;
std::vector<std::string> commands;
for(cmTargets::const_iterator t = targets.begin(); t != targets.end(); ++t)
{
if(t->second.IsInAll())
{
depends.push_back(t->first+".requires");
}
}
this->OutputMakeRule(makefileStream, "all", "all",
depends, commands);
}
// Write include statements to get rules for each target.
makefileStream
<< "# Include target rule files.\n";
for(cmTargets::const_iterator t = targets.begin(); t != targets.end(); ++t)
{
std::string ruleFileName = this->GetTargetDirectory(t->second);
ruleFileName += "/";
ruleFileName += t->first;
ruleFileName += ".make";
makefileStream
<< m_IncludeDirective << " "
<< this->ConvertToOutputForExisting(ruleFileName.c_str()).c_str()
<< "\n";
}
}
//----------------------------------------------------------------------------
void cmLocalUnixMakefileGenerator2::GenerateCMakefile()
{
std::string makefileName = m_Makefile->GetStartOutputDirectory();
makefileName += "/Makefile2";
std::string cmakefileName = makefileName;
cmakefileName += ".cmake";
// Open the output file.
std::ofstream cmakefileStream(cmakefileName.c_str());
if(!cmakefileStream)
{
cmSystemTools::Error("Error can not open for write: ",
cmakefileName.c_str());
cmSystemTools::ReportLastSystemError("");
return;
}
// Write the do not edit header.
this->WriteDisclaimer(cmakefileStream);
// Get the list of files contributing to this generation step.
// Sort the list and remove duplicates.
std::vector<std::string> lfiles = m_Makefile->GetListFiles();
std::sort(lfiles.begin(), lfiles.end(), std::less<std::string>());
std::vector<std::string>::iterator new_end = std::unique(lfiles.begin(),
lfiles.end());
lfiles.erase(new_end, lfiles.end());
// Save the list to the cmake file.
cmakefileStream
<< "# The corresponding makefile\n"
<< "# \"" << makefileName << "\"\n"
<< "# was generated from the following files:\n"
<< "SET(CMAKE_MAKEFILE_DEPENDS\n"
<< " \"" << m_Makefile->GetHomeOutputDirectory() << "/CMakeCache.txt\"\n";
for(std::vector<std::string>::const_iterator i = lfiles.begin();
i != lfiles.end(); ++i)
{
cmakefileStream
<< " \"" << i->c_str() << "\"\n";
}
cmakefileStream
<< " )\n\n";
// Set the corresponding makefile in the cmake file.
cmakefileStream
<< "# The corresponding makefile is:\n"
<< "SET(CMAKE_MAKEFILE_OUTPUTS\n"
<< " \"" << makefileName.c_str() << "\"\n"
<< " )\n";
}
//----------------------------------------------------------------------------
void
cmLocalUnixMakefileGenerator2
::GenerateTargetRuleFile(const cmTarget& target)
{
// Create a directory for this target.
std::string dir = this->GetTargetDirectory(target);
cmSystemTools::MakeDirectory(dir.c_str());
// First generate the object rule files. Save a list of all object
// files for this target.
std::vector<std::string> objects;
const std::vector<cmSourceFile*>& sources = target.GetSourceFiles();
for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
source != sources.end(); ++source)
{
if(!(*source)->GetPropertyAsBool("HEADER_FILE_ONLY") &&
!(*source)->GetCustomCommand())
{
// Generate this object file's rule file.
this->GenerateObjectRuleFile(target, *(*source));
// Save the object file full path.
std::string obj = dir;
obj += "/";
obj += this->GetObjectFileName(*(*source));
objects.push_back(obj);
}
}
// If there is no dependencies file, create an empty one.
std::string depFileName = dir;
depFileName += "/";
depFileName += target.GetName();
depFileName += ".depends.make";
if(!cmSystemTools::FileExists(depFileName.c_str()))
{
std::ofstream depFileStream(depFileName.c_str());
this->WriteDisclaimer(depFileStream);
depFileStream
<< "# Empty dependencies file for target " << target.GetName() << ".\n"
<< "# This may be replaced when dependencies are built.\n";
}
// Open the rule file. This should be copy-if-different because the
// rules may depend on this file itself.
std::string ruleFileName = dir;
ruleFileName += "/";
ruleFileName += target.GetName();
ruleFileName += ".make";
cmGeneratedFileStream ruleFile(ruleFileName.c_str());
std::ostream& ruleFileStream = ruleFile.GetStream();
if(!ruleFileStream)
{
// TODO: Produce error message that accounts for generated stream
// .tmp.
return;
}
this->WriteDisclaimer(ruleFileStream);
ruleFileStream
<< "# Rule file for target " << target.GetName() << ".\n\n";
// Include the dependencies for the target.
ruleFileStream
<< "# Include any dependencies generated for this rule.\n"
<< m_IncludeDirective << " "
<< this->ConvertToOutputForExisting(depFileName.c_str()).c_str()
<< "\n\n";
// Include the rule file for each object.
if(!objects.empty())
{
ruleFileStream
<< "# Include rules for object files.\n";
for(std::vector<std::string>::const_iterator obj = objects.begin();
obj != objects.end(); ++obj)
{
std::string objRuleFileName = *obj;
objRuleFileName += ".make";
ruleFileStream
<< m_IncludeDirective << " "
<< this->ConvertToOutputForExisting(objRuleFileName.c_str()).c_str()
<< "\n";
}
ruleFileStream
<< "\n";
}
// Write the dependency generation rule.
{
std::vector<std::string> depends;
std::vector<std::string> commands;
std::string depComment = "dependencies for ";
depComment += target.GetName();
std::string depTarget = dir;
depTarget += "/";
depTarget += target.GetName();
depTarget += ".depends";
for(std::vector<std::string>::const_iterator obj = objects.begin();
obj != objects.end(); ++obj)
{
depends.push_back((*obj)+".depends");
}
depends.push_back(ruleFileName);
std::string touchCmd = "@touch ";
touchCmd += this->ConvertToRelativeOutputPath(depTarget.c_str());
// TODO: Construct dependency generation rule and append command.
commands.push_back(touchCmd);
this->OutputMakeRule(ruleFileStream, depComment.c_str(), depTarget.c_str(),
depends, commands);
}
// Write the requires rule.
{
std::vector<std::string> depends;
std::vector<std::string> commands;
std::string reqComment = "requirements for ";
reqComment += target.GetName();
std::string reqTarget = target.GetName();
reqTarget += ".requires";
for(std::vector<std::string>::const_iterator obj = objects.begin();
obj != objects.end(); ++obj)
{
depends.push_back(*obj);
}
depends.push_back(ruleFileName);
this->OutputMakeRule(ruleFileStream, reqComment.c_str(), reqTarget.c_str(),
depends, commands);
}
#if 0
// Write the build rule.
{
std::vector<std::string> depends;
std::vector<std::string> commands;
std::string buildComment = " target ";
buildComment += objName;
for(std::vector<std::string>::const_iterator obj = objects.begin();
obj != objects.end(); ++obj)
{
depends.push_back(*obj);
}
depends.push_back(ruleFileName);
std::string touchCmd = "@touch ";
touchCmd += this->ConvertToRelativeOutputPath(obj.c_str());
// TODO: Construct build rule and append command.
commands.push_back(touchCmd);
this->OutputMakeRule(ruleFileStream, buildComment.c_str(), obj.c_str(),
depends, commands);
}
#endif
}
//----------------------------------------------------------------------------
void
cmLocalUnixMakefileGenerator2
::GenerateObjectRuleFile(const cmTarget& target, const cmSourceFile& source)
{
// Get the full path name of the object file.
std::string objName = this->GetObjectFileName(source);
std::string obj = this->GetTargetDirectory(target);
obj += "/";
obj += objName;
// Create the directory containing the object file. This may be a
// subdirectory under the target's directory.
std::string dir = cmSystemTools::GetFilenamePath(obj.c_str());
cmSystemTools::MakeDirectory(dir.c_str());
// If there is no dependencies file, create an empty one.
std::string depFileName = obj;
depFileName += ".depends.make";
if(!cmSystemTools::FileExists(depFileName.c_str()))
{
std::ofstream depFileStream(depFileName.c_str());
this->WriteDisclaimer(depFileStream);
depFileStream
<< "# Empty dependencies file for object file "
<< objName.c_str() << ".\n"
<< "# This may be replaced when dependencies are built.\n";
}
// Open the rule file for writing. This should be copy-if-different
// because the rules may depend on this file itself.
std::string ruleFileName = obj;
ruleFileName += ".make";
cmGeneratedFileStream ruleFile(ruleFileName.c_str());
std::ostream& ruleFileStream = ruleFile.GetStream();
if(!ruleFileStream)
{
// TODO: Produce error message that accounts for generated stream
// .tmp.
return;
}
this->WriteDisclaimer(ruleFileStream);
ruleFileStream
<< "# Rule file for object file " << objName.c_str() << ".\n\n";
// Include the dependencies for the target.
ruleFileStream
<< "# Include any dependencies generated for this rule.\n"
<< m_IncludeDirective << " "
<< this->ConvertToOutputForExisting(depFileName.c_str()).c_str()
<< "\n\n";
// Identify the language of the source file.
const char* lang =
m_GlobalGenerator->GetLanguageFromExtension(source.GetSourceExtension().c_str());
// Write the dependency generation rule.
std::string depTarget = obj;
depTarget += ".depends";
{
std::vector<std::string> depends;
std::vector<std::string> commands;
std::string depComment = "dependencies for ";
depComment += objName;
depends.push_back(source.GetFullPath());
depends.push_back(ruleFileName);
cmOStringStream depCmd;
// TODO: Account for source file properties and directory-level
// definitions.
depCmd << "$(CMAKE_COMMAND) -E cmake_depends " << lang << " "
<< this->ConvertToRelativeOutputPath(obj.c_str()) << " "
<< this->ConvertToRelativeOutputPath(source.GetFullPath().c_str());
std::vector<std::string> includeDirs;
this->GetIncludeDirectories(includeDirs);
for(std::vector<std::string>::iterator i = includeDirs.begin();
i != includeDirs.end(); ++i)
{
depCmd << " -I" << this->ConvertToRelativeOutputPath(i->c_str());
}
commands.push_back(depCmd.str());
std::string touchCmd = "@touch ";
touchCmd += this->ConvertToRelativeOutputPath(depTarget.c_str());
commands.push_back(touchCmd);
this->OutputMakeRule(ruleFileStream, depComment.c_str(), depTarget.c_str(),
depends, commands);
}
// Write the build rule.
{
std::vector<std::string> depends;
std::vector<std::string> commands;
std::string buildComment = "object ";
buildComment += objName;
depends.push_back(source.GetFullPath());
depends.push_back(ruleFileName);
std::string touchCmd = "@touch ";
touchCmd += this->ConvertToRelativeOutputPath(obj.c_str());
// TODO: Construct build rule and append command.
commands.push_back(touchCmd);
this->OutputMakeRule(ruleFileStream, buildComment.c_str(), obj.c_str(),
depends, commands);
}
}
//----------------------------------------------------------------------------
void cmLocalUnixMakefileGenerator2::WriteDisclaimer(std::ostream& os)
{
os
<< "# CMAKE generated file: DO NOT EDIT!\n"
<< "# Generated by \"" << m_GlobalGenerator->GetName() << "\""
<< " Generator, CMake Version "
<< cmMakefile::GetMajorVersion() << "."
<< cmMakefile::GetMinorVersion() << "\n\n";
}
//----------------------------------------------------------------------------
std::string
cmLocalUnixMakefileGenerator2
::GetTargetDirectory(const cmTarget& target)
{
std::string dir = m_Makefile->GetStartOutputDirectory();
dir += "/";
dir += target.GetName();
dir += ".dir";
return dir;
}
//----------------------------------------------------------------------------
std::string
cmLocalUnixMakefileGenerator2
::GetObjectFileName(const cmSourceFile& source)
{
// If the full path to the source file includes this directory,
// we want to use the relative path for the filename of the
// object file. Otherwise, we will use just the filename
// portion.
std::string objectName;
if((cmSystemTools::GetFilenamePath(
source.GetFullPath()).find(
m_Makefile->GetCurrentDirectory()) == 0)
|| (cmSystemTools::GetFilenamePath(
source.GetFullPath()).find(
m_Makefile->GetCurrentOutputDirectory()) == 0))
{
objectName = source.GetSourceName();
}
else
{
objectName = cmSystemTools::GetFilenameName(source.GetSourceName());
}
// Append the object file extension.
objectName +=
m_GlobalGenerator->GetLanguageOutputExtensionFromExtension(
source.GetSourceExtension().c_str());
return objectName;
}
//----------------------------------------------------------------------------
bool
cmLocalUnixMakefileGenerator2
::ScanDependencies(std::vector<std::string> const& args)
{
// Format of arguments is:
// $(CMAKE_COMMAND), cmake_depends, <lang>, <obj>, <src>, [include-flags]
// The caller has ensured that all required arguments exist.
// The file to which to write dependencies.
const char* objFile = args[3].c_str();
// The source file at which to start the scan.
const char* srcFile = args[4].c_str();
// Convert the include flags to full paths.
std::vector<std::string> includes;
for(unsigned int i=5; i < args.size(); ++i)
{
if(args[i].substr(0, 2) == "-I")
{
// Get the include path without the -I flag.
std::string inc = args[i].substr(2);
includes.push_back(cmSystemTools::CollapseFullPath(inc.c_str()));
}
}
// Dispatch the scan for each language.
std::string const& lang = args[2];
if(lang == "C" || lang == "CXX")
{
return cmLocalUnixMakefileGenerator2::ScanDependenciesC(objFile, srcFile,
includes);
}
return false;
}
//----------------------------------------------------------------------------
void
cmLocalUnixMakefileGenerator2ScanDependenciesC(
std::ifstream& fin,
std::set<cmStdString>& encountered,
std::queue<cmStdString>& unscanned)
{
// Regular expression to identify C preprocessor include directives.
cmsys::RegularExpression
includeLine("^[ \t]*#[ \t]*include[ \t]*[<\"]([^\">]+)[\">]");
// Read one line at a time.
std::string line;
while(cmSystemTools::GetLineFromStream(fin, line))
{
// Match include directives.
if(includeLine.find(line.c_str()))
{
// Get the file being included.
std::string includeFile = includeLine.match(1);
// Queue the file if it has not yet been encountered.
if(encountered.find(includeFile) == encountered.end())
{
encountered.insert(includeFile);
unscanned.push(includeFile);
}
}
}
}
//----------------------------------------------------------------------------
bool
cmLocalUnixMakefileGenerator2
::ScanDependenciesC(const char* objFile, const char* srcFile,
std::vector<std::string> const& includes)
{
// Walk the dependency graph starting with the source file.
std::set<cmStdString> dependencies;
std::set<cmStdString> encountered;
std::set<cmStdString> scanned;
std::queue<cmStdString> unscanned;
unscanned.push(srcFile);
encountered.insert(srcFile);
while(!unscanned.empty())
{
// Get the next file to scan.
std::string fname = unscanned.front();
unscanned.pop();
// If not a full path, find the file in the include path.
std::string fullName;
if(cmSystemTools::FileIsFullPath(fname.c_str()))
{
fullName = fname;
}
else
{
for(std::vector<std::string>::const_iterator i = includes.begin();
i != includes.end(); ++i)
{
std::string temp = *i;
temp += "/";
temp += fname;
if(cmSystemTools::FileExists(temp.c_str()))
{
fullName = temp;
break;
}
}
}
// Scan the file if it has not been scanned already.
if(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.
cmLocalUnixMakefileGenerator2ScanDependenciesC(fin, encountered,
unscanned);
}
}
}
// Write the dependencies to the output file.
std::string depMakeFile = objFile;
depMakeFile += ".depends.make";
std::ofstream fout(depMakeFile.c_str());
fout << "# Dependencies for " << objFile << std::endl;
for(std::set<cmStdString>::iterator i=dependencies.begin();
i != dependencies.end(); ++i)
{
fout << objFile << " : " << i->c_str() << std::endl;
}
fout << std::endl;
fout << "# Dependencies for " << objFile << ".depends" << std::endl;
for(std::set<cmStdString>::iterator i=dependencies.begin();
i != dependencies.end(); ++i)
{
fout << objFile << ".depends : " << i->c_str() << std::endl;
}
return true;
}
|