1# Copyright © 2010 Cisco Systems, Inc.  All rights reserved.
2# Copyright © 2015 Inria.  All rights reserved.
3# See COPYING in top-level directory.
4#
5# hwloc modification to the following PKG_* macros -- add HWLOC_
6# prefix to make it "safe" to embed these macros in other packages.
7# Originally copied from the pkg-config package; see copyright and
8# license below.
9
10# pkg.m4 - Macros to locate and utilise pkg-config.            -*- Autoconf -*-
11#
12# Copyright © 2004 Scott James Remnant <scott@netsplit.com>.
13#
14# This program is free software; you can redistribute it and/or modify
15# it under the terms of the GNU General Public License as published by
16# the Free Software Foundation; either version 2 of the License, or
17# (at your option) any later version.
18#
19# This program is distributed in the hope that it will be useful, but
20# WITHOUT ANY WARRANTY; without even the implied warranty of
21# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22# General Public License for more details.
23#
24# You should have received a copy of the GNU General Public License
25# along with this program; if not, write to the Free Software
26# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27#
28# As a special exception to the GNU General Public License, if you
29# distribute this file as part of a program that contains a
30# configuration script generated by Autoconf, you may include it under
31# the same distribution terms that you use for the rest of that program.
32
33# HWLOC_PKG_PROG_PKG_CONFIG([MIN-VERSION])
34# ----------------------------------
35# hwloc note: Per https://git.open-mpi.org/trac/hwloc/ticket/55, keep
36# the environment variable $PKG_CONFIG (vs. renaming it
37# $HWLOC_PKG_CONFIG).  Short explanation: $PKG_CONFIG is a well-known
38# environment variable that can be set by users to override what these
39# .m4 macros do.  There's no reason we should have a different env
40# variable name (e.g., $HWLOC_PKG_CONFIG).  So leave it named
41# $PKG_CONFIG both here in this specific macro, and all the other
42# macros that use it.
43AC_DEFUN([HWLOC_PKG_PROG_PKG_CONFIG],
44[m4_pattern_forbid([^_?PKG_[A-Z_]+$])
45m4_pattern_allow([^PKG_CONFIG(_PATH)?$])
46AC_ARG_VAR([PKG_CONFIG], [path to pkg-config utility])dnl
47
48if test "x$ac_cv_env_PKG_CONFIG_set" != "xset"; then
49	AC_PATH_TOOL([PKG_CONFIG], [pkg-config])
50fi
51if test -n "$PKG_CONFIG"; then
52	HWLOC_pkg_min_version=m4_default([$1], [0.9.0])
53	AC_MSG_CHECKING([pkg-config is at least version $HWLOC_pkg_min_version])
54	if $PKG_CONFIG --atleast-pkgconfig-version $HWLOC_pkg_min_version; then
55		AC_MSG_RESULT([yes])
56	else
57		AC_MSG_RESULT([no])
58		PKG_CONFIG=""
59	fi
60
61fi[]dnl
62])# HWLOC_PKG_PROG_PKG_CONFIG
63
64# HWLOC_PKG_CHECK_EXISTS(MODULES, [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])
65#
66# Check to see whether a particular set of modules exists.  Similar
67# to HWLOC_PKG_CHECK_MODULES(), but does not set variables or print errors.
68#
69#
70# Similar to HWLOC_PKG_CHECK_MODULES, make sure that the first instance of
71# this or HWLOC_PKG_CHECK_MODULES is called, or make sure to call
72# HWLOC_PKG_CHECK_EXISTS manually
73# --------------------------------------------------------------
74AC_DEFUN([HWLOC_PKG_CHECK_EXISTS],
75[AC_REQUIRE([HWLOC_PKG_PROG_PKG_CONFIG])dnl
76if test -n "$PKG_CONFIG" && \
77    AC_RUN_LOG([$PKG_CONFIG --exists --silence-errors "$1"]); then
78    m4_ifval([$2], [$2], [:])
79    m4_ifvaln([$3], [else
80                     $3])dnl
81fi])
82
83
84# _HWLOC_PKG_CONFIG([VARIABLE], [COMMAND], [MODULES])
85# ---------------------------------------------
86m4_define([_HWLOC_PKG_CONFIG],
87[if test -n "$PKG_CONFIG"; then
88    if test -n "$$1"; then
89        HWLOC_pkg_cv_[]$1="$$1"
90    else
91        HWLOC_PKG_CHECK_EXISTS([$3],
92                         [HWLOC_pkg_cv_[]$1=`$PKG_CONFIG --[]$2 "$3" 2>/dev/null`],
93			 [HWLOC_pkg_failed=yes])
94    fi
95else
96	HWLOC_pkg_failed=untried
97fi[]
98])# _HWLOC_PKG_CONFIG
99
100# _HWLOC_PKG_SHORT_ERRORS_SUPPORTED
101# -----------------------------
102AC_DEFUN([_HWLOC_PKG_SHORT_ERRORS_SUPPORTED],
103[AC_REQUIRE([HWLOC_PKG_PROG_PKG_CONFIG])
104if $PKG_CONFIG --atleast-pkgconfig-version 0.20; then
105        HWLOC_pkg_short_errors_supported=yes
106else
107        HWLOC_pkg_short_errors_supported=no
108fi[]dnl
109])# _HWLOC_PKG_SHORT_ERRORS_SUPPORTED
110
111
112# HWLOC_PKG_CHECK_MODULES(VARIABLE-PREFIX, MODULES, FUNCTION, HEADER, [ACTION-IF-FOUND],
113# [ACTION-IF-NOT-FOUND])
114#
115#
116# Note that if there is a possibility the first call to
117# HWLOC_PKG_CHECK_MODULES might not happen, you should be sure to include an
118# explicit call to HWLOC_PKG_PROG_PKG_CONFIG in your configure.ac
119#
120#
121# --------------------------------------------------------------
122AC_DEFUN([HWLOC_PKG_CHECK_MODULES],[
123    AC_REQUIRE([HWLOC_PKG_PROG_PKG_CONFIG])dnl
124    AC_ARG_VAR([HWLOC_]$1[_CFLAGS], [C compiler flags for $1, overriding pkg-config])dnl
125    AC_ARG_VAR([HWLOC_]$1[_LIBS], [linker flags for $1, overriding pkg-config])dnl
126
127    HWLOC_pkg_failed=no
128    AC_MSG_CHECKING([for $1])
129
130    _HWLOC_PKG_CONFIG([HWLOC_][$1][_CFLAGS], [cflags], [$2])
131    _HWLOC_PKG_CONFIG([HWLOC_][$1][_LIBS], [libs], [$2])
132
133    m4_define([_HWLOC_PKG_TEXT], [Alternatively, you may set the environment variables HWLOC_[]$1[]_CFLAGS
134and HWLOC_[]$1[]_LIBS to avoid the need to call pkg-config.
135See the pkg-config man page for more details.])
136
137    # Check for failure of pkg-config
138    if test $HWLOC_pkg_failed = yes; then
139        _HWLOC_PKG_SHORT_ERRORS_SUPPORTED
140        if test $HWLOC_pkg_short_errors_supported = yes; then
141            HWLOC_[]$1[]_PKG_ERRORS=`$PKG_CONFIG --short-errors --errors-to-stdout --print-errors "$2" 2>&1`
142        else
143            HWLOC_[]$1[]_PKG_ERRORS=`$PKG_CONFIG --errors-to-stdout --print-errors "$2" 2>&1`
144        fi
145        # Put the nasty error message in config.log where it belongs
146	echo "$HWLOC_[]$1[]_PKG_ERRORS" >&AS_MESSAGE_LOG_FD
147
148	ifelse([$6], , [AC_MSG_ERROR(dnl
149[Package requirements ($2) were not met:
150
151$HWLOC_$1_PKG_ERRORS
152
153Consider adjusting the PKG_CONFIG_PATH environment variable if you
154installed software in a non-standard prefix.
155
156_HWLOC_PKG_TEXT
157])],
158		[AC_MSG_RESULT([no])
159                $6])
160    elif test $HWLOC_pkg_failed = untried; then
161        ifelse([$6], , [AC_MSG_FAILURE(dnl
162[The pkg-config script could not be found or is too old.  Make sure it
163is in your PATH or set the PKG_CONFIG environment variable to the full
164path to pkg-config.
165
166_HWLOC_PKG_TEXT
167
168To get pkg-config, see <http://pkg-config.freedesktop.org/>.])],
169		[AC_MSG_RESULT([cannot check without pkg-config])
170		$6])
171    else
172        AC_MSG_RESULT([yes])
173
174        # If we got good results from pkg-config, check that they
175        # actually work (i.e., that we can link against the resulting
176        # $LIBS).  The canonical example why we do this is if
177        # pkg-config returns 64 bit libraries but ./configure was run
178        # with CFLAGS=-m32 LDFLAGS=-m32.  pkg-config gave us valid
179        # results, but we'll fail if we try to link.  So detect that
180        # failure now.
181        # There are also cases on Mac where pkg-config returns paths
182        # that do not actually exists until some magic is applied.
183        # http://www.open-mpi.org/community/lists/hwloc-devel/2015/03/4402.php
184        # So check whether we find the header as well.
185        hwloc_cflags_save=$CFLAGS
186        hwloc_cppflags_save=$CPPFLAGS
187        hwloc_libs_save=$LIBS
188        CFLAGS="$CFLAGS $HWLOC_pkg_cv_HWLOC_[]$1[]_CFLAGS"
189        CPPFLAGS="$CPPFLAGS $HWLOC_pkg_cv_HWLOC_[]$1[]_CFLAGS"
190        LIBS="$LIBS $HWLOC_pkg_cv_HWLOC_[]$1[]_LIBS"
191        AC_CHECK_HEADER([$4], [
192            AC_CHECK_FUNC([$3], [hwloc_result=yes], [hwloc_result=no])
193            ], [hwloc_result=no])
194        CFLAGS=$hwloc_cflags_save
195        CPPFLAGS=$hwloc_cppflags_save
196        LIBS=$hwloc_libs_save
197
198        AC_MSG_CHECKING([for final $1 support])
199        AS_IF([test "$hwloc_result" = "yes"],
200              [HWLOC_[]$1[]_CFLAGS=$HWLOC_pkg_cv_HWLOC_[]$1[]_CFLAGS
201               HWLOC_[]$1[]_LIBS=$HWLOC_pkg_cv_HWLOC_[]$1[]_LIBS
202               AC_MSG_RESULT([yes])
203               ifelse([$5], , :, [$5])],
204              [AC_MSG_RESULT([no])
205               ifelse([$6], , :, [$6])])
206    fi[]dnl
207])# HWLOC_PKG_CHECK_MODULES
208