lalsuite_swig.m4 21.7 KB
Newer Older
1 2
# -*- mode: autoconf; -*-
# lalsuite_swig.m4 - SWIG configuration
3
# Author: Karl Wette, 2011--2017
4
#
5
# serial 104
6

Karl Wette's avatar
Karl Wette committed
7
AC_DEFUN([_LALSUITE_CHECK_SWIG_VERSION],[
8 9 10
  # $0: check the version of $1, and store it in ${swig_version}
  swig_version=
  swig_version_output=[`$1 -version 2>/dev/null`]
Karl Wette's avatar
Karl Wette committed
11 12 13
  AS_IF([test $? -eq 0],[
    swig_version_regex=['s|^ *SWIG [Vv]ersion \([0-9.][0-9.]*\)|\1|p;d']
    swig_version=[`echo "${swig_version_output}" | ${SED} -e "${swig_version_regex}"`]
14 15 16
  ])
  AS_IF([test "x${swig_version}" = x],[
    AC_MSG_ERROR([could not determine version of $1])
Karl Wette's avatar
Karl Wette committed
17 18 19 20
  ])
  # end $0
])

21 22 23 24 25 26 27 28 29 30
AC_DEFUN([_LALSUITE_SWIG_OUTPUT_DEPENDENCY_COMMANDS],[
  # $0: create dummy SWIG dependency files
  AS_IF([test -d swig && test -f swig/Makefile],[
    # create dummy SWIG dependency files
    _AS_ECHO_LOG([cd swig && ${SED} -e '/^include /d' Makefile | ${MAKE} -f - swig-dummy-depfiles])
    ( cd swig && ${SED} -e '/^include /d' Makefile | ${MAKE} -f - swig-dummy-depfiles )
  ])
  # end $0
])

Karl Wette's avatar
Karl Wette committed
31 32
AC_DEFUN([LALSUITE_ENABLE_SWIG],[
  # $0: enable SWIG bindings
33 34 35 36
  AC_ARG_ENABLE(
    [swig],
    AC_HELP_STRING(
      [--enable-swig],
Karl Wette's avatar
Karl Wette committed
37
      [generate SWIG bindings for all languages]
38
    ),[
39 40 41
      AS_CASE(["${enableval}"],
        [yes],[swig_build_all=true],
        [no],[swig_build_all=false],
42
        [AC_MSG_ERROR([invalid value "${enableval}" for --enable-swig])]
43
      )
44 45 46 47
    ],[
      swig_build_all=
    ]
  )
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
  AC_ARG_ENABLE(
    [swig_iface],
    AC_HELP_STRING(
      [--enable-swig-iface],
      [generate SWIG interface only]
    ),[
      AS_CASE(["${enableval}"],
        [yes],[swig_build_iface=true],
        [no],[swig_build_iface=false],
        [AC_MSG_ERROR([invalid value "${enableval}" for --enable-swig-iface])]
      )
    ],[
      swig_build_iface=false
    ]
  )
63 64 65 66
  LALSUITE_ENABLE_SWIG_LANGUAGE([Octave],[false],[
    # C++ is required to build Octave wrappings
    LALSUITE_REQUIRE_CXX
  ])
67
  LALSUITE_ENABLE_SWIG_LANGUAGE([Python],[true],[
68 69 70 71 72 73 74
    # Python is required to configure Python wrappings
    LALSUITE_REQUIRE_PYTHON([2.6])
  ])
  AS_IF([test "${swig_build_iface}" = true],[
    # Python is required to run generate_swig_iface.py
    LALSUITE_REQUIRE_PYTHON([2.6])
  ])
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
  AC_CONFIG_COMMANDS_PRE([
    # used to include SWIG dependency files into lalsuite_swig.am
    # - cannot use 'include' directly as it is interpreted by Automake, so instead
    #   use low-level @am__include@ and @am__quote@ set by AM_MAKE_INCLUDE()
    AC_SUBST([SWIG_DEPDIR],["./.swigdeps"])
    AS_IF([test "x${am__include+set}" != xset],[
      AC_MSG_ERROR([could not determine how to include SWIG dependency files])
    ])
    AS_IF([test "x${am__quote+set}" != xset],[
      AC_MSG_ERROR([could not determine how to quote SWIG dependency files])
    ])
    AC_SUBST([swig__depfile_include_pre],["${am__include} ${am__quote}"])
    AC_SUBST([swig__depfile_include_post],["${am__quote}"])
  ])
  _AC_CONFIG_COMMANDS_INIT([MAKE="${MAKE-make}"])
  m4_ifdef([_AM_OUTPUT_DEPENDENCY_COMMANDS],[
    m4_rename([_AM_OUTPUT_DEPENDENCY_COMMANDS],[lalsuite_swig__AM_OUTPUT_DEPENDENCY_COMMANDS])
    AC_DEFUN([_AM_OUTPUT_DEPENDENCY_COMMANDS],[
    {
      _LALSUITE_SWIG_OUTPUT_DEPENDENCY_COMMANDS
      lalsuite_swig__AM_OUTPUT_DEPENDENCY_COMMANDS
    }
    ])
  ],[
    m4_fatal([could not link SWIG automatic dependency tracking into Automake])
  ])
Karl Wette's avatar
Karl Wette committed
101
  # end $0
102
])
103

