$NetBSD: patch-ac,v 1.20.4.1 2006/02/11 22:48:07 salo Exp $

--- ltmain.sh.orig	2005-05-16 05:39:29.000000000 -0400
+++ ltmain.sh
@@ -139,6 +139,43 @@ quote_scanset='[[~#^*{};<>?'"'"' 	]'
 # Shell function definitions:
 # This seems to be the best place for them
 
+# func_mktempdir [string]
+# Make a temporary directory that won't clash with other running
+# libtool processes, and avoids race conditions if possible.  If
+# given, STRING is the basename for that directory.
+func_mktempdir ()
+{
+    my_template="${TMPDIR-/tmp}/${1-$progname}"
+
+    if test "$run" = ":"; then
+      # Return a directory name, but don't create it in dry-run mode
+      my_tmpdir="${my_template}-$$"
+    else
+
+      # If mktemp works, use that first and foremost
+      my_tmpdir=`mktemp -d "${my_template}-XXXXXXXX" 2>/dev/null`
+
+      if test ! -d "$my_tmpdir"; then
+	# Failing that, at least try and use $RANDOM to avoid a race
+	my_tmpdir="${my_template}-${RANDOM-0}$$"
+
+	save_mktempdir_umask=`umask`
+	umask 0077
+	$mkdir "$my_tmpdir"
+	umask $save_mktempdir_umask
+      fi
+
+      # If we're not in dry-run mode, bomb out on failure
+      test -d "$my_tmpdir" || {
+        $echo "cannot create temporary directory \`$my_tmpdir'" 1>&2
+	exit $EXIT_FAILURE
+      }
+    fi
+
+    $echo "X$my_tmpdir" | $Xsed
+}
+
+
 # func_win32_libid arg
 # return the library type of file 'arg'
 #
@@ -188,6 +225,31 @@ func_win32_libid ()
 # arg is usually of the form 'gcc ...'
 func_infer_tag ()
 {
+    # Set the commonly-used compilers for some tags.
+    tag_compilers_CC="*cc *xlc"
+    tag_compilers_CXX="*++ *CC"
+    tag_compilers_F77="*77 *fort"
+    base_compiler=`set -- "$@"; $echo $1`
+    # If $tagname still isn't set, then try to infer if the default
+    # "CC" tag applies by matching up the base compile command to some
+    # compilers commonly used for the "CC" tag.
+    if test -z "$tagname"; then
+        z=CC
+        eval "tag_compilers=\$tag_compilers_${z}"
+        if test -n "$tag_compilers"; then
+        for zp in $tag_compilers; do
+          case $base_compiler in
+            $zp)
+            # The compiler in the base compile command matches
+            # one of the common compilers for the current tag.
+            # Assume this is the tagged configuration we want.
+            tagname=$z
+            break
+            ;;
+          esac
+        done
+        fi
+    fi
     if test -n "$available_tags" && test -z "$tagname"; then
       CC_quoted=
       for arg in $CC; do
@@ -228,6 +290,27 @@ func_infer_tag ()
 	      break
 	      ;;
 	    esac
+	    # If $tagname still isn't set, then try to infer it by
+	    # matching up the base compile command to some compilers
+	    # commonly used for certain tags.
+	    base_compiler=`set -- "$@"; $echo $1`
+	    eval "tag_compilers=\$tag_compilers_${z}"
+	    if test -n "$tag_compilers"; then
+	      for zp in $tag_compilers; do
+		case $base_compiler in 
+		  $zp)
+		  # The compiler in the base compile command matches
+		  # one of the common compilers for the current tag.
+		  # Assume this is the tagged configuration we want.
+		  tagname=$z
+		  break
+		  ;;
+		esac
+	      done
+	      if test -n "$tagname"; then
+		break
+	      fi
+	    fi
 	  fi
 	done
 	# If $tagname still isn't set, then no tagged configuration
@@ -512,7 +595,7 @@ if test -z "$show_help"; then
     $echo "*** Warning: inferring the mode of operation is deprecated." 1>&2
     $echo "*** Future versions of Libtool will require --mode=MODE be specified." 1>&2
     case $nonopt in
-    *cc | cc* | *++ | gcc* | *-gcc* | g++* | xlc*)
+    *cc | cc* | *++ | gcc* | *-gcc* | g++* | xlc* | *CC)
       mode=link
       for arg
       do
