Building OpenCV 5/OpenCvSharpExtern on Apple Silicon: when MLAS picks the host architecture #15
getguardianeye
started this conversation in
Articles
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
I needed separate arm64 and x86_64 builds of OpenCvSharpExtern from the same Apple Silicon runner. The native arm64 build got as far as the final link. The x86_64 cross-build failed earlier, while compiling MLAS assembly.
The OpenCV configure command already had:
but OpenCV 5.0.0's vendored MLAS selected its assembly sources from
CMAKE_SYSTEM_PROCESSOR. On an arm64 host that value still described the host, so Clang was given AArch64.Sfiles for an x86_64 target and stopped on instructions such asb.Passing
-D CMAKE_SYSTEM_PROCESSOR=x86_64did not fix it because CMake set the value again atproject(). I also did not want to force full cross-compiling withCMAKE_SYSTEM_NAME: the DNN build generates code withprotoc, and keepingCMAKE_CROSSCOMPILINGfalse lets that tool run on the host under Rosetta.The fix was to override the MLAS architecture flags from
CMAKE_OSX_ARCHITECTURES, after MLAS's own architecture detection and before it assembled its source list:For the x86_64 leg I also had to turn off the ARM-only HALs. They were detected from the arm64 host and otherwise reached the x86_64 compiler with
-mcpu=armv8-a:That got both architectures to the link step, where a second MLAS problem appeared:
The same symbol was missing from the arm64 build. The MLAS subset vendored by OpenCV declared
MlasHGemmSupportedand called it fromcompute.cpp, but did not provide a macOS definition; the FP16 HGemm implementation was disabled in that source tree.Disabling
opencv_dnnwas a dead end for this build because OpenCvSharp 5's aggregate header includesdnn.hppand several DNN-dependent contrib modules unconditionally. The smallest workaround was a macOS build patch that reports HGemm as unavailable:That keeps DNN enabled but prevents MLAS from selecting the missing FP16 HGemm path, so it uses the FP32/generic path instead.
After those two patches the same arm64 runner produced separate
osx-arm64andosx-x64dylibs. I validate the target architecture from the artifact rather than assuming that a successful cross-build produced what was requested:The part that cost the most time was that
CMAKE_OSX_ARCHITECTUREScorrectly changed Clang's target, while dependency-specific feature detection continued to followCMAKE_SYSTEM_PROCESSOR. For cross-builds on Apple Silicon, both need checking.All reactions