# -*- 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           active_variants 1.1
PortGroup           cmake 1.1
PortGroup           compiler_blacklist_versions 1.0
PortGroup           legacysupport 1.1
PortGroup           muniversal 1.0

name                gdal
version             3.11.0
revision            0

checksums           rmd160  f11943cf47130537408e60e1d2a1a96d553c7d4a \
                    sha256  ba1a17a74428bfd5c789ce293f59b6a3d8bfabab747431c33331ac0ac579ea71 \
                    size    9292588

categories          gis
license             MIT BSD
maintainers         {yahoo.com:n_larsson @nilason} {vince @Veence} openmaintainer

description         GDAL - Geospatial Data Abstraction Library
long_description    GDAL is a translator library for raster and vector         \
                    geospatial data formats that is released under an MIT      \
                    style Open Source License by the Open Source Geospatial    \
                    Foundation. As a library, it presents a single raster      \
                    abstract data model and single vector abstract data model  \
                    to the calling application for all supported formats. It   \
                    also comes with a variety of useful command line utilities \
                    for data translation and processing

homepage            https://www.gdal.org/
master_sites        https://download.osgeo.org/gdal/${version}
use_xz              yes

# error: call to 'abs' is ambiguous
compiler.blacklist-append {clang < 900}

# https://trac.macports.org/ticket/70193
compiler.cxx_standard 2017
cmake.set_cxx_standard yes

# Enable use of 'macports-libcxx' for macOS 10.12 and earlier, as port uses
# libc++ features normally only available on 10.13 and later.
# https://trac.macports.org/ticket/70228#comment:7
legacysupport.use_mp_libcxx \
                    yes
legacysupport.newest_darwin_requires_legacy \
                    16

# See https://trac.macports.org/ticket/56908
compiler.thread_local_storage yes

configure.optflags  -DGDAL_COMPILATION

# Supported PROJ versions
set proj_versions {6 7 8 9}
# Supported PostgreSQL versions
set pg_suffixes {12 13 14 15 16 17}

depends_build-append \
                    port:bash-completion \
                    path:bin/pkg-config:pkgconfig

depends_lib-append \
                    port:blosc \
                    port:brunsli \
                    port:charls \
                    port:curl \
                    port:expat \
                    port:giflib \
                    path:include/turbojpeg.h:libjpeg-turbo \
                    port:lerc \
                    port:libaec \
                    port:libdeflate \
                    port:libpng \
                    port:libxml2 \
                    port:lz4 \
                    path:lib/libssl.dylib:openssl \
                    port:pcre2 \
                    port:proj \
                    port:qhull \
                    port:spatialite \
                    port:sqlite3 \
                    port:webp \
                    port:zlib \
                    port:zstd

# By default, disable all drivers
configure.args-append                                        \
                    -DGDAL_BUILD_OPTIONAL_DRIVERS=OFF        \
                    -DOGR_BUILD_OPTIONAL_DRIVERS=OFF         \
                    -DGDAL_ENABLE_PLUGINS=ON                 \
                    -DGDAL_USE_EXTERNAL_LIBS=ON

