# -*- 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           github 1.0
PortGroup           python 1.0

github.setup        pytorch audio 0.12.0 v
# Change github.tarball_from to 'releases' or 'archive' next update
github.tarball_from tarball
revision            0
name                py-torchaudio

categories-append   audio
license             BSD
maintainers         nomaintainer
fetch.type          git

description         Data manipulation and transformation for audio \
                    signal processing, powered by PyTorch

long_description    The aim of torchaudio is to apply PyTorch to the \
                    audio domain. By supporting PyTorch, torchaudio \
                    follows the same philosophy of providing strong \
                    GPU acceleration, having a focus on trainable \
                    features through the autograd system, and having \
                    consistent style (tensor names and dimension \
                    names). Therefore, it is primarily a machine \
                    learning library and not a general signal \
                    processing library. The benefits of PyTorch can be \
                    seen in torchaudio through having all the \
                    computations be through PyTorch operations which\
                    makes it easy to use and feel like a natural \
                    extension.

# diff -NaurdwB ./py-torchaudio-orig/setup.py ./py-torchaudio-new/setup.py | sed -E -e 's/\.\/py-torchaudio-(orig|new)\//\.\//g' | sed -E -e 's|/opt/local|@@PREFIX@@|g' > ~/Downloads/patch-setup_py.diff
patchfiles-append   patch-setup_py.diff

# prevent cmake downloads
# sudo port configure py310-torchaudio
# find ${worksrcpath} -type f -name CMakeLists.txt -exec fgrep 'URL ' {} ';'
master_sites-append \
                    https://ftp.osuosl.org/pub/xiph/releases/ogg/:osuosl \
                    https://boostorg.jfrog.io/artifactory/main/release/1.78.0/source/:boost \
                    https://sourceforge.net/projects/opencore-amr/files/opencore-amr/:opencore \
                    https://downloads.sourceforge.net/project/lame/lame/3.99/:lame \
                    https://ftp.osuosl.org/pub/xiph/releases/ogg/:ogg \
                    https://ftp.osuosl.org/pub/xiph/releases/flac/:flac \
                    https://ftp.osuosl.org/pub/xiph/releases/vorbis/:vorbis \
                    https://ftp.osuosl.org/pub/xiph/releases/opus/:opus \
                    https://downloads.sourceforge.net/project/sox/sox/14.4.2/:sox \
                    https://github.com/madler/zlib/archive/refs/tags/:zlib \
                    https://tukaani.org/xz/:xz \
                    https://sourceware.org/pub/bzip2/:bzip2 \

set cmake_ExternalProject_Add_distfiles [list \
                    boost_1_78_0.tar.gz:boost \
                    bzip2-1.0.8.tar.gz:bzip2 \
                    flac-1.3.2.tar.xz:flac \
                    lame-3.99.5.tar.gz:lame \
                    libogg-1.3.3.tar.gz:ogg \
                    opencore-amr-0.1.5.tar.gz:opencore \
                    opus-1.3.1.tar.gz:opus \
                    opusfile-0.12.tar.gz:opus \
                    sox-14.4.2.tar.bz2:sox \
                    libvorbis-1.3.6.tar.gz:vorbis \
                    xz-5.2.5.tar.gz:xz \
                    v1.2.12.tar.gz:zlib \
                    ]
distfiles-append    {*}${cmake_ExternalProject_Add_distfiles}

extract.only

