Issue #15663: Tcl/Tk 8.5.15 is now included with the OS X 10.6+
64-bit/32-bit installer for 10.6+.  It is no longer necessary
to install a third-party version of Tcl/Tk 8.5 to work around the
problems in the Apple-supplied Tcl/Tk 8.5 shipped in OS X 10.6
and later releases.
diff --git a/Mac/BuildScript/build-installer.py b/Mac/BuildScript/build-installer.py
index a9d4091..de1a980 100755
--- a/Mac/BuildScript/build-installer.py
+++ b/Mac/BuildScript/build-installer.py
@@ -193,6 +193,44 @@
 
     LT_10_5 = bool(DEPTARGET < '10.5')
 
+    if DEPTARGET > '10.5':
+        result.extend([
+          dict(
+              name="Tcl 8.5.15",
+              url="ftp://ftp.tcl.tk/pub/tcl//tcl8_5/tcl8.5.15-src.tar.gz",
+              checksum='f3df162f92c69b254079c4d0af7a690f',
+              buildDir="unix",
+              configure_pre=[
+                    '--enable-shared',
+                    '--enable-threads',
+                    '--libdir=/Library/Frameworks/Python.framework/Versions/%s/lib'%(getVersion(),),
+              ],
+              useLDFlags=False,
+              install='make TCL_LIBRARY=%(TCL_LIBRARY)s && make install TCL_LIBRARY=%(TCL_LIBRARY)s DESTDIR=%(DESTDIR)s'%{
+                  "DESTDIR": shellQuote(os.path.join(WORKDIR, 'libraries')),
+                  "TCL_LIBRARY": shellQuote('/Library/Frameworks/Python.framework/Versions/%s/lib/tcl8.5'%(getVersion())),
+                  },
+              ),
+          dict(
+              name="Tk 8.5.15",
+              url="ftp://ftp.tcl.tk/pub/tcl//tcl8_5/tk8.5.15-src.tar.gz",
+              checksum='55b8e33f903210a4e1c8bce0f820657f',
+              buildDir="unix",
+              configure_pre=[
+                    '--enable-aqua',
+                    '--enable-shared',
+                    '--enable-threads',
+                    '--libdir=/Library/Frameworks/Python.framework/Versions/%s/lib'%(getVersion(),),
+              ],
+              useLDFlags=False,
+              install='make TCL_LIBRARY=%(TCL_LIBRARY)s TK_LIBRARY=%(TK_LIBRARY)s && make install TCL_LIBRARY=%(TCL_LIBRARY)s TK_LIBRARY=%(TK_LIBRARY)s DESTDIR=%(DESTDIR)s'%{
+                  "DESTDIR": shellQuote(os.path.join(WORKDIR, 'libraries')),
+                  "TCL_LIBRARY": shellQuote('/Library/Frameworks/Python.framework/Versions/%s/lib/tcl8.5'%(getVersion())),
+                  "TK_LIBRARY": shellQuote('/Library/Frameworks/Python.framework/Versions/%s/lib/tk8.5'%(getVersion())),
+                  },
+                ),
+        ])
+
     if getVersionTuple() >= (3, 3):
         result.extend([
           dict(
@@ -526,6 +564,20 @@
                 % frameworks['Tk'],
             ]
 
+    # For 10.6+ builds, we build two versions of _tkinter:
+    #    - the traditional version (renamed to _tkinter.so.framework) linked
+    #       with /Library/Frameworks/{Tcl,Tk}.framework
+    #    - the default version linked with our private copies of Tcl and Tk
+    if DEPTARGET > '10.5':
+        EXPECTED_SHARED_LIBS['_tkinter.so.framework'] = \
+            EXPECTED_SHARED_LIBS['_tkinter.so']
+        EXPECTED_SHARED_LIBS['_tkinter.so'] = [
+                "/Library/Frameworks/Python.framework/Versions/%s/lib/libtcl%s.dylib"
+                    % (getVersion(), frameworks['Tcl']),
+                "/Library/Frameworks/Python.framework/Versions/%s/lib/libtk%s.dylib"
+                    % (getVersion(), frameworks['Tk']),
+                ]
+
     # Remove inherited environment variables which might influence build
     environ_var_prefixes = ['CPATH', 'C_INCLUDE_', 'DYLD_', 'LANG', 'LC_',
                             'LD_', 'LIBRARY_', 'PATH', 'PYTHON']
@@ -634,13 +686,19 @@
 
     XXX: This function assumes that archives contain a toplevel directory
     that is has the same name as the basename of the archive. This is
-    save enough for anything we use.
+    safe enough for almost anything we use.  Unfortunately, it does not
+    work for current Tcl and Tk source releases where the basename of
+    the archive ends with "-src" but the uncompressed directory does not.
+    For now, just special case Tcl and Tk tar.gz downloads.
     """
     curdir = os.getcwd()
     try:
         os.chdir(builddir)
         if archiveName.endswith('.tar.gz'):
             retval = os.path.basename(archiveName[:-7])
+            if ((retval.startswith('tcl') or retval.startswith('tk'))
+                    and retval.endswith('-src')):
+                retval = retval[:-4]
             if os.path.exists(retval):
                 shutil.rmtree(retval)
             fp = os.popen("tar zxf %s 2>&1"%(shellQuote(archiveName),), 'r')
@@ -904,6 +962,23 @@
     print("Running make")
     runCommand("make")
 
+    # For deployment targets of 10.6 and higher, we build our own version
+    # of Tcl and Cocoa Aqua Tk libs because the Apple-supplied Tk 8.5 is
+    # out-of-date and has critical bugs.  Save the _tkinter.so that was
+    # linked with /Library/Frameworks/{Tck,Tk}.framework and build
+    # another _tkinter.so linked with our private Tcl and Tk libs.
+    if DEPTARGET > '10.5':
+        runCommand("find build -name '_tkinter.so' "
+                        " -execdir mv '{}' '{}'.framework \;")
+        print("Running make to rebuild _tkinter")
+        runCommand("make TCLTK_INCLUDES='-I%s/libraries/usr/local/include' "
+                "TCLTK_LIBS='-L%s/libraries/usr/local/lib -ltcl8.5 -ltk8.5'"%(
+            shellQuote(WORKDIR)[1:-1],
+            shellQuote(WORKDIR)[1:-1]))
+        # make a backup copy, just in case
+        runCommand("find build -name '_tkinter.so' "
+                        " -execdir cp -p '{}' '{}'.private \;")
+
     print("Running make install")
     runCommand("make install DESTDIR=%s"%(
         shellQuote(rootDir)))