104
AC_DEFUN([LALSUITE_ENABLE_SWIG_LANGUAGE],[
Karl Wette's avatar
Karl Wette committed
105 106 107
  # $0: enable SWIG binding languages
  m4_pushdef([uppercase],m4_translit([$1],[a-z],[A-Z]))
  m4_pushdef([lowercase],m4_translit([$1],[A-Z],[a-z]))
108 109 110 111
  AC_ARG_ENABLE(
    [swig-]lowercase,
    AC_HELP_STRING(
      [--enable-swig-]lowercase,
112
      [generate SWIG bindings for $1]
113 114 115 116
    ),[
      AS_CASE(["${enableval}"],
        [yes],[swig_build_]lowercase[=true],
        [no],[swig_build_]lowercase[=false],
117
        [AC_MSG_ERROR([invalid value "${enableval}" for --enable-swig-]]lowercase[)]
118 119 120 121 122
      )
    ],[
      swig_build_]lowercase[=${swig_build_all:-$2}
    ]
  )
Karl Wette's avatar
Karl Wette committed
123
  AS_IF([test "${swig_build_]lowercase[}" = true],[
124
    swig_build_iface=true
Karl Wette's avatar
Karl Wette committed
125 126 127 128
    SWIG_BUILD_]uppercase[_ENABLE_VAL=ENABLED
    $3
  ],[
    SWIG_BUILD_]uppercase[_ENABLE_VAL=DISABLED
129
  ])
Karl Wette's avatar
Karl Wette committed
130 131
  AM_CONDITIONAL([SWIG_BUILD_]uppercase,[test "${swig_build_]lowercase[}" = true])
  m4_popdef([uppercase])
132
  m4_popdef([lowercase])
Karl Wette's avatar
Karl Wette committed
133
  # end $0
134 135 136
])

