#!/bin/sh
#  file kernel/c/remake: compile rules for the libnumerix-c library
#-----------------------------------------------------------------------+
#  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 libnumerix-c                |
#                                                                       |
#-----------------------------------------------------------------------+

# 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/c/makefile: compile rules for the libnumerix-c library
EOF
sed -n -e '3,/^$/p' $0 
cat <<EOF
# This file was generated by the kernel/c/remake script
# switches:
#   USE_CLONG=$USE_CLONG
#   USE_DLONG=$USE_DLONG
#   USE_SLONG=$USE_SLONG
#   USE_GMP=$USE_GMP
#   MAKE_C_LIB=$MAKE_C_LIB
#   MAKE_PASCAL_LIB=$MAKE_PASCAL_LIB
#   SHARED=$SHARED
EOF

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

cat <<EOF
#-------------------- config files
lib_c_config = Makefile kernel/config.h kernel/c/makefile

#-------------------- source files
lib_c_c_src = \\
  kernel/c/c/numerix.c   \\
  kernel/x/c/add.c       \\
  kernel/x/c/cmp.c       \\
  kernel/x/c/convert.c   \\
  kernel/x/c/copy.c      \\
  kernel/x/c/div.c       \\
  kernel/x/c/dump.c      \\
  kernel/x/c/fact.c      \\
  kernel/x/c/gcd.c       \\
  kernel/x/c/mul.c       \\
  kernel/x/c/pow.c       \\
  kernel/x/c/powmod.c    \\
  kernel/x/c/prime.c     \\
  kernel/x/c/random.c    \\
  kernel/x/c/root.c      \\
  kernel/x/c/shift.c     \\
  kernel/x/c/sqrt.c      \\
  kernel/x/c/string.c

lib_c_h1_src = \\
  kernel/c/h/numerix.h   \\
  kernel/x/h/numerix.h   \\
  kernel/x/h/alloc.h

lib_c_h2_src = \\
  kernel/x/h/add.h       \\
  kernel/x/h/cmp.h       \\
  kernel/x/h/convert.h   \\
  kernel/x/h/copy.h      \\
  kernel/x/h/div.h       \\
  kernel/x/h/dump.h      \\
  kernel/x/h/fact.h      \\
  kernel/x/h/gcd.h       \\
  kernel/x/h/mul.h       \\
  kernel/x/h/pow.h       \\
  kernel/x/h/powmod.h    \\
  kernel/x/h/prime.h     \\
  kernel/x/h/random.h    \\
  kernel/x/h/root.h      \\
  kernel/x/h/shift.h     \\
  kernel/x/h/sqrt.h      \\
  kernel/x/h/string.h    \\
  kernel/x/h/chrono.h

lib_c_h_src = \$(lib_c_h1_src) \$(lib_c_h2_src)
EOF

cat <<EOF

#-------------------- object file list
lib_c_obj = \\
EOF

echo -n "  kernel/c/o/chrono.o "
for m in $m_list; do echo -n "\\
  kernel/c/o/numerix-$m.o "
done
echo

cat <<EOF

#-------------------- library compile rules
kernel/c/o/chrono.o: \$(lib_c_config) kernel/x/c/chrono.c kernel/x/h/chrono.h
	\$(GCC) \$(PIC) -Dc_api -c -o \$@ kernel/x/c/chrono.c
EOF
for m in $m_list; do cat <<EOF
kernel/c/o/numerix-$m.o: \$(lib_c_config) \$(lib_c_c_src) \$(lib_c_h_src) \$(lib_n_obj)
	\$(GCC) \$(PIC) -Duse_${m}long -c -o \$@ kernel/c/c/numerix.c
EOF
done

if test x$SHARED = x1; then
cat <<EOF
lib_c_0 = libnumerix-c.so
lib_c_1 = \$(lib_c_0).\$(NUMERIX_MAJOR)
lib_c_2 = \$(lib_c_1).\$(NUMERIX_MINOR)
lib_c = kernel/c/o/\$(lib_c_2)
\$(lib_c): \$(lib_c_obj) \$(lib_n_obj)
	\$(GCC) -shared -Wl,-soname,\$(lib_c_1) -o \$@ \$(lib_c_obj) \$(lib_n_obj)
	ln -s \$(lib_c_2) kernel/c/o/\$(lib_c_1)
	ln -s \$(lib_c_1) kernel/c/o/\$(lib_c_0)
