diff -r -c pgsql.orig/configure.in pgsql/configure.in
*** pgsql.orig/configure.in	2006-09-05 15:53:13.000000000 +0200
--- pgsql/configure.in	2006-09-05 16:18:56.000000000 +0200
***************
*** 709,722 ****
  fi
  
  
- if test "$with_ldap" = yes ; then
-   if test "$PORTNAME" != "win32"; then
-      AC_CHECK_LIB(ldap,    ldap_bind, [], [AC_MSG_ERROR([library 'ldap' is required for LDAP])])
-   else
-      AC_CHECK_LIB(wldap32, ldap_bind, [], [AC_MSG_ERROR([library 'wldap32' is required for LDAP])])
-   fi
- fi
- 
  ##
  ## Header files
  ##
--- 709,714 ----
***************
*** 1109,1122 ****
  PGAC_FUNC_GETPWUID_R_5ARG
  PGAC_FUNC_STRERROR_R_INT
  
- # this will link libpq against libldap_r
- if test "$with_ldap" = yes ; then
-   if test "$PORTNAME" != "win32"; then
-     AC_CHECK_LIB(ldap_r,    ldap_simple_bind, [], [AC_MSG_ERROR([library 'ldap_r' is required for LDAP])])
-     PTHREAD_LIBS="$PTHREAD_LIBS -lldap_r"
-   fi
- fi
- 
  CFLAGS="$_CFLAGS"
  LIBS="$_LIBS"
  
--- 1101,1106 ----
***************
*** 1130,1135 ****
--- 1114,1147 ----
  AC_SUBST(PTHREAD_LIBS)
  
  
+ # We can test for libldap_r only after we know PTHREAD_LIBS
+ if test "$with_ldap" = yes ; then
+   _LIBS="$LIBS"
+   if test "$PORTNAME" != "win32"; then
+     AC_CHECK_LIB(ldap, ldap_bind, [], [AC_MSG_ERROR([library 'ldap' is required for LDAP])])
+     LDAP_LIBS_BE="-lldap"
+     if test "$enable_thread_safety" = yes; then
+       # try without PTHREAD_LIBS first, different function to avoid cache
+       AC_CHECK_LIB(ldap_r, ldap_bind, [], [
+         AC_CHECK_LIB(ldap_r, ldap_simple_bind,
+           [LIBS="-lldap_r $PTHREAD_LIBS $LIBS"; HAVE_LIBLDAP_R=1],
+           [AC_MSG_ERROR([library 'ldap_r' is required for LDAP])], [$PTHREAD_LIBS])
+       ])
+       LDAP_LIBS_FE="-lldap_r"
+     else
+       LDAP_LIBS_FE="-lldap"
+     fi
+   else
+     AC_CHECK_LIB(wldap32,  ldap_bind, [], [AC_MSG_ERROR([library 'wldap32' is required for LDAP])])
+     LDAP_LIBS_FE="-lwldap32"
+     LDAP_LIBS_BE="-lwldap32"
+   fi
+   LIBS="$_LIBS"
+ fi
+ AC_SUBST(LDAP_LIBS_FE)
+ AC_SUBST(LDAP_LIBS_BE)
+ 
+ 
  # This test makes sure that run tests work at all.  Sometimes a shared
  # library is found by the linker, but the runtime linker can't find it.
  # This check should come after all modifications of compiler or linker
diff -r -c pgsql.orig/src/backend/Makefile pgsql/src/backend/Makefile
*** pgsql.orig/src/backend/Makefile	2006-09-05 15:53:16.000000000 +0200
--- pgsql/src/backend/Makefile	2006-09-05 16:20:16.000000000 +0200
***************
*** 26,32 ****
  OBJS = $(SUBSYSOBJS) $(LOCALOBJS) $(top_builddir)/src/port/libpgport_srv.a
  
  # We put libpgport into OBJS, so remove it from LIBS
! LIBS := $(filter-out -lpgport, $(LIBS))
  
  # The backend doesn't need everything that's in LIBS, however
  LIBS := $(filter-out -lz -lreadline -ledit -ltermcap -lncurses -lcurses, $(LIBS))
