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

Published Updated

By Jarle Aase

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.shif [ -z "$WHID_VERSION" ]; then    WHID_VERSION="2.0.0"    echo "Warning: Missing WHID_VERSION variable!"fiif [ -z ${DIST_DIR:-} ];then    DIST_DIR=`pwd`/dist/macosfiif [ -z ${SIGN_CERT:-} ];then    SIGN_CERT="Developer ID Application"fiif [ -z ${BUILD_DIR:-} ]; then    BUILD_DIR=`pwd`/buildfiif [ -z ${SRC_DIR:-} ];then# Just assume we are run from the scipts directory    SRC_DIR=`pwd`/..fiecho "Building whid for macos into ${DIST_DIR} from ${SRC_DIR}"rm -rf $DIST_DIR $BUILD_DIRmkdir -p $DIST_DIR && cd $DIST_DIRmkdir -p $BUILD_DIRpushd $BUILD_DIR$QTDIR/bin/qmake \    -spec macx-clang \    "CONFIG += release x86_64" \    $SRC_DIR/whid.promake -j4popdpushd $DIST_DIRmv $BUILD_DIR/whid.app $BUILD_DIR/whid-${WHID_VERSION}.appecho "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.promake -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.