

#--------------------------------------------------------------------
#	The statements below define a collection of symbols related to
#	dynamic loading and shared libraries:
#
#	DL_LIBS -	Library file(s) to include in tclsh and other base
#			applications in order for the "load" command to work.
#	DL_LD_FLAGS -	Flags to pass to the compiler when linking object
#			files into an executable application binary such
#			as tclsh.
#	DL_LD_SEARCH_FLAGS-Flags to pass to ld, such as "-R /usr/local/tcl/lib"
#			that tell the run-time dynamic linker where to look
#			for shared libraries such as libtcl.so.  Depends on
#			the variable SHLIB_RUNTIME_DIR in the Makefile.
#	SHLIB_CFLAGS -	Flags to pass to cc when compiling the components
#			of a shared library (may request position-independent
#			code, among other things).
#	SHLIB_LD -	Base command to use for combining object files
#			into a shared library.
#	SHLIB_LD_LIBS -	Dependent libraries for the linker to scan when
#			creating shared libraries.  This symbol typically
#			goes at the end of the "ld" commands that build
#			shared libraries. The value of the symbol is
#			"$V_LIB" if all of the dependent libraries should
#			be specified when creating a shared library.  If
#			dependent libraries should not be specified (as on
#			SunOS 4.x, where they cause the link to fail, or in
#			general if Tcl and Tk aren't themselves shared
#			libraries), then this symbol has an empty string
#			as its value.
#	SHLIB_SUFFIX -	Suffix to use for the names of dynamically loadable
#			extensions.  An empty string means we don't know how
#			to use shared libraries on this platform.
#--------------------------------------------------------------------


### XXX: AC_ARG_ENABLE(shlib, --enable-shlib          enable Makefile flags
###for building shared libraries, , enable_shlib="no")


AC_ARG_ENABLE(shlib, --enable-shlib          enable Makefile targets for mash shared libraries, , enable_shlib="no")


# Step 1: set the variable "system" to hold the name and version number
# for the system.  This can usually be done via the "uname" command, but
# there are a few systems, like Next, where this doesn't work.

AC_MSG_CHECKING([system version (for dynamic loading)])
if test -f /usr/lib/NextStep/software_version; then
    system=NEXTSTEP-`awk '/3/,/3/' /usr/lib/NextStep/software_version`
