halide 19.0.0__cp313-cp313-macosx_11_0_arm64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. halide/__init__.py +39 -0
  2. halide/_generator_helpers.py +835 -0
  3. halide/bin/adams2019_retrain_cost_model +0 -0
  4. halide/bin/adams2019_weightsdir_to_weightsfile +0 -0
  5. halide/bin/anderson2021_retrain_cost_model +0 -0
  6. halide/bin/anderson2021_weightsdir_to_weightsfile +0 -0
  7. halide/bin/featurization_to_sample +0 -0
  8. halide/bin/gengen +0 -0
  9. halide/bin/get_host_target +0 -0
  10. halide/halide_.cpython-313-darwin.so +0 -0
  11. halide/imageio.py +60 -0
  12. halide/include/Halide.h +35293 -0
  13. halide/include/HalideBuffer.h +2618 -0
  14. halide/include/HalidePyTorchCudaHelpers.h +64 -0
  15. halide/include/HalidePyTorchHelpers.h +120 -0
  16. halide/include/HalideRuntime.h +2221 -0
  17. halide/include/HalideRuntimeCuda.h +89 -0
  18. halide/include/HalideRuntimeD3D12Compute.h +91 -0
  19. halide/include/HalideRuntimeHexagonDma.h +104 -0
  20. halide/include/HalideRuntimeHexagonHost.h +157 -0
  21. halide/include/HalideRuntimeMetal.h +112 -0
  22. halide/include/HalideRuntimeOpenCL.h +119 -0
  23. halide/include/HalideRuntimeQurt.h +32 -0
  24. halide/include/HalideRuntimeVulkan.h +137 -0
  25. halide/include/HalideRuntimeWebGPU.h +44 -0
  26. halide/lib/cmake/Halide/FindHalide_LLVM.cmake +152 -0
  27. halide/lib/cmake/Halide/FindV8.cmake +33 -0
  28. halide/lib/cmake/Halide/Halide-shared-deps.cmake +0 -0
  29. halide/lib/cmake/Halide/Halide-shared-targets-release.cmake +29 -0
  30. halide/lib/cmake/Halide/Halide-shared-targets.cmake +154 -0
  31. halide/lib/cmake/Halide/HalideConfig.cmake +162 -0
  32. halide/lib/cmake/Halide/HalideConfigVersion.cmake +65 -0
  33. halide/lib/cmake/HalideHelpers/FindHalide_WebGPU.cmake +27 -0
  34. halide/lib/cmake/HalideHelpers/Halide-Interfaces-release.cmake +116 -0
  35. halide/lib/cmake/HalideHelpers/Halide-Interfaces.cmake +236 -0
  36. halide/lib/cmake/HalideHelpers/HalideGeneratorHelpers.cmake +1056 -0
  37. halide/lib/cmake/HalideHelpers/HalideHelpersConfig.cmake +28 -0
  38. halide/lib/cmake/HalideHelpers/HalideHelpersConfigVersion.cmake +54 -0
  39. halide/lib/cmake/HalideHelpers/HalideTargetHelpers.cmake +99 -0
  40. halide/lib/cmake/HalideHelpers/MutexCopy.ps1 +31 -0
  41. halide/lib/cmake/HalideHelpers/TargetExportScript.cmake +55 -0
  42. halide/lib/cmake/Halide_Python/Halide_Python-targets-release.cmake +30 -0
  43. halide/lib/cmake/Halide_Python/Halide_Python-targets.cmake +125 -0
  44. halide/lib/cmake/Halide_Python/Halide_PythonConfig.cmake +26 -0
  45. halide/lib/cmake/Halide_Python/Halide_PythonConfigVersion.cmake +65 -0
  46. halide/lib/libHalide.dylib +0 -0
  47. halide/lib/libHalidePyStubs.a +0 -0
  48. halide/lib/libHalide_GenGen.a +0 -0
  49. halide/lib/libautoschedule_adams2019.so +0 -0
  50. halide/lib/libautoschedule_anderson2021.so +0 -0
  51. halide/lib/libautoschedule_li2018.so +0 -0
  52. halide/lib/libautoschedule_mullapudi2016.so +0 -0
  53. halide/share/doc/Halide/LICENSE.txt +233 -0
  54. halide/share/doc/Halide/README.md +439 -0
  55. halide/share/doc/Halide/doc/BuildingHalideWithCMake.md +626 -0
  56. halide/share/doc/Halide/doc/CodeStyleCMake.md +393 -0
  57. halide/share/doc/Halide/doc/FuzzTesting.md +104 -0
  58. halide/share/doc/Halide/doc/HalideCMakePackage.md +812 -0
  59. halide/share/doc/Halide/doc/Hexagon.md +73 -0
  60. halide/share/doc/Halide/doc/Python.md +844 -0
  61. halide/share/doc/Halide/doc/RunGen.md +283 -0
  62. halide/share/doc/Halide/doc/Testing.md +125 -0
  63. halide/share/doc/Halide/doc/Vulkan.md +287 -0
  64. halide/share/doc/Halide/doc/WebAssembly.md +228 -0
  65. halide/share/doc/Halide/doc/WebGPU.md +128 -0
  66. halide/share/tools/RunGen.h +1470 -0
  67. halide/share/tools/RunGenMain.cpp +642 -0
  68. halide/share/tools/adams2019_autotune_loop.sh +227 -0
  69. halide/share/tools/anderson2021_autotune_loop.sh +591 -0
  70. halide/share/tools/halide_benchmark.h +240 -0
  71. halide/share/tools/halide_image.h +31 -0
  72. halide/share/tools/halide_image_info.h +318 -0
  73. halide/share/tools/halide_image_io.h +2794 -0
  74. halide/share/tools/halide_malloc_trace.h +102 -0
  75. halide/share/tools/halide_thread_pool.h +161 -0
  76. halide/share/tools/halide_trace_config.h +559 -0
  77. halide-19.0.0.data/data/share/cmake/Halide/HalideConfig.cmake +6 -0
  78. halide-19.0.0.data/data/share/cmake/Halide/HalideConfigVersion.cmake +65 -0
  79. halide-19.0.0.data/data/share/cmake/HalideHelpers/HalideHelpersConfig.cmake +6 -0
  80. halide-19.0.0.data/data/share/cmake/HalideHelpers/HalideHelpersConfigVersion.cmake +54 -0
  81. halide-19.0.0.dist-info/METADATA +301 -0
  82. halide-19.0.0.dist-info/RECORD +84 -0
  83. halide-19.0.0.dist-info/WHEEL +5 -0
  84. halide-19.0.0.dist-info/licenses/LICENSE.txt +233 -0
