#!/usr/local/bin/perl5
##
##       This utility is part of AIPS++, a software project centered at
##       the National Radio Astronomy Observatory.
##
##       Copyright (C) 1995,1997,1998,1999,2000
##       Associated Universities, Inc. Washington DC, USA.
## 
##       This utility is free software; you can redistribute it and/or modify
##       it under the same terms as Perl itself, i.e., under the terms of the
##       "Artistic License" or the "GNU General Public License". 
##
## This script goes through all of the aips++ libraries and generates
## DBM files containing all of the library symbols. These symbols can then be
## used for comparison against updated libraries. The only required paramter
## is a directory where the DBM files should be stored:
##
##	patch_chkpoint [ -E <EXCLUDE-REGEX> ] /the/directory/to/use
##
## multiple -E flags can be used to exclude any libraries which should not
## be considered.
## 

BEGIN {@AnyDBM_File::ISA = qw(GDBM_File NDBM_File DB_File SDBM_File ODBM_File);}
use AnyDBM_File;
use POSIX;

warn "\n*** THIS HAS ONLY BEEN TESTED ON LINUX ***" if `uname -a 2>&1` !~ m/linux/i;

($aroot, $aarch, $asite, $ahost) = split( /\s+/, $ENV{CASAPATH} );

@exclude = ();
while ($_ = @ARGV[0], (/^-/)) {
    shift(@ARGV);
    /^-E$/ && (push(@exclude,shift(@ARGV)), next);
    die;
}

$dir = shift(@ARGV);

die "no output directory specified" unless $dir;

die "can't open (or create or write to) \"$dir\""
  unless -d $dir && -w _ || mkdir( $dir, 0755 );
die "can't open \"$aroot/$aarch/lib\""
  unless opendir( LIB, "$aroot/$aarch/lib" );

die "can't attach to \"$dir/objects\", $!"
  unless tie( %objs, AnyDBM_File, "$dir/objects", O_RDWR | O_CREAT, 0644);
die "can't attach to \"$dir/symbols\", $!"
  unless tie( %symbols, AnyDBM_File, "$dir/symbols", O_RDWR | O_CREAT, 0644);
%objs = ();
%symbols = ();

libloop: foreach ( readdir(LIB) ) {
    if ( m/\.a$/ ) {
        foreach $re ( @exclude ) {
            next libloop if m/$re/;
        }
        ($lib = $_) =~ s/\.a$//;
        open( OBJS, "ar tv $aroot/$aarch/lib/$_ |" );
        foreach ( <OBJS> ) {
            if ( m/(\d+) (\w+) (\d\d) ([^\s]+) (\d+) ([^\s]+)\.o$/ ||
                 m/(\d+) (\w+)  (\d) ([^\s]+) (\d+) ([^\s]+)\.o$/ ) {
                die "object file name clash, $6.o" if defined $objs{$6};
                $objs{"$lib:$6"} = "$1 $3$2$5 $4";
            }
        }
        ##
        ## The GNU nm symbol types (uppercase implies global, lowercase implies local) :
        ## -------------------------------------------------------------------------------------
        ## A    The symbol's value is absolute, and will not be changed by further linking.
        ##
        ## B    The symbol is in the uninitialized data section (known as BSS).
        ##      gcc: Static variables, enums, etc.
        ##
        ## C    The symbol is common.  Common symbols are uninitialized data. When linking,
        ##      multiple common symbols may appear with the same name.  If the symbol is defined
        ##      anywhere, the common symbols are treated as undefined references.
        ##      gcc: runtime type information (compiler created)
        ##
        ## D    The symbol is in the initialized data section.
        ##      gcc: initialized global variables, initialized class statics.
        ##
        ## G    The symbol is in an initialized data section for small objects.  Some object
        ##      file formats permit more efficient access to small data objects, such as a global
        ##      int variable as opposed to a large global array.
        ##
        ## I    The symbol is an indirect reference to another symbol.  This is a GNU extension
        ##      to the a.out object file format which is rarely used.
        ##
        ## N    The symbol is a debugging symbol.
        ##
        ## R    The symbol is in a read only data section.
        ##      gcc: initialized global variables, initialized class statics.
        ##
        ## S    The symbol is in an uninitialized data section for small objects.
        ##
        ## T    The symbol is in the text (code) section.
        ##
        ## U    The symbol is undefined.
        ##
        ## W    The symbol is weak.  When a weak defined symbol is linked with a normal defined
        ##      symbol, the normal defined symbol is used with no error.  When a weak undefined
        ##      symbol is linked and the symbol is not defined, the value of the weak symbol
        ##      becomes zero with no error.
        ##      gcc: compiler supplied functions, e.g. copy constructors.
        ##
        ## -    The symbol is a stabs symbol in an a.out object file.  In this case, the next
        ##      values printed are the stabs other field, the stabs desc field, and the stab type.
        ##      Stabs symbols are used to hold debugging information.
        ##
        ## ?    The symbol type is unknown, or object file format specific.
        ##
        close(OBJS);
        open( SYMBOLS, "nm --extern-only --print-file-name $aroot/$aarch/lib/$_ |" );
        foreach ( <SYMBOLS> ) {
            ##                                    vvvv----------> must change in patch_findbin too!
            if ( m/:([^\s]+)\.o:(?:[a-f0-9]+)?\s+[TBDR]\s+([^\s]+)[\s\n]*$/ ) {
                die "symbol clash, $2 (in $lib:$1) previous " . $symbols{$2} . "\n" if defined $symbols{$2};
                $symbols{$2} = "$lib:$1";
            }
        }
        close(SYMBOLS);
    }
}

closedir(LIB);
untie( %objs );
untie( %symbols );