--- 26,32 ----
  OBJS = $(SUBSYSOBJS) $(LOCALOBJS) $(top_builddir)/src/port/libpgport_srv.a
  
  # We put libpgport into OBJS, so remove it from LIBS
! LIBS := $(filter-out -lpgport, $(LIBS) $(LDAP_LIBS_BE))
  
  # The backend doesn't need everything that's in LIBS, however
  LIBS := $(filter-out -lz -lreadline -ledit -ltermcap -lncurses -lcurses, $(LIBS))
diff -r -c pgsql.orig/src/bin/pg_ctl/Makefile pgsql/src/bin/pg_ctl/Makefile
*** pgsql.orig/src/bin/pg_ctl/Makefile	2006-09-05 15:53:19.000000000 +0200
--- pgsql/src/bin/pg_ctl/Makefile	2006-09-07 16:39:50.000000000 +0200
***************
*** 21,27 ****
  all: submake-libpq submake-libpgport pg_ctl
  
  pg_ctl: $(OBJS) $(libpq_builddir)/libpq.a
! 	$(CC) $(CFLAGS) $(OBJS) $(libpq_pgport) $(LDFLAGS) $(LIBS) -o $@$(X)
  
  install: all installdirs
  	$(INSTALL_PROGRAM) pg_ctl$(X) '$(DESTDIR)$(bindir)/pg_ctl$(X)'
--- 21,27 ----
  all: submake-libpq submake-libpgport pg_ctl
  
  pg_ctl: $(OBJS) $(libpq_builddir)/libpq.a
! 	$(CC) $(CFLAGS) $(OBJS) $(libpq_pgport) $(LDFLAGS) $(LIBS) $(LDAP_LIBS_FE) -o $@$(X)
  
  install: all installdirs
  	$(INSTALL_PROGRAM) pg_ctl$(X) '$(DESTDIR)$(bindir)/pg_ctl$(X)'
diff -r -c pgsql.orig/src/bin/pg_dump/Makefile pgsql/src/bin/pg_dump/Makefile
*** pgsql.orig/src/bin/pg_dump/Makefile	2006-09-05 15:53:19.000000000 +0200
--- pgsql/src/bin/pg_dump/Makefile	2006-09-07 16:29:43.000000000 +0200
***************
*** 15,20 ****
--- 15,21 ----
  include $(top_builddir)/src/Makefile.global
  
  override CPPFLAGS := -DFRONTEND -I$(libpq_srcdir) $(CPPFLAGS)
+ LIBS += $(LDAP_LIBS_FE)
  
  OBJS=	pg_backup_archiver.o pg_backup_db.o pg_backup_custom.o \
  	pg_backup_files.o pg_backup_null.o pg_backup_tar.o \
diff -r -c pgsql.orig/src/bin/psql/Makefile pgsql/src/bin/psql/Makefile
*** pgsql.orig/src/bin/psql/Makefile	2006-09-05 15:53:19.000000000 +0200
--- pgsql/src/bin/psql/Makefile	2006-09-07 16:32:27.000000000 +0200
***************
*** 31,37 ****
  all: submake-libpq submake-libpgport submake-backend psql
  
  psql: $(OBJS) $(libpq_builddir)/libpq.a
! 	$(CC) $(CFLAGS) $(OBJS) $(EXTRA_OBJS) $(libpq_pgport) $(LDFLAGS) $(LIBS) -o $@$(X)
  
  help.o: $(srcdir)/sql_help.h
  
--- 31,37 ----
  all: submake-libpq submake-libpgport submake-backend psql
  
  psql: $(OBJS) $(libpq_builddir)/libpq.a
! 	$(CC) $(CFLAGS) $(OBJS) $(EXTRA_OBJS) $(libpq_pgport) $(LDFLAGS) $(LIBS) $(LDAP_LIBS_FE) -o $@$(X)
  
  help.o: $(srcdir)/sql_help.h
  
