Make sure that the args array is initialized
[sxemacs] / m4 / sxe-linker.m4
1 dnl sxe-linker.m4 -- Linker stuff, esp. dynamic linking
2 dnl needed for emodules for one
3
4 dnl that's a too fucking long thing, split it!
5 AC_DEFUN([SXE_SHLIB_STUFF],[
6 dll_ld=
7 dll_ldflags=
8 dll_cflags=
9 dll_post=
10 dll_ldo="-o"
11 ld_dynamic_link_flags=
12 xehost=$ac_cv_build
13 xealias=$ac_cv_build_alias
14
15 AC_MSG_CHECKING([how to build dynamic libraries for ${xehost}])
16 AC_MSG_RESULT([])
17 # Transform *-*-linux* to *-*-linux-gnu*, to support old configure scripts.
18 case "$xehost" in
19 *-*-linux-gnu*) ;;
20 *-*-linux*) xehost=`echo $xehost | sed 's/^\(.*-.*-linux\)\(.*\)$/\1-gnu\2/'`
21 esac
22
23 xehost_cpu=`echo $xehost | sed 's/^\([[^-]]*\)-\([[^-]]*\)-\(.*\)$/\1/'`
24 xehost_vendor=`echo $xehost | sed 's/^\([[^-]]*\)-\([[^-]]*\)-\(.*\)$/\2/'`
25 xehost_os=`echo $xehost | sed 's/^\([[^-]]*\)-\([[^-]]*\)-\(.*\)$/\3/'`
26
27 case "$xehost_os" in
28 aix3*)
29   # AIX sometimes has problems with the GCC collect2 program.  For some
30   # reason, if we set the COLLECT_NAMES environment variable, the problems
31   # vanish in a puff of smoke.
32   if test "${COLLECT_NAMES+set}" != set; then
33     COLLECT_NAMES=
34     export COLLECT_NAMES
35   fi
36   ;;
37 esac
38
39 # Now see if the compiler is really GCC.
40 if test "$GCC" = "yes"; then
41   XEGCC=yes
42 else
43   AC_MSG_CHECKING([whether we are using GNU C])
44   AC_EGREP_CPP(yes,[
45 #ifdef __GNUC__
46   yes;
47 #endif
48 ],XEGCC=yes, XEGCC=no)
49   AC_MSG_RESULT([${XEGCC}])
50 fi
51
52 AC_MSG_CHECKING([how to produce PIC code])
53 wl=
54
55 can_build_shared=yes
56 if test "$XEGCC" = yes -o "$__ICC" = yes; then
57   wl='-Wl,'
58
59   case "$xehost_os" in
60   aix[[3-9]]* | irix[[5-9]]* | osf[[3-9]])
61     # PIC is the default for these OSes.
62     ;;
63
64   *darwin*) dll_cflags='-dynamic'
65     ;;
66
67   os2*)
68     # We can build DLLs from non-PIC.
69     ;;
70   amigaos*)
71     # FIXME: we need at least 68020 code to build shared libraries, but
72     # adding the `-m68020' flag to GCC prevents building anything better,
73     # like `-m68040'.
74     dll_cflags='-m68020 -resident32 -malways-restore-a4'
75     ;;
76   *)
77     dll_cflags='-fPIC'
78     ;;
79   esac
80 else
81   # PORTME Check for PIC flags for the system compiler.
82   case "$xehost_os" in
83   hpux9* | hpux1[[0-9]]*)
84     # Is there a better link_static_flag that works with the bundled CC?
85     wl='-Wl,'
86     dll_cflags='+Z'
87     ;;
88
89   irix[[5-9]]*)
90     wl='-Wl,'
91     # PIC (with -KPIC) is the default.
92     ;;
93
94   os2*)
95     # We can build DLLs from non-PIC.
96     ;;
97
98   osf[[3-9]]*)
99     # All OSF/1 code is PIC.
100     wl='-Wl,'
101     ;;
102
103   aix[[3-9]]*)
104     # All AIX code is PIC.
105     wl='-Wl,'
106     ;;
107
108   sco3.2v5*)
109     dll_cflags='-belf -Kpic'
110     wl='-Wl,'
111     ;;
112
113   unixware*)
114     dll_cflags="-KPIC"
115     wl="-Wl,"
116     ;;
117
118   sysv4*)
119     dll_cflags="-KPIC"
120     wl="-Wl,"
121     ;;
122
123   sysv5*)
124     dll_cflags="-KPIC"
125     wl="-Wl,"
126     ;;
127
128   solaris2*)
129     dll_cflags='-KPIC'
130     wl='-Wl,'
131     ;;
132
133   sunos4*)
134     dll_cflags='-PIC'
135     wl='-Qoption ld '
136     ;;
137
138   uts4*)
139     dll_cflags='-pic'
140     ;;
141
142   *)
143     can_build_shared=no
144     ;;
145   esac
146 fi
147
148 if test -n "$dll_cflags"; then
149   AC_MSG_RESULT([${dll_cflags}])
150
151   # Check to make sure the dll_cflags actually works.
152   AC_MSG_CHECKING([if PIC flag ${dll_cflags} really works])
153   save_CFLAGS="$CFLAGS"
154   CFLAGS="$CFLAGS $dll_cflags -DPIC"
155   AC_COMPILE_IFELSE([AC_LANG_SOURCE([[int x=0;]])],[
156     # On HP-UX, the stripped-down bundled CC doesn't accept +Z, but also
157     # reports no error.  So, we need to grep stderr for (Bundled).
158     if grep '(Bundled)' config.log >/dev/null; then
159       AC_MSG_RESULT(no)
160       can_build_shared=no
161       dll_cflags=
162     else
163       AC_MSG_RESULT(yes)
164     fi], [AC_MSG_RESULT(no)
165     can_build_shared=no
166     dll_cflags=])
167   CFLAGS="$save_CFLAGS"
168 else
169   AC_MSG_RESULT(none)
170 fi
171
172 dnl
173 dnl Now comes the LD trickery. We do things differently to libtool here.
174 dnl I believe that libtool is incorrect in trying to drive the linker
175 dnl directly. This can cause considerable problems if the module you are
176 dnl compiling has C++ or other static initializers. If we use ld directly,
177 dnl we don't end up with the crt stuff being linked in, and we don't end up
178 dnl with any .init or .fini sections (or the moral equivalent thereof).
179 dnl gcc takes great care to do this properly when invoked in -shared
180 dnl mode, and we really do want this behavior. Perhaps the libtool folks
181 dnl are not aware that any SVR4 based dynamic loader will automatically
182 dnl execute code in the .init section before dlopen() returns. This is
183 dnl vital, as the module may have been compiled to rely on that behavior.
184 dnl
185 dnl So, having said all of that, we diverge from libtool significantly
186 dnl here. We want to try and use the C compiler as much as possible. Only
187 dnl if the C compiler itself cannot create shared libraries do we try to
188 dnl find the linker.
189 dnl
190 dnl The other advantage to my scheme is that it removes the dependancy
191 dnl on a given compiler version remaining static with relation to the
192 dnl version of XEmacs. With the libtool way, it picks up the linker that
193 dnl gcc uses, which can be the internal collect2 that comes with gcc.
194 dnl If the user ever changes their compiler version, the paths will no
195 dnl longer be correct, and ellcc will break. This is clearly unacceptable.
196 dnl By using the compiler driver on the path, we don't have this problem.
197 dnl If that is not clear, consider that gcc -print-prog-name=ld can
198 dnl produce something along the lines of:
199 dnl   /usr/local/lib/gcc-lib/OS-NAME/GCC-VERSION/ld
200 dnl If you ever change GCC versions, then that path no longer exists.
201 dnl
202 dnl So, we change the check order here. We first check to see if we are
203 dnl using GCC, and if so, we see if -shared works. If it does, great.
204 dnl If we are not using gcc, but the system C compiler can produce
205 dnl shared objects, we try that. Only if all of that fails do we revert
206 dnl back to the libtool ld trickery.
207 dnl
208 dnl We don't do ANY of this if we can't produce shared objects.
209 dnl
210 if test "$can_build_shared" = "yes"; then
211 cc_produces_so=no
212 xldf=
213 xcldf=
214 AC_MSG_CHECKING([if C compiler can produce shared libraries])
215 if test "$XEGCC" = "yes" -o "$__ICC" = "yes"; then
216         case "$xehost_os" in
217         *darwin*)
218                 xcldf='-bundle'
219                 xldf='-bundle -undefined suppress -flat_namespace'
220                 ;;
221         *)
222                 xcldf="-shared"
223                 xldf="-shared"
224                 ;;
225         esac
226 else # Not using GCC
227         case "$xehost_os" in
228         aix[[3-9]]*)
229                 xldf="-bE:ELLSONAME.exp -H512 -T512 -bhalt:4 -bM:SRE -bnoentry -lc"
230                 xcldf="${wl}-bE:ELLSONAME.exp ${wl}-H512 ${wl}-T512 ${wl}-bhalt:4 ${wl}-bM:SRE ${wl}-bnoentry ${wl}-lc"
231                 ;;
232
233         freebsd2* | netbsd* | openbsd*)
234                 xldf="-Bshareable"
235                 xcldf="${wl}-Bshareable"
236                 ;;
237
238         freebsd3*)
239                 xcldf="-shared"
240                 ;;
241
242         hpux*)
243                 xldf="-b +s"
244                 xcldf="${wl}-b ${wl}+s"
245                 ;;
246
247         irix[[5-9]]* | osf[[3-9]]*)
248                 xcldf="${wl}-shared"
249                 xldf="-shared"
250                 ;;
251
252         sco3.2v5* | unixware* | sysv5* | sysv4* | solaris2* | solaris7* | uts4*)
253                 xcldf="-G"
254                 xldf="-G"
255                 ;;
256
257         sunos4*)
258                 xcldf="${wl}-assert ${wl}pure-text ${wl}-Bstatic"
259                 xldf="-assert pure-text -Bstatic"
260                 ;;
261         esac
262 fi # End if if we are using gcc
263
264 if test -n "$xcldf"; then
265         SXE_DUMP_LIBS
266         LDFLAGS="$xcldf $LDFLAGS"
267         LIBS=
268 dnl     xe_libs=
269 dnl     ac_link='${CC-cc} -o conftest $CFLAGS $CPPFLAGS $LDFLAGS '"$xe_cppflags $xe_ldflags"' conftest.$ac_ext '"$xe_libs"' 1>&AS_MESSAGE_LOG_FD'
270         AC_LINK_IFELSE([AC_LANG_SOURCE([[int x=0;]])],
271                 [cc_produces_so=yes], [cc_produces_so=no])
272         SXE_RESTORE_LIBS
273 dnl     ac_link='${CC-cc} -o conftest $CFLAGS $CPPFLAGS $LDFLAGS '"$xe_cppflags $xe_ldflags"' conftest.$ac_ext '"$xe_libs"' 1>&AS_MESSAGE_LOG_FD'
274         :
275 else
276         cc_produces_so=no
277 fi
278 AC_MSG_RESULT([${cc_produces_so}])
279
280 LTLD=$LD
281 if test -z "$LTLD"; then
282   ac_prog=ld
283   if test "$XEGCC" = yes; then
284     # Check if gcc -print-prog-name=ld gives a path.
285     AC_MSG_CHECKING([for ld used by GCC])
286     ac_prog=`($CC -print-prog-name=ld) 2>&5`
287     case "$ac_prog" in
288     # Accept absolute paths.
289     /*)
290       if test -z "$LTLD"; then
291 #        case "$ac_prog" in
292 #          *gcc-lib*) LTLD="$CC"
293 #                     ;;
294 #          *)
295          LTLD="$ac_prog"
296 #                     ;;
297 #        esac
298       fi
299       ;;
300     "")
301       # If it fails, then pretend we aren't using GCC.
302       ac_prog=ld
303       ;;
304     *)
305       # If it is relative, then search for the first ld in PATH.
306       with_gnu_ld=unknown
307       ;;
308     esac
309   else
310     AC_MSG_CHECKING([for GNU ld])
311   fi
312
313   if test -z "$LTLD"; then
314     IFS="${IFS=         }"; ac_save_ifs="$IFS"; IFS="${IFS}:"
315     for ac_dir in $PATH; do
316       test -z "$ac_dir" && ac_dir=.
317       if test -f "$ac_dir/$ac_prog"; then
318         LTLD="$ac_dir/$ac_prog"
319         # Check to see if the program is GNU ld.  I'd rather use --version,
320         # but apparently some GNU ld's only accept -v.
321         # Break only if it was the GNU/non-GNU ld that we prefer.
322         if "$LTLD" -v 2>&1 < /dev/null | egrep '(GNU|with BFD)' > /dev/null; then
323           xe_gnu_ld=yes
324         else
325           xe_gnu_ld=no
326         fi
327       fi
328     done
329     IFS="$ac_save_ifs"
330   fi
331
332   if test -n "$LTLD"; then
333     AC_MSG_RESULT([${LTLD}])
334   else
335     AC_MSG_RESULT(no)
336   fi
337
338   if test -z "$LTLD" -a "$cc_produces_so" = no; then
339     AC_MSG_ERROR(no acceptable linker found in \$PATH)
340     exit 1
341   fi
342 fi
343
344 dnl
345 dnl Order of the tests changed somewhat to prevent repetition
346 dnl
347 ld_dynamic_link_flags=
348
349 # Check to see if it really is or isn't GNU ld.
350 AC_MSG_CHECKING([if the linker is GNU ld])
351 # I'd rather use --version here, but apparently some GNU ld's only accept -v.
352 if $LTLD -v 2>&1 </dev/null | egrep '(GNU|with BFD)' 1>&5; then
353   xe_gnu_ld=yes
354 else
355   xe_gnu_ld=no
356 fi
357 AC_MSG_RESULT([${xe_gnu_ld}])
358
359 case "$xehost_os" in
360   amigaos* | sunos4*)
361     # On these operating systems, we should treat GNU ld like the system ld.
362     gnu_ld_acts_native=yes
363     ;;
364   *)
365     gnu_ld_acts_native=no
366     ;;
367 esac
368
369 if test "$cc_produces_so" = "yes"; then
370   dll_ld=$CC
371   case "$xehost_os" in
372     *darwin*)
373       dnl On Darwin, we test with xcldf, but we use xldf
374       dll_ldflags=$xldf 
375       ;;
376     *) 
377       dll_ldflags=$xcldf
378       ;;
379   esac
380   can_build_shared=yes
381   ld_shlibs=yes
382 else
383   # OK - only NOW do we futz about with ld.
384   # See if the linker supports building shared libraries.
385   AC_MSG_CHECKING([whether the linker supports shared libraries])
386   dll_ld=$CC
387   dll_ldflags=$LDFLAGS
388   ld_shlibs=yes
389   can_build_shared=yes
390   if test "$xe_gnu_ld" = yes && test "$gnu_ld_acts_native" != yes; then
391     # See if GNU ld supports shared libraries.
392     if $LTLD --help 2>&1 | egrep ': supported targets:.* elf' > /dev/null; then
393       dll_ld=$CC
394       dll_ldflags="-shared"
395       ld_shlibs=yes
396     else
397       ld_shlibs=no
398     fi
399   else
400     # PORTME fill in a description of your system's linker (not GNU ld)
401     case "$xehost_os" in
402     aix3*)
403       dll_ld=$LTLD
404       dll_ldflags=$xldf
405       ;;
406
407     aix[[4-9]]*)
408       dll_ldflags=$xcldf
409       ;;
410
411     # FreeBSD 2.2.[012] allows us to include c++rt0.o to get C++ constructor
412     # support.  Future versions do this automatically, but an explicit c++rt0.o
413     # doesn't break anything, and helps significantly (at the cost of a little
414     # extra space).
415     freebsd2.2*)
416       dll_ld=$LTLD
417       dll_ldflags=$xldf
418       dll_post="/usr/lib/c++rt0.o"
419       ;;
420
421     # Unfortunately, older versions of FreeBSD 2 don't have this feature.
422     freebsd2*)
423       dll_ld=$LTLD
424       dll_ldflags="-Bshareable"
425       ;;
426
427     # FreeBSD 3, at last, uses gcc -shared to do shared libraries.
428     freebsd3*)
429       dll_ldflags="-shared"
430       ;;
431
432     hpux*)
433       dll_ld=$LTLD
434       dll_ldflags=$xldf
435       ;;
436
437     irix[[5-9]]*)
438       dll_ld=$LTLD
439       dll_ldflags=$xldf
440       ;;
441
442     netbsd*)
443       # Tested with NetBSD 1.2 ld
444       dll_ld=$LTLD
445       dll_ldflags=$xldf
446       ;;
447
448     openbsd*)
449       dll_ld=$LTLD
450       dll_ldflags=$xldf
451       ;;
452
453     osf3* | osf4*)
454       dll_ld=$LTLD
455       dll_ldflags=$xldf
456       ;;
457
458     # For both SCO and Solaris we MAY want to have LDFLAGS include -z text
459     sco3.2v5* | unixware* | sysv5* | sysv4* | solaris2* | solaris7*)
460       dll_ld=$LTLD
461       case "$dll_ld" in
462         *gcc*) dll_ldflags="-shared"
463                dll_ld=$CC
464                ;;
465         *)     dll_ldflags="-G"
466                ;;
467       esac
468       ;;
469
470     sunos4*)
471       if test "$XEGCC" = yes; then
472         dll_ld=$CC
473       else
474         dll_ld=$LTLD
475       fi
476       dll_ldflags=$xldf
477       ;;
478
479     uts4*)
480       dll_ld=$LTLD
481       dll_ldflags="-G"
482       ;;
483
484     bsdi*)
485       dll_ldflags="-r"
486       dll_ld="shlicc2"
487       ;;
488
489     *)
490       ld_shlibs=no
491       can_build_shared=no
492       ;;
493     esac
494   fi
495   AC_MSG_RESULT([${ld_shlibs}])
496   if test "$ld_shlibs" = "no"; then
497     can_build_shared=no
498   fi
499 fi # End of if cc_produces_so = no
500
501 dnl
502 dnl Last thing, check how to get a linked executable to have its symbols
503 dnl exported, so that the modules have access to them.
504 dnl
505 dnl XEmacs FIXME - we need to set ld_dynamic_link_flags properly for
506 dnl most of these systems, which was missing from libtool. I know they
507 dnl all have a way of doing this, but someone needs to look at this
508 dnl for each OS and make sure it is correct. Remember that the arguments
509 dnl are passed when temacs is linked, this is NOT for modules. The sole
510 dnl purpose of the argument is to get the internal XEmacs symbols exposed
511 dnl for modules to use. This means that the COMPILER (and NOT the linker)
512 dnl is most often used to create temacs, so arguments to the linker will
513 dnl usually need to be prefix with ${wl} or some other such thing.
514 dnl
515
516 if test "$xe_gnu_ld" = yes; then
517   if test "$ld_shlibs" = yes; then
518     ld_dynamic_link_flags="${wl}-export-dynamic"
519   fi
520 fi
521
522 if test -z "$ld_dynamic_link_flags"; then
523   case "$xehost_os" in
524   aix[[3-9]]*)
525     ld_dynamic_link_flags=
526     ;;
527
528   *darwin*)
529     ld_dynamic_link_flags=
530     ;;
531
532   freebsd2.2*)
533     ld_dynamic_link_flags=
534     ;;
535
536   freebsd2*)
537     ld_dynamic_link_flags=
538     ;;
539
540   freebsd3*)
541     ld_dynamic_link_flags=
542     ;;
543
544   hpux*)
545     ld_dynamic_link_flags="${wl}-E"
546     ;;
547
548   irix[[5-9]]*)
549     ld_dynamic_link_flags=
550     ;;
551
552   netbsd*)
553     ld_dynamic_link_flags=
554     ;;
555
556   openbsd*)
557     ld_dynamic_link_flags=
558     ;;
559
560   osf3* | osf4*)
561     ld_dynamic_link_flags=
562     ;;
563
564   solaris2* | solaris7*)
565     ld_dynamic_link_flags=
566     ;;
567
568   sco3.2v5* | unixware* | sysv5* | sysv4*)
569     ld_dynamic_link_flags="${wl}-Bexport"
570     ;;
571
572   sunos4*)
573     ld_dynamic_link_flags=
574     ;;
575
576   uts4*)
577     ld_dynamic_link_flags=
578     ;;
579
580   bsdi*)
581     ld_dynamic_link_flags=
582     ;;
583
584   esac
585 fi # End of if -z ld_dynamic_link_flags
586 fi # End of if test "$can_build_shared" = "yes"
587
588 AC_SUBST(dll_ld)
589 AC_SUBST(dll_cflags)
590 AC_SUBST(dll_ldflags)
591 AC_SUBST(dll_post)
592 AC_SUBST(dll_ldo)
593 AC_SUBST(ld_dynamic_link_flags)
594 ])dnl SXE_SHLIB_STUFF
595
596
597 AC_DEFUN([SXE_CHECK_LD_ZFLAG], [dnl
598         pushdef([LD_ZFLAG], [$1])
599         pushdef([cv_zflag], [sxe_cv_ld__z_]translit(LD_ZFLAG,[-.=],[___]))
600
601         AC_CACHE_CHECK([whether linker supports -z ]LD_ZFLAG[],
602                 []cv_zflag[], [_SXE_CHECK_LD_ZFLAG(LD_ZFLAG)])
603
604         popdef([cv_zflag])
605         popdef([LD_ZFLAG])
606 ])dnl SXE_CHECK_LD_ZFLAG
607
608 AC_DEFUN([_SXE_CHECK_LD_ZFLAG], [dnl
609         ## arg1 is the flag to check for
610         pushdef([LD_ZFLAG], [$1])
611         pushdef([cv_zflag], [sxe_cv_ld__z_]translit(LD_ZFLAG,[-.=],[___]))
612
613         if test "$GCC" = "yes"; then
614                 if test "($CC -Xlinker --help 2>&1 | \
615                         grep \"-z []LD_ZFLAG[]\" > /dev/null 2>&1 ) "; then
616                         cv_zflag="yes"
617                 else
618                         cv_zflag="no"
619                 fi
620         elif test -n "$LD"; then
621                 if test "($LD --help 2>&1 | \
622                         grep \"-z []LD_ZFLAG[]\" > /dev/null 2>&1 )"; then
623                         cv_zflag="yes"
624                 else
625                         cv_zflag="no"
626                 fi
627         else
628                 cv_zflag="no"
629         fi
630
631         popdef([cv_zflag])
632         popdef([LD_ZFLAG])
633 ])dnl _SXE_CHECK_LD_ZFLAG
634
635 AC_DEFUN([SXE_CHECK_LD_NOCOMBRELOC], [dnl
636         SXE_CHECK_LD_ZFLAG([nocombreloc])
637 ])dnl SXE_CHECK_LD_NOCOMBRELOC
638
639
640 AC_DEFUN([SXE_CHECK_LINKER_FLAGS], [dnl
641         ## relocation
642         SXE_CHECK_LD_ZFLAG([combreloc])
643         SXE_CHECK_LD_ZFLAG([nocombreloc])
644         ## symbols
645         SXE_CHECK_LD_ZFLAG([defs])
646         SXE_CHECK_LD_ZFLAG([muldefs])
647         ## search paths
648         SXE_CHECK_LD_ZFLAG([nodefaultlib])
649         ## binding policy
650         SXE_CHECK_LD_ZFLAG([lazy])
651         SXE_CHECK_LD_ZFLAG([now])
652 ])dnl SXE_CHECK_LINKER_FLAGS
653
654 AC_DEFUN([SXE_PREPEND_LINKER_FLAG], [dnl
655         ## a convenience function to add such linker flags to variables
656         ## arg1 is the flag to add (must include -z if applicable)
657         ## arg2 is the variable whither to prepend
658         pushdef([FLAG], [$1])
659         pushdef([__FLAG], [-Wl,]patsubst([$1], [ ], [[,]]))
660         pushdef([VAR], [$2])
661
662         []VAR[]="[]__FLAG[] $[]VAR[]"
663         if test "$extra_verbose" = "yes"; then
664                 echo "    Prepending linker flag \"[]__FLAG[]\" to \$[]VAR[]"
665         fi
666
667         popdef([VAR])
668         popdef([__FLAG])
669         popdef([FLAG])
670 ])dnl SXE_PREPEND_LINKER_FLAG
671
672 dnl sxe-linker.m4 ends here