forked from Kitware/CMake
/
cmMakefile.cxx
4242 lines (3873 loc) · 129 KB
/
cmMakefile.cxx
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
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*============================================================================
CMake - Cross Platform Makefile Generator
Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
Distributed under the OSI-approved BSD License (the "License");
see accompanying file Copyright.txt for details.
This software is distributed WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the License for more information.
============================================================================*/
#include "cmMakefile.h"
#include "cmVersion.h"
#include "cmCommand.h"
#include "cmSourceFile.h"
#include "cmSourceFileLocation.h"
#include "cmSystemTools.h"
#include "cmGlobalGenerator.h"
#include "cmLocalGenerator.h"
#include "cmCommands.h"
#include "cmCacheManager.h"
#include "cmFunctionBlocker.h"
#include "cmListFileCache.h"
#include "cmCommandArgumentParserHelper.h"
#include "cmDocumentCompileDefinitions.h"
#include "cmTest.h"
#ifdef CMAKE_BUILD_WITH_CMAKE
# include "cmVariableWatch.h"
#endif
#include "cmInstallGenerator.h"
#include "cmTestGenerator.h"
#include "cmDefinitions.h"
#include "cmake.h"
#include <stdlib.h> // required for atoi
#include <cmsys/RegularExpression.hxx>
#include <cmsys/auto_ptr.hxx>
#include <stack>
#include <ctype.h> // for isspace
class cmMakefile::Internals
{
public:
std::stack<cmDefinitions, std::list<cmDefinitions> > VarStack;
std::stack<std::set<cmStdString> > VarInitStack;
std::stack<std::set<cmStdString> > VarUsageStack;
};
// default is not to be building executables
cmMakefile::cmMakefile(): Internal(new Internals)
{
const cmDefinitions& defs = cmDefinitions();
const std::set<cmStdString> globalKeys = defs.LocalKeys();
this->Internal->VarStack.push(defs);
this->Internal->VarInitStack.push(globalKeys);
this->Internal->VarUsageStack.push(globalKeys);
// Initialize these first since AddDefaultDefinitions calls AddDefinition
this->WarnUnused = false;
this->CheckSystemVars = false;
// Setup the default include file regular expression (match everything).
this->IncludeFileRegularExpression = "^.*$";
// Setup the default include complaint regular expression (match nothing).
this->ComplainFileRegularExpression = "^$";
// Source and header file extensions that we can handle
// Set up a list of source and header extensions
// these are used to find files when the extension
// is not given
// The "c" extension MUST precede the "C" extension.
this->SourceFileExtensions.push_back( "c" );
this->SourceFileExtensions.push_back( "C" );
this->SourceFileExtensions.push_back( "c++" );
this->SourceFileExtensions.push_back( "cc" );
this->SourceFileExtensions.push_back( "cpp" );
this->SourceFileExtensions.push_back( "cxx" );
this->SourceFileExtensions.push_back( "m" );
this->SourceFileExtensions.push_back( "M" );
this->SourceFileExtensions.push_back( "mm" );
this->HeaderFileExtensions.push_back( "h" );
this->HeaderFileExtensions.push_back( "hh" );
this->HeaderFileExtensions.push_back( "h++" );
this->HeaderFileExtensions.push_back( "hm" );
this->HeaderFileExtensions.push_back( "hpp" );
this->HeaderFileExtensions.push_back( "hxx" );
this->HeaderFileExtensions.push_back( "in" );
this->HeaderFileExtensions.push_back( "txx" );
this->DefineFlags = " ";
this->LocalGenerator = 0;
this->AddDefaultDefinitions();
this->Initialize();
this->PreOrder = false;
}
cmMakefile::cmMakefile(const cmMakefile& mf): Internal(new Internals)
{
this->Internal->VarStack.push(mf.Internal->VarStack.top().Closure());
this->Internal->VarInitStack.push(mf.Internal->VarInitStack.top());
this->Internal->VarUsageStack.push(mf.Internal->VarUsageStack.top());
this->Prefix = mf.Prefix;
this->AuxSourceDirectories = mf.AuxSourceDirectories;
this->cmStartDirectory = mf.cmStartDirectory;
this->StartOutputDirectory = mf.StartOutputDirectory;
this->cmHomeDirectory = mf.cmHomeDirectory;
this->HomeOutputDirectory = mf.HomeOutputDirectory;
this->cmCurrentListFile = mf.cmCurrentListFile;
this->ProjectName = mf.ProjectName;
this->Targets = mf.Targets;
this->SourceFiles = mf.SourceFiles;
this->Tests = mf.Tests;
this->IncludeDirectories = mf.IncludeDirectories;
this->LinkDirectories = mf.LinkDirectories;
this->SystemIncludeDirectories = mf.SystemIncludeDirectories;
this->ListFiles = mf.ListFiles;
this->OutputFiles = mf.OutputFiles;
this->LinkLibraries = mf.LinkLibraries;
this->InstallGenerators = mf.InstallGenerators;
this->TestGenerators = mf.TestGenerators;
this->IncludeFileRegularExpression = mf.IncludeFileRegularExpression;
this->ComplainFileRegularExpression = mf.ComplainFileRegularExpression;
this->SourceFileExtensions = mf.SourceFileExtensions;
this->HeaderFileExtensions = mf.HeaderFileExtensions;
this->DefineFlags = mf.DefineFlags;
this->DefineFlagsOrig = mf.DefineFlagsOrig;
#if defined(CMAKE_BUILD_WITH_CMAKE)
this->SourceGroups = mf.SourceGroups;
#endif
this->LocalGenerator = mf.LocalGenerator;
this->FunctionBlockers = mf.FunctionBlockers;
this->MacrosMap = mf.MacrosMap;
this->SubDirectoryOrder = mf.SubDirectoryOrder;
this->Properties = mf.Properties;
this->PreOrder = mf.PreOrder;
this->WarnUnused = mf.WarnUnused;
this->Initialize();
this->CheckSystemVars = mf.CheckSystemVars;
this->ListFileStack = mf.ListFileStack;
}
//----------------------------------------------------------------------------
void cmMakefile::Initialize()
{
this->cmDefineRegex.compile("#cmakedefine[ \t]+([A-Za-z_0-9]*)");
this->cmDefine01Regex.compile("#cmakedefine01[ \t]+([A-Za-z_0-9]*)");
this->cmAtVarRegex.compile("(@[A-Za-z_0-9/.+-]+@)");
// Enter a policy level for this directory.
this->PushPolicy();
// Protect the directory-level policies.
this->PushPolicyBarrier();
// By default the check is not done. It is enabled by
// cmListFileCache in the top level if necessary.
this->CheckCMP0000 = false;
}
unsigned int cmMakefile::GetCacheMajorVersion()
{
return this->GetCacheManager()->GetCacheMajorVersion();
}
unsigned int cmMakefile::GetCacheMinorVersion()
{
return this->GetCacheManager()->GetCacheMinorVersion();
}
bool cmMakefile::NeedCacheCompatibility(int major, int minor)
{
return this->GetCacheManager()->NeedCacheCompatibility(major, minor);
}
cmMakefile::~cmMakefile()
{
for(std::vector<cmInstallGenerator*>::iterator
i = this->InstallGenerators.begin();
i != this->InstallGenerators.end(); ++i)
{
delete *i;
}
for(std::vector<cmTestGenerator*>::iterator
i = this->TestGenerators.begin();
i != this->TestGenerators.end(); ++i)
{
delete *i;
}
for(std::vector<cmSourceFile*>::iterator i = this->SourceFiles.begin();
i != this->SourceFiles.end(); ++i)
{
delete *i;
}
for(std::map<cmStdString, cmTest*>::iterator i = this->Tests.begin();
i != this->Tests.end(); ++i)
{
delete i->second;
}
for(std::vector<cmTarget*>::iterator
i = this->ImportedTargetsOwned.begin();
i != this->ImportedTargetsOwned.end(); ++i)
{
delete *i;
}
for(unsigned int i=0; i < this->UsedCommands.size(); i++)
{
delete this->UsedCommands[i];
}
std::vector<cmFunctionBlocker*>::iterator pos;
for (pos = this->FunctionBlockers.begin();
pos != this->FunctionBlockers.end(); ++pos)
{
cmFunctionBlocker* b = *pos;
delete b;
}
this->FunctionBlockers.clear();
if (this->PolicyStack.size() != 1)
{
cmSystemTools::Error("Internal CMake Error, Policy Stack has not been"
" popped properly");
}
}
void cmMakefile::PrintStringVector(const char* s,
const std::vector<std::string>& v) const
{
std::cout << s << ": ( \n";
for(std::vector<std::string>::const_iterator i = v.begin();
i != v.end(); ++i)
{
std::cout << (*i).c_str() << " ";
}
std::cout << " )\n";
}
void cmMakefile
::PrintStringVector(const char* s,
const std::vector<std::pair<cmStdString, bool> >& v) const
{
std::cout << s << ": ( \n";
for(std::vector<std::pair<cmStdString, bool> >::const_iterator i
= v.begin(); i != v.end(); ++i)
{
std::cout << i->first.c_str() << " " << i->second;
}
std::cout << " )\n";
}
// call print on all the classes in the makefile
void cmMakefile::Print()
{
// print the class lists
std::cout << "classes:\n";
std::cout << " this->Targets: ";
for (cmTargets::iterator l = this->Targets.begin();
l != this->Targets.end(); l++)
{
std::cout << l->first << std::endl;
}
std::cout << " this->StartOutputDirectory; " <<
this->StartOutputDirectory.c_str() << std::endl;
std::cout << " this->HomeOutputDirectory; " <<
this->HomeOutputDirectory.c_str() << std::endl;
std::cout << " this->cmStartDirectory; " <<
this->cmStartDirectory.c_str() << std::endl;
std::cout << " this->cmHomeDirectory; " <<
this->cmHomeDirectory.c_str() << std::endl;
std::cout << " this->ProjectName; "
<< this->ProjectName.c_str() << std::endl;
this->PrintStringVector("this->IncludeDirectories;",
this->IncludeDirectories);
this->PrintStringVector("this->LinkDirectories", this->LinkDirectories);
#if defined(CMAKE_BUILD_WITH_CMAKE)
for( std::vector<cmSourceGroup>::const_iterator i =
this->SourceGroups.begin(); i != this->SourceGroups.end(); ++i)
{
std::cout << "Source Group: " << i->GetName() << std::endl;
}
#endif
}
bool cmMakefile::CommandExists(const char* name) const
{
return this->GetCMakeInstance()->CommandExists(name);
}
//----------------------------------------------------------------------------
void cmMakefile::IssueMessage(cmake::MessageType t,
std::string const& text) const
{
// Collect context information.
cmListFileBacktrace backtrace;
if(!this->CallStack.empty())
{
if((t == cmake::FATAL_ERROR) || (t == cmake::INTERNAL_ERROR))
{
this->CallStack.back().Status->SetNestedError(true);
}
this->GetBacktrace(backtrace);
}
else
{
cmListFileContext lfc;
if(this->ListFileStack.empty())
{
// We are not processing the project. Add the directory-level context.
lfc.FilePath = this->GetCurrentDirectory();
lfc.FilePath += "/CMakeLists.txt";
}
else
{
// We are processing the project but are not currently executing a
// command. Add whatever context information we have.
lfc.FilePath = this->ListFileStack.back();
}
lfc.Line = 0;
if(!this->GetCMakeInstance()->GetIsInTryCompile())
{
lfc.FilePath = this->LocalGenerator->Convert(lfc.FilePath.c_str(),
cmLocalGenerator::HOME);
}
backtrace.push_back(lfc);
}
// Issue the message.
this->GetCMakeInstance()->IssueMessage(t, text, backtrace);
}
//----------------------------------------------------------------------------
bool cmMakefile::GetBacktrace(cmListFileBacktrace& backtrace) const
{
if(this->CallStack.empty())
{
return false;
}
for(CallStackType::const_reverse_iterator i = this->CallStack.rbegin();
i != this->CallStack.rend(); ++i)
{
cmListFileContext lfc = *(*i).Context;
lfc.FilePath = this->LocalGenerator->Convert(lfc.FilePath.c_str(),
cmLocalGenerator::HOME);
backtrace.push_back(lfc);
}
return true;
}
//----------------------------------------------------------------------------
bool cmMakefile::ExecuteCommand(const cmListFileFunction& lff,
cmExecutionStatus &status)
{
bool result = true;
// quick return if blocked
if(this->IsFunctionBlocked(lff,status))
{
// No error.
return result;
}
std::string name = lff.Name;
// Place this call on the call stack.
cmMakefileCall stack_manager(this, lff, status);
static_cast<void>(stack_manager);
// Lookup the command prototype.
if(cmCommand* proto = this->GetCMakeInstance()->GetCommand(name.c_str()))
{
// Clone the prototype.
cmsys::auto_ptr<cmCommand> pcmd(proto->Clone());
pcmd->SetMakefile(this);
// Decide whether to invoke the command.
if(pcmd->GetEnabled() && !cmSystemTools::GetFatalErrorOccured() &&
(!this->GetCMakeInstance()->GetScriptMode() || pcmd->IsScriptable()))
{
// if trace is one, print out invoke information
if(this->GetCMakeInstance()->GetTrace())
{
cmOStringStream msg;
msg << lff.FilePath << "(" << lff.Line << "): ";
msg << lff.Name << "(";
for(std::vector<cmListFileArgument>::const_iterator i =
lff.Arguments.begin(); i != lff.Arguments.end(); ++i)
{
msg << i->Value;
msg << " ";
}
msg << ")";
cmSystemTools::Message(msg.str().c_str());
}
// Try invoking the command.
if(!pcmd->InvokeInitialPass(lff.Arguments,status) ||
status.GetNestedError())
{
if(!status.GetNestedError())
{
// The command invocation requested that we report an error.
this->IssueMessage(cmake::FATAL_ERROR, pcmd->GetError());
}
result = false;
if ( this->GetCMakeInstance()->GetScriptMode() )
{
cmSystemTools::SetFatalErrorOccured();
}
}
else if(pcmd->HasFinalPass())
{
// use the command
this->UsedCommands.push_back(pcmd.release());
}
}
else if ( this->GetCMakeInstance()->GetScriptMode()
&& !pcmd->IsScriptable() )
{
std::string error = "Command ";
error += pcmd->GetName();
error += "() is not scriptable";
this->IssueMessage(cmake::FATAL_ERROR, error);
result = false;
cmSystemTools::SetFatalErrorOccured();
}
}
else
{
if(!cmSystemTools::GetFatalErrorOccured())
{
std::string error = "Unknown CMake command \"";
error += lff.Name;
error += "\".";
this->IssueMessage(cmake::FATAL_ERROR, error);
result = false;
cmSystemTools::SetFatalErrorOccured();
}
}
return result;
}
//----------------------------------------------------------------------------
class cmMakefile::IncludeScope
{
public:
IncludeScope(cmMakefile* mf, const char* fname, bool noPolicyScope);
~IncludeScope();
void Quiet() { this->ReportError = false; }
private:
cmMakefile* Makefile;
const char* File;
bool NoPolicyScope;
bool CheckCMP0011;
bool ReportError;
void EnforceCMP0011();
};
//----------------------------------------------------------------------------
cmMakefile::IncludeScope::IncludeScope(cmMakefile* mf, const char* fname,
bool noPolicyScope):
Makefile(mf), File(fname), NoPolicyScope(noPolicyScope),
CheckCMP0011(false), ReportError(true)
{
if(!this->NoPolicyScope)
{
// Check CMP0011 to determine the policy scope type.
switch (this->Makefile->GetPolicyStatus(cmPolicies::CMP0011))
{
case cmPolicies::WARN:
// We need to push a scope to detect whether the script sets
// any policies that would affect the includer and therefore
// requires a warning. We use a weak scope to simulate OLD
// behavior by allowing policy changes to affect the includer.
this->Makefile->PushPolicy(true);
this->CheckCMP0011 = true;
break;
case cmPolicies::OLD:
// OLD behavior is to not push a scope at all.
this->NoPolicyScope = true;
break;
case cmPolicies::REQUIRED_IF_USED:
case cmPolicies::REQUIRED_ALWAYS:
// We should never make this policy required, but we handle it
// here just in case.
this->CheckCMP0011 = true;
case cmPolicies::NEW:
// NEW behavior is to push a (strong) scope.
this->Makefile->PushPolicy();
break;
}
}
// The included file cannot pop our policy scope.
this->Makefile->PushPolicyBarrier();
}
//----------------------------------------------------------------------------
cmMakefile::IncludeScope::~IncludeScope()
{
// Enforce matching policy scopes inside the included file.
this->Makefile->PopPolicyBarrier(this->ReportError);
if(!this->NoPolicyScope)
{
// If we need to enforce policy CMP0011 then the top entry is the
// one we pushed above. If the entry is empty, then the included
// script did not set any policies that might affect the includer so
// we do not need to enforce the policy.
if(this->CheckCMP0011 && this->Makefile->PolicyStack.back().empty())
{
this->CheckCMP0011 = false;
}
// Pop the scope we pushed for the script.
this->Makefile->PopPolicy();
// We enforce the policy after the script's policy stack entry has
// been removed.
if(this->CheckCMP0011)
{
this->EnforceCMP0011();
}
}
}
//----------------------------------------------------------------------------
void cmMakefile::IncludeScope::EnforceCMP0011()
{
// We check the setting of this policy again because the included
// script might actually set this policy for its includer.
cmPolicies* policies = this->Makefile->GetPolicies();
switch (this->Makefile->GetPolicyStatus(cmPolicies::CMP0011))
{
case cmPolicies::WARN:
// Warn because the user did not set this policy.
{
cmOStringStream w;
w << policies->GetPolicyWarning(cmPolicies::CMP0011) << "\n"
<< "The included script\n " << this->File << "\n"
<< "affects policy settings. "
<< "CMake is implying the NO_POLICY_SCOPE option for compatibility, "
<< "so the effects are applied to the including context.";
this->Makefile->IssueMessage(cmake::AUTHOR_WARNING, w.str());
}
break;
case cmPolicies::REQUIRED_IF_USED:
case cmPolicies::REQUIRED_ALWAYS:
{
cmOStringStream e;
e << policies->GetRequiredPolicyError(cmPolicies::CMP0011) << "\n"
<< "The included script\n " << this->File << "\n"
<< "affects policy settings, so it requires this policy to be set.";
this->Makefile->IssueMessage(cmake::FATAL_ERROR, e.str());
}
break;
case cmPolicies::OLD:
case cmPolicies::NEW:
// The script set this policy. We assume the purpose of the
// script is to initialize policies for its includer, and since
// the policy is now set for later scripts, we do not warn.
break;
}
}
//----------------------------------------------------------------------------
// Parse the given CMakeLists.txt file executing all commands
//
bool cmMakefile::ReadListFile(const char* filename_in,
const char *external_in,
std::string* fullPath,
bool noPolicyScope)
{
std::string currentParentFile
= this->GetSafeDefinition("CMAKE_PARENT_LIST_FILE");
std::string currentFile
= this->GetSafeDefinition("CMAKE_CURRENT_LIST_FILE");
this->AddDefinition("CMAKE_PARENT_LIST_FILE", filename_in);
this->MarkVariableAsUsed("CMAKE_PARENT_LIST_FILE");
const char* external = 0;
std::string external_abs;
const char* filename = filename_in;
std::string filename_abs;
if (external_in)
{
external_abs =
cmSystemTools::CollapseFullPath(external_in,
this->cmStartDirectory.c_str());
external = external_abs.c_str();
if (filename_in)
{
filename_abs =
cmSystemTools::CollapseFullPath(filename_in,
this->cmStartDirectory.c_str());
filename = filename_abs.c_str();
}
}
// keep track of the current file being read
if (filename)
{
if(this->cmCurrentListFile != filename)
{
this->cmCurrentListFile = filename;
}
}
// Now read the input file
const char *filenametoread= filename;
if( external)
{
filenametoread= external;
}
this->AddDefinition("CMAKE_CURRENT_LIST_FILE", filenametoread);
this->MarkVariableAsUsed("CMAKE_CURRENT_LIST_FILE");
this->AddDefinition("CMAKE_CURRENT_LIST_DIR",
cmSystemTools::GetFilenamePath(filenametoread).c_str());
this->MarkVariableAsUsed("CMAKE_CURRENT_LIST_DIR");
// try to see if the list file is the top most
// list file for a project, and if it is, then it
// must have a project command. If there is not
// one, then cmake will provide one via the
// cmListFileCache class.
bool requireProjectCommand = false;
if(!external && this->cmStartDirectory == this->cmHomeDirectory)
{
if(cmSystemTools::LowerCase(
cmSystemTools::GetFilenameName(filename)) == "cmakelists.txt")
{
requireProjectCommand = true;
}
}
// push the listfile onto the stack
this->ListFileStack.push_back(filenametoread);
if(fullPath!=0)
{
*fullPath=filenametoread;
}
cmListFile cacheFile;
if( !cacheFile.ParseFile(filenametoread, requireProjectCommand, this) )
{
// pop the listfile off the stack
this->ListFileStack.pop_back();
if(fullPath!=0)
{
*fullPath = "";
}
this->AddDefinition("CMAKE_PARENT_LIST_FILE", currentParentFile.c_str());
this->MarkVariableAsUsed("CMAKE_PARENT_LIST_FILE");
this->AddDefinition("CMAKE_CURRENT_LIST_FILE", currentFile.c_str());
this->MarkVariableAsUsed("CMAKE_CURRENT_LIST_FILE");
this->AddDefinition("CMAKE_CURRENT_LIST_DIR",
cmSystemTools::GetFilenamePath(currentFile).c_str());
this->MarkVariableAsUsed("CMAKE_CURRENT_LIST_DIR");
return false;
}
// add this list file to the list of dependencies
this->ListFiles.push_back( filenametoread);
// Enforce balanced blocks (if/endif, function/endfunction, etc.).
{
LexicalPushPop lexScope(this);
IncludeScope incScope(this, filenametoread, noPolicyScope);
// Run the parsed commands.
const size_t numberFunctions = cacheFile.Functions.size();
for(size_t i =0; i < numberFunctions; ++i)
{
cmExecutionStatus status;
this->ExecuteCommand(cacheFile.Functions[i],status);
if(cmSystemTools::GetFatalErrorOccured())
{
// Exit early due to error.
lexScope.Quiet();
incScope.Quiet();
break;
}
if(status.GetReturnInvoked())
{
// Exit early due to return command.
break;
}
}
}
// If this is the directory-level CMakeLists.txt file then perform
// some extra checks.
if(this->ListFileStack.size() == 1)
{
this->EnforceDirectoryLevelRules();
}
this->AddDefinition("CMAKE_PARENT_LIST_FILE", currentParentFile.c_str());
this->MarkVariableAsUsed("CMAKE_PARENT_LIST_FILE");
this->AddDefinition("CMAKE_CURRENT_LIST_FILE", currentFile.c_str());
this->MarkVariableAsUsed("CMAKE_CURRENT_LIST_FILE");
this->AddDefinition("CMAKE_CURRENT_LIST_DIR",
cmSystemTools::GetFilenamePath(currentFile).c_str());
this->MarkVariableAsUsed("CMAKE_CURRENT_LIST_DIR");
// pop the listfile off the stack
this->ListFileStack.pop_back();
// Check for unused variables
this->CheckForUnusedVariables();
return true;
}
//----------------------------------------------------------------------------
void cmMakefile::EnforceDirectoryLevelRules()
{
// Diagnose a violation of CMP0000 if necessary.
if(this->CheckCMP0000)
{
cmOStringStream msg;
msg << "No cmake_minimum_required command is present. "
<< "A line of code such as\n"
<< " cmake_minimum_required(VERSION "
<< cmVersion::GetMajorVersion() << "."
<< cmVersion::GetMinorVersion()
<< ")\n"
<< "should be added at the top of the file. "
<< "The version specified may be lower if you wish to "
<< "support older CMake versions for this project. "
<< "For more information run "
<< "\"cmake --help-policy CMP0000\".";
switch (this->GetPolicyStatus(cmPolicies::CMP0000))
{
case cmPolicies::WARN:
// Warn because the user did not provide a mimimum required
// version.
this->IssueMessage(cmake::AUTHOR_WARNING, msg.str().c_str());
case cmPolicies::OLD:
// OLD behavior is to use policy version 2.4 set in
// cmListFileCache.
break;
case cmPolicies::REQUIRED_IF_USED:
case cmPolicies::REQUIRED_ALWAYS:
case cmPolicies::NEW:
// NEW behavior is to issue an error.
this->IssueMessage(cmake::FATAL_ERROR, msg.str().c_str());
cmSystemTools::SetFatalErrorOccured();
return;
}
}
}
void cmMakefile::AddCommand(cmCommand* wg)
{
this->GetCMakeInstance()->AddCommand(wg);
}
// Set the make file
void cmMakefile::SetLocalGenerator(cmLocalGenerator* lg)
{
this->LocalGenerator = lg;
// the source groups need to access the global generator
// so don't create them until the lg is set
#if defined(CMAKE_BUILD_WITH_CMAKE)
this->AddSourceGroup("", "^.*$");
this->AddSourceGroup
("Source Files",
"\\.(C|M|c|c\\+\\+|cc|cpp|cxx|f|f90|for|fpp"
"|ftn|m|mm|rc|def|r|odl|idl|hpj|bat)$");
this->AddSourceGroup("Header Files",
"\\.(h|hh|h\\+\\+|hm|hpp|hxx|in|txx|inl)$");
this->AddSourceGroup("CMake Rules", "\\.rule$");
this->AddSourceGroup("Resources", "\\.plist$");
#endif
this->WarnUnused = this->GetCMakeInstance()->GetWarnUnused();
this->CheckSystemVars = this->GetCMakeInstance()->GetCheckSystemVars();
}
bool cmMakefile::NeedBackwardsCompatibility(unsigned int major,
unsigned int minor,
unsigned int patch)
{
if(this->LocalGenerator)
{
return
this->LocalGenerator->NeedBackwardsCompatibility(major, minor, patch);
}
else
{
return false;
}
}
void cmMakefile::FinalPass()
{
// do all the variable expansions here
this->ExpandVariables();
// give all the commands a chance to do something
// after the file has been parsed before generation
for(std::vector<cmCommand*>::iterator i = this->UsedCommands.begin();
i != this->UsedCommands.end(); ++i)
{
(*i)->FinalPass();
}
}
// Generate the output file
void cmMakefile::ConfigureFinalPass()
{
this->FinalPass();
const char* oldValue
= this->GetDefinition("CMAKE_BACKWARDS_COMPATIBILITY");
if (oldValue && atof(oldValue) <= 1.2)
{
cmSystemTools::Error("You have requested backwards compatibility "
"with CMake version 1.2 or earlier. This version "
"of CMake only supports backwards compatibility "
"with CMake 1.4 or later. For compatibility with "
"1.2 or earlier please use CMake 2.0");
}
for (cmTargets::iterator l = this->Targets.begin();
l != this->Targets.end(); l++)
{
l->second.FinishConfigure();
}
}
//----------------------------------------------------------------------------
void
cmMakefile::AddCustomCommandToTarget(const char* target,
const std::vector<std::string>& depends,
const cmCustomCommandLines& commandLines,
cmTarget::CustomCommandType type,
const char* comment,
const char* workingDir,
bool escapeOldStyle)
{
// Find the target to which to add the custom command.
cmTargets::iterator ti = this->Targets.find(target);
if(ti != this->Targets.end())
{
// Add the command to the appropriate build step for the target.
std::vector<std::string> no_output;
cmCustomCommand cc(this, no_output, depends,
commandLines, comment, workingDir);
cc.SetEscapeOldStyle(escapeOldStyle);
cc.SetEscapeAllowMakeVars(true);
switch(type)
{
case cmTarget::PRE_BUILD:
ti->second.GetPreBuildCommands().push_back(cc);
break;
case cmTarget::PRE_LINK:
ti->second.GetPreLinkCommands().push_back(cc);
break;
case cmTarget::POST_BUILD:
ti->second.GetPostBuildCommands().push_back(cc);
break;
}
}
}
//----------------------------------------------------------------------------
void
cmMakefile::AddCustomCommandToOutput(const std::vector<std::string>& outputs,
const std::vector<std::string>& depends,
const char* main_dependency,
const cmCustomCommandLines& commandLines,
const char* comment,
const char* workingDir,
bool replace,
bool escapeOldStyle)
{
// Make sure there is at least one output.
if(outputs.empty())
{
cmSystemTools::Error("Attempt to add a custom rule with no output!");
return;
}
// Validate custom commands. TODO: More strict?
for(cmCustomCommandLines::const_iterator i=commandLines.begin();
i != commandLines.end(); ++i)
{
cmCustomCommandLine const& cl = *i;
if(!cl.empty() && !cl[0].empty() && cl[0][0] == '"')
{
cmOStringStream e;
e << "COMMAND may not contain literal quotes:\n " << cl[0] << "\n";
this->IssueMessage(cmake::FATAL_ERROR, e.str());
return;
}
}
// Choose a source file on which to store the custom command.
cmSourceFile* file = 0;
if(main_dependency && main_dependency[0])
{
// The main dependency was specified. Use it unless a different
// custom command already used it.
file = this->GetSource(main_dependency);
if(file && file->GetCustomCommand() && !replace)
{
// The main dependency already has a custom command.
if(commandLines == file->GetCustomCommand()->GetCommandLines())
{
// The existing custom command is identical. Silently ignore
// the duplicate.
return;
}
else
{
// The existing custom command is different. We need to
// generate a rule file for this new command.
file = 0;
}
}
else
{
// The main dependency does not have a custom command or we are
// allowed to replace it. Use it to store the command.
file = this->GetOrCreateSource(main_dependency);
}
}
// Generate a rule file if the main dependency is not available.
if(!file)
{
// Construct a rule file associated with the first output produced.
std::string outName = outputs[0];
outName += ".rule";
const char* dir =
this->LocalGenerator->GetGlobalGenerator()->
GetCMakeCFGInitDirectory();
if(dir && dir[0] == '$')
{
cmSystemTools::ReplaceString(outName, dir,
cmake::GetCMakeFilesDirectory());
}
// Check if the rule file already exists.
file = this->GetSource(outName.c_str());
if(file && file->GetCustomCommand() && !replace)
{
// The rule file already exists.
if(commandLines != file->GetCustomCommand()->GetCommandLines())
{
cmSystemTools::Error("Attempt to add a custom rule to output \"",
outName.c_str(),
"\" which already has a custom rule.");
}
return;
}
// Create a cmSourceFile for the rule file.
file = this->GetOrCreateSource(outName.c_str(), true);
}
// Always create the output sources and mark them generated.
for(std::vector<std::string>::const_iterator o = outputs.begin();
o != outputs.end(); ++o)
{
if(cmSourceFile* out = this->GetOrCreateSource(o->c_str(), true))
{
out->SetProperty("GENERATED", "1");
}
}
// Construct a complete list of dependencies.
std::vector<std::string> depends2(depends);
if(main_dependency && main_dependency[0])
{
depends2.push_back(main_dependency);
}
// Attach the custom command to the file.
if(file)
{
cmCustomCommand* cc =
new cmCustomCommand(this, outputs, depends2, commandLines,
comment, workingDir);
cc->SetEscapeOldStyle(escapeOldStyle);
cc->SetEscapeAllowMakeVars(true);
file->SetCustomCommand(cc);
}
}