diff --git a/plugins/python-build/bin/python-build b/plugins/python-build/bin/python-build index bd0c9b5f..d6a77ed5 100755 --- a/plugins/python-build/bin/python-build +++ b/plugins/python-build/bin/python-build @@ -1880,26 +1880,24 @@ use_macports_zlib() { use_homebrew_tcltk() { can_use_homebrew || return 1 + local tcltk_formula # Since https://github.com/Homebrew/homebrew-core/commit/f10e88617b41555193c22fdcba6109fe82155ee2 (10.11.2024), - # tcl-tk is 9.0 which is not compatible with CPython as of this writing - # but we'll keep it as backup for cases like non-updated Homebrew - local tcltk - for tcltk in ${PYTHON_BUILD_TCLTK_FORMULA:-tcl-tk@8 tcl-tk}; do - local tcltk_libdir="$(brew --prefix "${tcltk}" 2>/dev/null || true)" - if [ -d "$tcltk_libdir" ]; then - echo "python-build: use tcl-tk from homebrew" - local tcltk_version="$(sh -c '. '"$tcltk_libdir"'/lib/tclConfig.sh; echo $TCL_VERSION')" - local tcltk_includes tcltk_libs + # tcl-tk is 9.0. It's supported in CPython since 3.14.0a1 (gh-124111) but is not officially supported as of 3.14.0 + # so we prefer 8 + for tcltk_formula in ${PYTHON_BUILD_TCLTK_FORMULA:-tcl-tk@8 tcl-tk}; do + local tcltk_prefix="$(brew --prefix "${tcltk_formula}" 2>/dev/null || true)" + if [ -d "$tcltk_prefix" ]; then + echo "python-build: use ${tcltk_formula} from homebrew" # In Homebrew Tcl/Tk 8.6.13, headers have been moved to the 'tcl-tk' subdir. - # We're not using tclConfig.sh here 'cuz it produces the version-specific path to /Cellar - # and we'd rather have rpath set to /opt/<...> to allow micro release upgrades without rebuilding - # XXX: do use tclConfig.sh and translate the paths if more shenanigans appear in later releases - if [ -d "$tcltk_libdir/include/tcl-tk" ]; then - tcltk_includes="-I$tcltk_libdir/include/tcl-tk" - else - tcltk_includes="-I$tcltk_libdir/include" + local tcltk_includes="$(sh -c 'cd '"$tcltk_prefix"'/lib; . ./tclConfig.sh; . ./tkConfig.sh; echo "$TCL_INCLUDE_SPEC $TK_INCLUDE_SPEC"')" + # Homebrew Tcl/Tk 9 is built with external libtommath. CPython's build as of 3.14.0 does not detect that and fails to link to tommath symbols + local tcltk_cflags + if sh -c '. '"$tcltk_prefix"'/lib/tclConfig.sh; echo "$TCL_DEFS"' | grep -qwFe '-DTCL_WITH_EXTERNAL_TOMMATH=1'; then + tcltk_cflags="-DTCL_WITH_EXTERNAL_TOMMATH=1" fi - tcltk_libs="-L$tcltk_libdir/lib -ltcl$tcltk_version -ltk$tcltk_version" + # For some reason, keg-only tcl-tk@8 successfully links with Tkinter without specifying rpath, with `/opt' rpath + # so no need to translate /Cellar path to /opt path + local tcltk_libs="$(sh -c 'cd '"$tcltk_prefix"'/lib; . ./tclConfig.sh; . ./tkConfig.sh; echo "$TCL_LIB_SPEC $TK_LIB_SPEC"')" # Since 2.7.6, 3.3.3, 3.4.0 (Issue #1584): --with-tcltk-includes + --with-tcltk-libs Configure options # Since 3.11.0 (bpo-45847): `pkg-config` call, TCLTK_CFLAGS + TCLTK_LIBS override if [[ -n "$PYTHON_BUILD_TCLTK_USE_PKGCONFIG" ]]; then @@ -1907,14 +1905,15 @@ use_homebrew_tcltk() { # There's no way to provide a fallback only if it's is not present # and Configure's logic of detecting if it's present is complicated. # So we just override it always - export TCLTK_CFLAGS="$tcltk_includes" + export TCLTK_CFLAGS="$tcltk_includes${tcltk_cflags:+ $tcltk_cflags}" export TCLTK_LIBS="$tcltk_libs" else package_option python configure --with-tcltk-includes="$tcltk_includes" package_option python configure --with-tcltk-libs="$tcltk_libs" + [[ -n $tcltk_cflags ]] && export CFLAGS="${tcltk_cflags}${CFLAGS:+ $CFLAGS}" fi #set in either case as a failsafe - export PKG_CONFIG_PATH="${tcltk_libdir}/lib/pkgconfig${PKG_CONFIG_PATH:+:$PKG_CONFIG_PATH}" + export PKG_CONFIG_PATH="${tcltk_prefix}/lib/pkgconfig${PKG_CONFIG_PATH:+:$PKG_CONFIG_PATH}" lock_in homebrew return 0 diff --git a/plugins/python-build/test/build.bats b/plugins/python-build/test/build.bats index d123103e..f59c4759 100644 --- a/plugins/python-build/test/build.bats +++ b/plugins/python-build/test/build.bats @@ -68,6 +68,20 @@ assert_build_log() { assert_output } +stub_tcltk() { + local tcltk_prefix="${1:?}" + mkdir -p "$tcltk_prefix/lib" + cat >"$tcltk_prefix/lib/tclConfig.sh" <"$tcltk_prefix/lib/tkConfig.sh" <>"$tcl_tk_libdir/lib/tclConfig.sh" + stub_tcltk "${tcl_tk_libdir:=$BATS_TEST_TMPDIR/homebrew-tcl-tk}" stub uname '-s : echo Darwin' stub sw_vers '-productVersion : echo 1010' @@ -788,7 +799,40 @@ DEF assert_build_log <>"$tcl_tk_libdir/lib/tclConfig.sh" <>"$tcl_tk_libdir/lib/tclConfig.sh" + stub_tcltk "${tcl_tk_libdir:=$BATS_TEST_TMPDIR/homebrew-tcl-tk}" stub uname '-s : echo Darwin' stub sw_vers '-productVersion : echo 1010' @@ -822,7 +863,7 @@ DEF assert_build_log <>"$tcl_tk_libdir/lib/tclConfig.sh" + stub_tcltk "${tcl_tk_libdir:=$BATS_TEST_TMPDIR/homebrew-tcl-tk}" stub uname '-s : echo Darwin' stub sw_vers '-productVersion : echo 1010' @@ -857,7 +895,42 @@ DEF assert_build_log <>"$tcl_tk_libdir/lib/tclConfig.sh" <