# The reims-graphics guest Metal-plugin bundle: the serialiser core and the Metal
# shell that drives it.
#
# The serialiser core (src/Rgfx{ObjectModel,Caps,RingWriter,Serializer,Encoder,
# WireLayout}.* and the rest of CORE_SRCS) is our own code, depends only on the
# committed wire header (and CommonCrypto, for a library's key), needs no Apple
# Metal SPI, and builds for the guest's arch (x86_64).
#
# The Metal shell is the ObjC++ layer Apple's Metal loads as the guest's MTLDevice:
#   - the NSObject-based object wrappers (src/RgfxResource.*, src/RgfxBuffer.*, and
#     the resource/pipeline/… wrappers the later slices add) subclass only NSObject,
#     so they instantiate and are unit-tested on the Mac; and
#   - the device shell (src/RgfxMetalDevice.*) subclasses an Apple-private class
#     whose symbols resolve only at load time in the guest, so it is COMPILED into
#     the bundle but excluded from the user-space unit test.
#
#   make            build the serialiser objects (x86_64, the guest's arch)
#   make test       build + run the checks: the serialiser check (x86_64, under
#                   Rosetta), the AIR argument reader's check (x86_64, under Rosetta,
#                   over .metallibs built from tests/air_arguments.metal,
#                   tests/air_arguments_old.metal and tests/air_arguments_11.metal, the module
#                   of every function of the first, and the repo's committed metallibs), the
#                   Metal-shell wrapper/routing check (this Mac's native arch; the
#                   device shell is not in its binary), and the device's base-class
#                   check (x86_64, under Rosetta), which loads the bundle
#                   (tests/inherited_tests.mm), the protocol members' check against
#                   their register (x86_64, under Rosetta, which loads the bundle:
#                   tests/protocol_members_tests.mm, tests/protocol_members.txt,
#                   tests/protocol_members_releases.txt), and the
#                   video driver's H.264 and HEVC parameter-set checks and its entry
#                   points' check (x86_64, under Rosetta, tests/video_h264_tests.cpp,
#                   tests/video_h265_tests.cpp, tests/video_driver_tests.cpp).
#                   With ARCH=arm64 the arch's checks run natively instead, over the
#                   arm64 bundle, as `make -C .. test` runs them on an Apple-silicon Mac
#                   in BUILD=build/arm64.
#   make bundle     compile + bundle-link the whole x86_64 loadable bundle, device
#                   shell included (links clean with -undefined dynamic_lookup; it
#                   only needs to COMPILE off the guest, not run).
#   make sweep      the guest's correctness sweep of the system frameworks on the device
#                   (tools/sweep), build/sweep-x86_64; ARCH=arm64 for an Apple-silicon Mac.
#   make pipelines  shader and pipeline costs (tools/pipelines), build/pipelines-x86_64 and
#                   build/pipelines-metallibs/; ARCH=arm64 for an Apple-silicon Mac.
#   make bench      the GPU benchmark (tools/bench), build/bench-x86_64; ARCH=arm64 for an
#                   Apple-silicon Mac.
#   make res        what resources cost (tools/res), build/res-x86_64; ARCH=arm64 likewise.
#   make submit-probe
#                   a command buffer's fixed cost and latency (tools/submit_probe.m),
#                   build/submit_probe-x86_64; ARCH=arm64 likewise.
#   make upload-probe
#                   what the CPU's uploads into Managed resources cost at WindowServer's shapes
#                   (tools/upload_probe.m), build/upload_probe-x86_64; ARCH=arm64 likewise.
#   make shtex-probe
#                   a texture shared across processes, drawn in one and read in another
#                   (tools/shtex_probe.m), build/shtex_probe-x86_64; ARCH=arm64 likewise.
#   make icb-probe  what the CPU's encodes into indirect command buffers and its writes into
#                   IOSurfaces cost (tools/icb_probe.m), build/icb_probe-x86_64; ARCH=arm64 likewise.
#   make encode-probe
#                   what the hot encoder calls cost the calling thread, in ns a call
#                   (tools/encode_probe.m), build/encode_probe-x86_64; ARCH=arm64 likewise.
#   make reflection-check
#                   the stitchable functions' reflection the bundle builds against Metal's own,
#                   build/reflection_check-x86_64 (ARCH=arm64 likewise) over
#                   build/air_arguments.metallib.
#   make pipeline-reflection-check
#                   pipelines' reflection the bundle builds against Metal's own,
#                   build/pipeline_reflection_check-x86_64 (ARCH=arm64 likewise) over
#                   build/oracle_reflection.metallib (and its c_atomic_texture case over
#                   build/oracle_reflection_31.metallib).
#   make peer-test-build, make peer-test
#                   the wire round trip of the bundle's ring code against the host's library
#                   (tests/peer_tests.mm; native arch, this Mac's GPU, never in `make test`):
#                   build, then run holding the GPU lock. DIST names `cargo xtask dist`'s tree.
#   make clean      remove build/
#
# Everything lands in build/, never committed.

SDK     ?= $(shell xcrun --sdk macosx --show-sdk-path)
# ARCH is the guest's arch (serialiser core + the bundle); NATIVE is this Mac's
# arch (the shell wrapper unit test). No inline comments on these two: make keeps
# the spaces before a `#`, and a trailing space breaks the object paths below.
ARCH    ?= x86_64
NATIVE  ?= $(shell uname -m)
WIRE     = ../../crates/reims-vgpu-graphics/include
BUILD    = build

CXX      = xcrun clang++

# Shared flags. The serialiser check already builds -Wall -Wextra -Werror; the
# shell holds to the same bar. The deployment target is macOS 11.0, the oldest
# release the guest kexts serve: every symbol the SDK marks newer is then a weak
# import, which dyld leaves NULL on a release that lacks it, where a strong one
# would make dyld refuse the whole bundle. A tool that sets its own
# -mmacosx-version-min later on its line overrides it.
CXXFLAGS = -isysroot $(SDK) -mmacosx-version-min=11.0 -std=c++17 -Os \
	-Wall -Wextra -Werror -Wno-unused-parameter \
	-I src -I $(WIRE)

# ObjC++ (.mm) also needs ARC and three warning suppressions (unguarded-availability-new:
# we target the running OS's SPI; deprecated-declarations: the device protocol has members
# Apple deprecated on macOS 27 — location/peer*/locationNumber/didModifyRange — that we still
# implement for MTLDevice/MTLBuffer conformance and older guests; objc-protocol-method-
# implementation: the device's category split implements MTLDevice protocol methods in
# categories whose primary class declares the conformance).
OBJCXXFLAGS = -fobjc-arc -Wno-unguarded-availability-new -Wno-deprecated-declarations \
	-Wno-objc-protocol-method-implementation
