#!/bin/sh #---------------------------------------------------------------------------# # Copyright (C) 1995 University of Melbourne. # This file may only be copied under the terms of the GNU General # Public License - see the file COPYING in the Mercury distribution. #---------------------------------------------------------------------------# # MOD2INIT - Convert *.mod (or *.c) to *_init.c # # This script outputs an appropriate init.c, given the .mod (or .c) files. # # Usage: mod2init [-w] modules... # # Environment variables: MERCURY_MOD_LIB_DIR, MERCURY_MOD_LIB_MODS. MERCURY_MOD_LIB_DIR=${MERCURY_MOD_LIB_DIR=@LIBDIR@/modules} MERCURY_MOD_LIB_MODS=${MERCURY_MOD_LIB_MODS=@LIBDIR@/modules/*} # maximum number of calls to put in a single function calls_per_func=50 defentry=mercury__io__run_0_0 while getopts w: c do case $c in w) defentry="$OPTARG";; \?) echo "Usage: mod2init -[wentry] modules ..." exit 1;; esac shift `expr $OPTIND - 1` done files="$* $MERCURY_MOD_LIB_MODS" modules="`sed -n '/^BEGIN_MODULE(\(.*\)).*$/s//\1/p' $files`" echo "/*"; echo "** This code was automatically generated by mod2init."; echo "** Do not edit."; echo "**" echo "** Input files:" for file in $files; do echo "** $file" done echo "*/"; echo ""; echo '#include '; echo '#include "init.h"'; echo ""; echo "Declare_entry($defentry);"; echo "#if defined(USE_GCC_NONLOCAL_GOTOS) && !defined(USE_ASM_LABELS)"; echo "Code *default_entry;"; echo "#else"; echo "Code *default_entry = ENTRY($defentry);"; echo "#endif"; echo ""; for mod in $modules; do echo "extern void $mod(void);"; done echo ""; echo "#ifdef CONSERVATIVE_GC"; echo "/* This works around a bug in the Solaris 2.X (X <= 4) linker. */"; echo "/* The reason it is here is that it needs to be in statically linked */"; echo "/* code, it won't work if it is in the dynamically linked library. */"; echo "void init_gc(void)"; echo "{"; echo " GC_INIT();"; echo "}"; echo "#endif"; echo ""; init_funcs= num_funcs=0 set - $modules while [ $# -gt 0 ]; do num_funcs=`expr $num_funcs + 1` init_funcs="$init_funcs init_modules_$num_funcs" echo "static void init_modules_$num_funcs(void) {" num_calls=0 while [ $# -gt 0 ] && [ $num_calls -lt $calls_per_func ]; do num_calls=`expr $num_calls + 1` echo " $1();" shift done echo "}" echo "" done echo "void init_modules(void)"; echo "{"; for init in $init_funcs; do echo " $init();"; done echo ""; echo " default_entry = ENTRY($defentry);"; echo "}";