Code

moving trunk for module inkscape
[inkscape.git] / src / mkfiles.pl
1 #!/usr/bin/perl
2 ############################################################################
3 #
4 # Creates make.files - a list of all source files
5 #
6 ############################################################################
8 #
9 # main - top level code
10 #
12 if ( @ARGV ) {                # parse command line args
13     print "usage: perl mkfiles.pl\n\n";
14     exit 1;
15 }
17 $tolower = 0;  #Do we want to convert items in make.files to lower case?
18 $verbose = 1;  #Do we want to list the files?
22 print "###########################\n";
23 print "## Generating make.files ##\n";
24 print "###########################\n";
26 &doMakeFiles();  #Do your magic!
28 print "###########################\n";
29 print "## make.files   done     ##\n";
30 print "###########################\n";
32 exit 0;
36 ############################################################################
37 #
38 # Main routine.  Read make.exclude,  then search the current directory
39 # recursively for source code items, rejecting those listed in make.exclude
40 #
41 ############################################################################
42 sub doMakeFiles
43 {
44     my $excludes;   #list of files to exclude
45     my @excluded;   #list of files that were actually excluded
46     my @irrelevant; #list of files in make.exclude that were not found
47     my $line;       #current line from make.exclude
48     my $patterns;   #The extensions for which to search
49     my @files;      #Result of find_files()
50     my @codefiles;  #Sorted list of @files
51     my $datestr;    #Current date
52     local(*MAKE_DOT_EXCLUDE);
53     local(*MAKE_DOT_FILES);
55     if ( -r "make.exclude" )
56         {
57         open MAKE_DOT_EXCLUDE, "make.exclude" or
58             die "make.exclude: $!";
59         while (<MAKE_DOT_EXCLUDE>)
60             {
61             $line = $_;
62             #Trim whitespace from ends
63             $line =~ s/^\s|\t|\n//;
64             #Ignore if no text in line
65             next if ( length($line) < 1 );
66             #Ignore if starts with '#'
67             next if ( $line =~ /^#/ );
68             # print $line, "\n";
69             $excludes->{$line} = 1;
70             }
71         close MAKE_DOT_EXCLUDE;
72         }
73     
74     #while ( my ($key, $value) = each(%{$excludes}) )
75     #    {
76     #    print "$key => $value\n";
77     #    }
78     
79     $patterns = '.c$|.cpp$|.h$|.hpp$';
80     
81     @files = &find_files(".", $patterns, $excludes, \@excluded);
82     
83     # Print the list of files excluded
84     print "###EXCLUDED: ", scalar(@excluded), " files/directories rejected by request\n";
85     @excluded = sort(@excluded);
86     foreach (@excluded)
87         {
88         if ($verbose)
89           {
90           print "    $_\n";
91           }
92         #mark the item in the hash table , so we know it occurred
93         $excludes->{$_} = 0;
94         }
96     # Count how many files in make.exclude were not used
97     foreach (keys(%{$excludes}))
98         {
99         #if still a 1, then it was not used
100         if ($excludes->{$_} !=0)
101             {
102             push @irrelevant, $_;
103             }
104         }
105  
106     # Print the list of file in make.exclude, but not used.
107     if (scalar(@irrelevant)>0)
108         {
109         @irrelevant = sort(@irrelevant);
110         print "###IRRELEVANT: ", scalar(@irrelevant), " entries in make.exclude that were not found\n";
111         foreach (@irrelevant)
112             {
113             if ($verbose)
114               {
115               print "    $_\n";
116               }
117             }
118         }
120     @codefiles = sort(@files);
121     
122     $datestr = gmtime();
123     open MAKE_DOT_FILES, ">make.files" or
124         die("make.files: $!");
125     print MAKE_DOT_FILES "########################################################\n";
126     print MAKE_DOT_FILES "## File: make.files\n";
127     print MAKE_DOT_FILES "## Purpose: Used by mkdep.pl\n";
128     print MAKE_DOT_FILES "## Generated by mkfiles.pl at :$datestr\n";
129     print MAKE_DOT_FILES "########################################################\n\n";
130     foreach (@codefiles)
131         {
132         next if (length($_)<1);
133         print MAKE_DOT_FILES "$_\n";
134         }
135     close MAKE_DOT_FILES;
136     
137 } # doMakeFiles
138     
142 ############################################################################
144 # Search the current directory recursively,  checking rejecting items from
145 # make.exclude, or adding those that match the extensions in $patterns.
146 # @param $dir      the current directory being searched
147 # @param $patterns the file extensions for which we are searching
148 # @param $excludes a hash of the files which should be rejected
149 # @param $excluded an array in which to store a record of all files rejected
151 ############################################################################
152 sub find_files
154     my($dir, $patterns, $excludes, $excluded) = @_;
155     my $file;
156     my $p;
157     my @files = ();
158     my @file_result;
159     local(*DIR);
160     
161     
162     $dir =~ s=\\=/=g;
163     
164     # Check for 0-length
165     if (length($dir)<1)
166         {
167         $dir = '.';
168         }
169     
170     # Process a directory of files
171     if ( opendir(DIR,$dir) )
172         {
173         # Remove leading .
174         if ( $dir eq '.' )
175             {
176             $dir = '';
177             }
179         foreach $file ( readdir(DIR) )
180             {
181             # Don't allow '..'
182             next if ( $file  =~ /^\.\.?$/ );
183             next if ( length($file)<1 );
185             if (length($dir)>0)
186                 {
187                 $p = $dir . '/' . $file;
188                 }
189             else
190                 {
191                 $p = $file;
192                 }
193                 
194             if ($tolower != 0)
195                 {
196                 $p = lc($p);
197                 }
199             #print "File:$p\n";
200             
201             # Check if it is an excluded file
202             if (defined($excludes->{$p}))
203                 {
204                 #print "EXCLUDED:$p\n";
205                 push @{$excluded}, $p;
206                 next;
207                 }
208             # Check for a desired extension
209             if ($p =~ /$patterns/i)
210                 {
211                 #print "match:$p\n";
212                 push @files, $p;
213                 next;
214                 }
215              #We have seen '0' inserted from a null result.  This
216              #should avoid this problem
217              @file_result = &find_files($p, $patterns, $excludes, $excluded);
218              if (scalar(@file_result)>0)
219                  {
220                  push @files, @file_result;
221                  }
222              }
223         closedir(DIR);
224         }
225     return @files;