# *** IMPORTANT ***

# Build Prerequisite requirements for Python 3.7.2 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.c.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.1c.tar.gz
cd openssl-1.1.1c
./config no-shared --prefix=/usr/local
make
sudo make install

# Download xz-5.2.4.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.4.tar.gz
cd xz-5.2.4
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.6 first
#       So see Building_Qt5_From_Source_with_QtWebkit_Added_Back_on_MacOSX.txt


# Building Python 3.7.2 to be fully relocatable for macOS Sierra and later

# Before building remember to rename any /Applications/Python 3.7.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.7.2.tgz 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}/devtools2/Frameworks

# now build Python 3.7.2 as a framework
# Need to patch Python-3.7.2 to allow it to build Mac OS X 10.11 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.7.2_fixes.patch"

tar -zxvf Python-3.7.2.tgz
cd Python-3.7.2
patch -p0 < python_3.7.2_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.7/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

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.7/
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.7/Python @rpath/Python python3.7
sudo install_name_tool -change ${MYDEST}/Python.framework/Versions/3.7/Python @rpath/Python python3.7m
sudo install_name_tool -add_rpath @executable_path/../ ./python3.7
# so that python3 can find the Qt Frameworks and proper plugins for PyQt5
sudo install_name_tool -add_rpath @executable_path/../../../../ ./python3.7

# 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.7/Resources/Python.app/Contents/MacOS
sudo install_name_tool -change ${MYDEST}/Python.framework/Versions/3.7/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.7 interpreter to install PyQt5 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.6 and therefore need:
# **** sip-4.19.18 and PyQt5_gpl-5.12.3


# 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.7/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


# Building PyQt5 for Qt-5.12.6 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.7/bin:${PATH}
which python3

# Must have the Qt5.12.6 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}/Qt512/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


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