# *** IMPORTANT ***

# Build Prerequisite requirements for Python 3.8.3 on macOS 10.12 Sierra and later users:
#
#     First make sure you are using the latest XCode for your version of Mac OSX
#     Then make sure you have the command line tools (CLT) installed (via xcode-select --install)
#
#
#     For Python's pip3 and lzma module you will need to compile and install the headers 
#     and static library vercd ..sions of liblzma.a (xz), libssl.a (libopenssl), and libcrypto.a (libopenssl)
#

If you are using XCode 10.X on MacOSX 10.14.X, remember to install the command line tools 
and their headers!  The headers are now not installed by default.  An installer package
for the headers can be found here:

/Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg


# Download openssl-1.1.1g.tar.gz or later from https://www.openssl.org/source

export MACOSX_DEPLOYMENT_TARGET=10.12
export LDFLAGS="-Wl,-macosx_version_min,10.12"
export CFLAGS="-mmacosx-version-min=10.12 -Werror=partial-availability"

tar -zxvf openssl-1.1.1g.tar.gz
cd openssl-1.1.1g
./config no-shared --prefix=/usr/local
make
sudo make install

# Download xz-5.2.5.tar.gz or later from https://tukaani.org/xz/  (via sourceforge)

export MACOSX_DEPLOYMENT_TARGET=10.12
export LDFLAGS="-Wl,-macosx_version_min,10.12"
export CFLAGS="-mmacosx-version-min=10.12 -Werror=partial-availability"

# Since configure is very stupid in how it tests symbols on macOSX we need
# to apply a patch to prevent it from using something that does not exist
# on our deployment target of 10.12 as below (futimens does not exist on macOS10.12)

cat fix_xz_for_macOS_10.12.patch
--- configure.orig	2019-06-04 11:12:26.000000000 -0400
+++ configure	2019-06-04 11:12:42.000000000 -0400
@@ -18233,7 +18233,7 @@
 
 
 # Find the best function to set timestamps.
-for ac_func in futimens futimes futimesat utimes _futime utime
+for ac_func in futimes futimesat utimes _futime utime
 do :
   as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
 ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var"
-------

tar -zxvf xz-5.2.5.tar.gz
cd xz-5.2.5
patch -p0 < fix_xz_for_macOS_10.12.patch
./configure --disable-shared --prefix=/usr/local
make
sudo make install

#     Also you need to install the latest free community edition of ActiveState's ActiveTCL 8.6.X or later.
#     See: http://www.activestate.com/activetcl/downloads
#
#     OR 
#
#     Build and install the latest 8.6.X versions of Tcl and Tk Frameworks on Mac OSX 
#     Source archives are Available from the Tcl/Tk sourceforge site (latest current version is 8.6.9)
#     https://www.tcl.tk/software/tcltk/download.html
#     https://www.tcl.tk/doc/howto/compile.html  (use --enable-framework --enable-aqua --enable-threads during configure)

#     And finally as we will be building a required PyQt5 python module, you will need to have built or
#     downloaded prebuilt versions of Qt 5.12.9 first
#       So see Building_Qt5_From_Source_with_QtWebkit_Added_Back_on_MacOSX.txt


# Building Python 3.8.3 to be fully relocatable for macOS 10.12 and later

# Before building remember to rename any /Applications/Python 3.8.app to save it and replace it afterwards
# as the damn python installation from source always overwrites it no matter the configure prefix used

# Download Python-3.8.3.tar.xz from www.python.org

export MACOSX_DEPLOYMENT_TARGET=10.12

# Pick a location where the relocatable Python framework will be installed
# stay away from /tmp locations since Mac OS X will auto delete files older 
# than 3 days in /tmp right out from under you

export MYDEST=/Users/${USER}/devtools/Frameworks

# now build Python 3.8.3 as a framework
# Need to patch Python-3.8.3 to allow it to build Mac OS X 10.12 compliant and later
# and for it to find and use the latest Tk and Tcl frameworks
# Look in Sigil's "docs" to get the required patch "python_3.8.3_fixes.patch"

tar -zxvf Python-3.8.3.tgz
cd Python-3.8.3
patch -p0 < python_3.8.3_fixes.patch

export LDFLAGS="-Wl,-macosx_version_min,10.12" 
export CFLAGS="-mmacosx-version-min=10.12 -Werror=partial-availability" 
./configure --prefix=${MYDEST} --enable-framework=${MYDEST} --with-ensurepip

make
sudo make frameworkinstall

# next update path in order to use the newly built/installed Python.framework's
# and then use pip3 to install all other required python packages to its site-packages

export PATH=${MYDEST}/Python.framework/Versions/3.8/bin:${PATH}
which pip3

sudo pip3 install six
sudo pip3 install html5lib
sudo pip3 install lxml
sudo pip3 install Pillow
sudo pip3 install regex
sudo pip3 install css-parser
sudo pip3 install cssselect
sudo pip3 install chardet
sudo pip3 install certifi
sudo pip3 install dulwich
sudo pip3 install urllib3

