# -*- 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 select 1.0

name                python38-devel

version             20180919

set branch          3.8
categories          lang
license             PSF
platforms           darwin
maintainers         {yan12125 @yan12125} openmaintainer

description         An interpreted, object-oriented programming language
long_description    Python is an interpreted, interactive, object-oriented \
                    programming language.

homepage            https://www.python.org/
master_sites        https://github.com/python/cpython/archive

set commit          b3b8cb419e496629873fa7dda82a01863f58617a

distname            ${commit}
use_zip             yes
checksums           md5     a74a616ede440d2737c261c9eccfb51a \
                    rmd160  a78f6aa52369606989152a57ccd3beeecfd06854 \
                    sha256  ad62eac8962df550c0f531404762ad8a4948631b650682a8a28d3cf94b4d91af \
                    size    25324108

worksrcdir          cpython-${commit}

patchfiles          patch-setup.py.diff \
                    patch-Lib-cgi.py.diff \
                    patch-configure.diff \
                    patch-Lib-ctypes-macholib-dyld.py.diff \
                    patch-libedit.diff \
                    patch-configure-xcode4bug.diff

depends_build       port:pkgconfig
depends_lib         port:bzip2 \
                    port:expat \
                    port:gettext \
                    port:libedit \
                    port:libffi \
                    port:ncurses \
                    path:lib/libssl.dylib:openssl \
                    port:sqlite3 \
                    port:xz \
                    port:zlib
depends_run         port:python_select \
                    port:python3_select

# blacklist llvm-gcc-4.2 compiler known to produce bad code
compiler.blacklist  *llvm-gcc-4.2

# ensurepip arg may be removed later, now conflicts with pip and setuptools
# packages
configure.args      --enable-framework=${frameworks_dir} \
                    --enable-ipv6 \
                    --enable-loadable-sqlite-extensions \
                    --with-computed-gotos \
                    --with-ensurepip=no \
                    --with-system-expat \
                    --with-system-ffi

configure.ccache    no

post-patch {
    reinplace "s|@@PREFIX@@|${prefix}|g" \
      ${worksrcpath}/Lib/cgi.py \
      ${worksrcpath}/Lib/ctypes/macholib/dyld.py

    reinplace "s|/setup.py|/setup.py --no-user-cfg|" ${worksrcpath}/Makefile.pre.in

    # replace /Applications with ${applications_dir}
    reinplace "s|@@APPLICATIONS_DIR@@|${applications_dir}|" \
      ${worksrcpath}/configure
}

build.target        all

test.run            yes
test.target         test

destroot.target     frameworkinstall maninstall

set pythonNoDot python[string map {. {}} $branch]
select.entries      [list python python-$pythonNoDot $pythonNoDot] \
                    [list python3 python3-$pythonNoDot $pythonNoDot]

platform darwin {
    set framewpath  ${frameworks_dir}/Python.framework
    set framewdir   ${framewpath}/Versions/${branch}
    set confdir     config-${branch}m-darwin

    post-configure {
        # See https://trac.macports.org/ticket/18376
        system -W ${worksrcpath} "ed - pyconfig.h < ${filespath}/pyconfig.ed"
    }

    post-build {
        set buildlibdir [lindex [glob -directory ${worksrcpath}/build lib.*-*-*-${branch}] 0]
        # Without this, LINKFORSHARED is set to
        # ... $(PYTHONFRAMEWORKDIR)/Versions/$(VERSION)/$(PYTHONFRAMEWORK)
        # (this becomes Python.framework/Versions/3.8/Python) which doesn't
        # work for dependents that incorrectly use this variable to find out
        # how to link against python (see ticket #15099); instead we mirror
        # the behavior of `python-config --ldflags` here.
        system -W ${buildlibdir} "awk -F : \
            \"/'LINKFORSHARED'/ {printf \\\"%s: '-L${framewdir}/lib/python${branch}/${confdir}\
            -lpython${branch}m -ldl -framework CoreFoundation',\\n\\\", \\\$1; getline; next} {print}\"\
            _sysconfigdata_m_darwin_darwin.py > _sysconfigdata_m_darwin_darwin.py.new"
        file rename -force ${buildlibdir}/_sysconfigdata_m_darwin_darwin.py.new \
            ${buildlibdir}/_sysconfigdata_m_darwin_darwin.py

        # remove -arch flags from the config
        reinplace -E {s|-arch [a-z0-9_]+||g} \
            ${buildlibdir}/_sysconfigdata_m_darwin_darwin.py

        # also remove gettext overlinking
        reinplace "s|-lintl||" \
            ${buildlibdir}/_sysconfigdata_m_darwin_darwin.py
    }

    post-destroot {
        foreach dir { Headers Resources Python Versions/Current } {
            file delete ${destroot}${framewpath}/${dir}
        }

        ln -s ${framewdir}/share/man/man1/python${branch}.1 ${destroot}${prefix}/share/man/man1/
        ln -s ${framewdir}/lib/pkgconfig/python-${branch}.pc ${destroot}${prefix}/lib/pkgconfig/
        ln -s ${framewdir}/lib/pkgconfig/python-${branch}.pc ${destroot}${prefix}/lib/pkgconfig/python-${branch}m.pc

        # Also make the sysconfig changes in the Makefile
        reinplace {s|^\(LINKFORSHARED=\).*$|\1 -L$(LIBPL) -lpython$(VERSION)$(ABIFLAGS) $(LIBS) $(SYSLIBS)|} \
            ${destroot}${framewdir}/lib/python${branch}/${confdir}/Makefile

        reinplace -E {s|-arch [a-z0-9_]+||g} \
           ${destroot}${framewdir}/lib/python${branch}/${confdir}/Makefile

        reinplace "s|-lintl||" \
           ${destroot}${framewdir}/lib/python${branch}/${confdir}/Makefile
    }
}

post-destroot {
    foreach unversioned {2to3 idle3 pydoc3 python3 python3-config pyvenv} {
        delete ${destroot}${prefix}/bin/${unversioned}
    }
}

notes "
To make this the default Python or Python 3 (i.e., the version run by\
the 'python' or 'python3' commands), run one or both of:

    sudo port select --set python $pythonNoDot
    sudo port select --set python3 $pythonNoDot
"

variant universal {
    post-patch {
        set universal_arch_flags {}
        set arch_run_32bit {}
        foreach arch ${universal_archs} {
            lappend universal_arch_flags -arch ${arch}
            if {${arch} eq "i386" || ${arch} eq "ppc"} {
                lappend arch_run_32bit -${arch}
            }
        }
        reinplace \
          "s|UNIVERSAL_ARCH_FLAGS=\".*\"|UNIVERSAL_ARCH_FLAGS=\"${universal_arch_flags}\"|" \
          ${worksrcpath}/configure
        if {${arch_run_32bit} ne ""} {
            reinplace \
              "s|ARCH_RUN_32BIT=\".*\"|ARCH_RUN_32BIT=\"arch ${arch_run_32bit}\"|" \
              ${worksrcpath}/configure
        }
    }
    if {${configure.sdkroot} ne ""} {
        configure.args-append   --enable-universalsdk=${configure.sdkroot}
    } else {
        configure.args-append   --enable-universalsdk=/
    }
    post-configure {
        system -W ${worksrcpath} "ed - pyconfig.h < ${filespath}/pyconfig.h-universal.ed"
    }

    post-destroot {
        foreach unversioned {python3-32} {
            delete ${destroot}${prefix}/bin/${unversioned}
        }
    }
}

variant optimizations description {Compile with LTO and PGO. Build time greatly increased} {
    configure.args-append   --enable-optimizations
}