# Enable all built-in raster drivers
configure.args-append                                        \
                    -DGDAL_ENABLE_DRIVER_AAIGRID=ON          \
                    -DGDAL_ENABLE_DRIVER_ADRG=ON             \
                    -DGDAL_ENABLE_DRIVER_AIGRID=ON           \
                    -DGDAL_ENABLE_DRIVER_AIRSAR=ON           \
                    -DGDAL_ENABLE_DRIVER_BMP=ON              \
                    -DGDAL_ENABLE_DRIVER_BSB=ON              \
                    -DGDAL_ENABLE_DRIVER_CALS=ON             \
                    -DGDAL_ENABLE_DRIVER_CEOS=ON             \
                    -DGDAL_ENABLE_DRIVER_COASP=ON            \
                    -DGDAL_ENABLE_DRIVER_COSAR=ON            \
                    -DGDAL_ENABLE_DRIVER_CTG=ON              \
                    -DGDAL_ENABLE_DRIVER_DIMAP=ON            \
                    -DGDAL_ENABLE_DRIVER_DTED=ON             \
                    -DGDAL_ENABLE_DRIVER_ERS=ON              \
                    -DGDAL_ENABLE_DRIVER_ENVISAT=ON          \
                    -DGDAL_ENABLE_DRIVER_ESRIC=ON            \
                    -DGDAL_ENABLE_DRIVER_GFF=ON              \
                    -DGDAL_ENABLE_DRIVER_GIF=ON              \
                    -DGDAL_ENABLE_DRIVER_GRIB=ON             \
                    -DGDAL_ENABLE_DRIVER_GSG=ON              \
                    -DGDAL_ENABLE_DRIVER_GTIFF=ON            \
                    -DGDAL_ENABLE_DRIVER_GXF=ON              \
                    -DGDAL_ENABLE_DRIVER_HF2=ON              \
                    -DGDAL_ENABLE_DRIVER_HFA=ON              \
                    -DGDAL_ENABLE_DRIVER_IDRISI=ON           \
                    -DGDAL_ENABLE_DRIVER_ILWIS=ON            \
                    -DGDAL_ENABLE_DRIVER_IRIS=ON             \
                    -DGDAL_ENABLE_DRIVER_JAXAPALSAR=ON       \
                    -DGDAL_ENABLE_DRIVER_JDEM=ON             \
                    -DGDAL_ENABLE_DRIVER_JPEG=ON             \
                    -DGDAL_ENABLE_DRIVER_KMLSUPEROVERLAY=ON  \
                    -DGDAL_ENABLE_DRIVER_L1B=ON              \
                    -DGDAL_ENABLE_DRIVER_LEVELLER=ON         \
                    -DGDAL_ENABLE_DRIVER_MAP=ON              \
                    -DGDAL_ENABLE_DRIVER_MRF=ON              \
                    -DGDAL_ENABLE_DRIVER_MSGN=ON             \
                    -DGDAL_ENABLE_DRIVER_NGSGEOID=ON         \
                    -DGDAL_ENABLE_DRIVER_NITF=ON             \
                    -DGDAL_ENABLE_DRIVER_NORTHWOOD=ON        \
                    -DGDAL_ENABLE_DRIVER_PCIDSK=ON           \
                    -DGDAL_ENABLE_DRIVER_PCRASTER=ON         \
                    -DGDAL_ENABLE_DRIVER_PDS=ON              \
                    -DGDAL_ENABLE_DRIVER_PNG=ON              \
                    -DGDAL_ENABLE_DRIVER_PRF=ON              \
                    -DGDAL_ENABLE_DRIVER_RAW=ON              \
                    -DGDAL_ENABLE_DRIVER_RMF=ON              \
                    -DGDAL_ENABLE_DRIVER_RS2=ON              \
                    -DGDAL_ENABLE_DRIVER_SAFE=ON             \
                    -DGDAL_ENABLE_DRIVER_SAGA=ON             \
                    -DGDAL_ENABLE_DRIVER_SAR_CEOS=ON         \
                    -DGDAL_ENABLE_DRIVER_SDTS=ON             \
                    -DGDAL_ENABLE_DRIVER_SENTINEL2=ON        \
                    -DGDAL_ENABLE_DRIVER_SIGDEM=ON           \
                    -DGDAL_ENABLE_DRIVER_SRTMHGT=ON          \
                    -DGDAL_ENABLE_DRIVER_STACIT=ON           \
                    -DGDAL_ENABLE_DRIVER_STACTA=ON           \
                    -DGDAL_ENABLE_DRIVER_TERRAGEN=ON         \
                    -DGDAL_ENABLE_DRIVER_TGA=ON              \
                    -DGDAL_ENABLE_DRIVER_TIL=ON              \
                    -DGDAL_ENABLE_DRIVER_TSX=ON              \
                    -DGDAL_ENABLE_DRIVER_USGSDEM=ON          \
                    -DGDAL_ENABLE_DRIVER_VRT=ON              \
                    -DGDAL_ENABLE_DRIVER_XYZ=ON              \
                    -DGDAL_ENABLE_DRIVER_ZARR=ON             \
                    -DGDAL_ENABLE_DRIVER_ZMAP=ON

# Same with built-in vector drivers
configure.args-append                                        \
                    -DOGR_ENABLE_DRIVER_AVC=ON               \
                    -DOGR_ENABLE_DRIVER_CAD=ON               \
                    -DOGR_ENABLE_DRIVER_CSV=ON               \
                    -DOGR_ENABLE_DRIVER_DGN=ON               \
                    -DOGR_ENABLE_DRIVER_DXF=ON               \
                    -DOGR_ENABLE_DRIVER_EDIGEO=ON            \
                    -DOGR_ENABLE_DRIVER_FLATGEOBUF=ON        \
                    -DOGR_ENABLE_DRIVER_JSONFG=ON            \
                    -DOGR_ENABLE_DRIVER_GEOJSON=ON           \
                    -DOGR_ENABLE_DRIVER_GMT=ON               \
                    -DOGR_ENABLE_DRIVER_MAPML=ON             \
                    -DOGR_ENABLE_DRIVER_MVT=ON               \
                    -DOGR_ENABLE_DRIVER_OPENFILEGDB=ON       \
                    -DOGR_ENABLE_DRIVER_PG=OFF               \
                    -DOGR_ENABLE_DRIVER_PGDUMP=ON            \
                    -DOGR_ENABLE_DRIVER_S57=ON               \
                    -DOGR_ENABLE_DRIVER_SDTS=ON              \
                    -DOGR_ENABLE_DRIVER_SELAFIN=ON           \
                    -DOGR_ENABLE_DRIVER_SHAPE=ON             \
                    -DOGR_ENABLE_DRIVER_SQLITE=ON            \
                    -DOGR_ENABLE_DRIVER_SXF=ON               \
                    -DOGR_ENABLE_DRIVER_VDV=ON               \
                    -DOGR_ENABLE_DRIVER_WASP=ON

