#!/bin/sh
#  file kernel/pascal/remake: compile rules for the Pascal interface
#-----------------------------------------------------------------------+
#  Copyright 2005-2006, Michel Quercia (michel.quercia@prepas.org)      |
#                                                                       |
#  This file is part of Numerix. Numerix is free software; you can      |
#  redistribute it and/or modify it under the terms of the GNU Lesser   |
#  General Public License as published by the Free Software Foundation; |
#  either version 2.1 of the License, or (at your option) any later     |
#  version.                                                             |
#                                                                       |
#  The Numerix Library is distributed in the hope that it will be       |
#  useful, but WITHOUT ANY WARRANTY; without even the implied warranty  |
#  of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU  |
#  Lesser General Public License for more details.                      |
#                                                                       |
#  You should have received a copy of the GNU Lesser General Public     |
#  License along with the GNU MP Library; see the file COPYING. If not, |
#  write to the Free Software Foundation, Inc., 59 Temple Place -       |
#  Suite 330, Boston, MA 02111-1307, USA.                               |
#-----------------------------------------------------------------------+
#                                                                       |
#              Rgles de construction de l'interface Pascal             |
#                                                                       |
#-----------------------------------------------------------------------+

# m_list = liste des modes (c/d/s)
m_list=
if test "$USE_CLONG" = "1"; then m_list="${m_list} c"; fi
if test "$USE_DLONG" = "1"; then m_list="${m_list} d"; fi
if test "$USE_SLONG" = "1"; then m_list="${m_list} s"; fi

# On y va
cat <<EOF
# File kernel/pascal/makefile: compile rules for the Pascal interface
EOF
sed -n -e '3,/^$/p' $0 
cat <<EOF
# This file was generated by the kernel/pascal/remake script
# switches:
#   USE_CLONG=$USE_CLONG
#   USE_DLONG=$USE_DLONG
#   USE_SLONG=$USE_SLONG
#   MAKE_PASCAL_LIB=$MAKE_PASCAL_LIB
#   PASCAL=$PASCAL
EOF

if test "$MAKE_PASCAL_LIB" = "0"; then exit; fi

cat <<EOF
#-------------------- config files
lib_pascal_config = Makefile kernel/config.h kernel/pascal/makefile

#-------------------- unit compile rules
EOF

# Spcialisation en fonction du compilateur et de l'environnement
case $PASCAL in
fpc) uext=ppu
     uswitch=
     cdecl="cdecl; "
     case $(uname) in
     CYGWIN*|MINGW*)
       links='{\$$link libnumerix-c.a}{\$$link libgcc.a}{\$$link libmsvcrt.a}'
     cat <<EOF
kernel/pascal/o/libgcc.a: \$(lib_pascal_config)
	cp \$\$(\$(GCC) -print-file-name=libgcc.a) kernel/pascal/o
kernel/pascal/o/libmsvcrt.a: \$(lib_pascal_config)
	cp \$\$(\$(GCC) -print-file-name=libmsvcrt.a) kernel/pascal/o
EOF
       runtime="kernel/pascal/o/libgcc.a kernel/pascal/o/libmsvcrt.a"
       ;;
     *)
       links='{\$$linklib numerix-c}{\$$linklib gcc_s}{\$$linklib c}'
       runtime=
       ;;
     esac
     eswitch="-Fukernel/pascal/o -Fokernel/pascal/o -Flkernel/c/o -Fokernel/c/o"
     ;;
gpc) uext=gpi
     uswitch=-c
     cdecl=
     links=
     runtime=
     eswitch="-o \$@ --unit-path=kernel/pascal/o -Lkernel/pascal/o -Lkernel/c/o -lnumerix-c"
     ;;
esac


for m in $m_list; do cat <<EOF
kernel/pascal/o/${m}long.p: \$(lib_pascal_config) kernel/pascal/p/xlong.p
	sed -e "s/xx(\([^)]*\))/${m}x_\1/g" \\
	    -e "s/_name_/${m}long/g" \\
	    -e "s/_links_/${links}/g" \\
	    -e "s/_cdecl_/${cdecl}/g" \\
	    kernel/pascal/p/xlong.p >\$@
kernel/pascal/o/${m}long.$uext: kernel/pascal/o/${m}long.p $runtime
	\$(PC) $uswitch kernel/pascal/o/${m}long.p
EOF
  if test $PASCAL = gpc; then cat <<EOF
	@mv ${m}long.* kernel/pascal/o
EOF
  fi
done
echo

echo -n "lib_pascal = "
for m in $m_list; do echo -n "\\
  kernel/pascal/o/${m}long.$uext "
done
echo

echo -n "lib_pascal_o = "
for m in $m_list; do echo -n "\\
  kernel/pascal/o/${m}long.o "
done
echo

echo -n "lib_pascal_p = "
for m in $m_list; do echo -n "\\
  kernel/pascal/o/${m}long.p "
done
echo

cat <<EOF
#-------------------- installation
.PHONY: install_pascal
install_pascal: \$(lib_pascal) \$(lib_pascal_p)
	mkdir -p \$(PASCAL_INSTALL_LIB) \$(PASCAL_INSTALL_INCLUDE)
	cp -p \$(lib_pascal) \$(lib_pascal_o) \$(PASCAL_INSTALL_LIB)
	cp -p \$(lib_pascal_p) \$(PASCAL_INSTALL_INCLUDE)
EOF

exemples="pi simple shanks"
cat <<EOF

#-------------------- examples
EOF
echo -n "ex_pascal_obj = "
for m in $m_list; do for e in $exemples; do echo -n "\\
  exemples/pascal/$e-$m "
done; done
echo
echo

for m in $m_list; do for e in $exemples; do cat <<EOF
exemples/pascal/$e-$m: exemples/pascal/$e.p \$(lib_c) \$(lib_pascal)
	sed -e "s/_name_/${m}long/g" exemples/pascal/$e.p >exemples/pascal/$e-$m.p
	\$(PC) exemples/pascal/$e-$m.p $eswitch
	@rm -f exemples/pascal/$e-$m.p exemples/pascal/$e-$m.o
EOF
done; done

cat <<EOF

.PHONY: test_pascal clean_pascal
test_pascal: \$(ex_pascal_obj)
	@echo "-------------------- Testing the Pascal interface"
	for i in $exemples; do for j in $m_list; do \\
	    LD_LIBRARY_PATH=kernel/c/o:\$\$LD_LIBRARY_PATH exemples/pascal/\$\$i-\$\$j -test; \\
	done; done

clean_pascal:
	rm -f kernel/pascal/o/* exemples/pascal/*{-c,-d,-s,.exe}

#-------------------- done
lib:          \$(lib_pascal)
install:      install_pascal
exemples:     \$(ex_pascal_obj)
test:         test_pascal
clean:        clean_pascal
EOF

