#!/bin/ksh93 CMD=${.sh.file##*/} function usage { print -u2 "Usage: ${CMD} [--linkwith-{exec|so|a}] heapfile execfile" exit 1 } function die { print -u2 "${CMD}: $1" exit 1 } [[ -z ${SMLNJ_HOME} ]] && BIN_DIR="@CLIENT_BASEDIR@/lib/smlnj/bin" || BIN_DIR=${SMLNJ_HOME}/bin . "${BIN_DIR}/.arch-n-opsys" RUNX="${BIN_DIR}/.run/runx.${ARCH}-${OPSYS}" RUN_SO="${BIN_DIR}/.run/run.${ARCH}-${OPSYS}.so" RUN_A="${BIN_DIR}/.run/run.${ARCH}-${OPSYS}.a" # prefer heap2asm from the same directory as this script H2A=${.sh.file} [[ ${H2A} =~ / ]] && H2A="${H2A%/*}" || H2A= [[ ${H2A:0:1} == '/' ]] || H2A="${PWD}/${H2A}" H2A+='/heap2asm' [[ ! -x ${H2A} ]] && H2A="${BIN_DIR}/heap2asm" FORMAT= if (( $# != 2 )); then (( $# != 3 )) && usage case $1 in --linkwith-exec) FORMAT='exec' ;; --linkwith-so) FORMAT='so' ;; --linkwith-a) FORMAT='a' ;; *) usage ;; esac shift elif [[ ${OPSYS} == 'darwin' ]]; then FORMAT='exec' elif [[ ${OPSYS} == 'freebsd' || ${OPSYS} == 'linux' ]]; then FORMAT='a' fi [[ -z ${FORMAT} ]] && die "no runtime object format specified" heapfile="$1" execfile="$2" CC=gcc LD=ld EXEC_PROG= EXEC_FLAGS= EXEC_LIBS= SO_PROG= SO_FLAGS= SO_LIBS= A_PROG= A_FLAGS= A_LIBS= case "${OPSYS}" in darwin) EXEC_PROG=${LD} EXEC_LIBS=-lc ;; freebsd) SO_PROG=${CC} SO_FLAGS=-Wl,--export-dynamic SO_LIBS=-lm A_PROG=${CC} A_FLAGS=-Wl,--export-dynamic A_LIBS=-lm ;; linux) SO_PROG=${CC} SO_FLAGS=-Wl,--export-dynamic A_PROG=${CC} A_FLAGS=-Wl,--export-dynamic A_LIBS="-lm -ldl" ;; esac # die early [[ -x ${H2A} ]] || { print -u2 "${CMD}: heap2asm is not found"; exit 2; } if [[ ${FORMAT} == 'exec' && -f "${RUNX}" ]]; then [[ -z ${EXEC_PROG} ]] && die "${MSG}" elif [[ ${FORMAT} == 'so' && -f ${RUN_SO} ]]; then [[ -z ${SO_PROG} ]] && die "${MSG}" elif [[ ${FORMAT} == 'a' && -f ${RUN_A} ]]; then [[ -z ${A_PROG} ]] && die "${MSG}" else print -u2 "${CMD}: linkable runtime system object (${FORMAT}) not available" exit 2 fi integer RESULT=0 ${H2A} ${heapfile} ${execfile}.s || die "heap2asm failed" [[ -f ${execfile}.s ]] || die "${execfile}.s creation failed" if ${CC} -c -o "${execfile}".o "${execfile}".s ; then rm "${execfile}".s else rm "${execfile}".s die "${execfile}.o creation failed" fi MSG="no linker specified for runtime format ${FORMAT}" if [[ ${FORMAT} == 'exec' ]] ; then ${EXEC_PROG} ${EXEC_FLAGS} -o "${execfile}" ${RUNX} "${execfile}".o ${EXEC_LIBS} RESULT=$? elif [[ ${FORMAT} == 'so' ]] ; then ${SO_PROG} ${SO_FLAGS} -o "${execfile}" ${RUN_SO} "${execfile}".o ${SO_LIBS} RESULT=$? elif [[ ${FORMAT} == 'a' ]] ; then ${A_PROG} ${A_FLAGS} -o "${execfile}" ${RUN_A} "${execfile}".o ${A_LIBS} RESULT=$? fi rm "${execfile}".o (( RESULT )) && die "linking failed with return code ${RESULT}" exit 0