Last active
March 19, 2023 00:15
-
-
Save the-moog/f2aa5d6ea5f77c1e07d67bd7b7f7f8e2 to your computer and use it in GitHub Desktop.
Improved fix for dynamic build and load of shared library support when using pyenv virtualenv (Python)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### pyenv hook script to prepare a build env | |
# | |
# 0: Intro | |
# $MYLOC points to your local sideload root (usually ~/.local) | |
MYLOC=$HOME/.local | |
# | |
export PYTHON_CONFIGURE_OPTS="--with-openssl=$MYLOC --enable-shared --enable-optimizations --with-lto" | |
export PYTHON_CFLAGS="-march=native -mtune=native" | |
echo "" | |
echo "This is $(basename $0) hook 'build.bash':" | |
echo " Location: $(pyenv root)/pyenv.d/install/build.bash" | |
echo "" | |
echo "This add-on performs additional tasks and creates additional" | |
echo "environment variables to pass to python build process" | |
echo "when called by the pyenv-build plugin:" | |
echo "" | |
echo " PYTHON_CONFIGURE_OPTS='${PYTHON_CONFIGURE_OPTS}'" | |
echo " CONFIGURE_OPTS='${CONFIGURE_OPTS}'" | |
echo " PYTHON_CFLAGS='${PYTHON_CFLAGS}'" | |
echo " CFLAGS='${CFLAGS}'" | |
echo "" | |
# | |
# Use: with_log "string" cmd... | |
function with_log () { | |
local TTTT=$(mktemp -d) | |
local MSG=$1; shift | |
"$@" >$TTTT/log 2>$TTTT/log | |
echo "$MSG results in $TTTT" | |
test -f $TTTT/err && \ | |
echo -e "ERRORS\nError log in $TTTT/err\n$(tail $TTTT/err)\nDONE: There were ERRORS" || \ | |
echo "$MSG DONE: No errors, results in $TTTT/log" | |
echo "" | |
return $? | |
} | |
# Use: update_pyenv | |
function update_pyenv () { | |
PYENV=$(pyenv root) | |
if [ -d $PYENV/.git ]; then | |
echo "Updating pyenv" | |
pushd $PYENV | |
git show-ref -q --heads mine || git branch mine | |
git branch -q --set-upstream-to=origin/master master | |
git checkout -q mine | |
git merge -q -m "auto merge" origin/master | |
fi | |
echo "" | |
} | |
# | |
# 1: Make sure pyenv is up to date | |
with_log "Update pyenv" update_pyenv | |
# | |
# 2: Build openssl and side-load it | |
OPENSSLVER=1.1.1t | |
echo "Side loading openssl $OPENSSLVER into $MYLOC" | |
if [ ! -d $MYLOC/bin/openssl ] || ! ( $MYLOC/bin/openssl version | grep $OPENSSLVER ); then | |
BUILDDIR=$(mktemp -d) | |
pushd $BUILDDIR | |
git clone https://github.com/openssl/openssl | |
cd openssl | |
git checkout -b build OpenSSL_$(echo "$OPENSSLVER" | tr '.' '_') | |
with_log "Config" ./config --prefix=$MYLOC --openssldir=$MYLOC/ssl | |
echo "Building openssl, please wait...." | |
with_log "Build" make | |
echo "Installing openssl, please wait...." | |
with_log "Install" make install_sw | |
popd | |
rm -rf $BUILDDIR | |
fi | |
echo "" | |
# 3: Build python, passing some extra options | |
echo "Building Python, please wait...." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I was thinking of adding detection of an environment variable or config file with the contents like this:
PYENV_BUILD_SHARED_=Y|N
PYENV_BUILD_SHARED_DEFAULT=Y|N
e.g.
.pyenv/pyenv.cfg
The same mechanism could be extended to pass any arbitraty configuration to a given pyenv-build build or pyenv virtualenv, through the use of a simple set of additional syntax elements in addition to Y/N, E.g.
e.g.
[--set-env]
3.9.1 = DEBUG=2
3.8 = LD_LIBRARY_PATH=$PYENV/versions/lib/$PY_VERSION
[--enable]
3.8 = zlib
[--sideload]
3.9.5 = git clone https://github.com/mozillazg/pypy #; "Installing PyPy"
[--post-build]
2.7 = scripts/deprecation_warning severity=10