1 dnl aclocal.m4 --- Dynamically linked library support for XEmacs
2 dnl Copyright (C) 1998, 1999 J. Kean Johnston.
3 dnl Author: J. Kean Johnston <jkj@sco.com>, based on work in libtool.
4 dnl This file is part of XEmacs.
7 dnl There are several things we care about here. First, we need to find
8 dnl out how we create an executable that has its symbols exported, so
9 dnl that dynamically loaded modules have access to the internal XEmacs
10 dnl symbols. This is stored in ``ld_dynamic_link_flags'' and is used
11 dnl in the main Makefile.
12 dnl Next, we need to know how we compile actual shared libraries, and
13 dnl the objects in them. For these purposes, we need to determine the
14 dnl C compiler flags used to produce shared objects (``dll_cflags''),
15 dnl what linker to use to create the final shared object that will be
16 dnl loaded (``dll_ld'') and the flags to pass to that linker
17 dnl (``dll_ldflags''). This information is used by ellcc to build up
18 dnl the command line when compiling modules. We build up two other commands
19 dnl for extremely weird systems where special things need to be done.
20 dnl The first is ``dll_ldo'', which is the flag used to specify the output
21 dnl file name, and the second is ``dll_post'' which is inserted after the
23 dnl After all of this, we should be able to:
24 dnl $(CC) $(CFLAGS) $(dll_cflags) -c module.c
25 dnl to produce a single shared object
27 dnl $(dll_ld) $(dll_ldflags) $(dll_ldo) module.ell module.o $(dll_post)
28 dnl to create the loadable shared library.
30 dnl NOTE: In the code below, where I have modified things to work with
31 dnl XEmacs, we use $canonical instead of libtool's $host, and we use
32 dnl $internal_configuration instead of $host_alias. To make typing
33 dnl shorter we assign these to $xehost and $xealias
35 AC_DEFUN(XE_SHLIB_STUFF,[
41 ld_dynamic_link_flags=
43 xealias=$internal_configuration
45 AC_CHECKING([how to build dynamic libraries for ${xehost}])
46 # Transform *-*-linux* to *-*-linux-gnu*, to support old configure scripts.
49 *-*-linux*) xehost=`echo $xehost | sed 's/^\(.*-.*-linux\)\(.*\)$/\1-gnu\2/'`
52 changequote(<<, >>)dnl
53 xehost_cpu=`echo $xehost | sed 's/^\([^-]*\)-\([^-]*\)-\(.*\)$/\1/'`
54 xehost_vendor=`echo $xehost | sed 's/^\([^-]*\)-\([^-]*\)-\(.*\)$/\2/'`
55 xehost_os=`echo $xehost | sed 's/^\([^-]*\)-\([^-]*\)-\(.*\)$/\3/'`
60 # AIX sometimes has problems with the GCC collect2 program. For some
61 # reason, if we set the COLLECT_NAMES environment variable, the problems
62 # vanish in a puff of smoke.
63 if test "${COLLECT_NAMES+set}" != set; then
70 # Now see if the compiler is really GCC.
71 if test "$GCC" = "yes"; then
74 AC_MSG_CHECKING(checking whether we are using GNU C)
79 ],XEGCC=yes, XEGCC=no)
80 AC_MSG_RESULT([${XEGCC}])
83 AC_MSG_CHECKING(how to produce PIC code)
87 if test "$XEGCC" = yes; then
91 aix3* | aix4* | irix5* | irix6* | osf3* | osf4*)
92 # PIC is the default for these OSes.
96 # We can build DLLs from non-PIC.
99 # FIXME: we need at least 68020 code to build shared libraries, but
100 # adding the `-m68020' flag to GCC prevents building anything better,
102 dll_cflags='-m68020 -resident32 -malways-restore-a4'
112 # PORTME Check for PIC flags for the system compiler.
114 hpux9* | hpux1[[0-9]]*)
115 # Is there a better link_static_flag that works with the bundled CC?
122 # PIC (with -KPIC) is the default.
126 # We can build DLLs from non-PIC.
130 # All OSF/1 code is PIC.
135 dll_cflags='-belf -Kpic'
174 if test -n "$dll_cflags"; then
175 AC_MSG_RESULT([${dll_cflags}])
177 # Check to make sure the dll_cflags actually works.
178 AC_MSG_CHECKING([if PIC flag ${dll_cflags} really works])
179 save_CFLAGS="$CFLAGS"
180 CFLAGS="$CFLAGS $dll_cflags -DPIC"
181 AC_TRY_COMPILE(,[int x=0;],[
182 # On HP-UX, the stripped-down bundled CC doesn't accept +Z, but also
183 # reports no error. So, we need to grep stderr for (Bundled).
184 if grep '(Bundled)' config.log >/dev/null; then
190 fi], [AC_MSG_RESULT(no)
193 CFLAGS="$save_CFLAGS"
199 dnl Now comes the LD trickery. We do things differently to libtool here.
200 dnl I believe that libtool is incorrect in trying to drive the linker
201 dnl directly. This can cause considerable problems if the module you are
202 dnl compiling has C++ or other static initializers. If we use ld directly,
203 dnl we don't end up with the crt stuff being linked in, and we don't end up
204 dnl with any .init or .fini sections (or the moral equivalent thereof).
205 dnl gcc takes great care to do this properly when invoked in -shared
206 dnl mode, and we really do want this behavior. Perhaps the libtool folks
207 dnl are not aware that any SVR4 based dynamic loader will automatically
208 dnl execute code in the .init section before dlopen() returns. This is
209 dnl vital, as the module may have been compiled to rely on that behavior.
211 dnl So, having said all of that, we diverge from libtool significantly
212 dnl here. We want to try and use the C compiler as much as possible. Only
213 dnl if the C compiler itself cannot create shared libraries to we try to
216 dnl The other advantage to my scheme is that it removes the dependancy
217 dnl on a given compiler version remaining static with relation to the
218 dnl version of XEmacs. With the libtool way, it picks up the linker that
219 dnl gcc uses, which can be the internal collect2 that comes with gcc.
220 dnl If the user ever changes their compiler version, the paths will no
221 dnl longer be correct, and ellcc will break. This is clearly unacceptable.
222 dnl By using the compiler driver on the path, we don't have this problem.
223 dnl If that is not clear, consider that gcc -print-prog-name=ld can
224 dnl produce something along the lines of:
225 dnl /usr/local/lib/gcc-lib/OS-NAME/GCC-VERSION/ld
226 dnl If you ever change GCC versions, then that path no longer exists.
228 dnl So, we change the check order here. We first check to see if we are
229 dnl using GCC, and if so, we see if -shared works. If it does, great.
230 dnl If we are not using gcc, but the system C compiler can produce
231 dnl shared objects, we try that. Only if all of that fails do we revert
232 dnl back to the libtool ld trickery.
234 dnl We don't do ANY of this if we can't produce shared objects.
236 if test "$can_build_shared" = "yes"; then
240 AC_MSG_CHECKING(if C compiler can produce shared libraries)
241 if test "$XEGCC" = yes; then
247 xldf="-bE:ELLSONAME.exp -H512 -T512 -bhalt:4 -bM:SRE -bnoentry -lc"
248 xcldf="${wl}-bE:ELLSONAME.exp ${wl}-H512 ${wl}-T512 ${wl}-bhalt:4 ${wl}-bM:SRE ${wl}-bnoentry ${wl}-lc"
251 freebsd2* | netbsd* | openbsd*)
253 xcldf="${wl}-Bshareable"
262 xcldf="${wl}-b ${wl}+s"
265 irix5* | irix6* | osf3* | osf4*)
270 sco3.2v5* | unixware* | sysv5* | sysv4* | solaris2* | solaris7* | uts4*)
276 xcldf="${wl}-assert ${wl}pure-text ${wl}-Bstatic"
277 xldf="-assert pure-text -Bstatic"
280 fi # End if if we are using gcc
282 if test -n "$xcldf"; then
283 save_LDFLAGS=$LDFLAGS
285 save_xe_libs=$xe_libs
286 LDFLAGS="$xcldf $LDFLAGS"
289 ac_link='${CC-cc} -o conftest $CFLAGS '"$xe_cppflags $xe_ldflags"' conftest.$ac_ext '"$xe_libs"' 1>&AC_FD_CC'
290 AC_TRY_LINK(,[int x=0;],cc_produces_so=yes,cc_produces_so=no)
291 LDFLAGS=$save_LDFLAGS
293 xe_libs=$save_xe_libs
294 ac_link='${CC-cc} -o conftest $CFLAGS '"$xe_cppflags $xe_ldflags"' conftest.$ac_ext '"$xe_libs"' 1>&AC_FD_CC'
298 AC_MSG_RESULT([${cc_produces_so}])
301 if test -z "$LTLD"; then
303 if test "$XEGCC" = yes; then
304 # Check if gcc -print-prog-name=ld gives a path.
305 AC_MSG_CHECKING(for ld used by GCC)
306 ac_prog=`($CC -print-prog-name=ld) 2>&5`
308 # Accept absolute paths.
310 if test -z "$LTLD"; then
312 *gcc-lib*) LTLD="$CC"
320 # If it fails, then pretend we aren't using GCC.
324 # If it is relative, then search for the first ld in PATH.
329 AC_MSG_CHECKING(for GNU ld)
332 if test -z "$LTLD"; then
333 IFS="${IFS= }"; ac_save_ifs="$IFS"; IFS="${IFS}:"
334 for ac_dir in $PATH; do
335 test -z "$ac_dir" && ac_dir=.
336 if test -f "$ac_dir/$ac_prog"; then
337 LTLD="$ac_dir/$ac_prog"
338 # Check to see if the program is GNU ld. I'd rather use --version,
339 # but apparently some GNU ld's only accept -v.
340 # Break only if it was the GNU/non-GNU ld that we prefer.
341 if "$LTLD" -v 2>&1 < /dev/null | egrep '(GNU|with BFD)' > /dev/null; then
351 if test -n "$LTLD"; then
352 AC_MSG_RESULT([${LTLD}])
357 if test -z "$LTLD" -a "$cc_produces_so" = no; then
358 AC_MSG_ERROR(no acceptable linker found in \$PATH)
364 dnl Order of the tests changed somewhat to prevent repetition
366 ld_dynamic_link_flags=
368 # Check to see if it really is or isn't GNU ld.
369 AC_MSG_CHECKING(if the linker is GNU ld)
370 # I'd rather use --version here, but apparently some GNU ld's only accept -v.
371 if $LTLD -v 2>&1 </dev/null | egrep '(GNU|with BFD)' 1>&5; then
376 AC_MSG_RESULT([${xe_gnu_ld}])
380 # On these operating systems, we should treat GNU ld like the system ld.
381 gnu_ld_acts_native=yes
384 gnu_ld_acts_native=no
388 if test "$cc_produces_so" = "yes"; then
394 # OK - only NOW do we futz about with ld.
395 # See if the linker supports building shared libraries.
396 AC_MSG_CHECKING(whether the linker supports shared libraries)
401 if test "$xe_gnu_ld" = yes && test "$gnu_ld_acts_native" != yes; then
402 # See if GNU ld supports shared libraries.
403 if $LTLD --help 2>&1 | egrep ': supported targets:.* elf' > /dev/null; then
405 dll_ldflags="-shared"
411 # PORTME fill in a description of your system's linker (not GNU ld)
422 # FreeBSD 2.2.[012] allows us to include c++rt0.o to get C++ constructor
423 # support. Future versions do this automatically, but an explicit c++rt0.o
424 # doesn't break anything, and helps significantly (at the cost of a little
429 dll_post="/usr/lib/c++rt0.o"
432 # Unfortunately, older versions of FreeBSD 2 don't have this feature.
435 dll_ldflags="-Bshareable"
438 # FreeBSD 3, at last, uses gcc -shared to do shared libraries.
440 dll_ldflags="-shared"
454 # Tested with NetBSD 1.2 ld
469 # For both SCO and Solaris we MAY want to have LDFLAGS include -z text
470 sco3.2v5* | unixware* | sysv5* | sysv4* | solaris2* | solaris7*)
473 *gcc*) dll_ldflags="-shared"
482 if test "$XEGCC" = yes; then
506 AC_MSG_RESULT([${ld_shlibs}])
507 if test "$ld_shlibs" = "no"; then
510 fi # End of if cc_produces_so = no
513 dnl Last thing, check how to get a linked executable to have its symbols
514 dnl exported, so that the modules have access to them.
516 dnl XEmacs FIXME - we need to set ld_dynamic_link_flags properly for
517 dnl most of these systems, which was missing from libtool. I know they
518 dnl all have a way of doing this, but someone needs to look at this
519 dnl for each OS and make sure it is correct. Remember that the arguments
520 dnl are passed when temacs is linked, this is NOT for modules. The sole
521 dnl purpose of the argument is to get the internal XEmacs symbols exposed
522 dnl for modules to use. This means that the COMPILER (and NOT the linker)
523 dnl is most often used to create temacs, so arguments to the linker will
524 dnl usually need to be prefix with ${wl} or some other such thing.
527 if test "$xe_gnu_ld" = yes; then
528 if test "$ld_shlibs" = yes; then
529 ld_dynamic_link_flags="${wl}-export-dynamic"
533 if test -z "$ld_dynamic_link_flags"; then
536 ld_dynamic_link_flags=
540 ld_dynamic_link_flags=
544 ld_dynamic_link_flags=
548 ld_dynamic_link_flags=
552 ld_dynamic_link_flags=
556 ld_dynamic_link_flags="${wl}-E"
560 ld_dynamic_link_flags=
564 ld_dynamic_link_flags=
568 ld_dynamic_link_flags=
572 ld_dynamic_link_flags=
575 solaris2* | solaris7*)
576 ld_dynamic_link_flags=
579 sco3.2v5* | unixware* | sysv5* | sysv4*)
580 ld_dynamic_link_flags="${wl}-Bexport"
584 ld_dynamic_link_flags=
588 ld_dynamic_link_flags=
592 ld_dynamic_link_flags=
596 fi # End of if -z ld_dynamic_link_flags
597 fi # End of if test "$can_build_shared" = "yes"
601 AC_SUBST(dll_ldflags)
604 AC_SUBST(ld_dynamic_link_flags)