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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
|
/* This file is part of mhmake.
*
* Copyright (C) 2001-2010 marha@sourceforge.net
*
* Mhmake is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Mhmake is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Mhmake. If not, see <http://www.gnu.org/licenses/>.
*/
/* $Rev$ */
#include "stdafx.h"
#include "util.h"
#include "md5.h"
#include "mhmakefileparser.h"
#include "rule.h"
#include "flexlexer.h"
commandqueue mhmakefileparser::sm_CommandQueue;
///////////////////////////////////////////////////////////////////////////////
int mhmakefileparser::yylex(void)
{
m_yyloc=m_ptheLexer->lineno();
return m_ptheLexer->yylex(m_theTokenValue);
}
///////////////////////////////////////////////////////////////////////////////
void mhmakefileparser::yyerror(const char *m)
{
cerr << this->m_ptheLexer->m_InputFileName<< " ("<<m_yyloc<<"): "<<m<<endl;
}
///////////////////////////////////////////////////////////////////////////////
int mhmakefileparser::ParseFile(const fileinfo *pFileInfo, const fileinfo *pMakeDir)
{
if (pMakeDir)
{
m_MakeDir=pMakeDir;
m_Variables[CURDIR]=m_MakeDir->GetQuotedFullFileName();
}
ifstream yyin(pFileInfo->GetFullFileName().c_str(),ios_base::in);
if (yyin.fail())
{
cerr << "Error opening makefile: "<<pFileInfo->GetQuotedFullFileName()<<endl;
return 1;
}
mhmakeFlexLexer theLexer(&yyin);
m_ptheLexer=&theLexer;
theLexer.m_InputFileName=pFileInfo->GetFullFileName();
theLexer.m_pParser=(mhmakeparser*)this;
int Ret=yyparse();
return Ret;
}
///////////////////////////////////////////////////////////////////////////////
bool mhmakefileparser::IsDefined(const string &Var) const
{
bool Ret = m_Variables.find(Var)!=m_Variables.end();
if (!Ret)
{
string Env=GetFromEnv(Var);
if (!Env.empty())
{
Ret=true;
}
}
return Ret;
}
///////////////////////////////////////////////////////////////////////////////
static inline size_t SkipUntilQuote(const string &Expr,size_t i,char Char)
{
while (Expr[i++]!=Char) ;
return i;
}
///////////////////////////////////////////////////////////////////////////////
static inline size_t SkipMakeExpr(const string &Expr,size_t i)
{
char Char=Expr[i++];
if (Char!='(')
return i;
Char=Expr[i++];
while (Char!=')')
{
if (Char=='$')
{
i=SkipMakeExpr(Expr,i);
}
#ifdef _DEBUG
if (i>=Expr.length())
throw(string(") not found in ")+Expr);
#endif
Char=Expr[i++];
}
return i;
}
///////////////////////////////////////////////////////////////////////////////
// Splits expression on the Item, but the item may not occur within
// a macro or quoted string
static pair<string,string> SplitExpr(const string &Expr,char Item)
{
size_t i=0;
char Char=Expr[i++];
while (Char!=Item)
{
if (Char=='"' || Char=='\'')
{
i=SkipUntilQuote(Expr,i,Char);
}
else if (Char=='$')
{
i=SkipMakeExpr(Expr,i);
}
Char=Expr[i++];
}
return pair<string,string>(Expr.substr(0,i-1),Expr.substr(i));
}
///////////////////////////////////////////////////////////////////////////////
bool mhmakefileparser::IsEqual(const string &EqualExpr) const
{
string Expr=ExpandExpression(EqualExpr);
const char *pStr=Expr.c_str();
const char *pTmp=pStr;
while (*pTmp && *pTmp!=',') pTmp++;
ptrdiff_t Pos=pTmp-pStr;
size_t Size=Expr.size();
pTmp=pStr+Size-1;
while (pTmp>pStr && strchr(" \t",*pTmp))
{
pTmp--;
}
if (2*Pos != pTmp-pStr)
{
return false;
}
pTmp=pStr;
const char *pTmp2=pTmp+Pos+1;
if (*pTmp=='(')
{
pTmp++;
Pos--;
}
for (int i=0; i<Pos; i++)
{
if (pTmp[i]!=pTmp2[i])
{
return false;
}
}
return true;
}
///////////////////////////////////////////////////////////////////////////////
string mhmakefileparser::ExpandExpression(const string &Expr) const
{
bool Recurse;
string Ret(Expr);
do
{
Recurse=false;
Ret=ExpandExpressionRecurse(Ret,Recurse);
}
while (Recurse);
return Ret;
}
///////////////////////////////////////////////////////////////////////////////
string mhmakefileparser::ExpandExpressionRecurse(const string &Expr, bool &Recurse) const
{
//((mhmakefileparser*)this)->m_InExpandExpression++;
size_t i=0;
size_t Length=Expr.size();
string Ret;
string ToAdd;
while (i<Length)
{
char Char=Expr[i++];
if (Char=='$')
{
char CharNext=Expr[i];
if (CharNext=='$')
{
ToAdd="$";
i++;
}
else
{
size_t inew=SkipMakeExpr(Expr,i);
i++;
if (inew>i)
{
ToAdd=ExpandMacro(Expr.substr(i,inew-i-1),Recurse);
i=inew;
}
else
{
// This is a single character expression
ToAdd=ExpandMacro(string(1,Expr[i-1]),Recurse);
}
}
Ret+=ToAdd;
}
else
{
Ret+=Char;
}
}
return Ret;
}
///////////////////////////////////////////////////////////////////////////////
string mhmakefileparser::ExpandMacro(const string &Expr, bool &Recurse) const
{
string ExpandedExpr=ExpandExpressionRecurse(Expr,Recurse);
const char *pTmp=ExpandedExpr.c_str();
/* First remove leading spaces */
while (*pTmp==' ' || *pTmp=='\t') pTmp++;
const char *pVar=pTmp;
while (*pTmp && *pTmp!=' ' && *pTmp!='\t' && *pTmp!=':') pTmp++;
const char *pVarEnd=pTmp;
char Type=*pTmp++;
while (*pTmp && (*pTmp==' ' || *pTmp=='\t')) pTmp++;
if (Type&&*pTmp)
{ // We have a match for the regular expression ^([^ \\t:]+)([: \\t])[ \\t]*(.+)
if (Type==':')
{
#ifdef WIN32
bool IsFileName=false;
if (pVarEnd-pVar == 1 && (*pVar=='<' || *pVar =='@'))
IsFileName=true;
#endif
string ToSubst=ExpandExpression(ExpandVar(string(pVar,pVarEnd)));
const char *pSrc=pTmp;
const char *pStop=pSrc;
while (*pStop!='=') pStop++;
const char *pTo=pStop+1;
string SrcStr(pSrc,pStop);
string ToStr(pTo);
#ifdef WIN32
if (IsFileName)
{
matchres Res;
string FileName(UnquoteFileName(ToSubst));
if (PercentMatch(FileName,UnquoteFileName(SrcStr),&Res))
{
FileName=ReplaceWithStem(UnquoteFileName(ToStr),Res.m_Stem);
}
return QuoteFileName(FileName);
}
#endif
return Substitute(ToSubst,SrcStr,ToStr);
}
else if (Type==' ' || Type == '\t')
{
string Func(pVar,pVarEnd);
string Arg(pTmp);
if (Recurse)
{
#ifdef _DEBUG
if (!(Arg.find('%')!=string::npos && Arg.find('$')!=string::npos))
throw(string("Bug in mhmake: expected a % and $ sign: ")+Arg);
#endif
return string("$(")+ExpandedExpr+")"; // we cannot call the function yet since there is still a $(*%*) macro to resolve. so
// return it a a macro again so it can be resolved when the % is resolved
// remark that the current test is not completely safe because the % could be out of
// the $ macro
}
function_f pFunc=m_Functions[Func];
#ifdef _DEBUG
if (pFunc)
{
return (this->*pFunc)(Arg, &Expr);
}
else
{
throw string("Unknown function specified in macro: ")+Func;
}
#else
return (this->*pFunc)(Arg);
#endif
}
else
{
#ifdef _DEBUG
throw string("Fatal error in ExpandMacro (bug in mhmake ? ? ?)");
#else
return g_EmptyString;
#endif
}
}
else
{
if (ExpandedExpr.find('%')!=string::npos)
{
// we have encountered a *%* macro. This means a previous subst is not yet finished. so return
// it back as a macro so it can be expanded again later when the % is replaced
Recurse=true;
return string("$(")+ExpandedExpr+")";
}
#ifdef _DEBUG
else if (ExpandedExpr.find('$')!=string::npos)
throw(string("Bug in mhmake: wasn't expecting a $ sign in: ")+ExpandedExpr);
#endif
else
{
return ExpandExpression(ExpandVar(ExpandedExpr));
}
}
}
///////////////////////////////////////////////////////////////////////////////
string mhmakefileparser::ExpandVar(const string &Var) const
{
map<string,string>::const_iterator pIt=m_Variables.find(Var);
if (pIt==m_Variables.end())
{
if (Var.size()==1)
{
char Char=Var[0];
if (m_RuleThatIsBuild)
{
switch (Char)
{
case '<': // return first prerequisit
#ifdef _DEBUG
if (!m_RuleThatIsBuild->GetDeps().size())
{
return "<No Dependencies defined.>";
}
#endif
return m_RuleThatIsBuild->GetDeps()[0]->GetQuotedFullFileName();
case '@': // return full target file name
return m_RuleThatIsBuild->GetQuotedFullFileName();
case '*': // return stem
return m_RuleThatIsBuild->GetRule()->GetStem();
case '^': // return all prerequisits
return m_RuleThatIsBuild->GetPrerequisits();
case '/':
return OSPATHSEPSTR;
default:
break;
}
}
else
{
switch (Char)
{
case '<': // return first prerequisit
case '@': // return full target file name
case '*': // return stem
case '^': // return all prerequisits
return Var; // To make comparing of rules more accurate
case '/':
return OSPATHSEPSTR;
default:
break;
}
}
}
string Env=GetFromEnv(Var);
if (Env.empty())
{
#ifdef _DEBUG
if (g_PrintAdditionalInfo)
{
cout<<"Warning: Variable "<<Var<<" not found\n";
}
#endif
return g_EmptyString;
}
else
return Env;
}
else
return pIt->second;
}
///////////////////////////////////////////////////////////////////////////////
void mhmakefileparser::SplitToItems(const string &String,vector<fileinfo*> &Items) const
{
const char *pTmp=String.c_str();
while (*pTmp)
{
string Item;
pTmp=NextItem(pTmp,Item);
if (!Item.empty())
{
Items.push_back(GetFileInfo(Item,m_MakeDir));
}
}
}
///////////////////////////////////////////////////////////////////////////////
void mhmakefileparser::ParseBuildedIncludeFiles()
{
vector<string>::iterator It=m_ToBeIncludeAfterBuild.begin();
while (It!=m_ToBeIncludeAfterBuild.end())
{
int result=ParseFile(GetFileInfo(*It,m_MakeDir));
if (result)
{
throw string("Error parsing ")+*It;
}
It++;
}
}
#ifdef _DEBUG
///////////////////////////////////////////////////////////////////////////////
void mhmakefileparser::PrintVariables(bool Expand) const
{
map<string,string>::const_iterator It=m_Variables.begin();
while (It!=m_Variables.end())
{
if (Expand)
{
try
{
cout<<It->first<<" : "<<ExpandExpression(It->second)<<endl;
}
catch (...)
{
cout<<endl;
}
}
else
{
cout<<It->first<<" : "<<It->second<<endl;
}
It++;
}
if (m_pEnv)
{
cout << "Environment overruled:\n";
#ifdef WIN32
char *pEnv=m_pEnv;
while (*pEnv)
{
cout<<pEnv<<endl;
pEnv+=strlen(pEnv)+1;
}
#else
char **pEnv=m_pEnv;
while (*pEnv)
{
cout<<*pEnv<<endl;
pEnv++;
}
#endif
}
}
#endif
//#define PAGETOSTRING(Nr) #Nr
//#define PAGETONBR(Nr) PAGETOSTRING(##Nr)
//#pragma message("ar=" PAGETONBR(NULL) ";")
///////////////////////////////////////////////////////////////////////////////
void mhmakefileparser::AddRule()
{
fileinfoarray::iterator pIt=m_pCurrentItems->begin();
while (pIt!=m_pCurrentItems->end())
{
if ((*pIt)->GetFullFileName().find('%')!=string::npos)
{
IMPLICITRULE::AddImplicitRule(*pIt,*m_pCurrentDeps,m_pCurrentRule);
}
else
{
// If we had a double colon we must make sure that the target is always build
if (m_DoubleColonRule)
{
(*pIt)->SetNotExist();
}
(*pIt)->SetRuleIfNotExist(m_pCurrentRule);
if (!m_pCurrentRule)
(*pIt)->AddDeps(*m_pCurrentDeps);
else
(*pIt)->InsertDeps(*m_pCurrentDeps);
if (!m_FirstTarget)
{ // Only check this if the rule is not an implicit rule
m_FirstTarget=(*m_pCurrentItems)[0];
}
}
pIt++;
}
m_pCurrentItems=NULL;
m_pCurrentRule=NullRule;
}
///////////////////////////////////////////////////////////////////////////////
void mhmakefileparser::GetAutoDeps(const fileinfo *pFirstDep, deps_t &Autodeps)
{
/* Here we have to scan only c/c++ headers so skip certain extensions */
const char *pFullName=pFirstDep->GetFullFileName().c_str();
const char *pExt=strrchr(pFullName,'.');
bool bPython=false;
if (pExt)
{
if (!_stricmp(pExt+1,"py"))
bPython=true;
}
FILE *pIn=fopen(pFullName,"r");
if (!pIn)
return;
char IncludeList[255];
int PrevRet=0;
int Ret=fgetc(pIn);
while(Ret!=EOF)
{
char Type[2];
bool bFound=false;
if (bPython)
{
Type[0]='"';
if (Ret=='i')
{
Ret=fscanf(pIn,"mport %254[^\"\n]",IncludeList);
if (Ret==1)
{
if (IncludeList[0]!='*')
bFound=true;
}
}
}
else
{
if (PrevRet=='/')
{
if (Ret=='/')
{
/* This is a C++ command so read until the next line-feed */
do
{
Ret=fgetc(pIn);
} while (Ret!='\n' && Ret!=EOF);
}
else if (Ret=='*')
{
/* This is a standard C comment, so read until then end of the command */
do
{
PrevRet=Ret;
Ret=fgetc(pIn);
} while ((PrevRet!='*' || Ret!='/') && Ret!=EOF);
}
}
else if (Ret=='#' || Ret=='.')
{
if (Ret=='#')
{
fscanf(pIn,"%*[ \t]");
Ret=fscanf(pIn,"include %1[\"<]%254[^>\"]%*[\">]",&Type,IncludeList);
}
else
Ret=fscanf(pIn,"import %1[\"<]%254[^>\"]%*[\">]",&Type,IncludeList);
if (Ret==2)
{
bFound=true;
}
}
}
if (bFound)
{
const char *pTmp=IncludeList;
while (*pTmp)
{
string IncludeFile;
pTmp=NextItem(pTmp,IncludeFile," \t,");
if (bPython)
IncludeFile+=".py";
if (SkipHeaderFile(IncludeFile))
continue;
#ifdef _DEBUG
m_ImplicitSearch++; // This is to avoid warnings of targets that does not exist
#endif
if (Type[0]=='"')
{
fileinfo *pInclude=GetFileInfo(IncludeFile,pFirstDep->GetDir());
/* Add the dependency when the file alrady exist or there is a rule available to be build */
mh_time_t Date=BuildTarget(pInclude);
if (Date.DoesExist()) // Try to build the target, and add it if it exists after building
{
deps_t::const_iterator pFind=Autodeps.find(pInclude);
if (pFind==Autodeps.end())
{
Autodeps.insert(pInclude);
GetAutoDepsIfNeeded(pInclude,pInclude);
}
}
}
const refptr<fileinfoarray> IncludeDirs=GetIncludeDirs();
fileinfoarray::const_iterator It=IncludeDirs->begin();
while (It<IncludeDirs->end())
{
fileinfo *pInclude=GetFileInfo(IncludeFile,*It);
mh_time_t Date=BuildTarget(pInclude);
if (Date.DoesExist()) // Try to build the target, and add it if it exists after building
{
deps_t::const_iterator pFind=Autodeps.find(pInclude);
if (pFind==Autodeps.end())
{
Autodeps.insert(pInclude);
GetAutoDepsIfNeeded(pInclude,pInclude);
}
break;
}
It++;
}
#ifdef _DEBUG
m_ImplicitSearch--;
#endif
}
}
PrevRet=Ret;
Ret=fgetc(pIn);
}
fclose(pIn);
}
///////////////////////////////////////////////////////////////////////////////
void mhmakefileparser::GetAutoDepsIfNeeded(fileinfo *pTarget, const fileinfo *pFirstDep)
{
autodeps_entry_t &Autodeps=m_AutoDeps[pTarget];
if (!Autodeps.first)
{
Autodeps.first=true;
/* We are going to rescan, so throw away the old. */
Autodeps.second.clear();
GetAutoDeps(pFirstDep,Autodeps.second);
// Now add these dependencies also to the rules
deps_t::iterator It=Autodeps.second.begin();
while (It!=Autodeps.second.end())
{
pTarget->AddDep(*It);
It++;
}
}
}
///////////////////////////////////////////////////////////////////////////////
void mhmakefileparser::UpdateAutomaticDependencies(fileinfo *pTarget)
{
if (pTarget->IsAutoDepExtention())
{
// we have to search for the include files in the first dependency of Target
vector<fileinfo*> &Deps=pTarget->GetDeps();
if (!Deps.size())
return; // There is no first dep
fileinfo *pFirstDep=Deps[0];
GetAutoDepsIfNeeded(pTarget,pFirstDep);
SetAutoDepsDirty();
}
}
///////////////////////////////////////////////////////////////////////////////
void mhmakefileparser::UpdateNoRuleAutomaticDependencies(fileinfo *pTarget)
{
// we have to search for the include files in the Target
deps_t Autodeps;
GetAutoDeps(pTarget,Autodeps);
// Now add these dependencies also to the rules
deps_t::iterator It=Autodeps.begin();
while (It!=Autodeps.end())
{
pTarget->AddDep(*It);
It++;
}
}
///////////////////////////////////////////////////////////////////////////////
const refptr<fileinfoarray> mhmakefileparser::GetIncludeDirs() const
{
string Includes=ExpandExpression("$(INCLUDES)");
if (!m_pIncludeDirs || Includes != m_IncludeDirs)
{
((mhmakefileparser*)this)->m_IncludeDirs=Includes;
((mhmakefileparser*)this)->m_pIncludeDirs=refptr<fileinfoarray>(new fileinfoarray);
if (Includes.empty()) // If not defined try a default path
Includes="include inc .."OSPATHSEPSTR"include .."OSPATHSEPSTR"inc";
const char *pTmp=Includes.c_str();
while (*pTmp)
{
string Item;
pTmp=NextItem(pTmp,Item);
fileinfo *pIncDir=GetFileInfo(Item,m_MakeDir);
if (pIncDir->Exists() || pIncDir->GetRule())
((mhmakefileparser*)this)->m_pIncludeDirs->push_back(pIncDir);
}
}
return m_pIncludeDirs;
}
static void ReadStr(FILE *pFile,char *Str)
{
int i=0;
while (1)
{
Str[i]=fgetc(pFile);
#ifdef _DEBUG
if (Str[i]==EOF)
{
cout<<"Premature end of depency file.\n";
Str[i]=0;
return;
}
#endif
if (Str[i]=='\n')
break;
i++;
}
Str[i]='\0';
}
void mhmakefileparser::LoadAutoDepsFile(fileinfo *pDepFile)
{
if (m_AutoDepFileLoaded && m_AutoDepFileLoaded==pDepFile)
return; /* This autodep file is already loaded. */
m_AutoDepFileLoaded=pDepFile;
FILE *pIn=fopen(pDepFile->GetFullFileName().c_str(),"rb");
#ifdef _DEBUG
if (!pIn)
{
cerr << "Error opening autodep file "<<pDepFile->GetQuotedFullFileName()<<endl;
return;
}
#endif
fread(&m_EnvMd5_32,sizeof(m_EnvMd5_32),1,pIn);
#ifdef _DEBUG
if (g_PrintAdditionalInfo)
cout << "Reading Env Md5 from "<<pDepFile->GetQuotedFullFileName()<<": "<<hex<<m_EnvMd5_32<<endl;
#endif
char UsedEnvVars[1024];
ReadStr(pIn,UsedEnvVars);
SetVariable(USED_ENVVARS,UsedEnvVars);
char FileName[MAX_PATH];
ReadStr(pIn,FileName);
while (FileName[0])
{
fileinfo *pTarget=GetFileInfo(FileName,m_MakeDir);
autodeps_entry_t &Autodeps=m_AutoDeps[pTarget];
ReadStr(pIn,FileName);
while (FileName[0])
{
if (!g_ForceAutoDepRescan) /* If we are forcing the autodepscan we do not have to load the dependencies. */
{
fileinfo *pDep=GetFileInfo(FileName,m_MakeDir);
Autodeps.second.insert(pDep);
pTarget->AddDep(pDep);
}
ReadStr(pIn,FileName);
}
ReadStr(pIn,FileName);
}
uint32 Md5_32;
bool MakeNotDirty=true;
while (fread(&Md5_32,sizeof(Md5_32),1,pIn))
{
ReadStr(pIn,FileName);
fileinfo *pTarget=GetAbsFileInfo(FileName);
if (!pTarget->CompareMd5_32(0) && !pTarget->CompareMd5_32(Md5_32))
{
MakeNotDirty=false; /* BuildTarget had set the dirty flag, but since the md5 did not change it was a false dirty. This is for BuildTargets called before this routine */
#ifdef _DEBUG
cout << "Warning: trying to set to different md5's for Target "<<pTarget->GetQuotedFullFileName()<<" Old: "<<hex<<pTarget->GetCommandsMd5_32()<<" New: "<<Md5_32<<endl;
#endif
}
#ifdef _DEBUG
if (g_PrintAdditionalInfo)
cout << "Setting Md5 for Target "<<pTarget->GetQuotedFullFileName()<<" to "<<hex<<Md5_32<<endl;
#endif
pTarget->SetCommandsMd5_32(Md5_32); // If it was already there, just update the md5 value
AddTarget(pTarget);
}
if (MakeNotDirty)
ClearAutoDepsDirty();
fclose(pIn);
}
static void MakeDirs(const fileinfo *pDir)
{
fileinfo *pParentDir=pDir->GetDir();
if (!pParentDir->GetDate().DoesExist())
{ /* First make parent dirs */
MakeDirs(pParentDir);
}
if (!pDir->GetDate().DoesExist())
{ /* Create directory */
mkdir(pDir->GetFullFileName().c_str(),S_IRWXU);
}
}
void mhmakefileparser::SaveAutoDepsFile()
{
if (!IsAutoDepsDirty())
return;
if (g_Clean
#ifdef _DEBUG
|| g_DoNotExecute || g_GenProjectTree
#endif
)
return; // do not save on clean or if no commands are executed
string DepFile=ExpandVar(AUTODEPFILE);
if (!DepFile.size())
{
return;
}
fileinfo *pDepFile=GetFileInfo(DepFile,m_MakeDir);
#ifdef _DEBUG
if (g_PrintAdditionalInfo)
cout<<"Saving automatic dependency file "<<DepFile<<endl;
#endif
FILE *pOut=fopen(pDepFile->GetFullFileName().c_str(),"wb");
if (!pOut)
{
/* Maybe it is because the directory does not exist, so try to create this first */
MakeDirs(pDepFile->GetDir());
pOut=fopen(pDepFile->GetFullFileName().c_str(),"wb");
if (!pOut)
{
#ifdef _DEBUG
if (!g_DoNotExecute)
#endif
cerr << "Error creating file "<<DepFile<<endl;
return;
}
}
// First update the USER_ENVVARS variable and then save it to the dep file together with the md5 string
CreateUSED_ENVVARS();
uint32 Md5_32=CreateEnvMd5_32();
fwrite(&Md5_32,sizeof(Md5_32),1,pOut);
fprintf(pOut,"%s\n",m_Variables[USED_ENVVARS].c_str());
autodeps_t::const_iterator It=m_AutoDeps.begin();
while (It!=m_AutoDeps.end())
{
if (!It->second.second.empty())
{
fprintf(pOut,"%s\n",It->first->GetFullFileName().c_str());
deps_t::const_iterator DepIt=It->second.second.begin();
while (DepIt!=It->second.second.end())
{
fprintf(pOut,"%s\n",(*DepIt)->GetFullFileName().c_str());
DepIt++;
}
fprintf(pOut,"\n");
}
It++;
}
/* Now save the Md5 strings */
fprintf(pOut,"\n");
set<const fileinfo *>::iterator pIt=m_Targets.begin();
while (pIt!=m_Targets.end())
{
if (!(*pIt)->CompareMd5_32(0))
{
(*pIt)->WriteMd5_32(pOut);
string FileName=(*pIt)->GetFullFileName();
fwrite(FileName.c_str(),FileName.size(),1,pOut);
fputc('\n',pOut);
}
pIt++;
}
fclose(pOut);
}
///////////////////////////////////////////////////////////////////////////////
// Uses the SKIPHEADERS variable to check if we have to skip the header in
// the automatic dependency scanning
bool mhmakefileparser::SkipHeaderFile(const string &FileName)
{
if (!m_SkipHeadersInitialized)
{
m_SkipHeadersInitialized=true;
string HeadersToSkip=ExpandVar(SKIPHEADERS);
const char *pTmp=HeadersToSkip.c_str();
while (*pTmp)
{
string Item;
pTmp=NextItem(pTmp,Item);
if (Item.find('%')==string::npos)
{
m_SkipHeaders.insert(Item);
}
else
{
m_PercentHeaders.push_back(Item);
}
}
}
if (m_SkipHeaders.find(FileName)!=m_SkipHeaders.end())
return true;
vector<string>::const_iterator It=m_PercentHeaders.begin();
vector<string>::const_iterator ItEnd=m_PercentHeaders.end();
while (It!=ItEnd)
{
if (PercentMatch(FileName,*It++))
return true;
}
return false;
}
///////////////////////////////////////////////////////////////////////////////
void mhmakefileparser::SetExport(const string &Var, const string &Val)
{
m_Exports.insert(Var);
#ifdef WIN32
if (!m_pEnv)
{
/* Environment not created yet, so create one */
char *pEnv=GetEnvironmentStrings();
char *pEnd=pEnv;
while (*pEnd)
{
while (*pEnd++);
}
size_t Len=pEnd-pEnv+1;
m_pEnv=(char*)malloc(Len);
memcpy(m_pEnv,pEnv,Len);
m_EnvLen=Len;
FreeEnvironmentStrings(pEnv);
}
/* First check if the variable is in the environment, if so remove it first */
char *pEnv=m_pEnv;
while (*pEnv)
{
const char *pVar=Var.c_str();
char *pStart=pEnv;
while (*pEnv!='=' && *pEnv==*pVar)
{
pEnv++; pVar++;
}
if (*pEnv=='=' && !*pVar)
{
/* Variable found, remove it */
while (*pEnv++);
m_EnvLen-=pEnv-pStart;
while (*pEnv)
{
while (*pEnv)
{
*pStart=*pEnv++;
pStart++;
}
*pStart=*pEnv++;
pStart++;
}
*pStart=*pEnv;
break;
}
while (*pEnv++);
}
size_t VarLen=Var.length();
size_t ValLen=Val.length();
size_t Extra=VarLen+ValLen+2;
/* Add the variable at the end */
m_pEnv=(char*)realloc(m_pEnv,m_EnvLen+Extra);
pEnv=m_pEnv+m_EnvLen-1;
memcpy(pEnv,Var.c_str(),VarLen);
pEnv+=VarLen;
*pEnv++='=';
memcpy(pEnv,Val.c_str(),ValLen);
pEnv+=ValLen;
*pEnv++='\0';
*pEnv++='\0';
m_EnvLen+=Extra;
#else
if (!m_pEnv)
{
/* Environment not created yet, so create one */
char **pEnv=environ;
char **pEnd=pEnv;
int Len=1;
while (*pEnd)
{
Len++;
pEnd++;
}
m_EnvLen=Len;
m_pEnv=(char**)malloc(Len*sizeof(pEnv));
int i=0;
while (*pEnv)
{
m_pEnv[i]=strdup(*pEnv);
i++;
pEnv++;
}
m_pEnv[i]=NULL;
}
/* First check if the variable is in the environment, if so replace it */
char **pEnv=m_pEnv;
while (*pEnv)
{
const char *pVar=Var.c_str();
char *pStart=*pEnv;
char *pTmp=pStart;
while (*pTmp!='=' && *pTmp==*pVar)
{
pTmp++; pVar++;
}
if (*pTmp=='=' && !*pVar)
{
free(*pEnv);
*pEnv=strdup((Var+"="+Val).c_str());
break;
}
pEnv++;
}
if (!*pEnv)
{
// Add it at the end of the list
m_pEnv=(char**)realloc(m_pEnv,(m_EnvLen+1)*sizeof(*pEnv));
m_pEnv[m_EnvLen-1]=strdup((Var+"="+Val).c_str());
m_pEnv[m_EnvLen++]=NULL;
}
#endif
}
///////////////////////////////////////////////////////////////////////////////
void mhmakefileparser::SetvPath(const string &Pattern, const string &Path)
{
// First create the array of directory from the Path parameter. For now all entries need to
// be valid directories (extra checking). This could be improved by checking if there
// is a rule for creating the directory entry, but like first said, not for now.
refptr<fileinfoarray> pDirArray=new fileinfoarray;
const char *pTmp=Path.c_str();
while (*pTmp)
{
string Item;
pTmp=NextItem(pTmp,Item," \t:;");
if (!Item.empty())
{
fileinfo *pDir=GetFileInfo(Item,m_MakeDir);
if (!pDir->IsDir())
{
#ifdef WIN32
// On windows this could happen if a full pathname was specified, so we try together
// with the next item to check if this is not a valid directories
// but only if the the length of the item was 1 namely the driver letter
if (Item.length()==1)
{
string Rest;
pTmp=NextItem(pTmp,Rest," \t:;");
pDir=GetFileInfo(Item+":"+Rest,m_MakeDir);
#ifdef _DEBUG
if (g_PrintAdditionalInfo && !pDir->IsDir())
throw(pDir->GetFullFileName()+" is not a valid directory.");
#endif
}
#ifdef _DEBUG
else if (g_PrintAdditionalInfo)
cout << pDir->GetFullFileName() << " is not a valid directory.\n";
#endif
#else
#ifdef _DEBUG
if (g_PrintAdditionalInfo)
cout << pDir->GetFullFileName() << " is not a valid directory.\n";
#endif
#endif
}
pDirArray->push_back(pDir);
}
}
m_vPath.push_back(pair<string, refptr<fileinfoarray> >(Pattern,pDirArray));
}
///////////////////////////////////////////////////////////////////////////////
//
// Search for the target using the vPath
fileinfo* mhmakefileparser::SearchvPath(const fileinfo* pTarget)
{
string TargetName=pTarget->GetName();
vector< pair< string, refptr<fileinfoarray> > >::iterator vPathIt=m_vPath.begin();
while (vPathIt!=m_vPath.end())
{
matchres Res;
if (PercentMatch(TargetName,vPathIt->first,&Res))
{
fileinfoarray::iterator pIt=vPathIt->second->begin();
while (pIt!=vPathIt->second->end())
{
fileinfo* pNewTarget=GetFileInfo(TargetName,*pIt);
mh_time_t TargetDate=StartBuildTarget(pNewTarget,false);
if (!TargetDate.IsDateValid())
TargetDate=WaitBuildTarget(pNewTarget);
if (pNewTarget->GetDate().DoesExist())
return pNewTarget;
pIt++;
}
}
vPathIt++;
}
return NULL;
}
///////////////////////////////////////////////////////////////////////////////
//
//Checks if the variables retreived from the environment or command-line have been
//changed. Do this at late as possible because they can also be changed in theLexer
//makefiles.
//
void mhmakefileparser::CheckEnv(void)
{
if (CompareEnv())
{
#ifdef _DEBUG
if (!g_GenProjectTree)
cout << "Rebuilding everything of "<< m_MakeDir->GetQuotedFullFileName() <<" because environment and/or command-line variables have been changed.\n";
#endif
SetRebuildAll();
}
}
///////////////////////////////////////////////////////////////////////////////
//
//Create a Md5 string from m_GlobalCommandLineVars and USED_ENVVARS
//
static bool SkipVar(const string &Var)
{
if (Var==WC_REVISION)
return true;
if (Var==WC_URL)
return true;
if (Var==MAKE)
return true;
return false;
}
#define DBGOUT(stuff)
uint32 mhmakefileparser::CreateEnvMd5_32() const
{
md5_context ctx;
string Md5;
string EnvVars=ExpandVar(USED_ENVVARS);
const char *pTmp=EnvVars.c_str();
// Now create the md5 string
md5_starts( &ctx );
DBGOUT(cout << "MD5 of " << m_MakeDir->GetQuotedFullFileName() << endl);
while (*pTmp)
{
string Var;
pTmp=NextItem(pTmp,Var,";");
if (!SkipVar(Var))
{
string Val=ExpandVar(Var);
transform(Val.begin(),Val.end(),Val.begin(),(int(__CDECL *)(int))toupper);
DBGOUT(cout << GetMakeDir()->GetQuotedFullFileName() << " -> Setting GetFromEnv var " << Var << " to " << Val << endl);
md5_update( &ctx, (uint8 *) Var.c_str(), (unsigned long)Var.size());
md5_update( &ctx, (uint8 *) "=", 1);
md5_update( &ctx, (uint8 *) Val.c_str(), (unsigned long)Val.size());
}
}
return md5_finish32( &ctx);
}
///////////////////////////////////////////////////////////////////////////////
// Makes sure that the makefile exports are set in the environment
bool mhmakefileparser::CompareEnv() const
{
uint32 Md5_32=CreateEnvMd5_32();
#ifdef _DEBUG
if (!g_GenProjectTree && Md5_32!=m_EnvMd5_32)
cout << "Environment has been changed: "<<hex<<m_EnvMd5_32<<" to "<<Md5_32<<endl;
#endif
return Md5_32!=m_EnvMd5_32;
}
///////////////////////////////////////////////////////////////////////////////
// Makes sure that the makefile exports are set in the environment
mh_time_t mhmakefileparser::m_sBuildTime;
void mhmakefileparser::InitBuildTime()
{
m_sBuildTime=time(NULL);
}
///////////////////////////////////////////////////////////////////////////////
// Returns a variable from the environment or from the command line and adds it the m_UsedEnvVars
string mhmakefileparser::GetFromEnv(const string &Var, bool Cache) const
{
/* First we look into the command line variables, before we are looking in the environment */
map<string,string>::const_iterator pLineFind=m_CommandLineVars.find(Var);
if (pLineFind!=m_CommandLineVars.end())
{
((mhmakefileparser*)this)->m_UsedEnvVars.insert(Var);
if (Cache)
((mhmakefileparser*)this)->m_Variables[Var]=pLineFind->second;
return pLineFind->second;
}
const char *pEnv=getenv(Var.c_str());
if (!pEnv)
{
return g_EmptyString;
}
((mhmakefileparser*)this)->m_UsedEnvVars.insert(Var);
if (Cache)
((mhmakefileparser*)this)->m_Variables[Var]=pEnv;
return pEnv;
}
///////////////////////////////////////////////////////////////////////////////
// Creates the variable USER_ENVVARS to be saved in the autodeps file
//
void mhmakefileparser::CreateUSED_ENVVARS()
{
set<string> Variables;
set<string>::const_iterator It=m_UsedEnvVars.begin();
set<string>::const_iterator ItEnd=m_UsedEnvVars.end();
while (It!=ItEnd)
{
string Var=*It;
if (!SkipVar(Var))
{
transform(Var.begin(),Var.end(),Var.begin(),(int(__CDECL *)(int))toupper);
Variables.insert(Var);
}
It++;
}
It=m_Exports.begin();
ItEnd=m_Exports.end();
while (It!=ItEnd)
{
string Var=*It;
if (!SkipVar(Var))
{
transform(Var.begin(),Var.end(),Var.begin(),(int(__CDECL *)(int))toupper);
Variables.insert(Var);
}
It++;
}
map<string,string>::const_iterator CLItEnd=loadedmakefile::sm_Statics.m_GlobalCommandLineVars.end();
map<string,string>::const_iterator CLIt=loadedmakefile::sm_Statics.m_GlobalCommandLineVars.begin();
while (CLIt!=CLItEnd)
{
string Var=CLIt->first;
if (!SkipVar(Var))
{
transform(Var.begin(),Var.end(),Var.begin(),(int(__CDECL *)(int))toupper);
Variables.insert(Var);
}
CLIt++;
}
It=Variables.begin();
ItEnd=Variables.end();
string Val;
while (It!=ItEnd)
{
Val+=*It;
Val+=";";
It++;
}
m_Variables[USED_ENVVARS]=Val;
}
static string s_TrueString("1");
static string s_FalseString("0");
static string AndExpression(const string &First, const string &Second)
{
if (First.empty() || First==s_FalseString)
{
return g_EmptyString;
}
else
{
return Second.empty() || Second==s_FalseString ? g_EmptyString : s_TrueString;
}
}
static string OrExpression(const string &First, const string &Second)
{
if (First.empty() || First==s_FalseString)
{
return Second.empty() || Second==s_FalseString ? g_EmptyString : s_TrueString;
}
else
{
return s_TrueString;
}
}
string mhmakefileparser::ResolveExpression(const string &InExpr,string &Rest) const
{
unsigned i=0;
string Ret;
string Expr=InExpr;
Rest=g_EmptyString;
while (i<Expr.length())
{
while (strchr(" \t\r",Expr[i])) i++;
switch (Expr[i])
{
case '!':
if (Expr[i+1]!='=')
{
Ret=ResolveExpression(Expr.substr(i+1),Expr);
Ret = Ret.empty() || Ret==s_FalseString ? s_TrueString : g_EmptyString;
i=0;
}
else
{
Ret = Ret!=ResolveExpression(Expr.substr(i+2),Expr) ? s_TrueString : g_EmptyString;
i=0;
}
break;
case '&':
if (Expr[i+1]!='&')
{
Ret+=Expr[i++];
}
else
{
Ret=AndExpression(Ret,ResolveExpression(Expr.substr(i+2),Expr));
i=0;
}
break;
case '|':
if (Expr[i+1]!='|')
{
Ret+=Expr[i++];
}
else
{
Ret=OrExpression(Ret,ResolveExpression(Expr.substr(i+2),Expr));
i=0;
}
break;
case '(':
if (Ret=="defined")
{
Ret=IsDefined(ResolveExpression(Expr.substr(i+1),Expr)) ? s_TrueString : g_EmptyString;
}
else
{
Ret+=ResolveExpression(Expr.substr(i+1),Expr);
}
i=0;
break;
case ')':
Rest=Expr.substr(i+1);
return Ret;
break;
case '"':
{
i++;
while (i<Expr.length())
{
char Char=Expr[i++];
if (Char=='"')
break;
Ret+=Char;
}
}
break;
case '=':
if (Expr[i+1]!='=')
{
Ret+=Expr[i++];
}
else
{
Ret = Ret==ResolveExpression(Expr.substr(i+2),Expr) ? s_TrueString : g_EmptyString;
i=0;
}
break;
default:
Ret+=Expr[i++];
break;
}
}
return Ret;
}
///////////////////////////////////////////////////////////////////////////////
bool mhmakefileparser::IsExprTrue(const string &EqualExpr) const
{
string Expr=ExpandExpression(EqualExpr);
string Rest;
Expr=ResolveExpression(Expr,Rest);
if (Expr.empty() || Expr==s_FalseString)
return false;
else
return true;
}
|