import  vk   = libvulkan-loader%lib{vulkan}
import  util = libmoltenvk%lib{mvk-direct-util}
import! [metadata, rule_hint=cxx.link] dylib_meta = libmoltenvk%libs{MoltenVK}

include ../shared/

# Creates a real VkDevice, which needs actual Metal. testscript probes
# for it first (see driver.cpp) and falls back to checking just the
# probe's own diagnostic when running on a host without Metal (e.g. CI).
#
# No adhoc prerequisite on libs{MoltenVK} to force it to build (dropped
# a plain, ordinary import previously used only for that): a plain
# import of a sys:-resolved library (e.g. CI's test-installed phase)
# constructs a reference without validating it, and using it as an
# adhoc prerequisite needs build2 to independently match a rule to
# build/update that target on its own, which no rule can do for a
# sys:-resolved library (confirmed by testing against a real installed
# libmoltenvk). libmoltenvk-tests already depends on libmoltenvk as a
# whole package, so a normal project build already updates the dylib.
# This only gives up detecting a dylib-only change with no other edits
# as reason to rerun this specific test, same tradeoff as
# ../icd/buildfile.
#
exe{driver-direct-driver-loading}: \
  {hxx ixx txx cxx}{**} $vk $util ../shared/libue{hw-probe} testscript
exe{driver-direct-driver-loading}: install = false

cxx.poptions += "-DMVK_DYLIB_PATH=\"$posix_string($($dylib_meta: libmoltenvk.dylib_path))\"" \
                "-DMVK_TEST_DYLIB_PATH=\"$posix_string($($dylib_meta: libmoltenvk.test_dylib_path))\"" \
                "-I$src_root/shared"

# dlopen(MVK_DYLIB_PATH/MVK_TEST_DYLIB_PATH) below needs libMoltenVK.dylib's
# own dependencies (e.g. libcpp-base64) resolvable via @rpath, which dyld
# looks up against every loaded image's LC_RPATH, including this main
# executable's, even though it never links libmoltenvk directly.
# Same-configuration builds get this for free (cc adds an absolute,
# build-tree rpath automatically). A separately installed libmoltenvk
# (e.g. CI's test-installed phase, sys:) does not, hence this explicit
# one, taken from test_dylib_path since it is always absolute.
#
bin.rpath += $directory($($dylib_meta: libmoltenvk.test_dylib_path))
