2d2b53e948080cd6f6e2bc4bf474cd83ebc5816d
[jelmer/samba4-debian.git] / source / build / m4 / check_ld.m4
1 dnl SMB Build Environment LD Checks
2 dnl -------------------------------------------------------
3 dnl  Copyright (C) Stefan (metze) Metzmacher 2004
4 dnl  Copyright (C) Jelmer Vernooij 2006
5 dnl  Released under the GNU GPL
6 dnl -------------------------------------------------------
7 dnl
8
9 AC_PATH_PROG(PROG_LD,ld)
10 LD=${PROG_LD}
11 AC_PROG_LD_GNU
12 LD=""
13 AC_PATH_PROG(PROG_AR, ar)
14
15 AC_SUBST(STLD)
16 AC_SUBST(STLD_FLAGS)
17 AC_SUBST(BLDSHARED)
18 AC_SUBST(LD)
19 AC_SUBST(LDFLAGS)
20 AC_SUBST(SHLD)
21 AC_SUBST(SHLD_FLAGS)
22 AC_SUBST(SHLD_UNDEF_FLAGS)
23 AC_SUBST(SHLIBEXT)
24 AC_SUBST(SONAMEFLAG)
25 AC_SUBST(PICFLAG)
26
27 # Assume non-shared by default and override below
28 # these are the defaults, good for lots of systems
29 STLD=${PROG_AR}
30 STLD_FLAGS="-rcs"
31 BLDSHARED="false"
32 LD="${CC}"
33 SHLD="${CC}"
34 SHLD_FLAGS="-shared"
35 SHLIBEXT="so"
36 SONAMEFLAG=""
37 PICFLAG=""
38
39 # allow for --with-hostld=gcc
40 AC_ARG_WITH(hostld,[  --with-hostld=linker    choose host linker],
41 [HOSTLD=$withval],
42 [
43 if test z"$cross_compiling" = "yes"; then
44         HOSTLD='$(HOSTCC)'
45 else
46         HOSTLD='$(LD)'
47 fi
48 ])
49
50 AC_MSG_CHECKING([whether to try to build shared libraries on $host_os])
51
52 # and these are for particular systems
53 case "$host_os" in
54         *linux*)
55                 BLDSHARED="true"
56                 SHLD_FLAGS="-shared -Wl,-Bsymbolic"
57                 SHLD_UNDEF_FLAGS="-Wl,--allow-shlib-undefined"
58                 LDFLAGS="$LDFLAGS -Wl,--export-dynamic"
59                 SONAMEFLAG="-Wl,-soname="
60                 ;;
61         *solaris*)
62                 BLDSHARED="true"
63                 SHLD_FLAGS="-G"
64                 SONAMEFLAG="-h "
65                 if test "${GCC}" = "yes"; then
66                         SONAMEFLAG="-Wl,-soname="
67                         if test "${ac_cv_prog_gnu_ld}" = "yes"; then
68                                 LDFLAGS="$LDFLAGS -Wl,-E"
69                         fi
70                 else
71                         ## ${CFLAGS} added for building 64-bit shared 
72                         ## libs using Sun's Compiler
73                         SHLD_FLAGS="-G \${CFLAGS}"
74                 fi
75                 ;;
76         *sunos*)
77                 BLDSHARED="true"
78                 SHLD_FLAGS="-G"
79                 SONAMEFLAG="-Wl,-h,"
80                 ;;
81         *netbsd* | *freebsd* | *dragonfly* )  
82                 BLDSHARED="true"
83                 LDFLAGS="$LDFLAGS -Wl,--export-dynamic"
84                 SONAMEFLAG="-Wl,-soname,"
85                 ;;
86         *openbsd*)
87                 # disabled because us4 fails to link libtorture.so
88                 BLDSHARED="false"
89                 LDFLAGS="$LDFLAGS -Wl,-Bdynamic"
90                 SONAMEFLAG="-Wl,-soname,"
91                 ;;
92         *irix*)
93                 # disabled because us4 fails to link libtorture.so
94                 BLDSHARED="false"
95                 SHLD_FLAGS="-set_version sgi1.0 -shared"
96                 SONAMEFLAG="-soname "
97                 SHLD="${PROG_LD}"
98                 ;;
99         *aix*)
100                 # disabled because us4 fails to link libtorture.so
101                 BLDSHARED="false"
102                 SHLD_FLAGS="-Wl,-G,-bexpall,-bbigtoc"
103                 LDFLAGS="$LDFLAGS -Wl,-brtl,-bexpall,-bbigtoc"
104                 ;;
105         *hpux*)
106                 # Use special PIC flags for the native HP-UX compiler.
107                 if test $ac_cv_prog_cc_Ae = yes; then
108                         BLDSHARED="true"
109                         SHLD_FLAGS="-b -Wl,-B,symbolic,-b,-z"
110                         SONAMEFLAG="-Wl,+h "
111                 elif test "${GCC}" = "yes"; then
112                         BLDSHARED="true" # I hope this is correct
113                 fi
114                 if test "$host_cpu" = "ia64"; then
115                         SHLIBEXT="so"
116                         LDFLAGS="$LDFLAGS -Wl,-E,+b/usr/local/lib/hpux32:/usr/lib/hpux32"
117                 else
118                         SHLIBEXT="sl"
119                         LDFLAGS="$LDFLAGS -Wl,-E,+b/usr/local/lib:/usr/lib"
120                 fi
121                 ;;
122         *osf*)
123                 # disabled because tru64 fails to link libtorture.so
124                 BLDSHARED="false"
125                 SONAMEFLAG="-Wl,-soname,"
126                 ;;
127         *unixware*)
128                 BLDSHARED="true"
129                 SONAMEFLAG="-Wl,-soname,"
130                 ;;
131         *darwin*)
132                 BLDSHARED="true"
133                 SHLD_FLAGS="-bundle -flat_namespace -undefined suppress"
134                 SHLIBEXT="dylib"
135                 ;;
136 esac
137
138 AC_MSG_RESULT($BLDSHARED)
139
140 AC_MSG_CHECKING([LD])
141 AC_MSG_RESULT([$LD])
142 AC_MSG_CHECKING([LDFLAGS])
143 AC_MSG_RESULT([$LDFLAGS])
144
145 AC_SUBST(HOSTLD)
146
147 AC_MSG_CHECKING([STLD])
148 AC_MSG_RESULT([$STLD])
149 AC_MSG_CHECKING([STLD_FLAGS])
150 AC_MSG_RESULT([$STLD_FLAGS])
151
152 AC_LD_PICFLAG
153
154 AC_ARG_ENABLE(shared,
155 [  --disable-shared        Disable testing for building shared libraries],
156 [],[enable_shared=yes])
157
158 if test x"$enable_shared" = x"no" -o x"$enable_shared" = x"false"; then
159         BLDSHARED=false
160 fi
161
162 #######################################################
163 # test whether building a shared library actually works
164 if test $BLDSHARED = true; then
165
166         AC_MSG_CHECKING([SHLD])
167         AC_MSG_RESULT([$SHLD])
168         AC_MSG_CHECKING([SHLD_FLAGS])
169         AC_MSG_RESULT([$SHLD_FLAGS])
170
171         AC_MSG_CHECKING([SHLIBEXT])
172         AC_MSG_RESULT([$SHLIBEXT])
173         AC_MSG_CHECKING([SONAMEFLAG])
174         AC_MSG_RESULT([$SONAMEFLAG])
175
176         AC_MSG_CHECKING([PICFLAG])
177         AC_MSG_RESULT([$PICFLAG])
178
179         AC_CACHE_CHECK([whether building shared libraries actually works], 
180                        [ac_cv_shlib_works],[
181                         ac_cv_shlib_works=no
182                         # try building a trivial shared library
183                         ${CC} ${CFLAGS} ${PICFLAG} -c ${srcdir-.}/build/tests/shlib.c -o shlib.o &&
184                                 ${SHLD} `eval echo ${SHLD_FLAGS} ` -o shlib.${SHLIBEXT} shlib.o && 
185                                 ac_cv_shlib_works=yes
186                         rm -f shlib.${SHLIBEXT} shlib.o
187         ])
188         if test $ac_cv_shlib_works = no; then
189                 BLDSHARED=false
190         fi
191 fi
192
193 if test $BLDSHARED != true; then
194         SHLD="shared-libraries-disabled"
195         SHLD_FLAGS="shared-libraries-disabled"
196         SHLIBEXT="shared_libraries_disabled"
197         SONAMEFLAG="shared-libraries-disabled"
198         PICFLAG=""
199         AC_MSG_CHECKING([SHLD])
200         AC_MSG_RESULT([$SHLD])
201         AC_MSG_CHECKING([SHLD_FLAGS])
202         AC_MSG_RESULT([$SHLD_FLAGS])
203
204         AC_MSG_CHECKING([SHLIBEXT])
205         AC_MSG_RESULT([$SHLIBEXT])
206         AC_MSG_CHECKING([SONAMEFLAG])
207         AC_MSG_RESULT([$SONAMEFLAG])
208
209         AC_MSG_CHECKING([PICFLAG])
210         AC_MSG_RESULT([$PICFLAG])
211 fi
212
213 AC_DEFINE_UNQUOTED(SHLIBEXT, "$SHLIBEXT", [Shared library extension])
214
215 AC_MSG_CHECKING([if we can link using the selected flags])
216 AC_TRY_RUN([#include "${srcdir-.}/build/tests/trivial.c"],
217             AC_MSG_RESULT(yes),
218             AC_MSG_ERROR([we cannot link with the selected cc and ld flags. Aborting configure]),
219             AC_MSG_WARN([cannot run when cross-compiling]))
220
221
222 USESHARED=false
223 AC_SUBST(USESHARED)
224
225 AC_ARG_ENABLE(dso,
226 [  --enable-dso                   Enable using shared libraries internally (experimental)],
227 [],[enable_dso=no])
228
229 if test x"$enable_dso" = x"yes" -a x"$BLDSHARED" != x"true"; then
230         AC_MSG_ERROR([--enable-dso: no support for shared libraries])
231 fi
232
233 if test x"$enable_dso" != x"no"; then
234         USESHARED=$BLDSHARED
235 fi
236
237 AC_MSG_CHECKING([if binaries will use shared libraries])
238 AC_MSG_RESULT([$USESHARED])