root/openpgpsdk/trunk/configure

Revision 431 (checked in by rachel, 6 years ago)

new cmdline app to flex Standard API functions
succinct print_keyring function

  • Property svn:executable set to *
Line 
1 #!/usr/bin/perl -w
2
3 use strict;
4
5 use Carp;
6 use File::Temp 0.14;
7 use File::Spec;
8 use POSIX qw(:errno_h);
9 use IO::File;
10
11 $|=1;
12
13 our $Log;
14 our $LogFile='/dev/null';
15
16 my $use_oink;
17
18 sub trace {
19     return if !$Log;
20
21     $Log->seek(0,2);
22     print $Log join '',@_;
23 }
24
25 our %Subst=(
26             CRYPTO_LIBS => '-lcrypto',
27             ZLIB => '-lz',
28             INCLUDES => '',
29             CFLAGS => '',
30            );
31
32 my %Args=(
33           '--help' => \&usage,
34           '--use-dmalloc' => sub {
35               $Subst{DM_FLAGS}='-I/usr/local/include -DDMALLOC';
36               $Subst{DM_LIB}='/usr/local/lib/libdmalloc.a';
37           },
38           '--with-openssl=' => sub {
39               my $loc=shift;
40               $Subst{INCLUDES}.=" -I $loc/include";
41               $Subst{CRYPTO_LIBS}="$loc/lib/libcrypto.a";
42               # assume developer version if library isn't in .../lib
43               $Subst{CRYPTO_LIBS}="$loc/libcrypto.a"
44                   if !-f $Subst{CRYPTO_LIBS};
45           },
46           '--with-zlib=' => sub {
47               my $loc=shift;
48               $Subst{INCLUDES}.=" -I $loc";
49               $Subst{ZLIB}="$loc/libz.a";
50           },
51           '--log=' => sub {
52               $LogFile=shift;
53               $Log=new IO::File(">$LogFile");
54               $Log->autoflush(1);
55           },
56           '--64' => sub {
57               $Subst{CFLAGS}.=' -m64';
58           },
59           '--maintainer' => sub {
60               $Subst{CFLAGS}.=' -fno-builtin';
61           },
62           '--cc=' => sub {
63               my $cc=shift;
64               $Subst{CC}=$cc;
65           },
66           '--oink=' => sub {
67               $Subst{OINK}=shift;
68           },
69          );
70
71 my %Functions=(
72                'socket' => { headers => ['sys/types.h','sys/socket.h'],
73                              call => 'socket(0,0,0)',
74                              libs => [[],['socket','nsl']] },
75                );
76
77 my @Headers=qw(alloca.h);
78 my @Types=qw(time_t);
79 my @RHeaders=qw(openssl/bn.h zlib.h);
80
81 our %Knowledge=(
82                 cc => sub { return $Subst{CC} || chooseBinary('gcc','cc')
83                              || croak 'Can\'t find C compiler'; },
84                 path => sub { return [split /:/,$ENV{PATH}]; },
85                 time_t => sub { return typeInfo('time_t','time.h'); },
86                 base_cflags => \&baseCFlags,
87                 is_gcc => \&isGCC,
88                 gcc_major => \&gccMajor,
89                 gcc_version => \&gccVersion,
90               );
91
92 while(my $arg=shift) {
93     my $arg2;
94     if($arg =~ /^(.+=)(.+)$/) {
95         $arg=$1;
96         $arg2=$2;
97     }
98     my $code=$Args{$arg};
99     croak "Don't understand: $arg" if !defined $code;
100     &$code($arg2);
101 }
102
103 #my $os=`uname -s`;
104
105 $Subst{'CC'}=getKnowledge('cc');
106
107 $Subst{'CFLAGS'}.=' '.getKnowledge('base_cflags');
108
109 checkHeaders(\@RHeaders);
110
111 findHeaders(\@Headers);
112 investigateTypes(\@Types);
113 my $libs=findLibraries(\%Functions);
114 $Subst{'LIBS'}=join(' ',map { "-l$_" } @$libs);
115 #exit;
116
117 if($Subst{OINK}) {
118     use Cwd;
119     my $path=getcwd();
120     $Subst{CC}="$path/util/oink_cc.pl --cc=$Subst{CC}";
121 }
122
123 fixSubst();
124
125 my $path=`pwd`;
126 chomp $path;
127
128
129 create('src/advanced/.depend');
130 create('src/standard/.depend');
131 create('src/app/.depend');
132 create('examples/.depend');
133
134 fileSubst('src/Makefile','#','');
135 fileSubst('src/advanced/Makefile','#','');
136 fileSubst('src/standard/Makefile','#','');
137 fileSubst('src/app/Makefile','#','');
138 fileSubst('examples/Makefile','#','');
139 fileSubst('util/Makefile.oink','#','');
140 fileSubst('include/openpgpsdk/configure.h','/*','*/');
141
142 print "make clean\n";
143 system('make clean') == 0 || exit;
144 print "make force_depend\n";
145 if(system('make force_depend') != 0) {
146     print STDERR "Configuration failed\n";
147     exit 1;
148 }
149
150 sub subst {
151     my $line=shift;
152
153     while(my($k,$v)=each %Subst) {
154         $line =~ s/\%$k\%/$v/g;
155     }
156     $line =~ s/\%.+?\%//g;
157     return $line;
158 }
159
160 sub chooseBinary {
161     my $path=getKnowledge('path');
162     while(my $bin=shift) {
163         foreach my $p (@$path) {
164             return $bin if -x "$p/$bin";
165         }
166     }
167     return 'false';
168 }
169
170 sub getKnowledge {
171     my $thing=shift;
172
173     croak "Asked for nonexistent knowledge $thing"
174       if !exists $Knowledge{$thing};
175
176     if(ref $Knowledge{$thing} eq 'CODE') {
177         print "Finding $thing\n";
178         $Knowledge{$thing}=&{$Knowledge{$thing}}();
179     }
180     return $Knowledge{$thing};
181 }
182
183 sub usage {
184     foreach my $k (keys %Args) {
185         print "$k\n";
186     }
187     exit;
188 }
189
190 sub findHeaders {
191     my $list=shift;
192
193     foreach my $h (@$list) {
194         my $n='HAVE_'.uc $h;
195         $n =~ s/[\.\/]/_/;
196         print "Looking for header $h ($n)\n";
197         if(-e "/usr/include/$h") {
198             $Subst{$n}="#define $n 0";
199         } else {
200             $Subst{$n}="#undef $n";
201         }
202     }
203 }
204
205 sub fileSubst {
206     my $file=shift;
207     my $cs=shift;
208     my $ce=shift;
209
210     open(T,"$file.template") || croak "Can't open $file.template: $!";
211     unlink $file;
212     open(M,">$file") || croak "Can't create $file: $!";
213
214     print M "$cs generated by configure from $file.template. Don't edit. $ce\n\n";
215
216     while(my $line=<T>) {
217         print M subst($line);
218     }
219
220     close M;
221     close T;
222
223     chmod 0444,"$file";
224 }
225
226 sub fixSubst {
227     foreach my $k (keys %Subst) {
228         $Subst{$k} =~ s/~/$ENV{HOME}/g;
229     }
230 }
231
232 sub create {
233     my $file=shift;
234
235     print "Creating $file\n";
236     open(D,">$file") || croak "Can't create $file: $!";
237     close D;
238 }
239
240 sub build {
241     my $code=shift;
242     my $link=shift;
243
244     my $cc=getKnowledge('cc');
245     my $fh=new File::Temp(SUFFIX => '.c');
246
247     my($v,$d,$f)=File::Spec->splitpath($fh->filename());
248
249     my $cur=File::Spec->rel2abs(File::Spec->curdir());
250     chdir($d) || croak "chdir($d): $!";
251
252     print $fh $code;
253     $fh->close();
254
255     my $cflag='';
256     $cflag='-c' if !defined $link;
257     my $cmd="$cc $Subst{CFLAGS} $Subst{INCLUDES} $cflag ".$f;
258     $cmd.=" $link" if defined $link;
259     trace("$cmd\n--code--\n$code--------\n");
260     my $ret=system("$cmd >> $LogFile 2>&1");
261
262     my $obj=$fh->filename();
263     $obj =~ s/\.c$/.o/;
264     unlink($obj) || $! == ENOENT || croak "unlink($obj): $!";
265     unlink('a.out') || $! == ENOENT || croak "unlink(a.out): $!";
266
267     chdir($cur) || croak "chdir($cur): $!";
268
269     return $ret == 0;
270 }
271
272 sub typeInfo {
273     my $type=shift;
274     my $header=shift;
275
276     my %info;
277
278     print "Getting info about $type.\n";
279
280     foreach my $fmt (qw(%d %ld)) {
281         my $code="#include <$header>\n";
282         $code .= "#include <stdio.h>\n";
283         $code .= "void f(void) { static $type t; printf(\"$fmt\",t); }\n";
284         if(build($code)) {
285             $info{fmt}=$fmt;
286             print "  $type printf format is $fmt\n";
287         }
288     }
289     croak "Can't determine print format for $type" if !defined $info{fmt};
290
291     return \%info;
292 }
293
294 sub investigateTypes {
295     my $types=shift;
296
297     foreach my $type (@$types) {
298         my $info=getKnowledge($type);
299         $Subst{uc($type)."_FMT"}="\"$info->{fmt}\"";
300     }
301 }
302
303 sub checkHeaders {
304     my $headers=shift;
305
306     foreach my $h (@$headers) {
307         print "Check required header $h\n";
308         build("#include <$h>\n") || croak "Can't find required header $h";
309     }
310 }
311
312 sub findLibraries {
313     my $funcs=shift;
314
315     my @libs;
316   func:
317     foreach my $func (keys %$funcs) {
318         print "Checking libraries for $func()\n";
319         my $code=join("\n",map { "#include <$_>" }
320                       @{$funcs->{$func}->{headers}});
321         $code.="\nint main() { $funcs->{$func}->{call}; return 0; }\n";
322         foreach my $lgroup (@{$funcs->{$func}->{libs}}) {
323             print "  trying ",@$lgroup ? join(', ',@$lgroup) : 'no libraries';
324             if(build($code,join(' ',map { "-l$_" } @$lgroup))) {
325                 push @libs,@$lgroup;
326                 print " ... OK\n";
327                 next func;
328             }
329             print " ... no\n";
330         }
331     }
332     return \@libs;
333 }
334
335 sub baseCFlags {
336     my $flags='';
337     if(getKnowledge('is_gcc')) {
338         $flags='-Wall -Werror -W -g';
339 #       my $v=getKnowledge('gcc_major');
340 #       $flags.=' -Wdeclaration-after-statement' if $v >= 3;
341     }
342     return $flags;
343 }
344
345 sub isGCC {
346     my $cc=getKnowledge('cc');
347
348     my $ret=build("int main()\n{\n#ifndef __GNUC__\n  syntax error\n#endif\n return 0; }\n");
349     trace("isGCC=$ret\n");
350     return $ret;
351 }
352
353 sub gccVersion {
354     return undef if !getKnowledge('is_gcc');
355
356     my $cc=getKnowledge('cc');
357     my $vstr=`$cc --version`;
358
359     my($v)=$vstr =~ /(\d+\.\d+\.\d+)/;
360
361     trace("gcc version=$v\n");
362
363     return $v;
364 }
365
366 sub gccMajor {
367     my $v=getKnowledge('gcc_version');
368     return undef if !defined $v;
369
370     ($v)=$v =~ /^(\d+)/;
371
372     trace("gcc major=$v\n");
373
374     return $v;
375 }
Note: See TracBrowser for help on using the browser.