# These drivers require curl
configure.args-append                                        \
                    -DGDAL_ENABLE_DRIVER_DAAS=ON             \
                    -DGDAL_ENABLE_DRIVER_EEDA=ON             \
                    -DGDAL_ENABLE_DRIVER_OGCAPI=ON           \
                    -DGDAL_ENABLE_DRIVER_PLMOSAIC=ON         \
                    -DGDAL_ENABLE_DRIVER_WCS=ON              \
                    -DGDAL_ENABLE_DRIVER_WMS=ON              \
                    -DGDAL_ENABLE_DRIVER_WMTS=ON             \
                    -DOGR_ENABLE_DRIVER_AMIGOCLOUD=ON        \
                    -DOGR_ENABLE_DRIVER_CARTO=ON             \
                    -DOGR_ENABLE_DRIVER_CSW=OFF              \
                    -DOGR_ENABLE_DRIVER_ELASTIC=ON           \
                    -DOGR_ENABLE_DRIVER_NGW=ON               \
                    -DOGR_ENABLE_DRIVER_PLSCENES=ON          \
                    -DOGR_ENABLE_DRIVER_WFS=ON

# These drivers require sqlite3
configure.args-append                                        \
                    -DGDAL_ENABLE_DRIVER_MBTILES=ON          \
                    -DOGR_ENABLE_DRIVER_GPKG=ON              \
                    -DOGR_ENABLE_DRIVER_OSM=ON               \
                    -DOGR_ENABLE_DRIVER_PMTILES=ON           \
                    -DOGR_ENABLE_DRIVER_VFK=ON

# These drivers require expat
configure.args-append                                        \
                    -DOGR_ENABLE_DRIVER_GEORSS=ON            \
                    -DOGR_ENABLE_DRIVER_GML=ON               \
                    -DOGR_ENABLE_DRIVER_GPSBABEL=ON          \
                    -DOGR_ENABLE_DRIVER_GPX=ON               \
                    -DOGR_ENABLE_DRIVER_JML=ON               \
                    -DOGR_ENABLE_DRIVER_LVBAG=ON             \
                    -DOGR_ENABLE_DRIVER_ODS=ON               \
                    -DOGR_ENABLE_DRIVER_XLSX=ON

# These drivers require libxml2
configure.args-append                                        \
                    -DGDAL_USE_LIBXML2=ON

# These drivers require OpenEXR
configure.args-append                                        \
                     -DGDAL_ENABLE_DRIVER_EXR=OFF

# These drivers require Apache Arrow
configure.args-append                                        \
                    -DGDAL_USE_ARROW=OFF                     \
                    -DGDAL_USE_PARQUET=OFF                   \
                    -DOGR_ENABLE_DRIVER_ARROW=OFF            \
                    -DOGR_ENABLE_DRIVER_PARQUET=OFF

# Miscellaneous drivers
configure.args-append                                        \
                    -DBUILD_CSHARP_BINDINGS=OFF              \
                    -DBUILD_JAVA_BINDINGS=OFF                \
                    -DBUILD_PYTHON_BINDINGS=OFF              \
                    -DGDAL_ENABLE_DRIVER_HDF5=OFF            \
                    -DGDAL_ENABLE_DRIVER_KEA=OFF             \
                    -DGDAL_USE_ARCHIVE=OFF                   \
                    -DGDAL_USE_ARMADILLO=OFF                 \
                    -DGDAL_USE_CFITSIO=OFF                   \
                    -DGDAL_USE_CRYPTOPP=OFF                  \
                    -DGDAL_USE_CURL=ON                       \
                    -DGDAL_USE_DEFLATE=ON                    \
                    -DGDAL_USE_ECW=OFF                       \
                    -DGDAL_USE_EXPAT=ON                      \
                    -DGDAL_USE_GEOS=ON                       \
                    -DGDAL_USE_GEOTIFF_INTERNAL=ON           \
                    -DGDAL_USE_GIF=ON                        \
                    -DGDAL_USE_HDF4=OFF                      \
                    -DGDAL_USE_HDF5=OFF                      \
                    -DGDAL_USE_HEIF=OFF                      \
                    -DGDAL_USE_ICONV=ON                      \
                    -DGDAL_USE_IDB=OFF                       \
                    -DGDAL_USE_JXL=OFF                       \
                    -DGDAL_USE_JXL_THREADS=OFF               \
                    -DGDAL_USE_KEA=OFF                       \
                    -DGDAL_USE_LIBAEC=ON                     \
                    -DGDAL_USE_LIBLZMA=OFF                   \
                    -DGDAL_USE_MRSID=OFF                     \
                    -DGDAL_USE_MYSQL=OFF                     \
                    -DGDAL_USE_NETCDF=OFF                    \
                    -DGDAL_USE_ODBC=OFF                      \
                    -DGDAL_USE_OPENEXR=OFF                   \
                    -DGDAL_USE_OPENJPEG=OFF                  \
                    -DGDAL_USE_PCRE2=ON                      \
                    -DGDAL_USE_PDFIUM=OFF                    \
                    -DGDAL_USE_PNG=ON                        \
                    -DGDAL_USE_POSTGRESQL=ON                 \
                    -DGDAL_USE_PROJ=ON                       \
                    -DGDAL_USE_QHULL=ON                      \
                    -DGDAL_USE_RASTERLITE2=OFF               \
                    -DGDAL_USE_SPATIALITE=ON                 \
                    -DGDAL_USE_SQLITE3=ON                    \
                    -DGDAL_USE_TIFF_INTERNAL=ON              \
                    -DGDAL_USE_TILEDB=OFF                    \
                    -DGDAL_USE_WEBP=ON                       \
                    -DGDAL_USE_XERCESC=OFF                   \
                    -DGDAL_USE_ZSTD=ON