Note: we have replaced cssutils with a drop in replacement with many bugs fixed
called css-parser.


# Now a complete Python.framework has been built in ${MYDEST}
# But we still need to make it a relocatable framework

# To make it relocatable we need to use otool and install_name_tool to change
# the dylib name and path to it from all executables in the Python.framework

# A Quick Guide: On Mac OS X, one may use:
#     "otool -D <file>" to view the install name of a dylib
#     "otool -L <file>" to view the dependencies
#     "otool -l <file> | grep LC_RPATH -A2" to view the RPATHs
#     "install_name_tool -id ..." to change an install name
#     "install_name_tool -change ..." to change the dependencies
#     "install_name_tool -rpath ... -add_rpath ... -delete_rpath ..." to change RPATHs
 
# Make the framework's main dylib relocatable using rpath

cd ${MYDEST}/Python.framework/Versions/3.8/
sudo chmod u+w Python
otool -D ./Python
sudo install_name_tool -id @rpath/Python ./Python

# Change the dependencies of the executable files in bin to point to the relocatable 
# framework in a relative way and add the proper rpath to find the Python (renamed dylib)

cd bin
sudo install_name_tool -change ${MYDEST}/Python.framework/Versions/3.8/Python @rpath/Python python3.8
# NO_LONGER_NEEDED sudo install_name_tool -change ${MYDEST}/Python.framework/Versions/3.8/Python @rpath/Python python3.8m
sudo install_name_tool -add_rpath @executable_path/../ ./python3.8
# so that python3 can find the Qt Frameworks and proper plugins for PyQt5
sudo install_name_tool -add_rpath @executable_path/../../../../ ./python3.8

# now do the same for the Python.app stored inside the Python.framework Resources 
# This app is needed to allow gui use by python for plugins

cd ${MYDEST}/Python.framework/Versions/3.8/Resources/Python.app/Contents/MacOS
sudo install_name_tool -change ${MYDEST}/Python.framework/Versions/3.8/Python @rpath/Python ./Python
sudo install_name_tool -add_rpath @executable_path/../../../../ ./Python

# We should now have a fully relocatable Python.framework



# We will now use this just-built Python3.8 interpreter to install PyQt5, PyQtWebEngine and sip.

# **** Important: versions of PyQt5 and sip must be selected to match with the Qt version
# **** In these instructions we are using Qt 5.12.9 and therefore need:
# **** sip-4.19.18 and PyQt5_gpl-5.12.3 and PyQtWebEngine_gpl-5.12.1

# First Build sip
# Building sip-4.19.18 from source and installing it into your Python Interpreter

# From https://www.riverbankcomputing.com/software/pyqt/download5
# Download sip-4.19.18.tar.gz

# More detailed build instructions can be found here:
#    http://pyqt.sourceforge.net/Docs/sip4/installation.html

# Must have the python interpreter you want to install PyQt5 into in the PATH to be found first
export PATH=${MYDEST}/Python.framework/Versions/3.8/bin:${PATH}
which python3

tar -zxvf sip-4.19.18.tar.gz
cd sip-4.19.18
python3 ./configure.py --deployment-target=10.12 --sip-module=PyQt5.sip
make
sudo make install


# Next Build PyQt5 for Qt-5.12.9 from source and installing it into your Python Interpreter

# From https://www.riverbankcomputing.com/software/pyqt/download5
# Download PyQt5_gpl-5.12.3.tar.gz

# More detailed build instructions can be found here: 
#    http://pyqt.sourceforge.net/Docs/PyQt5/installation.html#building-and-installing-from-source

# Must have the python interpreter you want to install PyQt5 into in the path to be found first
export PATH=${MYDEST}/Python.framework/Versions/3.8/bin:${PATH}
which python3

# Must have the Qt5.12.9 bin directory in the path to specify which Qt to use
# See Building_Qt5_From_Source_with_QtWebkit_Added_Back_on_MacOSX.txt
export PATH=/Users/${USER}/Qt5129/bin:${PATH}
which qmake

export MACOSX_DEPLOYMENT_TARGET=10.12

tar -zxvf PyQt5_gpl-5.12.3.tar.gz
cd PyQt5_gpl-5.12.3
python3 ./configure.py --confirm-license --no-docstrings --no-stubs 
make -j4
sudo make install

# And finally build PyQtWebEngine (again with the destination python and qmake in your path)
# From https://sourceforge.net/projects/pyqtwebengine/
# Download: PyQtWebEngine-5.12.1.tar.gz

export MACOSX_DEPLOYMENT_TARGET=10.12

tar -zxvf PyQtWebEngine-5.12.1.tar.gz
cd PyQtWebEngine_gpl-5.12.1/
python3 ./configure.py --no-docstrings --no-stubs
make -j4
sudo make install

# Once complete you will have properly built a Python 3.8 interpreter to be embedded inside
# of Sigil
