Subject: 24.1.50; configure fails on Gentoo FreeBSD

From: Ulrich Mueller <[email-masked]>
Date: Thu, 26 Jul 2012 01:05:32 +0200

Emacs from BZR (but also 24.1) fails on Gentoo FreeBSD 9.0 during
configure:

   configure: error: Required file(s) not found: crtn.o crt1.o crti.o
   Try using the --with-crt-dir option.
   $ uname -a
   FreeBSD eunomia 9.0-Gentoo FreeBSD Gentoo 9.0 #0: Tue Jul 24 02:42:55 CEST 2012     root@:/usr/src/sys/amd64/compile/GENERIC  amd64

The problem is that crt{1,i,n}.o (which are installed by freebsd-lib)
are located in /usr/lib, whereas crt{begin,end}.o (installed by gcc)
are in /usr/lib/gcc/x86_64-gentoo-freebsd9.0/4.5.3.

Obviously, specifying a location with --with-crt-dir wouldn't help in
this situation, because configure assumes that all crt*.o files are
in the same directory.

Attached patch fixes the problem for me.

(Another question is why crtbegin.o and crtend.o are needed for
linking on FreeBSD, in the first place? Emacs builds and runs here
without apparent problems, even if linked without these files.)

--- emacs-orig/ChangeLog
+++ emacs/ChangeLog
@@ -1,3 +1,10 @@
+2012-07-25  Ulrich Müller  <[email-masked]>
+
+	* configure.in: Don't assume that crtbegin.o and crtend.o are in
+	the same directory as crt1.o.
+	(--with-crtbegin-dir): New option.
+	(CRTBEGIN_DIR): New output variable.
+
 2012-07-17  Dmitry Antipov  <[email-masked]>
 
 	Fix toolkit configuration report.
--- emacs-orig/src/ChangeLog
+++ emacs/src/ChangeLog
@@ -1,3 +1,7 @@
+2012-07-25  Ulrich Müller  <[email-masked]>
+
+	* Makefile.in (CRTBEGIN_DIR): New variable.
+
 2012-07-25  Martin Rudalics  <[email-masked]>
 
 	* frame.c (Fredirect_frame_focus): In doc-string don't mention
--- emacs-orig/configure.ac
+++ emacs/configure.ac
@@ -212,6 +212,12 @@
 The default is /usr/lib, or /usr/lib64 on some platforms.])])
 CRT_DIR="${with_crt_dir}"
 
+CRTBEGIN_DIR=$CRT_DIR
+AC_ARG_WITH([crtbegin-dir],
+[AS_HELP_STRING([--with-crtbegin-dir=DIR],
+[directory containing crtbegin.o and crtend.o; defaults to crt-dir])])
+CRTBEGIN_DIR="${with_crtbegin_dir}"
+
 AC_ARG_WITH(gameuser,dnl
 [AS_HELP_STRING([--with-gameuser=USER],[user for shared game score files])])
 test "X${with_gameuser}" != X && test "${with_gameuser}" != yes \
@@ -987,8 +993,8 @@
     START_FILES='pre-crt0.o'
     ;;
   freebsd )
-    LIB_STANDARD='-lgcc -lc -lgcc $(CRT_DIR)/crtend.o $(CRT_DIR)/crtn.o'
-    START_FILES='pre-crt0.o $(CRT_DIR)/crt1.o $(CRT_DIR)/crti.o $(CRT_DIR)/crtbegin.o'
+    LIB_STANDARD='-lgcc -lc -lgcc $(CRTBEGIN_DIR)/crtend.o $(CRT_DIR)/crtn.o'
+    START_FILES='pre-crt0.o $(CRT_DIR)/crt1.o $(CRT_DIR)/crti.o $(CRTBEGIN_DIR)/crtbegin.o'
     SYSTEM_TYPE=berkeley-unix
     ;;
   gnu-linux | gnu-kfreebsd )
@@ -1001,8 +1007,8 @@
     ;;
   dnl NB this may be adjusted below.
   netbsd | openbsd )