configure.args-append                                                        \
                    -DLIBAEC_INCLUDE_DIR=${prefix}/lib/libaec/include        \
                    -DLIBAEC_LIBRARY=${prefix}/lib/libaec/lib/libaec.dylib   \
                    -DPROJ_INCLUDE_DIR=${prefix}/lib/proj9/include           \
                    -DPROJ_LIBRARY_RELEASE=${prefix}/lib/proj9/lib/libproj.dylib

# Always choose internal libjson code (#44098)
configure.args-append -DGDAL_USE_JSONC_INTERNAL=ON

if {${build_arch} in [list i386 ppc]} {
    # https://trac.macports.org/ticket/67468
    configure.args-append \
                    -DBUILD_WITHOUT_64BIT_OFFSET=ON
}

pre-configure {
    # see https://trac.macports.org/ticket/56517
    if {[string match *clang* ${configure.cxx}] && ${configure.cxx_stdlib} ne ""} {
        configure.ldflags-append -stdlib=${configure.cxx_stdlib}
    }

    global worksrcpath_dirs
    if {![variant_exists universal] || ![variant_isset universal]} {
        set worksrcpath_dirs ${worksrcpath}
    } else {
        foreach arch ${universal_archs_to_use} {
            lappend worksrcpath_dirs ${worksrcpath}-${arch}
        }
    }

    # If HDF5 is built with a mpi variant, we need to know the path to "mpi.h".
    # Figure out HDF5's mpi include directory:
    set mpl_include_dir ""
    if {![catch {set result [active_variants hdf5 openmpi]}]} {
        if {$result} {
            set mpl_include_dir "-I${prefix}/include/openmpi-mp"
        }
    }
    if {![catch {set result [active_variants hdf5 mpich]}]} {
        if {$result} {
            set mpl_include_dir "-I${prefix}/include/mpich-mp"
        }
    }
    if {$mpl_include_dir ne ""} {
        configure.cxxflags-append ${mpl_include_dir}
    }
}