@@ -1313,6 +1396,13 @@ EOF
 	    exit $EXIT_FAILURE
 	    ;;
 	  esac
+	  # Canonicalise the pathname
+	  tmp=""
+	  while test "$arg" != "$tmp"
+          do
+            tmp=$arg
+            arg=`$echo "X$arg" | $Xsed -e 's%[^/.][^/.]*/\.\.%%g' -e 's%/\./%/%g' -e 's%//*%/%g' -e 's%/$%%g'`
+          done
 	  if test "$prev" = rpath; then
 	    case "$rpath " in
 	    *" $arg "*) ;;
@@ -1523,6 +1613,12 @@ EOF
 	compiler_flags="$compiler_flags $arg"
 	compile_command="$compile_command $arg"
 	finalize_command="$finalize_command $arg"
+	case $host:$arg in
+	*-*-dragonfly*:-pthread)
+	  # pkgsrc hack to use -pthread in .la file for final linking
+	  deplibs="$deplibs $arg"
+	  ;;
+	esac
 	continue
 	;;
 
@@ -2013,6 +2109,30 @@ EOF
 	  else
 	    compiler_flags="$compiler_flags $deplib"
 	  fi
+	  case $host:$deplib in
+	  *-*-dragonfly*:-pthread)
+	    # pkgsrc hack to use -pthread in .la file for final linking
+	    case $linkmode in
+	    lib)
+	      deplibs="$deplib $deplibs"
+	      test "$pass" = conv && continue
+	      newdependency_libs="$deplib $newdependency_libs"
+	      ;;
+	    prog)
+	      if test "$pass" = conv; then
+		deplibs="$deplib $deplibs"
+		continue
+	      fi
+	      if test "$pass" = scan; then
+		deplibs="$deplib $deplibs"
+	      else
+		compile_deplibs="$deplib $compile_deplibs"
+		finalize_deplibs="$deplib $finalize_deplibs"
+	      fi
+	      ;;
+	    esac
+	    ;;
+	  esac
 	  continue
 	  ;;
 	-l*)
@@ -3153,6 +3273,7 @@ EOF
 	# Calculate the version variables.
 	major=
 	versuffix=
+	versuffix2=
 	verstring=
 	case $version_type in
 	none) ;;
@@ -3202,6 +3323,7 @@ EOF
 	linux)
 	  major=.`expr $current - $age`
 	  versuffix="$major.$age.$revision"
+	  versuffix2="$major.$age"
 	  ;;
 
 	osf)
@@ -3264,6 +3386,7 @@ EOF
 	if test "$avoid_version" = yes && test "$need_version" = no; then
 	  major=
 	  versuffix=
+	  versuffix2=
 	  verstring=""
 	fi
 
@@ -5462,8 +5585,16 @@ relink_command=\"$relink_command\""
       exit $EXIT_FAILURE
     fi
 
-    # Strip any trailing slash from the destination.
-    dest=`$echo "X$dest" | $Xsed -e 's%/$%%'`
+    # Canonicalise the pathname:
+    #   - remove foo/../
+    #   - replace //
+    #   - remove /./
+    #   - strip any trailing /
+    tmp=""
+    while test "$dest" != "$tmp";  do
+      tmp=$dest
+      dest=`$echo "X$dest" | $Xsed -e 's%[^/.][^/.]*/\.\.%%g' -e 's%/\./%/%g' -e 's%//*%/%g' -e 's%/$%%g'`
+    done
 
     # Check to see that the destination is a directory.
     test -d "$dest" && isdir=yes
@@ -5767,18 +5898,7 @@ relink_command=\"$relink_command\""
 	  outputname=
 	  if test "$fast_install" = no && test -n "$relink_command"; then
 	    if test "$finalize" = yes && test -z "$run"; then
-	      tmpdir="/tmp"
-	      test -n "$TMPDIR" && tmpdir="$TMPDIR"
-	      tmpdir="$tmpdir/libtool-$$"
-	      save_umask=`umask`
-	      umask 0077
-	      if $mkdir "$tmpdir"; then
-	        umask $save_umask
-	      else
-	        umask $save_umask
-		$echo "$modename: error: cannot create temporary directory \`$tmpdir'" 1>&2
-		continue
-	      fi
+	      tmpdir=`func_mktempdir`
 	      file=`$echo "X$file$stripped_ext" | $Xsed -e 's%^.*/%%'`
 	      outputname="$tmpdir/$file"
 	      # Replace the output file specification.
