summaryrefslogtreecommitdiffstats
path: root/src/mkfiles.pl
blob: 100e54b5be40e15eb2c035d156a6d01da96b9fce (plain)
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
#!/usr/bin/perl
############################################################################
#
# Creates make.files - a list of all source files
#
############################################################################

#
# main - top level code
#

if ( @ARGV ) {                # parse command line args
    print "usage: perl mkfiles.pl\n\n";
    exit 1;
}

$tolower = 0;  #Do we want to convert items in make.files to lower case?
$verbose = 1;  #Do we want to list the files?



print "###########################\n";
print "## Generating make.files ##\n";
print "###########################\n";

&doMakeFiles();  #Do your magic!

print "###########################\n";
print "## make.files   done     ##\n";
print "###########################\n";

exit 0;



############################################################################
#
# Main routine.  Read make.exclude,  then search the current directory
# recursively for source code items, rejecting those listed in make.exclude
#
############################################################################
sub doMakeFiles
{
    my $excludes;   #list of files to exclude
    my @excluded;   #list of files that were actually excluded
    my @irrelevant; #list of files in make.exclude that were not found
    my $line;       #current line from make.exclude
    my $patterns;   #The extensions for which to search
    my @files;      #Result of find_files()
    my @codefiles;  #Sorted list of @files
    my $datestr;    #Current date
    local(*MAKE_DOT_EXCLUDE);
    local(*MAKE_DOT_FILES);

    if ( -r "make.exclude" )
        {
        open MAKE_DOT_EXCLUDE, "make.exclude" or
            die "make.exclude: $!";
        while (<MAKE_DOT_EXCLUDE>)
            {
            $line = $_;
            #Trim whitespace from ends
            # $line =~ s/^\s|\t|\n//;
+           $line =~ s/^\s+//;
+           $line =~ s/\s+$//;
            #Ignore if no text in line
            next if ( length($line) < 1 );
            #Ignore if starts with '#'
            next if ( $line =~ /^#/ );
            # print $line, "\n";
            $excludes->{$line} = 1;
            }
        close MAKE_DOT_EXCLUDE;
        }
    
    #while ( my ($key, $value) = each(%{$excludes}) )
    #    {
    #    print "$key => $value\n";
    #    }
    
    $patterns = '.c$|.cpp$|.h$|.hpp$|.xpm$';
    
    @files = &find_files(".", $patterns, $excludes, \@excluded);
    
    # Print the list of files excluded
    print "###EXCLUDED: ", scalar(@excluded), " files/directories rejected by request\n";
    @excluded = sort(@excluded);
    foreach (@excluded)
        {
        if ($verbose)
          {
          print "    $_\n";
          }
        #mark the item in the hash table , so we know it occurred
        $excludes->{$_} = 0;
        }

    # Count how many files in make.exclude were not used
    foreach (keys(%{$excludes}))
        {
        #if still a 1, then it was not used
        if ($excludes->{$_} !=0)
            {
            push @irrelevant, $_;
            }
        }
 
    # Print the list of file in make.exclude, but not used.
    if (scalar(@irrelevant)>0)
        {
        @irrelevant = sort(@irrelevant);
        print "###IRRELEVANT: ", scalar(@irrelevant), " entries in make.exclude that were not found\n";
        foreach (@irrelevant)
            {
            if ($verbose)
              {
              print "    $_\n";
              }
            }
        }

    @codefiles = sort(@files);
    
    $datestr = gmtime();
    open MAKE_DOT_FILES, ">make.files" or
        die("make.files: $!");
    print MAKE_DOT_FILES "########################################################\n";
    print MAKE_DOT_FILES "## File: make.files\n";
    print MAKE_DOT_FILES "## Purpose: Used by mkdep.pl\n";
    print MAKE_DOT_FILES "## Generated by mkfiles.pl at :$datestr\n";
    print MAKE_DOT_FILES "########################################################\n\n";
    foreach (@codefiles)
        {
        next if (length($_)<1);
        print MAKE_DOT_FILES "$_\n";
        }
    close MAKE_DOT_FILES;
    
} # doMakeFiles
    



############################################################################
#
# Search the current directory recursively,  checking rejecting items from
# make.exclude, or adding those that match the extensions in $patterns.
# @param $dir      the current directory being searched
# @param $patterns the file extensions for which we are searching
# @param $excludes a hash of the files which should be rejected
# @param $excluded an array in which to store a record of all files rejected
#
############################################################################
sub find_files
{
    my($dir, $patterns, $excludes, $excluded) = @_;
    my $file;
    my $p;
    my @files = ();
    my @file_result;
    local(*DIR);
    
    
    $dir =~ s=\\=/=g;
    
    # Check for 0-length
    if (length($dir)<1)
        {
        $dir = '.';
        }
    
    # Process a directory of files
    if ( opendir(DIR,$dir) )
        {
        # Remove leading .
        if ( $dir eq '.' )
            {
            $dir = '';
            }

        foreach $file ( readdir(DIR) )
            {
            # Don't allow '..'
            next if ( $file  =~ /^\.\.?$/ );
            next if ( length($file)<1 );

            if (length($dir)>0)
                {
                $p = $dir . '/' . $file;
                }
            else
                {
                $p = $file;
                }
                
            if ($tolower != 0)
                {
                $p = lc($p);
                }

            #print "File:$p\n";
            
            # Check if it is an excluded file
            if (defined($excludes->{$p}))
                {
                #print "EXCLUDED:$p\n";
                push @{$excluded}, $p;
                next;
                }
            # Check for a desired extension
            if ($p =~ /$patterns/i)
                {
                #print "match:$p\n";
                push @files, $p;
                next;
                }
             #We have seen '0' inserted from a null result.  This
             #should avoid this problem
             @file_result = &find_files($p, $patterns, $excludes, $excluded);
             if (scalar(@file_result)>0)
                 {
                 push @files, @file_result;
                 }
             }
        closedir(DIR);
        }
    return @files;
}