if {$subport eq $name} {
    # Register plugins
    configure.args-append  \
                        -DGDAL_REGISTER_DRIVER_HDF4_FOR_LATER_PLUGIN=ON \
                        -DGDAL_DRIVER_HDF4_PLUGIN_INSTALLATION_MESSAGE="Install the HDF4 driver with 'sudo port install gdal-hdf4'." \
                        -DGDAL_REGISTER_DRIVER_HDF5_FOR_LATER_PLUGIN=ON \
                        -DGDAL_DRIVER_HDF5_PLUGIN_INSTALLATION_MESSAGE="Install the HDF5 driver with 'sudo port install gdal-hdf5'." \
                        -DGDAL_REGISTER_DRIVER_KEA_FOR_LATER_PLUGIN=ON \
                        -DGDAL_DRIVER_KEA_PLUGIN_INSTALLATION_MESSAGE="Install the KEA driver with 'sudo port install gdal-kea'." \
                        -DGDAL_REGISTER_DRIVER_PDF_FOR_LATER_PLUGIN=ON \
                        -DGDAL_DRIVER_PDF_PLUGIN_INSTALLATION_MESSAGE="Install the PDF driver with 'sudo port install gdal-pdf'." \
                        -DGDAL_USE_POPPLER=ON \
                        -DOGR_REGISTER_DRIVER_LIBKML_FOR_LATER_PLUGIN=ON \
                        -DOGR_DRIVER_LIBKML_PLUGIN_INSTALLATION_MESSAGE="Install the LIBKML driver with 'sudo port install gdal-libkml'." \
                        -DGDAL_USE_LIBKML=OFF

    depends_build-append \
                        path:lib/pkgconfig/poppler.pc:poppler \
                        port:hdf5 \
                        port:kealib \
                        port:libkml

    # Set target to none
    build.target

    variant arrow description {Enable (Geo)Arrow IPC File Format / Stream and (Geo)Parquet support} {
        depends_lib-append      port:apache-arrow
        configure.args-replace  -DGDAL_USE_ARROW=OFF            -DGDAL_USE_ARROW=ON \
                                -DGDAL_USE_PARQUET=OFF          -DGDAL_USE_PARQUET=ON \
                                -DOGR_ENABLE_DRIVER_ARROW=OFF   -DOGR_ENABLE_DRIVER_ARROW=ON \
                                -DOGR_ENABLE_DRIVER_PARQUET=OFF -DOGR_ENABLE_DRIVER_PARQUET=ON
        configure.args-append   -DARROW_USE_STATIC_LIBRARIES=OFF

        if {[string match *gcc* ${configure.compiler}]} {
            configure.ldflags-append \
                                    -latomic
        }
    }

    variant cfitsio description {Enable FITS support} {
        depends_lib-append      port:cfitsio
        configure.args-replace  -DGDAL_USE_CFITSIO=OFF    -DGDAL_USE_CFITSIO=ON
        configure.args-append   -DGDAL_ENABLE_DRIVER_FITS=ON
    }

    variant cryptopp description {Enable Crypto++ support} {
        depends_lib-append      port:libcryptopp
        configure.args-replace  -DGDAL_USE_CRYPTOPP=OFF    -DGDAL_USE_CRYPTOPP=ON
    }

    variant ecw description {Enable ECW file format} {
        configure.args-replace  -DGDAL_USE_ECW=OFF    -DGDAL_USE_ECW=ON
        configure.args-append   -DECW_ROOT=${prefix}/lib/ECW \
                                -DGDAL_ENABLE_DRIVER_ECW=ON
        pre-configure {
            if {(![file exists ${prefix}/lib/ECW]) && \
            (![file exists /Intergraph/ERDASEcwJpeg2000SDK5.1.1/Desktop_Read-Only])} {
                ui_error "
    ****
    * The Intergraph ECW SDK version 5.5.0.2034 is needed to
    * compile gdal with the ecw variant. It must be installed separately.
    * Please download the macOS version of the SDK
    * 'ERDAS ECW/JP2 SDK v5.5 (MacOSX)' from
    * https://download.hexagongeospatial.com/downloads/ecw/erdas-ecw-jp2-sdk-v5-5-update-3-macos
    * Choose to install it in ${prefix}/lib/ECW.
    * Then try to install GDAL again.
    ****"
                return -code error "ECW SDK 5.5 not installed."
            }

            if {(![file exists ${prefix}/lib/ECW]) && \
                ([file exists /Intergraph/ERDASEcwJpeg2000SDK5.1.1/Desktop_Read-Only])} {

              ui_error "
    ****
    * Please create symbolic links from the installation directory to
    * ${prefix}/lib/ECW, by executing at the prompt:
    * sudo ln -s \${ECW_install_dir}/ERDASEcwJpeg2000SDK5.5.1/Desktop_Read-Only \
                      ${prefix}/lib/ECW
    * Or reinstall the ECW SDK in ${prefix}/lib/ECW directly.
    * Then try again.
    ****"
                return -code error "Symlinks missing."
            }
        }
    }

    variant hdf4 description {Enable HDF4 file format} {
        pre-fetch {
            ui_error "The '+hdf4' variant has been removed and replaced by the 'gdal-hdf4' subport."
            return -code error "unsupported variant"
        }
    }

    variant hdf5 description {Enable HDF5 file format} {
        pre-fetch {
            ui_error "The '+hdf5' variant has been removed and replaced by the 'gdal-hdf5' subport."
            return -code error "unsupported variant"
        }
    }

    variant heif description {Enable HEIF support} {
        depends_lib-append      port:libheif
        configure.args-replace  -DGDAL_USE_HEIF=OFF    -DGDAL_USE_HEIF=ON
        configure.args-append   -DGDAL_ENABLE_DRIVER_HEIF=ON
    }

    variant jpegxl description {Enable JPEG XL support} {
        depends_lib-append      port:libjxl
        configure.args-replace  -DGDAL_USE_JXL=OFF         -DGDAL_USE_JXL=ON \
                                -DGDAL_USE_JXL_THREADS=OFF -DGDAL_USE_JXL_THREADS=ON
    }

    variant kea description {Enable KEA support} {
        pre-fetch {
            ui_error "The '+kea' variant has been removed and replaced by the 'gdal-kea' subport."
            return -code error "unsupported variant"
        }
    }

    variant libarchive description {Enable libarchive support} {
        depends_lib-append      port:libarchive
        configure.args-replace  -DGDAL_USE_ARCHIVE=OFF    -DGDAL_USE_ARCHIVE=ON
    }

    variant lto description {Enable Link Time Optimization} {
        configure.args-append   -DENABLE_IPO=ON
    }

    variant lzma description {Enable LZMA (7Z) compression support} {
        depends_lib-append      port:lzma
        configure.args-replace  -DGDAL_USE_LIBLZMA=OFF    -DGDAL_USE_LIBLZMA=ON
    }

    variant mrsid description {Enable MrSID file format} {
        depends_lib-append      port:geoexpress-sdk
        configure.args-replace  -DGDAL_USE_MRSID=OFF    -DGDAL_USE_MRSID=ON \
                                -DGDAL_USE_JP2MRSID=OFF -DGDAL_USE_JP2MRSID=ON
    }

    variant mysql57 conflicts mysql8 description {Enable MySQL 5.7 support} {
        depends_lib-append      port:mysql57
        configure.args-replace  -DGDAL_USE_MYSQL=OFF    -DGDAL_USE_MYSQL=ON
        configure.args-append   -DMYSQL_INCLUDE_DIR=${prefix}/include/mysql57/mysql \
                                -DMYSQL_LIBRARY=${prefix}/lib/mysql57/mysql/libmysqlclient.dylib
    }

    variant mysql8 conflicts mysql57 description {Enable MySQL 8 support} {
        depends_lib-append      port:mysql8
        configure.args-replace  -DGDAL_USE_MYSQL=OFF    -DGDAL_USE_MYSQL=ON
        configure.args-append   -DMYSQL_INCLUDE_DIR=${prefix}/include/mysql8/mysql \
                                -DMYSQL_LIBRARY=${prefix}/lib/mysql8/mysql/libmysqlclient.dylib
    }

    variant native description {Optimize for speed} {
        if {${os.arch} eq "arm"} {
            configure.optflags  -O3 -pipe -mcpu=apple-m1 -DGDAL_COMPILATION
        } elseif {${os.arch} eq "powerpc"} {
            # https://github.com/macports/macports-ports/pull/18587#issuecomment-1540025830
            configure.optflags  -O3 -pipe -mtune=native -mcpu=native -DGDAL_COMPILATION
        } else {
            configure.optflags  -O3 -pipe -march=native -DGDAL_COMPILATION
        }
    }

    variant netcdf description {Enable NetCDF file format} {
        pre-configure {
            # If NetCDF is built with a mpi variant, we need to know the path to "mpi.h".
            # Figure out NetCDF's mpi include directory:
            set mpl_include_dir ""
            if {![catch {set result [active_variants netcdf openmpi]}]} {
                if {$result} {
                    set mpl_include_dir "-I${prefix}/include/openmpi-mp"
                }
            }
            if {![catch {set result [active_variants netcdf mpich]}]} {
                if {$result} {
                    set mpl_include_dir "-I${prefix}/include/mpich-mp"
                }
            }
            if {$mpl_include_dir ne ""} {
                configure.cxxflags-append ${mpl_include_dir}
            }
        }
        depends_lib-append      port:netcdf
        configure.args-replace  -DGDAL_USE_NETCDF=OFF  -DGDAL_USE_NETCDF=ON
        configure.args-append   -DGDAL_ENABLE_DRIVER_NETCDF=ON
    }

    variant odbc description {Enable ODBC support} {
        depends_lib-append      port:unixODBC
        configure.args-replace  -DGDAL_USE_ODBC=OFF  -DGDAL_USE_ODBC=ON
        # These drivers depend on odbc
        configure.args-append   -DOGR_ENABLE_DRIVER_ODBC=ON \
                                -DOGR_ENABLE_DRIVER_PGEO=ON \
                                -DOGR_ENABLE_DRIVER_MSSQLSPATIAL=ON
    }

    variant openexr description {Enable OpenEXR image file format} {
        depends_lib-append      port:openexr
        configure.args-replace  -DGDAL_USE_OPENEXR=OFF        -DGDAL_USE_OPENEXR=ON \
                                -DGDAL_ENABLE_DRIVER_EXR=OFF  -DGDAL_ENABLE_DRIVER_EXR=ON
    }

    variant openjpeg description {Enable OpenJPEG JPEG-2000 format support} {
        depends_lib-append      port:openjpeg
        configure.args-replace  -DGDAL_USE_OPENJPEG=OFF  -DGDAL_USE_OPENJPEG=ON
        configure.args-append   -DGDAL_ENABLE_DRIVER_JP2OPENJPEG=ON
    }

    variant rasterlite2 description {Enable RasterLite2 coverages support} {
        depends_lib-append      port:librasterlite2
        configure.args-replace  -DGDAL_USE_RASTERLITE2=OFF -DGDAL_USE_RASTERLITE2=ON
    }

    variant tiledb description {Enable TileDB support} {
        depends_lib-append      port:tiledb
        configure.args-replace  -DGDAL_USE_TILEDB=OFF  -DGDAL_USE_TILEDB=ON
        configure.args-append   -DGDAL_ENABLE_DRIVER_TILEDB=ON
    }

    # PostgreSQL variants
    set pg_variants {}
    set pg_default_variant "if {"
    foreach v ${pg_suffixes} {
        lappend pg_variants postgresql${v}
        set pg_default_variant "${pg_default_variant}!\[variant_isset postgresql${v}] && "
    }
    set pg_default_variant [string range ${pg_default_variant} 0 end-4]
    set pg_default_variant "${pg_default_variant}} {default_variants +postgresql${v}}"
    eval $pg_default_variant
    foreach v ${pg_suffixes} {
        set p postgresql${v}
        set i [lsearch -exact ${pg_variants} ${p}]
        set c [lreplace ${pg_variants} ${i} ${i}]
        variant ${p} description "Enable PostgreSQL ${v} support" conflicts {*}${c} "
            depends_lib-append      port:${p}
            configure.args-replace  -DOGR_ENABLE_DRIVER_PG=OFF    -DOGR_ENABLE_DRIVER_PG=ON
            configure.args-append   -DPostgreSQL_INCLUDE_DIR=${prefix}/include/${p} \
                                    -DPostgreSQL_LIBRARY_DIR=${prefix}/lib/${p} \
                                    -DPostgreSQL_ADDITIONAL_VERSIONS=${v}
        "
    }

    variant xerces description {Enable Xerces XML support for GML file format} {
        depends_lib-append      path:include/xercesc/util/XercesVersion.hpp:xercesc3
        configure.args-replace  -DGDAL_USE_XERCESC=OFF   -DGDAL_USE_XERCESC=ON
        # These drivers need xerces-c
        configure.args-append   -DOGR_ENABLE_DRIVER_NAS=ON \
                                -DOGR_ENABLE_DRIVER_ILI=ON \
                                -DOGR_ENABLE_DRIVER_GMLAS=ON
    }

    set jpeg2k 0
    foreach jpeg2kVariant {openjpeg ecw} {
        if {[variant_isset ${jpeg2kVariant}]} {
            incr jpeg2k 1
        }
    }
    if {${jpeg2k} > 1} {
        notes-append "Multiple drivers are able to open JPEG-2000 datasets. See https://trac.osgeo.org/gdal/wiki/ConfigOptions#GDAL_SKIP."
    }

    post-destroot {
        xinstall -m 755 -d ${destroot}${prefix}/share/doc/${name}
        xinstall -m 644 -W ${worksrcpath} HOWTO-RELEASE VERSION \
            ${destroot}${prefix}/share/doc/${name}

        if {[variant_isset ecw]} {
              foreach f [list \
                bin/gdal_contour \
                bin/gdal_create \
                bin/gdal_grid \
                bin/gdal_rasterize \
                bin/gdal_translate \
                bin/gdal_viewshed \
                bin/gdaladdo \
                bin/gdalbuildvrt \
                bin/gdaldem \
                bin/gdalenhance \
                bin/gdalinfo \
                bin/gdallocationinfo \
                bin/gdalmanage \
                bin/gdalmdiminfo \
                bin/gdalmdimtranslate \
                bin/gdalsrsinfo \
                bin/gdaltindex \
                bin/gdaltransform \
                bin/gdalwarp \
                bin/gnmanalyse \
                lib/libgdal.dylib] \
                {
                    if {${configure.cxx_stdlib} eq "libc++"} {
                        system "install_name_tool -change \
                            /Users/jenkins/hudson/workspace/RL_5.1.1_ECWJP2SDK_OSX/Master/libNCSEcw.dylib ${prefix}/lib/ECW/redistributable/libc++/libNCSEcw.dylib \
                            ${destroot}${prefix}/${f}"
                    } else {
                        system "install_name_tool -change \
                            /Users/jenkins/hudson/workspace/RL_5.1.1_ECWJP2SDK_OSX/Master/libNCSEcw.dylib ${prefix}/lib/ECW/redistributable/libstdc++/libNCSEcw.dylib \
                            ${destroot}${prefix}/${f}"
                    }
                }
        }
    }

    livecheck.type  regex
    livecheck.url   https://download.osgeo.org/gdal/CURRENT
    livecheck.regex ${name}-(\\d+(?:\\.\\d+)*)\\.tar
}

