Skip to content

Commit 86dc45e

Browse files
committed
patching python build to include tk support
1 parent 87e1dd9 commit 86dc45e

2 files changed

Lines changed: 30 additions & 3 deletions

File tree

bin/compile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,9 @@ set_env PYTHONHOME "\${HOME}/.heroku/python"
228228
# Set variables for C libraries.
229229
set_env LIBRARY_PATH "\${HOME}/.heroku/python/lib\${LIBRARY_PATH:+:\${LIBRARY_PATH}}"
230230
set_env LD_LIBRARY_PATH "\${HOME}/.heroku/python/lib\${LD_LIBRARY_PATH:+:\${LD_LIBRARY_PATH}}"
231+
# Ensure Tk can locate its bundled runtime libraries when the system image lacks Tk packages.
232+
set_env TCL_LIBRARY "\${HOME}/.heroku/python/lib/tcl8.6"
233+
set_env TK_LIBRARY "\${HOME}/.heroku/python/lib/tk8.6"
231234
# Locale.
232235
set_default_env LANG en_US.UTF-8
233236
# Tell Python to look for Python modules in the /app dir. Don't change this.

builds/build_python_runtime.sh

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,10 @@ if [[ "${PYTHON_MAJOR_VERSION}" != +(3.9|3.10) ]]; then
146146
)
147147
fi
148148

149-
# Allow additional configure flags to be supplied via PYTHON_CONFIGURE_OPTS while keeping
150-
# the defaults (such as --with-tcltk) earlier in the list so user-supplied values win.
149+
# Tk is discovered via pkg-config from the `tk-dev`/`tcl-dev` packages in the build image,
150+
# so no extra CPPFLAGS/LDFLAGS/PKG_CONFIG_PATH tweaking is required here. Allow additional
151+
# configure flags to be supplied via PYTHON_CONFIGURE_OPTS while keeping the defaults earlier
152+
# in the list so user-supplied values win.
151153
if [[ -n "${PYTHON_CONFIGURE_OPTS:-}" ]]; then
152154
# shellcheck disable=SC2206 # We intentionally rely on word-splitting like configure scripts.
153155
PYTHON_CONFIGURE_OPTS_ARRAY=( ${PYTHON_CONFIGURE_OPTS} )
@@ -190,9 +192,31 @@ if [[ "${PYTHON_MAJOR_VERSION}" == +(3.9) ]]; then
190192
# - `lib/python3.9/config-3.9-x86_64-linux-gnu/libpython3.9.a`
191193
find "${INSTALL_DIR}" -type f -name '*.a' -print -exec strip --strip-unneeded '{}' +
192194
elif ! find "${INSTALL_DIR}" -type f -name '*.a' -print -exec false '{}' +; then
193-
abort "Unexpected static libraries found!"
195+
abort "Unexpected static libraries found!"
194196
fi
195197

198+
# Bundle the Tk runtime shared libraries and data files so they remain available on the Heroku
199+
# run image (which doesn't include Tk by default). This also avoids runtime lookups against the
200+
# system library paths which would break once the archive is relocated by the buildpack.
201+
shopt -s nullglob
202+
TCL_TK_VERSION="$(tclsh <<< 'puts $tcl_version')"
203+
TK_RUNTIME_LIBS=(/usr/lib/*/libtcl${TCL_TK_VERSION}.so*)
204+
TK_RUNTIME_LIBS+=(/usr/lib/*/libtk${TCL_TK_VERSION}.so*)
205+
if [[ "${#TK_RUNTIME_LIBS[@]}" -eq 0 ]]; then
206+
abort "Tk runtime shared libraries weren't found for version ${TCL_TK_VERSION}!"
207+
fi
208+
209+
TK_LIBRARY_DIRS=(/usr/share/tcltk/tcl${TCL_TK_VERSION})
210+
TK_LIBRARY_DIRS+=(/usr/share/tcltk/tk${TCL_TK_VERSION})
211+
for tk_library_dir in "${TK_LIBRARY_DIRS[@]}"; do
212+
if [[ ! -d "${tk_library_dir}" ]]; then
213+
abort "Tk runtime library directory '${tk_library_dir}' wasn't found!"
214+
fi
215+
done
216+
217+
cp --dereference --target-directory "${INSTALL_DIR}/lib/" --verbose "${TK_RUNTIME_LIBS[@]}"
218+
cp --archive --target-directory "${INSTALL_DIR}/lib/" --verbose "${TK_LIBRARY_DIRS[@]}"
219+
196220
# Remove unneeded test directories, similar to the official Docker Python images:
197221
# https://github.com/docker-library/python
198222
# This is a no-op on Python 3.11+, since --disable-test-modules will have prevented

0 commit comments

Comments
 (0)