1dnl PAC_WITH_LIB_HELP_STRING(with_option)
2dnl internal macro for PAC_SET_HEADER_LIB_PATH
3AC_DEFUN([PAC_WITH_LIB_HELP_STRING], [
4    [specify path where $1 include directory and lib directory can be found.
5     Having this option explicitly requires the library. When PATH is not given,
6     it checks the library from system paths.]
7    m4_ifdef([$1_embedded_dir], [Pass "embedded" to force use of embedded version.])
8])
9
10dnl PAC_SET_HEADER_LIB_PATH(with_option)
11dnl This macro looks for the --with-xxx=, --with-xxx-include and --with-xxx-lib=
12dnl options and sets the library and include paths.
13dnl
14dnl If the library has an embedded version, m4_define xxx_embedded_dir to allow
15dnl embedded options.
16dnl
17dnl TODO as written, this macro cannot handle a "with_option" arg that has "-"
18dnl characters in it.  Use AS_TR_SH (and possibly AS_VAR_* macros) to handle
19dnl this case if it ever arises.
20AC_DEFUN([PAC_SET_HEADER_LIB_PATH],[
21    AC_ARG_WITH([$1], [AC_HELP_STRING([--with-$1=[[PATH]]],PAC_WITH_LIB_HELP_STRING($1))])
22
23    AC_ARG_WITH([$1-include],
24                [AC_HELP_STRING([--with-$1-include=PATH],
25                                [specify path where $1 include directory can be found])],
26                [AS_CASE(["$withval"],
27                         [yes|no|''],
28                         [AC_MSG_WARN([--with[out]-$1-include=PATH expects a valid PATH])
29                          with_$1_include=""])],
30                [])
31    AC_ARG_WITH([$1-lib],
32                [AC_HELP_STRING([--with-$1-lib=PATH],
33                                [specify path where $1 lib directory can be found])],
34                [AS_CASE(["$withval"],
35                         [yes|no|''],
36                         [AC_MSG_WARN([--with[out]-$1-lib=PATH expects a valid PATH])
37                          with_$1_lib=""])],
38                [])
39
40    # The args have been sanitized into empty/non-empty values above.
41    # Now append -I/-L args to CPPFLAGS/LDFLAGS, with more specific options
42    # taking priority
43
44    case "${with_$1}" in
45        embedded)
46            m4_ifndef([$1_embedded_dir],[AC_MSG_ERROR([embedded $1 is requested but we do not have the embedded version])])
47            ;;
48        yes|no)
49            # skip
50            ;;
51        *)
52    AS_IF([test -n "${with_$1_include}"],
53          [PAC_APPEND_FLAG([-I${with_$1_include}],[CPPFLAGS])],
54          [AS_IF([test -n "${with_$1}"],
55                 [PAC_APPEND_FLAG([-I${with_$1}/include],[CPPFLAGS])])])
56
57    AS_IF([test -n "${with_$1_lib}"],
58          [PAC_APPEND_FLAG([-L${with_$1_lib}],[LDFLAGS])],
59          [AS_IF([test -n "${with_$1}"],
60                 dnl is adding lib64 by default really the right thing to do?  What if
61                 dnl we are on a 32-bit host that happens to have both lib dirs available?
62                 [PAC_APPEND_FLAG([-L${with_$1}/lib],[LDFLAGS])
63                  AS_IF([test -d "${with_$1}/lib64"],
64                       [PAC_APPEND_FLAG([-L${with_$1}/lib64],[LDFLAGS])])
65                 ])
66          ])
67            ;;
68    esac
69])
70
71
72dnl PAC_CHECK_HEADER_LIB(header.h, libname, function, action-if-yes, action-if-no)
73dnl This macro checks for a header and lib.  It is assumed that the
74dnl user can specify a path to the includes and libs using --with-xxx=.
75dnl The xxx is specified in the "with_option" parameter.
76dnl
77dnl NOTE: This macro expects a corresponding PAC_SET_HEADER_LIB_PATH
78dnl macro (or equivalent logic) to be used before this macro is used.
79dnl
80dnl NOTE: since setting LIBS may break runtime checks (e.g. AC_CHECK_SIZEOF), we
81dnl prepend the library to WRAPPER_LIBS instead.
82
83AC_DEFUN([PAC_CHECK_HEADER_LIB],[
84    failure=no
85    AC_CHECK_HEADER([$1],,failure=yes)
86    PAC_PUSH_FLAG(LIBS)
87    AC_CHECK_LIB($2,$3,,failure=yes)
88    PAC_POP_FLAG(LIBS)
89    if test "$failure" = "no" ; then
90       $4
91    else
92       $5
93    fi
94])
95
96dnl PAC_LIBS_ADD(libname)
97dnl Explicitly add -llibname to LIBS or WRAPPER_LIBS
98AC_DEFUN([PAC_LIBS_ADD], [
99    m4_ifdef([use_wrapper_flags],[PAC_APPEND_FLAG([$1],[WRAPPER_LIBS])],[PAC_APPEND_FLAG([$1],[LIBS])])
100])
101
102dnl PAC_CHECK_HEADER_LIB_OPTIONAL(with_option, header.h, libname, function)
103dnl Check optional library. The results are in $pac_have_$1.
104AC_DEFUN([PAC_CHECK_HEADER_LIB_OPTIONAL],[
105    PAC_SET_HEADER_LIB_PATH($1)
106    if test "${with_$1}" = "embedded" ; then
107        dnl Caller still need configure the embedded version
108        pac_have_$1=yes
109    elif test "${with_$1}" = "no" ; then
110        pac_have_$1=no
111    else
112        dnl Other than "embedded" or "no", we check ...
113        PAC_CHECK_HEADER_LIB($2,$3,$4,pac_have_$1=yes,pac_have_$1=no)
114        if test "${pac_have_$1}" = "no" -a -n "${with_$1}" ; then
115            dnl user asks for it, so missing is an error
116            AC_MSG_ERROR([--with-$1 is given but not found])
117        fi
118        if test "${pac_have_$1}" = "yes" ; then
119            PAC_LIBS_ADD(-l$3)
120        fi
121    fi
122])
123
124dnl PAC_PROBE_HEADER_LIB(with_option, header.h, libname, function)
125dnl Similar to PAC_CHECK_HEADER_LIB_OPTIONAL, but will not set LIBS.
126dnl Results are in $pac_have_$1.
127dnl This is used in more complex situation, e.g. checking libfabric
128dnl to decide default netmod options
129AC_DEFUN([PAC_PROBE_HEADER_LIB],[
130    PAC_SET_HEADER_LIB_PATH($1)
131    if test "${with_$1}" = "embedded" ; then
132        dnl Caller still need configure the embedded version
133        pac_have_$1=yes
134    elif test "${with_$1}" = "no" ; then
135        pac_have_$1=no
136    else
137        dnl Other than "embedded" or "no", we check ...
138        PAC_CHECK_HEADER_LIB($2,$3,$4,pac_have_$1=yes,pac_have_$1=no)
139        if test "${pac_have_$1}" = "no" -a -n "${with_$1}" ; then
140            dnl user asks for it, so missing is an error
141            AC_MSG_ERROR([--with-$1 is given but not found])
142        fi
143    fi
144])
145
146dnl PAC_CHECK_HEADER_LIB_EXPLICIT(with_option, header.h, libname, function)
147dnl Similar to PAC_CHECK_HEADER_LIB_OPTIONAL, but only with explicit --with-libname
148AC_DEFUN([PAC_CHECK_HEADER_LIB_EXPLICIT],[
149    pac_have_$1=no
150    if test -n "${with_$1}" -a "${with_$1}" != "no" ; then
151        PAC_CHECK_HEADER_LIB_OPTIONAL($1, $2, $3, $4)
152    fi
153    dnl Use embedded version by default
154    m4_ifdef([$1_embedded_dir],[
155        if test "$pac_have_$1" = "no" ; then
156            with_$1=embedded
157        fi
158    ])
159])
160
161dnl PAC_CHECK_HEADER_LIB_FATAL(with_option, header.h, libname, function)
162dnl Similar to PAC_CHECK_HEADER_LIB, but errors out on failure
163AC_DEFUN([PAC_CHECK_HEADER_LIB_FATAL],[
164    PAC_SET_HEADER_LIB_PATH($1)
165    PAC_CHECK_HEADER_LIB($2,$3,$4,success=yes,success=no)
166    if test "$success" = "no" ; then
167        AC_MSG_ERROR([$2 or lib$3 library not found. Did you specify --with-$1= or --with-$1-include= or --with-$1-lib=?])
168    fi
169    PAC_LIBS_ADD(-l$3)
170])
171
172dnl PAC_CHECK_PREFIX(with_option,prefixvar)
173AC_DEFUN([PAC_CHECK_PREFIX],[
174	AC_ARG_WITH([$1-prefix],
175            [AS_HELP_STRING([[--with-$1-prefix[=DIR]]], [use the $1
176                            library installed in DIR, rather than the
177                            one included in the distribution.  Pass
178                            "embedded" to force usage of the included
179                            $1 source.])],
180            [if test "$withval" = "system" ; then
181                 :
182             elif test "$withval" = "embedded" ; then
183                 :
184             elif test "$withval" = "no" ; then
185                 :
186             else
187                 PAC_APPEND_FLAG([-I${with_$1_prefix}/include],[CPPFLAGS])
188                 if test -d "${with_$1_prefix}/lib64" ; then
189                     PAC_APPEND_FLAG([-L${with_$1_prefix}/lib64],[LDFLAGS])
190                 fi
191                 PAC_APPEND_FLAG([-L${with_$1_prefix}/lib],[LDFLAGS])
192             fi
193             ],
194            [with_$1_prefix="embedded"])
195	]
196)
197
198dnl PAC_LIB_DEPS(library_name, library_pc_path)
199dnl library_pc_path is the path to the library pkg-config directory
200AC_DEFUN([PAC_LIB_DEPS],[
201if test "x$2" != "x"; then
202    ac_lib$1_deps=`pkg-config --static --libs $2/lib$1.pc 2>/dev/null`
203    # remove the library itself in case it is embedded
204    ac_lib$1_deps=`echo $ac_lib$1_deps | sed 's/-l$1//'`
205else
206    # use system default
207    ac_lib$1_deps=`pkg-config --static --libs lib$1 2>/dev/null`
208fi
209])
210
211