# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4

PortSystem    1.0
PortGroup     compiler_blacklist_versions 1.0

name          lal
version       7.0.0
revision      7

description   LSC Algorithm Library
long_description \
  LIGO Scientific Collaboration Algorithm Library containing core \
  routines for gravitational wave data analysis.

checksums     rmd160  9f5d23c4d2171ffa533af204b5cdcd3d12068c43 \
              sha256  83a92377285ca0a2e6e48ae5ef8ad96a15dde62dc919f85ae93512dc942f860f \
              size    1713536

depends_lib-append \
              port:gsl \
              port:fftw-3 \
              port:fftw-3-single \
              port:hdf5

# add Python dependencies for python subports
if {[string match "py*" ${subport}]} {
    set numbers [regexp -all -inline -- {[0-9]+} ${subport}]
    set v [lindex ${numbers} 0]
    # python, C-library port, and numpy are automatically added below
    depends_lib     port:py${v}-dateutil port:py${v}-scipy port:py${v}-six \
                    port:py${v}-ligo-segments port:py${v}-lscsoft-glue
}

# add Octave dependencies for octave subports
if {[string match "octave-*" ${subport}]} {
    # octave, and C-library port are automatically added below
}

###############################################################################
### Everything below this line is common to all LALSuite ports. ###############
###############################################################################

categories    science
platforms     darwin
maintainers   {aronnax @lpsinger}
license       GPL-2+

# The GPL and OpenSSL licenses conflict with each other, and our build
# dependency on Python results in an indirect dependency on OpenSSL.
# However, there is no real conflict in the case of LALSuite because Python
# is used (1) as an interpreter to run part of the build and (2) as a
# separately installed interpreter that is invoked when the user runs some of
# our scripts.
license_noconflict openssl

homepage      https://wiki.ligo.org/Computing/DASWG/LALSuite
master_sites  https://software.igwn.org/lscsoft/source/lalsuite
use_xz        yes

depends_build-append \
              port:pkgconfig \
              port:swig \

patchfiles          dynamic_lookup-11.patch

# Choose a compiler that supports OpenMP.
compiler.openmp_version 2.5

configure.args-append \
               --mandir=${prefix}/share/man \
               --disable-silent-rules \
               --disable-gcc-flags \
               --enable-swig-iface \
               --disable-swig-octave \
               --disable-swig-python \
               --disable-python

test.run      yes
test.target   check

compiler.cxx_standard  2011
configure.cxxflags-append -std=c++11
compiler.blacklist-append {clang < 700}

# Create subports for each supported Python version
foreach v {27 37 38} {
  subport py${v}-${name} {
    set pythonversion     [string index ${v} 0].[string range ${v} 1 end]
    set pythonhome \
      "${frameworks_dir}/Python.framework/Versions/${pythonversion}"
    set pythondir "${pythonhome}/lib/python${pythonversion}/site-packages"

    categories-prepend    python
    description           Python ${pythonversion} bindings for ${description}
    long_description      ${long_description} This package provides Python \
                          ${pythonversion} bindings, modules, and scripts.

    depends_build-append  port:swig-python
    depends_lib-prepend   port:python${v} port:${name} port:py${v}-numpy

    configure.args-append     PYTHON=${pythonhome}/bin/python${pythonversion}
    configure.args-replace    --disable-python --enable-python
    configure.args-replace    --disable-swig-python --enable-swig-python

    destroot.args-append  pythondir="${pythondir}" \
                          pyexecdir="${pythondir}" \
                          pkgpythondir="${pythondir}/${name}" \
                          pkgpyexecdir="${pythondir}/${name}" \
                          prefix="${pythonhome}"

    # Only install SWIG bindings, Python libraries, and Python scripts
    destroot {
      command_exec destroot "" "-C swig"
      command_exec destroot "" "-C python"
    }

    post-destroot {
      # Remove Python include directory
      # (duplicate of SWIG headers in ${prefix}/include)
      file delete -force ${destroot}${pythonhome}/include

      # Create symbolic links in ${prefix}/bin to Python 2.7 scripts
      if {${subport} eq "py27-${name}"} {
        foreach script [glob -tails -nocomplain -directory ${destroot}${pythonhome}/bin *] {
          file link -symbolic ${destroot}${prefix}/bin/${script} ../Library/Frameworks/Python.framework/Versions/${pythonversion}/bin/${script}
        }
      }
    }
  }
}

subport octave-${name} {
  set pythonversion     [string index ${v} 0].[string range ${v} 1 end]
  set pythonhome \
    "${frameworks_dir}/Python.framework/Versions/${pythonversion}"
  set pythondir "${pythonhome}/lib/python${pythonversion}/site-packages"

  categories-prepend    octave
  description           Octave ${pythonversion} bindings for ${description}
  long_description      ${long_description} This package provides Octave \
                        bindings, modules, and scripts.

  depends_build-append  port:swig-octave
  depends_lib-prepend   port:octave port:${name}

  configure.args-replace --disable-swig-octave --enable-swig-octave

  # Only install SWIG bindings, Python libraries, and Python scripts
  destroot {
    command_exec destroot "" "-C swig"
  }

  post-destroot {
    # Remove Octave include directory
    # (duplicate of SWIG headers in ${prefix}/include)
    file delete -force ${destroot}${prefix}/include
  }
}

if {${name} eq ${subport}} {
  livecheck.type        regex
  livecheck.url         ${master_sites}
  livecheck.regex       ${name}-(\\d+(?:\\.\\d+)*)${extract.suffix}
} else {
  livecheck.type        none
}