proc set_gdal_variants {} {
    global prefix
    global proj_versions
    global pg_suffixes
    global os.arch

    set pg ""
    set pg_v ""
    foreach v ${pg_suffixes} {
        if {![catch {set result [active_variants gdal postgresql${v}]}]} {
            if {$result} {
                set pg postgresql${v}
                set pg_v ${v}
            }
        }
    }
    if {$pg ne ""} {
        depends_build-append     port:${pg}
        configure.args-replace  -DOGR_ENABLE_DRIVER_PG=OFF    -DOGR_ENABLE_DRIVER_PG=ON
        configure.args-append   -DPostgreSQL_INCLUDE_DIR=${prefix}/include/${pg} \
                                -DPostgreSQL_LIBRARY_DIR=${prefix}/lib/${pg} \
                                -DPostgreSQL_ADDITIONAL_VERSIONS=${pg_v}
    }

    set proj_v ""
    foreach v ${proj_versions} {
        if {![catch {set result [active_variants gdal proj${v}]}]} {
            if {$result} {
                set proj_v ${v}
            }
        }
        if {$proj_v ne ""} {
            configure.args-append   -DGDAL_USE_PROJ=ON \
                                    -DPROJ_INCLUDE_DIR=${prefix}/lib/proj${proj_v}/include \
                                    -DPROJ_LIBRARY_RELEASE=${prefix}/lib/proj${proj_v}/lib/libproj.dylib
        }
    }

    if {![catch {set result [active_variants gdal lto]}]} {
        if {$result} {
            configure.args-append   -DENABLE_IPO=ON
        }
    }

    if {![catch {set result [active_variants gdal native]}]} {
        if {${os.arch} eq "arm"} {
            configure.optflags  -O3 -pipe -mcpu=apple-m1 -DGDAL_COMPILATION
        } elseif {${os.arch} eq "powerpc"} {
            # https://github.com/macports/macports-ports/pull/18587#issuecomment-1540025830
            configure.optflags  -O3 -pipe -mtune=native -mcpu=native -DGDAL_COMPILATION
        } else {
            configure.optflags  -O3 -pipe -march=native -DGDAL_COMPILATION
        }
    }
}

