Published:

Jarle Aase

How to make a dmg package for macos for a QT Application

bookmark 1 min read

Recently I made a Jenkins Pipeline to build a QT Application for multiple Linux distributions, Windows and macOS. In the process I had to figure out how to package a macOS Application.

I ended up building it with a script similar to what I did for Linux.

#!/bin/bash

# Compile and prepare a signed .dmg package for distribution
# Assumes the environment-variable QTDIR to point to the
# Qt installation
#
# Example:
#     Jarles-Mac-mini:scripts jgaa$ QTDIR=/Users/jgaa/Qt/5.10.0/clang_64 ./package-macos.sh

if [ -z "$WHID_VERSION" ]; then
    WHID_VERSION="2.0.0"
    echo "Warning: Missing WHID_VERSION variable!"
fi

if [ -z ${DIST_DIR:-} ];
then
    DIST_DIR=`pwd`/dist/macos
fi

if [ -z ${SIGN_CERT:-} ];
then
    SIGN_CERT="Developer ID Application"
fi

if [ -z ${BUILD_DIR:-} ]; then
    BUILD_DIR=`pwd`/build
fi

if [ -z ${SRC_DIR:-} ];
then
# Just assume we are run from the scipts directory
    SRC_DIR=`pwd`/..
fi

echo "Building whid for macos into ${DIST_DIR} from ${SRC_DIR}"

rm -rf $DIST_DIR $BUILD_DIR

mkdir -p $DIST_DIR && cd $DIST_DIR
mkdir -p $BUILD_DIR

pushd $BUILD_DIR

$QTDIR/bin/qmake \
    -spec macx-clang \
    "CONFIG += release x86_64" \
    $SRC_DIR/whid.pro

make -j4

popd

pushd $DIST_DIR

mv $BUILD_DIR/whid.app $BUILD_DIR/whid-${WHID_VERSION}.app

echo "Making dmg package with $QTDIR/bin/macdeployqt"
$QTDIR/bin/macdeployqt $BUILD_DIR/whid-${WHID_VERSION}.app -dmg -appstore-compliant -codesign="$SIGN_CERT"

mv  $BUILD_DIR/whid-${WHID_VERSION}.dmg .

popd

As you can see, I use qmake and make to build the application. That is great, because it can be done from a script, and scripts can be started from Jenkins.

$QTDIR/bin/qmake \
    -spec macx-clang \
    "CONFIG += release x86_64" \
    $SRC_DIR/whid.pro

make -j4

To build the actual dmg package, I use a utility shipped with QT, macdeployqt. This makes sure that all the required libraries are properly packaged into the bundle. My first attempt worked fine on my local machine, but when I downloaded the package from the build machine, I was unable to install it because it was unsigned.

Signing

It turned out that macdeployqt can sign the package. However I needed to create a certificate signed by Apple, of the correct type, issued to the correct entity.

In order to do that, you must be a registered Apple Developer. Then you must go to xcode, open preferences and accounts. I have enrolled my company, rather than myself, in the Apple Developer program, so I had to select the "The Last Viking LTD" team (rather than the personal, team), click on "Manage Certificates", and then create a "Developer ID Application" certificate.

Screenshot

The name of the certificate is then given to macdeployqt after the -codesign option.

$QTDIR/bin/macdeployqt $BUILD_DIR/whid-${WHID_VERSION}.app -dmg -appstore-compliant -codesign="$SIGN_CERT"

So with this in place, I can build a deployable package for macOS directly from Jenkins.