root/openpgpsdk/trunk/configure

Revision 323 (checked in by ben, 7 years ago)

Preliminary setup to allow the use of oink.

  • Property svn:executable set to *
Line 
1 #!/usr/bin/perl -w
2
3 use strict;
4
5 use Carp;
6 use File::Temp;
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/.depend');
130 create('examples/.depend');
131
132 fileSubst('src/Makefile','#','');
133 fileSubst('examples/Makefile','#','');
134 fileSubst('util/Makefile.oink','#','');
135 fileSubst('include/openpgpsdk/configure.h','/*','*/');
136
137 print "make clean\n";
138 system 'make clean' || exit;
139 print "make force_depend\n";
140 if(system('make force_depend') != 0) {
141     print STDERR "Configuration failed\n";
142     exit 1;
143 }
144
145 sub subst {
146     my $line=shift;
147
148     while(my($k,$v)=each %Subst) {
149         $line =~ s/\%$k\%/$v/g;
150     }
151     $line =~ s/\%.+?\%//g;
152     return $line;
153 }
154
155 sub chooseBinary {
156     my $path=getKnowledge('path');
157     while(my $bin=shift) {
158         foreach my $p (@$path) {
159             return $bin if -x "$p/$bin";
160         }
161     }
162     return 'false';
163 }
164
165 sub getKnowledge {
166     my $thing=shift;
167
168     croak "Asked for nonexistent knowledge $thing"
169       if !exists $Knowledge{$thing};
170
171     if(ref $Knowledge{$thing} eq 'CODE') {
172         print "Finding $thing\n";
173         $Knowledge{$thing}=&{$Knowledge{$thing}}();
174     }
175     return $Knowledge{$thing};
176 }
177
178 sub usage {
179     foreach my $k (keys %Args) {
180         print "$k\n";
181     }
182     exit;
183 }
184
185 sub findHeaders {
186     my $list=shift;
187
188     foreach my $h (@$list) {
189         my $n='HAVE_'.uc $h;
190         $n =~ s/[\.\/]/_/;
191         print "Looking for header $h ($n)\n";
192         if(-e "/usr/include/$h") {
193             $Subst{$n}="#define $n 0";
194         } else {
195             $Subst{$n}="#undef $n";
196         }
197     }
198 }
199
200 sub fileSubst {
201     my $file=shift;
202     my $cs=shift;
203     my $ce=shift;
204
205     open(T,"$file.template") || croak "Can't open $file.template: $!";
206     unlink $file;
207     open(M,">$file") || croak "Can't create $file: $!";
208
209     print M "$cs generated by configure from $file.template. Don't edit. $ce\n\n";
210
211     while(my $line=<T>) {
212         print M subst($line);
213     }
214
215     close M;
216     close T;
217
218     chmod 0444,"$file";
219 }
220
221 sub fixSubst {
222     foreach my $k (keys %Subst) {
223         $Subst{$k} =~ s/~/$ENV{HOME}/g;
224     }
225 }
226
227 sub create {
228     my $file=shift;
229
230     print "Creating $file\n";
231     open(D,">$file") || croak "Can't create $file: $!";
232     close D;
233 }
234
235 sub build {
236     my $code=shift;
237     my $link=shift;
238
239     my $cc=getKnowledge('cc');
240     my $fh=new File::Temp(SUFFIX => '.c');
241
242     my($v,$d,$f)=File::Spec->splitpath($fh->filename());
243
244     my $cur=File::Spec->rel2abs(File::Spec->curdir());
245     chdir($d) || croak "chdir($d): $!";
246
247     print $fh $code;
248     $fh->close();
249
250     my $cflag='';
251     $cflag='-c' if !defined $link;
252     my $cmd="$cc $Subst{CFLAGS} $Subst{INCLUDES} $cflag ".$f;
253     $cmd.=" $link" if defined $link;
254     trace("$cmd\n--code--\n$code--------\n");
255     my $ret=system("$cmd >> $LogFile 2>&1");
256
257     my $obj=$fh->filename();
258     $obj =~ s/\.c$/.o/;
259     unlink($obj) || $! == ENOENT || croak "unlink($obj): $!";
260     unlink('a.out') || $! == ENOENT || croak "unlink(a.out): $!";
261
262     chdir($cur) || croak "chdir($cur): $!";
263
264     return $ret == 0;
265 }
266
267 sub typeInfo {
268     my $type=shift;
269     my $header=shift;
270
271     my %info;
272
273     print "Getting info about $type.\n";
274
275     foreach my $fmt (qw(%d %ld)) {
276         my $code="#include <$header>\n";
277         $code .= "#include <stdio.h>\n";
278         $code .= "void f(void) { static $type t; printf(\"$fmt\",t); }\n";
279         if(build($code)) {
280             $info{fmt}=$fmt;
281             print "  $type printf format is $fmt\n";
282         }
283     }
284     croak "Can't determine print format for $type" if !defined $info{fmt};
285
286     return \%info;
287 }
288
289 sub investigateTypes {
290     my $types=shift;
291
292     foreach my $type (@$types) {
293         my $info=getKnowledge($type);
294         $Subst{uc($type)."_FMT"}="\"$info->{fmt}\"";
295     }
296 }
297
298 sub checkHeaders {
299     my $headers=shift;
300
301     foreach my $h (@$headers) {
302         print "Check required header $h\n";
303         build("#include <$h>\n") || croak "Can't find required header $h";
304     }
305 }
306
307 sub findLibraries {
308     my $funcs=shift;
309
310     my @libs;
311   func:
312     foreach my $func (keys %$funcs) {
313         print "Checking libraries for $func()\n";
314         my $code=join("\n",map { "#include <$_>" }
315                       @{$funcs->{$func}->{headers}});
316         $code.="\nint main() { $funcs->{$func}->{call}; return 0; }\n";
317         foreach my $lgroup (@{$funcs->{$func}->{libs}}) {
318             print "  trying ",@$lgroup ? join(', ',@$lgroup) : 'no libraries';
319             if(build($code,join(' ',map { "-l$_" } @$lgroup))) {
320                 push @libs,@$lgroup;
321                 print " ... OK\n";
322                 next func;
323             }
324             print " ... no\n";
325         }
326     }
327     return \@libs;
328 }
329
330 sub baseCFlags {
331     my $flags='';
332     if(getKnowledge('is_gcc')) {
333         $flags='-Wall -Werror -W -g';
334 #       my $v=getKnowledge('gcc_major');
335 #       $flags.=' -Wdeclaration-after-statement' if $v >= 3;
336     }
337     return $flags;
338 }
339
340 sub isGCC {
341     my $cc=getKnowledge('cc');
342
343     my $ret=build("int main()\n{\n#ifndef __GNUC__\n  syntax error\n#endif\n return 0; }\n");
344     trace("isGCC=$ret\n");
345     return $ret;
346 }
347
348 sub gccVersion {
349     return undef if !getKnowledge('is_gcc');
350
351     my $cc=getKnowledge('cc');
352     my $vstr=`$cc --version`;
353
354     my($v)=$vstr =~ /(\d+\.\d+\.\d+)/;
355
356     trace("gcc version=$v\n");
357
358     return $v;
359 }
360
361 sub gccMajor {
362     my $v=getKnowledge('gcc_version');
363     return undef if !defined $v;
364
365     ($v)=$v =~ /^(\d+)/;
366
367     trace("gcc major=$v\n");
368
369     return $v;
370 }
Note: See TracBrowser for help on using the browser.