AC_DEFUN([LALSUITE_USE_SWIG],[
Karl Wette's avatar
Karl Wette committed
137
  # $0: configure enabled SWIG bindings
138
  AS_IF([test "${swig_build_iface}" = true],[
Karl Wette's avatar
Karl Wette committed
139

140
    # configure SWIG binding languages
141
    swig_min_version=2.0.12
142 143 144 145
    swig_min_version_info=""
    LALSUITE_USE_SWIG_OCTAVE
    LALSUITE_USE_SWIG_PYTHON

146 147
    # check for SWIG binary with version ${swig_min_version} or later;
    # use ${SWIG} if set, otherwise check common SWIG binary names
148
    AC_SUBST([SWIG])
149 150 151 152 153
    AS_IF([test "x${SWIG}" != x],[
      AC_MSG_CHECKING([if ${SWIG} version is at least ${swig_min_version}])
      _LALSUITE_CHECK_SWIG_VERSION([${SWIG}])
      LALSUITE_VERSION_COMPARE([${swig_version}],[<],[${swig_min_version}],[
        AC_MSG_RESULT([no (${swig_version})])
154 155
        AC_MSG_ERROR([[SWIG version ${swig_min_version} or later is required ${swig_min_version_info}
SWIG support can be disabled by using the --disable-swig configure option]])
156
      ])
157
      AC_MSG_RESULT([yes (${swig_version})])
Karl Wette's avatar
Karl Wette committed
158
    ],[
159
      AC_PATH_PROGS_FEATURE_CHECK([SWIG],[swig swig2.0 swig3.0],[
160 161 162 163 164 165
        AC_MSG_CHECKING([if ${ac_path_SWIG} version is at least ${swig_min_version}])
        _LALSUITE_CHECK_SWIG_VERSION([${ac_path_SWIG}])
        LALSUITE_VERSION_COMPARE([${swig_version}],[>=],[${swig_min_version}],[
          ac_path_SWIG_found=true
          AC_MSG_RESULT([yes (${swig_version})])
          ac_cv_path_SWIG="${ac_path_SWIG}"
166
        ],[
167 168
          ac_path_SWIG_found=false
          AC_MSG_RESULT([no (${swig_version})])
169
        ])
170
      ],[
171 172
        AC_MSG_ERROR([[SWIG version ${swig_min_version} or later is required ${swig_min_version_info}
SWIG support can be disabled by using the --disable-swig configure option]])
173
      ])
174
      SWIG="${ac_cv_path_SWIG}"
175 176
    ])

177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
    # check if SWIG works with ccache
    ccache_swig_env="CCACHE_CPP2=1"
    AC_MSG_CHECKING([if ${SWIG} works with ${ccache_swig_env}])
    echo '%module conftest;' > conftest-swig.i
    env_ccache_swig_cmd="env ${ccache_swig_env} ${SWIG} -includeall -ignoremissing -xml -xmllite -MP -MD -MF conftest-swig.deps -o conftest-swig.xml conftest-swig.i"
    _AS_ECHO_LOG([${env_ccache_swig_cmd}])
    ${env_ccache_swig_cmd} >&AS_MESSAGE_LOG_FD 2>&AS_MESSAGE_LOG_FD
    result=$?
    _AS_ECHO_LOG([\$? = ${result}])
    AS_IF([test ${result} -eq 0],[
      AC_MSG_RESULT([yes])
    ],[
      AC_MSG_RESULT([no])
      ccache_swig_env="CCACHE_DISABLE=1"
    ])
    SWIG="env ${ccache_swig_env} ${SWIG}"
    rm -f conftest-swig.i conftest-swig.deps conftest-swig.xml

Karl Wette's avatar
Karl Wette committed
195 196 197 198 199
    # extract -I and -D flags from LALSuite library preprocessor flags
    AC_SUBST([SWIG_CPPFLAGS],[])
    for flag in ${CPPFLAGS}; do
      AS_CASE([${flag}],
        [-I*|-D*],[SWIG_CPPFLAGS="${SWIG_CPPFLAGS} ${flag}"]
200 201
      )
    done
Karl Wette's avatar
Karl Wette committed
202 203 204 205 206 207

    # extract -L flags from LALSuite library linker flags
    AC_SUBST([SWIG_LDFLAGS],[])
    for flag in ${LDFLAGS}; do
      AS_CASE([${flag}],
        [-L*],[SWIG_LDFLAGS="${SWIG_LDFLAGS} ${flag}"]
208 209
      )
    done
210

Karl Wette's avatar
Karl Wette committed
211 212 213
    # determine various libtool parameters
    AC_SUBST([SWIG_LTLIBDIR],["${objdir}"])
    AC_SUBST([SWIG_SOEXT],[`module=yes; eval shrext=\"${shrext_cmds}\"; echo ${shrext}`])
214

Karl Wette's avatar
Karl Wette committed
215 216 217 218 219 220 221 222
    # substitute list of LALSuite bindings that these bindings depend on
    AC_MSG_CHECKING([for LALSuite binding dependencies])
    AC_SUBST([SWIG_DEPENDENCIES],[])
    for arg in ${lalsuite_libs}; do
      AS_CASE([${arg}],
        [lalsupport],[:],
        [SWIG_DEPENDENCIES="${SWIG_DEPENDENCIES} ${arg}"]
      )
223
    done
Karl Wette's avatar
Karl Wette committed
224 225
    AS_IF([test "x${SWIG_DEPENDENCIES}" = x],[
      AC_MSG_RESULT([none])
226
    ],[
Karl Wette's avatar
Karl Wette committed
227
      AC_MSG_RESULT([${SWIG_DEPENDENCIES}])
228 229
    ])

230
  ])
231
  AM_CONDITIONAL([SWIG_BUILD],[test "${swig_build_iface}" = true])
232 233 234 235

  # run SWIG binding language post-actions
  _LALSUITE_USE_SWIG_POST_ACTIONS

Karl Wette's avatar
Karl Wette committed
236
  # end $0
237 238
])

239 240
m4_define([_LALSUITE_USE_SWIG_POST_ACTIONS],[])

