build: include libmtmd in Apple XCFramework (opt-in LLAMA_BUILD_MTMD) - #21935
Conversation
|
Hi @theabecaster, thanks for your contribution! Per our contribution guidelines, the automated PR checker found the following issue(s) that need your attention:
Please note that maintainers reserve the right to make final decisions on PRs. If you believe there is a mistake, please comment below. |
507dea0 to
0d1ebf7
Compare
|
Flagging the disclosure line at the bottom of the PR body / commit message — I used Claude for CMake wording, but the investigation and fix are mine. For context on motivation: I'm building an iOS app that needs vision-capable on-device inference. Linking against the released xcframework failed because the |
0d1ebf7 to
21dcc2d
Compare
danbev
left a comment
There was a problem hiding this comment.
Manually verified using llama-swift-macos.
21dcc2d to
819264a
Compare
ngxson
left a comment
There was a problem hiding this comment.
Nice, very useful option. Can you have a look? @ggerganov
| # Standalone libmtmd build (e.g. for Apple XCFramework packaging) without pulling | ||
| # in the rest of the tools/ tree. When the full tools build is already enabled, | ||
| # mtmd is built by the tools/ subdirectory above; this hook only fires when | ||
| # LLAMA_BUILD_TOOLS is OFF to avoid double-adding the target. |
There was a problem hiding this comment.
I think it's not just isolated to XCFramework, but seems like in my use case, wasm binding also need this.
Probably better to clarify that this can be useful for building llama and mtmd for binding, so it's not just isolated for XCFramework use case
|
Only My change is Apple-only: Can someone retrigger? |
No need, that CI is just broken ATM. |
|
Strong +1 on this — the missing libmtmd in the Apple xcframework is the single Real-world data point: we already run qwen3-vl-4b doing on-device image The opt-in LLAMA_BUILD_MTMD=OFF default here looks clean and non-breaking — would |
|
For a bit more context on viability: our Android build compiles libmtmd directly |
Adds an opt-in LLAMA_BUILD_MTMD CMake option so build-xcframework.sh can link libmtmd.a into the framework binary without pulling in the rest of tools/ (which doesn't cross-build cleanly to iOS/tvOS/visionOS). - CMakeLists.txt: new option, default OFF. When on with LLAMA_BUILD_TOOLS=OFF, only the tools/mtmd subdir is added. Useful for any binding that wants just libmtmd (Apple XCFramework, WASM). - tools/mtmd/CMakeLists.txt: gate the CLI exe targets on LLAMA_BUILD_TOOLS. Gating on LLAMA_BUILD_COMMON is not enough — it defaults ON in standalone builds and visionOS xcodebuild then fails with "install TARGETS given no BUNDLE DESTINATION for MACOSX_BUNDLE executable target 'llama-mtmd-cli'". - build-xcframework.sh: turn the option on, pass -DLLAMA_BUILD_MTMD, add libmtmd.a to combine_static_libraries, and copy mtmd.h and mtmd-helper.h into the framework Headers dir. The umbrella module map then exposes them, so Swift / Obj-C consumers can import the mtmd C API directly. After this, nm on ios-arm64/llama.framework/llama shows 52 _mtmd_ symbols. Verified end-to-end: a Swift target links the produced framework and calls mtmd_default_marker, mtmd_bitmap_init, etc. without a shim on macos / iphoneos / iphonesimulator / xros slices.
819264a to
87b8ed4
Compare
|
@ggerganov Following up from our side (production iOS app, currently text-only solely because the published xcframework ships We're validating the currently shipping head ( While doing this I hit one concrete, separable improvement worth folding into this PR:
LLAMA_BUILD_SERVER=OFF
+MTMD_VIDEO=OFF
GGML_METAL=ON -DLLAMA_BUILD_MTMD=${LLAMA_BUILD_MTMD}
+ -DMTMD_VIDEO=${MTMD_VIDEO}Safe because image (stb_image) and audio (miniaudio) decoding are compiled unconditionally and unaffected, and the full One smaller, forward-looking note, not a blocker: the umbrella sweeps in I haven't reproduced a break with the current modulemap (our consumer uses the C API), so treat this strictly as optional hygiene. Glad to PR the |
|
what are we missing for this? @ggerganov |
Adds an opt-in LLAMA_BUILD_MTMD CMake option so build-xcframework.sh can link libmtmd.a into the framework binary without pulling in the rest of tools/ (which doesn't cross-build cleanly to iOS/tvOS/visionOS). - CMakeLists.txt: new option, default OFF. When on with LLAMA_BUILD_TOOLS=OFF, only the tools/mtmd subdir is added. Useful for any binding that wants just libmtmd (Apple XCFramework, WASM). - tools/mtmd/CMakeLists.txt: gate the CLI exe targets on LLAMA_BUILD_TOOLS. Gating on LLAMA_BUILD_COMMON is not enough — it defaults ON in standalone builds and visionOS xcodebuild then fails with "install TARGETS given no BUNDLE DESTINATION for MACOSX_BUNDLE executable target 'llama-mtmd-cli'". - build-xcframework.sh: turn the option on, pass -DLLAMA_BUILD_MTMD, add libmtmd.a to combine_static_libraries, and copy mtmd.h and mtmd-helper.h into the framework Headers dir. The umbrella module map then exposes them, so Swift / Obj-C consumers can import the mtmd C API directly. After this, nm on ios-arm64/llama.framework/llama shows 52 _mtmd_ symbols. Verified end-to-end: a Swift target links the produced framework and calls mtmd_default_marker, mtmd_bitmap_init, etc. without a shim on macos / iphoneos / iphonesimulator / xros slices. Co-authored-by: Abraham Gonzalez <abraham@theabecaster.com>
Adds an opt-in LLAMA_BUILD_MTMD CMake option so build-xcframework.sh can link libmtmd.a into the framework binary without pulling in the rest of tools/ (which doesn't cross-build cleanly to iOS/tvOS/visionOS). - CMakeLists.txt: new option, default OFF. When on with LLAMA_BUILD_TOOLS=OFF, only the tools/mtmd subdir is added. Useful for any binding that wants just libmtmd (Apple XCFramework, WASM). - tools/mtmd/CMakeLists.txt: gate the CLI exe targets on LLAMA_BUILD_TOOLS. Gating on LLAMA_BUILD_COMMON is not enough — it defaults ON in standalone builds and visionOS xcodebuild then fails with "install TARGETS given no BUNDLE DESTINATION for MACOSX_BUNDLE executable target 'llama-mtmd-cli'". - build-xcframework.sh: turn the option on, pass -DLLAMA_BUILD_MTMD, add libmtmd.a to combine_static_libraries, and copy mtmd.h and mtmd-helper.h into the framework Headers dir. The umbrella module map then exposes them, so Swift / Obj-C consumers can import the mtmd C API directly. After this, nm on ios-arm64/llama.framework/llama shows 52 _mtmd_ symbols. Verified end-to-end: a Swift target links the produced framework and calls mtmd_default_marker, mtmd_bitmap_init, etc. without a shim on macos / iphoneos / iphonesimulator / xros slices. Co-authored-by: Abraham Gonzalez <abraham@theabecaster.com>
The Apple xcframework from
build-xcframework.shis built withLLAMA_BUILD_TOOLS=OFF, so nomtmd_*symbols end up in it. That means Swift / Obj-C consumers on iOS, macOS, tvOS, visionOS can't use vision even though the sources cross-compile fine.This adds an opt-in
LLAMA_BUILD_MTMDoption (default OFF). When on and full tools are off, only thetools/mtmdsubdirectory gets added so just the library target builds. The CLI exe targets intools/mtmd/CMakeLists.txtare now gated onLLAMA_BUILD_TOOLS— without that, standalone visionOS xcodebuild fails withinstall TARGETS given no BUNDLE DESTINATION for MACOSX_BUNDLE executable target 'llama-mtmd-cli'.build-xcframework.shturns the option on, addslibmtmd.atocombine_static_libraries, copiesmtmd.handmtmd-helper.hinto the frameworkHeaders/dir, and lists them in the modulemap. The C++ sections inmtmd.hare#ifdef __cplusplus-guarded, so Clang's pure-C module precompile never sees them; Swift consumersimport llamaand call the mtmd C API directly.Tested:
build-xcframework.shpasses on macos-15 for all 8 platform slicesnm ios-arm64/llama.framework/llama | grep -c ' _mtmd_'→ 52mtmd_default_marker,mtmd_context_params_default,mtmd_bitmap_init,mtmd_input_chunks_initon the macOS slice; typechecks on iphoneos / iphonesimulator / xros with the same modulemapLLAMA_BUILD_MTMD=OFForLLAMA_BUILD_TOOLS=ON) unchanged — full-tools build still pulls mtmd in viatools/Disclosure: I used Claude to help draft the CMake wording on the original version. Design, debugging, and on-device verification are mine.