1dnl                                            -*- Autoconf -*-
2dnl Process this file with autoconf to produce a configure script.
3dnl
4dnl   Copyright (C) 2019-2022 Free Software Foundation, Inc.
5dnl
6dnl This file is free software; you can redistribute it and/or modify
7dnl it under the terms of the GNU General Public License as published by
8dnl the Free Software Foundation; either version 2 of the License, or
9dnl (at your option) any later version.
10dnl
11dnl This program is distributed in the hope that it will be useful,
12dnl but WITHOUT ANY WARRANTY; without even the implied warranty of
13dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14dnl GNU General Public License for more details.
15dnl
16dnl You should have received a copy of the GNU General Public License
17dnl along with this program; see the file COPYING.  If not see
18dnl <http://www.gnu.org/licenses/>.
19dnl
20
21AC_PREREQ(2.64)
22AC_INIT([libctf], 1.2.0)
23AC_CONFIG_SRCDIR(ctf-impl.h)
24AC_CANONICAL_BUILD
25AC_CANONICAL_HOST
26AC_CANONICAL_TARGET
27AC_USE_SYSTEM_EXTENSIONS
28AM_INIT_AUTOMAKE
29AM_SILENT_RULES([yes])
30
31# Checks for programs.
32AC_PROG_MAKE_SET
33AC_PROG_CC
34AC_PROG_RANLIB
35AM_PROG_AR
36ZW_GNU_GETTEXT_SISTER_DIR
37
38dnl Default to a non shared library.  This may be overridden by the
39dnl configure option --enable-shared.
40AC_DISABLE_SHARED
41
42LT_INIT
43AC_SYS_LARGEFILE
44LT_OUTPUT
45
46MISSING=`cd $ac_aux_dir && ${PWDCMD-pwd}`/missing
47AC_CHECK_PROGS([ACLOCAL], [aclocal], [$MISSING aclocal])
48AC_CHECK_PROGS([AUTOCONF], [autoconf], [$MISSING autoconf])
49AC_CHECK_PROGS([AUTOHEADER], [autoheader], [$MISSING autoheader])
50
51# Figure out what compiler warnings we can enable.
52# See config/warnings.m4 for details.
53
54ACX_PROG_CC_WARNING_OPTS([-W -Wall -Wno-narrowing -Wwrite-strings \
55			  -Wmissing-format-attribute], [warn])
56ACX_PROG_CC_WARNING_OPTS([-Wstrict-prototypes -Wmissing-prototypes \
57			  -Wold-style-definition], [c_warn])
58ACX_PROG_CC_WARNING_ALMOST_PEDANTIC([-Wno-long-long])
59
60# Only enable with --enable-werror-always until existing warnings are
61# corrected.
62ACX_PROG_CC_WARNINGS_ARE_ERRORS([manual])
63
64AM_MAINTAINER_MODE
65AM_INSTALL_LIBBFD
66ACX_PROG_CC_WARNING_OPTS([-Wall], [ac_libctf_warn_cflags])
67
68AC_FUNC_MMAP
69# Needed for BFD capability checks.
70AC_SEARCH_LIBS(dlsym, dl)
71AM_ZLIB
72
73GCC_ENABLE([libctf-hash-debugging], [no], [], [Enable expensive debugging of CTF deduplication type hashing])
74if test "${enable_libctf_hash_debugging}" = yes; then
75    AC_DEFINE(ENABLE_LIBCTF_HASH_DEBUGGING, 1, [Enable expensive debugging of CTF deduplication type hashing])
76fi
77AM_CONDITIONAL(ENABLE_LIBCTF_HASH_DEBUGGING, test "${enable_libctf_hash_debugging}" = yes)
78
79# Similar to GDB_AC_CHECK_BFD.
80OLD_CFLAGS=$CFLAGS
81OLD_LDFLAGS=$LDFLAGS
82OLD_LIBS=$LIBS
83OLD_CC=$CC
84# Put the old CFLAGS/LDFLAGS last, in case the user's (C|LD)FLAGS
85# points somewhere with bfd, with -I/foo/lib and -L/foo/lib.  We
86# always want our bfd.
87CC="./libtool --quiet --mode=link $OLD_CC"
88CFLAGS="-I${srcdir}/../include -I../bfd -I${srcdir}/../bfd $CFLAGS"
89LDFLAGS="-L../bfd -L../libiberty $LDFLAGS"
90intl=`echo $LIBINTL | sed 's,${top_builddir}/,,g'`
91LIBS="-lbfd -liberty $intl $LIBS"
92AC_CACHE_CHECK([for ELF support in BFD], ac_cv_libctf_bfd_elf,
93[AC_TRY_LINK([#include <stdlib.h>
94	     #include <string.h>
95	     #include "bfd.h"
96	     #include "elf-bfd.h"],
97	    [(void) bfd_section_from_elf_index (NULL, 0);
98	     return 0;],
99	    [ac_cv_libctf_bfd_elf=yes],
100	    [ac_cv_libctf_bfd_elf=no])])
101CFLAGS=$OLD_CFLAGS
102LDFLAGS=$OLD_LDFLAGS
103LIBS=$OLD_LIBS
104CC=$OLD_CC
105
106if test $ac_cv_libctf_bfd_elf = yes; then
107  AC_DEFINE([HAVE_BFD_ELF], 1,
108	    [Whether libbfd was configured for an ELF target.])
109fi
110
111AC_C_BIGENDIAN
112AC_CHECK_HEADERS(byteswap.h endian.h)
113AC_CHECK_FUNCS(pread)
114
115dnl Check for bswap_{16,32,64}
116AC_CHECK_DECLS([bswap_16, bswap_32, bswap_64], [], [], [[#include <byteswap.h>]])
117AC_CHECK_DECLS([asprintf, vasprintf, stpcpy])
118
119dnl Check for qsort_r.  (Taken from gnulib.)
120AC_CHECK_FUNCS_ONCE([qsort_r])
121if test $ac_cv_func_qsort_r = yes; then
122  AC_CACHE_CHECK([for qsort_r signature], [ac_cv_libctf_qsort_r_signature],
123    [AC_LINK_IFELSE(
124       [AC_LANG_PROGRAM([[#undef qsort_r
125                          #include <stdlib.h>
126                          void qsort_r (void *, size_t, size_t,
127                                        int (*) (void const *, void const *,
128                                                 void *),
129                                        void *);
130                          void (*p) (void *, size_t, size_t,
131                                     int (*) (void const *, void const *,
132                                              void *),
133                                     void *) = qsort_r;
134                        ]])],
135       [ac_cv_libctf_qsort_r_signature=GNU],
136       [AC_LINK_IFELSE(
137          [AC_LANG_PROGRAM([[#undef qsort_r
138	                     #include <stdlib.h>
139                             void qsort_r (void *, size_t, size_t, void *,
140                                           int (*) (void *,
141                                                    void const *,
142                                                    void const *));
143                             void (*p) (void *, size_t, size_t, void *,
144                                        int (*) (void *, void const *,
145                                                 void const *)) = qsort_r;
146                           ]])],
147          [ac_cv_libctf_qsort_r_signature=BSD],
148          [ac_cv_libctf_qsort_r_signature=unknown])])])
149fi
150
151case x$ac_cv_libctf_qsort_r_signature in
152  xGNU)     AC_DEFINE([HAVE_QSORT_R_ARG_LAST], 1,
153	     [Whether a qsort_r exists with a void *arg as its last arg.]);;
154  xBSD)     AC_DEFINE([HAVE_QSORT_R_COMPAR_LAST], 1,
155	     [Whether a qsort_r exists with the compar function as its last arg.]);;
156  *) ac_cv_libctf_qsort_r_signature=unknown;;
157esac
158
159AM_CONDITIONAL(NEED_CTF_QSORT_R, test "${ac_cv_libctf_qsort_r_signature}" = unknown)
160
161AC_CACHE_CHECK([for O_CLOEXEC], [ac_cv_libctf_macro_O_CLOEXEC],
162  [AC_LINK_IFELSE(
163    [AC_LANG_PROGRAM([[#include <fcntl.h>
164                       #ifndef O_CLOEXEC
165                         choke me;
166                       #endif
167                     ]],
168                     [[return O_CLOEXEC;]])],
169    [ac_cv_libctf_macro_O_CLOEXEC=yes],
170    [ac_cv_libctf_macro_O_CLOEXEC=no])])
171
172if test $ac_cv_libctf_macro_O_CLOEXEC = yes; then
173  AC_DEFINE([HAVE_O_CLOEXEC], 1,
174	    [Whether the platform has a definition of O_CLOEXEC.])
175fi
176
177build_info=
178makeinfo_too_old=
179AC_CHECK_PROGS([MAKEINFO], makeinfo, [$MISSING makeinfo])
180case " $build_configdirs " in
181  *" texinfo "*) MAKEINFO='$$r/$(BUILD_SUBDIR)/texinfo/makeinfo/makeinfo' ;;
182esac
183changequote(,)
184    # We require texinfo to be 6.3 or later, for a working synindex
185    # and validatemenus: otherwise we fall back to /bin/true.
186    if ${MAKEINFO} --version \
187       | egrep 'texinfo[^0-9]*(6\.[3-9]|[7-9][0-9])' >/dev/null 2>&1; then
188      build_info=yes
189    else
190        build_info=
191        makeinfo_too_old=t
192    fi
193changequote([,])
194if test -n "$makeinfo_too_old"; then
195    AC_MSG_WARN([
196*** Makeinfo is too old. Info documentation will not be built.])
197fi
198AC_SUBST(MAKEINFO)
199AM_CONDITIONAL(BUILD_INFO, test "${build_info}" = yes)
200
201CTF_LIBADD="-L`pwd`/../libiberty -liberty"
202SHARED_LDFLAGS=
203
204# Horrible hacks to build DLLs on Windows and a shared library elsewhere.
205if test "$enable_shared" = "yes"; then
206# When building a shared libctf, link against the pic version of libiberty
207# so that apps that use libctf won't need libiberty just to satisfy any
208# libctf references.  We can't do that if a pic libiberty is unavailable
209# since including non-pic # code would insert text relocations into libctf.
210# Note that linking against libbfd as we do here, which is itself linked
211# against libiberty, may not satisfy all the libctf libiberty references
212# since libbfd may not pull in the entirety of libiberty.
213# Also, jam libintl into the right place in all of this: after libiberty,
214# which uses it, but before -lcygwin, which it uses.
215changequote(,)dnl
216  x=`sed -n -e 's/^[ 	]*PICFLAG[ 	]*=[ 	]*//p' < ../libiberty/Makefile | sed -n '$p'`
217changequote([,])dnl
218  if test -n "$x"; then
219    CTF_LIBADD="-L`pwd`/../libiberty/pic -liberty"
220  fi
221fi
222
223CTF_LIBADD="$CTF_LIBADD $LIBINTL"
224
225if test "$enable_shared" = "yes"; then
226  case "${host}" in
227  # More hacks to build DLLs on Windows.
228    *-*-cygwin*)
229      SHARED_LDFLAGS="-no-undefined"
230      CTF_LIBADD="$CTF_LIBADD -lcygwin"
231      ;;
232  esac
233fi
234AC_SUBST(SHARED_LDFLAGS)
235AC_SUBST(CTF_LIBADD)
236
237AC_PATH_PROG([EXPECT], [expect])
238AC_CACHE_CHECK([for Tcl supporting try/catch], [ac_cv_libctf_tcl_try],
239  [ac_cv_libctf_tcl_try=`if test -z $EXPECT; then echo no; else $EXPECT << EOF
240if @<:@llength @<:@info commands try@:>@@:>@ then { puts yes } else { puts no }
241EOF
242fi`
243])
244AM_CONDITIONAL(TCL_TRY, test "${ac_cv_libctf_tcl_try}" = yes)
245
246# Use a version script, if possible, or an -export-symbols-regex otherwise.
247decommented_version_script=
248AC_CACHE_CHECK([for linker versioning flags], [ac_cv_libctf_version_script],
249  [echo 'FOO { global: mai*; local: ctf_fo*; };' > conftest.ver
250   old_LDFLAGS="$LDFLAGS"
251   old_CFLAGS="$CFLAGS"
252   LDFLAGS="$LDFLAGS -shared -Wl,--version-script=conftest.ver"
253   CFLAGS="$CFLAGS -fPIC"
254   AC_LINK_IFELSE([AC_LANG_SOURCE([[int ctf_foo (void) { return 0; }
255				    int main (void) { return ctf_foo(); }]])],
256		  [ac_cv_libctf_version_script="-Wl,--version-script='$srcdir/libctf.ver'"],
257		  [])
258   LDFLAGS="$old_LDFLAGS"
259
260   if test -z "$ac_cv_libctf_version_script"; then
261     LDFLAGS="$LDFLAGS -shared -Wl,-B,local -Wl,-z,gnu-version-script=conftest.ver"
262     AC_LINK_IFELSE([AC_LANG_SOURCE([[int ctf_foo (void) { return 0; }
263				      int main (void) { return ctf_foo(); }]])],
264		    [ac_cv_libctf_version_script="-Wl,-B,local -Wl,-z,gnu-version-script"
265		     decommented_version_script=t],
266		    [])
267     LDFLAGS="$old_LDFLAGS"
268   fi
269   CFLAGS="$old_CFLAGS"
270
271   if test -z "$ac_cv_libctf_version_script"; then
272     ac_cv_libctf_version_script='-export-symbols-regex ctf_.*'
273   fi
274   rm -f conftest.ver])
275if test -n "$decommented_version_script"; then
276   # Solaris's version scripts use shell-style comments rather than the C-style
277   # used by GNU ld.  Use cpp to strip the comments out.  (cpp exists under this
278   # name on all platforms that support ld -z gnu-version-script.)
279   # Also ensure that no symbols exist in the version script for libctf-nobfd.so
280   # that do not exist in the shared library itself, since some linkers add such
281   # symbols with type NOTYPE.
282   /lib/cpp < $srcdir/libctf.ver > libctf-decommented.ver
283   grep -v 'libctf only' $srcdir/libctf.ver | /lib/cpp > libctf-nobfd-decommented.ver
284   VERSION_FLAGS="$ac_cv_libctf_version_script='libctf-decommented.ver'"
285   VERSION_FLAGS_NOBFD="$ac_cv_libctf_version_script='libctf-nobfd-decommented.ver'"
286else
287   VERSION_FLAGS="$ac_cv_libctf_version_script"
288   VERSION_FLAGS_NOBFD="$ac_cv_libctf_version_script"
289fi
290AC_SUBST(VERSION_FLAGS)
291AC_SUBST(VERSION_FLAGS_NOBFD)
292
293AC_CONFIG_FILES(Makefile)
294AC_CONFIG_HEADERS(config.h)
295AC_OUTPUT
296
297GNU_MAKE_JOBSERVER
298