diff -r -c pgsql.orig/src/bin/scripts/Makefile pgsql/src/bin/scripts/Makefile
*** pgsql.orig/src/bin/scripts/Makefile	2006-09-05 15:53:19.000000000 +0200
--- pgsql/src/bin/scripts/Makefile	2006-09-07 16:33:44.000000000 +0200
***************
*** 21,27 ****
  all: submake-libpq submake-backend $(PROGRAMS)
  
  %: %.o $(WIN32RES)
! 	$(CC) $(CFLAGS) $^ $(libpq_pgport) $(LDFLAGS) $(LIBS) -o $@$(X)
  
  createdb: createdb.o common.o dumputils.o $(top_builddir)/src/backend/parser/keywords.o
  createlang: createlang.o common.o print.o mbprint.o
--- 21,27 ----
  all: submake-libpq submake-backend $(PROGRAMS)
  
  %: %.o $(WIN32RES)
! 	$(CC) $(CFLAGS) $^ $(libpq_pgport) $(LDFLAGS) $(LIBS) $(LDAP_LIBS_FE) -o $@$(X)
  
  createdb: createdb.o common.o dumputils.o $(top_builddir)/src/backend/parser/keywords.o
  createlang: createlang.o common.o print.o mbprint.o
diff -r -c pgsql.orig/src/interfaces/ecpg/compatlib/Makefile pgsql/src/interfaces/ecpg/compatlib/Makefile
*** pgsql.orig/src/interfaces/ecpg/compatlib/Makefile	2006-09-05 15:53:20.000000000 +0200
--- pgsql/src/interfaces/ecpg/compatlib/Makefile	2006-09-06 11:28:17.000000000 +0200
***************
*** 22,28 ****
  override CFLAGS += $(PTHREAD_CFLAGS)
  SHLIB_LINK = -L../ecpglib -lecpg -L../pgtypeslib -lpgtypes $(libpq) \
  	$(filter -lintl -lssl -lcrypto -lkrb5 -lcrypt -lm, $(LIBS)) \
! 	$(PTHREAD_LIBS)
  
  OBJS= informix.o
  
--- 22,28 ----
  override CFLAGS += $(PTHREAD_CFLAGS)
  SHLIB_LINK = -L../ecpglib -lecpg -L../pgtypeslib -lpgtypes $(libpq) \
  	$(filter -lintl -lssl -lcrypto -lkrb5 -lcrypt -lm, $(LIBS)) \
! 	$(LDAP_LIBS_FE) $(PTHREAD_LIBS)
  
  OBJS= informix.o
  
diff -r -c pgsql.orig/src/interfaces/ecpg/ecpglib/Makefile pgsql/src/interfaces/ecpg/ecpglib/Makefile
*** pgsql.orig/src/interfaces/ecpg/ecpglib/Makefile	2006-09-05 15:53:20.000000000 +0200
--- pgsql/src/interfaces/ecpg/ecpglib/Makefile	2006-09-06 11:27:16.000000000 +0200
***************
*** 35,41 ****
  endif
  
  SHLIB_LINK = -L../pgtypeslib -lpgtypes $(libpq) \
! 	$(filter -lintl -lssl -lcrypto -lkrb5 -lcrypt -lm, $(LIBS)) $(PTHREAD_LIBS)
  
  ifeq ($(PORTNAME), win32)
  # Link to shfolder.dll instead of shell32.dll
--- 35,41 ----
  endif
  
  SHLIB_LINK = -L../pgtypeslib -lpgtypes $(libpq) \
! 	$(filter -lintl -lssl -lcrypto -lkrb5 -lcrypt -lm, $(LIBS)) $(LDAP_LIBS_FE) $(PTHREAD_LIBS)
  
  ifeq ($(PORTNAME), win32)
  # Link to shfolder.dll instead of shell32.dll
