# Author: Damyan Ivanov <dmn@debian.org>
# Description: avoid linking with libraries whose symbols are not used
# Forwarded: no
--- a/builds/posix/make.defaults
+++ b/builds/posix/make.defaults
@@ -148,11 +148,11 @@ LD =	@CXX@
 LIB_LINK:= @CXX@ $(GLOB_OPTIONS) $(CXXFLAGS)
 STATICLIB_LINK:= ar cruvs
 CLIENTLIB_LINK:= @CC@ $(GLOB_OPTIONS) $(CFLAGS)
-EXE_LINK:= @CXX@ $(GLOB_OPTIONS) $(CXXFLAGS)
-STATICEXE_LINK:= @CXX@ $(GLOB_OPTIONS) $(CXXFLAGS)
+EXE_LINK:= @CXX@ $(GLOB_OPTIONS) $(CXXFLAGS) $(THR_FLAGS)
+STATICEXE_LINK:= @CXX@ $(GLOB_OPTIONS) $(CXXFLAGS) $(THR_FLAGS)
 
 LINK_LIBS = @LIBS@
-ICU_LIBS = -licuuc -licudata -licui18n
+ICU_LIBS = -licuuc
 STATICLINK_LIBS = @LIBS@
 SO_LINK_LIBS = @LIBS@
 
@@ -264,8 +264,8 @@ LIB_LINK_SONAME= -Wl,-soname,$(1)
 LIB_LINK_MAPFILE= -Wl,--version-script,
 FBEMBED_LINK= -L$(LIB) -lfbembed
 
-LINK_OPTS= $(LDFLAGS) $(THR_FLAGS) $(UNDEF_FLAGS) $(LIB_PATH_OPTS)
-LIB_LINK_OPTIONS= $(LDFLAGS) $(THR_FLAGS) -shared
+LINK_OPTS= $(LDFLAGS) $(UNDEF_FLAGS) $(LIB_PATH_OPTS)
+LIB_LINK_OPTIONS= $(LDFLAGS) -shared
 
 FB_SUPER_SERVER	= $(BIN)/fbserver$(EXEC_EXT)
 FB_CLASSIC_SERVER = $(BIN)/fb_inet_server$(EXEC_EXT)
@@ -278,26 +278,26 @@ LIB_GUI =
 # Per-library link rules
 LINK_UDF = $(LIB_LINK) $(LIB_LINK_OPTIONS) $(call LIB_LINK_SONAME,$(1).$(SHRLIB_EXT))\
  $(call LIB_LINK_RPATH,lib) -lm
-LINK_UDF_LIBS = $(THR_LIBS) -L$(LIB) -lib_util
+LINK_UDF_LIBS = -L$(LIB) -lib_util
 
 LINK_IB_UTIL = $(LIB_LINK) $(LINK_IBUTIL_SYMBOLS) $(LIB_LINK_OPTIONS)\
  $(call LIB_LINK_SONAME,$(IbUtilLibraryName)) $(call LIB_LINK_RPATH,lib)
-LINK_IB_UTIL_LIBS = $(THR_LIBS)
+LINK_IB_UTIL_LIBS =
 
 LINK_INTL = $(LIB_LINK) $(LINK_FBINTL_SYMBOLS) $(LIB_LINK_OPTIONS)\
  $(call LIB_LINK_SONAME,libintl.$(SHRLIB_EXT).1) $(call LIB_LINK_RPATH,lib)
-LINK_INTL_LIBS = -L$(LIB) $(ICU_LIBS) $(SO_LINK_LIBS)
+LINK_INTL_LIBS = -L$(LIB) $(ICU_LIBS) $(SO_LINK_LIBS) $(THR_LIBS) $(THR_FLAGS)
 
 LINK_TRACE = $(LIB_LINK) $(LINK_TRACE_SYMBOLS) $(LIB_LINK_OPTIONS)\
  $(call LIB_LINK_SONAME,$(LIB_PREFIX)fbtrace.$(SHRLIB_EXT).0) $(call LIB_LINK_RPATH,lib)
 LINK_TRACE_LIBS = -L$(LIB) $(SO_LINK_LIBS)
 
 LINK_CLIENT = $(CLIENTLIB_LINK) $(LINK_FIREBIRD_CLIENT_SYMBOLS) $(LIB_LINK_OPTIONS)\
- $(LIB_LINK_IMPLIB) $(call LIB_LINK_SONAME,$(ClientLibrarySoName)) $(call LIB_LINK_RPATH,lib)
+ $(LIB_LINK_IMPLIB) $(call LIB_LINK_SONAME,$(ClientLibrarySoName)) $(call LIB_LINK_RPATH,lib) $(THR_LIBS) $(THR_FLAGS)
 LINK_CLIENT_LIBS = $(SO_LINK_LIBS) $(STATIC_CXXSUPPORT_LIB)
 
 LINK_EMBED = $(LIB_LINK) $(LINK_FIREBIRD_EMBED_SYMBOLS) $(LIB_LINK_OPTIONS) $(LIB_EMBED_LINK_OPTIONS)\
- $(call LIB_LINK_SONAME,$(SharedLibrarySoName)) $(call LIB_LINK_RPATH,lib)
+ $(call LIB_LINK_SONAME,$(SharedLibrarySoName)) $(call LIB_LINK_RPATH,lib) $(THR_LIBS) $(THR_FLAGS)
 LINK_EMBED_LIBS = -L$(LIB) $(LIB_GUI) $(SO_LINK_LIBS) $(ICU_LIBS)
 
 # From utilities 
--- a/configure.in
+++ b/configure.in
@@ -679,7 +679,6 @@ if test "$EDITLINE_FLG" = "Y"; then
 				AC_CHECK_LIB(tinfo, tgetent, TERMLIB=tinfo, \
 					AC_MSG_ERROR(termcap support not found)))))
 	AC_SUBST(TERMLIB)