checksums-append \
                    boost_1_78_0.tar.gz \
                    rmd160  ddf75f8a7d187e92a3de49440988495f091107a8 \
                    sha256  94ced8b72956591c4775ae2207a9763d3600b30d9d7446562c552f0a14a63be7 \
                    size    131140716 \
                    bzip2-1.0.8.tar.gz \
                    rmd160  a593488c5c0240bcfceb292c9fa95fb7ad42cb27 \
                    sha256  ab5a03176ee106d3f0fa90e381da478ddae405918153cca248e682cd0c4a2269 \
                    size    810029 \
                    flac-1.3.2.tar.xz \
                    rmd160  fd25a098941556c5787d7369b12c43aca9a9df78 \
                    sha256  91cfc3ed61dc40f47f050a109b08610667d73477af6ef36dcad31c31a4a8d53f \
                    size    776192 \
                    lame-3.99.5.tar.gz \
                    rmd160  8a0990af918b635cc79eb27a3beafc6c0ee13a9f \
                    sha256  24346b4158e4af3bd9f2e194bb23eb473c75fb7377011523353196b19b9a23ff \
                    size    1445348 \
                    libogg-1.3.3.tar.gz \
                    rmd160  2b7a83948ff0e58da80b2153008875f7bd1f9402 \
                    sha256  c2e8a485110b97550f453226ec644ebac6cb29d1caef2902c007edab4308d985 \
                    size    579853 \
                    opencore-amr-0.1.5.tar.gz \
                    rmd160  02c092bf631345fe4d53787739e30722f5989a7c \
                    sha256  2c006cb9d5f651bfb5e60156dbff6af3c9d35c7bbcc9015308c0aff1e14cd341 \
                    size    929581 \
                    opus-1.3.1.tar.gz \
                    rmd160  c8351ef3aa78dd620b2ee02ccd986f025525b0da \
                    sha256  65b58e1e25b2a114157014736a3d9dfeaad8d41be1c8179866f144a2fb44ff9d \
                    size    1040054 \
                    opusfile-0.12.tar.gz \
                    rmd160  9b51fa303625462ce3a2c1b5fc1bfc552913d466 \
                    sha256  118d8601c12dd6a44f52423e68ca9083cc9f2bfe72da7a8c1acb22a80ae3550b \
                    size    471354 \
                    sox-14.4.2.tar.bz2 \
                    rmd160  738ac41a07f74f66e27cd642f786cc5815ca87ff \
                    sha256  81a6956d4330e75b5827316e44ae381e6f1e8928003c6aa45896da9041ea149c \
                    size    935449 \
                    libvorbis-1.3.6.tar.gz \
                    rmd160  ba4efb81513d0f431d4d04f6a7b2590c80778a43 \
                    sha256  6ed40e0241089a42c48604dc00e362beee00036af2d8b3f46338031c9e0351cb \
                    size    1634357 \
                    xz-5.2.5.tar.gz \
                    rmd160  acb4c9132e88327a1bd39d99a735ee3755b1862e \
                    sha256  f6f4910fd033078738bd82bfba4f49219d03b17eb0794eb91efbae419f4aba10 \
                    size    1791345 \
                    v1.2.12.tar.gz \
                    rmd160  d1a657f579b4b2efb2044673238f8dc258154b7a \
                    sha256  d8688496ea40fb61787500e863cc63c9afcbc524468cedeb478068924eb54932 \
                    size    1547173

python.versions     39 310

if {${name} ne ${subport}} {
    depends_build-append \
                    path:bin/cmake:cmake \
                    port:ninja \
                    port:py${python.version}-setuptools

    depends_lib-append \
                    port:ffmpeg \
                    port:libomp \
                    port:py${python.version}-dill \
                    port:py${python.version}-pytorch \
                    port:sox \
                    port:zmq

    post-fetch {
        system -W ${worksrcpath} "git submodule update --init --recursive"
        system "git config --global --add safe.directory ${worksrcpath}"
    }

    post-patch {
        xinstall -d ${worksrcpath}/third_party/archives
        foreach f ${cmake_ExternalProject_Add_distfiles} {
            xinstall -m 0644 \
                ${distpath}/[lindex [split ${f} :] 0] \
                ${worksrcpath}/third_party/archives
        }
    }

    # https://github.com/pytorch/audio#from-source
    set torchaudio_switches [list \
                    BUILD_SOX=0 \
                    USE_FFMPEG=1 \
                    USE_CUDA=0 \
                    USE_ROCM=0 \
                    USE_OPENMP=1 \
                    ]

    build.env-append \
                    {*}${torchaudio_switches}

    destroot.env-append \
                    {*}${torchaudio_switches}

    post-destroot {
        set docdir ${prefix}/share/doc/${subport}
        xinstall -d ${destroot}${docdir}
        xinstall -m 0644 -W ${worksrcpath} LICENSE README.md \
            ${destroot}${docdir}
    }

    test.run        yes

    livecheck.type  none
}