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

name                    python26
# Remember to keep py26-tkinter and py26-gdbm's versions sync'd with this
version                 2.6.9
revision                7
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    933a811f11e3db3d73ae492f6c3a7a76 \
                        rmd160 42edf7c9f2f64b77ab173de30ea453257c2c06b3 \
                        sha256 cae7bb995006ea5b703d9d28446f694894c441fe4bfb95d561c0ac908cd06e41

# patch-Lib-distutils-dist.py.diff comes from
# <https://bugs.python.org/issue1180>
# Setting PYTHONHOME with patch-Mac-IDLE-Makefile.in.diff,
# patch-Mac-Makefile.in.diff, and patch-Mac-PythonLauncher-Makefile.in.diff
# is to work around https://bugs.python.org/issue5766
patchfiles              patch-Makefile.pre.in.diff \
                        patch-setup.py.diff \
                        patch-Lib-cgi.py.diff \
                        patch-Lib-distutils-dist.py.diff \
                        patch-Mac-IDLE-Makefile.in.diff \
                        patch-Mac-Makefile.in.diff \
                        patch-Mac-PythonLauncher-Makefile.in.diff \
                        patch-Mac-Tools-Doc-setup.py.diff \
                        patch-setup.py-db46.diff \
                        patch-Lib-ctypes-macholib-dyld.py.diff \
                        patch-setup.py-disabled_modules.diff \
                        patch-libedit.diff \
                        yosemite.patch \
                        improve-QuickTime.h-check.patch \
                        darwin20.diff \
                        implicit.patch \
                        libedit-types.patch

depends_lib             port:gettext-runtime port:zlib \
                        port:sqlite3 port:db46 port:ncurses \
                        port:bzip2 port:libedit
depends_run             port:python_select \
                        port:python2_select

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

configure.args          --enable-framework=${frameworks_dir} \
                        --enable-ipv6
configure.ccache        no

# signed integer overflow is normally undefined as per the C spec, but python
# needs it to be defined as twos-complement
configure.cflags-append -fwrapv
openssl.branch 1.0
openssl.configure build_flags

post-patch {
   reinplace "s|@@PREFIX@@|${prefix}|g" ${worksrcpath}/Lib/cgi.py \
      ${worksrcpath}/Lib/ctypes/macholib/dyld.py
   reinplace "s|@@APPLICATIONS_DIR@@|${applications_dir}|g" \
      ${worksrcpath}/Mac/Makefile.in ${worksrcpath}/Mac/IDLE/Makefile.in \
      ${worksrcpath}/Mac/Tools/Doc/setup.py \
      ${worksrcpath}/Mac/PythonLauncher/Makefile.in

   # 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

   # http://trac.macports.org/ticket/21517
   system -W ${worksrcpath} "ed - Modules/_localemodule.c < ${filespath}/_localemodule.c.ed"
   system -W ${worksrcpath} "ed - Lib/locale.py < ${filespath}/locale.py.ed"

   if {${os.platform} eq "darwin" && ${os.major} > 9} {
      # http://trac.macports.org/ticket/21559
      system -W ${worksrcpath} "ed - Modules/posixmodule.c < ${filespath}/posixmodule.c.ed"
   }
}

build.target            all

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 python2 python2-$pythonNoDot $pythonNoDot]

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

    sudo port select --set python $pythonNoDot
    sudo port select --set python2 $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.1 ${destroot}${prefix}/share/man/man1/python${branch}.1
   ln -s ${framewdir}/Python ${destroot}${prefix}/lib/libpython${branch}.dylib
   ln -s ${framewdir}/bin/2to3 ${destroot}${prefix}/bin/2to3-${branch}

   # Without this, LINKFORSHARED is set to
   # ... $(PYTHONFRAMEWORKDIR)/Versions/$(VERSION)/$(PYTHONFRAMEWORK)
   # (this becomes Python.framework/Versions/2.6/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) $(LIBS) $(SYSLIBS)|} \
      ${destroot}${framewdir}/lib/python${branch}/config/Makefile

   foreach unversionedFile {idle pydoc python python-config pythonw smtpd.py} {
      delete ${destroot}${prefix}/bin/${unversionedFile}
   }
   
   # remove -arch flags from the config
   reinplace -E {s|-arch [a-z0-9_]+||g} \
      ${destroot}${framewdir}/lib/python${branch}/config/Makefile
}

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
   }
   post-patch {
		if {![file exists /usr/lib/libSystemStubs.a]} {
			reinplace s/-lSystemStubs//g ${worksrcpath}/configure
		}
   }
}

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

variant ucs4 description {Enable support for UCS4} {
   configure.args-append   --enable-unicode=ucs4
}

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