| 
									
										
										
										
											2023-11-29 10:52:08 -08:00
										 |  |  | include(CMakeParseArguments)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-26 10:02:06 -08:00
										 |  |  | # clang format off
 | 
					
						
							|  |  |  | #
 | 
					
						
							| 
									
										
										
										
											2024-09-16 20:53:01 +01:00
										 |  |  | # ##############################################################################
 | 
					
						
							| 
									
										
										
										
											2023-11-29 10:52:08 -08:00
										 |  |  | # Build metal library
 | 
					
						
							|  |  |  | #
 | 
					
						
							|  |  |  | # Adds a custom target ${TARGET} to build ${OUTPUT_DIRECTORY}/{TITLE}.metallib
 | 
					
						
							|  |  |  | # from list ${SOURCES}, including list ${INCLUDE_DIRS}, depends on list ${DEPS}
 | 
					
						
							|  |  |  | #
 | 
					
						
							| 
									
										
										
										
											2024-09-16 20:53:01 +01:00
										 |  |  | # Args: TARGET: Custom target to be added for the metal library TITLE: Name of
 | 
					
						
							|  |  |  | # the .metallib OUTPUT_DIRECTORY: Where to place ${TITLE}.metallib SOURCES: List
 | 
					
						
							|  |  |  | # of source files INCLUDE_DIRS: List of include dirs DEPS: List of dependency
 | 
					
						
							| 
									
										
										
										
											2025-05-20 13:22:26 -04:00
										 |  |  | # files (like headers) DEBUG: Boolean, if true, enables debug compile options
 | 
					
						
							|  |  |  | # for this specific library. If not provided, uses global MLX_METAL_DEBUG.
 | 
					
						
							| 
									
										
										
										
											2023-11-29 10:52:08 -08:00
										 |  |  | #
 | 
					
						
							| 
									
										
										
										
											2025-02-26 10:02:06 -08:00
										 |  |  | # clang format on
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-29 10:52:08 -08:00
										 |  |  | macro(mlx_build_metallib)
 | 
					
						
							|  |  |  |   # Parse args
 | 
					
						
							| 
									
										
										
										
											2025-05-20 13:22:26 -04:00
										 |  |  |   set(oneValueArgs TARGET TITLE OUTPUT_DIRECTORY DEBUG)
 | 
					
						
							| 
									
										
										
										
											2023-11-29 10:52:08 -08:00
										 |  |  |   set(multiValueArgs SOURCES INCLUDE_DIRS DEPS)
 | 
					
						
							| 
									
										
										
										
											2024-09-16 20:53:01 +01:00
										 |  |  |   cmake_parse_arguments(MTLLIB "" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
 | 
					
						
							| 
									
										
										
										
											2023-11-29 10:52:08 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   # Set output
 | 
					
						
							|  |  |  |   set(MTLLIB_BUILD_TARGET "${MTLLIB_OUTPUT_DIRECTORY}/${MTLLIB_TITLE}.metallib")
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-16 20:53:01 +01:00
										 |  |  |   # Collect compile options
 | 
					
						
							| 
									
										
										
										
											2025-02-26 10:02:06 -08:00
										 |  |  |   set(MTLLIB_COMPILE_OPTIONS -Wall -Wextra -fno-fast-math -Wno-c++17-extensions)
 | 
					
						
							| 
									
										
										
										
											2025-05-20 13:22:26 -04:00
										 |  |  |   if(MLX_METAL_DEBUG OR MTLLIB_DEBUG)
 | 
					
						
							|  |  |  |     set(MTLLIB_COMPILE_OPTIONS ${MTLLIB_COMPILE_OPTIONS} -gline-tables-only | 
					
						
							|  |  |  |                                -frecord-sources)
 | 
					
						
							|  |  |  |   endif()
 | 
					
						
							| 
									
										
										
										
											2023-11-29 10:52:08 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-02 00:08:17 -05:00
										 |  |  |   # Prepare metallib build command
 | 
					
						
							| 
									
										
										
										
											2023-11-29 10:52:08 -08:00
										 |  |  |   add_custom_command( | 
					
						
							|  |  |  |     OUTPUT ${MTLLIB_BUILD_TARGET}
 | 
					
						
							| 
									
										
										
										
											2024-09-16 20:53:01 +01:00
										 |  |  |     COMMAND
 | 
					
						
							|  |  |  |       xcrun -sdk macosx metal
 | 
					
						
							|  |  |  |       "$<LIST:TRANSFORM,${MTLLIB_INCLUDE_DIRS},PREPEND,-I>"
 | 
					
						
							|  |  |  |       ${MTLLIB_COMPILE_OPTIONS} ${MTLLIB_SOURCES} -o ${MTLLIB_BUILD_TARGET}
 | 
					
						
							| 
									
										
										
										
											2023-11-29 10:52:08 -08:00
										 |  |  |     DEPENDS ${MTLLIB_DEPS} ${MTLLIB_SOURCES}
 | 
					
						
							|  |  |  |     COMMAND_EXPAND_LISTS
 | 
					
						
							|  |  |  |     COMMENT "Building ${MTLLIB_TITLE}.metallib"
 | 
					
						
							| 
									
										
										
										
											2024-09-16 20:53:01 +01:00
										 |  |  |     VERBATIM)
 | 
					
						
							| 
									
										
										
										
											2023-11-29 10:52:08 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   # Add metallib custom target
 | 
					
						
							| 
									
										
										
										
											2024-09-16 20:53:01 +01:00
										 |  |  |   add_custom_target(${MTLLIB_TARGET} DEPENDS ${MTLLIB_BUILD_TARGET})
 | 
					
						
							| 
									
										
										
										
											2023-11-29 10:52:08 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-16 20:53:01 +01:00
										 |  |  | endmacro(mlx_build_metallib)
 |