else
    system=`uname -s`-`uname -r`
    if test "$?" -ne 0 ; then
	AC_MSG_RESULT([unknown (can't find uname command)])
	system=unknown
    else
	# Special check for weird MP-RAS system (uname returns weird
	# results, and the version is kept in special file).
    
	if test -r /etc/.relid -a "X`uname -n`" = "X`uname -s`" ; then
	    system=MP-RAS-`awk '{print $3}' /etc/.relid`
	fi
	if test "`uname -s`" = "AIX" ; then
	    system=AIX-`uname -v`.`uname -r`
	fi
	AC_MSG_RESULT($system)
    fi
fi

# Step 2: check for existence of -ldl library.  This is needed because
# Linux can use either -ldl or -ldld for dynamic loading.

AC_CHECK_LIB(dl, dlopen, have_dl=yes, have_dl=no)

if test -z "$V_TCLSH"
then
	local_TCLSH=tclsh
else
	local_TCLSH=$V_TCLSH
fi

# Step 3: set configuration options based on system name and version.

case $system in
    AIX-*)
	enable_dl="no"
	;;
    BSD/OS-2.1*|BSD/OS-3*)
	SHLIB_CFLAGS=""
	SHLIB_LD="shlicc -r"
	SHLIB_LD_LIBS="$V_LIB"
	SHLIB_SUFFIX=".so"
	DL_LIBS="-ldl"
	DL_LD_FLAGS=""
	DL_LD_SEARCH_FLAGS=""
	;;
    Darwin-5.*|Darwin-6.*)
        SHLIB_CFLAGS="-fno-common -fPIC -pipe"
        SHLIB_LD="cc -dynamiclib -flat_namespace -undefined suppress"
        SHLIB_SUFFIX=".dylib"
        DL_LIBS=""
        SHLD_FLAGS="-Wl,-bind_at_load -Wl,-multiply_defined -Wl,suppress"
        ;;
    Darwin-7.*|Darwin-8.*|Darwin-9.*|Darwin-10.*) # Remove `-mcpu=7450' for G5 (PPC970) optimization:
        SHLIB_CFLAGS="-fno-common -fPIC -pipe"
        SHLIB_LD="cc -dynamiclib -flat_namespace -undefined suppress"
        SHLIB_SUFFIX=".dylib"
        DL_LIBS=""
        SHLD_FLAGS="-Wl,-bind_at_load -Wl,-multiply_defined -Wl,suppress"
        ;;
    dgux*)
	SHLIB_CFLAGS="-K PIC"
	SHLIB_LD="cc -G"
	SHLIB_LD_LIBS=""
	SHLIB_SUFFIX=".so"
	DL_LIBS="-ldl"
	DL_LD_FLAGS=""
	DL_LD_SEARCH_FLAGS=""
	;;
    HP-UX-*.08.*|HP-UX-*.09.*|HP-UX-*.10.*)
	AC_CHECK_LIB(dld, shl_load, dl_ok=yes, dl_ok=no)
	if test "$dl_ok" = yes; then
	    SHLIB_CFLAGS="+z"
	    SHLIB_LD="ld -b"
	    SHLIB_LD_LIBS=""
	    SHLIB_SUFFIX=".sl"
	    DL_LIBS="-ldld"
	    DL_LD_FLAGS="-Wl,-E"
	    DL_LD_SEARCH_FLAGS='-Wl,+b,${SHLIB_RUNTIME_DIR}:.'
	fi
	;;
    IRIX-4.*)
	SHLIB_CFLAGS="-G 0"
	SHLIB_SUFFIX=".a"
	SHLIB_LD="echo tclLdAout $CC \{$SHLIB_CFLAGS\} | $local_TCLSH -r -G 0"
	SHLIB_LD_LIBS="$V_LIB"
	DL_LIBS=""
	DL_LD_FLAGS="-Wl,-D,08000000"
	DL_LD_SEARCH_FLAGS='-L${SHLIB_RUNTIME_DIR}'
	;;
    IRIX-5.*|IRIX-6.*)
	SHLIB_CFLAGS=""
	SHLIB_LD="ld -shared -rdata_shared"
	SHLIB_LD_LIBS=""
	SHLIB_SUFFIX=".so"
	DL_LIBS=""
	DL_LD_FLAGS=""
	DL_LD_SEARCH_FLAGS='-Wl,-rpath,${SHLIB_RUNTIME_DIR}'
	;;
    IRIX64-6.*)
	SHLIB_CFLAGS=""
	SHLIB_LD="ld -32 -shared -rdata_shared -rpath /usr/local/lib"
	SHLIB_LD_LIBS=""
	SHLIB_SUFFIX=".so"
	DL_LIBS=""
	DL_LD_FLAGS=""
	DL_LD_SEARCH_FLAGS='-Wl,-rpath,${SHLIB_RUNTIME_DIR}'
	;;
    Linux*)
	SHLIB_CFLAGS="-fPIC"
	SHLIB_LD_LIBS=""
	SHLIB_SUFFIX=".so"
	if test "$have_dl" = yes; then
	    SHLIB_LD="${CC} -shared"
	    DL_LIBS="-ldl"
	    DL_LD_FLAGS="-rdynamic"
	    DL_LD_SEARCH_FLAGS=""
	else
	    AC_CHECK_HEADER(dld.h, [
		SHLIB_LD="ld -shared"
		DL_LIBS="-ldld"
		DL_LD_FLAGS=""
		DL_LD_SEARCH_FLAGS=""])
	fi
	;;
    MP-RAS-02*)
	SHLIB_CFLAGS="-K PIC"
	SHLIB_LD="cc -G"
	SHLIB_LD_LIBS=""
	SHLIB_SUFFIX=".so"
	DL_LIBS="-ldl"
	DL_LD_FLAGS=""
	DL_LD_SEARCH_FLAGS=""
	;;
    MP-RAS-*)
	SHLIB_CFLAGS="-K PIC"
	SHLIB_LD="cc -G"
	SHLIB_LD_LIBS=""
	SHLIB_SUFFIX=".so"
	DL_LIBS="-ldl"
	DL_LD_FLAGS="-Wl,-Bexport"
	DL_LD_SEARCH_FLAGS=""
	;;
    NetBSD-*|FreeBSD-*|OpenBSD-*)
	# Not available on all versions:  check for include file.
	AC_CHECK_HEADER(dlfcn.h, [
	    SHLIB_CFLAGS="-fpic"
	    SHLIB_LD="ld -Bshareable -x"
	    SHLIB_LD_LIBS=""
	    SHLIB_SUFFIX=".so"
	    DL_LIBS=""
	    DL_LD_FLAGS=""
	    DL_LD_SEARCH_FLAGS=""
	], [
	    SHLIB_CFLAGS=""
	    SHLIB_LD="echo tclLdAout $CC \{$SHLIB_CFLAGS\} | $local_TCLSH -r"
	    SHLIB_LD_LIBS='$V_LIB'
	    SHLIB_SUFFIX=".a"
	    DL_LIBS=""
	    DL_LD_FLAGS=""
	    DL_LD_SEARCH_FLAGS='-L${SHLIB_RUNTIME_DIR}'
	])

	;;
    NEXTSTEP-*)
	SHLIB_CFLAGS=""
	SHLIB_LD="cc -nostdlib -r"
	SHLIB_LD_LIBS=""
	SHLIB_SUFFIX=".so"
	DL_LIBS=""
	DL_LD_FLAGS=""
	DL_LD_SEARCH_FLAGS=""
	;;
    OSF1-1.0|OSF1-1.1|OSF1-1.2)
	# OSF/1 1.[012] from OSF, and derivatives, including Paragon OSF/1
	SHLIB_CFLAGS=""
	# Hack: make package name same as library name
	SHLIB_LD='ld -R -export $@:'
	SHLIB_LD_LIBS=""
	SHLIB_SUFFIX=".so"
	DL_LIBS=""
	DL_LD_FLAGS=""
	DL_LD_SEARCH_FLAGS=""
	;;
    OSF1-1.*)
	# OSF/1 1.3 from OSF using ELF, and derivatives, including AD2
	SHLIB_CFLAGS="-fpic"
	SHLIB_LD="ld -shared"
	SHLIB_LD_LIBS=""
	SHLIB_SUFFIX=".so"
	DL_LIBS=""
	DL_LD_FLAGS=""
	DL_LD_SEARCH_FLAGS=""
	;;
    OSF1-V*)
        # Digital OSF/1
	SHLIB_CFLAGS=""
	SHLIB_LD='ld -shared -expect_unresolved "*"'
	SHLIB_LD_LIBS=""
	SHLIB_SUFFIX=".so"
	DL_LIBS=""
	DL_LD_FLAGS=""
	DL_LD_SEARCH_FLAGS='-Wl,-rpath,${SHLIB_RUNTIME_DIR}'
	;;
    RISCos-*)
	SHLIB_CFLAGS="-G 0"
	SHLIB_LD="echo tclLdAout $CC \{$SHLIB_CFLAGS\} | $local_TCLSH -r -G 0"
	SHLIB_LD_LIBS='$V_LIB'
	SHLIB_SUFFIX=".a"
	DL_LIBS=""
	DL_LD_FLAGS="-Wl,-D,08000000"
	DL_LD_SEARCH_FLAGS='-L${SHLIB_RUNTIME_DIR}'
	;;
    SCO_SV-3.2*)
        # Note, dlopen is available only on SCO 3.2.5 and greater.  However,
        # this test works, since "uname -s" was non-standard in 3.2.4 and
        # below.
	SHLIB_CFLAGS="-Kpic -belf"
	SHLIB_LD="ld -G"
	SHLIB_LD_LIBS=""
	SHLIB_SUFFIX=".so"
	DL_LIBS=""
	DL_LD_FLAGS="-belf -Wl,-Bexport"
	DL_LD_SEARCH_FLAGS=""
	;;
     SINIX*5.4*)
	SHLIB_CFLAGS="-K PIC"
	SHLIB_LD="cc -G"
	SHLIB_LD_LIBS=""
	SHLIB_SUFFIX=".so"
	DL_LIBS="-ldl"
	DL_LD_FLAGS=""
	DL_LD_SEARCH_FLAGS=""
	;;
    SunOS-4*)
	SHLIB_CFLAGS="-PIC"
	SHLIB_LD="ld"
	SHLIB_LD_LIBS=""
	SHLIB_SUFFIX=".so"
	DL_LIBS="-ldl"
	DL_LD_FLAGS=""
	DL_LD_SEARCH_FLAGS='-L${SHLIB_RUNTIME_DIR}'
	;;
    SunOS-5*)
	SHLIB_CFLAGS="-fpic"
	SHLIB_LD="/usr/ccs/bin/ld -G -z text"

	# Note: need the LIBS below, otherwise Tk won't find Tcl's
	# symbols when dynamically loaded into tclsh.

	SHLIB_LD_LIBS='$V_LIB'
	SHLIB_SUFFIX=".so"
	DL_LIBS="-ldl"
	DL_LD_FLAGS=""
	DL_LD_SEARCH_FLAGS='-R ${SHLIB_RUNTIME_DIR}'
	;;
    ULTRIX-4.*)
	SHLIB_CFLAGS="-G 0"
	SHLIB_SUFFIX=".a"
	SHLIB_LD="echo tclLdAout $CC \{$SHLIB_CFLAGS\} | $local_TCLSH -r -G 0"
	SHLIB_LD_LIBS='$V_LIB'
	DL_LIBS=""
	DL_LD_FLAGS="-Wl,-D,08000000"
	DL_LD_SEARCH_FLAGS='-L${SHLIB_RUNTIME_DIR}'
	;;
    UNIX_SV*)
	SHLIB_CFLAGS="-KPIC"
	SHLIB_LD="cc -G"
	SHLIB_LD_LIBS=""
	SHLIB_SUFFIX=".so"
	DL_LIBS="-ldl"
	# Some UNIX_SV* systems (unixware 1.1.2 for example) have linkers
	# that don't grok the -Bexport option.  Test that it does.
	hold_ldflags=$LDFLAGS
	AC_MSG_CHECKING(for ld accepts -Bexport flag)
	LDFLAGS="${LDFLAGS} -Wl,-Bexport"
	AC_TRY_LINK(, [int i;], found=yes, found=no)
	LDFLAGS=$hold_ldflags
	AC_MSG_RESULT($found)
	if test $found = yes; then
	  SH_LD_FLAGS="-Wl,-Bexport"
	else
	  SH_LD_FLAGS=""
	fi
	SH_LD_SEARCH_FLAGS=""
	;;