subport gdal-hdf4 {
    description         GDAL - HDF4 driver
    long_description    {*}${description}.

    depends_lib         port:gdal \
                        port:hdf4

    pre-configure {
        set_gdal_variants
    }

    configure.args-replace  -DGDAL_USE_HDF4=OFF    -DGDAL_USE_HDF4=ON
    configure.args-append   -DGDAL_ENABLE_DRIVER_HDF4=ON \
                            -DGDAL_ENABLE_DRIVER_HDF4_PLUGIN=ON

    post-destroot {
        system -W ${destroot} "find . ! -name 'gdal_HDF4.dylib' \\( -type f -or -type l \\) -exec rm -f {} +"
    }

    livecheck.type  none
}

subport gdal-hdf5 {
    description         GDAL - HDF5 driver
    long_description    {*}${description}.

    depends_lib-append      port:gdal \
                            port:hdf5

    pre-configure {
        set_gdal_variants
    }

    configure.args-replace  -DGDAL_USE_HDF5=OFF  -DGDAL_USE_HDF5=ON \
                            -DGDAL_ENABLE_DRIVER_HDF5=OFF  -DGDAL_ENABLE_DRIVER_HDF5=ON \
    configure.args-append   -DGDAL_ENABLE_DRIVER_HDF5_PLUGIN=ON

    post-destroot {
        system -W ${destroot} "find . ! -name 'gdal_HDF5.dylib' \\( -type f -or -type l \\) -exec rm -f {} +"
    }

    livecheck.type  none
}