-	XE_APPEND(-l$TERMLIB, LIBS)
 fi
 
 AC_SEARCH_LIBS(inet_aton, resolv)
@@ -805,6 +804,10 @@ AC_CHECK_FUNCS(pthread_mutex_consistent_
 
 dnl Semaphores
 if test "$ac_cv_header_semaphore_h" = "yes"; then
+    save_sem_libs=$LIBS
+    save_sem_cflags=$CFLAGS
+    LIBS="$LIBS $PTHREAD_LIBS"
+    CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
 AC_SEARCH_LIBS(sem_init, rt)
 AC_CHECK_FUNCS(sem_timedwait)
 AC_CHECK_FUNCS(sem_init)
@@ -821,6 +824,8 @@ AC_MSG_RESULT(yes)],
 [AC_MSG_RESULT(no)
 AC_SEARCH_LIBS(sem_open, rt)])
 fi
+LIBS=$save_sem_libs
+CFLAGS=$save_sem_cflags
 fi
 
 dnl HPUX has a bug in .h files. To detect it we need C++ here.
--- a/builds/posix/Makefile.in.client.util
+++ b/builds/posix/Makefile.in.client.util
@@ -174,13 +174,13 @@ $(FBSVCMGR):	$(FBSVCMGR_Objects) $(LIBFB
 fbtracemgr: $(FBTRACEMGR)
 
 $(FBTRACEMGR):	$(FBTRACEMGR_Objects) $(LIBFBCLIENT_SO)
-	$(EXE_LINK) $(LINK_EMPTY_SYMBOLS) $(LINK_OPTS) $^ -o $@ -L$(LIB) $(LINK_LIBS)
+	$(EXE_LINK) $(LINK_EMPTY_SYMBOLS) $(LINK_OPTS) $^ -o $@ -L$(LIB)
 
 
 fbguard: $(LIBFBCLIENT_SO) $(FBGUARD)
 
 $(FBGUARD):	$(FBGUARD_Objects) $(FBCOMMON_Objects) $(FBCLASSES_Objects)
-	$(EXE_LINK) $(LINK_EMPTY_SYMBOLS) $(LINK_OPTS) $^ -o $@ -L$(LIB) -lfbclient $(LIB_GUI) $(LINK_LIBS)
+	$(EXE_LINK) $(LINK_EMPTY_SYMBOLS) $(LINK_OPTS) $^ -o $@ -L$(LIB) -lfbclient $(LIB_GUI)
 
 
 ibguard: $(LIBFBCLIENT_SO) $(IBGUARD)
--- a/builds/posix/Makefile.in.embed.util
+++ b/builds/posix/Makefile.in.embed.util
@@ -169,7 +169,7 @@ $(FBSVCMGR):	$(FBSVCMGR_Objects) $(LIBFB
 fbtracemgr: $(FBTRACEMGR)
 
 $(FBTRACEMGR):	$(FBTRACEMGR_Objects) $(LIBFBEMBED_SO)
-	$(EXE_LINK) $(LINK_EMPTY_SYMBOLS) $(LINK_OPTS) $^ -o $@ $(FBEMBED_LINK) $(LINK_LIBS)
+	$(EXE_LINK) $(LINK_EMPTY_SYMBOLS) $(LINK_OPTS) $^ -o $@ $(FBEMBED_LINK)
 
 
 gds_relay: $(GDS_RELAY)
@@ -187,7 +187,7 @@ $(GSEC):	$(GSEC_Objects) $(CLUMPLETS_Obj
 fbguard: $(LIBFBCLIENT_SO) $(FBGUARD)
 
 $(FBGUARD):	$(FBGUARD_Objects) $(FBCOMMON_Objects) $(FBCLASSES_Objects)
-	$(EXE_LINK) $(LINK_EMPTY_SYMBOLS) $(LINK_OPTS) $^ -o $@ -L$(LIB) -lfbclient $(LIB_GUI) $(LINK_LIBS)
+	$(EXE_LINK) $(LINK_EMPTY_SYMBOLS) $(LINK_OPTS) $^ -o $@ -L$(LIB) -lfbclient $(LIB_GUI)
 
 
 ibmgr_bin: $(IBMGR_BIN)
--- a/builds/posix/Makefile.in.inet_server
+++ b/builds/posix/Makefile.in.inet_server
@@ -52,7 +52,7 @@ Dependencies = $(AllObjects:.o=.d)
 fb_inet_server : $(LIBFBEMBED_SO) $(FB_CLASSIC_SERVER)
 
 $(FB_CLASSIC_SERVER):	$(SERVER_Objects)
-	$(EXE_LINK) $(LINK_EMPTY_SYMBOLS) $(LINK_OPTS) $^ -o $@ $(FBEMBED_LINK) $(LIB_GUI) $(LINK_LIBS)
+	$(EXE_LINK) $(LINK_EMPTY_SYMBOLS) $(LINK_OPTS) $^ -o $@ $(FBEMBED_LINK) $(LIB_GUI)
 
 include $(ROOT)/gen/make.shared.targets
 
--- a/builds/posix/Makefile.in.fbtrace
+++ b/builds/posix/Makefile.in.fbtrace
@@ -78,7 +78,7 @@ else
 endif
 
 $(FBTRACE):	$(FBTRACE_Objects)
-	$(LINK_TRACE) -o $@ $^ $(LINK_TRACE_LIBS)
+	$(LINK_TRACE) -o $@ $^ $(LINK_TRACE_LIBS) $(ICU_LIBS) $(THR_FLAGS)
 
 
 include $(ROOT)/gen/make.shared.targets
