1## -*- mode: autoconf; autoconf-indentation: 4; -*-
2##
3##  nloptr configure.ac
4##
5##  nloptr -- R interface to NLopt
6##
7##  Copyright (C) 2014  Dirk Eddelbuettel
8##
9## This file is licensed under the GPL-2 or later just like most of my
10## Open Source code, and is granted an exemption (should it be needed)
11## for inclusion into nloptr
12##
13## History:
14##   2020-02-26 (JY): Removed references to CPPFLAGS.
15##   2020-02-26 (JY): Changed CPP to $CC -E and CXXCPP to $CXX -E as support
16##     for these flags in R CMD config is deprecated from R 3.6.2.
17
18# require at least autoconf 2.69
19AC_PREREQ([2.69])
20
21# Process this file with autoconf to produce a configure script.
22AC_INIT([nloptr],[1.0.1])
23
24## consider command-line options if given
25##
26AC_ARG_WITH([nlopt-cflags],
27            AS_HELP_STRING([--with-nlopt-cflags=CFLAGS],[Supply compiler flags such as the location of NLopt header files]),
28            [nlopt_cflags=$withval],
29            [nlopt_cflags=""])
30AC_ARG_WITH([nlopt-libs],
31            AS_HELP_STRING([--with-nlopt-libs=LIBS],[Supply the linker flags such as the location and name of NLopt libraries]),
32            [nlopt_libs=$withval],
33            [nlopt_libs=""])
34
35## Set R_HOME, respecting an environment variable if set
36: ${R_HOME=$(R RHOME)}
37if test -z "${R_HOME}"; then
38 AC_MSG_ERROR([Could not determine R_HOME.])
39fi
40
41## Get R compilers and flags.
42## Add user-supplied flags to flags specified by R if they have been set.
43CC=$("${R_HOME}/bin/R" CMD config CC)
44CFLAGS="$("${R_HOME}/bin/R" CMD config CFLAGS) ${CFLAGS}"
45CPP="$("${R_HOME}/bin/R" CMD config CC) -E"
46CXX=$("${R_HOME}/bin/R" CMD config CXX)
47CXXFLAGS="$("${R_HOME}/bin/R" CMD config CXXFLAGS) ${CXXFLAGS}"
48CXXCPP="$("${R_HOME}/bin/R" CMD config CXX) -E"
49AR=$("${R_HOME}/bin/R" CMD config AR)
50RANLIB=$("${R_HOME}/bin/R" CMD config RANLIB)
51
52# We are using C++
53AC_LANG(C++)
54AC_REQUIRE_CPP
55AC_PROG_CXX
56
57## check for pkg-config
58AC_DEFUN([AC_PROG_PKGCONFIG], [AC_CHECK_PROG(PKGCONFIG,pkg-config,yes)])
59AC_PROG_PKGCONFIG
60
61## default to assuming no sufficient NLopt library has been found
62nlopt_good="no"
63nlopt_cflags=""
64nlopt_libs=""
65
66## also use pkg-config to check for NLopt
67##
68if test x"${nlopt_libs}" == x""; then
69    if test x"${PKGCONFIG}" == x"yes"; then
70        ## check via pkg-config for nlopt
71        if pkg-config --exists nlopt; then
72            ## obtain cflags and obtain libs
73            nlopt_cflags=$(pkg-config --cflags nlopt)
74            nlopt_libs=$(pkg-config --libs nlopt)
75            nlopt_good="yes"
76        fi
77    fi
78fi
79
80
81## And make sure these flags are used for the test below.
82CPPFLAGS="${nlopt_cflags}"
83CXXFLAGS="${nlopt_cflags} ${CXXFLAGS}"
84
85## check for headers Debian has in libhiredis-dev
86AC_MSG_NOTICE([Now testing for NLopt header file.])
87nlopt_header=nlopt.h
88AC_CHECK_HEADER([$nlopt_header],
89                [nlopt_good="yes"],  # may want to check for minimal version here too
90                [nlopt_good="no"],
91                [])
92
93## Check for NLopt version number using pkg-config.
94# Perform this check after the header check,
95# because the correct header with the wrong
96# version, does not work.
97if test x"${nlopt_good}" = x"yes"; then
98    AC_MSG_NOTICE([Now testing for NLopt version 2.4.0 or greater.])
99    if test x"${PKGCONFIG}" == x"yes"; then
100        ## check via pkg-config for version number of nlopt
101        if pkg-config --exists nlopt; then
102            ## obtain version number
103            nlopt_version=$(pkg-config --modversion nlopt)
104
105            case ${nlopt_version} in
106                 1.*|2.0.*|2.1.*|2.2.*|2.3.*)
107                    AC_MSG_WARN([Only NLopt version 2.4.0 or greater can be used with nloptr.])
108                    nlopt_good="no"
109                ;;
110            esac
111        fi
112    else
113        AC_MSG_WARN([Could not test version of NLopt because pkg-config is not installed. Make sure the version number of NLopt is 2.4.0 or greater. Otherwise nloptr can not be used.])
114    fi
115fi
116
117## in case neither of the two methods above worked, build NLopt locally
118## from included source files.
119if test x"${nlopt_good}" = x"no"; then
120   AC_MSG_NOTICE([Need to configure and build NLopt])
121
122   ## Libraries necessary to link with NLopt
123   NLOPTR_LIBS="-lm $(pwd)/src/nlopt_src/lib/libnlopt_cxx.a"
124
125   ## Necessary Include dirs
126   NLOPTR_INCL="-I$(pwd)/src/nlopt_src/include"
127
128   ## Get flags that have been re-defined above
129   ## to test whether NLopt headers are present.
130   NLOPT_CXXFLAGS=$("${R_HOME}/bin/R" CMD config CXXFLAGS)
131
132   ## Compile NLopt source code and clean up
133   ## --prefix="`pwd`", which is the directory we want to
134   ## install in, after we changed our current directory
135   AC_MSG_NOTICE([Starting to install library to $(pwd)/src/nlopt_src])
136   (cd src/nlopt_src; \
137        ./configure --prefix="$(pwd)" --enable-shared --enable-static --without-octave \
138                    --without-matlab --without-guile --without-python --with-cxx \
139		    AR="${AR}" RANLIB="${RANLIB}" \
140                    CC="${CC}" CFLAGS="${CFLAGS}" CPP="${CPP}" CXX="${CXX}" \
141                    CXXFLAGS="${NLOPT_CXXFLAGS}" CXXCPP="${CXXCPP}" && \
142        make && make install && rm -rf .libs) || exit 1
143   AC_MSG_NOTICE([Done installing library to $(pwd)/src/nlopt_src])
144
145   ## Copy header files to inst/include such that they are available from R.
146   mkdir -p inst/include
147   cp src/nlopt_src/include/* inst/include
148
149   ## Store compiler and linker flags
150   makevars_nlopt_cflags="${CFLAGS} ${NLOPTR_INCL}"
151   makevars_nlopt_libs="${NLOPTR_LIBS}"
152   nlopt_libs="-lm"
153
154else
155   AC_MSG_NOTICE([Suitable NLopt library found.])
156fi
157
158## could add a test of building a three-liner here
159
160## now use all these
161AC_SUBST([MAKEVARS_PKG_CFLAGS],["${PKG_CFLAGS} $nlopt_cflags $makevars_nlopt_cflags"])
162AC_SUBST([MAKEVARS_PKG_LIBS],["${PKG_LIBS} $nlopt_libs $makevars_nlopt_libs"])
163AC_SUBST([PKG_CFLAGS],["${PKG_CFLAGS} $nlopt_cflags"])
164AC_SUBST([PKG_LIBS],["${PKG_LIBS} $nlopt_libs"])
165AC_CONFIG_FILES([src/Makevars R/PkgFlags.R])
166AC_OUTPUT
167