EOF
else
cat <<EOF
lib_c = kernel/c/o/libnumerix-c.a
\$(lib_c): \$(lib_c_obj) \$(lib_n_obj)
	rm -f \$@
	\$(AR) \$@ \$(lib_c_obj) \$(lib_n_obj)
	\$(RANLIB) \$@
lib_c_lib = \$(lib_c)
EOF
fi

cat <<EOF

#-------------------- make a decent numerix.h for the outside world
kernel/c/o/numerix.h: \$(lib_c_config) kernel/c/h/numerix.h \$(lib_c_h2_src)
	(sed -e '/#if 0/d' -e '/End of the public interface/,\$\$d' kernel/c/h/numerix.h; \\
	  for i in \$(lib_c_h2_src); do sed -e '2,/\*\//d' \$\$i; done |\\
	  \$(GCC) -Dc_api -E -P -C -) >\$@
lib_c_include = kernel/c/o/numerix.h

#-------------------- installation
.PHONY: install_c
install_c: \$(lib_c) \$(lib_c_include)
	mkdir -p \$(C_INSTALL_LIB) \$(C_INSTALL_INCLUDE)
	cp -p \$(lib_c) \$(C_INSTALL_LIB)
	cp -p \$(lib_c_include) \$(C_INSTALL_INCLUDE)
EOF
if test x$SHARED = x1; then cat <<EOF
	ln -s \$(lib_c_2) \$(C_INSTALL_LIB)/\$(lib_c_1)
	ln -s \$(lib_c_1) \$(C_INSTALL_LIB)/\$(lib_c_0)
EOF
else cat <<EOF
	\$(RANLIB) \$(C_INSTALL_LIB)/libnumerix-c.a
EOF
fi

exemples="pi shanks simple"
other="chrono prime-test"
cat <<EOF

#-------------------- examples
EOF
echo -n "ex_c_obj = "
for m in $m_list; do for e in $exemples $other; do echo -n "\\
  exemples/c/$e-$m "
done; done
if test "$USE_GMP" = "1"; then echo -n "\\
  exemples/c/pi-g \\
  exemples/c/chrono-g "
fi
echo
echo

for m in $m_list; do for e in $exemples $other; do cat <<EOF
exemples/c/$e-$m: exemples/c/$e.c \$(lib_c) kernel/c/o/numerix.h
	\$(GCC) -Duse_${m}long -Ikernel/c/o -o \$@ exemples/c/$e.c -Lkernel/c/o -lnumerix-c
EOF
done; done
if test "$USE_GMP" = "1"; then cat <<EOF
exemples/c/pi-g: exemples/c/pi-gmp.c kernel/c/o/chrono.o
	\$(GCC) -o \$@ exemples/c/pi-gmp.c kernel/c/o/chrono.o -lgmp
exemples/c/chrono-g: exemples/c/chrono-gmp.c kernel/c/o/chrono.o
	\$(GCC) -o \$@ exemples/c/chrono-gmp.c kernel/c/o/chrono.o -lgmp
EOF
fi

cat <<EOF

.PHONY: test_c
test_c: \$(ex_c_obj)
	@echo "-------------------- Testing the C interface"
EOF
if test "$USE_GMP" = "1"; then cat <<EOF
	exemples/c/\pi-g -test && \\
EOF
fi
cat <<EOF
	for i in $exemples; do for j in $m_list; do \\
	    LD_LIBRARY_PATH=kernel/c/o:\$\$LD_LIBRARY_PATH exemples/c/\$\$i-\$\$j -test; \\
	done; done

#-------------------- cleanup
.PHONY: clean_c
clean_c:
	rm -f kernel/c/o/* exemples/c/*{-c,-d,-g,-s,.exe}
EOF

cat <<EOF

#-------------------- done
lib:          \$(lib_c) kernel/c/o/numerix.h
install:      install_c
exemples:     \$(ex_c_obj)
test:         test_c
clean:        clean_c
EOF