# The macOS 27 SDK's names the ObjC++ files use, declared ahead of each file on an older SDK
# (src/RgfxSdkCompat.h); compile lines only, as a link would take it as an unused argument.
SDK_COMPAT = -include src/RgfxSdkCompat.h
# The protocol-completeness diagnostics: a Metal protocol member a wrapper class leaves out is
# a warning, never an error, so what is left shows on every build of the file. A member the
# device does not support is not left out: it is marked RGFX_UNSUPPORTED (src/RgfxUnsupported.h),
# and the bundle's build prints how many there are. The device's own files (RgfxMetalDevice*.mm)
# keep them quiet: its members live in its categories and in the IOGPU base, which these
# diagnostics do not see, so they would list every member. tools/protocol_audit.m checks
# the device at run time instead, through the base.
PROTOCOL_WARN  = -Wprotocol -Wincomplete-implementation -Wobjc-protocol-property-synthesis \
	-Wno-error=protocol -Wno-error=incomplete-implementation \
	-Wno-error=objc-protocol-property-synthesis
PROTOCOL_QUIET = -Wno-protocol -Wno-incomplete-implementation \
	-Wno-objc-protocol-property-synthesis
PROTOCOL_FLAGS = $(PROTOCOL_WARN)
FRAMEWORKS  = -framework Metal -framework Foundation -framework IOKit -framework IOSurface
# The device shell subclasses IOGPUMetalDevice, whose ObjC class symbol
# (_OBJC_CLASS_$_IOGPUMetalDevice) is a hard link-time dependency. It lives in
# the private IOGPU framework, which the SDK ships a .tbd stub for. We link it
# TWO-LEVEL (not dynamic_lookup) so the bundle records an LC_LOAD_DYLIB for
# IOGPU and dyld auto-loads it at the bundle's load in the guest, resolving the
# superclass — exactly as Apple's AppleParavirtGPUMetalIOGPUFamily.bundle does.
# A flat-namespace dynamic_lookup of this symbol does NOT resolve when Metal
# dlopens the plugin (Metal's process is two-level; IOGPU's symbols aren't in
# the flat namespace), so the class never realises and MTLCopyAllDevices=0.
PRIVFW      = -F $(SDK)/System/Library/PrivateFrameworks -framework IOGPU

