# Building_Qt6.7.x_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.7.x

# These instructions will lead you through building from source
# set the deployment target (this is minimum needed)

export MACOSX_DEPLOYMENT_TARGET=11.0  # now set to 11.0

# 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, openssl3, nodejs (see https://nodejs.org/en/download/), 
#      ninja build tool
# Note: older versions of these prerequisites may work but have not been tested

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


# Download libpng 1.6.43 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 support/libpng-1.6.43.tar.gz
cd libpng-1.6.43
./configure --enable-static=yes --enable-shared=no --prefix=/opt/support
make -j4
sudo make install
unset CFLAGS
unset LDFLAGS
cd ..


# if the "nasm" network assembler tool is not installed, you may
# want to install it before building libjpegturbo to speed up the jpeg
# library performance
# https://github.com/netwide-assembler/nasm
# get a stable release nasm-nasm-2.16.03.zip and configure with --prefix=/usr/local
unzip support/nasm-nasm-2.16.03.zip
cd nasm-nasm-2.16.03
sh autogen.sh
./configure --prefix=/usr/local
make -j4
# manpages seem to no longer exist
sudo make install
cd ..  


# libjpeg-turbo 3.0.3 or later from libjpeg-turbo.org
# https://github.com/libjpeg-turbo/libjpeg-turbo
tar -zxvf support/libjpeg-turbo-3.0.3.tar.gz
mkdir buildjt3
cd buildjt3
cmake -G"Unix Makefiles" -DCMAKE_INSTALL_PREFIX=/opt/support -DENABLE_SHARED=0 -DCMAKE_OSX_DEPLOYMENT_TARGET=11.0 \
  -DCMAKE_C_FLAGS_RELEASE="-O3 -mmacosx-version-min=11.0 -Werror=partial-availability" -DWITH_JPEG8=1 ../libjpeg-turbo-3.0.3/
make -j4
sudo make install
cd ..


# Download openssl-3.0.14.tar.gz or later into sources from https://www.openssl.org/source
export MACOSX_DEPLOYMENT_TARGET=11.0
export LDFLAGS="-Wl,-macosx_version_min,11.0"
tar -zxvf support/openssl-3.0.14.tar.gz
cd openssl-3.0.14
./config no-shared --prefix=/opt/support
make -j4
sudo make install
unset LDFLAGS
cd ..


# To enable qt's internal freetype to handle woff2 fonts
# Download brotli-1.1.0.tar.gz from https://github.com/google/brotli/releases download
export MACOSX_DEPLOYMENT_TARGET=11.0
tar -zxvf support/brotli-1.1.0.tar.gz
cd brotli-1.1.0
# to build static libraries
# edit CMakeLists.txt and replace this line:
#     option(BUILD_SHARED_LIBS "Build shared libraries" ON)
# with
#     option(BUILD_SHARED_LIBS "Build shared libraries" OFF)
mkdir out && cd out
cmake -DCMAKE_OSX_DEPLOYMENT_TARGET="11.0" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/opt/support ..
sudo cmake --build . --config Release --target install
cd ../../


# Add a static version of the ICU libs needed QStringConverter classes to work properly
export MACOSX_DEPLOYMENT_TARGET=11.0
tar -zxvf support/icu-release-75-1.tar.gz
cd icu-release-75-1
cd icu4c
cd source
chmod u+x runConfigureICU
./runConfigureICU MacOSX --prefix=/opt/support --enable-shared=no --enable-static=yes --with-data-packaging=static
gnumake -j
sudo gnumake install
cd ../../../

# Building Qt6.7.2 from source
# -----------------------------

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


# alternatively use git to check out the source
# ---------------------------------------------
# git clone https://code.qt.io/qt/qt5.git qt6
# cd qt6
# git switch 6.7.2
# perl init-repository --branch

# without '--branch' specified, init-repository pulls a fixed set of shas
# determined at the time of branch first creation and not the current branch
# heads.  This creates the potential to miss late cherry-picked bug fix 
# commits made to the branch after first creation

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

# tar -jxf qt-everywhere-src-6.7.2.tar.xz
# cd qt-everywhere-src-6.7.2
# or if direct checkout from github use
cd qt6

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

# If you have built static versions of the brotli dcompression library
# and installed it in /usr/local you can modify qt6's internal freetype
# to properly handle woff2 fonts by applying the following patch
patch -p0 < ../sources/qt672_freetype_add_brotli_for_woff2.patch

# 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/qt672_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 Qt672

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


# 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.7.2/configure --prefix=/Users/${USER}/Qt672 -webengine-proprietary-codecs -feature-optimize_full -icu -- -DCMAKE_OSX_DEPLOYMENT_TARGET="11.0" -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH=/opt/support 

# 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}/Qt672
patch -p0 < ~/Desktop/qt672_post_install_macos_ignore_bad_cups_cmake_find_failure.patch

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