#!/usr/bin/perl

# --------------------
# This script warns about undefined symbols in the arb perl module
#     ../lib/ARB.pm
#
# Most public functions in library ARBDB will be exported to arb perl.
#
# If you get warnings about unsatisfied symbols you should fix them,
# as they will cause runtime errors on some systems (macOS >= 12).
#
# Possible fixes:
# - declare function as 'static'
# - prefix a function with 'NOT4PERL' to avoid it gets exported.
#
# --------------------

use strict;
use warnings;

sub warn_undefined_symbols() {
  my $warned = 0;

  while (defined($_=<>)) {
    chomp;

    my ($sym, $rest);
    if (/ [A-Z] /o) { ($sym, $rest) = ($`, $&.$'); }
    else            { ($sym, $rest) = ($_, undef); }

    my $msgtype = 'Warning';

    if    ($sym =~ /^Perl_/o) { $msgtype = undef; }
    elsif ($sym =~ /^PL_/o) { $msgtype = undef; }
    elsif ($sym =~ /^pthread_getspecific$/o) { $msgtype = undef; }
    elsif ($sym =~ /\@\@(GCC|CXXABI|GLIBC|GLIBCXX)_[0-9\.]+/o) { $msgtype = undef; }

    if (defined $msgtype) {
      print "ARB.c:0: $msgtype: xsub references unsatisfied symbol '$sym'\n";
      $warned++;
    }
  }

  if ($warned>0) {
    print "warn_undefined_symbols.pl:4: Note: unsatisfied symbols may cause runtime errors in perl scripts\n";
  }
}

warn_undefined_symbols();