subport gdal-kea {
    description         GDAL - KEA driver
    long_description    {*}${description}.

    depends_lib-append      port:gdal \
                            port:hdf5 \
                            port:kealib

    pre-configure {
        set_gdal_variants
    }

    configure.args-replace  -DGDAL_USE_KEA=OFF  -DGDAL_USE_KEA=ON \
                            -DGDAL_ENABLE_DRIVER_KEA=OFF  -DGDAL_ENABLE_DRIVER_KEA=ON \
                            -DGDAL_USE_HDF5=OFF  -DGDAL_USE_HDF5=ON
    configure.args-append   -DGDAL_ENABLE_DRIVER_KEA_PLUGIN=ON

    post-destroot {
        system -W ${destroot} "find . ! -name 'gdal_KEA.dylib' \\( -type f -or -type l \\) -exec rm -f {} +"
    }

    livecheck.type  none
}

subport gdal-libkml {
    description         GDAL - LIBKML driver
    long_description    {*}${description}.

    depends_lib         port:libkml \
                        port:gdal

    pre-configure {
        set boost_includedir  [exec ${prefix}/bin/pkg-config --variable=boost_includedir libkml]
        configure.cxxflags-append -I${boost_includedir}
        set_gdal_variants
    }

    configure.args-replace -DGDAL_USE_LIBKML=OFF -DGDAL_USE_LIBKML=ON
    configure.args-append \
                        -DOGR_ENABLE_DRIVER_LIBKML=ON \
                        -DOGR_ENABLE_DRIVER_LIBKML_PLUGIN=ON

    post-destroot {
        system -W ${destroot} "find . ! -name 'ogr_LIBKML.dylib' \\( -type f -or -type l \\) -exec rm -f {} +"
    }

    livecheck.type  none
}

subport gdal-pdf {
    description         GDAL - PDF driver
    long_description    {*}${description}.

    depends_lib         path:lib/pkgconfig/poppler.pc:poppler \
                        port:gdal

    pre-configure {
        set_gdal_variants
    }

    configure.args-append \
                        -DGDAL_ENABLE_DRIVER_PDF=ON \
                        -DGDAL_ENABLE_DRIVER_PDF_PLUGIN=ON \
                        -DGDAL_USE_POPPLER=ON

    post-destroot {
        system -W ${destroot} "find . ! -name 'gdal_PDF.dylib' \\( -type f -or -type l \\) -exec rm -f {} +"
    }

    livecheck.type  none
}