241
AC_DEFUN([LALSUITE_USE_SWIG_LANGUAGE],[
Karl Wette's avatar
Karl Wette committed
242 243 244 245
  # $0: configure SWIG binding languages
  m4_pushdef([uppercase],m4_translit([$1],[a-z],[A-Z]))
  m4_pushdef([lowercase],m4_translit([$1],[A-Z],[a-z]))
  AS_IF([test "${swig_build_]lowercase[}" = true],[
246
    AC_LANG_PUSH([$2])
Karl Wette's avatar
Karl Wette committed
247
    swig_build=true
248
    $3
249
    AC_LANG_POP([$2])
250
  ])
251 252 253 254 255
  m4_append([_LALSUITE_USE_SWIG_POST_ACTIONS],[
    AS_IF([test "${swig_build_]]lowercase[[}" = true],[
      $4
    ])
  ])
256 257
  m4_popdef([uppercase])
  m4_popdef([lowercase])
Karl Wette's avatar
Karl Wette committed
258
  # end $0
259 260
])

261
AC_DEFUN([LALSUITE_USE_SWIG_OCTAVE],[
Karl Wette's avatar
Karl Wette committed
262
  # $0: configure SWIG Octave bindings
263
  LALSUITE_USE_SWIG_LANGUAGE([Octave],[C++],[
264

265
    # check for GSL, needed for LAL complex number support in C++
266 267 268 269
    PKG_CHECK_MODULES([GSL],[gsl],[true],[false])
    LALSUITE_ADD_FLAGS([C],[${GSL_CFLAGS}],[${GSL_LIBS}])
    AC_CHECK_HEADERS([gsl/gsl_complex.h],,[AC_MSG_ERROR([could not find the gsl/gsl_complex.h header])])

Karl Wette's avatar
Karl Wette committed
270
    # check for Octave
271
    AC_PATH_PROGS(OCTAVE,[octave-cli octave],[],[])
272
    AS_IF([test "x${OCTAVE}" = x],[
Karl Wette's avatar
Karl Wette committed
273
      AC_MSG_ERROR([could not find octave in PATH])
274 275
    ])

Karl Wette's avatar
Karl Wette committed
276 277 278 279 280 281 282 283 284 285 286
    # check for Octave utilities octave-config and mkoctfile
    octave_dir=`AS_DIRNAME(["${OCTAVE}"])`
    AC_MSG_CHECKING([for octave-config])
    octave_cfg="${octave_dir}/octave-config"
    AS_IF([test -x "${octave_cfg}"],[
      AC_MSG_RESULT([${octave_cfg}])
    ],[
      AC_MSG_RESULT([not found])
      AC_MSG_ERROR([could not find octave-config in ${octave_dir}])
    ])
    octave_cfg="env - ${octave_cfg}"
287
    AC_MSG_CHECKING([for mkoctfile])
Karl Wette's avatar
Karl Wette committed
288 289 290
    mkoctfile="${octave_dir}/mkoctfile"
    AS_IF([test -x "${mkoctfile}"],[
      AC_MSG_RESULT([${mkoctfile}])
291
    ],[
Karl Wette's avatar
Karl Wette committed
292 293
      AC_MSG_RESULT([not found])
      AC_MSG_ERROR([could not find mkoctfile in ${octave_dir}])
294
    ])
Karl Wette's avatar
Karl Wette committed
295
    mkoctfile="env - ${mkoctfile}"
296 297 298 299

    # check Octave version
    octave_min_version=3.2.0
    AC_MSG_CHECKING([${OCTAVE} version])
Karl Wette's avatar
Karl Wette committed
300
    octave_version=[`${octave_cfg} -p VERSION 2>/dev/null`]
301
    AS_IF([test "x${octave_version}" = x],[
302
      AC_MSG_ERROR([could not determine ${OCTAVE} version])
303 304
    ])
    AC_MSG_RESULT([${octave_version}])
305
    LALSUITE_VERSION_COMPARE([${octave_version}],[<],[${octave_min_version}],[
306 307
      AC_MSG_ERROR([Octave version ${octave_min_version} or later is required])
    ])
308
    LALSUITE_VERSION_COMPARE([${octave_version}],[>=],[4.0.0],[
309 310 311 312
      LALSUITE_VERSION_COMPARE([${swig_min_version}],[<],[3.0.7],[
        swig_min_version=3.0.7
        swig_min_version_info="for Octave version ${octave_version}"
      ])
313
    ])
314 315 316 317 318 319
    LALSUITE_VERSION_COMPARE([${octave_version}],[>=],[4.2.0],[
      LALSUITE_VERSION_COMPARE([${swig_min_version}],[<],[3.0.12],[
        swig_min_version=3.0.12
        swig_min_version_info="for Octave version ${octave_version}"
      ])
    ])
320 321 322 323 324 325
    LALSUITE_VERSION_COMPARE([${octave_version}],[>=],[4.4.0],[
      LALSUITE_VERSION_COMPARE([${swig_min_version}],[<],[4.0.0],[
        swig_min_version=4.0.0
        swig_min_version_info="for Octave version ${octave_version}"
      ])
    ])
326

327 328 329 330
    # determine where to install Octave bindings: take versioned site .oct file
    # directory given by octave-config, and strip off prefix; thus, if LALSuite
    # is installed in the same directory as Octave, .oct files will be found by
    # Octave without having to add to OCTAVE_PATH
Karl Wette's avatar
Karl Wette committed
331
    AC_MSG_CHECKING([${OCTAVE} .oct installation directory])
332 333
    # at least in Debian Buster, "HOME" has been dropped in favour of "OCTAVE_HOME"
    octave_prefix=[`${octave_cfg} -p OCTAVE_HOME 2>/dev/null | ${SED} -e 's|/*$||'`]
334 335 336 337
    # fallback if OCTAVE_HOME is yet unknown
    AS_IF([test "x${octave_prefix}" = x],[
      octave_prefix=[`${octave_cfg} -p PREFIX 2>/dev/null | ${SED} -e 's|/*$||'`]
    ])                                                                                                                                                                                                                                                            
Karl Wette's avatar
Karl Wette committed
338 339 340 341 342 343 344 345 346
    octexecdir=[`${octave_cfg} -p LOCALVEROCTFILEDIR 2>/dev/null | ${SED} -e 's|/*$||'`]
    octexecdir=[`echo ${octexecdir} | ${SED} -e "s|^${octave_prefix}/||"`]
    AS_IF([test "x`echo ${octexecdir} | ${SED} -n -e '\|^/|p'`" != x],[
      AC_MSG_ERROR([could not build relative path from "${octexecdir}"])
    ])
    octexecdir='${prefix}'/"${octexecdir}"
    AC_MSG_RESULT([${octexecdir}])
    AC_SUBST([octexecdir])

347
    # determine C++ compiler used to compile Octave itself
348
    AC_MSG_CHECKING([C++ compiler used for building ${OCTAVE}])
Karl Wette's avatar
Karl Wette committed
349
    octave_CXX=`${mkoctfile} -p CXX 2>/dev/null`
350
    AC_MSG_RESULT([${octave_CXX}])
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388

    # check that configured C++ compiler is compatible with C++ compiler used to
    # compile Octave itself, i.e. that both compilers link against compatible C++
    # libraries (e.g. libstdc++ vs libc++).
    AC_MSG_CHECKING([configured C++ compiler "${CXX}" is compatible with ${OCTAVE} C++ compiler "${octave_CXX}"])
    AS_IF([test "x${build_vendor}" = xapple && otool --version >/dev/null 2>&1],[
      print_shared_libs="otool -L"
    ],[ldd --version >/dev/null 2>&1],[
      print_shared_libs="ldd"
    ],[
      AC_MSG_ERROR([could not determine tool to print shared library dependencies])
    ])
    swig_save_CXX=${CXX}
    LALSUITE_PUSH_UVARS
    LALSUITE_CLEAR_UVARS
    m4_foreach([cxxloop],[CXX,octave_CXX],[
      CXX=${cxxloop}
      AC_LINK_IFELSE([
        AC_LANG_SOURCE([[
#include <string>
int main() { std::string s = "a"; return 0; }
        ]])
      ],[
        print_shared_libs_regex=["\|conftest${EXEEXT}|d;"'s|(0x[^)]*)||g;s|^ *||g;s| *$||g']
        ${print_shared_libs} conftest${EXEEXT} | ${SED} -e "${print_shared_libs_regex}" | sort > conftest_lalsuite_swig_[]cxxloop[]_shared_libs
        echo "${as_me}:${as_lineno-$LINENO}:${CXX} shared libraries:" >&AS_MESSAGE_LOG_FD
        ${SED} -e ["s/^/${as_me}:${as_lineno-$LINENO}:/"] conftest_lalsuite_swig_[]cxxloop[]_shared_libs >&AS_MESSAGE_LOG_FD
      ],[
        AC_MSG_ERROR([could not link using ${CXX}])
      ])
    ])
    LALSUITE_POP_UVARS
    CXX=${swig_save_CXX}
    AS_IF([diff conftest_lalsuite_swig_CXX_shared_libs conftest_lalsuite_swig_octave_CXX_shared_libs >/dev/null 2>&1],[
      AC_MSG_RESULT([yes])
    ],[
      AC_MSG_RESULT([no])
      AC_MSG_ERROR([configured C++ compiler "${CXX}" is incompatible with ${OCTAVE} C++ compiler "${octave_CXX}"])
389
    ])
390

Karl Wette's avatar
Karl Wette committed
391 392
    # determine Octave preprocessor flags
    AC_SUBST([SWIG_OCTAVE_CPPFLAGS],[])
393
    AC_SUBST([SWIG_OCTAVE_CPPFLAGS_IOCTAVE],[])
394
    for arg in CPPFLAGS INCFLAGS; do
395 396 397 398 399 400
      for flag in `${mkoctfile} -p ${arg} 2>/dev/null`; do
        AS_CASE([${flag}],
          [-I*/octave],[SWIG_OCTAVE_CPPFLAGS_IOCTAVE="${flag}"],
          [SWIG_OCTAVE_CPPFLAGS="${SWIG_OCTAVE_CPPFLAGS} ${flag}"]
        )
      done
401
    done
Karl Wette's avatar
Karl Wette committed
402 403 404

    # determine Octave compiler flags
    AC_SUBST([SWIG_OCTAVE_CXXFLAGS],[])
405
    swig_octave_cxxflags=
406
    for arg in CXX CXXPICFLAG ALL_CXXFLAGS; do
407
      for flag in `${mkoctfile} -p ${arg} 2>/dev/null`; do
408 409 410
        AS_CASE([${flag}],
          [-*],[swig_octave_cxxflags="${swig_octave_cxxflags} ${flag}"]
        )
411
      done
Karl Wette's avatar
Karl Wette committed
412
    done
413 414
    LALSUITE_CHECK_COMPILE_FLAGS([
      ${swig_octave_cxxflags}
415 416 417 418 419
      -Wno-uninitialized
      -Wno-unused-variable
      -Wno-unused-but-set-variable
      -Wno-format-extra-args
      -Wno-tautological-compare
420
      -Wno-deprecated-declarations
421 422 423
      -fno-strict-aliasing
      -O0
      -Wp[,]-U_FORTIFY_SOURCE
424 425
      ],[SWIG_OCTAVE_CXXFLAGS="${SWIG_OCTAVE_CXXFLAGS} ${flag}"]
    )
Karl Wette's avatar
Karl Wette committed
426 427 428

    # determine Octave linker flags
    AC_SUBST([SWIG_OCTAVE_LDFLAGS],[])
429
    swig_octave_ldflags=
Karl Wette's avatar
Karl Wette committed
430
    for arg in LFLAGS LIBOCTINTERP LIBOCTAVE LIBCRUFT OCT_LINK_OPTS OCT_LINK_DEPS; do
431 432 433
      for flag in `${mkoctfile} -p ${arg} 2>/dev/null`; do
        AS_CASE([${flag}],
          [-L/usr/lib|-L/usr/lib64],[:],
434 435
          [-Xlinker],[swig_octave_ldflags="${swig_octave_ldflags}-Wl,"],
          [swig_octave_ldflags="${swig_octave_ldflags}${flag} "]
436 437
        )
      done
438
    done
439 440 441 442
    LALSUITE_CHECK_LINK_FLAGS([
      ${swig_octave_ldflags}
      ],[SWIG_OCTAVE_LDFLAGS="${SWIG_OCTAVE_LDFLAGS} ${flag}"]
    )
443 444

    # check for Octave headers
Karl Wette's avatar
Karl Wette committed
445
    LALSUITE_PUSH_UVARS
446
    CPPFLAGS="${SWIG_OCTAVE_CPPFLAGS_IOCTAVE} ${SWIG_OCTAVE_CPPFLAGS}"
447
    CXXFLAGS="${SWIG_OCTAVE_CXXFLAGS}"
448
    AC_CHECK_HEADER([octave/oct.h],[],[
449 450 451
      AC_MSG_ERROR([could not find the header "octave/oct.h"])
    ],[
      AC_INCLUDES_DEFAULT
452
    ])
Karl Wette's avatar
Karl Wette committed
453
    LALSUITE_POP_UVARS
454

455 456 457 458 459
  ],[

    # determine SWIG Octave flags
    AC_SUBST([SWIG_OCTAVE_FLAGS],["-globals . -DSWIG_CPLUSPLUS_CAST"])

460
  ])
Karl Wette's avatar
Karl Wette committed
461
  # end $0
462
])
463

