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

python.versions     27 37 38 39 310 311 312
python.pep517       yes

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  c5cdccb51bab89cb05abf0789878f51bbcca16c9 \
                    sha256  0c2a89eca28bd8290d9b7764f0117fcc578df0341f235807d7bffb668ac595b2 \
                    size    36020322

# 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
            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
            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

        # Needs to use the same openssl version as python itself was built with...
        # https://trac.macports.org/ticket/63968
        if { ${python.version} < 38 } {
            openssl.branch  1.1
        } else {
            openssl.branch  3
        }

        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 \
                    port:py${python.version}-pytest-benchmark \
                    port:py${python.version}-pytest-cov \
                    port:py${python.version}-pytest-xdist \
                    port:py${python.version}-pretend

        # 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.15.5 ae3ecbce89a22627b5e8e6e11d69715617138290289e385cde773b1fe50befdb \
                    asn1_derive 0.15.5 861af988fac460ac69a09f41e6217a8fb9178797b76fcc9478444be6a59be19c \
                    autocfg 1.1.0 d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa \
                    base64 0.21.7 9d297deb1925b89f2ccc13d7635fa0714f12c87adce1c75356b39ca9b7178567 \
                    bitflags 1.3.2 bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a \
                    bitflags 2.4.2 ed570934406eb16438a4e976b1b4500774099c13b8cb96eec99f620f05090ddf \
                    cc 1.0.83 f1174fb0b6ec23863f8b971027804a42614e347eafb0a95bf0b12cdae21fc4d0 \
                    cfg-if 1.0.0 baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd \
                    foreign-types 0.3.2 f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1 \
                    foreign-types-shared 0.1.1 00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b \
                    heck 0.4.1 95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8 \
                    indoc 2.0.4 1e186cfbae8084e513daff4240b4797e342f988cecda4fb6c939150f96315fd8 \
                    libc 0.2.152 13e3bf6590cbc649f4d1a3eefc9d5d6eb746f5200ffb04e5e142700b8faa56e7 \
                    lock_api 0.4.11 3c168f8615b12bc01f9c17e2eb0cc07dcae1940121185446edc3744920e8ef45 \
                    memoffset 0.9.0 5a634b1c61a95585bd15607c6ab0c4e5b226e695ff2800ba0cdccddf208c406c \
                    once_cell 1.19.0 3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92 \
                    openssl 0.10.63 15c9d69dd87a29568d4d017cfe8ec518706046a05184e5aea92d0af890b803c8 \
                    openssl-macros 0.1.1 a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c \
                    openssl-sys 0.9.99 22e1bf214306098e4832460f797824c05d25aacdf896f64a985fb0fd992454ae \
                    parking_lot 0.12.1 3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f \
                    parking_lot_core 0.9.9 4c42a9226546d68acdd9c0a280d17ce19bfe27a46bf68784e4066115788d008e \
                    pem 3.0.3 1b8fcc794035347fb64beda2d3b462595dd2753e3f268d89c5aae77e8cf2c310 \
                    pkg-config 0.3.29 2900ede94e305130c13ddd391e0ab7cbaeb783945ae07a279c268cb05109c6cb \
                    portable-atomic 1.6.0 7170ef9988bc169ba16dd36a7fa041e5c4cbeb6a35b76d4c03daded371eae7c0 \
                    proc-macro2 1.0.78 e2422ad645d89c99f8f3e6b88a9fdeca7fabeac836b1002371c4367c8f984aae \
                    pyo3 0.20.3 53bdbb96d49157e65d45cc287af5f32ffadd5f4761438b527b055fb0d4bb8233 \
                    pyo3-build-config 0.20.3 deaa5745de3f5231ce10517a1f5dd97d53e5a2fd77aa6b5842292085831d48d7 \
                    pyo3-ffi 0.20.3 62b42531d03e08d4ef1f6e85a2ed422eb678b8cd62b762e53891c05faf0d4afa \
                    pyo3-macros 0.20.3 7305c720fa01b8055ec95e484a6eca7a83c841267f0dd5280f0c8b8551d2c158 \
                    pyo3-macros-backend 0.20.3 7c7e9b68bb9c3149c5b0cade5d07f953d6d125eb4337723c4ccdb665f1f96185 \
                    quote 1.0.35 291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef \
                    redox_syscall 0.4.1 4722d768eff46b75989dd134e5c353f0d6296e5aaa3132e776cbdb56be7731aa \
                    scopeguard 1.2.0 94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49 \
                    self_cell 1.0.3 58bf37232d3bb9a2c4e641ca2a11d83b5062066f88df7fed36c28772046d65ba \
                    smallvec 1.13.1 e6ecd384b10a64542d77071bd64bd7b231f4ed5940fba55e98c3de13824cf3d7 \
                    syn 2.0.48 0f3531638e407dfc0814761abb7c00a5b54992b849452a0646b7f65c9f770f3f \
                    target-lexicon 0.12.13 69758bda2e78f098e4ccb393021a0963bb3442eac05f135c30f61b7370bbafae \
                    unicode-ident 1.0.12 3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b \
                    unindent 0.2.3 c7de7d73e1754487cb58364ee906a499937a0dfabd86bcb980fa99ec8c8fa2ce \
                    vcpkg 0.2.15 accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426 \
                    windows-targets 0.48.5 9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c \
                    windows_aarch64_gnullvm 0.48.5 2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8 \
                    windows_aarch64_msvc 0.48.5 dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc \
                    windows_i686_gnu 0.48.5 a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e \
                    windows_i686_msvc 0.48.5 8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406 \
                    windows_x86_64_gnu 0.48.5 53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e \
                    windows_x86_64_gnullvm 0.48.5 0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc \
                    windows_x86_64_msvc 0.48.5 ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538

        pre-test {
            test.env-append \
                    PYTHONPATH=[glob -nocomplain ${worksrcpath}/build/lib*]
        }

        test.run    yes
        test.cmd    py.test-${python.branch}
        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}"

        if { ${python.version} >= 37 } {
            python.pep517_backend   flit
        }

        build.dir   ${worksrcpath}/vectors

        livecheck.type  none
    }
}
