Handling the program version with Qt, part 2

If you code with Qt and write a GUI application on your Mac (OSX), you will probably add a TEMPLATE = app statement to your .PRO file. This will tell qmake to create an application bundle (BTW, I love the concept of application bundles) Oh, isn't that nice? If I add the VERSION variable to my .PRO file than it will be added automatically, right?

Nope!

Would have been too easy.

After reading the qmake Variable Reference you may think that QMAKE_INFO_PLIST is your friend (just ignore the awkward humor in Note: Most of the time, the default Info.plist is good enough.). It is, if there wasn't a tiny little bug. Oh, and while we are at bugs, @SHORT_VERSION@ is replaced by the first two digits of the program version only (1.2.3 will be 1.2). Ah, the application icon will not be copied, if you use QMAKE_INFO_PLIST, but that was already clear, wasn't it?

So, with

# copy our info.plist template and replace variables, see qmake Variable Reference
QMAKE_INFO_PLIST = $$PWD/osx/info.plist
# see https://bugreports.qt-project.org/browse/QTBUG-21267
QMAKE_INFO_PLIST_OUT = $${TARGET}.app/Contents/Info.plist
PRE_TARGETDEPS +=	$${TARGET}.app/Contents/Info.plist

you will get your own info.plist file copied into the bundle.

Now just add

HC_ICONNAME = name_of_a_beautiful_icon.icns
# does not work, so maybe redundant
ICON = $$PWD/Icons/$${HC_ICONNAME}
# write some extra info in the info.plist
QMAKE_POST_LINK += /usr/libexec/PlistBuddy -c \"Set :GIT_COMMIT_HASH $${HC_GITHASH}\" $${OUT_PWD}/$${TARGET}.app/Contents/Info.plist
# @SHORT_VERSION@ is replaced by the first 2 digits of the version number only. No @TEMPLATE@ for full version available
QMAKE_POST_LINK += ;/usr/libexec/PlistBuddy -c \"Set :CFBundleShortVersionString $${VERSION}\" $${OUT_PWD}/$${TARGET}.app/Contents/Info.plist
# if QMAKE_INFO_PLIST is assigned, the icon is not copied
QMAKE_POST_LINK += ;cp -n $$PWD/Icons/$${HC_ICONNAME} $${OUT_PWD}/$${TARGET}.app/Contents/Resources/.

to your .PRO file and the git hash (you use git, don't you?) is also placed into the info.plist file. Be careful, PlistBuddy is picky and awaits the attribute GIT_COMMIT_HASH in your template info.plist. The program version is added to your bundle, see CFBundleShortVersionString. And finally, the icon is copied.

Didn't I have something better to do on a sunday? Yep, the rest of the sunny afternoon was quality time spent with the family and BBQ!

Did I mention BBQ?