Downtown Doug Brown » Learn how to create a Qt 5 ARM/Intel common binary for Mac
I just lately launched a big update for my Mac ROM SIMM Programmer software which is written utilizing Qt for cross-platform compatibility. As a part of the replace I needed to launch the Mac construct as a common x86_64/arm64 binary in order that M1/M2 Mac customers would have the ability to run it natively. It doesn’t presently compile for Qt 6, though I believe I can repair that sooner or later with out an excessive amount of effort. Nevertheless, Qt 5.15.9 and later do help creating common binaries out of the field, so I made a decision to determine the right way to set all of it up.
Though I believe I’ve fairly respectable Google-fu, it was tough to piece the whole lot collectively to perform this purpose. I’m hoping this put up can function a reference for folks sooner or later. These directions are based mostly on Qt 5.15.10 as a result of that’s the newest model that’s presently open supply. I did this on an M2 Mac Mini working macOS 13.5.1 Ventura.
Initially, earlier than anyone suggests it, you possibly can’t use homebrew to put in Qt for this as a result of it doesn’t supply a universal build of Qt. Should you’re going for an open-source construct, you will have to construct your personal Qt as an alternative. I’m assuming folks with a industrial license can simply get compiled binaries from Qt. So when you’ve got entry to that, you possibly can in all probability skip the step of constructing Qt. In any other case, comply with together with me…
Arrange a construct surroundings
Qt 5.15.10 formally solely helps as much as the macOS 12 SDK, so step one is to put in a model of Xcode that features a macOS 12.x SDK. Should you don’t do that, it can in all probability nonetheless work, however I acquired annoying warnings each time I constructed apps with a more moderen Xcode telling me that my SDK 13.3 was unsupported:
Mission WARNING: Qt has solely been examined with model 12 of the platform SDK, you are utilizing 13. Mission WARNING: That is an unsupported configuration. You could expertise construct points, and through the use of Mission WARNING: the 13.3 SDK you're opting in to new options that Qt has not been ready for. Mission WARNING: Please downgrade the SDK you utilize to construct your app to model 12, or configure Mission WARNING: with CONFIG+=sdk_no_version_check when working qmake to silence this warning.
Don’t fear, it’s simple sufficient to repair this downside with out having to make use of sdk_no_version_check to silence the warning. You gained’t need to mess together with your current Xcode setup in any respect. I downloaded Xcode 14.0, which comprises the macOS 12.3 SDK. This site has excellent links to older Xcode versions. It appears like maybe I ought to have downloaded 14.0.1 as an alternative…oh effectively!
I discovered plenty of Stack Overflow solutions about downloading older SDK variations by hand and sticking them within the right location inside your current Xcode.app, however the options I discovered all the time ended up modifying one thing about your system’s default Xcode, and I didn’t wish to try this!
As an alternative, I extracted the older Xcode, renamed it to Xcode_14, and positioned it in my /Functions listing subsequent to my newer Xcode. I don’t assume it’s important to put it in /Functions although.
Subsequent, I arrange my surroundings to make use of this older Xcode and SDK:
export DEVELOPER_DIR=/Functions/Xcode_14.app/Contents/Developer
That is the magic incantation that tells the system and varied automated SDK detection to briefly use this tradition Xcode slightly than the system one in /Functions/Xcode.app, so long as you continue to have that surroundings variable set. For some cause this was actually tough for me to search out. Possibly it was as a result of I used to be looking for Qt-related issues. All I do know is I didn’t wish to use xcode-select to alter my system-wide Xcode setup.
Construct Qt 5.15.10
Obtain and extract the supply code for Qt 5.15.10: qt-everywhere-opensource-src-5.15.10.tar.xz.
tar -xJf qt-everywhere-opensource-src-5.15.10.tar.xz
Be sure you’re in a Terminal window the place you have got already run the “export DEVELOPER_DIR=…” command from above. Now, comply with the steps from CrystalIDEA’s excellent build tools repo to construct Qt:
cd qt-everywhere-src-5.15.10 export PATH=$PATH:$(pwd)/qtbase/bin cd qtbase ./configure QMAKE_APPLE_DEVICE_ARCHS="x86_64 arm64" -opensource -confirm-license -nomake examples -nomake checks -no-openssl -securetransport make -j$(sysctl -n hw.ncpu) sudo make set up cd ../qttools qmake make -j$(sysctl -n hw.ncpu) sudo make set up cd ../qtmacextras qmake make -j$(sysctl -n hw.ncpu) sudo make set up
After working these steps, you must now have a brand new open-source common set up of Qt 5.15.10 in /usr/native/Qt-5.15.10. Be aware that these directions appear to solely construct qtbase, qttools, and qtmacextras. I’m positive if you would like, you possibly can construct different elements as effectively. This was all I wanted although.
Arrange a equipment in Qt Creator
Subsequent up, I opened up Qt Creator, went to Preferences, and arrange a Qt model and a equipment for this new Qt 5.15.10:
- Create a brand new Qt model, select the qmake binary at /usr/native/Qt-5.15.10/bin/qmake.
- Be aware: I had bother navigating to this listing within the Qt Creator select file dialog. I used to be in a position to open the /usr/native/Qt-5.15.10 listing within the Finder and drag it into Qt Creator’s open file dialog to achieve entry.
- It should show a warning about no QML utility put in. Ignore the warning, except you’re really utilizing QML. In case you are utilizing QML, you’ll in all probability have to construct extra elements of Qt.
- The Mac Qt Creator preferences UI is sort of janky, so it’s possible you’ll have to click on OK after which reopen the Qt Creator preferences window at this level in an effort to save the brand new Qt model. I couldn’t discover the “Apply” button that the Home windows model has.
- Create a brand new Qt equipment. Select “clang (C, arm 64bit at /usr/bin/clang)” and the corresponding C++ model for the compilers. Select the brand new Qt model you created because the Qt model.
- Vital: customise the surroundings of this equipment. Add the next line:
- DEVELOPER_DIR=/Functions/Xcode_14.app/Contents/Developer
- This ensures that everytime you’re constructing Qt applications with this equipment, it can use the right model of the macOS SDK.
- Vital: customise the surroundings of this equipment. Add the next line:
Construct the undertaking in Qt Creator
Open your Qt undertaking. Earlier than you construct it the primary time, you might want to set an possibility within the undertaking to inform it to construct for each x86_64 and arm64. Within the Tasks tab on the left, underneath Construct to your equipment, click on Particulars subsequent to qmake. Be sure to allow each ABIs:
It is possible for you to to inform whether it is arrange accurately as a result of the “Efficient qmake name” will embody QMAKE_APPLE_DEVICE_ARCHS=”x86_64 arm64″:
You’ll doubtless want to do that for each the Debug and Launch construct configurations. Should you’re extra of a “run qmake and make by hand” kind of individual, the QMAKE_APPLE_DEVICE_ARCHS addition to the qmake command is the way in which to inform Qt to make a common binary. Simply don’t overlook to additionally export DEVELOPER_DIR as described above in case you do it that method. Additionally, I’m positive there’s an identical course of in case you use cmake as an alternative of qmake. I didn’t hassle figuring it out since my undertaking nonetheless makes use of qmake.
That’s just about the method! Now you possibly can construct the undertaking. Get Information within the Finder will present that you’ve created a common utility:
Deploying the app
You might have observed that the binary within the screenshot above is large. 45 MB? That’s like a 3rd of the dimensions of the whole laborious drive within the Performa 550 we had once I was rising up. Sheesh! My SIMM Programmer utility isn’t that huge — it’s as a result of I used Qt’s macdeployqt utility to bundle the common construct of Qt 5.15.10 within the app so it may be distributed to anybody. I’ve an Apple developer account so I may also signal the app with Apple. Right here is the method I adopted to complete the distribution of SIMMProgrammer.app as a dmg file:
/usr/native/Qt-5.15.10/bin/macdeployqt SIMMProgrammer.app -dmg -sign-for-notarization="Developer ID Utility: Your Identify Right here"
The “Developer ID Utility: Your Identify Right here” must be the identify of a signing certificates you have already got arrange, by the way in which.
After I run this, I get a bunch of errors:
ERROR: no file at "/usr/lib/libqcocoa.dylib" ERROR: no file at "/usr/lib/libcocoaprintersupport.dylib" ERROR: no file at "/usr/lib/libqmacstyle.dylib" ERROR: no file at "/usr/lib/libqgif.dylib" ERROR: no file at "/usr/lib/libqico.dylib" ERROR: no file at "/usr/lib/libqjpeg.dylib"
So far as I can inform, these errors are utterly irrelevant and don’t matter. These libraries undoubtedly find yourself contained in the app bundle in SIMMProgrammer.app/Contents/PlugIns. Evidently macdeployqt is on the lookout for them within the improper location by default and printing that error, nevertheless it nonetheless finds them within the correct location and efficiently creates the ultimate app bundle and dmg file.
Subsequent, I take advantage of notarytool to notarize the dmg:
xcrun notarytool submit SIMMProgrammer.dmg --wait
You’ll need so as to add extra parameters to the command to authenticate your developer ID account. I’ve left them out of this instance invocation.
Lastly, after all of it comes again with success, I staple it:
xcrun stapler staple SIMMProgrammer.dmg
In spite of everything of that, I lastly have a signed, notarized, and so forth. SIMMProgrammer.dmg file that I can distribute to others. I actually struggled to determine the right way to get this all arrange with Qt 5 to create a common binary. It undoubtedly took plenty of time and analysis to determine this course of. I hope this helps somebody on the market! Particular because of CrystalIDEA for sharing their Qt construct instruments with the world. Their data was an enormous assist.