464
AC_DEFUN([LALSUITE_USE_SWIG_PYTHON],[
Karl Wette's avatar
Karl Wette committed
465
  # $0: configure SWIG Python bindings
466
  LALSUITE_USE_SWIG_LANGUAGE([Python],[C],[
467

468 469 470 471 472 473 474 475 476 477 478 479 480
    # check Python version
    AC_MSG_CHECKING([${PYTHON} version])
    AS_IF([test "x${PYTHON_VERSION}" = x],[
      AC_MSG_ERROR([could not determine ${PYTHON} version])
    ])
    AC_MSG_RESULT([${PYTHON_VERSION}])
    LALSUITE_VERSION_COMPARE([${PYTHON_VERSION}],[>=],[3.0.0],[
      LALSUITE_VERSION_COMPARE([${swig_min_version}],[<],[3.0.9],[
        swig_min_version=3.0.9
        swig_min_version_info="for Python version ${PYTHON_VERSION}"
      ])
    ])

481 482 483 484 485
    # check for distutils
    AC_MSG_CHECKING([for distutils])
    cat <<EOD | ${PYTHON} - 2>/dev/null
import distutils
EOD
486
    AS_IF([test $? -ne 0],[
487
      AC_MSG_ERROR([could not import distutils])
488 489 490
    ])
    AC_MSG_RESULT([yes])

491 492 493 494 495 496 497 498 499 500 501 502 503 504
    # check for NumPy
    numpy_min_version=1.3
    AC_MSG_CHECKING([for NumPy])
    numpy_version=[`cat <<EOD | ${PYTHON} - 2>/dev/null
import numpy
print(numpy.__version__)
EOD`]
    AS_IF([test $? -ne 0],[
      AC_MSG_ERROR([could not import NumPy])
    ])
    AC_MSG_RESULT([yes])

    # check NumPy version
    AC_MSG_CHECKING([NumPy version])
505
    LALSUITE_VERSION_COMPARE([${numpy_version}],[<],[${numpy_min_version}],[
506
      AC_MSG_ERROR([NumPy version ${numpy_min_version} or later is required])
507 508 509
    ])
    AC_MSG_RESULT([${numpy_version}])

Karl Wette's avatar
Karl Wette committed
510 511 512
    # determine Python preprocessor flags
    AC_SUBST([SWIG_PYTHON_CPPFLAGS],[])
    python_out=[`cat <<EOD | ${PYTHON} - 2>/dev/null
513 514 515 516 517 518 519 520
import sys
import distutils.sysconfig as cfg
import numpy.lib.utils as npyutil
sys.stdout.write( '-I' + cfg.get_python_inc())
sys.stdout.write(' -I' + cfg.get_python_inc(plat_specific=1))
sys.stdout.write(' -I' + npyutil.get_include())
EOD`]
    AS_IF([test $? -ne 0],[
Karl Wette's avatar
Karl Wette committed
521
      AC_MSG_ERROR([could not determine Python preprocessor flags])
522
    ])
523 524 525
    for flag in ${python_out}; do
      SWIG_PYTHON_CPPFLAGS="${SWIG_PYTHON_CPPFLAGS} ${flag}"
    done
526

Karl Wette's avatar
Karl Wette committed
527 528 529
    # determine Python compiler flags
    AC_SUBST([SWIG_PYTHON_CFLAGS],[])
    python_out=[`cat <<EOD | ${PYTHON} - 2>/dev/null
530 531 532 533 534 535 536
import sys
import distutils.sysconfig as cfg
cflags = cfg.get_config_var('CFLAGS').split()
cflags = [f for f in cflags if f != '-DNDEBUG']
sys.stdout.write(" ".join(cflags))
EOD`]
    AS_IF([test $? -ne 0],[
Karl Wette's avatar
Karl Wette committed
537 538
      AC_MSG_ERROR([could not determine Python compiler flags])
    ])
539
    for flag in ${python_out}; do
540
      swig_python_cflags="${swig_python_cflags} ${flag}"
541
    done
542 543
    LALSUITE_CHECK_COMPILE_FLAGS([
      ${swig_python_cflags}
544 545 546 547 548 549
      -Wno-uninitialized
      -Wno-unused-variable
      -Wno-unused-but-set-variable
      -Wno-format-extra-args
      -Wno-tautological-compare
      -fno-strict-aliasing
550 551
      ],[SWIG_PYTHON_CFLAGS="${SWIG_PYTHON_CFLAGS} ${flag}"]
    )
Karl Wette's avatar
Karl Wette committed
552 553 554 555 556 557

    # determine Python linker flags
    AC_SUBST([SWIG_PYTHON_LDFLAGS],[])
    python_out=[`cat <<EOD | ${PYTHON} - 2>/dev/null
import sys, os
import distutils.sysconfig as cfg
558
sys.stdout.write(cfg.get_config_var('LDFLAGS'))
Karl Wette's avatar
Karl Wette committed
559 560 561 562 563 564 565
sys.stdout.write(' -L' + cfg.get_python_lib())
sys.stdout.write(' -L' + cfg.get_python_lib(plat_specific=1))
sys.stdout.write(' -L' + cfg.get_python_lib(plat_specific=1,standard_lib=1))
sys.stdout.write(' -L' + cfg.get_config_var('LIBDIR'))
EOD`]
    AS_IF([test $? -ne 0],[
      AC_MSG_ERROR([could not determine Python linker flags])
566
    ])
567
    swig_python_ldflags=
568 569 570
    for flag in ${python_out}; do
      AS_CASE([${flag}],
        [-L/usr/lib|-L/usr/lib64],[:],
571 572
        [-Xlinker],[swig_python_ldflags="${swig_python_ldflags}-Wl,"],
        [swig_python_ldflags="${swig_python_ldflags}${flag} "]
573 574
      )
    done
575 576 577 578
    LALSUITE_CHECK_LINK_FLAGS([
      ${swig_python_ldflags}
      ],[SWIG_PYTHON_LDFLAGS="${SWIG_PYTHON_LDFLAGS} ${flag}"]
    )
579

580 581 582
    # allow addition of extra Python linker flags
    AC_ARG_VAR([EXTRA_SWIG_PYTHON_LDFLAGS],[Extra linker flags for SWIG Python bindings])

Karl Wette's avatar
Karl Wette committed
583 584 585
    # check for Python and NumPy headers
    LALSUITE_PUSH_UVARS
    CPPFLAGS="${SWIG_PYTHON_CPPFLAGS}"
586
    AC_CHECK_HEADER([Python.h],[],[
587 588 589 590
      AC_MSG_ERROR([could not find the header "Python.h"])
    ],[
      AC_INCLUDES_DEFAULT
    ])
591
    AC_CHECK_HEADER([numpy/arrayobject.h],[],[
592 593 594 595 596
      AC_MSG_ERROR([could not find the header "numpy/arrayobject.h"])
    ],[
      AC_INCLUDES_DEFAULT
      #include <Python.h>
    ])
Karl Wette's avatar
Karl Wette committed
597
    LALSUITE_POP_UVARS
598

599
    # remove deprecated code in NumPy API >= 1.7
Karl Wette's avatar
Karl Wette committed
600
    SWIG_PYTHON_CPPFLAGS="${SWIG_PYTHON_CPPFLAGS} -DNPY_NO_DEPRECATED_API=NPY_1_7_API_VERSION"
601 602

    # check for declarations which may need compatibility code for NumPy API < 1.7
Karl Wette's avatar
Karl Wette committed
603 604
    LALSUITE_PUSH_UVARS
    CPPFLAGS="${SWIG_PYTHON_CPPFLAGS}"
605 606 607 608 609 610 611 612 613 614
    AC_CHECK_DECL([NPY_ARRAY_WRITEABLE],[
      SWIG_PYTHON_CPPFLAGS="${SWIG_PYTHON_CPPFLAGS} -DSWIGLAL_HAVE_NPY_ARRAY_WRITEABLE"
    ],[],[
      AC_INCLUDES_DEFAULT
      #include <Python.h>
      #include <numpy/arrayobject.h>
    ])
    AC_CHECK_DECL([PyArray_SetBaseObject],[
      SWIG_PYTHON_CPPFLAGS="${SWIG_PYTHON_CPPFLAGS} -DSWIGLAL_HAVE_PyArray_SetBaseObject"
    ],[],[
615 616 617 618
      AC_INCLUDES_DEFAULT
      #include <Python.h>
      #include <numpy/arrayobject.h>
    ])
Karl Wette's avatar
Karl Wette committed
619
    LALSUITE_POP_UVARS
620

621 622 623 624 625 626 627 628 629 630 631 632
  ],[

    # determine SWIG Python flags
    AC_SUBST([SWIG_PYTHON_FLAGS],["-O -builtin -globals globalvar"])
    AC_MSG_CHECKING([if SWIG supports relative Python imports])
    LALSUITE_VERSION_COMPARE([${swig_version}],[<],[3.0.0],[
      AC_MSG_RESULT([no])
    ],[
      AC_MSG_RESULT([yes])
      SWIG_PYTHON_FLAGS="-py3 -relativeimport ${SWIG_PYTHON_FLAGS}"
    ])

633
  ])
Karl Wette's avatar
Karl Wette committed
634
  # end $0
635
])