# -*- 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        pyca cryptography 48.0.0
github.tarball_from archive
name                py-${github.project}
revision            0
categories-append   devel
license             BSD

python.versions     27 310 311 312 313 314

maintainers         {stromnov @stromnov} openmaintainer

description         cryptography is a package designed to expose \
                    cryptographic primitives and recipes to Python developers.

long_description    {*}${description}

checksums           ${distname}${extract.suffix} \
                    rmd160  202a568b99976a941e499234c27fec4217640671 \
                    sha256  6afca628a004be66b3793481e02eb22ad4a067a052a6785c3736cc135dc7dc24 \
                    size    58437275

# See:
# * https://github.com/pyca/cryptography/blob/main/CHANGELOG.rst
# * https://ports.macports.org/port/rust/summary
set cryptography_darwin_min_ver 13

if {${name} ne ${subport} \
    && [string match "py*-${github.project}" ${subport}]} {
    depends_build-append \
                    port:py${python.version}-setuptools

    depends_lib-append \
                    port:py${python.version}-cffi

    # legacy support
    if {${python.version} eq 27 \
        || ${os.platform} eq "darwin" && ${os.major} < ${cryptography_darwin_min_ver}} {
        PortGroup           openssl 1.0

        if {${python.version} eq 27} {
            # Needs the same OpenSSL version as Python itself was built with.
            openssl.branch  1.1

            github.setup    pyca cryptography 3.3.2
            # Change github.tarball_from to 'releases' or 'archive' next update
            github.tarball_from tarball
            revision        0
            checksums       rmd160  13380349b4a5559153c1e8a328fd4629b81e134d \
                            sha256  2c8af64316fa1c09162c6e99dba9c23c76a07c3466dbbf62db8df970b45f935f \
                            size    35729139

            depends_lib-append \
                            port:py${python.version}-enum34 \
                            port:py${python.version}-ipaddress
        } else {
            github.setup    pyca cryptography 3.4.8
            # Change github.tarball_from to 'releases' or 'archive' next update
            github.tarball_from tarball
            revision        0
            checksums       rmd160  01ea4549ac0dc751bf7a81bfeb4b3c942303f254 \
                            sha256  cce7ee0b1082753df56c6bbbbe3c4122daba3b821b0d2129537d058624e67198 \
                            size    35736917

            # CRYPTOGRAPHY_DONT_BUILD_RUST is not respected by the build.
            # So just remove Rust stuff from setup.py, it is optional in 3.4.x.
            patchfiles-append \
                            patch-build-no-rust.diff
            # We do not want a build conflict with OpenSSL 3 here.
            # Thise version is easily fixable to build against it.
            patchfiles-append \
                            patch-openssl3.diff
        }

        python.pep517   no

        description     Legacy support of Python cryptography.

        long_description    {*}${description}

        if {${os.platform} eq "darwin" && ${os.major} < 11} {
            # https://trac.macports.org/ticket/54519
            patchfiles-append   patch-src__cffi_src_build_openssl.py.diff
        }

        compiler.cpath-prepend \
                        [openssl::include_dir]

        depends_lib-append \
                        port:py${python.version}-six

    } else {
        PortGroup   rust 1.0

        python.pep517_backend   maturin
        openssl.branch  3

        # https://github.com/pyca/cryptography/pull/14319
        patchfiles-append   patch-setuptools_no_version.diff

        # cd ${worksrcpath}/src/rust
        # sudo cargo update
        # egrep -e '^(name|version|checksum) = ' Cargo.lock | perl -pe 's/^(?:name|version|checksum) = "(.+)"/$1/' | tr '\n' ' ' | perl -pe 's|([0-9a-f]{64})|\1 \\\n|g' | pbcopy

        cargo.crates \
            asn1 0.24.1 c9795210620c0cb3f9a7ce4f882808c38e1ef7b347c90591dceae0886e031fb1 \
            asn1_derive 0.24.1 909e307f1cc32bb8bccbd98f446e6d1bf03fa30f7b53a4337da7181ad30fa11a \
            base64 0.22.1 72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6 \
            bitflags 2.11.1 c4512299f36f043ab09a583e57bceb5a5aab7a73db1805848e8fef3c9e8c78b3 \
            cc 1.2.61 d16d90359e986641506914ba71350897565610e87ce0ad9e6f28569db3dd5c6d \
            cfg-if 1.0.4 9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801 \
            find-msvc-tools 0.1.9 5baebc0774151f905a1a2cc41989300b1e6fbb29aff0ceffa1064fdd3088d582 \
            foreign-types 0.3.2 f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1 \
            foreign-types-shared 0.1.1 00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b \
            heck 0.5.0 2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea \
            itoa 1.0.18 8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682 \
            libc 0.2.186 68ab91017fe16c622486840e4c83c9a37afeff978bd239b5293d61ece587de66 \
            once_cell 1.21.4 9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50 \
            openssl 0.10.79 bf0b434746ee2832f4f0baf10137e1cabb18cbe6912c69e2e33263c45250f542 \
            openssl-macros 0.1.1 a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c \
            openssl-sys 0.9.115 158fe5b292746440aa6e7a7e690e55aeb72d41505e2804c23c6973ad0e9c9781 \
            pem 3.0.6 1d30c53c26bc5b31a98cd02d20f25a7c8567146caf63ed593a9d87b2775291be \
            pkg-config 0.3.33 19f132c84eca552bf34cab8ec81f1c1dcc229b811638f9d283dceabe58c5569e \
            portable-atomic 1.13.1 c33a9471896f1c69cecef8d20cbe2f7accd12527ce60845ff44c153bb2a21b49 \
            proc-macro2 1.0.106 8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934 \
            pyo3 0.28.3 91fd8e38a3b50ed1167fb981cd6fd60147e091784c427b8f7183a7ee32c31c12 \
            pyo3-build-config 0.28.3 e368e7ddfdeb98c9bca7f8383be1648fd84ab466bf2bc015e94008db6d35611e \
            pyo3-ffi 0.28.3 7f29e10af80b1f7ccaf7f69eace800a03ecd13e883acfacc1e5d0988605f651e \
            pyo3-macros 0.28.3 df6e520eff47c45997d2fc7dd8214b25dd1310918bbb2642156ef66a67f29813 \
            pyo3-macros-backend 0.28.3 c4cdc218d835738f81c2338f822078af45b4afdf8b2e33cbb5916f108b813acb \
            quote 1.0.45 41f2619966050689382d2b44f664f4bc593e129785a36d6ee376ddf37259b924 \
            self_cell 1.2.2 b12e76d157a900eb52e81bc6e9f3069344290341720e9178cde2407113ac8d89 \
            shlex 1.3.0 0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64 \
            syn 2.0.117 e665b8803e7b1d2a727f4023456bbbbe74da67099c585258af0ad9c5013b9b99 \
            target-lexicon 0.13.5 adb6935a6f5c20170eeceb1a3835a49e12e19d792f6dd344ccc76a985ca5a6ca \
            unicode-ident 1.0.24 e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75 \
            vcpkg 0.2.15 accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426 \

        depends_build-append \
                    port:py${python.version}-setuptools-rust

        depends_test-append \
                    port:py${python.version}-cryptography_vectors \
                    port:py${python.version}-certifi \
                    port:py${python.version}-pytest-benchmark \
                    port:py${python.version}-pytest-xdist \
                    port:py${python.version}-pretend

        test.run    yes
        test.target tests
    }

    livecheck.type  none
}

# package cryptography_vectors
regsub 27 ${python.versions} "" python_versions_no27
foreach python_version ${python_versions_no27} {
    subport py${python_version}-cryptography_vectors {
        supported_archs noarch
        platforms       "darwin any >= ${cryptography_darwin_min_ver}"

        python.pep517_backend uv
        post-patch {
            reinplace "s|<0.9.0|<1.0.0|g" ${worksrcpath}/vectors/pyproject.toml
            reinplace "s|<0.10.0|<1.0.0|g" ${worksrcpath}/vectors/pyproject.toml
        }

        build.dir   ${worksrcpath}/vectors

        livecheck.type  none
    }
}
