File: HalideGeneratorHelpers.cmake

package info (click to toggle)
halide 21.0.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 55,752 kB
  • sloc: cpp: 289,334; ansic: 22,751; python: 7,486; makefile: 4,299; sh: 2,508; java: 1,549; javascript: 282; pascal: 207; xml: 127; asm: 9
file content (1078 lines) | stat: -rw-r--r-- 42,104 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
cmake_minimum_required(VERSION 3.28)

option(Halide_NO_DEFAULT_FLAGS "When enabled, suppresses recommended flags in add_halide_generator" OFF)

include(${CMAKE_CURRENT_LIST_DIR}/HalideTargetHelpers.cmake)
include(${CMAKE_CURRENT_LIST_DIR}/TargetExportScript.cmake)

define_property(TARGET PROPERTY Halide_RT_TARGETS
                BRIEF_DOCS "On a Halide runtime target, lists the targets the runtime backs"
                FULL_DOCS "On a Halide runtime target, lists the targets the runtime backs")

define_property(TARGET PROPERTY Halide_GENERATOR_HAS_POST_BUILD
                BRIEF_DOCS "On a Halide generator target, true if Halide.dll copy command has already been added."
                FULL_DOCS "On a Halide generator target, true if Halide.dll copy command has already been added.")

define_property(TARGET PROPERTY Halide_PYTHON_GENERATOR_SOURCE
                BRIEF_DOCS "Used to store the source file(s) for a Python Generator"
                FULL_DOCS "Used to store the source file(s) for a Python Generator")

define_property(TARGET PROPERTY Halide_LIBRARY_RUNTIME_TARGET
                BRIEF_DOCS "On a Halide library target, the runtime it uses."
                FULL_DOCS "On a Halide library target, the runtime it uses.")

define_property(TARGET PROPERTY Halide_LIBRARY_PYTHON_EXTENSION_CPP
                BRIEF_DOCS "On a Halide library target, the .py.cpp generated for it (absent if none)."
                FULL_DOCS "On a Halide library target, the .py.cpp generated for it (absent if none).")

define_property(TARGET PROPERTY Halide_LIBRARY_FUNCTION_NAME
                BRIEF_DOCS "On a Halide library target, the FUNCTION_NAME used."
                FULL_DOCS "On a Halide library target, the FUNCTION_NAME used.")

##
# Function to simplify writing the CMake rules for creating a generator executable
# that follows our recommended cross-compiling workflow.
##

