Commit 0cef6de
[MLX] Isolate submodule build with ExternalProject (#20585)
### Summary
Fixes #20556.
Right now MLX gets pulled in with `add_subdirectory`, so MLX's entire
CMake project lands in ExecuTorch's target/option namespace. This is a
problem because MLX fetches `nlohmann_json`, which collides with the
copy ExecuTorch already provides. The current workaround patches MLX's
`CMakeLists.txt` at configure time to guard the fetch. That patch is
brittle as it's pinned to specific lines, meaning an MLX bump that
touches them makes it silently stop applying, and the collision comes
back with no obvious signal. It also leaves the submodule dirty and lets
MLX's `MLX_BUILD_*` options bleed into ExecuTorch's cache.
This builds MLX as an `ExternalProject` in its own CMake scope and
consumes it through an imported `mlx` target, so MLX runs its
`FetchContent` in its own namespace and the patch is no longer needed.
- The imported `mlx` target re-adds the Metal/Foundation/QuartzCore
frameworks, since a static `libmlx.a` doesn't carry them. This mirrors
the imported `mlx` target already in
`tools/cmake/executorch-config.cmake`. `mlxdelegate` depends on
`mlx_external` explicitly, because `add_dependencies` on an imported
target doesn't order the build on its own.
- `install(TARGETS mlx)` is changed to `install(FILES
${_mlx_static_lib})`; an imported target cannot be installed with
`install(TARGETS)`.
- The metallib install and `MLX_METALLIB_PATH` now point at the
ExternalProject output.
- Removed `backends/mlx/patches/mlx_json.patch` and the patch loop.
The `BINARY_DIR` is kept where `add_subdirectory` put it, so `libmlx.a`
and `mlx.metallib` land at the same paths as before. The package config,
the metallib copy helper in `Utils.cmake`, and the wheel path in
`setup.py` are unchanged.
### Test plan
Verified on Apple Silicon (macOS, Metal toolchain installed):
`cmake --preset mlx-release` configures, builds, and installs;
`libmlx.a`, `libmlxdelegate.a`, and `mlx.metallib` are installed to
`cmake-out/lib/`, and the MLX submodule stays clean (`git -C
backends/mlx/third-party/mlx status`).
With `-DEXECUTORCH_BUILD_TESTS=ON`, `op_test_runner`,
`multi_thread_test_runner`, and `mlx_mutable_state_test` build and link
`mlx`; `mlx_mutable_state_test` passes.
I don't have the model checkpoints to run the qwen3_5_moe / gemma4_31b
MLX runners or the wheel end-to-end, so I'm relying on CI for those.
Since the artifact paths are unchanged, I'd expect them to behave the
same as before.
### AI assistance disclosure
I used an AI coding assistant for parts of this change, including
drafting the CMake and investigating the build. I reviewed and tested
everything in the test plan myself and take complete responsibility for
the submission.
---------
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-authored-by: Scott Roy <scroy@meta.com>1 parent 73c259e commit 0cef6de
3 files changed
Lines changed: 98 additions & 117 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
162 | 162 | | |
163 | 163 | | |
164 | 164 | | |
165 | | - | |
166 | | - | |
167 | | - | |
168 | | - | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
169 | 182 | | |
170 | | - | |
171 | | - | |
172 | | - | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
173 | 216 | | |
174 | | - | |
175 | | - | |
176 | | - | |
177 | | - | |
178 | | - | |
179 | | - | |
180 | | - | |
181 | | - | |
182 | | - | |
183 | | - | |
184 | | - | |
185 | | - | |
186 | | - | |
187 | | - | |
188 | | - | |
189 | | - | |
190 | | - | |
191 | | - | |
192 | | - | |
193 | | - | |
194 | | - | |
195 | | - | |
196 | | - | |
197 | | - | |
198 | | - | |
199 | | - | |
200 | | - | |
201 | | - | |
202 | | - | |
203 | | - | |
204 | | - | |
205 | | - | |
206 | | - | |
207 | | - | |
208 | | - | |
209 | | - | |
210 | | - | |
211 | | - | |
212 | | - | |
213 | | - | |
214 | | - | |
215 | | - | |
216 | | - | |
217 | | - | |
218 | | - | |
219 | | - | |
220 | | - | |
221 | | - | |
222 | | - | |
223 | | - | |
224 | | - | |
225 | | - | |
226 | | - | |
227 | | - | |
228 | | - | |
229 | | - | |
230 | | - | |
231 | | - | |
232 | | - | |
233 | | - | |
234 | | - | |
235 | | - | |
236 | | - | |
237 | | - | |
238 | 217 | | |
239 | | - | |
240 | | - | |
241 | | - | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
242 | 237 | | |
243 | 238 | | |
244 | 239 | | |
| |||
266 | 261 | | |
267 | 262 | | |
268 | 263 | | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
269 | 268 | | |
270 | 269 | | |
271 | 270 | | |
| |||
283 | 282 | | |
284 | 283 | | |
285 | 284 | | |
286 | | - | |
287 | | - | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
288 | 288 | | |
289 | 289 | | |
290 | 290 | | |
| |||
301 | 301 | | |
302 | 302 | | |
303 | 303 | | |
304 | | - | |
305 | | - | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
306 | 308 | | |
307 | 309 | | |
308 | 310 | | |
| |||
325 | 327 | | |
326 | 328 | | |
327 | 329 | | |
328 | | - | |
329 | | - | |
330 | | - | |
331 | | - | |
332 | | - | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
333 | 334 | | |
334 | 335 | | |
335 | 336 | | |
336 | 337 | | |
337 | | - | |
| 338 | + | |
338 | 339 | | |
339 | 340 | | |
340 | 341 | | |
| |||
This file was deleted.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
134 | 134 | | |
135 | 135 | | |
136 | 136 | | |
137 | | - | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
138 | 141 | | |
139 | 142 | | |
140 | 143 | | |
141 | | - | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
142 | 149 | | |
143 | | - | |
144 | | - | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
145 | 154 | | |
146 | 155 | | |
147 | 156 | | |
| |||
0 commit comments