# Building_Qt5_From_Source_with_QtWebkit_Added_Back_on_MacOSX

# We highly recommend you use the same Qt versions as official Sigil builds on Mac OS X.
# which is now Qt 5.11.2

# As of Qt 5.6, QtWebkit was removed from the stock Qt releases.  Sigil depends on this 
# module for both its BookView and Preview windows.  We are following the examples of
# the Linux Distributions and Adding back the latest QtWebKit to build our own

# FIRST:  make sure you have XCode 8, 9 or 10 or later installed and the Command Line Tools

# We will need to build Qt5 from source first.  
# These instructions will lead you through building from source

# set the delpoyment target
export MACOSX_DEPLOYMENT_TARGET=10.11

# cd to a location to store your src tree then do
export MYQTSRC=`pwd`


# Build Prerequisites
# -------------------
# First build and install the following prerequisites for the build: 
#      cmake, libpng, ligjpeg 
# and install into /usr/local so that they can be found during qtwebkit's build
# Note: older versions of these prerequisites may work but have not been tested


# cmake 3.12.0 or later from https://cmake.org/download
tar -zxvf cmake-3.12.0.tar.gz
cd cmake-3.12.0
./bootstrap --prefix=/usr/local -- -DCMAKE_BUILD_TYPE:STRING=Release
make
sudo make install

# libpng 1.6.35 or later from png's sourcefore site: http://www.libpng.org/pub/png/libpng.html
tar -zxvf libpng-1.6.35.tar.gz
cd libpng-1.6.35
./configure --enable-static=yes --enable-shared=no --prefix=/usr/local
make
sudo make install

#libjpeg from the Independent JPEG Group -  https://http://www.ijg.org
tar -zxvf jpegsrc.v9c.tar.gz
cd jpeg-9c
./configure --enable-static=yes --enable-shared=no --prefix=/usr/local
make
sudo make install


# Building Qt5.11.2 from source
# -----------------------------

# download qt-everywhere-src-5.11.2.tar.gz directly from Qt
# from:  http://download.qt.io/archive/qt/5.11/5.11.2/single/

# and then unpack it
tar -zxvf qt-everywhere-src-5.11.2.tar.gz

cd qt-everywhere-src-5.11.2

# now copy 3 required patches from Sigil//docs/ into this directory
cp YOUR_PATH_TO_SIGIL_SRC_TREE/Sigil/docs/qt_mojave_fontweight_fix.patch ./
cp YOUR_PATH_TO_SIGIL_SRC_TREE/Sigil/docs/qt511.2_fixes1.patch  ./
cp YOUR_PATH_TO_SIGIL_SRC_TREE/Sigil/docs/qt511.2_fix_widget_heap_corruption.patch  ./

# then apply the font weight fix patch if you are on Mojave or later  
patch -p0 < ./qt_mojave_fontweight_fix.patch

# then apply build fixes for qtwebengine when built with 10.14 SDK (XCode 10)
patch -p0 < ./qt511.2_fixes1.patch

# then backport critical heap corruption fix from Qt 5.12-alpha
patch -p0 < ./qt511.2_fix_widget_corruption.patch

# Create a destination directory to house your complete Qt binary in your home directory
# to be similar to how stock Qt does it
cd ~/
mkdir Qt511

# Now return and create a shadow build inside a new directory to keep your Qt 5.11.2 sources clean
cd ${MYQTSRC}
mkdir buildqt
cd buildqt

../qt-everywhere-src-5.11.2/configure --prefix=/Users/${USER}/Qt511 -opensource -nomake examples -nomake tests

# note the build itself can take a couple of hours depending on memory available, disk and cpus used
make -j4
make install

# After the install phase completes your newly built Qt should exist in ~/Qt511 ready to be
# Used to build QtWebKit


# Building The Latest QtWebKit for Qt5.11.2
# -----------------------------------------

# Detailed QtWebKit build instructions can be found here:
# https://github.com/annulen/webkit/wiki/Building-QtWebKit-on-OS-X

cd ${MYQTSRC}

# clone qtwebkit from git::/code.qt.io/qt/qtwebkit.git and get the QtWebKit 5.212 Branch
git clone git://code.qt.io/qt.qtwebkit.git

cd qtwebkit

# copy in the necessary patches for building with the mac osx 10.14 sdk
cp YOUR_PATH_TO_SIGIL_SRC_TREE/Sigil/docs/qtwebkit_memory_leak_fix.patch ./
cp YOUR_PATH_TO_SIGIL_SRC_TREE/Sigil/docs/qtwebkit_suppress_tests_10.14_build_fix.patch ./
cp YOUR_PATH_TO_SIGIL_SRC_TREE/Sigil/docs/qtwebkit_xpc_10.14_build_fix.patch
cp YOUR_PATH_TO_SIGIL_SRC_TREE/Sigil/docs/qtwebkit_no_load_plugins_when_disabled.patch

# apply the patches
patch -p0 < ./qtwebkit_memory_leak_fix.patch
patch -p0 < ./qtwebkit_supress_tests_10.14_build_fix.patch
patch -p0 < ./qtwebkit_xpc_10.14_build_fix.patch
patch -p0 < ./qtwebkit_no_load_plugins_when_disabled.patch

./Tools/Scripts/build-webkit --qt --cmakeargs="-Wno-dev -DQt5_DIR=$HOME/Qt511/lib/cmake/Qt5 -DCMAKE_PREFIX_PATH=/usr/local"

make -C WebKitBuild/Release install

# QtWebKit will be installed into your Qt directory, side by side with other Qt modules, 
# and will be available for building your projects.
 
