1#!/usr/bin/m4
2#
3# Copyright (c) 2013-2018 The strace developers.
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10#    notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12#    notice, this list of conditions and the following disclaimer in the
13#    documentation and/or other materials provided with the distribution.
14# 3. The name of the author may not be used to endorse or promote products
15#    derived from this software without specific prior written permission.
16#
17# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28AC_DEFUN([st_ARG_LIBUNWIND], [dnl
29
30AC_ARG_WITH([libunwind],
31	    [AS_HELP_STRING([--with-libunwind],
32			    [use libunwind to implement stack tracing support])],
33	    [case "${withval}" in
34	     yes|no|check) ;;
35	     *) with_libunwind=yes
36		libunwind_CPPFLAGS="-I${withval}/include"
37		libunwind_LDFLAGS="-L${withval}/lib" ;;
38	     esac],
39	    [with_libunwind=check]
40)
41
42])
43
44AC_DEFUN([st_LIBUNWIND], [dnl
45
46libunwind_CPPFLAGS=
47libunwind_LDFLAGS=
48libunwind_LIBS=
49
50AS_IF([test "x$with_libunwind" != xno && test "x$use_unwinder" = x],
51      [saved_CPPFLAGS="$CPPFLAGS"
52       CPPFLAGS="$CPPFLAGS $libunwind_CPPFLAGS"
53
54       AC_CHECK_HEADERS([libunwind-ptrace.h],
55	 [saved_LDFLAGS="$LDFLAGS"
56	  LDFLAGS="$LDFLAGS $libunwind_LDFLAGS"
57
58	  AC_CHECK_LIB([unwind], [backtrace],
59	    [libunwind_LIBS="-lunwind $libunwind_LIBS"
60
61	     AC_MSG_CHECKING([for unw_create_addr_space in libunwind-generic])
62	     saved_LIBS="$LIBS"
63	     LIBS="-lunwind-generic $libunwind_LIBS $LIBS"
64
65	     AC_LINK_IFELSE(
66	       [AC_LANG_PROGRAM([[#include <libunwind-ptrace.h>]],
67				[[return !unw_create_addr_space(0, 0)]])
68	       ],
69	       [AC_MSG_RESULT([yes])
70		libunwind_LIBS="-lunwind-generic $libunwind_LIBS"
71
72		AC_CHECK_LIB([unwind-ptrace], [_UPT_create],
73		  [libunwind_LIBS="-lunwind-ptrace $libunwind_LIBS"
74		   use_unwinder=libunwind
75		  ],
76		  [if test "x$with_libunwind" != xcheck; then
77		     AC_MSG_FAILURE([failed to find _UPT_create in libunwind-ptrace])
78		   fi
79		  ],
80		  [$libunwind_LIBS]
81		)
82	       ],
83	       [AC_MSG_RESULT([no])
84		if test "x$with_libunwind" != xcheck; then
85		  AC_MSG_FAILURE([failed to find unw_create_addr_space in libunwind-generic])
86		fi
87	       ]
88	     )
89
90	     LIBS="$saved_LIBS"
91	    ],
92	    [if test "x$with_libunwind" != xcheck; then
93	       AC_MSG_FAILURE([failed to find libunwind])
94	     fi
95	    ],
96	    [$libunwind_LIBS]
97	  )
98
99	  LDFLAGS="$saved_LDFLAGS"
100	 ],
101	 [if test "x$with_libunwind" != xcheck; then
102	    AC_MSG_FAILURE([failed to find libunwind-ptrace.h])
103	  fi
104	 ]
105       )
106
107       CPPFLAGS="$saved_CPPFLAGS"
108      ]
109)
110
111if test "x$use_unwinder" = xlibunwind; then
112	AC_DEFINE([USE_LIBUNWIND], 1,
113		  [Whether to use libunwind for stack tracing])
114	AC_SUBST(libunwind_LIBS)
115	AC_SUBST(libunwind_LDFLAGS)
116	AC_SUBST(libunwind_CPPFLAGS)
117fi
118
119])
120