fb8295d755ee1603ed3410be7ba2a24f000ac9bf
[openafs.git] / src / config / make_libafs_tree.pl
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4
5 $| = 1;
6 #
7 # Build the libafs_tree by reading component list files in the src dir, and copying the
8 # listed files from the src and obj dirs into the libafs_tree directory. 
9 #
10 # Dependent components are listed in file named "libafsdep" in each dir with dependencies.
11 #
12 use File::Find;
13 use File::Path;
14
15 my $quiet = 0;
16 my $showonly = 0;
17 my $treedir;
18 my $projdir;
19 my $sysname;
20 my $ostype;
21 my $objdir;
22
23 while ( $_ = shift @ARGV )
24 {
25         if (m/^-t/) { $treedir = testArg( shift @ARGV ); next; }
26         if (m/^-p/) { $projdir = testArg( shift @ARGV ); next; }
27         if (m/^-sn/) { $sysname = testArg( shift @ARGV ); next; }
28
29         if (m/^-os/) { $ostype = testArg( shift @ARGV ); next; }
30         elsif (m/^-o/) { $objdir  = testArg( shift @ARGV ); next; }
31
32         if (m/^-q/) { $quiet = 1; next; }
33         if (m/^-n/) { $showonly = 1; next; }
34         &usage;
35 }
36 if ( !$treedir || !$projdir || !$ostype || !$sysname)
37 {
38         &usage;
39         exit 1;
40 }
41 if ( !$objdir )
42 {
43         $objdir = $projdir;
44 }
45
46 $quiet || print "libafs_tree_dir = $treedir\n";
47 $quiet || print "top_proj_dir = $projdir\n";
48 $quiet || print "top_obj_dir = $objdir\n";
49 $quiet || print "\n";
50 my $qprojdir = quotemeta($projdir);
51
52 #
53 # Start with an empty dir
54 #
55 if ( -e $treedir && ! -d $treedir )
56 {
57         die "Ick. Destination is not a directory and already exists!\n";
58 }
59 elsif ( -e $treedir )
60 {
61         #$quiet || print "Cleaning up previous tree build:\n";
62         #rmtree([$treedir],!$quiet,0);
63         #$quiet || print "Done.\n\n";
64 }
65
66 #
67 # Find all the dep list files in the src dir
68 #
69 finddepth(\&find_libafsdep, $projdir);
70
71 #
72 # Manual actions
73 #
74 &copyit("$projdir/configure-libafs", "$treedir/configure");
75 &copyit("$projdir/Makefile-libafs.in", "$treedir/Makefile.in");
76 &copyit("$projdir/src/libafs/MakefileProto.$ostype.in",
77         "$treedir/src/libafs/MakefileProto.in");
78
79 # We need to regenerate this to support building amd64 kernels from a
80 # libafs_tree built on i386.
81 unlink("$treedir/include/afs/param.h");
82
83 #
84 # Subs
85 #
86
87 sub find_libafsdep
88 {
89         my ($fname) = $_;
90
91         if ( $fname eq "libafsdep" )
92         {
93                 &process_libafsdep($File::Find::dir, $fname);
94         }
95 }
96
97 sub process_libafsdep
98 {
99         my ($dir, $depname) = @_;
100         my ($file);
101
102         my $subdir = $dir;
103         $subdir =~ s|^$qprojdir||o;
104         $subdir =~ s|^/||gio;
105         $subdir =~ s|/$||gio;
106
107         print "# $dir/$depname\n";
108         open(COMPS, "$depname");
109         while ( defined($file = <COMPS>) )
110         {
111                 my ($destdir, $proj_src,$obj_src);
112
113                 chomp($file);
114                 $file =~ s|#.*||gio;
115
116                 #
117                 # do some simple substitution in dep file
118                 #
119                 $file =~ s/MKAFS_OSTYPE/$ostype/ge;
120
121                 next if ( $file eq "" );
122
123                 $proj_src = "$projdir/$subdir/$file";
124                 $obj_src = "$objdir/$subdir/$file";
125                 $proj_src =~ s|//+|/|gio;
126                 $obj_src =~ s|//+|/|gio;
127
128                 if ( $file !~ /\*/ && $file !~ /\[/  ) # no globs in filename
129                 {
130                         my $fname = $file;
131                         $fname =~ s|.*/||gio;
132
133                         if ( -f $proj_src )
134                         {
135                                 $destdir = mkfullpath($projdir, $proj_src, $treedir);           
136                                 copyit($proj_src, "$destdir/$fname");
137                         }
138         
139                         if ( $obj_src ne $proj_src && -f $obj_src)
140                         {
141                                 $destdir = mkfullpath($objdir, $obj_src, $treedir);
142                                 copyit($obj_src, "$destdir/$fname");
143                         }
144                 }
145                 else
146                 {
147                         #print "Globbing ($proj_src)\n";
148                         foreach my $src ( glob($proj_src) )
149                         {
150                                 my $fname = $src;
151                                 $fname =~ s|.*/||gio;
152
153                                 $destdir = mkfullpath($projdir, $src, $treedir);                
154                                 copyit($src, "$destdir/$fname");
155                         }
156                         #print "Globbing ($obj_src)\n";
157                         foreach my $src ( glob($obj_src) )
158                         {
159                                 my $fname = $src;
160                                 $fname =~ s|.*/||gio;
161
162                                 $destdir = mkfullpath($objdir, $src, $treedir);
163                                 copyit($src, "$destdir/$fname");
164                         }
165                 }
166         }
167         close(COMPS);
168                 
169         $quiet || print "\n";
170 }
171
172 sub usage
173 {
174         print "usage: $0 -p top_proj_dir -os mkafs_ostype -sn sysname\n";
175         print "\t\t[-o top_obj_dir] [-t libafs_tree_dir] [-h] [-q] [-n]\n";
176         print "\ttop_proj_dir = /path/to/openafs - top level of openafs checkout/tarball\n";
177         print "\ttop_obj_dir = /path/to/objdir - top level of build directory, defaults to top_proj_dir\n";
178         print "\tlibafs_tree_dir = /path/to/libafs_tree - tree dir to create, defaults to top_obj_dir/libafs_tree\n";
179         print "\t-os = ostype value (i.e. LINUX)\n";
180         print "\t-sn = afs sysname (i.e. i386_linux24)\n";
181         print "\t-q = quiet build\n";
182         print "\t-n = just show what will be done\n";
183         print "\t-h = show this message\n";
184         
185         exit 1;
186 }
187
188 sub testArg
189 {
190         my ($arg) = @_;
191         return $arg if ( $arg && $arg ne "" );
192         &usage;
193 }
194
195 sub mkfullpath
196 {
197         my ($frombase, $fromfile, $destbase) = @_;
198         my $qfrombase = quotemeta $frombase;
199
200         my $subdir = $fromfile;
201         $subdir =~ s|^$qfrombase||;
202         $subdir =~ s|^(.*)/(.*?)$|$1|o;
203         $subdir =~ s|^/||gio;
204         $subdir =~ s|/$||gio;
205         $subdir =~ s|//+|/|gio;
206
207         my $destdir = "$destbase/$subdir";
208         $destdir =~ s|/$||gio;
209         $destdir =~ s|//+|/|gio;
210
211         $showonly || mkpath([$destdir], !$quiet, 0755);
212
213         return $destdir;
214 }
215
216 sub copyit
217 {
218         my ( $from, $to ) = @_;
219         my @from;
220         my @new;
221         my @to;
222
223         @from = stat($from);
224         @to = stat($to);
225
226         if ( ($#to < 0) || ($from[7] != $to[7]) || ($from[9] != $to[9]) )
227         {
228                 $quiet || print "+ cp -p $from $to\n";
229                 $showonly || system( "cp", "-p", $from, $to );
230         }
231 }
232
233
234 exit 0;