Automate installing the API library for testing

To run dEQP tests we previously either had to install the Vulkan Loader
and configure it to find the SwiftShader Vulkan ICD, or manually copy
over the library.

This change introduces the SWIFTSHADER_VULKAN_API_LIBRARY_INSTALL_PATH
environment variable, which can be set to a path where the API library
should be copied to. When used with dEQP it ensures we always test with
the last build, instead of the path provided in the JSON file used by
the Vulkan loader.

Note this is only meant as a convenience for SwiftShader development.
It shouldn't be used as a substitute for installing the ICD for
system-wide usage. The latter should probably use CMAKE_INSTALL_PREFIX
instead, but there are no known use cases yet for installing SwiftShader
as the system-level driver, through the CMake build.

Bug: b/149486511
Change-Id: I5ec9d669967ba0c05a763263605ee0ad5cfd505e
Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/53768
Presubmit-Ready: Nicolas Capens <nicolascapens@google.com>
Tested-by: Nicolas Capens <nicolascapens@google.com>
Kokoro-Result: kokoro <noreply+kokoro@google.com>
Reviewed-by: Alexis Hétu <sugoi@google.com>
diff --git a/src/Vulkan/CMakeLists.txt b/src/Vulkan/CMakeLists.txt
index 2c1ce24..6c9e696 100644
--- a/src/Vulkan/CMakeLists.txt
+++ b/src/Vulkan/CMakeLists.txt
@@ -193,6 +193,12 @@
 
 set_shared_library_export_map(vk_swiftshader ${CMAKE_CURRENT_SOURCE_DIR})
 
+# The name of the application-facing library. Usually this would be the Vulkan Loader,
+# which then loads the Installable Client Driver (ICD), see:
+# https://www.lunarg.com/tutorial-overview-of-vulkan-loader-layers/
+# But SwiftShader can also be loaded directlty by the application. This enables using
+# SwiftShader without requiring a Loader to be installed, and simplifies testing and
+# debugging. ${VULKAN_API_LIBRARY_NAME} is the name of this drop-in API library.
 if(WIN32)
     set(VULKAN_API_LIBRARY_NAME "vulkan-1.dll")
 elseif(LINUX)
@@ -217,6 +223,17 @@
     COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:vk_swiftshader> ${CMAKE_BINARY_DIR}/bin/${VULKAN_API_LIBRARY_NAME}
 )
 
+# The SWIFTSHADER_VULKAN_API_LIBRARY_INSTALL_PATH environment variable can be set to a path
+# where we'd like the drop-in API library to be installed. For example for testing with dEQP
+# this could be something like C:/src/deqp/build/external/vulkancts/modules/vulkan/Debug/
+if(DEFINED ENV{SWIFTSHADER_VULKAN_API_LIBRARY_INSTALL_PATH})
+    add_custom_command(
+        TARGET vk_swiftshader
+        POST_BUILD
+        COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:vk_swiftshader> $ENV{SWIFTSHADER_VULKAN_API_LIBRARY_INSTALL_PATH}${VULKAN_API_LIBRARY_NAME}
+    )
+endif()
+
 # The vk_swiftshader_icd.json manifest file will point to ICD_LIBRARY_PATH.
 # Set ICD_LIBRARY_PATH to be a relative path similar to "./libvk_swiftshader.so", so both files can be moved.
 # A relative path is relative to the manifest file.