# Stub DLLs carrying MEASURED vendor export names, for the hook fixtures.
#
# NOT SHIPPED, and structurally so: 12_BUILD publishes FrameLedger.App and
# FrameLedger.Agent only, neither references anything here, and these are built
# under FL_BUILD_TOOLS. They exist so `hook-harness` can put a module named
# sl.interposer.dll into a process and see whether the Overlay finds it.
#
# THE OUTPUT NAMES ARE THE VENDOR'S; THE VERSION BLOCKS ARE OURS. A file called
# sl.interposer.dll that announced itself as NVIDIA's would be a lie sitting in
# our build tree, and 19_SAFETY's whole posture is that our binaries say plainly
# what they are. fl_add_version_resource is given the real output filename so the
# resource matches the file rather than the CMake target name.
#
# They are NOT added to tools/versioninfo-check.ps1's shipped-binary list: that
# gate asserts the three binaries we ship carry a version block, and a fixture is
# not one of them.

function(fl_add_vendor_stub target output source)
    add_library(${target} SHARED ${source})
    set_target_properties(${target} PROPERTIES OUTPUT_NAME "${output}")
    # fl_hook_inventory.h (via fl_dxgi_vtable's sibling include dir) binds the
    # exported name to the Overlay's; fl_streamline_headers and fl_fidelityfx_headers
    # supply the vendor signatures so the stub and the detour cannot disagree about
    # the ABI. Both are INTERFACE include targets and link nothing.
    target_link_libraries(${target} PRIVATE FrameLedger.Shm fl_native_flags fl_streamline_headers
        fl_fidelityfx_headers fl_fsr3_host_headers)
    target_include_directories(${target} PRIVATE
        "${CMAKE_SOURCE_DIR}/FrameLedger.Overlay/include"
        "${CMAKE_CURRENT_SOURCE_DIR}")
    target_compile_definitions(${target} PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX)
    fl_add_version_resource(${target} "FrameLedger test fixture - not a vendor library" "${output}.dll")
endfunction()

# The module the inventory names. Its slEvaluateFeature is the one that must be
# hooked.
fl_add_vendor_stub(fl_stub_sl_interposer "sl.interposer" stub_sl_interposer.cpp)

# The decoy: same symbol, different module, must NOT be hooked. Without it,
# "module-scoped resolution" is a property no test can falsify.
fl_add_vendor_stub(fl_stub_sl_common "sl.common" stub_sl_common.cpp)

# The WRONG-GENERATION fixture: right module name, right symbol name, Streamline
# 1.x ABI. Must NOT be hooked either, for a different reason than the decoy —
# scoping cannot see it, because the module name is correct.
#
# ITS OWN SUBDIRECTORY, and that is forced rather than tidy: its output name is
# also `sl.interposer.dll`, so it would collide with the SL2 stub in a shared
# output directory. Separate directories also mean a test can load exactly one of
# them by absolute path, which is what makes the two cases independent —
# GetModuleHandleExW resolves by name, so a process holding both would answer
# whichever loaded first and the fixtures would silently test each other.
fl_add_vendor_stub(fl_stub_sl_interposer_v1 "sl.interposer" stub_sl_interposer_v1.cpp)
# ARCHIVE_ too, and it is not optional: on Windows a SHARED library emits the DLL
# under RUNTIME_ and the IMPORT LIBRARY under ARCHIVE_. Redirecting only the first
# two leaves both targets generating tools/vendor-stubs/sl.interposer.lib, and
# ninja refuses the configure with "multiple rules generate" -- which is the
# generator catching a collision the DLL rename hid.
set_target_properties(fl_stub_sl_interposer_v1 PROPERTIES
    RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/sl1"
    LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/sl1"
    ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/sl1")

# --- AMD FidelityFX (ffx-api) -------------------------------------------------
#
# THREE LEAVES AND THE LOADER, ALL FOUR HOOKED. The four modules export the same
# five ffx-api names (docs/vendor-exports.json). The loader was first a decoy that
# must never be hooked; three loader-shipping titles at FSR then produced zero
# dispatches at any leaf export (2026-09-04), so it is a row, and this stub forwards
# to the two 2.x leaves through their DIRECT entry -- the measured shape -- so "a
# dispatch through the loader is counted once with everything hooked" is a property
# the [ffx] tests can falsify rather than a comment in fl_hook_inventory.h.
#
# Distinct output names, so no output-directory games are needed here.
fl_add_vendor_stub(fl_stub_ffx_dx12 "amd_fidelityfx_dx12" stub_amd_fidelityfx_dx12.cpp)
fl_add_vendor_stub(fl_stub_ffx_upscaler "amd_fidelityfx_upscaler_dx12" stub_amd_fidelityfx_upscaler_dx12.cpp)
fl_add_vendor_stub(fl_stub_ffx_fg "amd_fidelityfx_framegeneration_dx12" stub_amd_fidelityfx_framegeneration_dx12.cpp)
fl_add_vendor_stub(fl_stub_ffx_loader "amd_fidelityfx_loader_dx12" stub_amd_fidelityfx_loader_dx12.cpp)

# --- AMD FidelityFX, the FSR 3.0 HOST API (Cyberpunk 2077's ffx_fsr3_x64.dll) ---
#
# NOT A LEAF: named exports, the 3.0 host's own descriptor, one row
# (ffxFsr3ContextDispatchUpscale). Exports exactly the four names SpeaksFsr3Host
# probes for, through the vendored header's own FFX_API (dllexport, no switch) -- and
# is therefore hookinventory-check Pass C's SECOND positive control for the export
# parser, beside the ffx-api leaf.
fl_add_vendor_stub(fl_stub_ffx_fsr3_host "ffx_fsr3_x64" stub_ffx_fsr3_x64.cpp)

# --- NVIDIA NVAPI, for the spike of Reflex route D (docs/spike-notes §31, owner decision D132) ---
#
# A stand-in for nvapi64.dll: the real module's one way in (nvapi_QueryInterface) answering the two getters route D
# would call, with knobs for what a driver cannot be made to do on demand -- a missing getter, a report of nothing,
# an error, a call that does not return for seconds. Used by fl-probe-reflex only; the Overlay is not involved and
# no inventory row names it, so it takes the vendored NVAPI headers (the include directory alone -- nvapi64.lib is
# not linked) and nothing of the hook inventory.
#
# ITS OWN SUBDIRECTORY for its outputs: a file called nvapi64.lib beside the other stubs' import libraries would be
# one careless link line away from being taken for the vendored one.
fl_add_vendor_stub(fl_stub_nvapi64 "nvapi64" stub_nvapi64.cpp)
target_include_directories(fl_stub_nvapi64 SYSTEM PRIVATE "${CMAKE_SOURCE_DIR}/third_party/nvapi")
set_target_properties(fl_stub_nvapi64 PROPERTIES
    RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/nvapi"
    LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/nvapi"
    ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/nvapi")
