r19507: Merge my DSO fixes branch. Building Samba's libraries as shared libraries
[nivanova/samba-autobuild/.git] / source4 / 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(SHLIBEXT)
23 AC_SUBST(SONAMEFLAG)
24 AC_SUBST(PICFLAG)
25
26 # Assume non-shared by default and override below
27 # these are the defaults, good for lots of systems
28 STLD=${PROG_AR}
29 STLD_FLAGS="-rcs"
30 BLDSHARED="false"
31 LD="${CC}"
32 LDFLAGS=""
33 SHLD="${CC}"
34 SHLD_FLAGS="-shared"
35 SHLIBEXT="so"
36 SONAMEFLAG=""
37 PICFLAG=""
38
39 AC_MSG_CHECKING([ability to build shared libraries])
40
41 # and these are for particular systems
42 case "$host_os" in
43         *linux*)
44                 BLDSHARED="true"
45                 SHLD_FLAGS="-shared -Wl,-Bsymbolic -Wl,--allow-shlib-undefined" 
46                 LDFLAGS="-Wl,--export-dynamic"
47                 PICFLAG="-fPIC"
48                 SONAMEFLAG="-Wl,-soname="
49                 ;;
50         *solaris*)
51                 BLDSHARED="true"
52                 SHLD_FLAGS="-G"
53                 SONAMEFLAG="-h "
54                 if test "${GCC}" = "yes"; then
55                         PICFLAG="-fPIC"
56                         SONAMEFLAG="-Wl,-soname="
57                         if test "${ac_cv_prog_gnu_ld}" = "yes"; then
58                                 LDFLAGS="-Wl,-E"
59                         fi
60                 else
61                         PICFLAG="-KPIC"
62                         ## ${CFLAGS} added for building 64-bit shared 
63                         ## libs using Sun's Compiler
64                         SHLD_FLAGS="-G \${CFLAGS}"
65                 fi
66                 ;;
67         *sunos*)
68                 BLDSHARED="true"
69                 SHLD_FLAGS="-G"
70                 SONAMEFLAG="-Wl,-h,"
71                 PICFLAG="-KPIC"   # Is this correct for SunOS
72                 ;;
73         *netbsd* | *freebsd* | *dragonfly* )  
74                 BLDSHARED="true"
75                 LDFLAGS="-Wl,--export-dynamic"
76                 SONAMEFLAG="-Wl,-soname,"
77                 PICFLAG="-fPIC -DPIC"
78                 ;;
79         *openbsd*)
80                 BLDSHARED="true"
81                 LDFLAGS="-Wl,-Bdynamic"
82                 SONAMEFLAG="-Wl,-soname,"
83                 PICFLAG="-fPIC"
84                 ;;
85         *irix*)
86                 BLDSHARED="true"
87                 SHLD_FLAGS="-set_version sgi1.0 -shared"
88                 SONAMEFLAG="-soname "
89                 SHLD="${PROG_LD}"
90                 if test "${GCC}" = "yes"; then
91                         PICFLAG="-fPIC"
92                 else 
93                         PICFLAG="-KPIC"
94                 fi
95                 ;;
96         *aix*)
97                 BLDSHARED="true"
98                 SHLD_FLAGS="-Wl,-G,-bexpall,-bbigtoc"
99                 LDFLAGS="-Wl,-brtl,-bexpall,-bbigtoc"
100                 # as AIX code is always position independent...
101                 PICFLAG="-O2"
102                 ;;
103         *hpux*)
104                 # Use special PIC flags for the native HP-UX compiler.
105                 if test $ac_cv_prog_cc_Ae = yes; then
106                         BLDSHARED="true"
107                         SHLD_FLAGS="-b -Wl,-B,symbolic,-b,-z"
108                         SONAMEFLAG="-Wl,+h "
109                         PICFLAG="+z"
110                 elif test "${GCC}" = "yes"; then
111                         BLDSHARED="true" # I hope this is correct
112                         PICFLAG="-fPIC"
113                 fi
114                 if test "$host_cpu" = "ia64"; then
115                         SHLIBEXT="so"
116                         LDFLAGS="-Wl,-E,+b/usr/local/lib/hpux32:/usr/lib/hpux32"
117                 else
118                         SHLIBEXT="sl"
119                         LDFLAGS="-Wl,-E,+b/usr/local/lib:/usr/lib"
120                 fi
121                 ;;
122         *osf*)
123                 BLDSHARED="true"
124                 SONAMEFLAG="-Wl,-soname,"
125                 PICFLAG="-fPIC"
126                 ;;
127         *unixware*)
128                 BLDSHARED="true"
129                 SONAMEFLAG="-Wl,-soname,"
130                 PICFLAG="-KPIC"
131                 ;;
132         *darwin*)
133                 BLDSHARED="true"
134                 SHLD_FLAGS="-bundle -flat_namespace -undefined suppress"
135                 SHLIBEXT="dylib"
136                 ;;
137 esac
138
139 AC_MSG_RESULT($BLDSHARED)
140
141 AC_MSG_CHECKING([LD])
142 AC_MSG_RESULT([$LD])
143 AC_MSG_CHECKING([LDFLAGS])
144 AC_MSG_RESULT([$LDFLAGS])
145
146 AC_MSG_CHECKING([STLD])
147 AC_MSG_RESULT([$STLD])
148 AC_MSG_CHECKING([STLD_FLAGS])
149 AC_MSG_RESULT([$STLD_FLAGS])
150
151 #######################################################
152 # test whether building a shared library actually works
153 if test $BLDSHARED = true; then
154
155 AC_MSG_CHECKING([SHLD])
156 AC_MSG_RESULT([$SHLD])
157 AC_MSG_CHECKING([SHLD_FLAGS])
158 AC_MSG_RESULT([$SHLD_FLAGS])
159
160 AC_DEFINE_UNQUOTED(SHLIBEXT, "$SHLIBEXT", [Shared library extension])
161 AC_MSG_CHECKING([SHLIBEXT])
162 AC_MSG_RESULT([$SHLIBEXT])
163 AC_MSG_CHECKING([SONAMEFLAG])
164 AC_MSG_RESULT([$SONAMEFLAG])
165
166 AC_MSG_CHECKING([PICFLAG])
167 AC_MSG_RESULT([$PICFLAG])
168
169 AC_CACHE_CHECK([whether building shared libraries actually works], 
170                [ac_cv_shlib_works],[
171    ac_cv_shlib_works=no
172    # try building a trivial shared library
173    ${CC} ${CFLAGS} ${PICFLAG} -c ${srcdir-.}/build/tests/shlib.c -o shlib.o &&
174        ${SHLD} `eval echo ${SHLD_FLAGS} ` -o shlib.${SHLIBEXT} shlib.o &&
175        ac_cv_shlib_works=yes
176    rm -f shlib.${SHLIBEXT} shlib.o
177 ])
178 if test $ac_cv_shlib_works = no; then
179    BLDSHARED=false
180 fi
181 fi
182
183 AC_MSG_CHECKING([if we can link using the selected flags])
184 AC_TRY_RUN([#include "${srcdir-.}/build/tests/trivial.c"],
185             AC_MSG_RESULT(yes),
186             AC_MSG_ERROR([we cannot link with the selected cc and ld flags. Aborting configure]),
187             AC_MSG_WARN([cannot run when cross-compiling]))
188
189 AC_ARG_ENABLE(dso,
190 [  --enable-dso                 Enable building internal libraries as DSO's (experimental)],
191 [ if test x$enable_dso != xyes; then
192         BLDSHARED=false
193   fi], 
194 [BLDSHARED=false])