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

name                python33

epoch               20141012
# Remember to keep py33-tkinter and py33-gdbm's versions sync'd with this
version             3.3.7
revision            3

deprecated.eol_version  yes

set major           [lindex [split $version .] 0]
set branch          [join [lrange [split ${version} .] 0 1] .]
categories          lang
license             PSF
maintainers         {jmr @jmroot}

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        ${homepage}ftp/python/${version}/

distname            Python-${version}
use_xz              yes
checksums           md5 84e2f12f044ca53b577f6224c53f82ac \
                    rmd160 3a915ea86ef3826fc0483eb94a2b4e2b9ad787b1 \
                    sha256 85f60c327501c36bc18c33370c14d472801e6af2f901dafbba056f61685429fe

patchfiles          patch-setup.py.diff \
                    patch-Lib-cgi.py.diff \
                    patch-Lib-distutils-dist.py.diff \
                    patch-configure.diff \
                    patch-libedit.diff \
                    omit-local-site-packages.patch \
                    patch-Include-pyport.h.diff \
                    uuid-64bit.patch \
                    darwin20.diff \
                    libedit-types.patch

# https://bugs.python.org/issue21811
patchfiles-append   yosemite-configure-fixes.patch \
                    yosemite-python-fixes.patch

depends_lib         port:zlib port:openssl10 \
                    port:sqlite3 port:ncurses \
                    port:bzip2 port:libedit port:gettext-runtime \
                    port:xz
depends_run         port:python_select \
                    port:python3_select

if {${os.platform} eq "darwin" && ${os.major} >= 20} {
    supported_archs x86_64
}

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

configure.args      --enable-framework=${frameworks_dir} \
                    --enable-ipv6 \
                    --enable-loadable-sqlite-extensions \
                    --with-computed-gotos
configure.ccache    no
configure.cppflags-prepend -I${prefix}/include/openssl-1.0
configure.ldflags-prepend -L${prefix}/lib/openssl-1.0

post-patch {
    reinplace "s|@@PREFIX@@|${prefix}|g" ${worksrcpath}/Lib/cgi.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

    # See http://trac.macports.org/changeset/37861
    reinplace "s|xargs -0 rm -r|/usr/bin/xargs -0 /bin/rm -r|g" \
      ${worksrcpath}/Mac/PythonLauncher/Makefile.in
}

build.target        all

# test_cmd_line fails
# see https://bugs.python.org/issue4388 and
#     https://bugs.python.org/issue4474
test.run            yes
test.target         test

destroot.target     frameworkinstall maninstall

# ensure that correct compiler is used
build.args-append   MAKE="${build.cmd} CC=${configure.cc}"
destroot.args-append    MAKE="${destroot.cmd} CC=${configure.cc}"

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

set confdir config-${branch}m

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
"

post-destroot {
    set framewpath ${frameworks_dir}/Python.framework
    set framewdir  ${framewpath}/Versions/${branch}

    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/

    # Without this, LINKFORSHARED is set to
    # ... $(PYTHONFRAMEWORKDIR)/Versions/$(VERSION)/$(PYTHONFRAMEWORK)
    # (this becomes Python.framework/Versions/3.3/Python) which doesn't
    # quite work (see ticket #15099); instead we mirror the behavior of
    # `python-config --ldflags` here.
    reinplace {s|^\(LINKFORSHARED=\).*$|\1 -L$(LIBPL) -lpython$(VERSION)$(ABIFLAGS) $(LIBS) $(SYSLIBS)|} \
        ${destroot}${framewdir}/lib/python${branch}/config-3.3m/Makefile

    # remove -arch flags from the config
    reinplace -E {s|-arch [a-z0-9_]+||g} \
      ${destroot}${framewdir}/lib/python${branch}/${confdir}/Makefile \
      ${destroot}${framewdir}/lib/python${branch}/_sysconfigdata.py
    # also remove gettext overlinking
    reinplace "s|-lintl||" \
      ${destroot}${framewdir}/lib/python${branch}/${confdir}/Makefile \
      ${destroot}${framewdir}/lib/python${branch}/_sysconfigdata.py
}

platform darwin {
    post-configure {
        # See http://trac.macports.org/ticket/18376
        set oldmtime [file mtime ${worksrcpath}/pyconfig.h]
        system -W ${worksrcpath} "ed - pyconfig.h < ${filespath}/pyconfig.ed"
        file mtime ${worksrcpath}/pyconfig.h $oldmtime
    }
}

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} != "" } {
            reinplace \
              "s|ARCH_RUN_32BIT=\".*\"|ARCH_RUN_32BIT=\"arch ${arch_run_32bit}\"|" \
            ${worksrcpath}/configure
        }
    }
    if {${configure.sdkroot} != ""} {
        configure.args-append   --enable-universalsdk=${configure.sdkroot}
    } else {
        configure.args-append   --enable-universalsdk=/
    }
    post-configure {
        set oldmtime [file mtime ${worksrcpath}/pyconfig.h]
        system -W ${worksrcpath} "ed - pyconfig.h < ${filespath}/pyconfig.h-universal.ed"
        file mtime ${worksrcpath}/pyconfig.h $oldmtime
    }

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

livecheck.type      regex
livecheck.url       ${homepage}downloads/
livecheck.regex     Python (${branch}(?:\\.\\d+)*)
