# Building_Qt6.5.3_From_Source_on_MacOSX

On Ventura you need XCode 14.3.1

# *** IMPORTANT ***
# We do highly recommend you use the same Qt versions as official Sigil builds on Mac OS X.
# which is now Qt 6.5.3 or later

# These instructions will lead you through building from source
# FIRST:  make sure you have XCode 14 or later installed and the Command Line Tools

# set the deployment target (this is minimum needed)

export MACOSX_DEPLOYMENT_TARGET=11.0  # now set to 11.0 - but that is absurd in macx.conf

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


# Build Prerequisites
# -------------------

1. Make sure you have installed Python 3.11.3 or later and then use pip3 install html5lib as these
are needed by QtWebEngine

2. Make sure you have installed the ninja build tool into /usr/local (or use MacPorts).


# Next build and install the following prerequisites for the build: 
#      cmake, libpng, libjpeg-turbo 
# 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

# You should also download and install the ninja build tool

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


# Download libpng 1.6.39 or later from png's sourceforge site: http://www.libpng.org/pub/png/libpng.html
export LDFLAGS="-Wl,-macosx_version_min,11.0"
export CFLAGS="-mmacosx-version-min=11.0 -Werror=partial-availability"
tar -zxvf sources/libpng-1.6.39.tar.gz
cd libpng-1.6.39
./configure --enable-static=yes --enable-shared=no --prefix=/usr/local
make -j4
sudo make install
unset CFLAGS
unset LDFLAGS


# libjpeg-turbo 2.1.5.1 or later from libjpeg-turbo.org
# https://sourceforge.net/projects/libjpeg-turbo/files/
tar -xvf libjpeg-turbo-2.1.5.1.tar.gz
mkdir buildjt
cd buildjt
cmake -G"Unix Makefiles" -DCMAKE_INSTALL_PREFIX=/usr/local -DENABLE_SHARED=0 -DCMAKE_OSX_DEPLOYMENT_TARGET=11.0 \
  -DCMAKE_C_FLAGS_RELEASE="-O3 -mmacosx-version-min=11.0 -Werror=partial-availability" ../libjpeg-turbo-2.1.5.1/
make -j4
sudo make install


# Building Qt6.5.3 from source
# -----------------------------

# download qt-everywhere-src-6.5.3.tar.xz directly from Qt
# from:  https://download.qt.io/archive/qt/
# for: PySide - https://download.qt.io/official_releases/QtForPython/

export MACOSX_DEPLOYMENT_TARGET=11.0
export MYDEST=/Users/kbhend/devpython/libraries/Frameworks
export PATH=${MYDEST}/Python.framework/Versions/3.11/bin:${PATH}


tar -jxf qt-everywhere-src-6.5.3.tar.xz

cd qt-everywhere-src-6.5.3

# Apply an obvious fix for insertParagraph execcommand and h6 (see w3c spec)
# See https://bugreports.qt.io/browse/QTBUG-79778
patch -p0 < ../sources/qt650_fix_h6_insertParagraph.patch

# now apply the CVE security fix that is still needed in 6.5.3
cd qtbase
patch -p1 < ../../sources/CVE-2023-43114-6.5-qtbase.patch
cd ..

# the remaining patches are ONLY important for Qt on macOS

# apply workaround to prevent missing macos application menu items
# See https://bugreports.qt.io/browse/QTBUG-80795
patch -p0 < ../sources/qt650_fix_missing_macos_menubar.patch

# The last patch is a generated cmake file fix and it must be used to fix the cmake
# file *AFTER* the Qt6 is built and installed

# 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 Qt653

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


# Remember to include the -webengine-proprietary-codecs configure switch
# and add -feature-optimize_full to use -O3 (note _full not -full)

../qt-everywhere-src-6.5.3/configure --prefix=/Users/${USER}/Qt653 -webengine-proprietary-codecs -feature-optimize_full -- -DCMAKE_OSX_DEPLOYMENT_TARGET="11.0" -DCMAKE_BUILD_TYPE=Release 

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

# Now apply the final patch to the post installed cmake file
cd /Users/${USER}/Qt653
patch -p0 < ~/Desktop/qt650_post_install_macos_ignore_bad_cups_cmake_find_failure.patch

# After the install phase completes your newly built Qt should exist in ~/Qt653 ready to be used
# to build Sigil and PageEdit
