forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmakedep.cpp
887 lines (764 loc) · 26.6 KB
/
makedep.cpp
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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: NPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Netscape Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the NPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the NPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
// Dependency building hack
//
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <ctype.h>
#include <afxcoll.h>
#include <afxtempl.h>
int mainReturn = 0;
BOOL b16 = FALSE;
BOOL bSimple = FALSE;
// freopen won't work on stdout in win16
FILE *pAltFile = stdout;
CStringArray includeDirectories;
// turn a file, relative path or other into an absolute path
// This function copied from MFC 1.52
BOOL PASCAL _AfxFullPath(LPSTR lpszPathOut, LPCSTR lpszFileIn)
// lpszPathOut = buffer of _MAX_PATH
// lpszFileIn = file, relative path or absolute path
// (both in ANSI character set)
{
OFSTRUCT of;
if (OpenFile(lpszFileIn, &of, OF_PARSE) != HFILE_ERROR)
{
// of.szPathName is in the OEM character set
OemToAnsi(of.szPathName, lpszPathOut);
AnsiUpper(lpszPathOut); // paths in upper case just to be sure
return TRUE;
}
else
{
TRACE1("Warning: could not parse the path %Fs\n", lpszFileIn);
lstrcpy(lpszPathOut, lpszFileIn); // take it literally
AnsiUpper(lpszPathOut); // paths in upper case just to be sure
return FALSE;
}
}
void AddIncludeDirectory( char *pString ){
CString s = pString;
int len = s.GetLength();
if(len > 0 && s[len - 1] != '\\' ){
s += "\\";
}
includeDirectories.Add(s);
}
BOOL FileExists( const char *pString ){
struct _stat buf;
int result;
result = _stat( pString, &buf );
return (result == 0);
}
void FixPathName(CString& str) {
str.MakeUpper(); // all upper case
// now switch all forward slashes to back slashes
int index;
while ((index = str.Find('/')) != -1) {
str.SetAt(index, '\\');
}
}
void FATName(CString& csLONGName)
{
// Only relevant for 16 bits.
if(b16) {
// Convert filename to FAT (8.3) equivalent.
char aBuffer[2048];
if(GetShortPathName(csLONGName, aBuffer, 2048)) {
csLONGName = aBuffer;
}
}
}
class CFileRecord {
public:
CString m_shortName;
CString m_pathName;
CPtrArray m_includes; // pointers to CFileRecords in fileMap
BOOL m_bVisited;
BOOL m_bSystem;
BOOL m_bSource;
static CMapStringToPtr fileMap; // contains all allocated CFileRecords
static CStringArray orderedFileNames;
static CMapStringToPtr includeMap; // pointer to CFileRecords in fileMap
static CMapStringToPtr noDependMap;
CFileRecord( const char *shortName, const char* pFullName, BOOL bSystem, BOOL bSource):
m_shortName( shortName ),
m_pathName(),
m_includes(),
m_bVisited(FALSE),
m_bSource( bSource ),
m_bSystem(bSystem){
m_pathName = pFullName;
FixPathName(m_pathName); // all upper case for consistency
ASSERT(FindFileRecord(m_pathName) == NULL); // make sure it's not already in the map
fileMap[m_pathName] = this; // add this to the filemap, using the appropriate name as the key
if (bSource) {
orderedFileNames.Add(m_pathName); // remember the order we saw source files in
}
}
//
// open the file and grab all the includes.
//
void ProcessFile(){
FILE *f;
CString fullName;
BOOL bSystem;
DWORD lineCntr = 0;
char *a = new char[2048];
memset(a, 0, 2048);
char srcPath[_MAX_PATH];
// construct the full path
if (!_AfxFullPath(srcPath, m_pathName)) {
strcpy(srcPath, m_pathName);
}
// strip off the source filename to end up with just the path
LPSTR pTemp = strrchr(srcPath, '\\');
if (pTemp) {
*(pTemp + 1) = 0;
}
f = fopen(m_pathName, "r");
if(f != NULL && f != (FILE *)-1) {
setvbuf(f, NULL, _IOFBF, 32768); // use a large file buffer
while(fgets(a, 2047, f)) {
// if the string "//{{NO_DEPENDENCIES}}" is at the start of one of the
// first 10 lines of a file, don't build dependencies on it or any
// of the files it includes
if (lineCntr < 10) {
static char* pDependStr = "//{{NO_DEPENDENCIES}}";
if (strncmp(a, pDependStr, strlen(pDependStr)) == 0) {
noDependMap[m_pathName] = 0; // add it to the noDependMap
break; // no need for further processing of this file
}
}
++lineCntr;
// have to handle a variety of legal syntaxes that we find in our source files:
// #include
// # include
// #include
// if the first non-whitespace char is a '#', consider this line
pTemp = a;
pTemp += strspn(pTemp, " \t"); // skip whitespace
if (*pTemp == '#') {
++pTemp; // skip the '#'
pTemp += strspn(pTemp, " \t"); // skip more whitespace
if( !strncmp(pTemp, "include", 7) ){
pTemp += 7; // skip the "include"
pTemp += strspn(pTemp, " \t"); // skip more whitespace
bSystem = (*pTemp == '<'); // mark if it's a system include or not
// forget system files -- we just have to search all the paths
// every time and never find them! This change alone speeds a full
// depend run on my system from 5 minutes to 3:15
// if (bSystem || (*pTemp == '"')) {
if (*pTemp == '"') {
LPSTR pStart = pTemp + 1; // mark the start of the string
pTemp = pStart + strcspn(pStart, ">\" "); // find the end of the string
*pTemp = 0; // terminate the string
// construct the full pathname from the path part of the
// source file and the name listed here
fullName = srcPath;
fullName += pStart;
CFileRecord *pAddMe = AddFile( pStart, fullName, bSystem );
if (pAddMe) {
m_includes.Add(pAddMe);
}
}
}
}
}
fclose(f);
}
delete [] a;
}
void PrintIncludes(){
int i = 0;
while( i < m_includes.GetSize() ){
CFileRecord *pRec = (CFileRecord*) m_includes[i];
// Don't write out files that don't exist or are not in the namespace
// of the programs using it (netscape_AppletMozillaContext.h doesn't
// mix well with 16 bits).
// Also don't write out files that are in the noDependMap
void* lookupJunk;
if( !pRec->m_bVisited && pRec->m_pathName.GetLength() != 0 && !noDependMap.Lookup(pRec->m_pathName, lookupJunk)) {
// not supposed to have a file in the list that doesn't exist
ASSERT(FileExists(pRec->m_pathName));
CString csOutput;
csOutput = pRec->m_pathName;
FATName(csOutput);
fprintf(pAltFile, "\\\n %s ", (const char *) csOutput );
// mark this one as done so we don't do it more than once
pRec->m_bVisited = TRUE;
pRec->PrintIncludes();
}
i++;
}
}
void PrintDepend(){
CFileRecord *pRec;
BOOL bFound;
POSITION next;
CString name;
// clear all the m_bVisisted flags so we can use it to keep track
// of whether we've already output this file as a dependency
next = fileMap.GetStartPosition();
while( next ){
fileMap.GetNextAssoc( next, name, *(void**)&pRec );
pRec->m_bVisited = FALSE;
}
char fname[_MAX_FNAME];
if (pRec->m_pathName.GetLength() != 0) {
if( bSimple ){
fprintf(pAltFile, "\n\n\n%s:\t", m_pathName );
}
else {
CString csOutput;
csOutput = m_pathName;
FATName(csOutput);
_splitpath( csOutput, NULL, NULL, fname, NULL );
fprintf(pAltFile, "\n\n\n$(OUTDIR)\\%s.obj: %s ", fname, (const char*) csOutput );
}
m_bVisited = TRUE; // mark it as done so we won't do it again
PrintIncludes();
}
}
static CString NormalizeFileName( const char* pName ){
return CString(pName);
}
static CFileRecord* FindFileRecord( const char *pName ){
CFileRecord* pRec = NULL;
CString name(pName);
FixPathName(name);
fileMap.Lookup(name, (void*&)pRec);
return(pRec);
}
public:
static CFileRecord* AddFile( const char* pShortName, const char* pFullName, BOOL bSystem = FALSE,
BOOL bSource = FALSE ){
char fullName[_MAX_PATH];
BOOL bFound = FALSE;
CString foundName;
CString fixedShortName;
CString s;
// normalize the name
fixedShortName = pShortName;
FixPathName(fixedShortName);
pShortName = fixedShortName;
// if it is source, we might be getting an obj file. If we do,
// convert it to a c or c++ file.
if( bSource && (strcmp(GetExt(pShortName),".obj") == 0) ){
char path_buffer[_MAX_PATH];
char fname[_MAX_FNAME] = "";
CString s;
_splitpath( pShortName, NULL, NULL, fname, NULL );
if( FileExists( s = CString(fname) + ".cpp") ){
pShortName = s;
pFullName = s;
}
else if( FileExists( s = CString(fname) + ".c" ) ){
pShortName = s;
pFullName = s;
}
else {
return 0;
}
}
// if pFullName was not constructed, construct it here based on the current directory
if (!pFullName) {
_AfxFullPath(fullName, pShortName);
pFullName = fullName;
}
// first check to see if we already have this exact file
CFileRecord *pRec = FindFileRecord(pFullName);
// if not found and not a source file check the header list --
// all files we've found in include directories are in the includeMap.
// we can save gobs of time by getting it from there
if (!pRec && !bSource)
includeMap.Lookup(fixedShortName, (void*&)pRec);
if (!pRec) {
// not in one of our lists, start scrounging on disk
// check the fullname first
if (FileExists(pFullName)) {
foundName = pFullName;
bFound = TRUE;
}
else {
// if still not found, search the include paths
int i = 0;
while( i < includeDirectories.GetSize() ){
if( FileExists( includeDirectories[i] + pShortName ) ){
foundName = includeDirectories[i] + pShortName;
bFound = TRUE;
break;
}
i++;
}
}
}
else {
// we found it
bFound = TRUE;
}
// source files are not allowed to be missing
if (bSource && !pRec && !bFound) {
fprintf(stderr, "Source file: %s doesn't exist\n", pFullName);
mainReturn = -1; // exit with an error, don't write out the results
}
#ifdef _DEBUG
if (!pRec && !bFound && !bSystem) {
fprintf(stderr, "Header not found: %s (%s)\n", pShortName, pFullName);
}
#endif
// if none of the above logic found it already in the list,
// must be a new file, add it to the list
if (bFound && (pRec == NULL)) {
pRec = new CFileRecord( pShortName, foundName, bSystem, bSource);
// if this one isn't a source file add it to the includeMap
// for performance reasons (so we can find it there next time rather
// than having to search the file system again)
if (!bSource) {
includeMap[pShortName] = pRec;
}
}
return pRec;
}
static void PrintDependancies(){
CFileRecord *pRec;
BOOL bFound;
POSITION next;
CString name;
// use orderedFileNames to preserve order
for (int pos = 0; pos < orderedFileNames.GetSize(); pos++) {
pRec = FindFileRecord(orderedFileNames[pos]);
if(pRec && pRec->m_bSource ){
pRec->PrintDepend();
}
}
}
void PrintDepend2(){
CFileRecord *pRec;
int i;
if( m_includes.GetSize() != 0 ){
fprintf(pAltFile, "\n\n\n%s: \\\n",m_pathName );
i = 0;
while( i < m_includes.GetSize() ){
pRec = (CFileRecord*) m_includes[i];
fprintf(pAltFile, "\t\t\t%s\t\\\n",pRec->m_pathName );
i++;
}
}
}
static void PrintDependancies2(){
CFileRecord *pRec;
BOOL bFound;
POSITION next;
CString name;
next = fileMap.GetStartPosition();
while( next ){
fileMap.GetNextAssoc( next, name, *(void**)&pRec );
pRec->PrintDepend2();
}
}
static void PrintTargets(const char *pMacroName, const char *pDelimeter){
CFileRecord *pRec;
BOOL bFound;
POSITION next;
CString name;
BOOL bNeedDelimeter = FALSE;
fprintf(pAltFile, "%s = ", pMacroName);
// use orderedFileNames to preserve target order
for (int pos = 0; pos < orderedFileNames.GetSize(); pos++) {
pRec = FindFileRecord(orderedFileNames[pos]);
ASSERT(pRec);
if( pRec && pRec->m_bSource && pRec->m_pathName.GetLength() != 0){
char fname[_MAX_FNAME];
CString csOutput;
csOutput = pRec->m_pathName;
FATName(csOutput);
_splitpath( csOutput, NULL, NULL, fname, NULL );
if(bNeedDelimeter) {
fprintf(pAltFile, "%s\n", pDelimeter);
bNeedDelimeter = FALSE;
}
fprintf(pAltFile, " $(OUTDIR)\\%s.obj ", fname );
bNeedDelimeter = TRUE;
}
}
fprintf(pAltFile, "\n\n\n");
}
static CString DirDefine( const char *pPath ){
char path_buffer[_MAX_PATH];
char dir[_MAX_DIR] = "";
char dir2[_MAX_DIR] = "";
char fname[_MAX_FNAME] = "";
char ext[_MAX_EXT] = "";
CString s;
_splitpath( pPath, 0, dir, 0, ext );
BOOL bDone = FALSE;
while( dir && !bDone){
// remove the trailing slash
dir[ strlen(dir)-1] = 0;
_splitpath( dir, 0, dir2, fname, 0 );
if( strcmp( fname, "SRC" ) == 0 ){
strcpy( dir, dir2 );
}
else {
bDone = TRUE;
}
}
s = CString(fname) + "_" + (ext+1);
return s;
}
static void PrintSources(){
int i;
CString dirName, newDirName;
for( i=0; i< orderedFileNames.GetSize(); i++ ){
newDirName= DirDefine( orderedFileNames[i] );
if( newDirName != dirName ){
fprintf( pAltFile, "\n\n\nFILES_%s= $(FILES_%s) \\",
(const char*)newDirName, (const char*)newDirName );
dirName = newDirName;
}
fprintf( pAltFile, "\n\t%s^", (const char*)orderedFileNames[i] );
}
}
static CString SourceDirName( const char *pPath, BOOL bFileName){
char path_buffer[_MAX_PATH];
char drive[_MAX_DRIVE] = "";
char dir[_MAX_DIR] = "";
char fname[_MAX_FNAME] = "";
char ext[_MAX_EXT] = "";
CString s;
_splitpath( pPath, drive, dir, fname, ext );
s = CString(drive) + dir;
if( bFileName ){
s += CString("FNAME") + ext;
}
else {
// remove the trailing slash
s = s.Left( s.GetLength() - 1 );
}
return s;
}
static CString GetExt( const char *pPath){
char ext[_MAX_EXT] = "";
_splitpath( pPath, 0,0,0, ext );
CString s = CString(ext);
s.MakeLower();
return s;
}
static void PrintBuildRules(){
int i;
CString dirName;
CMapStringToPtr dirList;
for( i=0; i< orderedFileNames.GetSize(); i++ ){
dirList[ SourceDirName(orderedFileNames[i], TRUE) ]= 0;
}
POSITION next;
CString name;
void *pVal;
next = dirList.GetStartPosition();
while( next ){
dirList.GetNextAssoc( next, name, pVal);
CString dirDefine = DirDefine( name );
CString ext = GetExt( name );
name = SourceDirName( name, FALSE );
CString response = dirDefine.Left(8);
fprintf( pAltFile,
"\n\n\n{%s}%s{$(OUTDIR)}.obj:\n"
"\t@rem <<$(OUTDIR)\\%s.cl\n"
"\t$(CFILEFLAGS)\n"
"\t$(CFLAGS_%s)\n"
"<<KEEP\n"
"\t$(CPP) @$(OUTDIR)\\%s.cl %%s\n",
(const char*)name,
(const char*)ext,
(const char*)response,
(const char*)dirDefine,
(const char*)response
);
fprintf( pAltFile,
"\n\n\nBATCH_%s:\n"
"\t@rem <<$(OUTDIR)\\%s.cl\n"
"\t$(CFILEFLAGS)\n"
"\t$(CFLAGS_%s)\n"
"\t$(FILES_%s)\n"
"<<KEEP\n"
"\t$(TIMESTART)\n"
"\t$(CPP) @$(OUTDIR)\\%s.cl\n"
"\t$(TIMESTOP)\n",
(const char*)dirDefine,
(const char*)response,
(const char*)dirDefine,
(const char*)dirDefine,
(const char*)response
);
}
//
// Loop through one more time and build the final batch build
// rule
//
fprintf( pAltFile,
"\n\n\nBATCH_BUILD_OBJECTS:\t\t\\\n");
next = dirList.GetStartPosition();
while( next ){
dirList.GetNextAssoc( next, name, pVal);
CString dirDefine = DirDefine( name );
fprintf( pAltFile,
"\tBATCH_%s\t\t\\\n", dirDefine );
}
fprintf( pAltFile,
"\n\n");
}
static void ProcessFiles(){
CFileRecord *pRec;
BOOL bFound;
POSITION next;
CString name;
// search all the files for headers, adding each one to the list when found
// rather than do it recursively, it simple marks each one it's done
// and starts over, stopping only when all are marked as done
next = fileMap.GetStartPosition();
while( next ){
fileMap.GetNextAssoc( next, name, *(void**)&pRec );
if( pRec->m_bVisited == FALSE && pRec->m_bSystem == FALSE ){
// mark this file as already done so we don't read it again
// to find its headers
pRec->m_bVisited = TRUE;
pRec->ProcessFile();
// Start searching from the beginning again
// because ProcessFile may have added new files
// and changed the GetNextAssoc order
next = fileMap.GetStartPosition();
}
}
}
};
CMapStringToPtr CFileRecord::fileMap; // contains all allocated CFileRecords
CStringArray CFileRecord::orderedFileNames;
CMapStringToPtr CFileRecord::includeMap; // pointers to CFileRecords in fileMap
CMapStringToPtr CFileRecord::noDependMap; // no data, just an index
int main( int argc, char** argv ){
int i = 1;
char *pStr;
static int iRecursion = 0; // Track levels of recursion.
static CString outputFileName;
// Entering.
iRecursion++;
while( i < argc ){
if( argv[i][0] == '-' || argv[i][0] == '/' ){
switch( argv[i][1] ){
case 'i':
case 'I':
if( argv[i][2] != 0 ){
pStr = &(argv[i][2]);
}
else {
i++;
pStr = argv[i];
}
if( pStr == 0 || *pStr == '-' || *pStr == '/' ){
goto usage;
}
else {
AddIncludeDirectory( pStr );
}
break;
case 'f':
case 'F':
if( argv[i][2] != 0 ){
pStr = &(argv[i][2]);
}
else {
i++;
pStr = argv[i];
}
if( pStr == 0 || *pStr == '-' || *pStr == '/'){
goto usage;
}
else {
CStdioFile f;
CString s;
if( f.Open( pStr, CFile::modeRead ) ){
while(f.ReadString(s)){
s.TrimLeft();
s.TrimRight();
if( s.GetLength() ){
CFileRecord::AddFile( s, NULL, FALSE, TRUE );
}
}
f.Close();
}
else {
fprintf(stderr,"makedep: file not found: %s", pStr );
exit(-1);
}
}
break;
case 'o':
case 'O':
if( argv[i][2] != 0 ){
pStr = &(argv[i][2]);
}
else {
i++;
pStr = argv[i];
}
if( pStr == 0 || *pStr == '-' || *pStr == '/'){
goto usage;
}
else {
CStdioFile f;
CString s;
outputFileName = pStr;
if(!(pAltFile = fopen(pStr, "w+"))) {
fprintf(stderr, "makedep: file not found: %s", pStr );
exit(-1);
}
}
break;
case '1':
if( argv[i][2] == '6') {
b16 = TRUE;
}
break;
case 's':
case 'S':
bSimple = TRUE;
break;
case 'h':
case 'H':
case '?':
usage:
fprintf(stderr, "usage: makedep -I <dirname> -F <filelist> <filename>\n"
" -I <dirname> Directory name, can be repeated\n"
" -F <filelist> List of files to scan, one per line\n"
" -O <outputFile> File to write output, default stdout\n");
exit(-1);
}
}
else if( argv[i][0] == '@' ){
// file contains our commands.
CStdioFile f;
CString s;
int iNewArgc = 0;
char **apNewArgv = new char*[5000];
memset(apNewArgv, 0, sizeof(apNewArgv));
// First one is always the name of the exe.
apNewArgv[0] = argv[0];
iNewArgc++;
const char *pTraverse;
const char *pBeginArg;
if( f.Open( &argv[i][1], CFile::modeRead ) ){
while( iNewArgc < 5000 && f.ReadString(s) ) {
// Scan the string for args, and do the right thing.
pTraverse = (const char *)s;
while(iNewArgc < 5000 && *pTraverse) {
if(isspace(*pTraverse)) {
pTraverse++;
continue;
}
// Extract to next space.
pBeginArg = pTraverse;
do {
pTraverse++;
}
while(*pTraverse && !isspace(*pTraverse));
apNewArgv[iNewArgc] = new char[pTraverse - pBeginArg + 1];
memset(apNewArgv[iNewArgc], 0, pTraverse - pBeginArg + 1);
strncpy(apNewArgv[iNewArgc], pBeginArg, pTraverse - pBeginArg);
iNewArgc++;
}
}
f.Close();
}
// Recurse if needed.
if(iNewArgc > 1) {
main(iNewArgc, apNewArgv);
}
// Free off the argvs (but not the very first one which we didn't allocate).
while(iNewArgc > 1) {
iNewArgc--;
delete [] apNewArgv[iNewArgc];
}
delete [] apNewArgv;
}
else {
CFileRecord::AddFile( argv[i], NULL, FALSE, TRUE );
}
i++;
}
// Only of the very bottom level of recursion do we do this.
if(iRecursion == 1) {
// only write the results out if no errors encountered
if (mainReturn == 0) {
CFileRecord::ProcessFiles();
if( !bSimple ){
CFileRecord::PrintTargets("OBJ_FILES", "\\");
if(b16) {
CFileRecord::PrintTargets("LINK_OBJS", "+\\");
}
else {
CFileRecord::PrintTargets("LINK_OBJS", "^");
}
CFileRecord::PrintSources();
CFileRecord::PrintBuildRules();
}
CFileRecord::PrintDependancies();
}
if(pAltFile != stdout) {
fclose(pAltFile);
if (mainReturn != 0) {
remove(outputFileName); // kill output file if returning an error
}
}
}
iRecursion--;
if (iRecursion == 0 )
{
// last time through -- clean up allocated CFileRecords!
CFileRecord *pFRec;
CString name;
POSITION next;
next = CFileRecord::fileMap.GetStartPosition();
while( next ){
CFileRecord::fileMap.GetNextAssoc( next, name, *(void**)&pFRec );
delete pFRec;
}
}
return mainReturn;
}