diff -r -c pgsql.orig/src/interfaces/ecpg/test/Makefile.regress pgsql/src/interfaces/ecpg/test/Makefile.regress
*** pgsql.orig/src/interfaces/ecpg/test/Makefile.regress	2006-09-05 15:53:21.000000000 +0200
--- pgsql/src/interfaces/ecpg/test/Makefile.regress	2006-09-07 16:23:31.000000000 +0200
***************
*** 3,9 ****
  override CFLAGS += $(PTHREAD_CFLAGS) 
  
  override LDFLAGS := -L../../ecpglib -L../../pgtypeslib -L../../../libpq $(LDFLAGS)
! override LIBS := -lpgtypes -lecpg -lpq $(LIBS) $(PTHREAD_LIBS)
  
  ECPG = ../../preproc/ecpg -I$(srcdir)/../../include
  
--- 3,9 ----
  override CFLAGS += $(PTHREAD_CFLAGS) 
  
  override LDFLAGS := -L../../ecpglib -L../../pgtypeslib -L../../../libpq $(LDFLAGS)
! override LIBS := -lpgtypes -lecpg -lpq $(LIBS) $(LDAP_LIBS_FE) $(PTHREAD_LIBS)
  
  ECPG = ../../preproc/ecpg -I$(srcdir)/../../include
  
diff -r -c pgsql.orig/src/interfaces/libpq/Makefile pgsql/src/interfaces/libpq/Makefile
*** pgsql.orig/src/interfaces/libpq/Makefile	2006-09-05 15:53:21.000000000 +0200
--- pgsql/src/interfaces/libpq/Makefile	2006-09-05 16:16:32.000000000 +0200
***************
*** 57,68 ****
  # shared library link.  (The order in which you list them here doesn't
  # matter.)
  ifneq ($(PORTNAME), win32)
! SHLIB_LINK += $(filter -lcrypt -ldes -lcom_err -lcrypto -lk5crypto -lkrb5 -lssl -lsocket -lnsl -lresolv -lintl, $(LIBS)) $(PTHREAD_LIBS)
  else
! SHLIB_LINK += $(filter -lcrypt -ldes -lcom_err -lcrypto -lk5crypto -lkrb5 -lssl -lsocket -lnsl -lresolv -lintl $(PTHREAD_LIBS), $(LIBS))
  endif
  ifeq ($(PORTNAME), win32)
! SHLIB_LINK += -lshfolder -lwsock32 -lws2_32 $(filter -leay32 -lssleay32 -lcomerr32 -lkrb5_32 -lwldap32, $(LIBS))
  endif
  
  
--- 57,68 ----
  # shared library link.  (The order in which you list them here doesn't
  # matter.)
  ifneq ($(PORTNAME), win32)
! SHLIB_LINK += $(filter -lcrypt -ldes -lcom_err -lcrypto -lk5crypto -lkrb5 -lssl -lsocket -lnsl -lresolv -lintl -lldap -lldap_r, $(LIBS) $(LDAP_LIBS_FE)) $(PTHREAD_LIBS)
  else
! SHLIB_LINK += $(filter -lcrypt -ldes -lcom_err -lcrypto -lk5crypto -lkrb5 -lssl -lsocket -lnsl -lresolv -lintl -lwldap32 $(PTHREAD_LIBS), $(LIBS) $(LDAP_LIBS_FE))
  endif
  ifeq ($(PORTNAME), win32)
! SHLIB_LINK += -lshfolder -lwsock32 -lws2_32 $(filter -leay32 -lssleay32 -lcomerr32 -lkrb5_32, $(LIBS))
  endif
  
  
diff -r -c pgsql.orig/src/Makefile.global.in pgsql/src/Makefile.global.in
*** pgsql.orig/src/Makefile.global.in	2006-09-05 15:53:21.000000000 +0200
--- pgsql/src/Makefile.global.in	2006-09-05 16:17:27.000000000 +0200
***************
*** 219,224 ****
--- 219,226 ----
  # Linking
  
  LIBS = @LIBS@
+ LDAP_LIBS_FE = @LDAP_LIBS_FE@
+ LDAP_LIBS_BE = @LDAP_LIBS_BE@
  LD = @LD@
  with_gnu_ld = @with_gnu_ld@
  ld_R_works = @ld_R_works@