@@ -0,0 +1,1056 @@
1
+ cmake_minimum_required(VERSION 3.28)
2
+
3
+ option(Halide_NO_DEFAULT_FLAGS "When enabled, suppresses recommended flags in add_halide_generator" OFF)
4
+
5
+ include(${CMAKE_CURRENT_LIST_DIR}/HalideTargetHelpers.cmake)
6
+ include(${CMAKE_CURRENT_LIST_DIR}/TargetExportScript.cmake)
7
+
8
+ define_property(TARGET PROPERTY Halide_RT_TARGETS
9
+ BRIEF_DOCS "On a Halide runtime target, lists the targets the runtime backs"
10
+ FULL_DOCS "On a Halide runtime target, lists the targets the runtime backs")
11
+
12
+ define_property(TARGET PROPERTY Halide_GENERATOR_HAS_POST_BUILD
13
+ BRIEF_DOCS "On a Halide generator target, true if Halide.dll copy command has already been added."
14
+ FULL_DOCS "On a Halide generator target, true if Halide.dll copy command has already been added.")
15
+
16
+ define_property(TARGET PROPERTY Halide_PYTHON_GENERATOR_SOURCE
17
+ BRIEF_DOCS "Used to store the source file(s) for a Python Generator"
18
+ FULL_DOCS "Used to store the source file(s) for a Python Generator")
19
+
20
+ define_property(TARGET PROPERTY Halide_LIBRARY_RUNTIME_TARGET
21
+ BRIEF_DOCS "On a Halide library target, the runtime it uses."
22
+ FULL_DOCS "On a Halide library target, the runtime it uses.")
23
+
24
+ define_property(TARGET PROPERTY Halide_LIBRARY_PYTHON_EXTENSION_CPP
25
+ BRIEF_DOCS "On a Halide library target, the .py.cpp generated for it (absent if none)."
26
+ FULL_DOCS "On a Halide library target, the .py.cpp generated for it (absent if none).")
27
+
28
+ define_property(TARGET PROPERTY Halide_LIBRARY_FUNCTION_NAME
29
+ BRIEF_DOCS "On a Halide library target, the FUNCTION_NAME used."
30
+ FULL_DOCS "On a Halide library target, the FUNCTION_NAME used.")
31
+
32
+ ##
33
+ # Function to simplify writing the CMake rules for creating a generator executable
34
+ # that follows our recommended cross-compiling workflow.
35
+ ##
36
+
37
+ function(add_halide_generator TARGET)
38
+ set(options "")
39
+ set(oneValueArgs PACKAGE_NAME PACKAGE_NAMESPACE EXPORT_FILE PYSTUB)
40
+ set(multiValueArgs SOURCES LINK_LIBRARIES)
41
+ cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
42
+
43
+ if (NOT ARG_PACKAGE_NAME)
44
+ set(ARG_PACKAGE_NAME "${PROJECT_NAME}-halide_generators")
45
+ endif ()
46
+
47
+ if (NOT ARG_PACKAGE_NAMESPACE)
48
+ set(ARG_PACKAGE_NAMESPACE "${PROJECT_NAME}::halide_generators::")
49
+ endif ()
50
+
51
+ if (NOT ARG_EXPORT_FILE)
52
+ file(MAKE_DIRECTORY "${PROJECT_BINARY_DIR}/cmake")
53
+ set(ARG_EXPORT_FILE "${PROJECT_BINARY_DIR}/cmake/${ARG_PACKAGE_NAME}Config.cmake")
54
+ endif ()
55
+
56
+ if (NOT ARG_SOURCES)
57
+ set(ARG_SOURCES "${ARG_UNPARSED_ARGUMENTS}")
58
+ endif ()
59
+
60
+ _Halide_try_load_generators("${ARG_PACKAGE_NAME}")
61
+
62
+ # Communicate found information to the caller
63
+ set(${ARG_PACKAGE_NAME}_FOUND "${${ARG_PACKAGE_NAME}_FOUND}" PARENT_SCOPE)
64
+
65
+ set(gen "${ARG_PACKAGE_NAMESPACE}${TARGET}")
66
+ if (NOT TARGET "${gen}")
67
+ if (NOT TARGET "${ARG_PACKAGE_NAME}")
68
+ add_custom_target("${ARG_PACKAGE_NAME}")
69
+ endif ()
70
+
71
+ if (NOT Halide_FOUND)
72
+ find_package(Halide REQUIRED)
73
+ endif ()
74
+
75
+ if (ARG_SOURCES MATCHES ".py$")
76
+ if (ARG_LINK_LIBRARIES)
77
+ message(FATAL_ERROR "You cannot specify LINK_LIBRARIES in conjunction with Python source code.")
78
+ endif ()
79
+ if (ARG_PYSTUB)
80
+ message(FATAL_ERROR "You cannot specify PYSTUB in conjunction with Python source code (only C++ Generators can produce PyStubs).")
81
+ endif ()
82
+
83
+ list(LENGTH ARG_SOURCES len)
84
+ if (NOT len EQUAL 1)
85
+ message(FATAL_ERROR "Python Generators must specify exactly one source file.")
86
+ endif ()
87
+
88
+ # Make a fake target here that we can attach the Python source to,
89
+ # so that we can extract 'em in add_halide_library()
90
+ add_custom_target(${TARGET} ALL)
91
+ set_property(TARGET ${TARGET} PROPERTY Halide_PYTHON_GENERATOR_SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/${ARG_SOURCES}")
92
+
93
+ # TODO: what do we need to do for PACKAGE_NAME PACKAGE_NAMESPACE EXPORT_FILE in this case?
94
+ else ()
95
+ add_executable(${TARGET} ${ARG_SOURCES})
96
+ add_executable(${gen} ALIAS ${TARGET})
97
+ target_link_libraries(${TARGET} PRIVATE Halide::Generator ${ARG_LINK_LIBRARIES})
98
+
99
+ _Halide_place_dll(${TARGET})
100
+
101
+ if (NOT ARG_NO_DEFAULT_FLAGS AND NOT Halide_NO_DEFAULT_FLAGS)
102
+ # For crosscompiling builds, the Halide headers will be included using -isystem,
103
+ # which will cause all warnings to be ignored. This is not helpful, since
104
+ # we *want* deprecation warnings to be propagated. So we must set
105
+ # NO_SYSTEM_FROM_IMPORTED in order for it to be seen.
106
+ set_target_properties(${TARGET} PROPERTIES NO_SYSTEM_FROM_IMPORTED YES)
107
+ target_compile_options(
108
+ ${TARGET} PRIVATE
109
+ $<$<CXX_COMPILER_ID:GNU,Clang,AppleClang>:-Wdeprecated-declarations>
110
+ $<$<CXX_COMPILER_ID:MSVC>:/w14996> # 4996: compiler encountered deprecated declaration
111
+ )
112
+ endif ()
113
+
114
+ add_dependencies("${ARG_PACKAGE_NAME}" ${TARGET})
115
+ export(TARGETS ${TARGET}
116
+ NAMESPACE ${ARG_PACKAGE_NAMESPACE}
117
+ APPEND FILE "${ARG_EXPORT_FILE}")
118
+ endif ()
119
+ endif ()
120
+
121
+ if (ARG_PYSTUB)
122
+ set(GEN_NAME ${ARG_PYSTUB})
123
+ set(MODULE_NAME ${ARG_PYSTUB}_pystub)
124
+ # Generate a small C++ file that includes the boilerplate code needed to
125
+ # register a PyInit function that has the stub glue code.
126
+ string(CONCAT stub_text
127
+ "#include <Python.h>\n"
128
+ "#include \"Halide.h\"\n"
129
+ "HALIDE_GENERATOR_PYSTUB(${GEN_NAME}, ${MODULE_NAME})\n")
130
+
131
+ set(stub_file "${CMAKE_CURRENT_BINARY_DIR}/${TARGET}.${GEN_NAME}.${MODULE_NAME}.py_stub_generated.cpp")
132
+ file(CONFIGURE OUTPUT "${stub_file}" CONTENT "${stub_text}" @ONLY)
133
+
134
+ Python_add_library(${TARGET}_pystub MODULE WITH_SOABI "${stub_file}" ${ARG_SOURCES})
135
+ set_target_properties(${TARGET}_pystub PROPERTIES
136
+ CXX_VISIBILITY_PRESET hidden
137
+ VISIBILITY_INLINES_HIDDEN ON
138
+ POSITION_INDEPENDENT_CODE ON)
139
+ target_link_libraries(${TARGET}_pystub PRIVATE Halide::PyStubs Halide::Halide ${ARG_LINK_LIBRARIES})
140
+ set_target_properties(${TARGET}_pystub PROPERTIES OUTPUT_NAME ${MODULE_NAME})
141
+ _Halide_target_export_single_symbol(${TARGET}_pystub "PyInit_${MODULE_NAME}")
142
+ endif ()
143
+ endfunction()
144
+
145
+ function(_Halide_try_load_generators package_name)
146
+ # Don't repeatedly run the search for the tools package.
147
+ if (NOT DEFINED ${package_name}_FOUND)
148
+ # Some toolchains, like Emscripten, try to disable finding packages
149
+ # outside their sysroots, but we always want to find the native
150
+ # generators. Setting CMAKE_FIND_ROOT_PATH_BOTH here overrides
151
+ # the toolchain search preference. This is okay since a user can
152
+ # always override this call by setting ${package_name}_ROOT.
153
+ find_package(${package_name} QUIET CMAKE_FIND_ROOT_PATH_BOTH)
154
+
155
+ # Communicate found information to the caller
156
+ set(${package_name}_FOUND "${${package_name}_FOUND}" PARENT_SCOPE)
157
+
158
+ if (NOT ${package_name}_FOUND AND CMAKE_CROSSCOMPILING AND NOT CMAKE_CROSSCOMPILING_EMULATOR)
159
+ message(WARNING
160
+ "'${package_name}' was not found and it looks like you "
161
+ "are cross-compiling without an emulator. This is likely to "
162
+ "fail. Please set -D${package_name}_ROOT=... at the CMake "
163
+ "command line to the build directory of a host-built ${PROJECT_NAME}.")
164
+ endif ()
165
+ endif ()
166
+ endfunction()
167
+
168
+ function(_Halide_library_from_generator TARGET)
169
+ cmake_parse_arguments(
170
+ PARSE_ARGV 1 ARG ""
171
+ "FUNCTION_NAME;GENERATOR;GRADIENT_DESCENT;TYPE;USE_RUNTIME"
172
+ "COMMAND;DEPENDS;EXTRA_OUTPUTS;PARAMS;PLUGINS;TARGETS"
173
+ )
174
+
175
+ ## "hash table" of extra outputs to extensions
176
+ set(assembly_extension ".s")
177
+ set(bitcode_extension ".bc")
178
+ set(c_source_extension ".halide_generated.cpp")
179
+ set(compiler_log_extension ".halide_compiler_log")
180
+ set(featurization_extension ".featurization")
181
+ set(function_info_header_extension ".function_info.h")
182
+ set(hlpipe_extension ".hlpipe")
183
+ set(llvm_assembly_extension ".ll")
184
+ set(python_extension_extension ".py.cpp")
185
+ set(pytorch_wrapper_extension ".pytorch.h")
186
+ set(registration_extension ".registration.cpp")
187
+ set(schedule_extension ".schedule.h")
188
+ set(stmt_extension ".stmt")
189
+ set(stmt_html_extension ".stmt.html")
190
+
191
+ ## Validate TYPE
192
+ if (NOT ARG_TYPE MATCHES "^(c_source|static_library|object)$")
193
+ message(FATAL_ERROR "`${ARG_TYPE}` not among: c_source, object, static_library")
194
+ endif ()
195
+
196
+ ## Validate plugins
197
+ foreach (plugin IN LISTS ARG_PLUGINS)
198
+ if (NOT TARGET "${plugin}")
199
+ message(FATAL_ERROR "Plugin `${plugin}` is not a target.")
200
+ endif ()
201
+ endforeach ()
202
+
203
+ ## Always omit the runtime
204
+ list(TRANSFORM ARG_TARGETS APPEND "-no_runtime")
205
+
206
+ ## Resolve plugins
207
+ if (ARG_PLUGINS)
208
+ list(TRANSFORM ARG_PLUGINS REPLACE "(.+)" "$<TARGET_FILE:\\1>" OUTPUT_VARIABLE plugins_args)
209
+ list(JOIN plugins_args "," plugins_args)
210
+ list(PREPEND plugins_args -p)
211
+ else ()
212
+ set(plugins_args "")
213
+ endif ()
214
+
215
+ ## Gather platform information
216
+ _Halide_get_platform_extensions(
217
+ object_extension
218
+ static_library_extension
219
+ "${ARG_TARGETS}"
220
+ )
221
+
222
+ macro(_Halide_add_output type base_name)
223
+ list(APPEND outputs "${type}")
224
+ list(APPEND output_files "${base_name}${${type}_extension}")
225
+ endmacro()
226
+
227
+ ## Check the type to determine outputs
228
+ set(outputs c_header)
229
+ set(output_files "${TARGET}.h")
230
+
231
+ list(LENGTH ARG_TARGETS num_targets)
232
+ if (ARG_TYPE STREQUAL "object" AND num_targets GREATER 1)
233
+ foreach (t IN LISTS ARG_TARGETS)
234
+ _Halide_add_output("${ARG_TYPE}" "${TARGET}-${t}")
235
+ endforeach ()
236
+ _Halide_add_output("${ARG_TYPE}" "${TARGET}_wrapper")
237
+ else ()
238
+ # c_source, static_library, or object with one target
239
+ _Halide_add_output("${ARG_TYPE}" "${TARGET}")
240
+ endif ()
241
+
242
+ foreach (output IN LISTS ARG_EXTRA_OUTPUTS)
243
+ if (output STREQUAL "c_source" AND ARG_TYPE STREQUAL "c_source")
244
+ # Skip this as it was handled above
245
+ else ()
246
+ _Halide_add_output("${output}" "${TARGET}")
247
+ endif ()
248
+ endforeach ()
249
+
250
+ ## Run the generator
251
+ add_custom_command(
252
+ OUTPUT ${output_files}
253
+ COMMAND ${ARG_COMMAND}
254
+ -n "${TARGET}"
255
+ -d "$<BOOL:${ARG_GRADIENT_DESCENT}>"
256
+ -g "${ARG_GENERATOR}"
257
+ -f "${ARG_FUNCTION_NAME}"
258
+ -e "$<LOWER_CASE:$<JOIN:$<REMOVE_DUPLICATES:${outputs}>,$<COMMA>>>"
259
+ ${plugins_args}
260
+ -o .
261
+ "target=$<JOIN:${ARG_TARGETS},$<COMMA>>"
262
+ ${ARG_PARAMS}
263
+ DEPENDS ${ARG_DEPENDS} ${ARG_PLUGINS}
264
+ VERBATIM
265
+ )
266
+
267
+ ## Populate output variables
268
+ list(TRANSFORM output_files PREPEND "${CMAKE_CURRENT_BINARY_DIR}/")
269
+
270
+ foreach (out IN LISTS outputs)
271
+ set("local_out_${out}" "")
272
+ endforeach ()
273
+
274
+ foreach (out file IN ZIP_LISTS outputs output_files)
275
+ list(APPEND "local_out_${out}" "${file}")
276
+ list(APPEND "OUT_${out}" "${file}")
277
+ endforeach ()
278
+
279
+ foreach (out IN LISTS outputs)
280
+ set("OUT_${out}" "${OUT_${out}}" PARENT_SCOPE)
281
+ endforeach ()
282
+
283
+ # Create the filter's library target
284
+ if (ARG_TYPE STREQUAL "static_library")
285
+ add_library("${TARGET}" STATIC IMPORTED GLOBAL)
286
+ set_target_properties("${TARGET}" PROPERTIES IMPORTED_LOCATION "${local_out_${ARG_TYPE}}")
287
+ else ()
288
+ add_library("${TARGET}" STATIC ${local_out_${ARG_TYPE}})
289
+ set_property(TARGET "${TARGET}" PROPERTY POSITION_INDEPENDENT_CODE ON)
290
+ set_property(TARGET "${TARGET}" PROPERTY LINKER_LANGUAGE CXX)
291
+
292
+ if (NOT Halide_NO_DEFAULT_FLAGS)
293
+ # Silence many useless warnings in generated C++ code compilation
294
+ target_compile_options(
295
+ "${TARGET}" PRIVATE $<$<CXX_COMPILER_ID:GNU,Clang,AppleClang>:-Wno-psabi>
296
+ )
297
+ endif ()
298
+ _Halide_fix_xcode("${TARGET}")
299
+ endif ()
300
+
301
+ add_custom_target("${TARGET}.update" DEPENDS ${output_files})
302
+ add_dependencies("${TARGET}" "${TARGET}.update")
303
+
304
+ target_link_libraries("${TARGET}" INTERFACE "${ARG_USE_RUNTIME}")
305
+ add_dependencies("${TARGET}" "${ARG_USE_RUNTIME}")
306
+
307
+ if (NOT ARG_TYPE STREQUAL "c_source")
308
+ _Halide_add_targets_to_runtime("${ARG_USE_RUNTIME}" TARGETS ${ARG_TARGETS})
309
+ endif ()
310
+
311
+ target_sources("${TARGET}" INTERFACE
312
+ FILE_SET HEADERS
313
+ BASE_DIRS "${CMAKE_CURRENT_BINARY_DIR}"
314
+ FILES "${local_out_c_header}")
315
+ endfunction()
316
+
317
+ function(_Halide_lipo)
318
+ cmake_parse_arguments(PARSE_ARGV 0 ARG "OVERWRITE" "TARGET" "INPUTS")
319
+
320
+ set(merged_libs ${ARG_INPUTS})
321
+
322
+ list(TRANSFORM merged_libs REPLACE "^(.+)$" "$<TARGET_FILE:\\1>"
323
+ OUTPUT_VARIABLE merged_libs_files)
324
+
325
+ if (ARG_OVERWRITE)
326
+ list(APPEND merged_libs_files "$<TARGET_FILE:${ARG_TARGET}>")
327
+ endif ()
328
+
329
+ find_program(LIPO lipo REQUIRED)
330
+ add_custom_command(
331
+ TARGET "${ARG_TARGET}" POST_BUILD
332
+ COMMAND "${LIPO}" -create ${merged_libs_files} -output "$<TARGET_FILE:${ARG_TARGET}>"
333
+ VERBATIM
334
+ )
335
+
336
+ list(TRANSFORM merged_libs REPLACE "^(.+)$" "$<COMPILE_ONLY:\\1>"
337
+ OUTPUT_VARIABLE merged_libs_targets)
338
+ target_link_libraries("${ARG_TARGET}" INTERFACE ${merged_libs_targets})
339
+
340
+ ## Visual Studio and Xcode lack any way to specify that the link step of
341
+ # building a target has dependencies outside the object files that go inside
342
+ # the target. CMake models this with LINK_DEPENDS, which only has an effect
343
+ # on the Ninja and Makefile generators. However, it does not consider static
344
+ # libraries to have a link step! So instead, we create a dummy file and use
345
+ # OBJECT_DEPENDS (subject to the same generator restrictions).
346
+
347
+ if (NOT CMAKE_GENERATOR MATCHES "Ninja|Make")
348
+ message(
349
+ WARNING
350
+ "Generator ${CMAKE_GENERATOR} does not support OBJECT_DEPENDS! "
351
+ "${ARG_TARGET} will not be rebuilt accurately in incremental builds."
352
+ )
353
+ endif ()
354
+
355
+ set(sources "")
356
+ foreach (lib IN LISTS merged_libs)
357
+ get_property(lib_sources TARGET "${lib}" PROPERTY SOURCES)
358
+ foreach (source IN LISTS lib_sources)
359
+ cmake_path(ABSOLUTE_PATH source BASE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}")
360
+ list(APPEND sources ${source})
361
+ endforeach ()
362
+ endforeach ()
363
+
364
+ string(MAKE_C_IDENTIFIER "Halide_lipo_dummy_symbol_${ARG_TARGET}" dummy_symbol)
365
+ set(dep_src "${CMAKE_CURRENT_BINARY_DIR}/${dummy_symbol}.cpp")
366
+ file(CONFIGURE OUTPUT "${dep_src}" CONTENT [[
367
+ /* suppresses warning from ranlib */
368
+ void @dummy_symbol@() {}
369
+ ]] @ONLY)
370
+ target_sources("${ARG_TARGET}" PRIVATE "${dep_src}")
371
+ set_source_files_properties("${dep_src}" PROPERTIES OBJECT_DEPENDS "${sources}")
372
+ endfunction()
373
+
374
+ function(_Halide_compute_generator_cmd)
375
+ cmake_parse_arguments(PARSE_ARGV 0 ARG "" "FROM;OUT_COMMAND;OUT_DEPENDS" "")
376
+
377
+ if (NOT ARG_FROM)
378
+ message(FATAL_ERROR "Missing FROM argument specifying a Halide generator target")
379
+ endif ()
380
+
381
+ if (NOT TARGET "${ARG_FROM}")
382
+ # FROM is usually an unqualified name; if we are crosscompiling, we might need a
383
+ # fully-qualified name, so add the default package name and retry
384
+ set(FQ_ARG_FROM "${PROJECT_NAME}::halide_generators::${ARG_FROM}")
385
+ if (NOT TARGET "${FQ_ARG_FROM}")
386
+ message(FATAL_ERROR "Unable to locate FROM as either ${ARG_FROM} or ${FQ_ARG_FROM}")
387
+ endif ()
388
+
389
+ set(ARG_FROM "${FQ_ARG_FROM}")
390
+ endif ()
391
+
392
+ get_property(py_src TARGET "${ARG_FROM}" PROPERTY Halide_PYTHON_GENERATOR_SOURCE)
393
+ if (NOT py_src)
394
+ set("${ARG_OUT_COMMAND}" "${ARG_FROM}" PARENT_SCOPE)
395
+ set("${ARG_OUT_DEPENDS}" "${ARG_FROM}" PARENT_SCOPE)
396
+ return()
397
+ endif ()
398
+
399
+ # TODO: Python Generators need work to support crosscompiling (https://github.com/halide/Halide/issues/7014)
400
+ if (NOT TARGET Halide::Python)
401
+ message(FATAL_ERROR "Missing Halide::Python. Load the Python component "
402
+ "in find_package() or set WITH_PYTHON_BINDINGS=ON if in tree.")
403
+ endif ()
404
+
405
+ if (NOT TARGET Python::Interpreter)
406
+ message(FATAL_ERROR "Missing Python::Interpreter. Missing call to find_package(Python 3)?")
407
+ endif ()
408
+
409
+ set("${ARG_OUT_COMMAND}"
410
+ ${CMAKE_COMMAND} -E env "PYTHONPATH=$<PATH:NORMAL_PATH,$<TARGET_FILE_DIR:Halide::Python>/..>" --
411
+ ${Halide_PYTHON_LAUNCHER} "$<TARGET_FILE:Python::Interpreter>" $<SHELL_PATH:${py_src}>
412
+ PARENT_SCOPE)
413
+ set("${ARG_OUT_DEPENDS}" ${ARG_FROM} Halide::Python ${py_src} PARENT_SCOPE)
414
+ endfunction()
415
+
416
+ function(_Halide_set_osx_arch TARGET TRIPLE)
417
+ if (APPLE)
418
+ if (TRIPLE STREQUAL "arm-64-osx")
419
+ set_property(TARGET "${TARGET}" PROPERTY OSX_ARCHITECTURES "arm64")
420
+ elseif (TRIPLE STREQUAL "x86-64-osx")
421
+ set_property(TARGET "${TARGET}" PROPERTY OSX_ARCHITECTURES "x86_64")
422
+ else ()
423
+ message(FATAL_ERROR "Could not set OSX_ARCHITECTURES for ${TRIPLE}")
424
+ endif ()
425
+ endif ()
426
+ endfunction()
427
+
428
+ ##
429
+ # Function to simplify writing the CMake rules for invoking a generator executable
430
+ # and getting a usable CMake library out of it.
431
+ ##
432
+
433
+ function(add_halide_library TARGET)
434
+ ##
435
+ # Set up argument parsing for extra outputs.
436
+ ##
437
+
438
+ # See Module.cpp for list of extra outputs. The following outputs intentionally do not appear:
439
+ # - `c_header` is always generated
440
+ # - `c_source` is selected by C_BACKEND
441
+ # - `object` is selected for CMake-target-compile
442
+ # - `static_library` is selected for cross-compile
443
+ # - `cpp_stub` is not available
444
+ set(extra_output_names
445
+ ASSEMBLY
446
+ BITCODE
447
+ COMPILER_LOG
448
+ C_SOURCE
449
+ FEATURIZATION
450
+ FUNCTION_INFO_HEADER
451
+ HLPIPE
452
+ LLVM_ASSEMBLY
453
+ PYTHON_EXTENSION
454
+ PYTORCH_WRAPPER
455
+ REGISTRATION
456
+ SCHEDULE
457
+ STMT
458
+ STMT_HTML)
459
+
460
+ ##
461
+ # Parse the arguments and set defaults for missing values.
462
+ ##
463
+
464
+ set(features_args FEATURES)
465
+ foreach (arch IN ITEMS x86 arm powerpc hexagon wasm riscv)
466
+ foreach (bits IN ITEMS 32 64)
467
+ foreach (os IN ITEMS linux windows osx android ios qurt noos fuchsia wasmrt)
468
+ list(APPEND features_args "FEATURES[${arch}-${bits}-${os}]")
469
+ endforeach ()
470
+ endforeach ()
471
+ endforeach ()
472
+
473
+ set(options C_BACKEND GRADIENT_DESCENT)
474
+ set(oneValueArgs FROM GENERATOR FUNCTION_NAME NAMESPACE USE_RUNTIME AUTOSCHEDULER HEADER ${extra_output_names} NO_THREADS NO_DL_LIBS)
475
+ set(multiValueArgs TARGETS PARAMS PLUGINS ${features_args})
476
+ cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
477
+
478
+ if (NOT "${ARG_UNPARSED_ARGUMENTS}" STREQUAL "")
479
+ message(AUTHOR_WARNING "Arguments to add_halide_library were not recognized: ${ARG_UNPARSED_ARGUMENTS}")
480
+ endif ()
481
+
482
+ if (ARG_C_BACKEND AND ARG_TARGETS)
483
+ message(AUTHOR_WARNING "The C backend sources will be compiled with the current CMake toolchain.")
484
+ endif ()
485
+
486
+ if (NOT ARG_GENERATOR)
487
+ set(ARG_GENERATOR "${TARGET}")
488
+ endif ()
489
+
490
+ if (NOT ARG_FUNCTION_NAME)
491
+ set(ARG_FUNCTION_NAME "${TARGET}")
492
+ endif ()
493
+
494
+ if (ARG_NAMESPACE)
495
+ set(ARG_FUNCTION_NAME "${ARG_NAMESPACE}::${ARG_FUNCTION_NAME}")
496
+ endif ()
497
+
498
+ # If no TARGETS argument, use Halide_TARGET instead
499
+ if (NOT ARG_TARGETS)
500
+ set(ARG_TARGETS "${Halide_TARGET}")
501
+ endif ()
502
+
503
+ # If still no TARGET, try to use host, but if that would
504
+ # cross-compile, then default to 'cmake' and warn.
505
+ if (NOT ARG_TARGETS)
506
+ if (Halide_HOST_TARGET STREQUAL Halide_CMAKE_TARGET)
507
+ set(ARG_TARGETS host)
508
+ else ()
509
+ message(AUTHOR_WARNING
510
+ "Targets must be manually specified to add_halide_library when cross-compiling. "
511
+ "The default 'host' target ${Halide_HOST_TARGET} differs from the active CMake "
512
+ "target ${Halide_CMAKE_TARGET}. Using ${Halide_CMAKE_TARGET} to compile ${TARGET}. "
513
+ "This might result in performance degradation from missing arch flags (eg. avx).")
514
+ set(ARG_TARGETS cmake)
515
+ endif ()
516
+ endif ()
517
+
518
+ _Halide_validate_multitarget(common_triple ${ARG_TARGETS})
519
+
520
+ _Halide_is_crosscompiling(is_crosscompiling "${common_triple}")
521
+ _Halide_get_platform_extensions(
522
+ object_extension
523
+ static_library_extension
524
+ "${common_triple}"
525
+ )
526
+
527
+ ##
528
+ # Set up the runtime library, if needed
529
+ ##
530
+
531
+ if (ARG_C_BACKEND)
532
+ # The C backend does not provide a runtime, so just supply headers.
533
+ if (ARG_USE_RUNTIME)
534
+ message(AUTHOR_WARNING "The C backend does not use a runtime.")
535
+ endif ()
536
+ set(ARG_USE_RUNTIME Halide::Runtime)
537
+ elseif (NOT ARG_USE_RUNTIME)
538
+ # If we're not using an existing runtime, create one.
539
+ set(runtime_args "")
540
+ if (DEFINED ARG_NO_THREADS)
541
+ list(APPEND runtime_args NO_THREADS "${ARG_NO_THREADS}")
542
+ endif ()
543
+ if (DEFINED ARG_NO_DL_LIBS)
544
+ list(APPEND runtime_args NO_DL_LIBS "${ARG_NO_DL_LIBS}")
545
+ endif ()
546
+
547
+ add_halide_runtime(
548
+ "${TARGET}.runtime"
549
+ NO_DEFAULT_TARGETS TARGETS ${ARG_TARGETS}
550
+ ${runtime_args}
551
+ )
552
+
553
+ set(ARG_USE_RUNTIME "${TARGET}.runtime")
554
+ elseif (NOT TARGET ${ARG_USE_RUNTIME})
555
+ message(FATAL_ERROR "Invalid runtime target ${ARG_USE_RUNTIME}")
556
+ else ()
557
+ _Halide_add_targets_to_runtime(${ARG_USE_RUNTIME} TARGETS ${ARG_TARGETS})
558
+ endif ()
559
+
560
+ ##
561
+ # Determine which outputs the generator call will emit.
562
+ ##
563
+
564
+ if (ARG_C_BACKEND)
565
+ set(library_type c_source)
566
+ elseif (is_crosscompiling)
567
+ set(library_type static_library)
568
+ else ()
569
+ set(library_type object)
570
+ endif ()
571
+
572
+ # Add in extra outputs using the table defined at the start of this function
573
+ set(extra_outputs "")
574
+ foreach (out IN LISTS extra_output_names)
575
+ if (ARG_${out})
576
+ string(TOLOWER "${out}" out)
577
+ list(APPEND extra_outputs ${out})
578
+ endif ()
579
+ endforeach ()
580
+
581
+ ##
582
+ # Attach an autoscheduler if the user requested it
583
+ ##
584
+
585
+ if (ARG_AUTOSCHEDULER)
586
+ if ("${ARG_AUTOSCHEDULER}" MATCHES "::")
587
+ if (NOT TARGET "${ARG_AUTOSCHEDULER}")
588
+ message(FATAL_ERROR "Autoscheduler ${ARG_AUTOSCHEDULER} does not exist.")
589
+ endif ()
590
+
591
+ # Convention: if the argument names a target like "Namespace::Scheduler" then
592
+ # it is assumed to be a MODULE target providing a scheduler named "Scheduler".
593
+ list(APPEND ARG_PLUGINS "${ARG_AUTOSCHEDULER}")
594
+ string(REGEX REPLACE ".*::(.*)" "\\1" ARG_AUTOSCHEDULER "${ARG_AUTOSCHEDULER}")
595
+ elseif (NOT ARG_PLUGINS)
596
+ message(AUTHOR_WARNING "AUTOSCHEDULER set to a scheduler name but no plugins were loaded")
597
+ endif ()
598
+ list(PREPEND ARG_PARAMS "autoscheduler=${ARG_AUTOSCHEDULER}")
599
+ endif ()
600
+
601
+ ##
602
+ # Main library target for filter.
603
+ ##
604
+
605
+ _Halide_compute_generator_cmd(
606
+ FROM "${ARG_FROM}"
607
+ OUT_COMMAND generator_cmd
608
+ OUT_DEPENDS generator_cmd_deps
609
+ )
610
+
611
+ set(generator_args
612
+ COMMAND ${generator_cmd}
613
+ DEPENDS ${generator_cmd_deps}
614
+ EXTRA_OUTPUTS ${extra_outputs}
615
+ FUNCTION_NAME "${ARG_FUNCTION_NAME}"
616
+ GENERATOR "${ARG_GENERATOR}"
617
+ GRADIENT_DESCENT "${ARG_GRADIENT_DESCENT}"
618
+ PARAMS ${ARG_PARAMS}
619
+ PLUGINS ${ARG_PLUGINS}
620
+ TYPE "${library_type}"
621
+ USE_RUNTIME "${ARG_USE_RUNTIME}"
622
+ )
623
+
624
+ list(JOIN ARG_FEATURES "-" ARG_FEATURES)
625
+
626
+ # Clear output lists
627
+ foreach (output IN LISTS extra_outputs)
628
+ set(OUT_${output} "")
629
+ endforeach ()
630
+
631
+ list(LENGTH Halide_CMAKE_TARGET num_platforms)
632
+ if (common_triple STREQUAL "cmake" AND num_platforms GREATER 1)
633
+ set(merged_base "")
634
+ set(merged_libs "")
635
+
636
+ foreach (triple IN LISTS Halide_CMAKE_TARGET)
637
+ set(features_arch "ARG_FEATURES[${triple}]")
638
+ set(features_arch "${${features_arch}}")
639
+ if (features_arch)
640
+ list(TRANSFORM features_arch PREPEND "${triple}-"
641
+ OUTPUT_VARIABLE targets_arch)
642
+ else ()
643
+ set(targets_arch "${triple}")
644
+ endif ()
645
+
646
+ list(TRANSFORM targets_arch APPEND "-${ARG_FEATURES}")
647
+ list(TRANSFORM targets_arch REPLACE "-$" "")
648
+
649
+ if (NOT merged_base AND NOT ARG_C_BACKEND)
650
+ set(this_lib "${TARGET}")
651
+ set(merged_base "${this_lib}")
652
+ else ()
653
+ set(this_lib "${TARGET}-${triple}")
654
+ list(APPEND merged_libs "${this_lib}")
655
+ endif ()
656
+
657
+ # Appends to OUT_c_header, OUT_<extra-output>, etc.
658
+ _Halide_library_from_generator(
659
+ "${this_lib}" ${generator_args} TARGETS ${targets_arch})
660
+ _Halide_set_osx_arch("${this_lib}" "${triple}")
661
+ endforeach ()
662
+
663
+ if (ARG_C_BACKEND)
664
+ add_library("${TARGET}" STATIC)
665
+ _Halide_lipo(TARGET "${TARGET}" INPUTS ${merged_libs})
666
+ else ()
667
+ _Halide_lipo(TARGET "${merged_base}" INPUTS ${merged_libs} OVERWRITE)
668
+ endif ()
669
+ else ()
670
+ list(TRANSFORM ARG_TARGETS REPLACE "cmake" "${Halide_CMAKE_TARGET}")
671
+ if (ARG_FEATURES)
672
+ list(TRANSFORM ARG_TARGETS APPEND "-${ARG_FEATURES}")
673
+ endif ()
674
+
675
+ # Appends to OUT_c_header, OUT_<extra-output>, etc.
676
+ _Halide_library_from_generator(
677
+ "${TARGET}" ${generator_args} TARGETS ${ARG_TARGETS})
678
+ endif ()
679
+
680
+ # Save some info for add_halide_python_extension_library() in case it is used for this target.
681
+ set_property(TARGET "${TARGET}" PROPERTY Halide_LIBRARY_RUNTIME_TARGET "${ARG_USE_RUNTIME}")
682
+ set_property(TARGET "${TARGET}" PROPERTY Halide_LIBRARY_FUNCTION_NAME "${ARG_FUNCTION_NAME}")
683
+ if ("python_extension" IN_LIST extra_outputs)
684
+ list(GET OUT_python_extension 0 py_ext_cpp) # These files should always be identical
685
+ set_property(TARGET "${TARGET}" PROPERTY Halide_LIBRARY_PYTHON_EXTENSION_CPP "${py_ext_cpp}")
686
+ endif ()
687
+
688
+ # Propagate outputs
689
+ if (ARG_HEADER)
690
+ set(${ARG_HEADER} "${OUT_c_header}")
691
+ endif ()
692
+
693
+ foreach (output IN LISTS extra_outputs)
694
+ string(TOUPPER "ARG_${output}" outvar_arg)
695
+ if (${outvar_arg})
696
+ set("${${outvar_arg}}" ${OUT_${output}} PARENT_SCOPE)
697
+ endif ()
698
+ endforeach ()
699
+ endfunction()
700
+
701
+ function(_Halide_validate_multitarget OUT_TRIPLE)
702
+ list(LENGTH ARGN len)
703
+ if (len LESS 1)
704
+ message(FATAL_ERROR "Must supply at least one target")
705
+ endif ()
706
+
707
+ set(triple "")
708
+ set(all_features "")
709
+ foreach (target IN LISTS ARGN)
710
+ if (target MATCHES "^(host|cmake|[^-]+-[^-]+-[^-]+)(-[^-]+)*$")
711
+ set(this_triple "${CMAKE_MATCH_1}")
712
+ list(APPEND all_features ${CMAKE_MATCH_2})
713
+ if (NOT triple)
714
+ set(triple "${this_triple}")
715
+ elseif (NOT this_triple STREQUAL triple)
716
+ message(FATAL_ERROR "Multi-target entry `${target}` does not match earlier triple `${triple}`")
717
+ endif ()
718
+ else ()
719
+ message(FATAL_ERROR "TARGET `${target}` is malformed")
720
+ endif ()
721
+ endforeach ()
722
+
723
+ list(LENGTH Halide_CMAKE_TARGET num_platforms)
724
+ if (num_platforms GREATER 1)
725
+ if (NOT all_features STREQUAL "")
726
+ message(
727
+ FATAL_ERROR
728
+ "Multiarch builds cannot include features in the target list. "
729
+ "Use FEATURES[arch] instead. "
730
+ "Halide_CMAKE_TARGET=${Halide_CMAKE_TARGET} and saw TARGETS ${ARGN}."
731
+ )
732
+ endif ()
733
+ if (triple STREQUAL "host" AND "${Halide_HOST_TARGET}" IN_LIST Halide_CMAKE_TARGET)
734
+ set(triple "cmake")
735
+ endif ()
736
+ endif ()
737
+
738
+ set(${OUT_TRIPLE} "${triple}" PARENT_SCOPE)
739
+ endfunction()
740
+
741
+ function(add_halide_python_extension_library TARGET)
742
+ set(options "")
743
+ set(oneValueArgs MODULE_NAME)
744
+ set(multiValueArgs HALIDE_LIBRARIES)
745
+ cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
746
+
747
+ if (NOT ARG_MODULE_NAME)
748
+ set(ARG_MODULE_NAME "${TARGET}")
749
+ endif ()
750
+
751
+ if (NOT ARG_HALIDE_LIBRARIES)
752
+ message(FATAL_ERROR "HALIDE_LIBRARIES must be specified")
753
+ endif ()
754
+
755
+ set(runtimes "")
756
+ set(pycpps "")
757
+ set(function_names "") # space-separated X-macros
758
+ foreach (lib IN LISTS ARG_HALIDE_LIBRARIES)
759
+ if (NOT TARGET "${lib}")
760
+ message(FATAL_ERROR "${lib} is not a valid target")
761
+ endif ()
762
+
763
+ get_property(runtime_used TARGET ${lib} PROPERTY Halide_LIBRARY_RUNTIME_TARGET)
764
+ if (NOT runtime_used)
765
+ message(FATAL_ERROR "${lib} does not appear to have a Halide Runtime specified")
766
+ endif ()
767
+ list(APPEND runtimes ${runtime_used})
768
+
769
+ get_property(function_name TARGET ${lib} PROPERTY Halide_LIBRARY_FUNCTION_NAME)
770
+ if (NOT function_name)
771
+ message(FATAL_ERROR "${lib} does not appear to have a Function name specified")
772
+ endif ()
773
+ # Strip C++ namespace(s), if any
774
+ string(REGEX REPLACE ".*::(.*)" "\\1" function_name "${function_name}")
775
+ string(APPEND function_names " X(${function_name})")
776
+
777
+ get_property(pycpp TARGET ${lib} PROPERTY Halide_LIBRARY_PYTHON_EXTENSION_CPP)
778
+ if (NOT pycpp)
779
+ message(FATAL_ERROR "${lib} must be built with PYTHON_EXTENSION specified in order to use it with add_halide_python_extension_library()")
780
+ endif ()
781
+ list(APPEND pycpps ${pycpp})
782
+ endforeach ()
783
+
784
+ list(REMOVE_DUPLICATES runtimes)
785
+ list(LENGTH runtimes len)
786
+ if (NOT len EQUAL 1)
787
+ message(FATAL_ERROR "${TARGET} requires all libraries to use the same Halide Runtime, but saw ${len}: ${runtimes}")
788
+ endif ()
789
+
790
+ set(pyext_runtime_name ${TARGET}_module_definition)
791
+ set(pyext_module_definition_src "${CMAKE_CURRENT_BINARY_DIR}/${pyext_runtime_name}.py.cpp")
792
+
793
+ add_custom_command(OUTPUT ${pyext_module_definition_src}
794
+ COMMAND Halide::GenRT -r "${pyext_runtime_name}" -e python_extension -o "${CMAKE_CURRENT_BINARY_DIR}" target=host
795
+ DEPENDS Halide::GenRT
796
+ VERBATIM)
797
+
798
+ Python_add_library(${TARGET} MODULE WITH_SOABI ${pycpps} ${pyext_module_definition_src})
799
+ target_link_libraries(${TARGET} PRIVATE ${ARG_HALIDE_LIBRARIES})
800
+ target_compile_definitions(${TARGET} PRIVATE
801
+ # Skip the default module-definition code in each file
802
+ HALIDE_PYTHON_EXTENSION_OMIT_MODULE_DEFINITION
803
+ # Gotta explicitly specify the module name and function(s) for this mode
804
+ HALIDE_PYTHON_EXTENSION_MODULE_NAME=${ARG_MODULE_NAME}
805
+ "HALIDE_PYTHON_EXTENSION_FUNCTIONS=${function_names}")
806
+ target_compile_features(${TARGET} PRIVATE cxx_std_17)
807
+ set_target_properties(${TARGET} PROPERTIES OUTPUT_NAME ${ARG_MODULE_NAME})
808
+ _Halide_target_export_single_symbol(${TARGET} "PyInit_${ARG_MODULE_NAME}")
809
+ endfunction()
810
+
811
+ ##
812
+ # Function for ensuring that Halide.dll is visible to the generator
813
+ ##
814
+
815
+ function(_Halide_place_dll GEN)
816
+ if (NOT WIN32)
817
+ return()
818
+ endif ()
819
+
820
+ # Short circuit so that Halide::Halide isn't checked when importing a generator from another CMake project
821
+ get_property(is_imported TARGET ${GEN} PROPERTY IMPORTED)
822
+ if (is_imported)
823
+ return()
824
+ endif ()
825
+
826
+ get_property(has_post_build TARGET ${GEN} PROPERTY Halide_GENERATOR_HAS_POST_BUILD)
827
+ if (has_post_build)
828
+ return()
829
+ endif ()
830
+
831
+ # Here GEN is not IMPORTED, which means that it must be linked
832
+ # to Halide::Halide and therefore Halide::Halide must exist.
833
+ get_property(halide_type TARGET Halide::Halide PROPERTY TYPE)
834
+ if (NOT halide_type STREQUAL "SHARED_LIBRARY")
835
+ return()
836
+ endif ()
837
+
838
+ add_custom_command(
839
+ TARGET ${GEN} POST_BUILD
840
+ COMMAND powershell -NoProfile -ExecutionPolicy Bypass
841
+ -File "${CMAKE_CURRENT_FUNCTION_LIST_DIR}/MutexCopy.ps1"
842
+ -src "$<TARGET_FILE:Halide::Halide>"
843
+ -dstDir "$<TARGET_FILE_DIR:${GEN}>"
844
+ VERBATIM
845
+ )
846
+ set_property(TARGET ${GEN} PROPERTY Halide_GENERATOR_HAS_POST_BUILD 1)
847
+ endfunction()
848
+
849
+ ##
850
+ # Function for creating a standalone runtime from a generator.
851
+ ##
852
+
853
+ function(add_halide_runtime RT)
854
+ set(options NO_DEFAULT_TARGETS)
855
+ set(oneValueArgs NO_THREADS NO_DL_LIBS)
856
+ set(multiValueArgs TARGETS)
857
+ cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
858
+
859
+ # If no TARGETS argument, use Halide_TARGET instead
860
+ if (NOT ARG_TARGETS)
861
+ set(ARG_TARGETS "${Halide_TARGET}")
862
+ endif ()
863
+
864
+ # The default of NO_THREADS/NO_DL_LIBS is OFF unless Halide_RUNTIME_NO_THREADS/NO_DL_LIBS is defined globally
865
+ if (NOT DEFINED ARG_NO_THREADS)
866
+ set(ARG_NO_THREADS ${Halide_RUNTIME_NO_THREADS})
867
+ endif ()
868
+ if (NOT DEFINED ARG_NO_DL_LIBS)
869
+ set(ARG_NO_DL_LIBS ${Halide_RUNTIME_NO_DL_LIBS})
870
+ endif ()
871
+
872
+ _Halide_validate_multitarget(common_triple ${ARG_TARGETS})
873
+
874
+ _Halide_is_crosscompiling(is_crosscompiling "${common_triple}")
875
+ _Halide_get_platform_extensions(
876
+ object_extension
877
+ static_library_extension
878
+ "${common_triple}")
879
+
880
+ # We defer reading the list of targets for which to generate a common
881
+ # runtime to CMake _generation_ time. This prevents issues where a lower
882
+ # GCD is required by a later Halide library linking to this runtime.
883
+ set(target_list "$<TARGET_GENEX_EVAL:${RT},$<TARGET_PROPERTY:${RT},Halide_RT_TARGETS>>")
884
+
885
+ # Remove features that should not be attached to a runtime
886
+ # TODO: The fact that removing profile fixes a duplicate symbol linker error on Windows smells like a bug.
887
+ set(target_list "$<LIST:TRANSFORM,${target_list},REPLACE,-(user_context|no_asserts|no_bounds_query|no_runtime|profile),>")
888
+
889
+ if (is_crosscompiling)
890
+ set(GEN_OUTS "${RT}${static_library_extension}")
891
+ add_custom_command(
892
+ OUTPUT "${GEN_OUTS}"
893
+ COMMAND Halide::GenRT -r "${RT}" -o .
894
+ "target=$<JOIN:$<REMOVE_DUPLICATES:${target_list}>,$<COMMA>>"
895
+ DEPENDS Halide::GenRT
896
+ VERBATIM)
897
+ add_custom_target("${RT}.update" DEPENDS "${GEN_OUTS}")
898
+
899
+ add_library("${RT}" STATIC IMPORTED GLOBAL)
900
+ add_dependencies("${RT}" "${RT}.update")
901
+
902
+ set_target_properties("${RT}" PROPERTIES
903
+ IMPORTED_LOCATION ${CMAKE_CURRENT_BINARY_DIR}/${GEN_OUTS})
904
+ else ()
905
+ list(LENGTH Halide_CMAKE_TARGET num_platforms)
906
+ if (common_triple STREQUAL "cmake" AND num_platforms GREATER 1)
907
+ set(base_rt "")
908
+ set(arch_rt "")
909
+ foreach (triple IN LISTS Halide_CMAKE_TARGET)
910
+ set(arch_target_list "$<LIST:TRANSFORM,${target_list},REPLACE,cmake,${triple}>")
911
+ set(arch_target_list "$<FILTER:${arch_target_list},INCLUDE,^${triple}>")
912
+
913
+ if (NOT base_rt)
914
+ set(this_rt "${RT}")
915
+ set(base_rt "${this_rt}")
916
+ else ()
917
+ set(this_rt "${RT}-${triple}")
918
+ list(APPEND arch_rt "${this_rt}")
919
+ endif ()
920
+
921
+ add_custom_command(
922
+ OUTPUT "${this_rt}${object_extension}"
923
+ COMMAND Halide::GenRT -r "${this_rt}" -o . -e object
924
+ "target=$<JOIN:$<REMOVE_DUPLICATES:${arch_target_list}>,$<COMMA>>"
925
+ DEPENDS Halide::GenRT
926
+ VERBATIM)
927
+
928
+ add_library("${this_rt}" STATIC "${this_rt}${object_extension}")
929
+ set_target_properties("${this_rt}" PROPERTIES LINKER_LANGUAGE CXX)
930
+ _Halide_fix_xcode("${this_rt}")
931
+ _Halide_set_osx_arch("${this_rt}" "${triple}")
932
+ endforeach ()
933
+
934
+ _Halide_lipo(TARGET "${base_rt}" INPUTS ${arch_rt} OVERWRITE)
935
+ else ()
936
+ set(target_list "$<LIST:TRANSFORM,${target_list},REPLACE,cmake,${Halide_CMAKE_TARGET}>")
937
+ add_custom_command(
938
+ OUTPUT "${RT}${object_extension}"
939
+ COMMAND Halide::GenRT -r "${RT}" -o . -e object
940
+ "target=$<JOIN:$<REMOVE_DUPLICATES:${target_list}>,$<COMMA>>"
941
+ DEPENDS Halide::GenRT
942
+ VERBATIM)
943
+ add_library("${RT}" STATIC "${RT}${object_extension}")
944
+ set_target_properties("${RT}" PROPERTIES LINKER_LANGUAGE CXX)
945
+ _Halide_fix_xcode("${RT}")
946
+ endif ()
947
+ endif ()
948
+
949
+ # Take care of the runtime/toolchain which doesn't have Threads or DL libs
950
+ target_link_libraries("${RT}" INTERFACE Halide::Runtime)
951
+ if (NOT ARG_NO_THREADS)
952
+ find_package(Threads REQUIRED)
953
+ target_link_libraries("${RT}" INTERFACE Threads::Threads)
954
+ endif ()
955
+ if (NOT ARG_NO_DL_LIBS)
956
+ target_link_libraries("${RT}" INTERFACE ${CMAKE_DL_LIBS})
957
+ endif ()
958
+
959
+ if (NOT ARG_NO_DEFAULT_TARGETS)
960
+ _Halide_add_targets_to_runtime("${RT}" TARGETS ${ARG_TARGETS})
961
+ endif ()
962
+ endfunction()
963
+
964
+ function(_Halide_is_crosscompiling OUT_XC triple)
965
+ if (triple MATCHES "host")
966
+ set(triple "${Halide_HOST_TARGET}")
967
+ endif ()
968
+
969
+ if (triple MATCHES "cmake" OR triple IN_LIST Halide_CMAKE_TARGET)
970
+ set("${OUT_XC}" 0 PARENT_SCOPE)
971
+ else ()
972
+ set("${OUT_XC}" 1 PARENT_SCOPE)
973
+ endif ()
974
+ endfunction()
975
+
976
+ function(_Halide_get_platform_extensions OUT_OBJ OUT_STATIC triple)
977
+ if (triple MATCHES "host")
978
+ set(triple "${Halide_HOST_TARGET}")
979
+ endif ()
980
+
981
+ if (triple MATCHES "cmake")
982
+ set(triple "${Halide_CMAKE_TARGET}")
983
+ endif ()
984
+
985
+ if (triple MATCHES "windows")
986
+ set("${OUT_OBJ}" ".obj" PARENT_SCOPE)
987
+ set("${OUT_STATIC}" ".lib" PARENT_SCOPE)
988
+ else ()
989
+ # All other OSes use .a
990
+ set("${OUT_OBJ}" ".o" PARENT_SCOPE)
991
+ set("${OUT_STATIC}" ".a" PARENT_SCOPE)
992
+ endif ()
993
+ endfunction()
994
+
995
+ ##
996
+ # Utility for finding GPU libraries that are needed by
997
+ # the runtime when listed in the Halide target string.
998
+ ##
999
+
1000
+ function(_Halide_add_targets_to_runtime TARGET)
1001
+ cmake_parse_arguments(PARSE_ARGV 1 ARG "" "" "TARGETS")
1002
+
1003
+ if (NOT TARGET "${TARGET}")
1004
+ message(FATAL_ERROR "not a target: ${TARGET}")
1005
+ endif ()
1006
+
1007
+ get_property(aliased TARGET "${TARGET}" PROPERTY ALIASED_TARGET)
1008
+ if (aliased)
1009
+ set(TARGET "${aliased}")
1010
+ endif ()
1011
+
1012
+ set_property(TARGET "${TARGET}" APPEND PROPERTY Halide_RT_TARGETS "${ARG_TARGETS}")
1013
+ _Halide_target_link_gpu_libs(${TARGET} INTERFACE ${ARG_TARGETS})
1014
+ endfunction()
1015
+
1016
+ function(_Halide_target_link_gpu_libs TARGET VISIBILITY)
1017
+ if ("${ARGN}" MATCHES "metal")
1018
+ find_library(FOUNDATION_LIBRARY Foundation REQUIRED)
1019
+ find_library(METAL_LIBRARY Metal REQUIRED)
1020
+ target_link_libraries(${TARGET} ${VISIBILITY} "${FOUNDATION_LIBRARY}" "${METAL_LIBRARY}")
1021
+ endif ()
1022
+
1023
+ if ("${ARGN}" MATCHES "webgpu" AND NOT "${ARGN}" MATCHES "wasm")
1024
+ find_package(Halide_WebGPU REQUIRED)
1025
+ target_link_libraries(${TARGET} ${VISIBILITY} Halide::WebGPU)
1026
+ endif ()
1027
+ endfunction()
1028
+
1029
+ ##
1030
+ # Function for working around Xcode backend bugs
1031
+ ##
1032
+
1033
+ function(_Halide_fix_xcode TARGET)
1034
+ if (CMAKE_GENERATOR STREQUAL "Xcode")
1035
+ # Xcode generator requires at least one source file to work correctly.
1036
+ set(empty_file "${CMAKE_CURRENT_BINARY_DIR}/Halide_${TARGET}_empty.cpp")
1037
+ file(CONFIGURE OUTPUT "${empty_file}" CONTENT "")
1038
+ target_sources("${TARGET}" PRIVATE "${empty_file}")
1039
+ endif ()
1040
+ endfunction()
1041
+
1042
+ function(_Halide_target_export_single_symbol TARGET SYMBOL)
1043
+ file(CONFIGURE
1044
+ OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${TARGET}.${SYMBOL}.ldscript.apple"
1045
+ CONTENT "_${SYMBOL}\n")
1046
+
1047
+ file(CONFIGURE
1048
+ OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${TARGET}.${SYMBOL}.ldscript"
1049
+ CONTENT "{ global: ${SYMBOL}; local: *; };\n")
1050
+
1051
+ target_export_script(
1052
+ ${TARGET}
1053
+ APPLE_LD "${CMAKE_CURRENT_BINARY_DIR}/${TARGET}.${SYMBOL}.ldscript.apple"
1054
+ GNU_LD "${CMAKE_CURRENT_BINARY_DIR}/${TARGET}.${SYMBOL}.ldscript"
1055
+ )
1056
+ endfunction()