Run ctfconvert/ctfmerge for all objects
[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 $showonly || system("$projdir/build-tools/git-version $projdir > $treedir/.version");
80
81 # We need to regenerate this to support building amd64 kernels from a
82 # libafs_tree built on i386.
83 unlink("$treedir/include/afs/param.h");
84
85 #
86 # Subs
87 #
88
89 sub find_libafsdep
90 {
91         my ($fname) = $_;
92
93         if ( $fname eq "libafsdep" )
94         {
95                 &process_libafsdep($File::Find::dir, $fname);
96         }
97 }
98
99 sub process_libafsdep
100 {
101         my ($dir, $depname) = @_;
102         my ($file);
103
104         my $subdir = $dir;
105         $subdir =~ s|^$qprojdir||o;
106         $subdir =~ s|^/||gio;
107         $subdir =~ s|/$||gio;
108
109         print "# $dir/$depname\n";
110         open(COMPS, "$depname");
111         while ( defined($file = <COMPS>) )
112         {
113                 my ($destdir, $proj_src,$obj_src);
114
115                 chomp($file);
116                 $file =~ s|#.*||gio;
117
118                 #
119                 # do some simple substitution in dep file
120                 #
121                 $file =~ s/MKAFS_OSTYPE/$ostype/ge;
122
123                 next if ( $file eq "" );
124
125                 $proj_src = "$projdir/$subdir/$file";
126                 $obj_src = "$objdir/$subdir/$file";
127                 $proj_src =~ s|//+|/|gio;
128                 $obj_src =~ s|//+|/|gio;
129
130                 if ( $file !~ /\*/ && $file !~ /\[/  ) # no globs in filename
131                 {
132                         my $fname = $file;
133                         $fname =~ s|.*/||gio;
134
135                         if ( -f $proj_src )
136                         {
137                                 $destdir = mkfullpath($projdir, $proj_src, $treedir);           
138                                 copyit($proj_src, "$destdir/$fname");
139                         }
140         
141                         if ( $obj_src ne $proj_src && -f $obj_src)
142                         {
143                                 $destdir = mkfullpath($objdir, $obj_src, $treedir);
144                                 copyit($obj_src, "$destdir/$fname");
145                         }
146                 }
147                 else
148                 {
149                         #print "Globbing ($proj_src)\n";
150                         foreach my $src ( glob($proj_src) )
151                         {
152                                 my $fname = $src;
153                                 $fname =~ s|.*/||gio;
154
155                                 $destdir = mkfullpath($projdir, $src, $treedir);                
156                                 copyit($src, "$destdir/$fname");
157                         }
158                         #print "Globbing ($obj_src)\n";
159                         foreach my $src ( glob($obj_src) )
160                         {
161                                 my $fname = $src;
162                                 $fname =~ s|.*/||gio;
163
164                                 $destdir = mkfullpath($objdir, $src, $treedir);
165                                 copyit($src, "$destdir/$fname");
166                         }
167                 }
168         }
169         close(COMPS);
170                 
171         $quiet || print "\n";
172 }
173
174 sub usage
175 {
176         print "usage: $0 -p top_proj_dir -os mkafs_ostype -sn sysname\n";
177         print "\t\t[-o top_obj_dir] [-t libafs_tree_dir] [-h] [-q] [-n]\n";
178         print "\ttop_proj_dir = /path/to/openafs - top level of openafs checkout/tarball\n";
179         print "\ttop_obj_dir = /path/to/objdir - top level of build directory, defaults to top_proj_dir\n";
180         print "\tlibafs_tree_dir = /path/to/libafs_tree - tree dir to create, defaults to top_obj_dir/libafs_tree\n";
181         print "\t-os = ostype value (i.e. LINUX)\n";
182         print "\t-sn = afs sysname (i.e. i386_linux24)\n";
183         print "\t-q = quiet build\n";
184         print "\t-n = just show what will be done\n";
185         print "\t-h = show this message\n";
186         
187         exit 1;
188 }
189
190 sub testArg
191 {
192         my ($arg) = @_;
193         return $arg if ( $arg && $arg ne "" );
194         &usage;
195 }
196
197 sub mkfullpath
198 {
199         my ($frombase, $fromfile, $destbase) = @_;
200         my $qfrombase = quotemeta $frombase;
201
202         my $subdir = $fromfile;
203         $subdir =~ s|^$qfrombase||;
204         $subdir =~ s|^(.*)/(.*?)$|$1|o;
205         $subdir =~ s|^/||gio;
206         $subdir =~ s|/$||gio;
207         $subdir =~ s|//+|/|gio;
208
209         my $destdir = "$destbase/$subdir";
210         $destdir =~ s|/$||gio;
211         $destdir =~ s|//+|/|gio;
212
213         $showonly || mkpath([$destdir], !$quiet, 0755);
214
215         return $destdir;
216 }
217
218 sub copyit
219 {
220         my ( $from, $to ) = @_;
221         my @from;
222         my @new;
223         my @to;
224
225         @from = stat($from);
226         @to = stat($to);
227
228         if ( ($#to < 0) || ($from[7] != $to[7]) || ($from[9] != $to[9]) )
229         {
230                 $quiet || print "+ cp -p $from $to\n";
231                 $showonly || system( "cp", "-p", $from, $to );
232         }
233 }
234
235
236 exit 0;