From e56962b35781ee9512c99aeb8769f4e76b663596 Mon Sep 17 00:00:00 2001 From: tillhainbach <33448151+tillhainbach@users.noreply.github.com> Date: Fri, 17 Sep 2021 13:56:53 +0200 Subject: [PATCH] allow tcl-tk as argument or try with homebrew by default Replacement for #1409 (#1646) * feat(python-build): allow tcl-tk as argument or default to homebrew * refactor(python-build): detect tcl-tk-libs from confugre_opts_arr --- plugins/python-build/bin/python-build | 83 ++++++++++++++++++++++----- plugins/python-build/test/build.bats | 68 +++++++++++++++++++++- 2 files changed, 135 insertions(+), 16 deletions(-) diff --git a/plugins/python-build/bin/python-build b/plugins/python-build/bin/python-build index c08fadb5..a18680a4 100755 --- a/plugins/python-build/bin/python-build +++ b/plugins/python-build/bin/python-build @@ -773,6 +773,7 @@ build_package_standard_build() { local PACKAGE_CFLAGS="${package_var_name}_CFLAGS" if [ "$package_var_name" = "PYTHON" ]; then + use_tcltk || true use_homebrew_readline || use_freebsd_pkg || true if is_mac -ge 1014; then use_xcode_sdk_zlib || use_homebrew_zlib || true @@ -1523,6 +1524,75 @@ use_xcode_sdk_zlib() { fi } +use_homebrew_tcltk() { + # get the version from the folder that homebrew versions + local tcltk_version_long="$(ls "$(brew --cellar tcl-tk)")" + local tcltk_version="${tcltk_version_long%.*}" + local tcltk_flags="--with-tcltk-includes=-I$tcltk_libdir/include --with-tcltk-libs=-L$tcltk_libdir/lib -ltcl$tcltk_version -ltk$tcltk_version" + echo "python-build: use tcl-tk from homebrew" + package_option python configure --with-tcltk-libs="-L$tcltk_libdir/lib -ltcl$tcltk_version -ltk$tcltk_version" + package_option python configure --with-tcltk-includes="-I$tcltk_libdir/include" +} + +# FIXME: this function is a workaround for #1125 +# once fixed, it should be removed. +use_custom_tcltk() { + local tcltk_ops="$1" + local tcltk_ops_flag="--with-tcltk-libs=" + # get tcltk libs + local tcltk_libs="${tcltk_ops//$tcltk_ops_flag/}" + # remove tcltk-flag from configure_opts + # this allows for weird input such as + # --with-tcltk-libs=' -L/custom-tcl-tk/lib -ltcl8.6 -ltk8.4 ' + PYTHON_CONFIGURE_OPTS="${PYTHON_CONFIGURE_OPTS//"$tcltk_ops_flag"/}" + PYTHON_CONFIGURE_OPTS="${PYTHON_CONFIGURE_OPTS//$tcltk_libs/}" + + # remove quotes, because there mess up compilations + PYTHON_CONFIGURE_OPTS="${PYTHON_CONFIGURE_OPTS//"''"/}" + PYTHON_CONFIGURE_OPTS="${PYTHON_CONFIGURE_OPTS//'""'/}" + + echo "python-build: use tcl-tk from \$PYTHON_CONFIGURE_OPTS" + # echo "PYTHON_CONFIGURE_OPTS=${PYTHON_CONFIGURE_OPTS}" + package_option python configure --with-tcltk-libs="${tcltk_libs}" + # IFS="$OLDIFS" +} + +# FIXME: this function is a workaround for #1125 +# once fixed, it should be removed. +# Get tcltk-flag and options from `$1` +# expects one argument containing a string of configure opts, eg. `PYTHON_CONFIGURE_OPTS` +# returns tcl_tk flag or an empty string if nothing was found. +get_tcltk_flag_from() { + IFS=$'\n' + # parse input string into array + local opts_arr=( $(xargs -n1 <<<"$1") ) + + # iterate through `opts_arr`, break if `--with-tcltk-libs=` was found. + for opts in ${opts_arr[@]}; do + # `--with-tcltk-libs=` must be the prefix. + if [[ "$opts" == "--with-tcltk-libs="* ]]; then + # return + echo "$opts" + break + fi + done + + IFS="$OLDIFS" +} + +use_tcltk() { + local tcltk_libdir="$(brew --prefix tcl-tk 2>/dev/null || true)" + local tcl_tk_libs="$(get_tcltk_flag_from "$PYTHON_CONFIGURE_OPTS")" + + # if tcltk_ops_flag is in PYTHON_CONFIGURE_OPTS, use user provided tcltk + # otherwise default to homebrew-installed tcl-tk, if installed + if [[ -n "$tcl_tk_libs" ]]; then + use_custom_tcltk "$tcl_tk_libs" + elif [ -d "$tcltk_libdir" ]; then + use_homebrew_tcltk + fi +} + # CPython 3.9.1+ and 3.8.10+ have old config.sub that doesn't support "arm64" arch # which is what Gnu uname (but not Apple uname) produces on Apple M1 # (https://github.com/pyenv/pyenv/pull/2020#issuecomment-891911842) @@ -2083,19 +2153,6 @@ if [[ "$PYTHON_CONFIGURE_OPTS" != *"--enable-unicode="* ]]; then fi fi -# regex_to_match="(--with-tcltk-libs='([^']+)')" -if [[ "$PYTHON_CONFIGURE_OPTS" =~ (--with-tcltk-libs=\'([^\']+)\') ]]; then - tcltk_match="${BASH_REMATCH[1]}" - tcltk_match_quoted="${tcltk_match//--with-tcltk-libs=/}" - # remove it from PYTHON_CONFIGURE_OPTS since it will mess up compile - PYTHON_CONFIGURE_OPTS="${PYTHON_CONFIGURE_OPTS//$tcltk_match/}" - - # having issues passing the single quoted part, couldnt pass as single var and still work - package_option python configure "--with-tcltk-libs='${tcltk_match_quoted}'" - unset tcltk_match - unset tcltk_match_quoted -fi - # Unset `PIP_REQUIRE_VENV` during build (#216) unset PIP_REQUIRE_VENV unset PIP_REQUIRE_VIRTUALENV diff --git a/plugins/python-build/test/build.bats b/plugins/python-build/test/build.bats index b4afbdb7..3113b37b 100644 --- a/plugins/python-build/test/build.bats +++ b/plugins/python-build/test/build.bats @@ -165,7 +165,7 @@ OUT stub uname '-s : echo Linux' stub uname '-s : echo Darwin' - stub brew "--prefix libyaml : echo '$brew_libdir'" false + stub brew "--prefix libyaml : echo '$brew_libdir'" false false stub_make_install install_fixture definitions/needs-yaml @@ -192,7 +192,7 @@ OUT # pyenv/pyenv#1026 stub uname false false - stub brew "--prefix readline : echo '$readline_libdir'" + stub brew false "--prefix readline : echo '$readline_libdir'" stub_make_install run_inline_definition <