esac

# Step 4: disable dynamic loading if requested via a command-line switch.
#
#XXX:if test $enable_shlib = "no" ; then
#    echo "Disabling dynamic loading and shared libraries"
#    SHLIB_CFLAGS=""
#    SHLIB_LD=""
#    SHLIB_SUFFIX=""
#    DL_LIBS=""
#    DL_LD_FLAGS=""
#    DL_LD_SEARCH_FLAGS=""
#fi


# If we're running gcc, then change the C flags for compiling shared
# libraries to the right flags for gcc, instead of those for the
# standard manufacturer compiler.

####XXX:if test "enable_shlib" != "no" ; then
    if test "$CC" = "gcc" -o `$CC -v 2>&1 | grep -c gcc` != "0" ; then
	case $system in
	    AIX-*)
		;;
	    BSD/OS*)
		;;
	    IRIX*)
		;;
	    NetBSD-*|FreeBSD-*|OpenBSD-*)
		;;
	    RISCos-*)
		;;
	    ULTRIX-4.*)
		;;
	    *)
		SHLIB_CFLAGS="-fPIC"
		;;
	esac
    fi
####fi


if test "$enable_shlib" != "no" ; then
        PKG_SHLIB_CFLAGS=$SHLIB_CFLAGS
else
        PKG_SHLIB_CFLAGS=""
fi


AC_SUBST(DL_LIBS)
AC_SUBST(DL_LD_FLAGS)
AC_SUBST(DL_LD_SEARCH_FLAGS)
AC_SUBST(SHLIB_CFLAGS)
AC_SUBST(SHLIB_LD)
AC_SUBST(SHLIB_LD_LIBS)
AC_SUBST(SHLIB_SUFFIX)

AC_SUBST(PKG_SHLIB_CFLAGS)
