1# dnl Process this file with autoconf to produce a configure script.
2define([pkgversion], esyscmd([sh -c "grep Version: DESCRIPTION | cut -d' ' -f2 | tr -d '\n'"]))dnl
3AC_INIT([rgdal],[pkgversion],[Roger.Bivand@nhh.no])
4AC_CONFIG_SRCDIR(src/gdal-bindings.cpp)
5
6# AC_PROG_CXX()
7
8# find R home and set correct compiler + flags
9: ${R_HOME=`R RHOME`}
10if test -z "${R_HOME}"; then
11  AC_MSG_ERROR([cannot determine R_HOME. Make sure you use R CMD INSTALL!])
12fi
13AC_MSG_NOTICE([R_HOME: ${R_HOME}])
14RBIN="${R_HOME}/bin/R"
15
16# pick all flags for testing from R
17: ${CC=`"${RBIN}" CMD config CC`}
18: ${CXX=`"${RBIN}" CMD config CXX`}
19#: ${CPP=`"${RBIN}" CMD config CPP`}
20: ${CFLAGS=`"${RBIN}" CMD config CFLAGS`}
21: ${CPPFLAGS=`"${RBIN}" CMD config CPPFLAGS`}
22: ${CXXFLAGS=`"${RBIN}" CMD config CXXFLAGS`}
23: ${LDFLAGS=`"${RBIN}" CMD config LDFLAGS`}
24# AC_SUBST([CC],["clang"])
25# AC_SUBST([CXX],["clang++"])
26AC_MSG_NOTICE([CC: ${CC}])
27AC_MSG_NOTICE([CXX: ${CXX}])
28AC_MSG_NOTICE([CFLAGS: ${CFLAGS}])
29AC_MSG_NOTICE([CPPFLAGS: ${CPPFLAGS}])
30AC_MSG_NOTICE([CXXFLAGS: ${CXXFLAGS}])
31AC_MSG_NOTICE([LDFLAGS: ${LDFLAGS}])
32
33AC_ARG_ENABLE([loadflags],
34    [AS_HELP_STRING([--enable-loadflags=yes/no],[use value of R CMD config LDFLAGS for configure, default yes])],
35    [loadflags=${enableval}],
36    [loadflags="yes"])
37
38#echo "loadflags: $loadflags"
39
40if test "${loadflags}" = "no" ; then
41    LDFLAGS=""
42    AC_MSG_NOTICE(value of R CMD config LDFLAGS not used for configure)
43fi
44AC_MSG_NOTICE([LDFLAGS: ${LDFLAGS}])
45
46#CXX_11
47
48R_VERSION=`echo 'o <- R.version; cat(paste0(o$major, ".", o$minor))' | ${RBIN} --vanilla --slave`
49#AC_MSG_NOTICE([R_VERSION is: ${R_VERSION}])
50R_MINOR=`echo $R_VERSION | cut -f2 -d"."`
51R_MAJOR=`echo $R_VERSION | cut -f1 -d"."`
52#AC_MSG_NOTICE([R_MAJOR is: ${R_MAJOR}, R_MINOR is: ${R_MINOR}])
53
54#if test ${R_MINOR} -ge 4; then
55#: ${CXX=`"${RBIN}" CMD config CXX11`}
56#else
57#: ${CXX=`"${RBIN}" CMD config CXX1X`}
58#fi
59
60if test ${R_MAJOR} -ge 4; then
61 CXX11=`"${RBIN}" CMD config CXX11`
62 CXX11STD=`"${RBIN}" CMD config CXX11STD`
63else
64# if test ${R_MAJOR} -eq 3 -a ${R_MINOR} -ge 4; then
65 if test ${R_MAJOR} -eq 3 && test ${R_MINOR} -ge 4; then
66  CXX11=`"${RBIN}" CMD config CXX11`
67  CXX11STD=`"${RBIN}" CMD config CXX11STD`
68 else
69  CXX11=`"${RBIN}" CMD config CXX1X`
70  CXX11STD=`"${RBIN}" CMD config CXX1XSTD`
71 fi
72fi
73
74AC_MSG_NOTICE([CXX11 is: ${CXX11}, CXX11STD is: ${CXX11STD}])
75
76CXX="${CXX11} ${CXX11STD}"
77
78AC_MSG_NOTICE([CXX is: ${CXX}])
79
80if test -n "${CXX11}"; then
81HAVE_CXX11=1
82else
83HAVE_CXX11=0
84fi
85
86#HAVE_CXX11=1
87
88#m4_include([inst/m4/ax_cxx_compile_stdcxx.m4])
89#AX_CXX_COMPILE_STDCXX([11], [ext], [optional])
90if test [${HAVE_CXX11} = 1] ; then
91  AC_MSG_NOTICE([C++11 support available])
92else
93  AC_MSG_NOTICE([C++11 support not available])
94fi
95
96
97AC_MSG_NOTICE([${PACKAGE_NAME}: ${PACKAGE_VERSION}])
98
99AC_CHECK_FILE([/usr/bin/svnversion],
100 [SVN_VERSION=`svnversion -n '.'`],
101 [SVN_VERSION=""])
102
103#SVN_VERSION=`svnversion -n '.'`
104#if test "${SVN_VERSION}" != "exported" -a test -n "${SVN_VERSION}" -a test "${SVN_VERSION}" != "Unversioned directory"; then
105if test "${SVN_VERSION}" != "exported" && test -n "${SVN_VERSION}" && test "${SVN_VERSION}" != "Unversioned directory"; then  echo "${SVN_VERSION}" > inst/SVN_VERSION
106else
107  SVN_VERSION=`cat inst/SVN_VERSION | tr -d '\n'`
108fi
109AC_MSG_NOTICE([svn revision: ${SVN_VERSION}])
110
111#GDAL
112
113GDAL_CONFIG="gdal-config"
114
115GDAL_CONFIG_SET="no"
116
117AC_ARG_WITH([gdal-config],
118    AS_HELP_STRING([--with-gdal-config=GDAL_CONFIG],[the location of gdal-config]),
119           [gdal_config=$withval])
120if test [ -n "$gdal_config" ] ; then
121    GDAL_CONFIG_SET="yes"
122    AC_SUBST([GDAL_CONFIG],["${gdal_config}"])
123    AC_MSG_NOTICE(gdal-config set to $GDAL_CONFIG)
124fi
125
126if test ["$GDAL_CONFIG_SET" = "no"] ; then
127  AC_PATH_PROG([GDAL_CONFIG], ["$GDAL_CONFIG"],["no"])
128  if test ["$GDAL_CONFIG" = "no"] ; then
129    AC_MSG_RESULT(no)
130    AC_MSG_ERROR([gdal-config not found or not executable.])
131  fi
132else
133  AC_MSG_CHECKING(gdal-config exists)
134  if test -r "${GDAL_CONFIG}"; then
135    AC_MSG_RESULT(yes)
136  else
137    AC_MSG_RESULT(no)
138    AC_MSG_ERROR([gdal-config not found - configure argument error.])
139  fi
140  AC_MSG_CHECKING(gdal-config executable)
141  if test -x "${GDAL_CONFIG}"; then
142    AC_MSG_RESULT(yes)
143  else
144    AC_MSG_RESULT(no)
145    AC_MSG_ERROR([gdal-config not executable.])
146  fi
147fi
148
149
150AC_MSG_CHECKING(gdal-config usability)
151if test `${GDAL_CONFIG} --version`;
152then
153  GDAL_CPPFLAGS=`${GDAL_CONFIG} --cflags`
154  GDAL_LIBS=`${GDAL_CONFIG} --libs`
155  GDAL_VERSION=`${GDAL_CONFIG} --version`
156  GDAL_DEP_LIBS=`${GDAL_CONFIG} --dep-libs`
157  GDAL_DATADIR=`${GDAL_CONFIG} --datadir`
158  gdalok=yes
159  AC_MSG_RESULT(yes)
160else
161      AC_MSG_RESULT(no)
162	echo "Error: gdal-config not found"
163	echo "The gdal-config script distributed with GDAL could not be found."
164	echo "If you have not installed the GDAL libraries, you can"
165	echo "download the source from  http://www.gdal.org/"
166	echo "If you have installed the GDAL libraries, then make sure that"
167	echo "gdal-config is in your path. Try typing gdal-config at a"
168	echo "shell prompt and see if it runs. If not, use:"
169	echo " --configure-args='--with-gdal-config=/usr/local/bin/gdal-config'"
170	echo "with appropriate values for your installation."
171	echo ""
172
173	exit 1
174fi
175
176#AC_MSG_NOTICE([GDAL_LIBS is: ${GDAL_LIBS}])
177#AC_MSG_NOTICE([GDAL_DEP_LIBS is: ${GDAL_DEP_LIBS}])
178
179AC_MSG_NOTICE([GDAL: ${GDAL_VERSION}])
180#GDAL_VER_DOT=`echo $GDAL_VERSION | tr -d "."`
181GDAL_MAJOR=`echo $GDAL_VERSION | cut -f1 -d"."`
182GDAL_MINOR=`echo $GDAL_VERSION | cut -f2 -d"."`
183GDAL_PATCH=`echo $GDAL_VERSION | cut -f3 -d"."`
184
185#if test ${GDAL_MAJOR} = 2 -a test ${GDAL_MINOR} -ge 3 ; then
186if test ${GDAL_MAJOR} = 2 && test ${GDAL_MINOR} -ge 3 ; then
187  AC_MSG_CHECKING([C++11 support for GDAL >= 2.3.0])
188  if test ${HAVE_CXX11} = 1 ; then
189    AC_MSG_RESULT(yes)
190  else
191    AC_MSG_RESULT(no)
192    AC_MSG_ERROR([provide at least CXX11 compiler support for GDAL >= 2.3.0])
193  fi
194fi
195
196AC_MSG_CHECKING([GDAL version >= 1.11.4])
197if test ${GDAL_MAJOR} -gt 1 ; then
198  AC_MSG_RESULT(yes)
199else
200#  if test ${GDAL_MAJOR} = 1 -a test ${GDAL_MINOR} = 11 -a test ${GDAL_PATCH} -ge 4 ; then
201  if test ${GDAL_MAJOR} = 1 && test ${GDAL_MINOR} = 11 && test ${GDAL_PATCH} -ge 4 ; then
202    AC_MSG_RESULT(yes)
203  else
204    AC_MSG_RESULT(no)
205    AC_MSG_ERROR([upgrade GDAL to 1.11.4 or later])
206  fi
207fi
208
209
210AC_MSG_CHECKING([GDAL version <= 2.5 or >= 3.0])
211if test ${GDAL_MAJOR} -lt 2 || test ${GDAL_MAJOR} -gt 2; then
212  AC_MSG_RESULT(yes)
213else
214  if test ${GDAL_MAJOR} = 2; then
215    if test ${GDAL_MINOR} -le 5 ; then
216      AC_MSG_RESULT(yes)
217    else
218      AC_MSG_RESULT(no)
219      AC_MSG_ERROR([GDAL version out of bounds])
220    fi
221  fi
222fi
223
224
225INLIBS="${LIBS}"
226INCPPFLAGS="${CPPFLAGS}"
227INPKG_CPPFLAGS="${PKG_CPPFLAGS}"
228INPKG_LIBS="${PKG_LIBS}"
229
230AC_SUBST([PKG_CPPFLAGS], ["${INPKG_CPPFLAGS} ${GDAL_CPPFLAGS}"])
231AC_SUBST([PKG_LIBS], ["${INPKG_LIBS} ${GDAL_LIBS}"])
232
233# honor PKG_xx overrides
234# for CPPFLAGS we will superfluously double R's flags
235# since we'll set PKG_CPPFLAGS with this, but that shouldn't hurt
236CPPFLAGS="${INCPPFLAGS} ${PKG_CPPFLAGS}"
237
238
239NEED_DEPS=no
240LIBS="${INLIBS} ${PKG_LIBS}"
241[cat > gdal_test.cc <<_EOCONF
242#include <gdal.h>
243#ifdef __cplusplus
244extern "C" {
245#endif
246int main() {
247GDALAllRegister();
248}
249#ifdef __cplusplus
250}
251#endif
252_EOCONF]
253
254AC_MSG_CHECKING(GDAL: linking with --libs only)
255${CXX} ${CPPFLAGS} -o gdal_test gdal_test.cc ${LIBS} 2> errors.txt
256if test `echo $?` -ne 0 ; then
257gdalok=no
258AC_MSG_RESULT(no)
259else
260AC_MSG_RESULT(yes)
261fi
262
263if test "${gdalok}" = no; then
264AC_MSG_CHECKING(GDAL: linking with --libs and --dep-libs)
265LIBS="${LIBS} ${GDAL_DEP_LIBS}"
266gdalok=yes
267${CXX} ${CPPFLAGS} -o gdal_test gdal_test.cc ${LIBS} 2>> errors.txt
268if test `echo $?` -ne 0 ; then
269gdalok=no
270fi
271if test "${gdalok}" = yes; then
272    NEED_DEPS=yes
273    AC_MSG_RESULT(yes)
274else
275    AC_MSG_RESULT(no)
276fi
277fi
278
279if test "${gdalok}" = no; then
280   cat errors.txt
281   AC_MSG_NOTICE([Install failure: compilation and/or linkage problems.])
282   AC_MSG_ERROR([GDALAllRegister not found in libgdal.])
283fi
284
285rm -f gdal_test errors.txt gdal_test.cc
286
287AC_MSG_CHECKING(GDAL: gdal-config data directory readable)
288if test -d "${GDAL_DATADIR}" ; then
289  AC_MSG_RESULT(yes)
290else
291  AC_MSG_RESULT(no)
292  AC_MSG_CHECKING(GDAL: GDAL_DATA data directory readable)
293  if test -z ${GDAL_DATA} ; then
294    AC_MSG_ERROR([GDAL data directory not found.])
295  else
296    if test -d "${GDAL_DATA}" ; then
297      AC_MSG_RESULT(yes)
298      AC_SUBST([GDAL_DATADIR], [${GDAL_DATA}])
299    else
300      AC_MSG_RESULT(no)
301      AC_MSG_ERROR([GDAL data directory not found.])
302    fi
303  fi
304fi
305
306GDAL_GE_250="no"
307GDAL_MAJ_VER=`echo $GDAL_VERSION | cut -d "." -f1`
308GDAL_MOD_VER=`echo $GDAL_VERSION | cut -d "." -f2`
309if test "${GDAL_MAJ_VER}" = 2 ; then
310  if test "${GDAL_MOD_VER}" -ge 5 ; then
311    GDAL_GE_250="yes"
312  fi
313else
314  if test "${GDAL_MAJ_VER}" -ge 3 ; then
315    GDAL_GE_250="yes"
316  fi
317fi
318
319GDAL_DATA_TEST_FILE="${GDAL_DATADIR}/stateplane.csv"
320AC_MSG_CHECKING(GDAL: ${GDAL_DATADIR}/stateplane.csv readable)
321if test -r "${GDAL_DATA_TEST_FILE}" ; then
322  AC_MSG_RESULT(yes)
323else
324  AC_MSG_RESULT(no)
325#  if test "${GDAL_GE_250}" = no; then
326    AC_MSG_ERROR([stateplane.csv not found in GDAL data directory.])
327#  fi
328fi
329
330# PROJ.4
331
332# copied from sf/configure.ac
333
334PROJ_CONFIG="pkg-config proj"
335
336if `$PROJ_CONFIG --exists` ; then # pkg-config proj
337  AC_MSG_NOTICE([pkg-config proj exists, will use it])
338  proj_config_ok=yes
339else # pkg-config proj
340  AC_MSG_NOTICE([pkg-config proj not available])
341  echo "  set PKG_CONFIG_PATH to the directory containing proj.pc"
342  proj_config_ok=no
343fi # pkg-config proj
344
345PROJ_VERSION=""
346PROJ6=""
347
348if test "${proj_config_ok}" = yes; then # proj_config_ok
349  PROJ_VERSION=`${PROJ_CONFIG} --modversion`
350  AC_MSG_NOTICE([PROJ version: ${PROJ_VERSION}])
351
352  PROJ_VERSION_DOT=`echo ${PROJ_VERSION} | tr -d "."`
353  PROJV1=`echo "${PROJ_VERSION}" | cut -c 1`
354
355  if test "${PROJV1}" -ge 6; then # PROJ >= 6
356    PKG_CPPFLAGS="-DPROJ_H_API"
357    PROJ6="yes"
358  else # PROJ >= 6
359    AC_MSG_NOTICE([PROJ version < 6.0.0])
360    PKG_CPPFLAGS=""
361    PROJ6="no"
362  fi # PROJ >= 6
363
364else # proj_config_ok
365
366  AC_MSG_NOTICE([PROJ version not determined using pkg-config proj])
367
368fi # proj_config_ok
369
370
371AC_ARG_WITH([proj-include],
372    AS_HELP_STRING([--with-proj-include=DIR],[location of proj header files]),
373    [proj_include_path=$withval])
374if test [ -n "$proj_include_path" ] ; then # proj_include_path
375   proj_include_path="-I${proj_include_path}"
376   AC_SUBST([PKG_CPPFLAGS],["${PKG_CPPFLAGS} ${proj_include_path} ${INPKG_CPPFLAGS}"])
377  if test "${proj_config_ok}" = yes; then # proj_config_ok
378    AC_MSG_NOTICE([--with-proj-include= overrides pkg-config proj --cflags])
379  fi # proj_config_ok
380else # proj_include_path
381  if test "${proj_config_ok}" = yes; then # proj_config_ok
382    PROJ_INCLUDE_PATH=`${PROJ_CONFIG} --cflags`
383    proj_include_path=${PROJ_INCLUDE_PATH}
384    AC_SUBST([PKG_CPPFLAGS],["${PKG_CPPFLAGS} ${PROJ_INCLUDE_PATH} ${INPKG_CPPFLAGS}"])
385  fi # proj_config_ok
386fi # proj_include_path
387
388# honor PKG_xx overrides
389# for CPPFLAGS we will superfluously double R's flags
390# since we'll set PKG_CPPFLAGS with this, but that shouldn't hurt
391CPPFLAGS="${INCPPFLAGS} ${PKG_CPPFLAGS}"
392AC_MSG_NOTICE([PROJ CPP flags: ${PKG_CPPFLAGS}])
393PKG_LIBS=""
394PROJ_LIB_PATH=""
395
396# dnl ditto for a library path
397AC_ARG_WITH([proj-lib],
398    AS_HELP_STRING([--with-proj-lib=LIB_PATH],[the location of proj libraries]),
399               [proj_lib_path=$withval])
400if test [ -n "$proj_lib_path" ] ; then # proj_lib_path
401    AC_SUBST([PKG_LIBS], ["-L${proj_lib_path} ${INPKG_LIBS}"])
402  if test "${proj_config_ok}" = yes; then # proj_config_ok
403    AC_MSG_NOTICE([--with-proj-lib= overrides pkg-config proj --libs])
404  fi # proj_config_ok
405else # proj_lib_path
406  if test ${proj_config_ok} = "yes"; then # proj_config_ok
407    PROJ_LIB_PATH=`${PROJ_CONFIG} --libs`
408    AC_SUBST([PKG_LIBS], ["${PROJ_LIB_PATH} ${INPKG_LIBS}"])
409  else # proj_config_ok
410    AC_SUBST([PKG_LIBS], ["${INPKG_LIBS} -lproj"])
411  fi # proj_config_ok
412fi # proj_lib_path
413
414LIBS="${PKG_LIBS} ${LIBS}"
415AC_MSG_NOTICE([PROJ LIBS: ${PKG_LIBS}])
416
417
418AC_ARG_WITH([proj_api],
419    [  --with-proj_api[=ARG]        legacy proj_api.h while still available, or current proj.h (ARG="proj_api.h", "proj.h"(default))],,)
420
421AC_MSG_CHECKING([PROJ header API:])
422
423if test -z "$with_proj_api" ; then # with_proj_api
424  PROJ_API=""
425  if test "${PROJ6}" = "yes" ; then # PROJ6
426    PROJ_API="proj.h"
427  fi
428  if test "${PROJ6}" = "no" ; then # PROJ6
429    PROJ_API="proj_api.h"
430  fi
431else # with_proj_api
432  PROJ_API="$with_proj_api"
433fi # with_proj_api
434
435api_string_ok="no"
436if test -z "${PROJ_API}" ; then
437  api_string_ok="yes"
438fi
439if test "${PROJ_API}" = "proj.h" ; then
440  api_string_ok="yes"
441fi
442if test "${PROJ_API}" = "proj_api.h" ; then
443  api_string_ok="yes"
444fi
445
446if test ${api_string_ok} = "no" ; then # PROJ_API
447  AC_MSG_RESULT(no)
448  AC_MSG_ERROR([PROJ API string ${PROJ_API} not recognized.])
449else
450  AC_MSG_RESULT(yes)
451fi # PROJ_API
452
453if test -z ${PROJ_API} ; then # PROJ_API = ""
454  AC_MSG_NOTICE([API to be used as yet undetermined, searching ...])
455  PROJ_VERSION_H=""
456  LIBS="${INLIBS} ${PKG_LIBS}"
457# proj.h case
458[cat > proj_conf_test_proj.c <<_EOCONF
459#include <stdio.h>
460#include <stdlib.h>
461#include <proj.h>
462
463int main() {
464    printf("%d.%d.%d\n", PROJ_VERSION_MAJOR, PROJ_VERSION_MINOR, PROJ_VERSION_PATCH);
465    exit(0);
466}
467_EOCONF]
468
469  if test `${CC} ${CFLAGS} ${PKGCPPFLAGS} -o proj_conf_test_proj proj_conf_test_proj.c ${PKG_LIBS} 2> /dev/null; echo $?` = 0 ; then
470    PROJ_API="proj.h"
471    PROJ6="yes"
472    PROJ_VERSION_H=`./proj_conf_test_proj`
473    PROJ_VERSION_DOT=`echo ${PROJ_VERSION_H} | tr -d "."`
474rm -f proj_conf_test_proj.c proj_conf_test_proj
475  else
476[cat > proj_conf_test_proj_api.c <<_EOCONF
477#include <stdio.h>
478#include <stdlib.h>
479#include <proj_api.h>
480
481int main() {
482    printf("%d\n", PJ_VERSION);
483    exit(0);
484}
485_EOCONF]
486    if test `${CC} ${CFLAGS} ${PKG_CPPFLAGS} -o proj_conf_test_proj_api proj_conf_test_proj_api.c ${PKG_LIBS} 2> /dev/null; echo $?` = 0; then
487      PROJ_API="proj_api.h"
488      PROJ6="no"
489      PROJ_VERSION_H=`./proj_conf_test_proj_api`
490      PROJ_VERSION_DOT=`echo ${PROJ_VERSION_H} | tr -d "."`
491rm -f proj_conf_test_proj_api.c proj_conf_test_proj_api
492    fi
493  fi
494  if test -z "${PROJ_VERSION_H}"; then
495    AC_MSG_ERROR([API to be used not found])
496  else
497    AC_MSG_NOTICE([Using API: ${PROJ_API}])
498  fi
499fi # PROJ_API = ""
500
501dnl PROJ >= 6 contains C++ code, so AC_CHECK_LIB needs to be
502dnl with the C++ runtime (and rgdal is going to use C++).
503AC_LANG(C++)
504
505if test "${PROJ_API}" =  "proj.h" ; then # proj.h
506
507  if test ${PROJ6} = "yes"; then # PROJ6
508    projhok=yes
509    AC_CHECK_HEADER([proj.h],,projhok=no)
510    if test "${projhok}" = no; then # libprojok
511      AC_MSG_ERROR([proj.h not found in given locations.])
512    fi # libprojok
513    libprojok=yes
514    AC_CHECK_LIB([proj], [proj_context_create],,libprojok=no)
515    if test "${libprojok}" = no; then # libprojok
516      AC_MSG_ERROR([proj_context_create not found in libproj.])
517    fi # libprojok
518  fi # PROJ6
519
520fi # proj.h
521
522if test "${PROJ_API}" =  "proj_api.h"; then # proj_api.h
523
524LIBS="${INLIBS} ${PKG_LIBS}"
525AC_MSG_CHECKING(proj_api.h presence and usability)
526[cat > proj_conf_test1.c <<_EOCONF
527#include <stdio.h>
528#include <stdlib.h>
529#include <proj_api.h>
530
531int main() {
532    printf("%d\n", PJ_VERSION);
533    exit(0);
534}
535_EOCONF]
536
537  if test `${CC} ${CFLAGS} ${PKG_CPPFLAGS} -o proj_conf_test1 proj_conf_test1.c ${PKG_LIBS} 2> /dev/null; echo $?` = 0; then
538    PROJ_VERSION_H=`./proj_conf_test1`
539    AC_MSG_RESULT(yes)
540  else
541    if test `${CC} ${CFLAGS} ${PKG_CPPFLAGS} -DACCEPT_USE_OF_DEPRECATED_PROJ_API_H -o proj_conf_test1 proj_conf_test1.c ${PKG_LIBS} 2> /dev/null; echo $?` = 0; then
542      PROJ6="yes"
543      PKG_CPPFLAGS="${PKG_CPPFLAGS} -DACCEPT_USE_OF_DEPRECATED_PROJ_API_H"
544      PROJ_VERSION_H=`./proj_conf_test1`
545      AC_MSG_RESULT(yes)
546    else
547      AC_MSG_RESULT(no)
548      AC_MSG_ERROR([proj_api.h not found in standard or given locations.])
549    fi
550  fi
551
552rm -f proj_conf_test1.c proj_conf_test1
553
554#AC_MSG_NOTICE(["|${PROJ_VERSION}| |${PROJ_VERSION_H}|"])
555
556if test -n "${PROJ_VERSION}" ; then
557  AC_MSG_CHECKING([PROJ version agreement])
558  PROJ_VERSION_DOT=`echo ${PROJ_VERSION} | tr -d "."`
559#echo "|${PROJ_VERSION}| |${PROJ_VERSION_DOT}| |${PROJ_VERSION_H}|"
560  if test "${PROJ_VERSION_DOT}$" = "${PROJ_VERSION_H}$"; then
561    AC_MSG_RESULT(yes)
562  else
563    AC_MSG_RESULT(no)
564    AC_MSG_ERROR([proj_api.h and pkg-config give different versions])
565  fi
566fi
567
568fi
569
570if test -z "${PROJ_VERSION_DOT}"; then
571  PROJ_VERSION_DOT=`echo ${PROJ_VERSION_H} | tr -d "."`
572fi
573NEW_PROJ_WITH_OLD_GDAL="no"
574AC_MSG_CHECKING([Using GDAL < 3 with PROJ >= 6])
575#if test ${GDAL_MAJOR} -lt 3 -a test ${PROJ_VERSION_DOT} -ge 600; then
576if test ${GDAL_MAJOR} -lt 3 && test ${PROJ_VERSION_DOT} -ge 600; then
577    AC_MSG_RESULT(yes)
578    if test ${PROJ_API} = "proj.h"; then
579        AC_MSG_ERROR([GDAL >= 3 should be used with PROJ >= 6, use --with-proj_api="proj_api.h" for deprecated API])
580    else
581        NEW_PROJ_WITH_OLD_GDAL="yes"
582        AC_MSG_WARN([GDAL >= 3 should be used with PROJ >= 6])
583    fi
584  else
585    AC_MSG_RESULT(no)
586fi
587
588
589
590if test "${PROJ6}" = "yes" && test "${PROJ_API}" =  "proj_api.h"; then
591    PKG_CPPFLAGS="${PKG_CPPFLAGS} -DACCEPT_USE_OF_DEPRECATED_PROJ_API_H"
592fi
593#AC_MSG_NOTICE([CPP FLAGS: ${PKG_CPPFLAGS}])
594
595AC_MSG_CHECKING([PROJ version >= 4.8.0])
596if test ${PROJ_VERSION_DOT} -lt 480 ; then
597  AC_MSG_RESULT(no)
598  AC_MSG_ERROR([upgrade PROJ to 4.8.0 or later])
599else
600  AC_MSG_RESULT(yes)
601fi
602
603if test ${PROJ_VERSION_DOT} -lt 493; then
604
605if test ${PROJ_VERSION_DOT} = 480 ; then
606  ADDED_PKG_INCL_480=`pwd`"/inst/include"
607  AC_SUBST([PKG_CPPFLAGS],["${PKG_CPPFLAGS} -I${ADDED_PKG_INCL_480}"])
608  proj_include_path="${PKG_CPPFLAGS} -I${ADDED_PKG_INCL_480}"
609  CPPFLAGS="${INCPPFLAGS} ${PKG_CPPFLAGS}"
610fi
611
612#AC_MSG_NOTICE([${CFLAGS} ${PKG_CPPFLAGS}])
613
614AC_MSG_CHECKING(projects.h presence and usability)
615# FIXME re-using names
616[cat > proj_conf_test1a.c <<_EOCONF
617#include <stdio.h>
618#include <stdlib.h>
619#include <projects.h>
620
621int main() {
622    exit(0);
623}
624_EOCONF]
625
626if test `${CC} ${CFLAGS} ${PKG_CPPFLAGS} -o proj_conf_test1a proj_conf_test1a.c ${PKG_LIBS} 2> /dev/null; echo $?` = 0; then
627  AC_MSG_RESULT(yes)
628else
629  AC_MSG_RESULT(no)
630  STOP="stop"
631fi
632rm -f proj_conf_test1a.c proj_conf_test1a
633
634if test "$STOP" = "stop" ; then
635    echo "projects.h not found in standard or given locations."
636    echo "PROJ 4.8.0 was bundled wrongly, and did not provide"
637    echo "the file. A copy is provided in init/include in the source"
638    echo "package for the users to place in the same directory"
639    echo "as proj_api.h manually."
640    exit 1
641fi
642
643fi # < 493 test for projects.h
644
645#AC_MSG_NOTICE([CPP FLAGS: ${PKG_CPPFLAGS}])
646
647
648#AC_SUBST([PKG_CPPFLAGS],["-DP4CTX=1 ${PKG_CPPFLAGS}"])
649#AC_MSG_NOTICE([CPP FLAGS: ${PKG_CPPFLAGS}])
650
651AC_ARG_WITH([proj-share],
652    AS_HELP_STRING([--with-proj-share=SHARE_PATH],[the location of proj metadata files]),
653               [proj_share_path=$withval])
654if test [ -n "$proj_share_path" ] ; then
655    AC_MSG_NOTICE([PROJ_LIB: ${proj_share_path}])
656fi
657
658if test ${PROJ6} = "no"; then
659
660#AC_MSG_CHECKING(PROJ.6: )
661#AC_MSG_RESULT(no)
662
663[cat > proj_conf_test2.c <<_EOCONF
664#include <stdio.h>
665#include <stdlib.h>
666#include <proj_api.h>
667#if PJ_VERSION == 480
668FILE *pj_open_lib(projCtx, const char *, const char *);
669#endif
670
671int main() {
672#if PJ_VERSION == 480
673    FILE *fp;
674#else
675    PAFile fp;
676#endif
677    projCtx ctx;
678    ctx = pj_get_default_ctx();
679    fp = pj_open_lib(ctx, "epsg", "rb");
680    if (fp == NULL) exit(1);
681#if PJ_VERSION == 480
682    fclose(fp);
683#else
684    pj_ctx_fclose(ctx, fp);
685#endif
686    exit(0);
687}
688_EOCONF]
689
690${CC} ${CFLAGS} ${PKG_CPPFLAGS} -o proj_conf_test2 proj_conf_test2.c ${PKG_LIBS}
691if test [ -n "$proj_share_path" ] ; then
692  PROJ_LIB="${proj_share_path}" ./proj_conf_test2
693  proj_share=`echo $?`
694else
695  ./proj_conf_test2
696  proj_share=`echo $?`
697fi
698
699AC_MSG_CHECKING(PROJ.4: epsg found and readable)
700if test ${proj_share} -eq 1 ; then
701    AC_MSG_RESULT(no)
702    STOP="stop"
703else
704    AC_MSG_RESULT(yes)
705fi
706
707rm -f proj_conf_test2.c proj_conf_test2
708
709if test "$STOP" = "stop" ; then
710    echo "Error: proj/epsg not found"
711    echo "Either install missing proj support files, for example"
712    echo "the proj-nad and proj-epsg RPMs on systems using RPMs,"
713    echo "or if installed but not autodetected, set PROJ_LIB to the"
714    echo "correct path, and if need be use the --with-proj-share="
715    echo "configure argument."
716    exit 1
717fi
718
719else # test for new files if PROJ6
720
721[cat > proj_conf_test2.cpp <<_EOCONF
722#include <stdio.h>
723#include <stdlib.h>
724#include <proj.h>
725
726int main() {
727    PJ_CONTEXT *ctx = proj_context_create();
728    if (proj_context_get_database_path(ctx) == NULL) {
729        proj_context_destroy(ctx);
730        exit(1);
731    }
732    proj_context_destroy(ctx);
733    exit(0);
734}
735_EOCONF]
736
737dnl PROJ >= 6 needs C++ runtime and libsqlite3 (and perhaps more)
738dnl when statically linked.
739${CXX} ${CPPFLAGS} ${PKG_CPPFLAGS} -o proj_conf_test2 proj_conf_test2.cpp ${PKG_LIBS} -lproj -lsqlite3
740if test [ -n "$proj_share_path" ] ; then
741  PROJ_LIB="${proj_share_path}" ./proj_conf_test2
742  proj_share=`echo $?`
743else
744  ./proj_conf_test2
745  proj_share=`echo $?`
746fi
747
748AC_MSG_CHECKING(PROJ: proj.db found and readable)
749if test ${proj_share} -eq 1 ; then
750    AC_MSG_RESULT(no)
751    STOP="stop"
752else
753    AC_MSG_RESULT(yes)
754fi
755
756rm -f proj_conf_test2.cpp proj_conf_test2
757
758if test "$STOP" = "stop" ; then
759    echo "Error: proj/proj.db not found"
760    echo "Either install missing proj support files, set PROJ_LIB to the"
761    echo "correct path, and if need be use the --with-proj-share="
762    echo "configure argument."
763    exit 1
764fi
765
766fi # don't test for old files if PROJ6
767
768
769[cat > proj_conf_test3.c <<_EOCONF
770#include <stdio.h>
771#include <stdlib.h>
772#include <proj_api.h>
773#if PJ_VERSION == 480
774FILE *pj_open_lib(projCtx, const char *, const char *);
775#endif
776
777int main() {
778#if PJ_VERSION == 480
779    FILE *fp;
780#else
781    PAFile fp;
782#endif
783    projCtx ctx;
784    ctx = pj_get_default_ctx();
785    fp = pj_open_lib(ctx, "conus", "rb");
786    if (fp == NULL) exit(1);
787#if PJ_VERSION == 480
788    fclose(fp);
789#else
790    pj_ctx_fclose(ctx, fp);
791#endif
792    exit(0);
793}
794_EOCONF]
795
796if test ${PROJ6} = "no"; then
797 ${CC} ${CFLAGS} ${PKG_CPPFLAGS} -o proj_conf_test3 proj_conf_test3.c ${PKG_LIBS}
798 if test [ -n "$proj_share_path" ] ; then
799   PROJ_LIB="${proj_share_path}" ./proj_conf_test3
800   proj_share=`echo $?`
801 else
802   ./proj_conf_test3
803   proj_share=`echo $?`
804 fi
805
806 AC_MSG_CHECKING(PROJ.4: conus found and readable)
807 if test ${proj_share} -eq 1 ; then
808     WARN="warn"
809     AC_MSG_RESULT(no)
810 else
811     AC_MSG_RESULT(yes)
812 fi
813
814 if test "$WARN" = "warn" ; then
815     echo "Note: proj/conus not found"
816     echo "No support available in PROJ4 for NAD grid datum transformations"
817     echo "If required, consider re-installing from source with the contents"
818     echo "of proj-datumgrid-1.<latest>.zip from http://download.osgeo.org/proj/ in nad/."
819 fi
820fi
821
822rm -f proj_conf_test3.c proj_conf_test3
823
824# Optional local copy of GDAL datadir and PROJ_LIB
825
826data_copy=no
827if test "${PROJ_GDAL_DATA_COPY}" ; then
828    data_copy=yes
829    AC_MSG_NOTICE(["PROJ_GDAL_DATA_COPY used."])
830else
831    AC_ARG_WITH([data-copy],
832        AS_HELP_STRING([--with-data-copy=yes/no],[local copy of data directories in package, default no]),
833               [data_copy=$withval])
834fi
835if test "${data_copy}" = "yes" ; then
836AC_MSG_NOTICE([Copy data for:])
837  proj_lib0="${PROJ_LIB}"
838  AC_ARG_WITH([proj-data],
839    AS_HELP_STRING([--with-proj-data=DIR],[location of PROJ.4 data directory]),
840    [proj_lib1=$withval])
841  if test -n "${proj_lib0}" ; then
842    proj_lib="${proj_lib0}"
843  else
844    proj_lib="${proj_lib1}"
845  fi
846  if test -n "${proj_lib}" ; then
847    if test -d "${proj_lib}" ; then
848      cp -r "${proj_lib}" "${R_PACKAGE_DIR}"
849      AC_MSG_NOTICE([  PROJ: ${proj_lib}])
850    else
851      AC_MSG_ERROR([PROJ.4 data files not found; set environment variable PROJ_LIB=DIR or --with-proj-data=DIR.])
852    fi
853  else
854      AC_MSG_ERROR([PROJ.4 data files not found; set environment variable PROJ_LIB=DIR or --with-proj-data=DIR.])
855  fi
856
857  if test -d "${GDAL_DATADIR}" ; then
858    cp -r "${GDAL_DATADIR}" "${R_PACKAGE_DIR}"
859    AC_MSG_NOTICE([  GDAL: ${GDAL_DATADIR}])
860  else
861    AC_MSG_ERROR([GDAL data files not found.])
862  fi
863fi
864
865
866
867#AC_MSG_NOTICE([CPP FLAGS: ${PKG_CPPFLAGS}])
868
869# concluding substitution
870
871AC_SUBST([PKG_CPPFLAGS], ["${INPKG_CPPFLAGS} ${GDAL_CPPFLAGS}"])
872#AC_MSG_NOTICE([CPP FLAGS: ${PKG_CPPFLAGS}])
873if test [ -n "$proj_include_path" ] ; then
874#AC_MSG_NOTICE([INC_PATH: ${proj_include_path}])
875AC_SUBST([PKG_CPPFLAGS], ["${PKG_CPPFLAGS} ${proj_include_path}"])
876fi
877
878if test ${PROJ6} = "yes" ; then
879  if test ${PROJ_API} = "proj.h"; then
880    AC_SUBST([PKG_CPPFLAGS], ["${PKG_CPPFLAGS} -DPROJ_H_API"])
881  else
882    AC_SUBST([PKG_CPPFLAGS], ["${PKG_CPPFLAGS} -DACCEPT_USE_OF_DEPRECATED_PROJ_API_H"])
883  fi
884fi
885
886if test ${NEW_PROJ_WITH_OLD_GDAL} = "yes" ; then
887  AC_SUBST([PKG_CPPFLAGS], ["${PKG_CPPFLAGS} -DNEW_PROJ_WITH_OLD_GDAL"])
888fi
889
890AC_SUBST([PKG_LIBS], ["${INPKG_LIBS} ${GDAL_LIBS}"])
891if test "${NEED_DEPS}" = yes; then
892   AC_SUBST([PKG_LIBS], ["${PKG_LIBS} ${GDAL_DEP_LIBS}"])
893fi
894if test [ -n "$proj_lib_path" ] ; then
895AC_SUBST([PKG_LIBS], ["${PKG_LIBS} -L${proj_lib_path}"])
896fi
897if test [ -n "$PROJ_LIB_PATH" ] ; then
898AC_SUBST([PKG_LIBS], ["${PKG_LIBS} ${PROJ_LIB_PATH}"])
899else
900AC_SUBST([PKG_LIBS], ["${PKG_LIBS} -lproj"])
901fi
902AC_MSG_NOTICE([Package CPP flags: ${PKG_CPPFLAGS}])
903AC_MSG_NOTICE([Package LIBS: ${PKG_LIBS}])
904
905AC_CONFIG_FILES(src/Makevars)
906AC_OUTPUT
907
908