1# ===========================================================================
2#
3#                            PUBLIC DOMAIN NOTICE
4#               National Center for Biotechnology Information
5#
6#  This software/database is a "United States Government Work" under the
7#  terms of the United States Copyright Act.  It was written as part of
8#  the author's official duties as a United States Government employee and
9#  thus cannot be copyrighted.  This software/database is freely available
10#  to the public for use. The National Library of Medicine and the U.S.
11#  Government have not placed any restriction on its use or reproduction.
12#
13#  Although all reasonable efforts have been taken to ensure the accuracy
14#  and reliability of the software and data, the NLM and the U.S.
15#  Government do not and cannot warrant the performance or results that
16#  may be obtained by using this software or data. The NLM and the U.S.
17#  Government disclaim all warranties, express or implied, including
18#  warranties of performance, merchantability or fitness for any particular
19#  purpose.
20#
21#  Please cite the author in any work or product based on this material.
22#
23# ===========================================================================
24
25use strict;
26
27use Cwd 'abs_path';
28use File::Basename 'dirname';
29use lib dirname( abs_path $0 );
30
31sub println  { print @_; print "\n" }
32
33my ($filename, $directories, $suffix) = fileparse($0);
34if ($directories ne "./") {
35    println "configure: error: $filename should be run as ./$filename";
36    exit 1;
37}
38
39require 'package.prl';
40require 'os-arch.prl';
41
42use Cwd qw(abs_path getcwd);
43use File::Basename 'fileparse';
44use File::Spec 'catdir';
45use FindBin qw($Bin);
46use Getopt::Long "GetOptions";
47
48chdir '..' or die "cannot cd to package root";
49
50check();
51
52my $LOCAL_BUILD_OUT
53    = -e File::Spec->catdir($ENV{HOME}, 'tmp', 'local-build-out');
54
55my ($CONFIGURED, $RECONFIGURE) = ('');
56if (@ARGV) {
57    foreach (@ARGV) {
58        $CONFIGURED .= "\t" if ($CONFIGURED);
59        $CONFIGURED .= "'$_'";
60    }
61} elsif (-f 'reconfigure') {
62    ++$RECONFIGURE unless ($LOCAL_BUILD_OUT);
63}
64
65my %PKG = PKG();
66
67my $PACKAGE_NAME = PACKAGE_NAME();
68my $OUT_MAKEFILE = 'Makefile.config';
69my $INS_MAKEFILE = 'Makefile.config.install';
70
71my $PACKAGE = PACKAGE();
72
73my $HOME = $ENV{HOME} || $ENV{USERPROFILE}
74    || $ENV{LOGDIR} || getcwd || (getpwuid($<))[7] || abs_path('.');
75
76$PKG{UPATH} = expand($PKG{UPATH});
77
78my $package_default_prefix = $PKG{PATH};
79my $schema_default_dir = $PKG{SCHEMA_PATH} if ($PKG{SCHEMA_PATH});
80
81my @REQ = REQ();
82
83my @options = ( 'build-prefix=s',
84                'clean',
85                'debug',
86                'help',
87                'prefix=s',
88                'reconfigure',
89                'relative-build-out-dir',
90                'status',
91                'with-debug',
92                'without-debug', );
93{
94    my ($OS, $ARCH, $OSTYPE, $MARCH, @ARCHITECTURES) = OsArch();
95    push @options, 'arch=s'    if (@ARCHITECTURES);
96}
97push @options, 'source=s' if ($PKG{LNG} eq 'JAVA');
98push @options, 'enable-static' if (PACKAGE_TYPE() eq 'B');
99foreach my $href (@REQ) {
100    my %a = %$href;
101    push @options, "$a{option}=s"  if ($a {option});
102    push @options, "$a{boption}=s" if ($a{boption});
103    $href->{usrpath} = '' unless ($href->{usrpath});
104    $href->{usrpath} = expand($href->{usrpath});
105}
106push @options, "shemadir" if ($PKG{SCHEMA_PATH});
107
108my %OPT;
109die "configure: error" unless (GetOptions(\%OPT, @options));
110++$OPT{'reconfigure'} if ($RECONFIGURE);
111
112if ($OPT{'reconfigure'}) {
113    unless (eval 'use Getopt::Long qw(GetOptionsFromString); 1') {
114        print <<EndText;
115configure: error: your perl does not support Getopt::Long::GetOptionsFromString
116                  reconfigure option is not avaliable.
117Run "sh ./reconfigure" instead.
118EndText
119        exit 1;
120    }
121    println "reconfiguring...";
122    open F, 'reconfigure' or die 'cannot open reconfigure';
123    $_ = <F>;
124    chomp;
125    unless (m|^\./configure\s*(.*)$|) {
126        println 'configure: error: cannot reconfigure';
127        println 'run "./configure --clean" then run "./configure [OPTIONS]"';
128        exit 1;
129    }
130
131    my $ARG = $1;
132    println "running \"./configure $ARG\"...";
133    undef %OPT;
134    die "configure: error" unless (GetOptionsFromString($ARG, \%OPT, @options));
135    $CONFIGURED = $ARG if ($#ARGV == -1 && $RECONFIGURE);
136    ++$OPT{reconfigure};
137}
138
139$OPT{'local-build-out'} = $LOCAL_BUILD_OUT;
140unless ($OPT{'local-build-out'})
141{   $OPT{'local-build-out'} = $OPT{'relative-build-out-dir'} }
142my $OUTDIR = File::Spec->catdir($HOME, $PKG{OUT});
143my $REL_OUTDIR = expand_path(File::Spec->catdir($Bin, $PKG{LOCOUT}));
144if ($OPT{'local-build-out'})
145{   $OUTDIR = $REL_OUTDIR if ($REL_OUTDIR) }
146
147if ($OPT{'help'}) {
148    help();
149    exit 0;
150} elsif ($OPT{'clean'}) {
151    {
152        foreach ('reconfigure', glob(CONFIG_OUT() . '/Makefile.config*'),
153            File::Spec->catdir(CONFIG_OUT(), 'Makefile.userconfig'),
154            File::Spec->catdir(CONFIG_OUT(), 'user.status'))
155        {
156            my $f = $_;
157            print "removing $f... ";
158            if (-e $f) {
159                if (unlink $f) {
160                    println "ok";
161                } else {
162                    println "failed";
163                }
164            } else {
165                println "not found";
166            }
167        }
168    }
169    if (CONFIG_OUT() ne '.') {
170        foreach
171            (glob('Makefile.config*'), 'user.status', 'Makefile.userconfig')
172        {
173            my $f = $_;
174            print "removing $f... ";
175            if (-e $f) {
176                if (unlink $f) {
177                    println "ok";
178                } else {
179                    println "failed";
180                }
181            } else {
182                println "not found";
183            }
184        }
185    }
186    exit 0;
187} elsif ($OPT{'status'}) {
188    status(1);
189    exit 0;
190}
191
192foreach (@ARGV) {
193    @_ = split('=');
194    next if ($#_ != 1);
195    $OPT{$_[0]} = $_[1] if ($_[0] eq 'CC' || $_[0] eq 'CXX' || $_[0] eq 'LDFLAGS' || $_[0] eq 'TOOLS');
196}
197
198println "Configuring $PACKAGE_NAME package";
199
200$OPT{'prefix'} = $package_default_prefix unless ($OPT{'prefix'});
201
202my $AUTORUN = $OPT{status};
203print "checking system type... " unless ($AUTORUN);
204my ($OS, $ARCH, $OSTYPE, $MARCH, @ARCHITECTURES) = OsArch();
205println $OSTYPE unless ($AUTORUN);
206
207unless ($OSTYPE =~ /linux/i || $OSTYPE =~ /dragonfly/i || $OSTYPE =~ /darwin/i || $OSTYPE eq 'win') {
208    println "configure: error: unsupported system '$OSTYPE'";
209    exit 1;
210}
211
212my $OS_DISTRIBUTOR = '';
213if ($OS eq 'linux') {
214    print "checking OS distributor... " unless ($AUTORUN);
215    $OS_DISTRIBUTOR = `lsb_release -si 2> /dev/null`;
216    if ( $? != 0 ) {
217        $_ = `cat /etc/redhat-release 2> /dev/null`;
218        @_ = split ( / /  );
219        $OS_DISTRIBUTOR = $_[0] if ( $_[0] );
220    }
221    $OS_DISTRIBUTOR = '' unless ( $OS_DISTRIBUTOR );
222    chomp $OS_DISTRIBUTOR;
223    println $OS_DISTRIBUTOR unless ($AUTORUN);
224}
225
226print "checking machine architecture... " unless ($AUTORUN);
227println $MARCH unless ($AUTORUN);
228unless ($MARCH =~ /x86_64/i || $MARCH =~ /amd64/i || $MARCH =~ /i?86/i || $MARCH =~ /aarch64/) {
229    println "configure: error: unsupported architecture '$OSTYPE':'$MARCH'";
230    exit 1;
231}
232
233{
234    $OPT{'prefix'} = expand_path($OPT{'prefix'});
235    my $prefix = $OPT{'prefix'};
236    $OPT{'eprefix'} = $prefix unless ($OPT{'eprefix'} || $OS eq 'win');
237    my $eprefix = $OPT{'eprefix'};
238    unless ($OPT{'bindir'} || $OS eq 'win') {
239        $OPT{'bindir'} = File::Spec->catdir($eprefix, 'bin') ;
240    }
241    unless ($OPT{'libdir'} || $OS eq 'win') {
242        $OPT{'libdir'} = File::Spec->catdir($eprefix, 'lib');
243    }
244    unless ($OPT{'includedir'} || $OS eq 'win') {
245        $OPT{'includedir'} = File::Spec->catdir($eprefix, 'include');
246    }
247    if ($PKG{LNG} eq 'PYTHON' && ! $OPT{'pythondir'} && $OS ne 'win') {
248        $OPT{'pythondir'} = $eprefix;
249    }
250    if ($PKG{LNG} eq 'JAVA' && ! $OPT{'javadir'} && $OS ne 'win') {
251        $OPT{'javadir'} = File::Spec->catdir($eprefix, 'jar');
252    }
253    if ($PKG{EXAMP} && ! $OPT{'sharedir'} && $OS ne 'win') {
254        $OPT{'sharedir'} = File::Spec->catdir($eprefix, 'share');
255    }
256}
257
258# initial values
259my $TARGDIR = File::Spec->catdir($OUTDIR, $PACKAGE);
260if ($OPT{'build-prefix'}) {
261    $TARGDIR = $OPT{'build-prefix'} = expand_path($OPT{'build-prefix'});
262    unless ($TARGDIR =~ /$PACKAGE$/) {
263        $TARGDIR = File::Spec->catdir($TARGDIR, $PACKAGE);
264    }
265}
266my $BUILD_PREFIX = $TARGDIR;
267
268my $BUILD = 'rel';
269
270# parse command line
271$BUILD = 'dbg' if ($OPT{'with-debug'});
272$BUILD = 'rel' if ($OPT{'without-debug'});
273
274my $BUILD_TYPE = "release";
275$BUILD_TYPE = "debug" if ( $BUILD eq "dbg" );
276
277$OPT{arch} = $ARCH if (@ARCHITECTURES && ! $OPT{arch});
278
279if ($OPT{arch}) {
280    my $found;
281    foreach (@ARCHITECTURES) {
282        if ($_ eq $OPT{arch}) {
283            ++$found;
284            last;
285        }
286    }
287    if ($found) {
288        $ARCH = $MARCH = $OPT{arch};
289        while (1) {
290            open F, ">Makefile.config.$OS.arch" or last;
291            print F "$ARCH\n";
292            close F;
293            last;
294        }
295        println "build architecture: $ARCH" unless ($AUTORUN);
296    } else {
297        delete $OPT{arch};
298    }
299}
300
301$OUT_MAKEFILE = File::Spec->catdir(CONFIG_OUT(), "$OUT_MAKEFILE.$OS.$ARCH");
302$INS_MAKEFILE = File::Spec->catdir(CONFIG_OUT(), "$INS_MAKEFILE.$OS.$ARCH.prl");
303
304my $TOOLS = "";
305$TOOLS = "jdk" if ($PKG{LNG} eq 'JAVA');
306
307# determine architecture
308
309print "checking for supported architecture... " unless ($AUTORUN);
310
311my $BITS;
312
313if ($MARCH =~ /(x86_|amd)64/i) {
314    $BITS = 64;
315} elsif ($MARCH eq 'fat86') {
316    $BITS = '32_64';
317} elsif ($MARCH =~ /i?86/i) {
318    $BITS = 32;
319} elsif ($MARCH =~ /aarch64/) {
320    $BITS = 64;
321} else {
322    die "unrecognized Architecture '$ARCH'";
323}
324println "$MARCH ($BITS bits) is supported" unless ($AUTORUN);
325
326# determine OS and related norms
327my ($LPFX, $OBJX, $LOBX, $LIBX, $SHLX, $EXEX, $OSINC, $PYTHON);
328
329print "checking for supported OS... " unless ($AUTORUN);
330if ($OSTYPE =~ /linux/i) {
331    $LPFX = 'lib';
332    $OBJX = 'o';
333    $LOBX = 'pic.o';
334    $LIBX = 'a';
335    $SHLX = 'so';
336    $EXEX = '';
337    $OSINC = 'unix';
338    $TOOLS = 'gcc' unless ($TOOLS);
339    $PYTHON = 'python';
340} elsif ($OSTYPE =~ /dragonfly/i) {
341    $LPFX = 'lib';
342    $OBJX = 'o';
343    $LOBX = 'pic.o';
344    $LIBX = 'a';
345    $SHLX = 'so';
346    $EXEX = '';
347    $OSINC = 'unix';
348    unless ($TOOLS) {
349        $TOOLS = 'gcc';
350        $TOOLS = $OPT{TOOLS} if ($OPT{TOOLS});
351    }
352    $PYTHON = $ENV{FREEBSD_PYTHON_CMD};
353} elsif ($OSTYPE =~ /darwin/i) {
354    $LPFX = 'lib';
355    $OBJX = 'o';
356    $LOBX = 'pic.o';
357    $LIBX = 'a';
358    $SHLX = 'dylib';
359    $EXEX = '';
360    $OSINC = 'unix';
361    $TOOLS = 'gcc' unless ($TOOLS);
362    $PYTHON = 'python';
363} elsif ($OSTYPE eq 'win') {
364    $TOOLS = 'vc++';
365} else {
366    die "unrecognized OS '$OSTYPE'";
367}
368
369println "$OSTYPE ($OS) is supported" unless ($AUTORUN);
370
371# tool chain
372my ($CPP, $CC, $CP, $AR, $ARX, $ARLS, $LD, $LP, $MAKE_MANIFEST);
373my ($JAVAC, $JAVAH, $JAR);
374my ($ARCH_FL, $DBG, $OPT, $PIC, $INC, $MD, $LDFLAGS) = ('');
375
376print "checking for supported tool chain... " unless ($AUTORUN);
377
378$CC      = $OPT{CC     } if ($OPT{CC     });
379$CPP     = $OPT{CXX    } if ($OPT{CXX    });
380$LDFLAGS = $OPT{LDFLAGS} if ($OPT{LDFLAGS});
381
382if ($TOOLS =~ /gcc$/) {
383    $CPP  = 'g++' unless ($CPP);
384    $CC   = 'gcc' unless ($CC);
385    $LD   = $CC;
386    $CC   = "$CC -c";
387    $CP   = "$CPP -c";
388    $AR   = 'ar rc';
389    $ARX  = 'ar x';
390    $ARLS = 'ar t';
391    $LP   = $CPP;
392
393    $DBG = '-g -DDEBUG';
394    $OPT = '-O3';
395    $PIC = '-fPIC';
396    $INC = '-I';
397    $MD  = '-MD';
398} elsif ($TOOLS eq 'clang') {
399    $CPP  = 'clang++' unless ($CPP);
400    $CC   = 'clang' unless ($CC);
401    $LD   = $CC;
402    $CC   = "$CC -c";
403    my $versionMin = '';
404    $versionMin = '-mmacosx-version-min=10.6' if ($OSTYPE =~ /darwin/i);
405    $CP   = "$CPP -c $versionMin";
406    if ($BITS ne '32_64') {
407        $ARCH_FL = '-arch i386' if ($BITS == 32);
408        $OPT = '-O3';
409        $AR      = 'ar rc';
410        $LD      = "$LD $ARCH_FL";
411        $LP      = "$CPP $versionMin $ARCH_FL";
412    } else {
413        $MAKE_MANIFEST = '( echo "$^" > $@/manifest )';
414        $ARCH_FL       = '-arch i386 -arch x86_64';
415        $OPT    = '-O3';
416        $AR     = 'libtool -static -o';
417        $LD     = "$LD -Wl,-arch_multiple $ARCH_FL -Wl,-all_load";
418        $LP     = "$CPP $versionMin -Wl,-arch_multiple $ARCH_FL -Wl,-all_load";
419    }
420    $ARX  = 'ar x';
421    $ARLS = 'ar t';
422
423    $DBG = '-g -DDEBUG';
424    $PIC = '-fPIC';
425    $INC = '-I';
426    $MD  = '-MD';
427} elsif ($TOOLS eq 'jdk') {
428    $JAVAC = 'javac';
429    if ($OPT{source}) {
430        $JAVAC .= ' -target ' . $OPT{source} . ' -source ' . $OPT{source};
431    }
432    $JAVAH = 'javah';
433    $JAR   = 'jar cf';
434
435    $DBG = '-g';
436} elsif ($TOOLS eq 'vc++') {
437} else {
438    die "unrecognized tool chain '$TOOLS'";
439}
440println "$TOOLS tool chain is supported" unless ($AUTORUN);
441
442if ($OS ne 'win' && $PKG{LNG} ne 'JAVA') {
443    $TARGDIR = File::Spec->catdir($TARGDIR, $OS, $TOOLS, $ARCH, $BUILD);
444}
445
446if ($CPP) {
447    unless (check_tool__h($CPP)) {
448        println "configure: error: '$CPP' cannot be found";
449        exit 1;
450    }
451}
452
453if ($JAVAC) {
454    unless (check_tool_h($JAVAC)) {
455        println "configure: error: '$JAVAC' cannot be found";
456        exit 1;
457    }
458}
459
460print 'checking for Python 3... ' unless $AUTORUN;
461if ($PYTHON) {
462    my $p3;
463    for my $dir (File::Spec->path()) {
464        $p3 = File::Spec->join($dir, 'python3');
465        my $pX = substr($p3, 0, -1);
466        if (-x $pX && `$pX --version 2>&1` =~ /^\s*Python\s+(\d+)/i && $1 == 3) {
467            $p3 = $pX;
468            last;
469        }
470        if (-x $p3 && `$p3 --version 2>&1` =~ /^\s*Python\s+(\d+)/i && $1 == 3) {
471            last;
472        }
473        undef $p3;
474    }
475    if ($p3) {
476        $PYTHON = $p3;
477        println $PYTHON unless $AUTORUN;
478    }
479    else {
480        undef $PYTHON;
481        println 'no' unless $AUTORUN;
482    }
483}
484else {
485    println 'skipped' unless $AUTORUN;
486}
487
488my $NO_ARRAY_BOUNDS_WARNING = '';
489if ($TOOLS =~ /gcc$/ && check_no_array_bounds()) {
490    $NO_ARRAY_BOUNDS_WARNING = '-Wno-array-bounds';
491}
492
493my $STATIC_LIBSTDCPP = '';
494if ($TOOLS =~ /gcc$/) {
495    $STATIC_LIBSTDCPP = check_static_libstdcpp();
496}
497
498if ( $PKG{REQ} ) {
499    foreach ( @{ $PKG{REQ} } ) {
500        unless (check_tool__h($_)) {
501            println "configure: error: '$_' cannot be found";
502            exit 1;
503        }
504    }
505}
506
507my @dependencies;
508
509if ( $PKG{OPT} ) {
510    foreach ( @{ $PKG{OPT} } ) {
511        if ( /^qmake$/ ) {
512            my $qmake = check_qmake();
513            push @dependencies, "QMAKE_BIN = $qmake";
514        } else { die; }
515    }
516}
517
518my %DEPEND_OPTIONS;
519foreach my $href (DEPENDS()) {
520    $_ = $href->{name};
521    my $I = $href->{Include};
522    my @L;
523    my $o = "with-$_-prefix";
524    ++$DEPEND_OPTIONS{$o};
525    if ($OPT{$o}) {
526        $OPT{$o} = expand_path($OPT{$o});
527        $I = File::Spec->catdir($OPT{$o}, 'include');
528        if (/^xml2$/) {
529            my $t = File::Spec->catdir($I, 'libxml2');
530            $I = $t if (-e $t);
531        }
532        push ( @L, File::Spec->catdir($OPT{$o}, 'lib') );
533        push ( @L, File::Spec->catdir($OPT{$o}, 'lib64') );
534    }
535    my ($i, $l) = find_lib($_, $I, @L);
536    if (defined $i || $l) {
537        my $d = 'HAVE_' . uc($_) . ' = 1';
538        push @dependencies, $d;
539        println "\t\t$d" if ($OPT{'debug'});
540    }
541    if ($i) {
542        my $d = uc($_) . "_INCDIR = $i";
543        push @dependencies, $d;
544        println "\t\t$d" if ($OPT{'debug'});
545    }
546    if ($l) {
547        my $d = uc($_) . "_LIBDIR = $l";
548        push @dependencies, $d;
549        println "\t\t$d" if ($OPT{'debug'});
550    }
551}
552
553foreach my $href (@REQ) {
554    $href->{   bldpath} = expand($href->{   bldpath}) if ($href->{   bldpath});
555    $href->{locbldpath} = expand($href->{locbldpath}) if ($href->{locbldpath});
556
557    # found directories
558    my
559      ($found_itf, $found_bin, $found_lib, $found_ilib, $found_jar, $found_src);
560
561    my %a = %$href;
562    next if ($a{option} && $DEPEND_OPTIONS{$a{option}});
563    my $is_optional = optional($a{type});
564    my $quasi_optional = $a{type} =~ /Q/;
565    my $need_source = $a{type} =~ /S/;
566    my $need_bin = $a{type} =~ /E/;
567    my $need_build = $a{type} =~ /B/;
568    my $need_lib = $a{type} =~ /L|D/;
569    my $need_itf = ! ($a{type} =~ /D/ || $a{type} =~ /E/ || $a{type} =~ /J/);
570    $need_itf = 1 if ($a{type} =~ /I/);
571    my $need_jar = $a{type} =~ /J/;
572
573    my ($bin, $inc, $lib, $ilib, $src)
574        = ($a{bin}, $a{include}, $a{lib}, undef, $a{src}); # file names to check
575    $lib = '' unless ($lib);
576    $lib = expand($lib);
577
578    if ($need_build) {
579        $ilib = $a{ilib};
580        ++$need_lib;
581    }
582    unless ($AUTORUN) {
583        if ($need_source && $need_build) {
584            println
585               "checking for $a{name} package source files and build results..."
586        } elsif ($need_source) {
587            println "checking for $a{name} package source files...";
588        } else {
589            println "checking for $a{name} package...";
590        }
591    }
592    my %has_option;
593    my $tolib = $need_itf || $need_lib;
594    my $tojar = $need_jar;
595    foreach my $option ($a{option}, $a{boption}) {
596        next unless ($option);
597        if ($OPT{$option}) {
598            my $try = expand_path($OPT{$option});
599            if ($tojar && ! $found_jar && -f $try) {
600                println "\tjar... $try" unless ($AUTORUN);
601                $found_jar = $try;
602            }
603            elsif ($tolib) {
604                my ($i, $l, $il) = ($inc, $lib, $ilib);
605                if ($option =~ /-build$/) {
606                    undef $i;
607                    ++$has_option{build};
608                } elsif ($option =~ /-prefix$/) {
609                    undef $il;
610                    ++$has_option{prefix};
611                } elsif ($option =~ /-sources$/) {
612                    undef $l;
613                    undef $il;
614                    ++$has_option{sources};
615                }
616                my ($fi, $fl, $fil, $fs)
617                    = find_in_dir($try, $i, $l, $il, undef, undef, $src);
618                if ($fi || $fl || $fil) {
619                    $found_itf  = $fi  if (! $found_itf  && $fi);
620                    $found_lib  = $fl  if (! $found_lib  && $fl);
621                    $found_ilib = $fil if (! $found_ilib && $fil);
622                    $found_src  = $fs  if (! $found_src  && $fs);
623                } elsif (! ($try =~ /$a{name}$/)) {
624                    $try = File::Spec->catdir($try, $a{name});
625                    ($fi, $fl, $fil, $fs)
626                        = find_in_dir($try, $i, $l, $il, undef, undef, $src);
627                    $found_itf  = $fi  if (! $found_itf  && $fi);
628                    $found_lib  = $fl  if (! $found_lib  && $fl);
629                    $found_ilib = $fil if (! $found_ilib && $fil);
630                    $found_src  = $fs  if (! $found_src  && $fs);
631                }
632            } elsif ($need_bin) {
633                my (undef, $fl, $fil)
634                    = find_in_dir($try, undef, $lib, $ilib, undef, $bin);
635                $found_bin = $fl if ($fl);
636            }
637        }
638    }
639    if (! $found_itf && ! $has_option{sources} && $a{srcpath}) {
640        my $try = $a{srcpath};
641        ($found_itf, undef, undef, $found_src)
642            = find_in_dir($try, $inc, undef, undef, undef, undef, $src);
643    }
644    if (! $has_option{prefix}) {
645        my $try = $a{pkgpath};
646        if (($need_itf && ! $found_itf) || ($need_lib && ! $found_lib)) {
647            my ($fi, $fl) = find_in_dir($try, $inc, $lib);
648            $found_itf  = $fi  if (! $found_itf  && $fi);
649            $found_lib  = $fl  if (! $found_lib  && $fl);
650        }
651
652        if ($need_jar && ! $found_jar) {
653            (undef, $found_jar) = find_in_dir($try, undef, undef, undef, $lib);
654        }
655
656        $try = $a{usrpath};
657        if (($need_itf && ! $found_itf) || ($need_lib && ! $found_lib)) {
658            my ($fi, $fl) = find_in_dir($try, $inc, $lib);
659            $found_itf  = $fi  if (! $found_itf  && $fi);
660            $found_lib  = $fl  if (! $found_lib  && $fl);
661        }
662
663        if ($need_jar && ! $found_jar) {
664            (undef, $found_jar) = find_in_dir($try, undef, undef, undef, $lib);
665        }
666    }
667    if (! $has_option{build}) {
668        if ($a{bldpath}) {
669            my $tolib = $need_build || ($need_lib && ! $found_lib);
670            my $tobin = $need_bin && ! $found_bin;
671            my $tojar = $need_jar && ! $found_jar;
672            if ($tolib || $tobin || $tojar) {
673                my ($fl, $fil, $found);
674                if ($OPT{'build-prefix'}) {
675                    my $try = $OPT{'build-prefix'};
676                    if ($tolib) {
677                        (undef, $fl, $fil)
678                            = find_in_dir($try, undef, $lib, $ilib);
679                        if ($fl || $fil) {
680                            $found_lib  = $fl  if (! $found_lib  && $fl);
681                            $found_ilib = $fil if (! $found_ilib && $fil);
682                            ++$found;
683                        }
684                    }
685                    if ($tojar) {
686                        (undef, $found_jar)
687                            = find_in_dir($try, undef, undef, undef, $lib);
688                    }
689                    if (! ($try =~ /$a{name}$/)) {
690                        $try = File::Spec->catdir($try, $a{name});
691                        if ($tolib && ! $found) {
692                            (undef, $fl, $fil)
693                                = find_in_dir($try, undef, $lib, $ilib);
694                            if ($fl || $fil) {
695                                $found_lib  = $fl  if (! $found_lib  && $fl);
696                                $found_ilib = $fil if (! $found_ilib && $fil);
697                                ++$found;
698                            }
699                        }
700                        if ($need_jar && ! $found_jar) {
701                            (undef, $found_jar)
702                                = find_in_dir($try, undef, undef, undef, $lib);
703                        }
704                    }
705                }
706                unless ($found || $fl || $fil) {
707                    my $try = $a{bldpath};
708                    $try = $a{locbldpath} if ($OPT{'local-build-out'});
709                    if ($tolib && ! $found) {
710                        (undef, $fl, $fil)
711                            = find_in_dir($try, undef, $lib, $ilib);
712                        my $resetLib = ! $found_lib;
713                        if (! $found_ilib && $fil) {
714                            $found_ilib = $fil;
715                            ++$resetLib;
716                        }
717                        $found_lib  = $fl  if ($resetLib && $fl);
718                    }
719                    if ($tobin && ! $found) {
720                        (undef, $fl, $fil) =
721                            find_in_dir($try, undef, $lib, $ilib, undef, $bin);
722                        $found_bin = $fl if ($fl);
723                    }
724                    if ($need_jar && ! $found_jar) {
725                        (undef, $found_jar)
726                            = find_in_dir($try, undef, undef, undef, $lib);
727                    }
728                }
729            }
730        }
731    }
732    if (($need_itf && ! $found_itf) || ($need_lib && ! $found_lib) ||
733        ($need_jar && ! $found_jar) || ($ilib && ! $found_ilib) ||
734        ($need_bin && ! $found_bin))
735    {
736        if ($is_optional) {
737            println "configure: optional $a{name} package not found: skipped.";
738        } elsif ($quasi_optional && $found_itf && ($need_lib && ! $found_lib)) {
739            println "configure: $a{name} package: "
740                . "found interface files but not libraries.";
741            $found_itf = abs_path($found_itf);
742            push(@dependencies, "$a{aname}_INCDIR = $found_itf");
743            println "includes: $found_itf";
744        } else {
745            if ($OPT{'debug'}) {
746                $_ = "$a{name}: includes: ";
747                $found_itf = '' unless $found_itf;
748                $_ .= $found_itf;
749                unless ($need_lib) {
750                    $_ .= "; libs: not needed";
751                } else {
752                    $found_lib = '' unless $found_lib;
753                    $_ .= "; libs: " . $found_lib;
754                }
755                unless ($ilib) {
756                    $_ .= "; ilibs: not needed";
757                } else {
758                    $found_ilib = '' unless $found_ilib;
759                    $_ .= "; ilibs: " . $found_ilib;
760                }
761                println "\t\t$_";
762            }
763            println "configure: error: required $a{name} package not found.";
764            exit 1;
765        }
766    } else {
767        if ($found_itf) {
768            $found_itf = abs_path($found_itf);
769            push(@dependencies, "$a{aname}_INCDIR = $found_itf");
770            println "includes: $found_itf";
771        }
772        if ($found_src) {
773            $found_src = abs_path($found_src);
774            push(@dependencies, "$a{aname}_SRCDIR = $found_src");
775            println "sources: $found_src";
776        }
777        if ($found_lib) {
778            $found_lib = abs_path($found_lib);
779            if ($a{aname} eq 'NGS' || $a{aname} eq 'VDB') {
780                if ($OPT{PYTHON_LIB_PATH}) {
781                    $OPT{PYTHON_LIB_PATH} .= ':';
782                } else {
783                    $OPT{PYTHON_LIB_PATH} = '';
784                }
785                $OPT{PYTHON_LIB_PATH} .= $found_lib;
786            }
787            push(@dependencies, "$a{aname}_LIBDIR = $found_lib");
788            println "libraries: $found_lib";
789        }
790        if ($ilib && $found_ilib) {
791            $found_ilib = abs_path($found_ilib);
792            push(@dependencies, "$a{aname}_ILIBDIR = $found_ilib");
793            println "ilibraries: $found_ilib";
794        }
795        if ($found_bin) {
796            $found_bin = abs_path($found_bin);
797            push(@dependencies, "$a{aname}_BINDIR = $found_bin");
798            println "bin: $found_bin";
799        }
800        if ($found_jar) {
801            $found_jar = abs_path($found_jar);
802            push(@dependencies, "$a{aname}_JAR = $found_jar");
803            println "jar: $found_jar";
804        }
805    }
806}
807
808my ($E_BINDIR, $E_LIBDIR, $E_VERSION_LIBX, $E_MAJVERS_LIBX,
809                          $E_VERSION_EXEX, $E_MAJVERS_EXEX)
810    = (''    , '');
811
812println unless ($AUTORUN);
813
814if ($OS ne 'win' && ! $OPT{'status'}) {
815    if ($OSTYPE =~ /darwin/i && CONFIG_OUT() ne '.') {
816        my $COMP = File::Spec->catdir(CONFIG_OUT(), 'COMP.mac');
817        println "configure: creating '$COMP' ($TOOLS)" unless ($AUTORUN);
818        open F, ">$COMP" or die "cannot open $COMP to write";
819        print F "$TOOLS\n";
820        close F;
821    }
822
823    if ($TOOLS =~ /gcc$/) {
824        my $EXECMDF = File::Spec->catdir(CONFIG_OUT(), 'ld.linux.exe_cmd.sh');
825        println "configure: creating '$EXECMDF'" unless ($AUTORUN);
826        open F, ">$EXECMDF" or die "cannot open $EXECMDF to write";
827        print F "EXE_CMD=\"\$LD $STATIC_LIBSTDCPP -static-libgcc\"\n";
828        close F;
829    }
830
831    # create Makefile.config
832    println "configure: creating '$OUT_MAKEFILE'" unless ($AUTORUN);
833    open my $F, ">$OUT_MAKEFILE" or die "cannot open $OUT_MAKEFILE to write";
834
835    print $F <<EndText;
836### AUTO-GENERATED FILE ###
837
838# configuration command
839
840CONFIGURED = $CONFIGURED
841
842OS_ARCH = \$(shell perl \$(TOP)/setup/os-arch.perl)
843
844# install paths
845EndText
846
847    L($F, "INST_BINDIR = \$(DESTDIR)$OPT{'bindir'}"      ) if ($OPT{'bindir'});
848    L($F, "INST_LIBDIR = \$(DESTDIR)$OPT{'libdir'}"      ) if ($OPT{'libdir'});
849    L($F, "INST_INCDIR = \$(DESTDIR)$OPT{'includedir'}"  ) if ($OPT{'includedir'});
850    L($F, "INST_SCHEMADIR = \$(DESTDIR)$OPT{'shemadir'}" ) if ($OPT{'shemadir'});
851    L($F, "INST_SHAREDIR = \$(DESTDIR)$OPT{'sharedir'}"  ) if ($OPT{'sharedir'});
852    L($F, "INST_JARDIR = \$(DESTDIR)$OPT{'javadir'}"     ) if ($OPT{'javadir'});
853    L($F, "INST_PYTHONDIR = \$(DESTDIR)$OPT{'pythondir'}") if ($OPT{'pythondir'});
854
855    my ($E_VERSION_SHLX, $VERSION_SHLX,
856        $E_MAJVERS_SHLX , $MAJMIN_SHLX, $MAJVERS_SHLX);
857    if ($OSTYPE =~ /darwin/i) {
858        $E_VERSION_SHLX =  '$VERSION.$SHLX';
859        $VERSION_SHLX = '$(VERSION).$(SHLX)';
860        $MAJMIN_SHLX  = '$(MAJMIN).$(SHLX)';
861        $E_MAJVERS_SHLX = '$MAJVERS.$SHLX';
862        $MAJVERS_SHLX = '$(MAJVERS).$(SHLX)';
863    } else {
864        $E_VERSION_SHLX =  '$SHLX.$VERSION';
865        $VERSION_SHLX = '$(SHLX).$(VERSION)';
866        $MAJMIN_SHLX  = '$(SHLX).$(MAJMIN)';
867        $E_MAJVERS_SHLX =  '$SHLX.$MAJVERS';
868        $MAJVERS_SHLX = '$(SHLX).$(MAJVERS)';
869    }
870
871    $E_VERSION_LIBX = '$LIBX.$VERSION';
872    $E_MAJVERS_LIBX = '$LIBX.$MAJVERS';
873
874    L($F);
875    L($F, "# build type");
876
877    if ($OPT{'enable-static'}) {
878        L($F, "WANTS_STATIC = 1");
879    }
880
881    $E_VERSION_EXEX = '$EXEX.$VERSION';
882    $E_MAJVERS_EXEX = '$LIBX.$MAJVERS';
883
884    print $F <<EndText;
885BUILD = $BUILD
886
887# target OS
888OS    = $OS
889OSINC = $OSINC
890OS_DISTRIBUTOR = $OS_DISTRIBUTOR
891
892# prefix string for system libraries
893LPFX = $LPFX
894
895# suffix strings for system libraries
896LIBX = $LIBX
897VERSION_LIBX = \$(LIBX).\$(VERSION)
898MAJMIN_LIBX  = \$(LIBX).\$(MAJMIN)
899MAJVERS_LIBX = \$(LIBX).\$(MAJVERS)
900
901SHLX         = $SHLX
902VERSION_SHLX = $VERSION_SHLX
903MAJMIN_SHLX  = $MAJMIN_SHLX
904MAJVERS_SHLX = $MAJVERS_SHLX
905
906# suffix strings for system object files
907OBJX = $OBJX
908LOBX = $LOBX
909
910# suffix string for system executable
911EXEX         = $EXEX
912VERSION_EXEX = \$(EXEX).\$(VERSION)
913MAJMIN_EXEX  = \$(EXEX).\$(MAJMIN)
914MAJVERS_EXEX = \$(EXEX).\$(MAJVERS)
915
916# system architecture and wordsize
917override ARCH = $ARCH
918EndText
919
920    L($F, "# ARCH = $ARCH ( $MARCH )") if ($ARCH ne $MARCH);
921
922    print $F <<EndText;
923BITS = $BITS
924
925# tools
926EndText
927
928    L($F, "CC            = $CC"           ) if ($CC);
929    L($F, "CPP           = $CPP"          ) if ($CPP);
930    L($F, "CP            = $CP"           ) if ($CP);
931    L($F, "AR            = $AR"           ) if ($AR);
932    L($F, "ARX           = $ARX"          ) if ($ARX);
933    L($F, "ARLS          = $ARLS"         ) if ($ARLS);
934    L($F, "LD            = $LD"           ) if ($LD);
935    L($F, "LP            = $LP"           ) if ($LP);
936    L($F, "PYTHON        = $PYTHON"       ) if ($PYTHON);
937    L($F, "JAVAC         = $JAVAC"        ) if ($JAVAC);
938    L($F, "JAVAH         = $JAVAH"        ) if ($JAVAH);
939    L($F, "JAR           = $JAR"          ) if ($JAR);
940    L($F, "MAKE_MANIFEST = $MAKE_MANIFEST") if ($MAKE_MANIFEST);
941    L($F);
942
943    L($F, '# tool options');
944    if ($BUILD eq "dbg") {
945        L($F, "DBG     = $DBG");
946        L($F, "OPT     = ");
947    } else {
948        L($F, "DBG     = -DNDEBUG") if ($PKG{LNG} eq 'C');
949        L($F, "OPT     = $OPT"    ) if ($OPT);
950    }
951    L($F, "PIC     = $PIC") if ($PIC);
952    if ($PKG{LNG} eq 'C') {
953        if ($OSTYPE =~ /darwin/i) {
954   L($F, 'SONAME  = -install_name ' .
955               '$(INST_LIBDIR)$(BITS)/$(subst $(VERSION),$(MAJVERS),$(@F)) \\');
956   L($F, '    -compatibility_version $(MAJMIN) -current_version $(VERSION) \\');
957   L($F, '    -flat_namespace -undefined suppress');
958        } else {
959      L($F, 'SONAME = -Wl,-soname=$(subst $(VERSION),$(MAJVERS),$(@F))');
960     }
961     L($F, "SRCINC  = $INC. $INC\$(SRCDIR)");
962    } elsif ($PKG{LNG} eq 'JAVA') {
963        L($F, 'SRCINC  = -sourcepath $(INCPATHS)');
964    }
965    if ($PIC) {
966        if (PACKAGE_NAMW() eq 'NGS') {
967            L($F, "INCDIRS = \$(SRCINC) $INC\$(TOP) "
968                .        "$INC\$(TOP)/ngs/\$(OSINC)/\$(ARCH)")
969        } elsif (PACKAGE_NAMW() eq 'NGS_BAM') {
970            L($F, "INCDIRS = \$(SRCINC) $INC\$(TOP) "
971                . "$INC\$(NGS_INCDIR)/ngs/\$(OSINC)/\$(ARCH)")
972        } else {
973            L($F, "INCDIRS = \$(SRCINC) $INC\$(TOP)")
974        }
975    }
976    if ($PKG{LNG} eq 'C') {
977        L($F, "CFLAGS  = \$(DBG) \$(OPT) \$(INCDIRS) $MD $ARCH_FL");
978    }
979    L($F, "LDFLAGS = $LDFLAGS") if ($LDFLAGS);
980
981    L($F, 'CLSPATH = -classpath $(CLSDIR)');
982    L($F, "NO_ARRAY_BOUNDS_WARNING = $NO_ARRAY_BOUNDS_WARNING");
983    L($F);
984
985# $PACKAGE_NAME and library version
986# \$(VERSION) is defined in a separate file which is updated every release
987    L($F, "include \$(TOP)/" . CONFIG_OUT() . "/Makefile.vers" );
988
989    print $F <<EndText;
990
991empty :=
992space := \$(empty) \$(empty)
993MAJMIN  = \$(subst \$(space),.,\$(wordlist 1,2,\$(subst .,\$(space),\$(VERSION))))
994MAJVERS = \$(firstword \$(subst .,\$(space),\$(VERSION)))
995
996# output path
997BUILD_PREFIX = $BUILD_PREFIX
998TARGDIR = $TARGDIR
999
1000# derived paths
1001MODPATH  ?= \$(subst \$(TOP)/,,\$(CURDIR))
1002SRCDIR   ?= \$(TOP)/\$(MODPATH)
1003MAKEFILE ?= \$(abspath \$(firstword \$(MAKEFILE_LIST)))
1004BINDIR    = \$(TARGDIR)/bin
1005EndText
1006
1007    if ($PKG{LNG} eq 'C') {
1008        $E_BINDIR        = '$TARGDIR/bin';
1009        $E_LIBDIR        = '$TARGDIR/lib';
1010        L($F, 'LIBDIR    = $(TARGDIR)/lib');
1011    } elsif ($PKG{LNG} eq 'JAVA') {
1012        $E_LIBDIR        = '$TARGDIR/jar';
1013        L($F, 'LIBDIR    = $(TARGDIR)/jar');
1014    }
1015
1016    L($F, 'ILIBDIR   = $(TARGDIR)/ilib');
1017    if ($PKG{NOMODPATH}) {
1018        L($F, 'OBJDIR    = $(TARGDIR)/obj');
1019    } else {
1020        L($F, 'OBJDIR    = $(TARGDIR)/obj/$(MODPATH)');
1021    }
1022    L($F, 'CLSDIR    = $(TARGDIR)/cls');
1023
1024    if ($PKG{LNG} eq 'JAVA') {
1025        L($F,
1026            "INCPATHS = \$(SRCDIR):\$(SRCDIR)/itf:\$(TOP)/gov/nih/nlm/ncbi/ngs")
1027    }
1028
1029    print $F <<EndText;
1030
1031# exports
1032export TOP
1033export MODPATH
1034export SRCDIR
1035export MAKEFILE
1036
1037# auto-compilation rules
1038EndText
1039
1040    if ($PKG{LNG} eq 'C') {
1041        L($F, '$(OBJDIR)/%.$(OBJX): %.c');
1042        T($F, '$(CC) -o $@ $< $(CFLAGS)');
1043        L($F, '$(OBJDIR)/%.$(LOBX): %.c');
1044        T($F, '$(CC) -o $@ $< $(PIC) $(CFLAGS)');
1045    }
1046    L($F, '$(OBJDIR)/%.$(OBJX): %.cpp');
1047    T($F, '$(CP) -std=c++11 -o $@ $< $(CFLAGS)');
1048    L($F, '$(OBJDIR)/%.$(LOBX): %.cpp');
1049    T($F, '$(CP) -std=c++11 -o $@ $< $(PIC) $(CFLAGS)');
1050    L($F);
1051
1052    # this is part of Makefile
1053    L($F, 'VPATH = $(SRCDIR)');
1054    L($F);
1055
1056    # we know how to find jni headers
1057    if ($PKG{LNG} eq 'JAVA' and $OPT{'with-ngs-sdk-src'}) {
1058        L($F, "JNIPATH = $OPT{'with-ngs-sdk-src'}/language/java");
1059    }
1060
1061    L($F, '# directory rules');
1062    if ($PKG{LNG} eq 'C') {
1063        L($F, '$(BINDIR) $(LIBDIR) $(ILIBDIR) '
1064            . '$(OBJDIR) $(INST_LIBDIR):');
1065        T($F, 'mkdir -p $@');
1066    } elsif ($PKG{LNG} eq 'JAVA') {
1067        # test if we have jni header path
1068        L($F, '$(LIBDIR) $(CLSDIR) $(INST_JARDIR):');
1069        T($F, 'mkdir -p $@');
1070    }
1071    L($F);
1072
1073    L($F, '# not real targets');
1074    L($F, '.PHONY: default clean install all std $(TARGETS)');
1075    L($F);
1076
1077    L($F, '# dependencies');
1078    if ($PKG{LNG} eq 'C') {
1079        L($F, 'include $(wildcard $(OBJDIR)/*.d)');
1080    } elsif ($PKG{LNG} eq 'JAVA') {
1081        L($F, 'include $(wildcard $(CLSDIR)/*.d)');
1082    }
1083    L($F, $_) foreach (@dependencies);
1084    L($F);
1085
1086    # pass HAVE_XML2 to build scripts
1087    L($F, 'ifeq (,$(HAVE_XML2))');
1088    L($F, '    HAVE_XML2=0');
1089    L($F, 'endif');
1090    L($F, 'CONFIGURE_FOUND_XML2=$(HAVE_XML2)');
1091    L($F, 'export CONFIGURE_FOUND_XML2');
1092    L($F);
1093
1094    if ($OS eq 'linux' || $OS eq 'bsd' || $OS eq 'mac') {
1095        L($F, '# installation rules');
1096        L($F,
1097        '$(INST_LIBDIR)/%.$(VERSION_LIBX): $(LIBDIR)/%.$(VERSION_LIBX)');
1098        T($F, '@ echo -n "installing \'$(@F)\'... "');
1099        T($F, '@ if $(BSD_INSTALL_DATA) $^ $@;                        \\');
1100        T($F, '  then                                                 \\');
1101        T($F, '      rm -f $(patsubst %$(VERSION),%$(MAJVERS),$@) '
1102                     . '$(patsubst %$(VERSION_LIBX),%$(LIBX),$@) '
1103                     . '$(patsubst %.$(VERSION_LIBX),%-static.$(LIBX),$@); \\');
1104        T($F, '      ln -s $(@F) $(patsubst %$(VERSION),%$(MAJVERS),$@);   \\');
1105        T($F, '      ln -s $(patsubst %$(VERSION),%$(MAJVERS),$(@F)) '
1106                      . '$(patsubst %$(VERSION_LIBX),%$(LIBX),$@); \\');
1107        T($F, '      ln -s $(patsubst %$(VERSION_LIBX),%$(LIBX),$(@F)) ' .
1108   '$(INST_LIBDIR)/$(patsubst %.$(VERSION_LIBX),%-static.$(LIBX),$(@F));'
1109                                                              . ' \\');
1110        T($F, '      echo success;                                    \\');
1111        T($F, '  else                                                 \\');
1112        T($F, '      echo failure;                                    \\');
1113        T($F, '      false;                                           \\');
1114        T($F, '  fi');
1115        L($F);
1116
1117        L($F,
1118        '$(INST_LIBDIR)/%.$(VERSION_SHLX): $(LIBDIR)/%.$(VERSION_SHLX)');
1119        T($F, '@ echo -n "installing \'$(@F)\'... "');
1120        T($F, '@ if $(BSD_INSTALL_LIB) $^ $@;                         \\');
1121        T($F, '  then                                                 \\');
1122        if ($OS ne 'mac') {
1123          T($F, '      rm -f $(patsubst %$(VERSION),%$(MAJVERS),$@) '
1124                      . '$(patsubst %$(VERSION_SHLX),%$(SHLX),$@);    \\');
1125        }
1126        if ($OS eq 'linux' || $OS eq 'bsd') {
1127          T($F, '      ln -s $(@F) $(patsubst %$(VERSION),%$(MAJVERS),$@); \\');
1128        } elsif ($OS eq 'mac') {
1129          T($F, '      ln -sf $(@F) '
1130                   . '$(patsubst %$(VERSION_SHLX),%$(MAJVERS).$(SHLX),$@); \\');
1131        } else {
1132          die;
1133        }
1134        T($F, '      ln -sf $(patsubst %$(VERSION),%$(MAJVERS),$(@F)) '
1135                      . '$(patsubst %$(VERSION_SHLX),%$(SHLX),$@); \\');
1136        T($F, '      echo success;                                    \\');
1137        T($F, '  else                                                 \\');
1138        T($F, '      echo failure;                                    \\');
1139        T($F, '      false;                                           \\');
1140        T($F, '  fi');
1141        L($F);
1142
1143        L($F, '$(INST_BINDIR)/%$(VERSION_EXEX): $(BINDIR)/%$(VERSION_EXEX)');
1144        T($F, '@ echo -n "installing \'$(@F)\'... "');
1145        T($F, '@ if $(BSD_INSTALL_PROGRAM) $^ $@;                     \\');
1146        T($F, '  then                                                 \\');
1147        T($F, '      rm -f $(patsubst %$(VERSION),%$(MAJVERS),$@) '
1148                      . '$(patsubst %$(VERSION_EXEX),%$(EXEX),$@);     \\');
1149        T($F, '      ln -s $(@F) $(patsubst %$(VERSION),%$(MAJVERS),$@);   \\');
1150        T($F, '      ln -s $(patsubst %$(VERSION),%$(MAJVERS),$(@F)) '
1151                      . '$(patsubst %$(VERSION_EXEX),%$(EXEX),$@); \\');
1152        T($F, '      echo success;                                    \\');
1153        T($F, '  else                                                 \\');
1154        T($F, '      echo failure;                                    \\');
1155        T($F, '      false;                                           \\');
1156        T($F, '  fi');
1157    }
1158    close $F;
1159
1160	# creation of Makefile.config.install is disabled, since nobody uses it now
1161	# and I need to remove versions from prl scripts
1162	if (0) {
1163	    # create Makefile.config.install
1164	    println "configure: creating '$INS_MAKEFILE'" unless ($AUTORUN);
1165	    open $F, ">$INS_MAKEFILE" or die "cannot open $INS_MAKEFILE to write";
1166
1167	    $OPT{'javadir' } = '' unless ($OPT{'javadir' });
1168	    $OPT{'sharedir'} = '' unless ($OPT{'sharedir'});
1169
1170	    print $F "sub CONFIGURE {\n";
1171	    print $F "    \$_{PACKAGE_NAME } = '$PACKAGE_NAME';\n";
1172	    print $F "    \$_{VERSION      } = '\$VERSION';\n";
1173	    print $F "    \$_{LNG          } = '$PKG{LNG}';\n";
1174	    print $F "    \$_{OS           } = '$OS';\n";
1175	    print $F "    \$_{BITS         } =  $BITS;\n";
1176	    print $F "    \$_{MAJVERS      } =  \$MAJVERS;\n";
1177	    print $F "    \$_{LPFX         } = '$LPFX';\n";
1178	    print $F "    \$_{LIBX         } = '$LIBX';\n";
1179	    print $F "    \$_{MAJVERS_LIBX } = '" . expand($E_MAJVERS_LIBX) . "';\n";
1180	    print $F "    \$_{VERSION_LIBX } = '" . expand($E_VERSION_LIBX) . "';\n";
1181	    print $F "    \$_{SHLX         } = '$SHLX';\n";
1182	    print $F "    \$_{MAJVERS_SHLX } = '" . expand($E_MAJVERS_SHLX) . "';\n";
1183	    print $F "    \$_{VERSION_SHLX } = '" . expand($E_VERSION_SHLX) . "';\n";
1184	    print $F "    \$_{VERSION_EXEX } = '" . expand($E_VERSION_EXEX) . "';\n";
1185	    print $F "    \$_{MAJVERS_EXEX } = '" . expand($E_MAJVERS_EXEX) . "';\n";
1186	    print $F "    \$_{INCDIR       } = '" . expand("$Bin/.."      ) . "';\n";
1187	    if ($PKG{LNG} ne 'PYTHON') {
1188	        print $F "  \$_{BINDIR$BITS} = '" . expand($E_BINDIR      ) . "';\n";
1189	        print $F "  \$_{LIBDIR$BITS} = '" . expand($E_LIBDIR      ) . "';\n";
1190	    } elsif ($OPT{PYTHON_LIB_PATH}) {
1191	        print $F "  \$_{LIBDIR$BITS} = '$OPT{PYTHON_LIB_PATH}';\n";
1192	    }
1193	    print $F "    \$_{OTHER_PREFIX } = '$PKG{UPATH}';\n";
1194	    print $F "    \$_{PREFIX       } = '$OPT{'prefix'}';\n";
1195	    print $F "    \$_{INST_INCDIR  } = '$OPT{'includedir'}';\n";
1196	    print $F "    \$_{INST_BINDIR  } = '$OPT{'bindir'}';\n";
1197	    print $F "    \$_{INST_LIBDIR  } = '$OPT{'libdir'}';\n";
1198	    print $F "    \$_{INST_JARDIR  } = '$OPT{'javadir'}';\n";
1199	    print $F "    \$_{INST_SHAREDIR} = '$OPT{'sharedir'}';\n";
1200	    print $F "\n";
1201	    print $F "    \@_\n";
1202	    print $F "}\n";
1203	    print $F "\n";
1204	    print $F "1\n";
1205
1206	    close $F;
1207    }
1208}
1209
1210if (! $OPT{'status'} ) {
1211    if ($OS eq 'win') {
1212        my $OUT = File::Spec->catdir(CONFIG_OUT(), 'Makefile.config.win');
1213        println "configure: creating '$OUT'";
1214        open OUT, ">$OUT" or die "cannot open $OUT to write";
1215        my $name = PACKAGE_NAMW();
1216        my $outdir = $name . '_OUTDIR';
1217        my $root = $name . '_ROOT';
1218
1219        print OUT <<EndText;
1220<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
1221  <PropertyGroup Label="Globals">
1222    <$outdir>$TARGDIR/\</$outdir>
1223EndText
1224        foreach my $href (@REQ) {
1225            my %a = %$href;
1226            my $NGS_SDK_PREFIX = '';
1227            $NGS_SDK_PREFIX = $a{found_itf} if ($a{found_itf});
1228            if ($a{name} eq 'ngs-sdk') {
1229                my $root = "$a{aname}_ROOT";
1230                print OUT "    <$root>$NGS_SDK_PREFIX\/</$root>\n";
1231                last;
1232            }
1233        }
1234        print OUT <<EndText;
1235    <$root>$Bin/\</$root>
1236  </PropertyGroup>
1237</Project>
1238EndText
1239        close OUT;
1240    } else {
1241        println "configure: creating 'Makefile.config'" unless ($AUTORUN);
1242        my $CONFIG_OUT = CONFIG_OUT();
1243        my $out = File::Spec->catdir($CONFIG_OUT, 'Makefile.config');
1244        open COUT, ">$out" or die "cannot open $out to write";
1245        print COUT "### AUTO-GENERATED FILE ###\n";
1246        print COUT "\n";
1247        print COUT "OS_ARCH = \$(shell perl \$(TOP)/setup/os-arch.perl)\n";
1248        print COUT "include \$(TOP)/$CONFIG_OUT/Makefile.config.\$(OS_ARCH)\n";
1249        close COUT;
1250    }
1251}
1252
1253unless ($OPT{'reconfigure'}) {
1254    println "configure: creating 'reconfigure'" unless ($AUTORUN);
1255    $CONFIGURED =~ s/\t/ /g;
1256    open my $F, '>reconfigure' or die 'cannot open reconfigure to write';
1257    print $F "./configure $CONFIGURED\n";
1258    close $F;
1259   # my $perm = (stat $fh)[2] & 07777;
1260#   print "==================================================== $perm\n";
1261}
1262
1263status() if ($OS ne 'win');
1264
1265unlink 'a.out';
1266
1267sub L { $_[1] = '' unless ($_[1]); print { $_[0] }   "$_[1]\n" }
1268sub T {                            print { $_[0] } "\t$_[1]\n" }
1269
1270sub status {
1271    my ($load) = @_;
1272    if ($load) {
1273        ($OS, $ARCH, $OSTYPE, $MARCH, @ARCHITECTURES) = OsArch();
1274        my $MAKEFILE
1275            = File::Spec->catdir(CONFIG_OUT(), "$OUT_MAKEFILE.$OS.$ARCH");
1276        println "\t\tloading $MAKEFILE" if ($OPT{'debug'});
1277        unless (-e $MAKEFILE) {
1278            print STDERR "configure: error: run ./configure [OPTIONS] first.\n";
1279            exit 1;
1280        }
1281        open F, $MAKEFILE or die "cannot open $MAKEFILE";
1282        foreach (<F>) {
1283            chomp;
1284            if (/BUILD = (.+)/) {
1285                $BUILD_TYPE = $1;
1286            } elsif (/BUILD \?= /) {
1287                $BUILD_TYPE = $_ unless ($BUILD_TYPE);
1288            } elsif (/BUILD_PREFIX = /) {
1289                $BUILD_PREFIX = $_;
1290            } elsif (/^CC += (.+)/) {
1291                $CC = $1;
1292            } elsif (/CONFIGURED = (.*)/) {
1293                $CONFIGURED = $1;
1294            } elsif (/CPP += (.+)/) {
1295                $CPP = $1;
1296            } elsif (/LDFLAGS += (.+)/) {
1297                $LDFLAGS = $1;
1298            } elsif (/TARGDIR = /) {
1299                $TARGDIR = $_;
1300                println "\t\tgot $_" if ($OPT{'debug'});
1301            } elsif (/TARGDIR \?= (.+)/) {
1302                $TARGDIR = $1 unless ($TARGDIR);
1303                println "\t\tgot $_" if ($OPT{'debug'});
1304            }
1305            elsif (/INST_INCDIR = (.+)/) {
1306                $OPT{'includedir'} = $1;
1307            }
1308            elsif (/INST_BINDIR = (.+)/) {
1309                $OPT{'bindir'} = $1;
1310            }
1311            elsif (/INST_LIBDIR = (.+)/) {
1312                $OPT{'libdir'} = $1;
1313            }
1314        }
1315    }
1316
1317    println "build type: $BUILD_TYPE";
1318    println "build prefix: $BUILD_PREFIX" if ($OS ne 'win');
1319    println "build output path: $TARGDIR" if ($OS ne 'win');
1320
1321#   print "prefix: ";    print $OPT{'prefix'} if ($OS ne 'win');    println;
1322#   print "eprefix: ";    print $OPT{'eprefix'} if ($OPT{'eprefix'});   println;
1323
1324    print "includedir: ";
1325    print $OPT{'includedir'} if ($OPT{'includedir'});
1326    println;
1327
1328    print "bindir: ";
1329    print $OPT{'bindir'} if ($OPT{'bindir'});
1330    println;
1331
1332    print "libdir: ";
1333    print $OPT{'libdir'} if ($OPT{'libdir'});
1334    println;
1335
1336    println "schemadir: $OPT{'shemadir'}" if ($OPT{'shemadir'});
1337    println "sharedir: $OPT{'sharedir'}" if ($OPT{'sharedir'});
1338    println "javadir: $OPT{'javadir'}" if ($OPT{'javadir'});
1339    println "pythondir: $OPT{'pythondir'}" if ($OPT{'pythondir'});
1340
1341    println "CC = $CC"   if ($CC );
1342    println "CPP = $CPP" if ($CPP);
1343    println "LDFLAGS = $LDFLAGS" if ($LDFLAGS);
1344
1345    $CONFIGURED =~ s/\t/ /g;
1346    println "configured with: \"$CONFIGURED\"";
1347}
1348
1349sub expand { $_[0] =~ s/(\$\w+)/$1/eeg; $_[0]; }
1350
1351sub expand_path {
1352    my ($filename) = @_;
1353    return unless ($filename);
1354
1355    if ($filename =~ /^~/) {
1356        if ($filename =~ m|^~([^/]*)|) {
1357            if ($1 && ! getpwnam($1)) {
1358                print "configure: error: bad path: '$filename'\n";
1359                exit 1;
1360            }
1361        }
1362
1363        $filename =~ s{ ^ ~ ( [^/]* ) }
1364                      { $1
1365                            ? (getpwnam($1))[7]
1366                            : ( $ENV{HOME} || $ENV{USERPROFILE} || $ENV{LOGDIR}
1367                                || (getpwuid($<))[7]
1368                              )
1369                      }ex;
1370    }
1371
1372    my $a = abs_path($filename);
1373    $filename = $a if ($a);
1374    $filename;
1375}
1376
1377sub find_in_dir {
1378    my ($dir, $include, $lib, $ilib, $jar, $bin, $src) = @_;
1379    unless (-d $dir) {
1380        println "\t\tnot found $dir" if ($OPT{'debug'});
1381        return;
1382    }
1383    my ($found_inc, $found_lib, $found_ilib, $found_src);
1384    if ($include) {
1385        print "\tincludes... " unless ($AUTORUN);
1386        if (-e "$dir/$include") {
1387            println $dir unless ($AUTORUN);
1388            $found_inc = $dir;
1389        } elsif (-e "$dir/include/$include") {
1390            println $dir unless ($AUTORUN);
1391            $found_inc = "$dir/include";
1392        } elsif (-e "$dir/interfaces/$include") {
1393            println $dir unless ($AUTORUN);
1394            $found_inc = "$dir/interfaces";
1395        } else {
1396            print "$dir: " if ($OPT{'debug'});
1397            println 'no' unless ($AUTORUN);
1398        }
1399    }
1400    if ($lib || $ilib) {
1401        print "\tlibraries... " unless ($AUTORUN);
1402        if ($lib) {
1403            my $builddir = File::Spec->catdir($dir, $OS, $TOOLS, $ARCH, $BUILD);
1404            my $libdir  = File::Spec->catdir($builddir, 'lib');
1405            my $ilibdir = File::Spec->catdir($builddir, 'ilib');
1406            my $f = File::Spec->catdir($libdir, $lib);
1407            print "\n\t\tchecking $f\n\t" if ($OPT{'debug'});
1408            my $found;
1409            if (-e $f) {
1410                $found_lib = $libdir;
1411                if ($ilib) {
1412                    my $f = File::Spec->catdir($ilibdir, $ilib);
1413                    print "\tchecking $f\n\t" if ($OPT{'debug'});
1414                    if (-e $f) {
1415                        println $ilibdir;
1416                        $found_ilib = $ilibdir;
1417                    } else {
1418                        println 'no' unless ($AUTORUN);
1419                        return;
1420                    }
1421                } else {
1422                    println $libdir;
1423                }
1424                ++$found;
1425            }
1426            if (! $found) {
1427                my $libdir = File::Spec->catdir($dir, 'lib');
1428                my $f = File::Spec->catdir($libdir, $lib);
1429                print "\tchecking $f\n\t" if ($OPT{'debug'});
1430                if (-e $f) {
1431                    println $libdir;
1432                    $found_lib = $libdir;
1433                    ++$found;
1434                }
1435            }
1436            if (! $found) {
1437                my $builddir = File::Spec->catdir
1438                    ($dir, $OS, $TOOLS, $ARCH, reverse_build($BUILD));
1439                my $libdir  = File::Spec->catdir($builddir, 'lib');
1440                my $ilibdir = File::Spec->catdir($builddir, 'ilib');
1441                my $f = File::Spec->catdir($libdir, $lib);
1442                print "\tchecking $f\n\t" if ($OPT{'debug'});
1443                if (-e $f) {
1444                    $found_lib = $libdir;
1445                    if ($ilib) {
1446                        my $f = File::Spec->catdir($ilibdir, $ilib);
1447                        print "\tchecking $f\n\t" if ($OPT{'debug'});
1448                        if (-e $f) {
1449                            println $ilibdir;
1450                            $found_ilib = $ilibdir;
1451                        } else {
1452                            println 'no' unless ($AUTORUN);
1453                            return;
1454                        }
1455                    } else {
1456                        println $libdir;
1457                    }
1458                    ++$found;
1459                } else {
1460                    println 'no' unless ($AUTORUN);
1461                }
1462            }
1463        }
1464        if ($found_lib && $ilib && ! $found_ilib) {
1465            println "\n\t\tfound $found_lib but no ilib/" if ($OPT{'debug'});
1466            print "\t" if ($OPT{'debug'});
1467            println 'no' unless ($AUTORUN);
1468            undef $found_lib;
1469        }
1470    }
1471    if ($bin) {
1472        print "\t... " unless ($AUTORUN);
1473        my $builddir = File::Spec->catdir($dir, $OS, $TOOLS, $ARCH, $BUILD);
1474        my $bdir  = File::Spec->catdir($builddir, 'bin');
1475        my $f = File::Spec->catdir($bdir, $bin);
1476        print "\n\t\tchecking $f\n\t" if ($OPT{'debug'});
1477        if (-e $f) {
1478            $found_lib = $bdir;
1479            println $bdir;
1480        } else {
1481            println 'no' unless ($AUTORUN);
1482        }
1483    }
1484    if ($jar) {
1485        print "\tjar... " unless ($AUTORUN);
1486        my $try = "$dir/jar/$jar";
1487        if (-e "$try") {
1488            println $try unless ($AUTORUN);
1489            $found_lib = $try;
1490        }
1491    }
1492    if ($src) {
1493        print "\tsrc... " unless ($AUTORUN);
1494        my $try = "$dir/$src";
1495        if (-e "$try") {
1496            println $dir unless ($AUTORUN);
1497            $found_src = $dir;
1498        }
1499    }
1500    return ($found_inc, $found_lib, $found_ilib, $found_src);
1501}
1502
1503sub reverse_build {
1504    ($_) = @_;
1505    if ($_ eq 'rel') {
1506        return 'dbg';
1507    } elsif ($_ eq 'dbg') {
1508        return 'rel';
1509    } else {
1510        die $_;
1511    }
1512}
1513
1514################################################################################
1515
1516sub check_tool_h  { return check_tool(@_,  '-help'); }
1517sub check_tool__h { return check_tool(@_, '--help'); }
1518
1519sub check_tool {
1520    my ($tool, $o) = @_;
1521    print "checking for $tool... ";
1522    my $cmd = "$tool $o";
1523    print "\n\t\trunning $cmd\n\t" if ($OPT{'debug'});
1524    my $out = `$cmd 2>&1`;
1525    if ($? == 0) {
1526        println "yes";
1527        return 1;
1528    } else {
1529        println "no";
1530        return 0;
1531    }
1532}
1533
1534sub check_qmake {
1535    print "checking for QMake... ";
1536
1537    my $tool = 'qmake';
1538    print "\n\t\trunning $tool... " if ($OPT{'debug'});
1539    my $out = `$tool -v 2>&1`;
1540    if ($? == 0) {
1541        my $out = `( $tool -v | grep QMake ) 2>&1`;
1542        if ($? == 0) {
1543            print "$out " if ($OPT{'debug'});
1544            println $tool;
1545            return $tool;
1546        }
1547
1548        println "wrong qmake" if ($OPT{'debug'});
1549
1550        print "\t\tchecking $ENV{PATH}...\n" if ($OPT{'debug'});
1551        foreach ( split(/:/, $ENV{PATH})) {
1552            my $cmd = "$_/$tool";
1553            print "\t\trunning $cmd... " if ($OPT{'debug'});
1554            my $out = `( $cmd -v | grep QMake ) 2>&1`;
1555            if ($? == 0) {
1556                print "$out " if ($OPT{'debug'});
1557                if ( $out =~ /QMake/ ) {
1558                    println $cmd;
1559                    return $cmd;
1560                }
1561            }
1562
1563            println "no" if ($OPT{'debug'});
1564        }
1565    }
1566
1567    if ( $OS eq 'linux' ) {
1568        if ( $OS_DISTRIBUTOR eq 'CentOS' ) {
1569            foreach ( glob ( "$ENV{HOME}/Qt/*/gcc_64" ) ) {
1570                $tool =  "$_/bin/qmake";
1571                print "\n\t\tchecking $tool... " if ($OPT{'debug'});
1572                my $out = `( $tool -v | grep QMake ) 2>&1`;
1573                if ($? == 0) {
1574                    print "$out " if ($OPT{'debug'});
1575                    println $tool;
1576                    return $tool;
1577                }
1578            }
1579
1580            $tool = '/usr/lib64/qt5/bin/qmake';
1581
1582        } elsif ( $OS_DISTRIBUTOR eq 'Ubuntu' ) {
1583            foreach ( glob ( "$ENV{HOME}/Qt*/*/gcc_64" ) ) {
1584                $tool =  "$_/bin/qmake";
1585                print "\n\t\tchecking $tool... " if ($OPT{'debug'});
1586                my $out = `( $tool -v | grep QMake ) 2>&1`;
1587                if ($? == 0) {
1588                    print "$out " if ($OPT{'debug'});
1589                    println $tool;
1590                    return $tool;
1591                }
1592            }
1593
1594            $tool = '';
1595        }
1596    } elsif ( $OS eq 'mac' ) {
1597        $tool = '/Applications/QT/5.10.1/clang_64/bin/qmake';
1598    }
1599
1600    if ( $tool ) {
1601        print "\n\t\tchecking $tool... " if ($OPT{'debug'});
1602        my $out = `( $tool -v | grep QMake ) 2>&1`;
1603        if ($? == 0) {
1604            print "$out " if ($OPT{'debug'});
1605            println $tool;
1606            return $tool;
1607        }
1608    }
1609
1610    println "no";
1611    return '';
1612}
1613
1614sub check_static_libstdcpp {
1615    my $option = '-static-libstdc++';
1616
1617    print "checking whether $CPP accepts $option... ";
1618
1619    my $log = 'int main() {}\n';
1620    my $cmd = $log;
1621    $cmd =~ s/\\n/\n/g;
1622    my $gcc = "echo -e '$log' | $CPP -xc $option - 2>&1";
1623    print "\n\t\trunning $gcc\n" if ($OPT{'debug'});
1624    my $out = `$gcc`;
1625    my $ok = $? == 0;
1626    if ( $ok && $out ) {
1627        $ok = 0 if ( $out =~ /unrecognized option '-static-libstdc\+\+'/ );
1628    }
1629    print "$out\t" if ($OPT{'debug'});
1630    println $ok ? 'yes' : 'no';
1631
1632    unlink 'a.out';
1633
1634    return '' if (!$ok);
1635
1636    return $option;
1637}
1638
1639sub check_no_array_bounds {
1640    check_compiler('O', '-Wno-array-bounds');
1641}
1642
1643sub find_lib {
1644    check_compiler('L', @_);
1645}
1646
1647sub check_compiler {
1648    my ($t, $n, $I, @l) = @_;
1649    my $tool = $CC;
1650
1651    if ($t eq 'L') {
1652        print "checking for $n library... ";
1653    } elsif ($t eq 'O') {
1654        if ($tool && ($tool =~ /gcc/ || $tool =~ /g\+\+/)) {
1655            print "checking whether $tool accepts $n... ";
1656        } else {
1657            return;
1658        }
1659    } else {
1660        die "Unknown check_compiler option: '$t'";
1661    }
1662
1663    unless ($tool) {
1664        println "warning: unknown tool";
1665        return;
1666    }
1667
1668    while (1) {
1669        my ($flags, $library, $log) = ('', '');
1670
1671        if ($t eq 'O') {
1672            $flags = $n;
1673            $log = '                      int main() {                     }\n'
1674        } elsif ($n eq 'hdf5') {
1675            $library = '-Wl,-Bstatic -lhdf5 -Wl,-Bdynamic -ldl -lm -lz';
1676            $log = '#include <hdf5.h>  \n int main() { H5close         (); }\n'
1677        } elsif ($n eq 'fuse') {
1678            $flags = '-D_FILE_OFFSET_BITS=64';
1679            $library = '-lfuse';
1680            $log = '#include <fuse.h>  \n int main() { fuse_get_context(); }\n'
1681        } elsif ($n eq 'magic') {
1682            $library = '-lmagic';
1683            $log = '#include <magic.h> \n int main() { magic_open     (0); }\n'
1684        } elsif ($n eq 'xml2') {
1685            $library  = '-lxml2';
1686            $library .=       ' -liconv' if ($OS eq 'mac');
1687            $log = '#include <libxml/xmlreader.h>\n' .
1688                                         'int main() { xmlInitParser  ( ); }\n'
1689        } else {
1690            println 'unknown: skipped';
1691            return;
1692        }
1693
1694        if ($I && ! -d $I) {
1695            print "'$I': " if ($OPT{'debug'});
1696            println 'no';
1697            return;
1698        }
1699
1700        for ( my $i = 0; $i <= $#l; ++ $i ) {
1701            print "'$l[$i]': " if ($OPT{'debug'});
1702            if ( $l [ $i ] ) {
1703                if ( -d $l [ $i ] ) {
1704                    last;
1705                } elsif ( $i ==  $#l ) {
1706                    println 'no';
1707                    return;
1708                }
1709            }
1710        }
1711
1712        my $cmd = $log;
1713        $cmd =~ s/\\n/\n/g;
1714
1715        push ( @l, '' ) unless ( @l );
1716        for my $i ( 0 .. $#l ) {
1717            my $l = $l [ $i ];
1718            if ( $l && ! -d $l ) {
1719                if ( $i == $#l ) {
1720                    println 'no';
1721                    return;
1722                } else {
1723                    next;
1724                }
1725            }
1726            my $gcc = "| $tool -xc $flags " . ($I ? "-I$I " : ' ')
1727                                      . ($l ? "-L$l " : ' ') . "-o a.out - $library";
1728            $gcc .= ' 2> /dev/null' unless ($OPT{'debug'});
1729
1730            open GCC, $gcc or last;
1731            print "\n\t\trunning echo -e '$log' $gcc\n" if ($OPT{'debug'});
1732            print GCC "$cmd" or last;
1733            my $ok = close GCC;
1734            print "\t" if ($OPT{'debug'});
1735            if ( $ok ) {
1736                println 'yes';
1737            } else {
1738                println 'no' if ( $i == $#l );
1739            }
1740
1741            unlink 'a.out';
1742
1743            return if ( ! $ok && ( $i == $#l ) );
1744
1745            return 1 if ($t eq 'O');
1746
1747            return ($I, $l) if ( $ok) ;
1748        }
1749    }
1750
1751    println "cannot run $tool: skipped";
1752}
1753
1754################################################################################
1755
1756sub check {
1757    die "No CONFIG_OUT"   unless CONFIG_OUT();
1758    die "No PACKAGE"      unless PACKAGE();
1759    die "No PACKAGE_NAME" unless PACKAGE_NAME();
1760    die "No PACKAGE_NAMW" unless PACKAGE_NAMW();
1761    die "No PACKAGE_TYPE" unless PACKAGE_TYPE();
1762
1763    my %PKG = PKG();
1764
1765    die "No LNG"    unless $PKG{LNG};
1766    die "No LOCOUT" unless $PKG{LOCOUT};
1767    die "No OUT"    unless $PKG{OUT};
1768    die "No PATH"   unless $PKG{PATH};
1769    die "No UPATH"  unless $PKG{UPATH};
1770
1771    foreach my $href (DEPENDS()) { die "No DEPENDS::name" unless $href->{name} }
1772
1773    foreach my $href (REQ()) {
1774        die         "No REQ::name" unless $href->{name};
1775
1776        die         "No $href->{name}:option"  unless $href->{option}
1777                                                   || $href->{boption};
1778
1779        die         "No $href->{name}:type"    unless $href->{type};
1780        unless ($href->{type} =~ /I/) {
1781          unless ($href->{type} =~ /E/) {
1782            die     "No $href->{name}:lib"     unless $href->{lib};
1783          }
1784            die     "No $href->{name}:pkgpath" unless $href->{pkgpath};
1785            die     "No $href->{name}:usrpath" unless $href->{usrpath};
1786        }
1787
1788        die         "No $href->{name}:origin"  unless $href->{origin};
1789        if ($href->{origin} eq 'I') {
1790            die     "No $href->{name}:aname"   unless $href->{aname};
1791            unless ($href->{type} =~ /D/ || $href->{type} =~ /E/
1792                                         || $href->{type} =~ /J/)
1793            {
1794                die "No $href->{name}:include" unless $href->{include};
1795                die "No $href->{name}:srcpath" unless $href->{srcpath};
1796            }
1797            unless ($href->{type} =~ /I/) {
1798                die "No $href->{name}:bldpath"    unless $href->{bldpath   };
1799                die "No $href->{name}:locbldpath" unless $href->{locbldpath};
1800            }
1801            if ($href->{type} =~ /B/) {
1802                die "No $href->{name}:ilib"    unless $href->{ilib};
1803            }
1804        }
1805    }
1806}
1807
1808################################################################################
1809
1810sub optional { $_[0] =~ /O/ }
1811
1812sub help {
1813#  --prefix=PREFIX         install architecture-independent files in PREFIX
1814    print <<EndText;
1815`configure' configures $PACKAGE_NAME to adapt to many kinds of systems.
1816
1817Usage: ./configure [OPTION]...
1818
1819Defaults for the options are specified in brackets.
1820
1821Configuration:
1822  -h, --help              display this help and exit
1823
1824EndText
1825
1826    if ($^O ne 'MSWin32') {
1827        print <<EndText;
1828Installation directories:
1829  --prefix=PREFIX         install all files in PREFIX
1830                          [$package_default_prefix]
1831
1832EndText
1833
1834        my $other_prefix = $PKG{UPATH};
1835        if ($PACKAGE eq 'sra-tools' && 0) {
1836            print <<EndText;
1837  --shemadir=DIR          install schema files in DIR
1838                          [$schema_default_dir]
1839
1840EndText
1841        }
1842
1843        print "By default, \`make install' will install all the files in\n";
1844
1845        if (PACKAGE_TYPE() eq 'B') {
1846            print "\`$package_default_prefix/bin', ";
1847        } elsif (PACKAGE_TYPE() eq 'L') {
1848            print "\`$package_default_prefix/include', ";
1849        }
1850        if (PACKAGE_TYPE() eq 'P') {
1851            println "\`$package_default_prefix/share' etc.";
1852        } else {
1853            println "\`$package_default_prefix/lib' etc.";
1854        }
1855
1856        print <<EndText;
1857You can specify an installation prefix other than \`$package_default_prefix'
1858using \`--prefix', for instance \`--prefix=$other_prefix'.
1859EndText
1860    }
1861
1862    print <<EndText;
1863
1864For better control, use the options below.
1865
1866EndText
1867
1868    my ($required, $optional);
1869    foreach my $href (@REQ) {
1870        if (optional($href->{type})) {
1871            ++$optional;
1872        } else {
1873            ++$required;
1874        }
1875    }
1876
1877    if ($required) {
1878        print "Required Packages:\n";
1879        foreach my $href (@REQ) {
1880            next if (optional($href->{type}));
1881            my %a = %$href;
1882            if ($a{type} =~ /S/) {
1883                println "  --$a{option}=DIR    search for $a{name} package";
1884                println "                                 source files in DIR";
1885            } else {
1886                unless ($a{type} =~ /E/) {
1887                  println
1888                    "  --$a{option}=DIR      search for $a{name} package in DIR"
1889                }
1890            }
1891            if ($a{boption}) {
1892                println "  --$a{boption}=DIR      search for $a{name} package";
1893                println "                                 build output in DIR";
1894            }
1895            println;
1896        }
1897    }
1898
1899    if ($optional) {
1900        print "Optional Packages:\n";
1901        foreach my $href (@REQ) {
1902            next unless (optional($href->{type}));
1903            my %a = %$href;
1904            if ($a{option} && $a{option} =~ /-sources$/) {
1905                println "  --$a{option}=DIR    search for $a{name} package";
1906                println "                                source files in DIR";
1907            } elsif ($a{boption} && $a{boption} =~ /-build$/) {
1908                println "  --$a{boption}=DIR     search for $a{name} package";
1909                println "                                 build output in DIR";
1910            } else {
1911                println "  --$a{option}=DIR    search for $a{name} files in DIR"
1912            }
1913        }
1914        println;
1915    }
1916
1917    print <<EndText if (PACKAGE_TYPE() eq 'B');
1918Optional Features:
1919  --enable-static         build static executable [default=no]
1920
1921EndText
1922
1923    my ($OS, $ARCH, $OSTYPE, $MARCH, @ARCHITECTURES) = OsArch();
1924
1925    if ($^O ne 'MSWin32') {
1926        print "Build tuning:\n";
1927        if ($PKG{LNG} ne 'JAVA') {
1928            print <<EndText;
1929  --with-debug
1930  --without-debug
1931EndText
1932        }
1933
1934        if (@ARCHITECTURES) {
1935            print
1936"  --arch=name             specify the name of the target architecture\n";
1937        }
1938
1939        if ($PKG{LNG} eq 'JAVA') {
1940            print <<EndText;
1941  --source=release        provide source compatibility with specified release,
1942                          generate class files for specified VM version.
1943                          e.g. `--source=1.6'
1944EndText
1945        } else {
1946            print "\n";
1947        }
1948
1949        print <<EndText;
1950  --relative-build-out-dir generate build output into directory
1951                           relative to sources [$OUTDIR]
1952  --build-prefix=DIR       generate build output into DIR directory
1953                           [$OUTDIR]
1954
1955EndText
1956    }
1957
1958    println 'Miscellaneous:';
1959    println '  --reconfigure            rerun `configure\'';
1960    println '                           using the same command-line arguments';
1961    if ($^O ne 'MSWin32') {
1962        println
1963            '  --status                 print current configuration information'
1964    }
1965    print <<EndText;
1966  --clean                  remove all configuration results
1967  --debug                  print lots of debugging information
1968
1969If `configure' was already run running `configure' without options
1970will rerun `configure' using the same command-line arguments.
1971
1972Report bugs to sra-tools\@ncbi.nlm.nih.gov
1973EndText
1974}
1975
1976################################################################################
1977