#! @SHELL@
#
# tests/test_exported_symbols.in --- script for yaxt tests
#
# Copyright  (C)  2022 Jörg Behrens <behrens@dkrz.de>
#                      Moritz Hanke <hanke@dkrz.de>
#                      Thomas Jahns <jahns@dkrz.de>
#
# Author: Jörg Behrens <behrens@dkrz.de>
#         Moritz Hanke <hanke@dkrz.de>
#         Thomas Jahns <jahns@dkrz.de>
#
# Maintainer: Jörg Behrens <behrens@dkrz.de>
#             Moritz Hanke <hanke@dkrz.de>
#             Thomas Jahns <jahns@dkrz.de>
# URL: https://dkrz-sw.gitlab-pages.dkrz.de/yaxt/
#
# Redistribution and use in source and binary forms, with or without
# modification, are  permitted provided that the following conditions are
# met:
#
# Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# Neither the name of the DKRZ GmbH nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
# IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
# OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
set -e
LIBC_FATAL_STDERR_=1
export LIBC_FATAL_STDERR_
NM="@NM@"
lt_cv_nm_interface="@lt_cv_nm_interface@"
abs_builddir="@abs_builddir@"
abs_top_builddir="@abs_top_builddir@"
acx_symprfx="@acx_symprfx@"
if [ x"$lt_cv_nm_interface" = x"BSD nm" ]; then
  exp_sym_prefix='[0-9a-fA-F]*[ 	][ 	]*[TDBGRSVW][ 	]'
  badsyms=$("$abs_top_builddir"/libtool --mode=clean --silent \
    ls "$abs_top_builddir"/src/*.la 2>/dev/null \
    | grep '\.\(so.*\|dylib\|a\)$' | xargs $NM \
    | sed -E -n "
# select exported symbols of any kind for further investigation
/^${exp_sym_prefix}[^ 	]*/{
# disregard STABS debugging symbols
/^${exp_sym_prefix}\.*@[0-9]*@/b
# disregard xlf bogus module USE symbols
/^${exp_sym_prefix}&&/b
# disregard AIX relocations
/^${exp_sym_prefix}\./b
# ignore symbols mpi.h introduces into our namespace
/^${exp_sym_prefix}${acx_symprfx}[Mm][Pp][Ii](fc|_|priv)/b
# ignore initialize/finish symbols from ELF shared objects
/^${exp_sym_prefix}__*fini/b
/^${exp_sym_prefix}__*init/b
/^${exp_sym_prefix}__*e{0,1}data/b
/^${exp_sym_prefix}__*end/b
/^${exp_sym_prefix}__*bss_start/b
# ignore COMMON symbols nagfor injects into our shared objects
/^[0-9a-fA-F]*[ 	][ 	]*B[ 	]${acx_symprfx}__NAGf90_procref/b
# ignore symbols pgfortran/pgcc injects into our shared objects
/^${exp_sym_prefix}(__get_xmmreg|__pgdbg_stub|__pgi_trace|__set_xmmreg|__pgi_data_section_start|_mp_preinit)/b
# skip symbols that conform to name prefix xt_ or Fortran yaxt module
# ifort adds a symbol 'yaxt._'
/^${exp_sym_prefix}${acx_symprfx}([Yy][Aa]){0,1}[Xx][Tt].{0,1}_/b
# gfortran module member names start with module name prefixed by two underscores
/^${exp_sym_prefix}${acx_symprfx}__yaxt_MOD_/b
# also skip symbols mangled by asan
/^[0-9a-fA-F]*[ 	][ 	]*B[ 	]__odr_asan\\.[Xx][Tt]\\.*/b
# PGI compiler prefixes D and B entries of modules with underscore
/^[0-9a-fA-F]*[ 	][ 	]*[DB][ 	]_([Yy][Aa]){0,1}[Xx][Tt]_/b
# NVHPC compiler 24.x and later adds this
/^[0-9a-fA-F]*[ 	][ 	]*B[ 	]__(PGI|NV)_CUDA_LOC/b
# omp critical adds a common block for PGI compiler
/^[0-9a-fA-F]*[ 	][ 	]*B[ 	]__cs_unspc/b
# skip $-symbols that the Cray compiler puts in
/^${exp_sym_prefix}.*\\\$.*/b
# skip internal symbols for stubs created by xlf
/^${exp_sym_prefix}.*\\\*__xt.*/b
# skip symbols introduced by gcov
/^${exp_sym_prefix}__gcov_*/b
# skip symbols introduced by gcc -fprofile-arcs -ftest-coverage
/^${exp_sym_prefix}mangle_path*/b
# flang uses _QM prefix
/^${exp_sym_prefix}${acx_symprfx}_QM([Yy][Aa]){0,1}[Xx][Tt].{0,1}/b
# flang adds weak symbols for intrinsics
/^${exp_sym_prefix}${acx_symprfx}_QM__fortran_builtinsE[._0-9a-z]*$/b
# flang also injects some symbols prefixed by _QQ
/^${exp_sym_prefix}${acx_symprfx}_QQcl[0-9A-Za-z]*$/b
p
}")
  if [ -n "$badsyms" ]; then
    echo "Failure in created libraries!" >&2
    echo "Unexpected symbols exported from library!" >&2
    echo "$badsyms" >&2
    exit 1
  fi
else
  exit 77
fi

#
# Local Variables:
# coding: utf-8
# mode: sh
# End:
#