HEADERS      = $(wildcard src/*.hpp) $(wildcard src/*.h) ../kext/src/RgfxUserClientABI.hpp \
               ../kext/src/RgfxSurfaceRefill.hpp ../kext/src/RgfxDeviceName.hpp
TESTHEADERS  = $(wildcard tests/*.hpp)

# Serialiser core (C++).
CORE_SRCS = src/RgfxObjectModel.cpp src/RgfxCaps.cpp src/RgfxRingWriter.cpp \
	src/RgfxSerializer.cpp src/RgfxEncoder.cpp src/RgfxWireLayout.cpp \
	src/RgfxRegion.cpp src/RgfxReplyReader.cpp src/RgfxLibraryKey.cpp \
	src/RgfxCompletionThread.cpp src/RgfxFunctionConstants.cpp \
	src/RgfxBinaryArchiveFile.cpp src/RgfxAirArguments.cpp src/RgfxKnownPipelines.cpp \
	src/RgfxSlabs.cpp src/RgfxIcbSlotTable.cpp \
	src/RgfxLibraryStream.cpp src/RgfxMetallibWriter.cpp src/RgfxGLLowering.cpp \
	src/RgfxClMode.cpp src/RgfxLog.cpp src/RgfxFilePages.cpp src/RgfxMemoryShadow.cpp src/RgfxFiles.cpp

# The video driver AppleGVA loads from this bundle (C++, src/RgfxVideoDriver.cpp): bundle only.
VIDEO_SRCS = src/RgfxVideoDriver.cpp src/RgfxVideoH264.cpp src/RgfxVideoH265.cpp
VIDEO_OBJ_ARCH = $(VIDEO_SRCS:src/%.cpp=$(BUILD)/obj/$(ARCH)/core/%.o)
# The whole Metal shell (ObjC++): wrappers + the device. Goes in the bundle. Globbed
# so each later slice's wrappers + category are picked up with no Makefile edit.
SHELL_SRCS = $(wildcard src/*.mm)
# The NSObject-based wrappers alone: the device shell + its category slices subclass the
# Apple-private base, so they compile only into the bundle, never the native unit test.
WRAP_SRCS  = $(filter-out $(wildcard src/RgfxMetalDevice*.mm),$(wildcard src/*.mm))

# Objects are kept per-arch (so the x86_64 and native builds never clobber) and
# split core/ (C++) vs shell/ (ObjC++) so each compile rule has a unique target
# directory — GNU make 3.81 mishandles two pattern rules sharing one target dir.
CORE_OBJ_ARCH  = $(CORE_SRCS:src/%.cpp=$(BUILD)/obj/$(ARCH)/core/%.o)
SHELL_OBJ_ARCH = $(SHELL_SRCS:src/%.mm=$(BUILD)/obj/$(ARCH)/shell/%.o)
CORE_OBJ_NAT   = $(CORE_SRCS:src/%.cpp=$(BUILD)/obj/$(NATIVE)/core/%.o)
WRAP_OBJ_NAT   = $(WRAP_SRCS:src/%.mm=$(BUILD)/obj/$(NATIVE)/shell/%.o)

SER_TESTBIN   = $(BUILD)/serializer_tests
AIR_TESTBIN   = $(BUILD)/air_arguments_test
SHELL_TESTBIN = $(BUILD)/shell_tests
INHERIT_TESTBIN = $(BUILD)/inherited_tests
MEMBERS_TESTBIN = $(BUILD)/protocol_members_tests
VIDEO_TESTBIN = $(BUILD)/video_h264_tests
VIDEO_H265_TESTBIN = $(BUILD)/video_h265_tests
VIDEO_DRIVER_TESTBIN = $(BUILD)/video_driver_tests
BUNDLEBIN     = $(BUILD)/reims-graphics.bundle

all: $(CORE_OBJ_ARCH)

# --- compile rules (per arch; C++ and ObjC++) --------------------------------

$(BUILD)/obj/$(ARCH)/core/%.o: src/%.cpp $(HEADERS) $(WIRE)/reims_graphics_wire.h Makefile
	@mkdir -p $(@D)
	$(CXX) -arch $(ARCH) $(CXXFLAGS) -c $< -o $@

$(BUILD)/obj/$(ARCH)/shell/%.o: src/%.mm $(HEADERS) $(WIRE)/reims_graphics_wire.h Makefile
	@mkdir -p $(@D)
	$(CXX) -arch $(ARCH) $(CXXFLAGS) $(OBJCXXFLAGS) $(SDK_COMPAT) $(PROTOCOL_FLAGS) -c $< -o $@

$(BUILD)/obj/$(NATIVE)/core/%.o: src/%.cpp $(HEADERS) $(WIRE)/reims_graphics_wire.h Makefile
	@mkdir -p $(@D)
	$(CXX) -arch $(NATIVE) $(CXXFLAGS) -c $< -o $@

$(BUILD)/obj/$(NATIVE)/shell/%.o: src/%.mm $(HEADERS) $(WIRE)/reims_graphics_wire.h Makefile
	@mkdir -p $(@D)
	$(CXX) -arch $(NATIVE) $(CXXFLAGS) $(OBJCXXFLAGS) $(SDK_COMPAT) $(PROTOCOL_FLAGS) -c $< -o $@

# The device's files keep the protocol diagnostics quiet (above).
$(patsubst src/%.mm,$(BUILD)/obj/$(ARCH)/shell/%.o,$(wildcard src/RgfxMetalDevice*.mm)): \
	PROTOCOL_FLAGS = $(PROTOCOL_QUIET)

# --- the serialiser check (x86_64, Rosetta) ----------------------------------

$(SER_TESTBIN): $(CORE_OBJ_ARCH) tests/serializer_tests.cpp tests/slab_tests.cpp \
	tests/library_stream_tests.cpp tests/function_constants_tests.cpp $(HEADERS) $(TESTHEADERS) \
	Makefile
	@mkdir -p $(@D)
	$(CXX) -arch $(ARCH) $(CXXFLAGS) -I tests $(CORE_OBJ_ARCH) tests/serializer_tests.cpp \
		tests/slab_tests.cpp tests/library_stream_tests.cpp tests/function_constants_tests.cpp -o $@

# --- the Metal-shell wrapper/routing check (native arch) ---------------------
# The NSObject wrappers + serialiser core + the routing test; no device shell.

SHELL_TESTSRCS = tests/shell_tests.mm tests/protocol_tests.mm tests/archive_tests.mm \
	tests/resource_tests.mm tests/queue_tests.mm \
	tests/create_tests.mm tests/known_pipeline_tests.mm tests/icb_tests.mm tests/gl_tests.mm \
	tests/gl_store_tests.mm tests/cl_mode_tests.mm tests/residency_tests.mm tests/files_tests.mm

$(SHELL_TESTBIN): $(CORE_OBJ_NAT) $(WRAP_OBJ_NAT) $(SHELL_TESTSRCS) $(HEADERS) $(TESTHEADERS) Makefile
	@mkdir -p $(@D)
	$(CXX) -arch $(NATIVE) $(CXXFLAGS) $(OBJCXXFLAGS) $(PROTOCOL_QUIET) -I tests \
		$(CORE_OBJ_NAT) $(WRAP_OBJ_NAT) $(SHELL_TESTSRCS) $(FRAMEWORKS) -framework CoreVideo -o $@

# A Core Image kernel library, compiled from tests/ci_kernel.metal for the shell tests' extern
# functions; built here, never committed. Built for macOS 14 and Metal 3.1: a guest's Metal on
# macOS 26 refuses the macOS 27 SDK's default language version and deployment target.
CI_KERNEL = $(BUILD)/ci_kernel.metallib

$(CI_KERNEL): tests/ci_kernel.metal Makefile
	@mkdir -p $(@D)
	xcrun -sdk macosx metal -c -fcikernel -std=metal3.1 -mmacosx-version-min=14.0 \
		tests/ci_kernel.metal -o $(BUILD)/ci_kernel.air
	xcrun -sdk macosx metallib -cikernel $(BUILD)/ci_kernel.air -o $@

# The stitchable functions of tests/stitchable.metal, for the shell tests' stitched library.
STITCHABLE = $(BUILD)/stitchable.metallib

$(STITCHABLE): tests/stitchable.metal Makefile
	@mkdir -p $(@D)
	xcrun -sdk macosx metal -c -std=metal3.1 -mmacosx-version-min=14.0 tests/stitchable.metal \
		-o $(BUILD)/stitchable.air
	xcrun -sdk macosx metallib $(BUILD)/stitchable.air -o $@

# The stitchable functions of tests/stitch_constants.metal, specialised by constant values before
# the shell tests stitch them.
STITCH_CONSTANTS = $(BUILD)/stitch_constants.metallib

$(STITCH_CONSTANTS): tests/stitch_constants.metal Makefile
	@mkdir -p $(@D)
	xcrun -sdk macosx metal -c -std=metal3.1 -mmacosx-version-min=14.0 tests/stitch_constants.metal \
		-o $(BUILD)/stitch_constants.air
	xcrun -sdk macosx metallib $(BUILD)/stitch_constants.air -o $@

# A committed .metallib of our own (tests/shaders), which the shell tests read at URLs of each form
# as the device's newLibraryWithURL:error: reads them.
LIBRARY_FILE = ../../tests/shaders/compute_add.metallib

# The post-tessellation vertex functions of tests/patch.metal, for the shell tests' patch answers.
PATCH_LIB = $(BUILD)/patch.metallib

$(PATCH_LIB): tests/patch.metal Makefile
	@mkdir -p $(@D)
	xcrun -sdk macosx metal -c -std=metal3.1 -mmacosx-version-min=14.0 tests/patch.metal \
		-o $(BUILD)/patch.air
	xcrun -sdk macosx metallib $(BUILD)/patch.air -o $@

# The [[stage_in]] kernels of tests/stage_in_oracle.metal, against which the shell tests run a GL
# kernel made one under a stage input descriptor on this Mac's GPU.
STAGE_IN_ORACLE_LIB = $(BUILD)/stage_in_oracle.metallib

$(STAGE_IN_ORACLE_LIB): tests/stage_in_oracle.metal Makefile
	@mkdir -p $(@D)
	xcrun -sdk macosx metal -c -std=metal3.1 -mmacosx-version-min=14.0 tests/stage_in_oracle.metal \
		-o $(BUILD)/stage_in_oracle.air
	xcrun -sdk macosx metallib $(BUILD)/stage_in_oracle.air -o $@

# --- the AIR argument reader's check (x86_64, Rosetta) ------------------------
# The reader (src/RgfxAirArguments.*) over a .metallib Apple's Metal compiler builds here from
# tests/air_arguments.metal, never committed; its function constants against the library's CNST
# tags (src/RgfxFunctionConstants.*). AIR_MODULE is the compiler's module of every function of the
# source, which `metallib` splits into the library's one module a function: the reader's answers
# for a module of several entry points.
AIR_LIB = $(BUILD)/air_arguments.metallib
AIR_MODULE = $(BUILD)/air_arguments.air
# The same library built for macOS 26, whose functions Metal's library reflection answers
# (tests/protocol_tests.mm, test_argument_encoders_of_bindings).
AIR_REFLECTED = $(BUILD)/air_arguments_reflected.metallib

$(AIR_REFLECTED): tests/air_arguments.metal Makefile
	@mkdir -p $(@D)
	xcrun -sdk macosx metal -std=metal3.1 -mmacosx-version-min=26.0 tests/air_arguments.metal -o $@

$(AIR_LIB): tests/air_arguments.metal Makefile
	@mkdir -p $(@D)
	xcrun -sdk macosx metal -c -std=metal3.1 -mmacosx-version-min=14.0 tests/air_arguments.metal \
		-o $(AIR_MODULE)
	xcrun -sdk macosx metallib $(AIR_MODULE) -o $@

# Pipelines' reflection (src/RgfxPipelineReflection.*) over the oracle probe's library
# (tools/oracle/oracle_reflection.metal), built for macOS 26 and Metal 3.0 as build.sh builds it,
# whose pipelines' reflection the x86 Macs' answers were measured over; never committed.
ORACLE_REFLECTION_LIB = $(BUILD)/oracle_reflection.metallib

$(ORACLE_REFLECTION_LIB): tools/oracle/oracle_reflection.metal Makefile
	@mkdir -p $(@D)
	xcrun -sdk macosx metal -c -std=metal3.0 -mmacosx-version-min=26.0 tools/oracle/oracle_reflection.metal \
		-o $(BUILD)/oracle_reflection.air
	xcrun -sdk macosx metallib $(BUILD)/oracle_reflection.air -o $@

# The overlapping arguments' functions of the oracle probe oracle_overlap
# (tools/oracle/oracle_overlap.metal), built for macOS 26 and Metal 3.0 as build.sh builds it, whose
# specialisations the x86 Macs' and the M3 Max's answers were measured over; never committed.
ORACLE_OVERLAP_LIB = $(BUILD)/oracle_overlap.metallib

$(ORACLE_OVERLAP_LIB): tools/oracle/oracle_overlap.metal Makefile
	@mkdir -p $(@D)
	xcrun -sdk macosx metal -c -std=metal3.0 -mmacosx-version-min=26.0 tools/oracle/oracle_overlap.metal \
		-o $(BUILD)/oracle_overlap.air
	xcrun -sdk macosx metallib $(BUILD)/oracle_overlap.air -o $@

# The stage input attributes' functions of the oracle probe oracle_fnreflect
# (tools/oracle/oracle_fnreflect.metal), built for macOS 26 and Metal 3.0 as build.sh builds it, whose
# functions' attributes the x86 Macs' and the M3 Max's answers were measured over, and the shell
# tests' own (tests/stage_attributes.metal); never committed.
ORACLE_FNREFLECT_LIB = $(BUILD)/oracle_fnreflect.metallib
STAGE_ATTRIBUTES_LIB = $(BUILD)/stage_attributes.metallib

$(ORACLE_FNREFLECT_LIB): tools/oracle/oracle_fnreflect.metal Makefile
	@mkdir -p $(@D)
	xcrun -sdk macosx metal -c -std=metal3.0 -mmacosx-version-min=26.0 tools/oracle/oracle_fnreflect.metal \
		-o $(BUILD)/oracle_fnreflect.air
	xcrun -sdk macosx metallib $(BUILD)/oracle_fnreflect.air -o $@

$(STAGE_ATTRIBUTES_LIB): tests/stage_attributes.metal Makefile
	@mkdir -p $(@D)
	xcrun -sdk macosx metal -c -std=metal3.1 -mmacosx-version-min=14.0 tests/stage_attributes.metal \
		-o $(BUILD)/stage_attributes.air
	xcrun -sdk macosx metallib $(BUILD)/stage_attributes.air -o $@

# The shell tests' functions of arguments under function constants (tests/predicated_arguments.metal),
# built for macOS 26 and Metal 3.0 as the oracle probe that measured them was; never committed.
PREDICATED_ARGUMENTS_LIB = $(BUILD)/predicated_arguments.metallib

$(PREDICATED_ARGUMENTS_LIB): tests/predicated_arguments.metal Makefile
	@mkdir -p $(@D)
	xcrun -sdk macosx metal -c -std=metal3.0 -mmacosx-version-min=26.0 tests/predicated_arguments.metal \
		-o $(BUILD)/predicated_arguments.air
	xcrun -sdk macosx metallib $(BUILD)/predicated_arguments.air -o $@

# The shell tests' functions of arguments at locations function constants give
# (tests/undecided_locations.metal), built for macOS 26 and Metal 3.0; never committed.
UNDECIDED_LOCATIONS_LIB = $(BUILD)/undecided_locations.metallib

$(UNDECIDED_LOCATIONS_LIB): tests/undecided_locations.metal Makefile
	@mkdir -p $(@D)
	xcrun -sdk macosx metal -c -std=metal3.0 -mmacosx-version-min=26.0 tests/undecided_locations.metal \
		-o $(BUILD)/undecided_locations.air
	xcrun -sdk macosx metallib $(BUILD)/undecided_locations.air -o $@

# The library of pipeline_reflection_check's c_atomic_texture case, of a language Metal 3.0 has not
# (tools/oracle/oracle_reflection_31.metal); never committed.
ORACLE_REFLECTION_31_LIB = $(BUILD)/oracle_reflection_31.metallib

$(ORACLE_REFLECTION_31_LIB): tools/oracle/oracle_reflection_31.metal Makefile
	@mkdir -p $(@D)
	xcrun -sdk macosx metal -c -std=metal3.1 -mmacosx-version-min=26.0 tools/oracle/oracle_reflection_31.metal \
		-o $(BUILD)/oracle_reflection_31.air
	xcrun -sdk macosx metallib $(BUILD)/oracle_reflection_31.air -o $@

# The same reader over modules built for macOS 10.12, of LLVM 3.5's metadata nodes
# (tests/air_arguments_old.metal).
AIR_OLD_LIB = $(BUILD)/air_arguments_old.metallib

$(AIR_OLD_LIB): tests/air_arguments_old.metal Makefile
	@mkdir -p $(@D)
	xcrun -sdk macosx metal -c -std=macos-metal1.2 -mmacosx-version-min=10.12 \
		tests/air_arguments_old.metal -o $(BUILD)/air_arguments_old.air
	xcrun -sdk macosx metallib $(BUILD)/air_arguments_old.air -o $@

# The same reader, and pipelines' reflection, over modules built for macOS 11, whose AIR names no
# air.address_space on a buffer argument (tests/air_arguments_11.metal).
AIR_11_LIB = $(BUILD)/air_arguments_11.metallib

$(AIR_11_LIB): tests/air_arguments_11.metal Makefile
	@mkdir -p $(@D)
	xcrun -sdk macosx metal -c -std=macos-metal2.3 -mmacosx-version-min=11.0 \
		tests/air_arguments_11.metal -o $(BUILD)/air_arguments_11.air
	xcrun -sdk macosx metallib $(BUILD)/air_arguments_11.air -o $@

# The repo's committed metallibs, which Apple's compiler built from our own sources: the reader
# finds every function of them (tests/air_arguments_test.cpp).
COMMITTED_METALLIBS = ../../tests/suite/fixtures/shaders:../../crates/reims-vgpu-vulkan/metallibs

AIR_TESTOBJS = $(BUILD)/obj/$(ARCH)/core/RgfxAirArguments.o \
	$(BUILD)/obj/$(ARCH)/core/RgfxFunctionConstants.o

$(AIR_TESTBIN): $(AIR_TESTOBJS) tests/air_arguments_test.cpp $(HEADERS) Makefile
	@mkdir -p $(@D)
	$(CXX) -arch $(ARCH) $(CXXFLAGS) $(AIR_TESTOBJS) tests/air_arguments_test.cpp -o $@

# --- the device's base-class check (x86_64, Rosetta) --------------------------
# Loads the bundle and checks, at run time, that every creator of the device's base classes is
# answered by the device or listed as left to the base (tests/inherited_tests.mm).

$(INHERIT_TESTBIN): tests/inherited_tests.mm Makefile
	@mkdir -p $(@D)
	$(CXX) -arch $(ARCH) $(CXXFLAGS) $(OBJCXXFLAGS) tests/inherited_tests.mm -framework Foundation \
		-o $@

# --- the protocol members' check (x86_64, Rosetta) ----------------------------
# Loads the bundle and checks every member of every Metal protocol its classes adopt, with each
# one's SPI counterpart and the members only older guests' protocols carry
# (tests/protocol_members_releases.txt), against the register tests/protocol_members.txt,
# reading the members' bodies from src/: a member the
# register does not list, or one listed as carried out whose body does nothing or answers a
# constant, fails (tests/protocol_members_tests.mm).

$(MEMBERS_TESTBIN): tests/protocol_members_tests.mm Makefile
	@mkdir -p $(@D)
	$(CXX) -arch $(ARCH) $(CXXFLAGS) $(OBJCXXFLAGS) tests/protocol_members_tests.mm \
		-framework Foundation -o $@

# --- the video driver's H.264 parameter-set check (x86_64, Rosetta) ----------

$(VIDEO_TESTBIN): $(BUILD)/obj/$(ARCH)/core/RgfxVideoH264.o tests/video_h264_tests.cpp \
	tests/video_h264_cases.hpp $(HEADERS) Makefile
	@mkdir -p $(@D)
	$(CXX) -arch $(ARCH) $(CXXFLAGS) -I tests $(BUILD)/obj/$(ARCH)/core/RgfxVideoH264.o \
		tests/video_h264_tests.cpp -o $@

# --- the video driver's HEVC parameter-set check (x86_64, Rosetta) ----------

$(VIDEO_H265_TESTBIN): $(BUILD)/obj/$(ARCH)/core/RgfxVideoH265.o tests/video_h265_tests.cpp \
	tests/video_h265_cases.hpp $(HEADERS) Makefile
	@mkdir -p $(@D)
	$(CXX) -arch $(ARCH) $(CXXFLAGS) -I tests $(BUILD)/obj/$(ARCH)/core/RgfxVideoH265.o \
		tests/video_h265_tests.cpp -o $@

# --- the video driver's entry points over a stand-in for the host's sessions (x86_64, Rosetta) ----
# The driver logs through the bundle's log object (src/RgfxLog.cpp).

VIDEO_DRIVER_TESTOBJS = $(VIDEO_OBJ_ARCH) $(BUILD)/obj/$(ARCH)/core/RgfxLog.o

$(VIDEO_DRIVER_TESTBIN): $(VIDEO_DRIVER_TESTOBJS) tests/video_driver_tests.cpp $(HEADERS) Makefile
	@mkdir -p $(@D)
	$(CXX) -arch $(ARCH) $(CXXFLAGS) -I tests $(VIDEO_DRIVER_TESTOBJS) tests/video_driver_tests.cpp \
		-framework IOSurface -framework CoreFoundation -o $@

test: $(SER_TESTBIN) $(AIR_TESTBIN) $(AIR_LIB) $(AIR_OLD_LIB) $(AIR_11_LIB) $(AIR_REFLECTED) $(ORACLE_REFLECTION_LIB) \
	$(ORACLE_OVERLAP_LIB) $(ORACLE_FNREFLECT_LIB) $(STAGE_ATTRIBUTES_LIB) $(PREDICATED_ARGUMENTS_LIB) \
	$(UNDECIDED_LOCATIONS_LIB) \
	$(SHELL_TESTBIN) $(CI_KERNEL) $(STITCHABLE) $(STITCH_CONSTANTS) $(PATCH_LIB) $(STAGE_IN_ORACLE_LIB) \
	$(INHERIT_TESTBIN) \
	$(MEMBERS_TESTBIN) $(VIDEO_TESTBIN) $(VIDEO_H265_TESTBIN) $(VIDEO_DRIVER_TESTBIN) $(BUNDLEBIN)
	bash tests/bundle_exit_check.sh $(BUNDLEBIN) $(CORE_OBJ_ARCH) $(SHELL_OBJ_ARCH) $(VIDEO_OBJ_ARCH)
	arch -$(ARCH) $(SER_TESTBIN)
	RGFX_AIR_ARGUMENTS=$(AIR_LIB) RGFX_AIR_MODULE=$(AIR_MODULE) RGFX_AIR_ARGUMENTS_OLD=$(AIR_OLD_LIB) \
		RGFX_AIR_ARGUMENTS_11=$(AIR_11_LIB) RGFX_ORACLE_REFLECTION=$(ORACLE_REFLECTION_LIB) \
		RGFX_COMMITTED_METALLIBS=$(COMMITTED_METALLIBS) arch -$(ARCH) $(AIR_TESTBIN)
	RGFX_CI_KERNEL=$(CI_KERNEL) RGFX_STITCHABLE=$(STITCHABLE) RGFX_STITCH_CONSTANTS=$(STITCH_CONSTANTS) \
		RGFX_AIR_ARGUMENTS=$(AIR_LIB) RGFX_AIR_ARGUMENTS_11=$(AIR_11_LIB) RGFX_LIBRARY_FILE=$(LIBRARY_FILE) \
		RGFX_AIR_REFLECTED=$(AIR_REFLECTED) RGFX_ORACLE_REFLECTION=$(ORACLE_REFLECTION_LIB) \
		RGFX_PATCH=$(PATCH_LIB) RGFX_ORACLE_OVERLAP=$(ORACLE_OVERLAP_LIB) \
		RGFX_STAGE_IN_ORACLE=$(STAGE_IN_ORACLE_LIB) RGFX_ORACLE_FNREFLECT=$(ORACLE_FNREFLECT_LIB) \
		RGFX_STAGE_ATTRIBUTES=$(STAGE_ATTRIBUTES_LIB) RGFX_STAGE_ATTRIBUTES_SRC=tests/stage_attributes.metal \
		RGFX_PREDICATED_ARGUMENTS=$(PREDICATED_ARGUMENTS_LIB) RGFX_UNDECIDED_LOCATIONS=$(UNDECIDED_LOCATIONS_LIB) \
		RGFX_ORACLE_FNREFLECT_SRC=tools/oracle/oracle_fnreflect.metal $(SHELL_TESTBIN)
	arch -$(ARCH) $(INHERIT_TESTBIN) $(BUNDLEBIN)
	arch -$(ARCH) $(MEMBERS_TESTBIN) $(BUNDLEBIN) tests/protocol_members.txt \
		tests/protocol_members_releases.txt src
	arch -$(ARCH) $(VIDEO_TESTBIN)
	arch -$(ARCH) $(VIDEO_H265_TESTBIN)
	arch -$(ARCH) $(VIDEO_DRIVER_TESTBIN)
	bash tests/bundle_files_check.sh $(BUNDLEBIN)
	bash tests/bundle_sync_check.sh $(BUNDLEBIN) src $(WRAP_OBJ_NAT) $(CORE_OBJ_NAT)
	bash tests/bundle_log_check.sh $(BUNDLEBIN) Info.plist src/RgfxLog.hpp

# --- the wire round trip against the host's library (native arch, this Mac's GPU) ----------
# The bundle's own ring code (the serialiser, a queue's rings, reader and completion thread, a
# command buffer's commit) against the host's library through its C API, with a stand-in for the
# kext (tests/peer_tests.mm), recording the wire's traffic against tests/peer_wire2.trace. Never in
# `make test`: it runs on the GPU, so build it first (`make peer-test-build`), then run
# `make peer-test` holding the Mac's GPU lock. DIST is the tree `cargo xtask dist` wrote (release):
# its archive is linked as QEMU links it, with its pkg-config file's private libraries.
# RGFX_PEER_RECORD=1 records the trace again.
DIST          ?= ../../target/dist-vulkan
PEER_TESTBIN   = $(BUILD)/peer_tests
PEER_TRACE     = tests/peer_wire2.trace
PEER_LIBS      = $(shell sed -n 's/^Libs.private: //p' $(DIST)/lib/pkgconfig/reims-vgpu.pc)

$(PEER_TESTBIN): $(CORE_OBJ_NAT) $(WRAP_OBJ_NAT) tests/peer_tests.mm $(HEADERS) \
	$(DIST)/lib/libreims_vgpu.a $(DIST)/include/reims_vgpu.h Makefile
	@mkdir -p $(@D)
	$(CXX) -arch $(NATIVE) $(CXXFLAGS) $(OBJCXXFLAGS) $(PROTOCOL_QUIET) -I $(DIST)/include \
		$(CORE_OBJ_NAT) $(WRAP_OBJ_NAT) tests/peer_tests.mm $(DIST)/lib/libreims_vgpu.a \
		$(FRAMEWORKS) -framework CoreVideo $(PEER_LIBS) -o $@

peer-test-build: $(PEER_TESTBIN)

peer-test: $(PEER_TESTBIN)
	$(PEER_TESTBIN) $(PEER_TRACE)

# --- the loadable bundle (x86_64, device shell included) ---------------------
# The IOGPUMetalDevice superclass is linked two-level against IOGPU (PRIVFW), so
# dyld auto-loads IOGPU and resolves the device shell's base class when Metal
# dlopens the plugin in the guest. The remaining private Metal SPI (instance
# methods, resolved via the base at runtime) stays -undefined dynamic_lookup.

bundle: $(BUNDLEBIN)

# arm64e: the linker's message-send stubs (__objc_stubs) in their small form, which branch to
# objc_msgSend's own stub. In their default form each loads objc_msgSend from a slot of the
# authenticated GOT of its own, and ld (ld-27106.1) then makes two slots for objc_msgSend, one
# for those stubs and one for its call stub, and gives each its slot in either order from one
# link to the next: the bundle's bytes, and so RgfxGate's, alternated between two builds of one
# tree (measured on the M3 Max, 2026-11-08; docs/lessons.md). One slot, one build.
BUNDLE_LDFLAGS = $(if $(filter arm64e,$(ARCH)),-Wl$(comma)-objc_stubs_small)
comma := ,

$(BUNDLEBIN): $(CORE_OBJ_ARCH) $(SHELL_OBJ_ARCH) $(VIDEO_OBJ_ARCH) Makefile
	@mkdir -p $(@D)
	$(CXX) -arch $(ARCH) $(CXXFLAGS) $(OBJCXXFLAGS) $(BUNDLE_LDFLAGS) \
		-bundle -undefined dynamic_lookup $(FRAMEWORKS) $(PRIVFW) \
		$(CORE_OBJ_ARCH) $(SHELL_OBJ_ARCH) $(VIDEO_OBJ_ARCH) -o $@
	@echo "$(notdir $@): $$(cat src/*.mm | grep -o 'RGFX_UNSUPPORTED[A-Z_]*(' | wc -l | tr -d ' ')" \
	      "members marked RGFX_UNSUPPORTED (src/RgfxUnsupported.h)"

# --- the installable bundle: the directory Metal loads from /Library/GPUBundles -----
# Contents/Info.plist and Contents/MacOS/reims-graphics, ad-hoc signed: WindowServer is a
# platform process, whose Library Validation refuses an unsigned bundle (dlopen error 71),
# whatever amfi_get_out_of_my_way says; an ad-hoc signature it takes. The tarball is the same
# directory, as the guest install takes it.
#
# Contents/Resources/wire-version-N, empty, names the wire's version the bundle speaks
# (RGFX_VERSION in the wire header): the kext registers its accelerator only for a bundle
# carrying the marker of its own version (guest/kext/src/RgfxBundlePaths.hpp; design.md, "The
# wire's version"). It lies under the signature, as every file of the bundle does.

PACKAGE    = $(BUILD)/package/reims-graphics.bundle
PACKAGETGZ = $(BUILD)/reims-graphics.bundle.tgz
WIRE_H       = ../../crates/reims-vgpu-graphics/include/reims_graphics_wire.h
WIRE_VERSION = $(shell sed -n 's/^\#define RGFX_VERSION \([0-9][0-9]*\)$$/\1/p' $(WIRE_H))

package: $(PACKAGETGZ)

# PACKAGE_ARCHS: the architectures the installable bundle's executable carries; one is ARCH's
# build itself, more are each built with ARCH set to it and joined with lipo. An arm64 guest
# runs arm64e system processes (WindowServer) and arm64 apps, and Metal loads the bundle into
# both, so its package is PACKAGE_ARCHS="arm64e arm64".
PACKAGE_ARCHS ?= $(ARCH)
ifeq ($(words $(PACKAGE_ARCHS)),1)
PACKAGEBIN = $(BUNDLEBIN)
else
PACKAGEBIN = $(BUILD)/reims-graphics.universal
PACKAGESLICES = $(foreach a,$(PACKAGE_ARCHS),$(BUILD)/reims-graphics-$(a).bundle)
.PHONY: package-slices
package-slices:
	@for a in $(PACKAGE_ARCHS); do \
		$(MAKE) ARCH=$$a PACKAGE_ARCHS=$$a BUILD=$(BUILD) BUNDLEBIN=$(BUILD)/reims-graphics-$$a.bundle bundle || exit 1; \
	done
$(PACKAGEBIN): package-slices
	lipo -create $(PACKAGESLICES) -output $@
endif

$(PACKAGETGZ): $(PACKAGEBIN) Info.plist $(WIRE_H) Makefile
	@test -n "$(WIRE_VERSION)" || { echo "no RGFX_VERSION in $(WIRE_H)"; exit 1; }
	rm -rf $(PACKAGE)
	@mkdir -p $(PACKAGE)/Contents/MacOS $(PACKAGE)/Contents/Resources
	plutil -lint Info.plist > /dev/null
	cp Info.plist $(PACKAGE)/Contents/Info.plist
	cp $(PACKAGEBIN) $(PACKAGE)/Contents/MacOS/reims-graphics
	: > $(PACKAGE)/Contents/Resources/wire-version-$(WIRE_VERSION)
	codesign --force --sign - $(PACKAGE)
	codesign -v --strict $(PACKAGE)
	COPYFILE_DISABLE=1 tar -C $(dir $(PACKAGE)) -czf $@ reims-graphics.bundle
	@echo "$(notdir $@): $(PACKAGE), ad-hoc signed"

# --- the guest's correctness sweep (tools/sweep; sweep.m says what it checks) ----------------
# One binary for ARCH (x86_64, the guest's; ARCH=arm64 for an Apple-silicon Mac), run in a guest
# or on a Mac, never in `make test`: it needs the system frameworks on a real device.
SWEEP_SRCS = $(wildcard tools/sweep/*.m)
SWEEP_FW   = -framework Foundation -framework CoreGraphics -framework ImageIO -framework Metal \
	-framework MetalPerformanceShaders -framework CoreImage -framework AVFoundation \
	-framework CoreMedia -framework CoreVideo -framework VideoToolbox -framework Accelerate \
	-framework QuartzCore
SWEEPBIN   = $(BUILD)/sweep-$(ARCH)

sweep: $(SWEEPBIN)

$(SWEEPBIN): $(SWEEP_SRCS) $(wildcard tools/sweep/*.h) Makefile
	@mkdir -p $(@D)
	xcrun clang -arch $(ARCH) -isysroot $(SDK) -mmacosx-version-min=14.0 -fobjc-arc -O2 \
		-Wall -Wextra -Werror -Wno-unused-parameter $(SWEEP_SRCS) $(SWEEP_FW) -o $@

# --- the GPU benchmark (tools/bench; bench.m says what each case measures) ---------------------
# One binary for ARCH (x86_64, a guest's or an Intel Mac's; ARCH=arm64 for an Apple-silicon Mac),
# run on a real device, never in `make test`. Its shaders compile from source at run time.
BENCHBIN = $(BUILD)/bench-$(ARCH)

bench: $(BENCHBIN)

$(BENCHBIN): tools/bench/bench.m Makefile
	@mkdir -p $(@D)
	xcrun clang -arch $(ARCH) -isysroot $(SDK) -mmacosx-version-min=14.0 -fobjc-arc -O2 \
		-Wall -Wextra -Werror -Wno-unused-parameter tools/bench/bench.m -framework Foundation \
		-framework Metal -framework MetalPerformanceShaders -framework QuartzCore -framework AppKit -o $@

# --- what resources cost (tools/res; res.m says what each case measures) -----------------------
# One binary for ARCH (x86_64, a guest's or an Intel Mac's; ARCH=arm64 for an Apple-silicon Mac),
# run on a real device, never in `make test`.
RESBIN = $(BUILD)/res-$(ARCH)

res: $(RESBIN)

$(RESBIN): tools/res/res.m Makefile
	@mkdir -p $(@D)
	xcrun clang -arch $(ARCH) -isysroot $(SDK) -mmacosx-version-min=14.0 -fobjc-arc -O2 \
		-Wall -Wextra -Werror -Wno-unused-parameter tools/res/res.m -framework Foundation \
		-framework Metal -framework IOSurface -o $@

# --- the command buffer's fixed cost and latency (tools/submit_probe.m says what each case does) --
# One binary for ARCH (x86_64, a guest's or an Intel Mac's; ARCH=arm64 for an Apple-silicon Mac),
# run on a real device, never in `make test`.
SUBMITBIN = $(BUILD)/submit_probe-$(ARCH)

submit-probe: $(SUBMITBIN)

$(SUBMITBIN): tools/submit_probe.m Makefile
	@mkdir -p $(@D)
	xcrun clang -arch $(ARCH) -isysroot $(SDK) -mmacosx-version-min=14.0 -fobjc-arc -O2 \
		-Wall -Wextra -Werror -Wno-unused-parameter tools/submit_probe.m -framework Foundation \
		-framework Metal -o $@

# --- the uploads into Managed resources (tools/upload_probe.m says what each case measures) -------
# One binary for ARCH (x86_64, a guest's or an Intel Mac's; ARCH=arm64 for an Apple-silicon Mac),
# run on a real device, never in `make test`.
UPLOADBIN = $(BUILD)/upload_probe-$(ARCH)

upload-probe: $(UPLOADBIN)

$(UPLOADBIN): tools/upload_probe.m Makefile
	@mkdir -p $(@D)
	xcrun clang -arch $(ARCH) -isysroot $(SDK) -mmacosx-version-min=14.0 -fobjc-arc -O2 \
		-Wall -Wextra -Werror -Wno-unused-parameter tools/upload_probe.m -framework Foundation \
		-framework Metal -o $@

# --- a texture shared across processes (tools/shtex_probe.m says what each case checks) -----------
# One binary for ARCH (x86_64, a guest's or an Intel Mac's; ARCH=arm64 for an Apple-silicon Mac),
# run on a real device, never in `make test`.
SHTEXBIN = $(BUILD)/shtex_probe-$(ARCH)

shtex-probe: $(SHTEXBIN)

$(SHTEXBIN): tools/shtex_probe.m Makefile
	@mkdir -p $(@D)
	xcrun clang -arch $(ARCH) -isysroot $(SDK) -mmacosx-version-min=14.0 -fobjc-arc -O2 \
		-Wall -Wextra -Werror -Wno-unused-parameter tools/shtex_probe.m -framework Foundation \
		-framework Metal -framework IOSurface -o $@

# --- the CPU's encodes into indirect command buffers and writes into IOSurfaces (tools/icb_probe.m
# says what each case measures). One binary for ARCH, run on a real device, never in `make test`.
ICBBIN = $(BUILD)/icb_probe-$(ARCH)

icb-probe: $(ICBBIN)

$(ICBBIN): tools/icb_probe.m Makefile
	@mkdir -p $(@D)
	xcrun clang -arch $(ARCH) -isysroot $(SDK) -mmacosx-version-min=14.0 -fobjc-arc -O2 \
		-Wall -Wextra -Werror -Wno-unused-parameter tools/icb_probe.m -framework Foundation \
		-framework Metal -framework IOSurface -o $@

# --- the hot encoder calls' cost (tools/encode_probe.m says what each case measures) -------------
# One binary for ARCH (x86_64, a guest's or an Intel Mac's; ARCH=arm64 for an Apple-silicon Mac),
# run on a real device, never in `make test`.
ENCODEBIN = $(BUILD)/encode_probe-$(ARCH)

encode-probe: $(ENCODEBIN)

$(ENCODEBIN): tools/encode_probe.m Makefile
	@mkdir -p $(@D)
	xcrun clang -arch $(ARCH) -isysroot $(SDK) -mmacosx-version-min=14.0 -fobjc-arc -O2 \
		-Wall -Wextra -Werror -Wno-unused-parameter tools/encode_probe.m -framework Foundation \
		-framework Metal -o $@

# --- the shader and pipeline costs (tools/pipelines; pipelines.m says what each case measures) --
# One binary for ARCH (x86_64, a guest's or an Intel Mac's; ARCH=arm64 for an Apple-silicon Mac),
# run on a real device, never in `make test`. Its source compiles in (pipelines_source.h, from
# pipelines.metal) for newLibraryWithSource:, and into PIPELINES_LIBS metallibs, one nonce each,
# for newLibraryWithData:. Their nonces come from PIPELINES_SEED (default: the build's clock), so
# a new build's metallibs are new to every cache. Built for macOS 14 and Metal 3.0, which a
# guest's Metal on macOS 26 takes.
PIPELINESBIN  = $(BUILD)/pipelines-$(ARCH)
PIPELINES_LIBDIR = $(BUILD)/pipelines-metallibs
PIPELINES_LIBS = 8
PIPELINES_SEED ?= $(shell expr $$(date +%s) % 1000000 + 1000000)

pipelines: $(PIPELINESBIN) $(PIPELINES_LIBDIR)/stamp

$(BUILD)/pipelines_source.h: tools/pipelines/pipelines.metal Makefile
	@mkdir -p $(@D)
	cd tools/pipelines && xxd -i -n pipelines_metal pipelines.metal > ../../$@

$(PIPELINESBIN): tools/pipelines/pipelines.m $(BUILD)/pipelines_source.h Makefile
	@mkdir -p $(@D)
	xcrun clang -arch $(ARCH) -isysroot $(SDK) -mmacosx-version-min=14.0 -fobjc-arc -O2 \
		-Wall -Wextra -Werror -Wno-unused-parameter -I $(BUILD) tools/pipelines/pipelines.m \
		-framework Foundation -framework Metal -o $@

$(PIPELINES_LIBDIR)/stamp: tools/pipelines/pipelines.metal Makefile
	@mkdir -p $(@D)
	rm -f $(@D)/pipelines-*.metallib $(@D)/*.air
	for i in $$(seq 0 $$(($(PIPELINES_LIBS) - 1))); do \
		n=$$(( $(PIPELINES_SEED) * 16 + i )); \
		xcrun -sdk macosx metal -c -std=metal3.0 -mmacosx-version-min=14.0 -DRGFX_NONCE=$${n}u \
			tools/pipelines/pipelines.metal -o $(@D)/p$$i.air && \
		xcrun -sdk macosx metallib $(@D)/p$$i.air -o $(@D)/pipelines-$$(printf %02d $$i).metallib || exit 1; \
	done
	rm -f $(@D)/*.air
	echo $(PIPELINES_SEED) > $@

# --- the stitchable reflection's check against Metal's own (tools/reflection_check.mm) ---------
# One binary for ARCH (x86_64, a guest's or an Intel Mac's; ARCH=arm64 for an Apple-silicon Mac),
# run on a real device, never in `make test`: it compares with Metal's own compiler's answers.
# Its library is the AIR reader's (AIR_LIB).
REFLECTBIN = $(BUILD)/reflection_check-$(ARCH)

reflection-check: $(REFLECTBIN) $(AIR_LIB)

$(REFLECTBIN): tools/reflection_check.mm src/RgfxReflection.mm src/RgfxAirArguments.cpp $(HEADERS) Makefile
	@mkdir -p $(@D)
	$(CXX) -arch $(ARCH) $(CXXFLAGS) $(OBJCXXFLAGS) -mmacosx-version-min=14.0 -x objective-c++ \
		tools/reflection_check.mm src/RgfxReflection.mm -x c++ src/RgfxAirArguments.cpp \
		-framework Foundation -framework Metal -o $@

# --- pipelines' reflection's check against Metal's own (tools/pipeline_reflection_check.mm) ------
# As reflection-check: one binary for ARCH, run on a real device (an x86 Mac's: the answers the
# bundle's are of), never in `make test`. Its library is ORACLE_REFLECTION_LIB.
PIPEREFLECTBIN = $(BUILD)/pipeline_reflection_check-$(ARCH)

pipeline-reflection-check: $(PIPEREFLECTBIN) $(ORACLE_REFLECTION_LIB) $(ORACLE_REFLECTION_31_LIB)

$(PIPEREFLECTBIN): tools/pipeline_reflection_check.mm tools/oracle/oracle_walk.h src/RgfxPipelineReflection.mm \
	src/RgfxAirArguments.cpp $(HEADERS) Makefile
	@mkdir -p $(@D)
	$(CXX) -arch $(ARCH) $(CXXFLAGS) $(OBJCXXFLAGS) -I tools -mmacosx-version-min=26.0 -x objective-c++ \
		tools/pipeline_reflection_check.mm src/RgfxPipelineReflection.mm -x c++ src/RgfxAirArguments.cpp \
		-framework Foundation -framework Metal -o $@

clean:
	rm -rf $(BUILD)

.PHONY: all test bundle package sweep bench pipelines res submit-probe upload-probe shtex-probe icb-probe \
	encode-probe reflection-check pipeline-reflection-check peer-test peer-test-build clean

