# Building_Qt6.10.2_From_Source_on_MacOSX

You will need XCode 15 or later

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

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

export MACOSX_DEPLOYMENT_TARGET=13.0  # now set to 13.0 for Qt 6.10.X

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


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

1. Make sure you have installed Python 3.13.1 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.31.5 or later from https://cmake.org/download
tar -zxvf support/cmake-3.31.5.tar.gz
cd cmake-3.31.5
./bootstrap --prefix=/usr/local -- -DCMAKE_BUILD_TYPE:STRING=Release
make -j4
sudo make install
cd ..

# DO NOT BUILD THIS FOR QT - causes bad mixing of libpng versions
# Download libpng 1.6.54 or later from png's sourceforge site: http://www.libpng.org/pub/png/libpng.html
export LDFLAGS="-Wl,-macos_version_min,13.0"
export CFLAGS="-mmacos-version-min=13.0 -Werror=partial-availability"
tar -jxvf support/libpng-1.6.54.tar.xz
cd libpng-1.6.54
./configure --enable-static=yes --enable-shared=no --prefix=/opt/support
make -j4
sudo make install
unset CFLAGS
unset LDFLAGS
cd ..


# optional:  requires automake
# 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.1.3 or later from libjpeg-turbo.org
# https://github.com/libjpeg-turbo/libjpeg-turbo
tar -zxvf support/libjpeg-turbo-3.1.3.tar.gz
mkdir buildjt3
cd buildjt3
cmake -G"Unix Makefiles" -DCMAKE_INSTALL_PREFIX=/opt/support -DENABLE_SHARED=0 -DCMAKE_OSX_DEPLOYMENT_TARGET=13.0 \
  -DWITH_JPEG8=1 -DCMAKE_C_FLAGS_RELEASE="-O3 -mmacosx-version-min=13.0 -Werror=partial-availability" ../libjpeg-turbo-3.1.3/
make -j4
sudo make install
cd ..


# Download openssl-3.0.19.tar.gz or later into sources from https://www.openssl.org/source
export MACOSX_DEPLOYMENT_TARGET=13.0
export LDFLAGS="-Wl,-macos_version_min,13.0"
tar -zxvf support/openssl-3.0.19.tar.gz
cd openssl-3.0.19
./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.2.0.tar.gz from https://github.com/google/brotli/releases download
export MACOSX_DEPLOYMENT_TARGET=13.0
tar -zxvf support/brotli-1.2.0.tar.gz
cd brotli-1.2.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="13.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=13.0
tar -zxvf support/icu-release-78.2.tar.gz
cd icu-release-78.2
cd icu4c
cd source
chmod u+x runConfigureICU
./runConfigureICU MacOSX --prefix=/opt/support --enable-shared=no --enable-static=yes --with-data-packaging=static
gnumake -j4
sudo gnumake install
cd ../../../

# Building Qt6.10.2 from source
# -----------------------------

# download qt-everywhere-src-6.10.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.10.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=13.0
export MYDEST=/Users/kbhend/ndevpython/libraries/Frameworks
export PATH=${MYDEST}/Python.framework/Versions/3.14/bin:${PATH}

tar -jxf qt-everywhere-src-6.10.2.tar.xz
cd qt-everywhere-src-6.10.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

# Now Qt 6.10.2 has fixed both the qtwebengine mesa bug and the memory leak that existed in
# Qt 6.9.2 and Qt6.9.3

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

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


# Remember to include the -webengine-proprietary-codecs configure switch
# and add -feature-optimize_full to use -O3 (note _full not -full)
# and skip the damn qtpositioning,qtlocation crap

../qt-everywhere-src-6.10.2/configure --prefix=/Users/${USER}/Qt6102 -webengine-proprietary-codecs -feature-optimize_full -icu -skip qtpositioning,qtlocation -- -DCMAKE_OSX_DEPLOYMENT_TARGET="13.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
# need to allow a higher number of file descriptors to be open at one time
ulimit -n 10240

cmake --build . --parallel
cmake --install .

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

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