Skip to content

Commit

Permalink
Fix version string in macOS
Browse files Browse the repository at this point in the history
(I can't believe that this is the best way to do this, but I'm not seeing a lot
of options for how to get the version number into Info.plist!)

Qt no longer adds the deprecated CFBundleGetInfoString.  That string was
deprecated since macOS 10.5, and should not have been used:
https://bugreports.qt.io/browse/QTBUG-74872

When I added it:
    2018-08-16 build: add VERSION to the osx app bundle
    https://bugreports.qt.io/browse/QTBUG-74872
I did not understand the situation.

According to:
    https://developer.apple.com/documentation/bundleresources/information_property_list/cfbundleversion
    https://developer.apple.com/documentation/bundleresources/information_property_list/cfbundleshortversionstring
those property list keys were introduced in 10.0 (2001-03-24), so we don't need
to worry about whether older versions of macOS support it.

Unfortunately, /usr/libexec/PlistBuddy is rather temperamental.  It allows one
to "Add" or "Set", but both commands fail if the key already exists or does not
exist (as appropriate).  As such, if we hard-code "Set" then it fails
immediately.  If we hard-code "Add" then it succeeds on the first compilation
but fails after a second (i.e. `make && touch src/main.cpp && make` fails).

It appears that the "Merge" command will give a warning if the key already
exists, but this is only a warning, not a failure.  So our strategy is to merge
the CFBundleVersionString and CFBundleShortVersionString keys, then to "Set"
them.

One solution which I read about, but rejected, was to create an Info.plist
file, then use that instead of the qmake-generated one.  We could then use sed
(or the undocumented QMAKE_SUBSITUTES?) to add the version number.  However,
that would also require us to specify values for keys such as
LSMinimumSystemVersion and NSPrincipalClass, and presumably modify them as
appropriate for different versions of macOS, the system libraries, compiler,
etc.  I will happily admit that I trust qmake's default values more than my own
knowledge of macOS.
  • Loading branch information
gperciva committed Oct 21, 2023
1 parent f0e3694 commit 40885c4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
10 changes: 9 additions & 1 deletion Tarsnap.pro
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,15 @@ osx {

# Add VERSION to the app bundle. (I wish that qmake did this!)
INFO_PLIST_PATH = $$shell_quote($${OUT_PWD}/$${TARGET}.app/Contents/Info.plist)
QMAKE_POST_LINK += /usr/libexec/PlistBuddy -c \"Set :CFBundleGetInfoString $${VERSION}\" $${INFO_PLIST_PATH} ;
# Add (empty) version strings to the Info.plist. This will print a warning
# if the keys already exist; but it's only a warning, not an error!
QMAKE_POST_LINK += /usr/libexec/PlistBuddy \
-c \"Merge util/version-blank.plist.in\" $${INFO_PLIST_PATH} ;
# Replace those version strings with the real ones.
QMAKE_POST_LINK += /usr/libexec/PlistBuddy \
-c \"Set :CFBundleVersionString string $${VERSION}\" \
-c \"Set :CFBundleShortVersionString string $${VERSION}\" \
$${INFO_PLIST_PATH} ;
}

format.commands = find src/ tests/ lib/core/ lib/widgets/ lib/plugins \
Expand Down
6 changes: 6 additions & 0 deletions util/version-blank.plist.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<dict>
<key>CFBundleVersionString</key>
<string>0.0.0</string>
<key>CFBundleShortVersionString</key>
<string>0.0.0</string>
</dict>

0 comments on commit 40885c4

Please sign in to comment.