-    LIB_STANDARD='-lgcc -lc -lgcc $(CRT_DIR)/crtend.o'
-    START_FILES='pre-crt0.o $(CRT_DIR)/crt0.o $(CRT_DIR)/crtbegin.o'
+    LIB_STANDARD='-lgcc -lc -lgcc $(CRTBEGIN_DIR)/crtend.o'
+    START_FILES='pre-crt0.o $(CRT_DIR)/crt0.o $(CRTBEGIN_DIR)/crtbegin.o'
     SYSTEM_TYPE=berkeley-unix
     ;;
 
@@ -1019,14 +1025,17 @@
 
 dnl Not all platforms use crtn.o files.  Check if the current one does.
 crt_files=
+crtbegin_files=
 
 for file in x $LIB_STANDARD $START_FILES; do
   case "$file" in
     *CRT_DIR*) crt_files="$crt_files `echo $file | sed -e 's|.*/||'`" ;;
+    *CRTBEGIN_DIR*)
+      crtbegin_files="$crtbegin_files `echo $file | sed -e 's|.*/||'`" ;;
   esac
 done
 
-if test "x$crt_files" != x; then
+if test "x$crt_files" != x || test "x$crtbegin_files" != x; then
 
   ## If user specified a crt-dir, use that unconditionally.
   crt_gcc=no
@@ -1058,6 +1067,12 @@
 
   fi                            # CRT_DIR = ""
 
+  crtbegin_gcc=no
+  if test "X$CRTBEGIN_DIR" = X; then
+    CRTBEGIN_DIR=$CRT_DIR
+    crtbegin_gcc=$crt_gcc
+  fi
+
   crt_missing=
 
   for file in $crt_files; do
@@ -1082,13 +1097,26 @@
     test -e $CRT_DIR/$file || crt_missing="$crt_missing $file"
   done                          # $crt_files
 
+  dnl Same as above, but for crtbegin.o and crtend.o.
+  for file in $crtbegin_files; do
+    if test $crtbegin_gcc = yes && test ! -e "$CRTBEGIN_DIR/$file"; then
+       crt_file=`$CC --print-file-name=$file 2>/dev/null`
+       case "$crt_file" in
+         */*) CRTBEGIN_DIR=`AS_DIRNAME(["$crt_file"])` ;;
+       esac
+    fi
+    crtbegin_gcc=no
+    test -e "$CRTBEGIN_DIR/$file" || crt_missing="$crt_missing $file"
+  done
+
   test "x$crt_missing" = x || \
     AC_MSG_ERROR([Required file(s) not found:$crt_missing
-Try using the --with-crt-dir option.])
+Try using the --with-crt-dir or --with-crtbegin-dir option.])
 
-fi                              # crt_files != ""
+fi                              # crt_files != "" || crtbegin_files != ""
 
 AC_SUBST(CRT_DIR)
+AC_SUBST(CRTBEGIN_DIR)
 
 case $opsys in
   netbsd | openbsd )
@@ -1097,8 +1125,8 @@
         test -f $CRT_DIR/crtn.o || \
           AC_MSG_ERROR([Required file not found: crtn.o])
 
-        LIB_STANDARD='-lgcc -lc -lgcc $(CRT_DIR)/crtend.o $(CRT_DIR)/crtn.o'
-        START_FILES='pre-crt0.o $(CRT_DIR)/crt0.o $(CRT_DIR)/crti.o $(CRT_DIR)/crtbegin.o'
+        LIB_STANDARD='-lgcc -lc -lgcc $(CRTBEGIN_DIR)/crtend.o $(CRT_DIR)/crtn.o'
+        START_FILES='pre-crt0.o $(CRT_DIR)/crt0.o $(CRT_DIR)/crti.o $(CRTBEGIN_DIR)/crtbegin.o'
     fi
     ;;
 esac
--- emacs-orig/src/Makefile.in
+++ emacs/src/Makefile.in
@@ -126,6 +126,7 @@
 LIB_GCC=@LIB_GCC@
 
 CRT_DIR=@CRT_DIR@
+CRTBEGIN_DIR=@CRTBEGIN_DIR@
 ## May use $CRT_DIR.
 LIB_STANDARD=@LIB_STANDARD@
 START_FILES = @START_FILES@