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