function(add_halide_generator TARGET)
    set(options "")
    set(oneValueArgs PACKAGE_NAME PACKAGE_NAMESPACE EXPORT_FILE PYSTUB)
    set(multiValueArgs SOURCES LINK_LIBRARIES)
    cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

    if (NOT ARG_PACKAGE_NAME)
        set(ARG_PACKAGE_NAME "${PROJECT_NAME}-halide_generators")
    endif ()

    if (NOT ARG_PACKAGE_NAMESPACE)
        set(ARG_PACKAGE_NAMESPACE "${PROJECT_NAME}::halide_generators::")
    endif ()

    if (NOT ARG_EXPORT_FILE)
        file(MAKE_DIRECTORY "${PROJECT_BINARY_DIR}/cmake")
        set(ARG_EXPORT_FILE "${PROJECT_BINARY_DIR}/cmake/${ARG_PACKAGE_NAME}Config.cmake")
    endif ()

    if (NOT ARG_SOURCES)
        set(ARG_SOURCES "${ARG_UNPARSED_ARGUMENTS}")
    endif ()

    _Halide_try_load_generators("${ARG_PACKAGE_NAME}")

    # Communicate found information to the caller
    set(${ARG_PACKAGE_NAME}_FOUND "${${ARG_PACKAGE_NAME}_FOUND}" PARENT_SCOPE)

    set(gen "${ARG_PACKAGE_NAMESPACE}${TARGET}")
    if (NOT TARGET "${gen}")
        if (NOT TARGET "${ARG_PACKAGE_NAME}")
            add_custom_target("${ARG_PACKAGE_NAME}")
        endif ()

        if (ARG_SOURCES MATCHES ".py$")
            if (ARG_LINK_LIBRARIES)
                message(FATAL_ERROR "You cannot specify LINK_LIBRARIES in conjunction with Python source code.")
            endif ()
            if (ARG_PYSTUB)
                message(FATAL_ERROR "You cannot specify PYSTUB in conjunction with Python source code (only C++ Generators can produce PyStubs).")
            endif ()

            list(LENGTH ARG_SOURCES len)
            if (NOT len EQUAL 1)
                message(FATAL_ERROR "Python Generators must specify exactly one source file.")
            endif ()

            # Make a fake target here that we can attach the Python source to,
            # so that we can extract 'em in add_halide_library()
            add_custom_target(${TARGET} ALL)
            set_property(TARGET ${TARGET} PROPERTY Halide_PYTHON_GENERATOR_SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/${ARG_SOURCES}")

            # TODO: what do we need to do for PACKAGE_NAME PACKAGE_NAMESPACE EXPORT_FILE in this case?
        else ()
            add_executable(${TARGET} ${ARG_SOURCES})
            add_executable(${gen} ALIAS ${TARGET})

            if (NOT TARGET Halide::Generator)
                find_package(Halide REQUIRED)
            endif ()
            target_link_libraries(${TARGET} PRIVATE Halide::Generator ${ARG_LINK_LIBRARIES})

            _Halide_place_dll(${TARGET})

            if (NOT ARG_NO_DEFAULT_FLAGS AND NOT Halide_NO_DEFAULT_FLAGS)
                # For crosscompiling builds, the Halide headers will be included using -isystem,
                # which will cause all warnings to be ignored. This is not helpful, since
                # we *want* deprecation warnings to be propagated. So we must set
                # NO_SYSTEM_FROM_IMPORTED in order for it to be seen.
                set_target_properties(${TARGET} PROPERTIES NO_SYSTEM_FROM_IMPORTED YES)
                target_compile_options(
                    ${TARGET} PRIVATE
                    $<$<CXX_COMPILER_ID:GNU,Clang,AppleClang>:-Wdeprecated-declarations>
                    $<$<CXX_COMPILER_ID:MSVC>:/w14996>  # 4996: compiler encountered deprecated declaration
                )
            endif ()

            add_dependencies("${ARG_PACKAGE_NAME}" ${TARGET})
            export(TARGETS ${TARGET}
                   NAMESPACE ${ARG_PACKAGE_NAMESPACE}
                   APPEND FILE "${ARG_EXPORT_FILE}")
        endif ()
    endif ()

    if (ARG_PYSTUB)
        set(GEN_NAME ${ARG_PYSTUB})
        set(MODULE_NAME ${ARG_PYSTUB}_pystub)
        # Generate a small C++ file that includes the boilerplate code needed to
        # register a PyInit function that has the stub glue code.
        string(CONCAT stub_text
               "#include <Python.h>\n"
               "#include \"Halide.h\"\n"
               "HALIDE_GENERATOR_PYSTUB(${GEN_NAME}, ${MODULE_NAME})\n")

        set(stub_file "${CMAKE_CURRENT_BINARY_DIR}/${TARGET}.${GEN_NAME}.${MODULE_NAME}.py_stub_generated.cpp")
        file(CONFIGURE OUTPUT "${stub_file}" CONTENT "${stub_text}" @ONLY)

        Python_add_library(${TARGET}_pystub MODULE WITH_SOABI "${stub_file}" ${ARG_SOURCES})
        set_target_properties(${TARGET}_pystub PROPERTIES
                              CXX_VISIBILITY_PRESET hidden
                              VISIBILITY_INLINES_HIDDEN ON
                              POSITION_INDEPENDENT_CODE ON)
        target_link_libraries(${TARGET}_pystub PRIVATE Halide::PyStubs Halide::Halide ${ARG_LINK_LIBRARIES})
        set_target_properties(${TARGET}_pystub PROPERTIES OUTPUT_NAME ${MODULE_NAME})
        _Halide_target_export_single_symbol(${TARGET}_pystub "PyInit_${MODULE_NAME}")
    endif ()
endfunction()

function(_Halide_try_load_generators package_name)
    # Don't repeatedly run the search for the tools package.
    if (NOT DEFINED ${package_name}_FOUND)
        # Some toolchains, like Emscripten, try to disable finding packages
        # outside their sysroots, but we always want to find the native
        # generators. Setting CMAKE_FIND_ROOT_PATH_BOTH here overrides
        # the toolchain search preference. This is okay since a user can
        # always override this call by setting ${package_name}_ROOT.
        find_package(${package_name} QUIET CMAKE_FIND_ROOT_PATH_BOTH)

        # Communicate found information to the caller
        set(${package_name}_FOUND "${${package_name}_FOUND}" PARENT_SCOPE)

        if (NOT ${package_name}_FOUND AND CMAKE_CROSSCOMPILING AND NOT CMAKE_CROSSCOMPILING_EMULATOR)
            message(WARNING
                    "'${package_name}' was not found and it looks like you "
                    "are cross-compiling without an emulator. This is likely to "
                    "fail. Please set -D${package_name}_ROOT=... at the CMake "
                    "command line to the build directory of a host-built ${PROJECT_NAME}.")
        endif ()
    endif ()
endfunction()

function(_Halide_library_from_generator TARGET)
    cmake_parse_arguments(
        PARSE_ARGV 1 ARG ""
        "FUNCTION_NAME;GENERATOR;GRADIENT_DESCENT;TYPE;USE_RUNTIME"
        "COMMAND;DEPENDS;EXTRA_OUTPUTS;PARAMS;PLUGINS;TARGETS"
    )

    ## "hash table" of extra outputs to extensions
    # Keep in sync with Module.cpp
    set(assembly_extension ".s")
    set(bitcode_extension ".bc")
    # set(c_header_extension ".h")  # handled specially
    set(c_source_extension ".halide_generated.cpp")
    set(compiler_log_extension ".halide_compiler_log")
    set(conceptual_stmt_extension ".conceptual.stmt")
    set(conceptual_stmt_html_extension ".conceptual.stmt.html")
    # set(cpp_stub_extension ".stub.h")  # not implemented
    set(device_code_extension ".device_code")
    set(featurization_extension ".featurization")
    set(function_info_header_extension ".function_info.h")
    set(hlpipe_extension ".hlpipe")
    set(llvm_assembly_extension ".ll")
    # set(object_extension (is_windows_coff ? ".obj" : ".o"))  # handled specially
    set(python_extension_extension ".py.cpp")
    set(pytorch_wrapper_extension ".pytorch.h")
    set(registration_extension ".registration.cpp")
    set(schedule_extension ".schedule.h")
    # set(static_library_extension (is_windows_coff ? ".lib" : ".a"))  # handled specially
    set(stmt_extension ".stmt")
    set(stmt_html_extension ".stmt.html")

    ## Validate TYPE
    if (NOT ARG_TYPE MATCHES "^(c_source|static_library|object)$")
        message(FATAL_ERROR "`${ARG_TYPE}` not among: c_source, object, static_library")
    endif ()

    ## Validate plugins
    foreach (plugin IN LISTS ARG_PLUGINS)
        if (NOT TARGET "${plugin}")
            message(FATAL_ERROR "Plugin `${plugin}` is not a target.")
        endif ()
    endforeach ()

    ## Always omit the runtime
    list(TRANSFORM ARG_TARGETS APPEND "-no_runtime")

    ## Resolve plugins
    if (ARG_PLUGINS)
        list(TRANSFORM ARG_PLUGINS REPLACE "(.+)" "$<TARGET_FILE:\\1>" OUTPUT_VARIABLE plugins_args)
        list(JOIN plugins_args "," plugins_args)
        list(PREPEND plugins_args -p)
    else ()
        set(plugins_args "")
    endif ()

    ## Gather platform information
    _Halide_get_platform_extensions(
        object_extension
        static_library_extension
        "${ARG_TARGETS}"
    )

    macro(_Halide_add_output type base_name)
        list(APPEND outputs "${type}")
        list(APPEND output_files "${base_name}${${type}_extension}")
    endmacro()

    ## Check the type to determine outputs
    set(outputs c_header)
    set(output_files "${TARGET}.h")

    list(LENGTH ARG_TARGETS num_targets)
    if (ARG_TYPE STREQUAL "object" AND num_targets GREATER 1)
        foreach (t IN LISTS ARG_TARGETS)
            _Halide_add_output("${ARG_TYPE}" "${TARGET}-${t}")
        endforeach ()
        _Halide_add_output("${ARG_TYPE}" "${TARGET}_wrapper")
    else ()
        # c_source, static_library, or object with one target
        _Halide_add_output("${ARG_TYPE}" "${TARGET}")
    endif ()

    foreach (output IN LISTS ARG_EXTRA_OUTPUTS)
        if (output STREQUAL "c_source" AND ARG_TYPE STREQUAL "c_source")
            # Skip this as it was handled above
        else ()
            _Halide_add_output("${output}" "${TARGET}")
        endif ()
    endforeach ()

    ## Run the generator
    add_custom_command(
        OUTPUT ${output_files}
        COMMAND ${ARG_COMMAND}
        -n "${TARGET}"
        -d "$<BOOL:${ARG_GRADIENT_DESCENT}>"
        -g "${ARG_GENERATOR}"
        -f "${ARG_FUNCTION_NAME}"
        -e "$<LOWER_CASE:$<JOIN:$<REMOVE_DUPLICATES:${outputs}>,$<COMMA>>>"
        ${plugins_args}
        -o .
        "target=$<JOIN:${ARG_TARGETS},$<COMMA>>"
        ${ARG_PARAMS}
        DEPENDS ${ARG_DEPENDS} ${ARG_PLUGINS}
        VERBATIM
    )

    ## Populate output variables
    list(TRANSFORM output_files PREPEND "${CMAKE_CURRENT_BINARY_DIR}/")

    foreach (out IN LISTS outputs)
        set("local_out_${out}" "")
    endforeach ()

    foreach (out file IN ZIP_LISTS outputs output_files)
        list(APPEND "local_out_${out}" "${file}")
        list(APPEND "OUT_${out}" "${file}")
    endforeach ()

    foreach (out IN LISTS outputs)
        set("OUT_${out}" "${OUT_${out}}" PARENT_SCOPE)
    endforeach ()

    # Create the filter's library target
    if (ARG_TYPE STREQUAL "static_library")
        add_library("${TARGET}" STATIC IMPORTED GLOBAL)
        set_target_properties("${TARGET}" PROPERTIES IMPORTED_LOCATION "${local_out_${ARG_TYPE}}")
    else ()
        add_library("${TARGET}" STATIC ${local_out_${ARG_TYPE}})
        set_property(TARGET "${TARGET}" PROPERTY POSITION_INDEPENDENT_CODE ON)
        set_property(TARGET "${TARGET}" PROPERTY LINKER_LANGUAGE CXX)
        if (ARG_TYPE STREQUAL "c_source")
            set_source_files_properties("${local_out_${ARG_TYPE}}" PROPERTIES SKIP_LINTING ON)
        endif ()

        if (NOT Halide_NO_DEFAULT_FLAGS)
            # Silence many useless warnings in generated C++ code compilation
            target_compile_options(
                "${TARGET}" PRIVATE $<$<CXX_COMPILER_ID:GNU,Clang,AppleClang>:-Wno-psabi>
            )
        endif ()
        _Halide_fix_xcode("${TARGET}")
    endif ()

    add_custom_target("${TARGET}.update" DEPENDS ${output_files})
    add_dependencies("${TARGET}" "${TARGET}.update")

    target_link_libraries("${TARGET}" INTERFACE "${ARG_USE_RUNTIME}")
    add_dependencies("${TARGET}" "${ARG_USE_RUNTIME}")

    if (NOT ARG_TYPE STREQUAL "c_source")
        _Halide_add_targets_to_runtime("${ARG_USE_RUNTIME}" TARGETS ${ARG_TARGETS})
    endif ()

    target_sources("${TARGET}" INTERFACE
                   FILE_SET HEADERS
                   BASE_DIRS "${CMAKE_CURRENT_BINARY_DIR}"
                   FILES "${local_out_c_header}")
endfunction()

function(_Halide_lipo)
    cmake_parse_arguments(PARSE_ARGV 0 ARG "OVERWRITE" "TARGET" "INPUTS")

    set(merged_libs ${ARG_INPUTS})

    list(TRANSFORM merged_libs REPLACE "^(.+)$" "$<TARGET_FILE:\\1>"
         OUTPUT_VARIABLE merged_libs_files)

    if (ARG_OVERWRITE)
        list(APPEND merged_libs_files "$<TARGET_FILE:${ARG_TARGET}>")
    endif ()

    find_program(LIPO lipo REQUIRED)
    add_custom_command(
        TARGET "${ARG_TARGET}" POST_BUILD
        COMMAND "${LIPO}" -create ${merged_libs_files} -output "$<TARGET_FILE:${ARG_TARGET}>"
        VERBATIM
    )

    list(TRANSFORM merged_libs REPLACE "^(.+)$" "$<COMPILE_ONLY:\\1>"
         OUTPUT_VARIABLE merged_libs_targets)
    target_link_libraries("${ARG_TARGET}" INTERFACE ${merged_libs_targets})

    ## Visual Studio and Xcode lack any way to specify that the link step of
    # building a target has dependencies outside the object files that go inside
    # the target. CMake models this with LINK_DEPENDS, which only has an effect
    # on the Ninja and Makefile generators. However, it does not consider static
    # libraries to have a link step! So instead, we create a dummy file and use
    # OBJECT_DEPENDS (subject to the same generator restrictions).

    if (NOT CMAKE_GENERATOR MATCHES "Ninja|Make")
        message(
            WARNING
            "Generator ${CMAKE_GENERATOR} does not support OBJECT_DEPENDS! "
            "${ARG_TARGET} will not be rebuilt accurately in incremental builds."
        )
    endif ()

    set(sources "")
    foreach (lib IN LISTS merged_libs)
        get_property(lib_sources TARGET "${lib}" PROPERTY SOURCES)
        foreach (source IN LISTS lib_sources)
            cmake_path(ABSOLUTE_PATH source BASE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}")
            list(APPEND sources ${source})
        endforeach ()
    endforeach ()

    string(MAKE_C_IDENTIFIER "Halide_lipo_dummy_symbol_${ARG_TARGET}" dummy_symbol)
    set(dep_src "${CMAKE_CURRENT_BINARY_DIR}/${dummy_symbol}.cpp")
    file(CONFIGURE OUTPUT "${dep_src}" CONTENT [[
    /* suppresses warning from ranlib */
    void @dummy_symbol@() {}
    ]] @ONLY)
    target_sources("${ARG_TARGET}" PRIVATE "${dep_src}")
    set_source_files_properties("${dep_src}" PROPERTIES OBJECT_DEPENDS "${sources}")
endfunction()

function(_Halide_compute_generator_cmd)
    cmake_parse_arguments(PARSE_ARGV 0 ARG "" "FROM;OUT_COMMAND;OUT_DEPENDS" "")

    if (NOT ARG_FROM)
        message(FATAL_ERROR "Missing FROM argument specifying a Halide generator target")
    endif ()

    if (NOT TARGET "${ARG_FROM}")
        # FROM is usually an unqualified name; if we are crosscompiling, we might need a
        # fully-qualified name, so add the default package name and retry
        set(FQ_ARG_FROM "${PROJECT_NAME}::halide_generators::${ARG_FROM}")
        if (NOT TARGET "${FQ_ARG_FROM}")
            message(FATAL_ERROR "Unable to locate FROM as either ${ARG_FROM} or ${FQ_ARG_FROM}")
        endif ()

        set(ARG_FROM "${FQ_ARG_FROM}")
    endif ()

    get_property(py_src TARGET "${ARG_FROM}" PROPERTY Halide_PYTHON_GENERATOR_SOURCE)
    if (NOT py_src)
        set("${ARG_OUT_COMMAND}" "${ARG_FROM}" PARENT_SCOPE)
        set("${ARG_OUT_DEPENDS}" "${ARG_FROM}" PARENT_SCOPE)
        return()
    endif ()

    # TODO: Python Generators need work to support crosscompiling (https://github.com/halide/Halide/issues/7014)
    if (NOT TARGET Halide::Python)
        message(FATAL_ERROR "Missing Halide::Python. Load the Python component "
                "in find_package() or set WITH_PYTHON_BINDINGS=ON if in tree.")
    endif ()

    if (NOT TARGET Python::Interpreter)
        message(FATAL_ERROR "Missing Python::Interpreter. Missing call to find_package(Python 3)?")
    endif ()

    set("${ARG_OUT_COMMAND}"
        ${CMAKE_COMMAND} -E env "PYTHONPATH=$<PATH:NORMAL_PATH,$<TARGET_FILE_DIR:Halide::Python>/..>" --
        ${Halide_PYTHON_LAUNCHER} "$<TARGET_FILE:Python::Interpreter>" $<SHELL_PATH:${py_src}>
        PARENT_SCOPE)
    set("${ARG_OUT_DEPENDS}" ${ARG_FROM} Halide::Python ${py_src} PARENT_SCOPE)
endfunction()

function(_Halide_set_osx_arch TARGET TRIPLE)
    if (APPLE)
        if (TRIPLE STREQUAL "arm-64-osx")
            set_property(TARGET "${TARGET}" PROPERTY OSX_ARCHITECTURES "arm64")
        elseif (TRIPLE STREQUAL "x86-64-osx")
            set_property(TARGET "${TARGET}" PROPERTY OSX_ARCHITECTURES "x86_64")
        else ()
            message(FATAL_ERROR "Could not set OSX_ARCHITECTURES for ${TRIPLE}")
        endif ()
    endif ()
endfunction()

##
# Function to simplify writing the CMake rules for invoking a generator executable
# and getting a usable CMake library out of it.
##

function(add_halide_library TARGET)
    ##
    # Set up argument parsing for extra outputs.
    ##

    # See Module.cpp for list of extra outputs. The following outputs intentionally do not appear:
    # - `c_header` is always generated
    # - `cpp_stub` is not available
    # - `object` is selected for CMake-target-compile
    # - `static_library` is selected for cross-compile
    set(extra_output_names
        ASSEMBLY
        BITCODE
        COMPILER_LOG
        CONCEPTUAL_STMT
        CONCEPTUAL_STMT_HTML
        C_SOURCE
        DEVICE_CODE
        FEATURIZATION
        FUNCTION_INFO_HEADER
        HLPIPE
        LLVM_ASSEMBLY
        PYTHON_EXTENSION
        PYTORCH_WRAPPER
        REGISTRATION
        SCHEDULE
        STMT
        STMT_HTML
    )

    ##
    # Parse the arguments and set defaults for missing values.
    ##

    set(features_args FEATURES)
    foreach (arch IN ITEMS x86 arm powerpc hexagon wasm riscv)
        foreach (bits IN ITEMS 32 64)
            foreach (os IN ITEMS linux windows osx android ios qurt noos fuchsia wasmrt)
                list(APPEND features_args "FEATURES[${arch}-${bits}-${os}]")
            endforeach ()
        endforeach ()
    endforeach ()

    set(options C_BACKEND GRADIENT_DESCENT)
    set(oneValueArgs FROM GENERATOR FUNCTION_NAME NAMESPACE USE_RUNTIME AUTOSCHEDULER HEADER ${extra_output_names} NO_THREADS NO_DL_LIBS)
    set(multiValueArgs DEPENDS TARGETS PARAMS PLUGINS ${features_args})
    cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

    if (NOT "${ARG_UNPARSED_ARGUMENTS}" STREQUAL "")
        message(AUTHOR_WARNING "Arguments to add_halide_library were not recognized: ${ARG_UNPARSED_ARGUMENTS}")
    endif ()

    set(nonEmptyArgs ${oneValueArgs})
    list(REMOVE_ITEM nonEmptyArgs AUTOSCHEDULER FUNCTION_NAME GENERATOR NAMESPACE USE_RUNTIME)
    foreach (name IN LISTS ARG_KEYWORDS_MISSING_VALUES)
        if (name IN_LIST nonEmptyArgs)
            message(AUTHOR_WARNING "The argument ${name} passed to add_halide_library is undefined")
        endif ()
    endforeach ()

    if (ARG_C_BACKEND AND ARG_TARGETS)
        message(AUTHOR_WARNING "The C backend sources will be compiled with the current CMake toolchain.")
    endif ()

    if (NOT ARG_GENERATOR)
        set(ARG_GENERATOR "${TARGET}")
    endif ()

    if (NOT ARG_FUNCTION_NAME)
        set(ARG_FUNCTION_NAME "${TARGET}")
    endif ()

    if (ARG_NAMESPACE)
        set(ARG_FUNCTION_NAME "${ARG_NAMESPACE}::${ARG_FUNCTION_NAME}")
    endif ()

    # If no TARGETS argument, use Halide_TARGET instead
    if (NOT ARG_TARGETS)
        set(ARG_TARGETS "${Halide_TARGET}")
    endif ()

    # If still no TARGET, try to use host, but if that would
    # cross-compile, then default to 'cmake' and warn.
    if (NOT ARG_TARGETS)
        if (Halide_HOST_TARGET STREQUAL Halide_CMAKE_TARGET)
            set(ARG_TARGETS host)
        else ()
            message(AUTHOR_WARNING
                    "Targets must be manually specified to add_halide_library when cross-compiling. "
                    "The default 'host' target ${Halide_HOST_TARGET} differs from the active CMake "
                    "target ${Halide_CMAKE_TARGET}. Using ${Halide_CMAKE_TARGET} to compile ${TARGET}. "
                    "This might result in performance degradation from missing arch flags (eg. avx).")
            set(ARG_TARGETS cmake)
        endif ()
    endif ()

    _Halide_validate_multitarget(common_triple ${ARG_TARGETS})

    _Halide_is_crosscompiling(is_crosscompiling "${common_triple}")
    _Halide_get_platform_extensions(
        object_extension
        static_library_extension
        "${common_triple}"
    )

    ##
    # Set up the runtime library, if needed
    ##

    if (ARG_C_BACKEND)
        # The C backend does not provide a runtime, so just supply headers.
        if (ARG_USE_RUNTIME)
            message(AUTHOR_WARNING "The C backend does not use a runtime.")
        endif ()
        set(ARG_USE_RUNTIME Halide::Runtime)
    elseif (NOT ARG_USE_RUNTIME)
        # If we're not using an existing runtime, create one.
        set(runtime_args "")
        if (DEFINED ARG_NO_THREADS)
            list(APPEND runtime_args NO_THREADS "${ARG_NO_THREADS}")
        endif ()
        if (DEFINED ARG_NO_DL_LIBS)
            list(APPEND runtime_args NO_DL_LIBS "${ARG_NO_DL_LIBS}")
        endif ()

        add_halide_runtime(
            "${TARGET}.runtime"
            NO_DEFAULT_TARGETS TARGETS ${ARG_TARGETS}
            ${runtime_args}
        )

        set(ARG_USE_RUNTIME "${TARGET}.runtime")
    elseif (NOT TARGET ${ARG_USE_RUNTIME})
        message(FATAL_ERROR "Invalid runtime target ${ARG_USE_RUNTIME}")
    else ()
        _Halide_add_targets_to_runtime(${ARG_USE_RUNTIME} TARGETS ${ARG_TARGETS})
    endif ()

    ##
    # Determine which outputs the generator call will emit.
    ##

    if (ARG_C_BACKEND)
        set(library_type c_source)
    elseif (is_crosscompiling)
        set(library_type static_library)
    else ()
        set(library_type object)
    endif ()

    # Add in extra outputs using the table defined at the start of this function
    set(extra_outputs "")
    foreach (out IN LISTS extra_output_names)
        if (ARG_${out})
            string(TOLOWER "${out}" out)
            list(APPEND extra_outputs ${out})
        endif ()
    endforeach ()

    ##
    # Attach an autoscheduler if the user requested it
    ##

    if (ARG_AUTOSCHEDULER)
        if ("${ARG_AUTOSCHEDULER}" MATCHES "::")
            if (NOT TARGET "${ARG_AUTOSCHEDULER}")
                message(FATAL_ERROR "Autoscheduler ${ARG_AUTOSCHEDULER} does not exist.")
            endif ()

            # Convention: if the argument names a target like "Namespace::Scheduler" then
            # it is assumed to be a MODULE target providing a scheduler named "Scheduler".
            list(APPEND ARG_PLUGINS "${ARG_AUTOSCHEDULER}")
            string(REGEX REPLACE ".*::(.*)" "\\1" ARG_AUTOSCHEDULER "${ARG_AUTOSCHEDULER}")
        elseif (NOT ARG_PLUGINS)
            message(AUTHOR_WARNING "AUTOSCHEDULER set to a scheduler name but no plugins were loaded")
        endif ()
        list(PREPEND ARG_PARAMS "autoscheduler=${ARG_AUTOSCHEDULER}")
    endif ()

    ##
    # Main library target for filter.
    ##

    _Halide_compute_generator_cmd(
        FROM "${ARG_FROM}"
        OUT_COMMAND generator_cmd
        OUT_DEPENDS generator_cmd_deps
    )

    set(generator_args
        COMMAND ${generator_cmd}
        DEPENDS ${generator_cmd_deps} ${ARG_DEPENDS}
        EXTRA_OUTPUTS ${extra_outputs}
        FUNCTION_NAME "${ARG_FUNCTION_NAME}"
        GENERATOR "${ARG_GENERATOR}"
        GRADIENT_DESCENT "${ARG_GRADIENT_DESCENT}"
        PARAMS ${ARG_PARAMS}
        PLUGINS ${ARG_PLUGINS}
        TYPE "${library_type}"
        USE_RUNTIME "${ARG_USE_RUNTIME}"
    )

    list(JOIN ARG_FEATURES "-" ARG_FEATURES)

    # Clear output lists
    foreach (output IN LISTS extra_outputs)
        set(OUT_${output} "")
    endforeach ()

    list(LENGTH Halide_CMAKE_TARGET num_platforms)
    if (common_triple STREQUAL "cmake" AND num_platforms GREATER 1)
        set(merged_base "")
        set(merged_libs "")

        foreach (triple IN LISTS Halide_CMAKE_TARGET)
            set(features_arch "ARG_FEATURES[${triple}]")
            set(features_arch "${${features_arch}}")
            if (features_arch)
                list(TRANSFORM features_arch PREPEND "${triple}-"
                     OUTPUT_VARIABLE targets_arch)
            else ()
                set(targets_arch "${triple}")
            endif ()

            list(TRANSFORM targets_arch APPEND "-${ARG_FEATURES}")
            list(TRANSFORM targets_arch REPLACE "-$" "")

            if (NOT merged_base AND NOT ARG_C_BACKEND)
                set(this_lib "${TARGET}")
                set(merged_base "${this_lib}")
            else ()
                set(this_lib "${TARGET}-${triple}")
                list(APPEND merged_libs "${this_lib}")
            endif ()

            # Appends to OUT_c_header, OUT_<extra-output>, etc.
            _Halide_library_from_generator(
                "${this_lib}" ${generator_args} TARGETS ${targets_arch})
            _Halide_set_osx_arch("${this_lib}" "${triple}")
        endforeach ()

        if (ARG_C_BACKEND)
            add_library("${TARGET}" STATIC)
            _Halide_lipo(TARGET "${TARGET}" INPUTS ${merged_libs})
        else ()
            _Halide_lipo(TARGET "${merged_base}" INPUTS ${merged_libs} OVERWRITE)
        endif ()
    else ()
        list(TRANSFORM ARG_TARGETS REPLACE "cmake" "${Halide_CMAKE_TARGET}")
        if (ARG_FEATURES)
            list(TRANSFORM ARG_TARGETS APPEND "-${ARG_FEATURES}")
        endif ()

        # Appends to OUT_c_header, OUT_<extra-output>, etc.
        _Halide_library_from_generator(
            "${TARGET}" ${generator_args} TARGETS ${ARG_TARGETS})
    endif ()

    # Save some info for add_halide_python_extension_library() in case it is used for this target.
    set_property(TARGET "${TARGET}" PROPERTY Halide_LIBRARY_RUNTIME_TARGET "${ARG_USE_RUNTIME}")
    set_property(TARGET "${TARGET}" PROPERTY Halide_LIBRARY_FUNCTION_NAME "${ARG_FUNCTION_NAME}")
    if ("python_extension" IN_LIST extra_outputs)
        list(GET OUT_python_extension 0 py_ext_cpp) # These files should always be identical
        set_property(TARGET "${TARGET}" PROPERTY Halide_LIBRARY_PYTHON_EXTENSION_CPP "${py_ext_cpp}")
    endif ()

    # Propagate outputs
    if (ARG_HEADER)
        set(${ARG_HEADER} "${OUT_c_header}")
    endif ()

    foreach (output IN LISTS extra_outputs)
        string(TOUPPER "ARG_${output}" outvar_arg)
        if (${outvar_arg})
            set("${${outvar_arg}}" ${OUT_${output}} PARENT_SCOPE)
        endif ()
    endforeach ()
endfunction()

function(_Halide_validate_multitarget OUT_TRIPLE)
    list(LENGTH ARGN len)
    if (len LESS 1)
        message(FATAL_ERROR "Must supply at least one target")
    endif ()

    set(triple "")
    set(all_features "")
    foreach (target IN LISTS ARGN)
        if (target MATCHES "^(host|cmake|[^-]+-[^-]+-[^-]+)(-[^-]+)*$")
            set(this_triple "${CMAKE_MATCH_1}")
            list(APPEND all_features ${CMAKE_MATCH_2})
            if (NOT triple)
                set(triple "${this_triple}")
            elseif (NOT this_triple STREQUAL triple)
                message(FATAL_ERROR "Multi-target entry `${target}` does not match earlier triple `${triple}`")
            endif ()
        else ()
            message(FATAL_ERROR "TARGET `${target}` is malformed")
        endif ()
    endforeach ()

    list(LENGTH Halide_CMAKE_TARGET num_platforms)
    if (num_platforms GREATER 1)
        if (NOT all_features STREQUAL "")
            message(
                FATAL_ERROR
                "Multiarch builds cannot include features in the target list. "
                "Use FEATURES[arch] instead. "
                "Halide_CMAKE_TARGET=${Halide_CMAKE_TARGET} and saw TARGETS ${ARGN}."
            )
        endif ()
        if (triple STREQUAL "host" AND "${Halide_HOST_TARGET}" IN_LIST Halide_CMAKE_TARGET)
            set(triple "cmake")
        endif ()
    endif ()

    set(${OUT_TRIPLE} "${triple}" PARENT_SCOPE)
endfunction()

function(add_halide_python_extension_library TARGET)
    set(options "")
    set(oneValueArgs MODULE_NAME)
    set(multiValueArgs HALIDE_LIBRARIES)
    cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

    if (NOT ARG_MODULE_NAME)
        set(ARG_MODULE_NAME "${TARGET}")
    endif ()

    if (NOT ARG_HALIDE_LIBRARIES)
        message(FATAL_ERROR "HALIDE_LIBRARIES must be specified")
    endif ()

    set(runtimes "")
    set(pycpps "")
    set(function_names "")  # space-separated X-macros
    foreach (lib IN LISTS ARG_HALIDE_LIBRARIES)
        if (NOT TARGET "${lib}")
            message(FATAL_ERROR "${lib} is not a valid target")
        endif ()

        get_property(runtime_used TARGET ${lib} PROPERTY Halide_LIBRARY_RUNTIME_TARGET)
        if (NOT runtime_used)
            message(FATAL_ERROR "${lib} does not appear to have a Halide Runtime specified")
        endif ()
        list(APPEND runtimes ${runtime_used})

        get_property(function_name TARGET ${lib} PROPERTY Halide_LIBRARY_FUNCTION_NAME)
        if (NOT function_name)
            message(FATAL_ERROR "${lib} does not appear to have a Function name specified")
        endif ()
        # Strip C++ namespace(s), if any
        string(REGEX REPLACE ".*::(.*)" "\\1" function_name "${function_name}")
        string(APPEND function_names " X(${function_name})")

        get_property(pycpp TARGET ${lib} PROPERTY Halide_LIBRARY_PYTHON_EXTENSION_CPP)
        if (NOT pycpp)
            message(FATAL_ERROR "${lib} must be built with PYTHON_EXTENSION specified in order to use it with add_halide_python_extension_library()")
        endif ()
        list(APPEND pycpps ${pycpp})
    endforeach ()

    list(REMOVE_DUPLICATES runtimes)
    list(LENGTH runtimes len)
    if (NOT len EQUAL 1)
        message(FATAL_ERROR "${TARGET} requires all libraries to use the same Halide Runtime, but saw ${len}: ${runtimes}")
    endif ()

    set(pyext_runtime_name ${TARGET}_module_definition)
    set(pyext_module_definition_src "${CMAKE_CURRENT_BINARY_DIR}/${pyext_runtime_name}.py.cpp")

    add_custom_command(OUTPUT ${pyext_module_definition_src}
                       COMMAND Halide::GenRT -r "${pyext_runtime_name}" -e python_extension -o "${CMAKE_CURRENT_BINARY_DIR}" target=host
                       DEPENDS Halide::GenRT
                       VERBATIM)

    Python_add_library(${TARGET} MODULE WITH_SOABI ${pycpps} ${pyext_module_definition_src})
    target_link_libraries(${TARGET} PRIVATE ${ARG_HALIDE_LIBRARIES})
    target_compile_definitions(${TARGET} PRIVATE
                               # Skip the default module-definition code in each file
                               HALIDE_PYTHON_EXTENSION_OMIT_MODULE_DEFINITION
                               # Gotta explicitly specify the module name and function(s) for this mode
                               HALIDE_PYTHON_EXTENSION_MODULE_NAME=${ARG_MODULE_NAME}
                               "HALIDE_PYTHON_EXTENSION_FUNCTIONS=${function_names}")
    target_compile_features(${TARGET} PRIVATE cxx_std_17)
    set_target_properties(${TARGET} PROPERTIES OUTPUT_NAME ${ARG_MODULE_NAME})
    _Halide_target_export_single_symbol(${TARGET} "PyInit_${ARG_MODULE_NAME}")
endfunction()

##
# Function for ensuring that Halide.dll is visible to the generator
##

function(_Halide_place_dll GEN)
    if (NOT WIN32)
        return()
    endif ()

    # Short circuit so that Halide::Halide isn't checked when importing a generator from another CMake project
    get_property(is_imported TARGET ${GEN} PROPERTY IMPORTED)
    if (is_imported)
        return()
    endif ()

    get_property(has_post_build TARGET ${GEN} PROPERTY Halide_GENERATOR_HAS_POST_BUILD)
    if (has_post_build)
        return()
    endif ()

    # Here GEN is not IMPORTED, which means that it must be linked
    # to Halide::Halide and therefore Halide::Halide must exist.
    get_property(halide_type TARGET Halide::Halide PROPERTY TYPE)
    if (NOT halide_type STREQUAL "SHARED_LIBRARY")
        return()
    endif ()

    add_custom_command(
        TARGET ${GEN} POST_BUILD
        COMMAND powershell -NoProfile -ExecutionPolicy Bypass
        -File "${CMAKE_CURRENT_FUNCTION_LIST_DIR}/MutexCopy.ps1"
        -src "$<TARGET_FILE:Halide::Halide>"
        -dstDir "$<TARGET_FILE_DIR:${GEN}>"
        VERBATIM
    )
    set_property(TARGET ${GEN} PROPERTY Halide_GENERATOR_HAS_POST_BUILD 1)
endfunction()

##
# Function for creating a standalone runtime from a generator.
##

function(add_halide_runtime RT)
    set(options NO_DEFAULT_TARGETS)
    set(oneValueArgs NO_THREADS NO_DL_LIBS)
    set(multiValueArgs TARGETS)
    cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

    # If no TARGETS argument, use Halide_TARGET instead
    if (NOT ARG_TARGETS)
        set(ARG_TARGETS "${Halide_TARGET}")
    endif ()

    # The default of NO_THREADS/NO_DL_LIBS is OFF unless Halide_RUNTIME_NO_THREADS/NO_DL_LIBS is defined globally
    if (NOT DEFINED ARG_NO_THREADS)
        set(ARG_NO_THREADS ${Halide_RUNTIME_NO_THREADS})
    endif ()
    if (NOT DEFINED ARG_NO_DL_LIBS)
        set(ARG_NO_DL_LIBS ${Halide_RUNTIME_NO_DL_LIBS})
    endif ()

    _Halide_validate_multitarget(common_triple ${ARG_TARGETS})

    _Halide_is_crosscompiling(is_crosscompiling "${common_triple}")
    _Halide_get_platform_extensions(
        object_extension
        static_library_extension
        "${common_triple}")

    # We defer reading the list of targets for which to generate a common
    # runtime to CMake _generation_ time. This prevents issues where a lower
    # GCD is required by a later Halide library linking to this runtime.
    set(target_list "$<TARGET_GENEX_EVAL:${RT},$<TARGET_PROPERTY:${RT},Halide_RT_TARGETS>>")

    # Remove features that should not be attached to a runtime
    # TODO: The fact that removing profile fixes a duplicate symbol linker error on Windows smells like a bug.
    set(target_list "$<LIST:TRANSFORM,${target_list},REPLACE,-(user_context|no_asserts|no_bounds_query|no_runtime|profile),>")

    if (is_crosscompiling)
        set(GEN_OUTS "${RT}${static_library_extension}")
        add_custom_command(
            OUTPUT "${GEN_OUTS}"
            COMMAND Halide::GenRT -r "${RT}" -o .
            "target=$<JOIN:$<REMOVE_DUPLICATES:${target_list}>,$<COMMA>>"
            DEPENDS Halide::GenRT
            VERBATIM)
        add_custom_target("${RT}.update" DEPENDS "${GEN_OUTS}")

        add_library("${RT}" STATIC IMPORTED GLOBAL)
        add_dependencies("${RT}" "${RT}.update")

        set_target_properties("${RT}" PROPERTIES
                              IMPORTED_LOCATION ${CMAKE_CURRENT_BINARY_DIR}/${GEN_OUTS})
    else ()
        list(LENGTH Halide_CMAKE_TARGET num_platforms)
        if (common_triple STREQUAL "cmake" AND num_platforms GREATER 1)
            set(base_rt "")
            set(arch_rt "")
            foreach (triple IN LISTS Halide_CMAKE_TARGET)
                set(arch_target_list "$<LIST:TRANSFORM,${target_list},REPLACE,cmake,${triple}>")
                set(arch_target_list "$<FILTER:${arch_target_list},INCLUDE,^${triple}>")

                if (NOT base_rt)
                    set(this_rt "${RT}")
                    set(base_rt "${this_rt}")
                else ()
                    set(this_rt "${RT}-${triple}")
                    list(APPEND arch_rt "${this_rt}")
                endif ()

                add_custom_command(
                    OUTPUT "${this_rt}${object_extension}"
                    COMMAND Halide::GenRT -r "${this_rt}" -o . -e object
                    "target=$<JOIN:$<REMOVE_DUPLICATES:${arch_target_list}>,$<COMMA>>"
                    DEPENDS Halide::GenRT
                    VERBATIM)

                add_library("${this_rt}" STATIC "${this_rt}${object_extension}")
                set_target_properties("${this_rt}" PROPERTIES LINKER_LANGUAGE CXX)
                _Halide_fix_xcode("${this_rt}")
                _Halide_set_osx_arch("${this_rt}" "${triple}")
            endforeach ()

            _Halide_lipo(TARGET "${base_rt}" INPUTS ${arch_rt} OVERWRITE)
        else ()
            set(target_list "$<LIST:TRANSFORM,${target_list},REPLACE,cmake,${Halide_CMAKE_TARGET}>")
            add_custom_command(
                OUTPUT "${RT}${object_extension}"
                COMMAND Halide::GenRT -r "${RT}" -o . -e object
                "target=$<JOIN:$<REMOVE_DUPLICATES:${target_list}>,$<COMMA>>"
                DEPENDS Halide::GenRT
                VERBATIM)
            add_library("${RT}" STATIC "${RT}${object_extension}")
            set_target_properties("${RT}" PROPERTIES LINKER_LANGUAGE CXX)
            _Halide_fix_xcode("${RT}")
        endif ()
    endif ()

    # Take care of the runtime/toolchain which doesn't have Threads or DL libs
    target_link_libraries("${RT}" INTERFACE Halide::Runtime)
    if (NOT ARG_NO_THREADS)
        find_package(Threads REQUIRED)
        target_link_libraries("${RT}" INTERFACE Threads::Threads)
    endif ()
    if (NOT ARG_NO_DL_LIBS)
        target_link_libraries("${RT}" INTERFACE ${CMAKE_DL_LIBS})
    endif ()

    if (NOT ARG_NO_DEFAULT_TARGETS)
        _Halide_add_targets_to_runtime("${RT}" TARGETS ${ARG_TARGETS})
    endif ()
endfunction()

function(_Halide_is_crosscompiling OUT_XC triple)
    if (triple MATCHES "host")
        set(triple "${Halide_HOST_TARGET}")
    endif ()

    if (triple MATCHES "cmake" OR triple IN_LIST Halide_CMAKE_TARGET)
        set("${OUT_XC}" 0 PARENT_SCOPE)
    else ()
        set("${OUT_XC}" 1 PARENT_SCOPE)
    endif ()
endfunction()

function(_Halide_get_platform_extensions OUT_OBJ OUT_STATIC triple)
    if (triple MATCHES "host")
        set(triple "${Halide_HOST_TARGET}")
    endif ()

    if (triple MATCHES "cmake")
        set(triple "${Halide_CMAKE_TARGET}")
    endif ()

    if (triple MATCHES "windows")
        set("${OUT_OBJ}" ".obj" PARENT_SCOPE)
        set("${OUT_STATIC}" ".lib" PARENT_SCOPE)
    else ()
        # All other OSes use .a
        set("${OUT_OBJ}" ".o" PARENT_SCOPE)
        set("${OUT_STATIC}" ".a" PARENT_SCOPE)
    endif ()
endfunction()

##
# Utility for finding GPU libraries that are needed by
# the runtime when listed in the Halide target string.
##

function(_Halide_add_targets_to_runtime TARGET)
    cmake_parse_arguments(PARSE_ARGV 1 ARG "" "" "TARGETS")

    if (NOT TARGET "${TARGET}")
        message(FATAL_ERROR "not a target: ${TARGET}")
    endif ()

    get_property(aliased TARGET "${TARGET}" PROPERTY ALIASED_TARGET)
    if (aliased)
        set(TARGET "${aliased}")
    endif ()

    set_property(TARGET "${TARGET}" APPEND PROPERTY Halide_RT_TARGETS "${ARG_TARGETS}")
    _Halide_target_link_gpu_libs(${TARGET} INTERFACE ${ARG_TARGETS})
endfunction()

function(_Halide_target_link_gpu_libs TARGET VISIBILITY)
    if ("${ARGN}" MATCHES "metal")
        find_library(FOUNDATION_LIBRARY Foundation REQUIRED)
        find_library(METAL_LIBRARY Metal REQUIRED)
        target_link_libraries(${TARGET} ${VISIBILITY} "${FOUNDATION_LIBRARY}" "${METAL_LIBRARY}")
    endif ()

    if ("${ARGN}" MATCHES "webgpu" AND NOT "${ARGN}" MATCHES "wasm")
        find_package(Halide_WebGPU REQUIRED)
        target_link_libraries(${TARGET} ${VISIBILITY} Halide::WebGPU)
    endif ()
endfunction()

##
# Function for working around Xcode backend bugs
##

function(_Halide_fix_xcode TARGET)
    if (CMAKE_GENERATOR STREQUAL "Xcode")
        # Xcode generator requires at least one source file to work correctly.
        set(empty_file "${CMAKE_CURRENT_BINARY_DIR}/Halide_${TARGET}_empty.cpp")
        file(CONFIGURE OUTPUT "${empty_file}" CONTENT "")
        target_sources("${TARGET}" PRIVATE "${empty_file}")
    endif ()
endfunction()

function(_Halide_target_export_single_symbol TARGET SYMBOL)
    file(CONFIGURE
         OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${TARGET}.${SYMBOL}.ldscript.apple"
         CONTENT "_${SYMBOL}\n")

    file(CONFIGURE
         OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${TARGET}.${SYMBOL}.ldscript"
         CONTENT "{ global: ${SYMBOL}; local: *; };\n")

    target_export_script(
        ${TARGET}
        APPLE_LD "${CMAKE_CURRENT_BINARY_DIR}/${TARGET}.${SYMBOL}.ldscript.apple"
        GNU_LD "${CMAKE_CURRENT_BINARY_DIR}/${TARGET}.${SYMBOL}.ldscript"
    )
endfunction()