From 17b80f080d2904431978805f8af24b35b15eece4 Mon Sep 17 00:00:00 2001
From: Junil Kim <logyourself@gmail.com>
Date: Fri, 22 Nov 2024 11:37:14 +0900
Subject: [PATCH 01/10] fix: ggml: fix vulkan-shaders-gen build

The vulkan-shaders-gen target was not being built correctly
in case of cross-compilation.
Other outputs need to be built for the cross compile target,
but vulkan-shaders-gen needs to be built for the host.
---
 ggml/src/ggml-vulkan/CMakeLists.txt           | 36 +++++++++++++++++--
 .../ggml-vulkan/vulkan-shaders/CMakeLists.txt |  6 +++-
 2 files changed, 38 insertions(+), 4 deletions(-)

diff --git a/ggml/src/ggml-vulkan/CMakeLists.txt b/ggml/src/ggml-vulkan/CMakeLists.txt
index c0ddaac827f8..797658725e16 100644
--- a/ggml/src/ggml-vulkan/CMakeLists.txt
+++ b/ggml/src/ggml-vulkan/CMakeLists.txt
@@ -1,3 +1,6 @@
+cmake_minimum_required(VERSION 3.19)
+cmake_policy(SET CMP0114 NEW)
+
 find_package(Vulkan COMPONENTS glslc REQUIRED)
 
 if (Vulkan_FOUND)
@@ -73,9 +76,32 @@ if (Vulkan_FOUND)
         add_compile_definitions(GGML_VULKAN_RUN_TESTS)
     endif()
 
-    add_subdirectory(vulkan-shaders)
-
-    set (_ggml_vk_genshaders_cmd vulkan-shaders-gen)
+    if (NOT CMAKE_CROSSCOMPILING)
+        add_subdirectory(vulkan-shaders)
+    else()
+        include(ExternalProject)
+        # Native build through ExternalProject_Add
+        ExternalProject_Add(
+            vulkan-shaders-gen
+            SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/vulkan-shaders
+            CMAKE_ARGS -DCMAKE_BUILD_TYPE=Release
+                    -DCMAKE_INSTALL_PREFIX=${CMAKE_BINARY_DIR}
+                    -DCMAKE_TOOLCHAIN_FILE=
+                    -DCMAKE_SYSROOT=
+                    -DCMAKE_C_FLAGS=-O2
+                    -DCMAKE_CXX_FLAGS=-O2
+                    -DCMAKE_FIND_ROOT_PATH_MODE_PROGRAM=NEVER
+                    -DCMAKE_FIND_ROOT_PATH_MODE_LIBRARY=NEVER
+                    -DCMAKE_FIND_ROOT_PATH_MODE_INCLUDE=NEVER
+                    -DCMAKE_C_COMPILER=gcc
+                    -DCMAKE_CXX_COMPILER=g++
+            BUILD_COMMAND ${CMAKE_COMMAND} --build .
+            INSTALL_COMMAND ${CMAKE_COMMAND} --install .
+            INSTALL_DIR ${CMAKE_BINARY_DIR}
+        )
+        ExternalProject_Add_StepTargets(vulkan-shaders-gen build install)
+    endif()
+    set (_ggml_vk_genshaders_cmd ${CMAKE_BINARY_DIR}/bin/vulkan-shaders-gen)
     set (_ggml_vk_header     ${CMAKE_CURRENT_BINARY_DIR}/ggml-vulkan-shaders.hpp)
     set (_ggml_vk_source     ${CMAKE_CURRENT_BINARY_DIR}/ggml-vulkan-shaders.cpp)
     set (_ggml_vk_input_dir  ${CMAKE_CURRENT_SOURCE_DIR}/vulkan-shaders)
@@ -87,6 +113,10 @@ if (Vulkan_FOUND)
         set(_ggml_vk_genshaders_cmd "$<TARGET_FILE_DIR:vulkan-shaders-gen>/${_ggml_vk_genshaders_cmd}")
     endif ()
 
+    if (CMAKE_CROSSCOMPILING)
+        set(_ggml_vk_shader_deps ${_ggml_vk_shader_deps} vulkan-shaders-gen-build vulkan-shaders-gen-install)
+    endif()
+
     add_custom_command(
         OUTPUT ${_ggml_vk_header}
                 ${_ggml_vk_source}
diff --git a/ggml/src/ggml-vulkan/vulkan-shaders/CMakeLists.txt b/ggml/src/ggml-vulkan/vulkan-shaders/CMakeLists.txt
index bd0c74cb1c77..8f496d6c6b2e 100644
--- a/ggml/src/ggml-vulkan/vulkan-shaders/CMakeLists.txt
+++ b/ggml/src/ggml-vulkan/vulkan-shaders/CMakeLists.txt
@@ -3,7 +3,11 @@ find_package(Vulkan COMPONENTS glslc REQUIRED)
 
 set(TARGET vulkan-shaders-gen)
 add_executable(${TARGET} vulkan-shaders-gen.cpp)
-install(TARGETS ${TARGET} RUNTIME)
+if (${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
+    install(TARGETS ${TARGET} RUNTIME)
+else()
+    install(TARGETS ${TARGET} RUNTIME DESTINATION bin)
+endif()
 target_compile_features(${TARGET} PRIVATE cxx_std_17)
 target_link_libraries(vulkan-shaders-gen PUBLIC Threads::Threads)
 target_link_libraries(vulkan-shaders-gen PRIVATE Vulkan::Vulkan)

From 4a17b483c918ca5a56b19af246dbee3a623320c3 Mon Sep 17 00:00:00 2001
From: Junil Kim <logyourself@gmail.com>
Date: Thu, 28 Nov 2024 12:40:04 +0900
Subject: [PATCH 02/10] refactor: ggml: Improve vulkan-shaders-gen toolchain
 setup

- Add GGML_SHADERS_GEN_TOOLCHAIN CMake option.
- Auto-detect host toolchain if not set.
---
 ggml/CMakeLists.txt                 |  3 ++
 ggml/src/ggml-vulkan/CMakeLists.txt | 67 ++++++++++++++++++++++++-----
 2 files changed, 60 insertions(+), 10 deletions(-)

diff --git a/ggml/CMakeLists.txt b/ggml/CMakeLists.txt
index fe8acc8038b5..72a3e45ffc0e 100644
--- a/ggml/CMakeLists.txt
+++ b/ggml/CMakeLists.txt
@@ -185,6 +185,9 @@ option(GGML_OPENCL_PROFILING                "ggml: use OpenCL profiling (increas
 option(GGML_OPENCL_EMBED_KERNELS            "ggml: embed kernels"                             ON)
 option(GGML_OPENCL_USE_ADRENO_KERNELS       "ggml: use optimized kernels for Adreno"          ON)
 
+# toolchain for vulkan-shaders-gen
+set   (GGML_SHADERS_GEN_TOOLCHAIN "" CACHE FILEPATH "ggml: toolchain file for vulkan-shaders-gen")
+
 # extra artifacts
 option(GGML_BUILD_TESTS    "ggml: build tests"    ${GGML_STANDALONE})
 option(GGML_BUILD_EXAMPLES "ggml: build examples" ${GGML_STANDALONE})
diff --git a/ggml/src/ggml-vulkan/CMakeLists.txt b/ggml/src/ggml-vulkan/CMakeLists.txt
index 797658725e16..62d8ea35b8fc 100644
--- a/ggml/src/ggml-vulkan/CMakeLists.txt
+++ b/ggml/src/ggml-vulkan/CMakeLists.txt
@@ -3,6 +3,39 @@ cmake_policy(SET CMP0114 NEW)
 
 find_package(Vulkan COMPONENTS glslc REQUIRED)
 
+function(detect_host_compiler)
+    if (CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")
+        find_program(MSVC_COMPILER cl)
+        find_program(GNU_C_COMPILER gcc)
+        find_program(GNU_CXX_COMPILER g++)
+        if (MSVC_COMPILER)
+            set(HOST_C_COMPILER "${MSVC_COMPILER}" PARENT_SCOPE)
+            set(HOST_CXX_COMPILER "${MSVC_COMPILER}" PARENT_SCOPE)
+        elseif (CLANG_C_COMPILER AND CLANG_CXX_COMPILER)
+            set(HOST_C_COMPILER "${CLANG_C_COMPILER}" PARENT_SCOPE)
+            set(HOST_CXX_COMPILER "${CLANG_CXX_COMPILER}" PARENT_SCOPE)
+        else()
+            message(WARNING "Neither MSVC nor clang found")
+        endif()
+    elseif(CMAKE_HOST_SYSTEM_NAME STREQUAL "Linux")
+        find_program(CLANG_C_COMPILER clang)
+        find_program(CLANG_CXX_COMPILER clang++)
+        find_program(GNU_C_COMPILER gcc)
+        find_program(GNU_CXX_COMPILER g++)
+        if (GNU_C_COMPILER AND GNU_CXX_COMPILER)
+            set(HOST_C_COMPILER "${GNU_C_COMPILER}" PARENT_SCOPE)
+            set(HOST_CXX_COMPILER "${GNU_CXX_COMPILER}" PARENT_SCOPE)
+        elseif (CLANG_C_COMPILER AND CLANG_CXX_COMPILER)
+            set(HOST_C_COMPILER "${CLANG_C_COMPILER}" PARENT_SCOPE)
+            set(HOST_CXX_COMPILER "${CLANG_CXX_COMPILER}" PARENT_SCOPE)
+        else()
+            message(WARNING "Neither clang nor gcc found")
+        endif()
+    else()
+        message(WARNING "Unsupported host system: ${CMAKE_HOST_SYSTEM_NAME}")
+    endif()
+endfunction()
+
 if (Vulkan_FOUND)
     message(STATUS "Vulkan found")
 
@@ -79,22 +112,36 @@ if (Vulkan_FOUND)
     if (NOT CMAKE_CROSSCOMPILING)
         add_subdirectory(vulkan-shaders)
     else()
+        if (GGML_SHADERS_GEN_TOOLCHAIN)
+            set(HOST_CMAKE_TOOLCHAIN_FILE ${GGML_SHADERS_GEN_TOOLCHAIN})
+        else()
+            detect_host_compiler()
+            if (NOT HOST_C_COMPILER OR NOT HOST_CXX_COMPILER)
+                message(FATAL_ERROR "Host compiler not found")
+            else()
+                message(STATUS "Host compiler: ${HOST_C_COMPILER} ${HOST_CXX_COMPILER}")
+            endif()
+            file(WRITE ${CMAKE_BINARY_DIR}/host_toolchain.cmake
+                "set(CMAKE_BUILD_TYPE Release)\n"
+                "set(CMAKE_C_FLAGS -O2)\n"
+                "set(CMAKE_CXX_FLAGS -O2)\n"
+                "set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)\n"
+                "set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY NEVER)\n"
+                "set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE NEVER)\n"
+                "set(CMAKE_C_COMPILER ${HOST_C_COMPILER})\n"
+                "set(CMAKE_CXX_COMPILER ${HOST_CXX_COMPILER})\n"
+            )
+            set(HOST_CMAKE_TOOLCHAIN_FILE ${CMAKE_BINARY_DIR}/host_toolchain.cmake)
+        endif()
+        message(STATUS "vulkan-shaders-gen toolchain file: ${HOST_CMAKE_TOOLCHAIN_FILE}")
+
         include(ExternalProject)
         # Native build through ExternalProject_Add
         ExternalProject_Add(
             vulkan-shaders-gen
             SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/vulkan-shaders
-            CMAKE_ARGS -DCMAKE_BUILD_TYPE=Release
+            CMAKE_ARGS -DCMAKE_TOOLCHAIN_FILE=${HOST_CMAKE_TOOLCHAIN_FILE}
                     -DCMAKE_INSTALL_PREFIX=${CMAKE_BINARY_DIR}
-                    -DCMAKE_TOOLCHAIN_FILE=
-                    -DCMAKE_SYSROOT=
-                    -DCMAKE_C_FLAGS=-O2
-                    -DCMAKE_CXX_FLAGS=-O2
-                    -DCMAKE_FIND_ROOT_PATH_MODE_PROGRAM=NEVER
-                    -DCMAKE_FIND_ROOT_PATH_MODE_LIBRARY=NEVER
-                    -DCMAKE_FIND_ROOT_PATH_MODE_INCLUDE=NEVER
-                    -DCMAKE_C_COMPILER=gcc
-                    -DCMAKE_CXX_COMPILER=g++
             BUILD_COMMAND ${CMAKE_COMMAND} --build .
             INSTALL_COMMAND ${CMAKE_COMMAND} --install .
             INSTALL_DIR ${CMAKE_BINARY_DIR}

From 1921b9d39c9e7ae269130ce9d2cade0b74c4b276 Mon Sep 17 00:00:00 2001
From: Junil Kim <jjunil79.kim@lge.com>
Date: Tue, 24 Dec 2024 12:52:04 +0900
Subject: [PATCH 03/10] refactor: ggml: Improve vulkan-shaders-gen toolchain
 setup

Use configure_file to generate host_toolchain.cmake from template
---
 ggml/src/ggml-vulkan/CMakeLists.txt          | 15 ++-------------
 ggml/src/ggml-vulkan/host_toolchain.cmake.in |  8 ++++++++
 2 files changed, 10 insertions(+), 13 deletions(-)
 create mode 100644 ggml/src/ggml-vulkan/host_toolchain.cmake.in

diff --git a/ggml/src/ggml-vulkan/CMakeLists.txt b/ggml/src/ggml-vulkan/CMakeLists.txt
index 62d8ea35b8fc..a228d1120360 100644
--- a/ggml/src/ggml-vulkan/CMakeLists.txt
+++ b/ggml/src/ggml-vulkan/CMakeLists.txt
@@ -17,7 +17,7 @@ function(detect_host_compiler)
         else()
             message(WARNING "Neither MSVC nor clang found")
         endif()
-    elseif(CMAKE_HOST_SYSTEM_NAME STREQUAL "Linux")
+    else()
         find_program(CLANG_C_COMPILER clang)
         find_program(CLANG_CXX_COMPILER clang++)
         find_program(GNU_C_COMPILER gcc)
@@ -31,8 +31,6 @@ function(detect_host_compiler)
         else()
             message(WARNING "Neither clang nor gcc found")
         endif()
-    else()
-        message(WARNING "Unsupported host system: ${CMAKE_HOST_SYSTEM_NAME}")
     endif()
 endfunction()
 
@@ -121,16 +119,7 @@ if (Vulkan_FOUND)
             else()
                 message(STATUS "Host compiler: ${HOST_C_COMPILER} ${HOST_CXX_COMPILER}")
             endif()
-            file(WRITE ${CMAKE_BINARY_DIR}/host_toolchain.cmake
-                "set(CMAKE_BUILD_TYPE Release)\n"
-                "set(CMAKE_C_FLAGS -O2)\n"
-                "set(CMAKE_CXX_FLAGS -O2)\n"
-                "set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)\n"
-                "set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY NEVER)\n"
-                "set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE NEVER)\n"
-                "set(CMAKE_C_COMPILER ${HOST_C_COMPILER})\n"
-                "set(CMAKE_CXX_COMPILER ${HOST_CXX_COMPILER})\n"
-            )
+            configure_file(${CMAKE_CURRENT_SOURCE_DIR}/host_toolchain.cmake.in ${CMAKE_BINARY_DIR}/host_toolchain.cmake @ONLY)
             set(HOST_CMAKE_TOOLCHAIN_FILE ${CMAKE_BINARY_DIR}/host_toolchain.cmake)
         endif()
         message(STATUS "vulkan-shaders-gen toolchain file: ${HOST_CMAKE_TOOLCHAIN_FILE}")
diff --git a/ggml/src/ggml-vulkan/host_toolchain.cmake.in b/ggml/src/ggml-vulkan/host_toolchain.cmake.in
new file mode 100644
index 000000000000..f4d7480e0ce1
--- /dev/null
+++ b/ggml/src/ggml-vulkan/host_toolchain.cmake.in
@@ -0,0 +1,8 @@
+set(CMAKE_BUILD_TYPE Release)
+set(CMAKE_C_FLAGS -O2)
+set(CMAKE_CXX_FLAGS -O2)
+set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
+set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY NEVER)
+set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE NEVER)
+set(CMAKE_C_COMPILER @HOST_C_COMPILER@)
+set(CMAKE_CXX_COMPILER @HOST_CXX_COMPILER@)

From b6ebd4fc8c1fb9036609795d74df147e703d1ee0 Mon Sep 17 00:00:00 2001
From: Junil Kim <jjunil79.kim@lge.com>
Date: Fri, 10 Jan 2025 19:00:47 +0900
Subject: [PATCH 04/10] fix: ggml: Fix compile error

Fix compile error not finding vulkan-shaders-gen
---
 ggml/src/ggml-vulkan/CMakeLists.txt                | 6 +-----
 ggml/src/ggml-vulkan/vulkan-shaders/CMakeLists.txt | 6 +-----
 2 files changed, 2 insertions(+), 10 deletions(-)

diff --git a/ggml/src/ggml-vulkan/CMakeLists.txt b/ggml/src/ggml-vulkan/CMakeLists.txt
index a228d1120360..8ee1bfbcec2e 100644
--- a/ggml/src/ggml-vulkan/CMakeLists.txt
+++ b/ggml/src/ggml-vulkan/CMakeLists.txt
@@ -137,7 +137,7 @@ if (Vulkan_FOUND)
         )
         ExternalProject_Add_StepTargets(vulkan-shaders-gen build install)
     endif()
-    set (_ggml_vk_genshaders_cmd ${CMAKE_BINARY_DIR}/bin/vulkan-shaders-gen)
+    set (_ggml_vk_genshaders_cmd ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/vulkan-shaders-gen)
     set (_ggml_vk_header     ${CMAKE_CURRENT_BINARY_DIR}/ggml-vulkan-shaders.hpp)
     set (_ggml_vk_source     ${CMAKE_CURRENT_BINARY_DIR}/ggml-vulkan-shaders.cpp)
     set (_ggml_vk_input_dir  ${CMAKE_CURRENT_SOURCE_DIR}/vulkan-shaders)
@@ -145,10 +145,6 @@ if (Vulkan_FOUND)
 
     file(GLOB _ggml_vk_shader_deps "${_ggml_vk_input_dir}/*.comp")
 
-    if (NOT CMAKE_CROSSCOMPILING)
-        set(_ggml_vk_genshaders_cmd "$<TARGET_FILE_DIR:vulkan-shaders-gen>/${_ggml_vk_genshaders_cmd}")
-    endif ()
-
     if (CMAKE_CROSSCOMPILING)
         set(_ggml_vk_shader_deps ${_ggml_vk_shader_deps} vulkan-shaders-gen-build vulkan-shaders-gen-install)
     endif()
diff --git a/ggml/src/ggml-vulkan/vulkan-shaders/CMakeLists.txt b/ggml/src/ggml-vulkan/vulkan-shaders/CMakeLists.txt
index 8f496d6c6b2e..bd0c74cb1c77 100644
--- a/ggml/src/ggml-vulkan/vulkan-shaders/CMakeLists.txt
+++ b/ggml/src/ggml-vulkan/vulkan-shaders/CMakeLists.txt
@@ -3,11 +3,7 @@ find_package(Vulkan COMPONENTS glslc REQUIRED)
 
 set(TARGET vulkan-shaders-gen)
 add_executable(${TARGET} vulkan-shaders-gen.cpp)
-if (${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
-    install(TARGETS ${TARGET} RUNTIME)
-else()
-    install(TARGETS ${TARGET} RUNTIME DESTINATION bin)
-endif()
+install(TARGETS ${TARGET} RUNTIME)
 target_compile_features(${TARGET} PRIVATE cxx_std_17)
 target_link_libraries(vulkan-shaders-gen PUBLIC Threads::Threads)
 target_link_libraries(vulkan-shaders-gen PRIVATE Vulkan::Vulkan)

From ce14d9b7cb80ea2de76f783ccdf2f33d8f7eeec1 Mon Sep 17 00:00:00 2001
From: Junil Kim <logyourself@gmail.com>
Date: Sun, 12 Jan 2025 03:42:44 +0900
Subject: [PATCH 05/10] fix: vulkan-shaders-gen build and path handling

Fix build issues with vulkan-shaders-gen:
- Add target dependency for correct build order
- Use CMAKE_HOST_SYSTEM_NAME for executable suffix
- Fix MSVC output directory in host toolchain
- Normalize path handling for cross-compilation
---
 ggml/src/ggml-vulkan/CMakeLists.txt          | 13 +++++++++++--
 ggml/src/ggml-vulkan/host_toolchain.cmake.in |  7 +++++++
 2 files changed, 18 insertions(+), 2 deletions(-)

diff --git a/ggml/src/ggml-vulkan/CMakeLists.txt b/ggml/src/ggml-vulkan/CMakeLists.txt
index 8ee1bfbcec2e..2eed7ffb40db 100644
--- a/ggml/src/ggml-vulkan/CMakeLists.txt
+++ b/ggml/src/ggml-vulkan/CMakeLists.txt
@@ -109,6 +109,13 @@ if (Vulkan_FOUND)
 
     if (NOT CMAKE_CROSSCOMPILING)
         add_subdirectory(vulkan-shaders)
+        if (MSVC)
+            foreach(CONFIG ${CMAKE_CONFIGURATION_TYPES})
+                string(TOUPPER ${CONFIG} CONFIG)
+                set_target_properties(vulkan-shaders-gen PROPERTIES
+                    RUNTIME_OUTPUT_DIRECTORY_${CONFIG} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
+            endforeach()
+        endif()
     else()
         if (GGML_SHADERS_GEN_TOOLCHAIN)
             set(HOST_CMAKE_TOOLCHAIN_FILE ${GGML_SHADERS_GEN_TOOLCHAIN})
@@ -137,13 +144,15 @@ if (Vulkan_FOUND)
         )
         ExternalProject_Add_StepTargets(vulkan-shaders-gen build install)
     endif()
-    set (_ggml_vk_genshaders_cmd ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/vulkan-shaders-gen)
+    set (_ggml_vk_host_suffix $<IF:$<STREQUAL:${CMAKE_HOST_SYSTEM_NAME},Windows>,.exe,>)
+    set (_ggml_vk_genshaders_cmd ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/vulkan-shaders-gen${_ggml_vk_host_suffix})
     set (_ggml_vk_header     ${CMAKE_CURRENT_BINARY_DIR}/ggml-vulkan-shaders.hpp)
     set (_ggml_vk_source     ${CMAKE_CURRENT_BINARY_DIR}/ggml-vulkan-shaders.cpp)
     set (_ggml_vk_input_dir  ${CMAKE_CURRENT_SOURCE_DIR}/vulkan-shaders)
     set (_ggml_vk_output_dir ${CMAKE_CURRENT_BINARY_DIR}/vulkan-shaders.spv)
 
     file(GLOB _ggml_vk_shader_deps "${_ggml_vk_input_dir}/*.comp")
+    set (_ggml_vk_shader_deps ${_ggml_vk_shader_deps} vulkan-shaders-gen)
 
     if (CMAKE_CROSSCOMPILING)
         set(_ggml_vk_shader_deps ${_ggml_vk_shader_deps} vulkan-shaders-gen-build vulkan-shaders-gen-install)
@@ -161,7 +170,7 @@ if (Vulkan_FOUND)
             --target-cpp ${_ggml_vk_source}
             --no-clean
 
-        DEPENDS ${_ggml_vk_shader_deps} ${_ggml_vk_genshaders_cmd}
+        DEPENDS ${_ggml_vk_shader_deps}
         COMMENT "Generate vulkan shaders"
     )
 
diff --git a/ggml/src/ggml-vulkan/host_toolchain.cmake.in b/ggml/src/ggml-vulkan/host_toolchain.cmake.in
index f4d7480e0ce1..b6af747a500e 100644
--- a/ggml/src/ggml-vulkan/host_toolchain.cmake.in
+++ b/ggml/src/ggml-vulkan/host_toolchain.cmake.in
@@ -6,3 +6,10 @@ set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY NEVER)
 set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE NEVER)
 set(CMAKE_C_COMPILER @HOST_C_COMPILER@)
 set(CMAKE_CXX_COMPILER @HOST_CXX_COMPILER@)
+set(CMAKE_RUNTIME_OUTPUT_DIRECTORY @CMAKE_RUNTIME_OUTPUT_DIRECTORY@)
+
+if("@CMAKE_C_COMPILER_ID@" STREQUAL "MSVC")
+    foreach(CONFIG IN ITEMS DEBUG RELEASE MINSIZEREL RELWITHDEBINFO)
+        set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_${CONFIG} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
+    endforeach()
+endif()

From 46b4c8da440865668dcf507f8d66f80381e43ae5 Mon Sep 17 00:00:00 2001
From: Junil Kim <logyourself@gmail.com>
Date: Sun, 12 Jan 2025 11:22:58 +0900
Subject: [PATCH 06/10] fix: improve host compiler detection in vulkan shader
 build

Improve host compiler detection for vulkan shader generation:
- Add NO_CMAKE_FIND_ROOT_PATH to all compiler searches
- Consolidate compiler detection logic
- Fix Windows-specific MSVC detection
- Ensure correct compiler search in cross-compilation
---
 ggml/src/ggml-vulkan/CMakeLists.txt | 37 +++++++++++++----------------
 1 file changed, 16 insertions(+), 21 deletions(-)

diff --git a/ggml/src/ggml-vulkan/CMakeLists.txt b/ggml/src/ggml-vulkan/CMakeLists.txt
index 2eed7ffb40db..79708b916bbd 100644
--- a/ggml/src/ggml-vulkan/CMakeLists.txt
+++ b/ggml/src/ggml-vulkan/CMakeLists.txt
@@ -4,33 +4,28 @@ cmake_policy(SET CMP0114 NEW)
 find_package(Vulkan COMPONENTS glslc REQUIRED)
 
 function(detect_host_compiler)
+    find_program(GNU_C_COMPILER gcc NO_CMAKE_FIND_ROOT_PATH)
+    find_program(GNU_CXX_COMPILER g++ NO_CMAKE_FIND_ROOT_PATH)
+    find_program(CLANG_C_COMPILER clang NO_CMAKE_FIND_ROOT_PATH)
+    find_program(CLANG_CXX_COMPILER clang++ NO_CMAKE_FIND_ROOT_PATH)
+
     if (CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")
-        find_program(MSVC_COMPILER cl)
-        find_program(GNU_C_COMPILER gcc)
-        find_program(GNU_CXX_COMPILER g++)
+        find_program(MSVC_COMPILER cl NO_CMAKE_FIND_ROOT_PATH)
         if (MSVC_COMPILER)
             set(HOST_C_COMPILER "${MSVC_COMPILER}" PARENT_SCOPE)
             set(HOST_CXX_COMPILER "${MSVC_COMPILER}" PARENT_SCOPE)
-        elseif (CLANG_C_COMPILER AND CLANG_CXX_COMPILER)
-            set(HOST_C_COMPILER "${CLANG_C_COMPILER}" PARENT_SCOPE)
-            set(HOST_CXX_COMPILER "${CLANG_CXX_COMPILER}" PARENT_SCOPE)
-        else()
-            message(WARNING "Neither MSVC nor clang found")
+            return()
         endif()
+    endif()
+
+    if (GNU_C_COMPILER AND GNU_CXX_COMPILER)
+        set(HOST_C_COMPILER "${GNU_C_COMPILER}" PARENT_SCOPE)
+        set(HOST_CXX_COMPILER "${GNU_CXX_COMPILER}" PARENT_SCOPE)
+    elseif (CLANG_C_COMPILER AND CLANG_CXX_COMPILER)
+        set(HOST_C_COMPILER "${CLANG_C_COMPILER}" PARENT_SCOPE)
+        set(HOST_CXX_COMPILER "${CLANG_CXX_COMPILER}" PARENT_SCOPE)
     else()
-        find_program(CLANG_C_COMPILER clang)
-        find_program(CLANG_CXX_COMPILER clang++)
-        find_program(GNU_C_COMPILER gcc)
-        find_program(GNU_CXX_COMPILER g++)
-        if (GNU_C_COMPILER AND GNU_CXX_COMPILER)
-            set(HOST_C_COMPILER "${GNU_C_COMPILER}" PARENT_SCOPE)
-            set(HOST_CXX_COMPILER "${GNU_CXX_COMPILER}" PARENT_SCOPE)
-        elseif (CLANG_C_COMPILER AND CLANG_CXX_COMPILER)
-            set(HOST_C_COMPILER "${CLANG_C_COMPILER}" PARENT_SCOPE)
-            set(HOST_CXX_COMPILER "${CLANG_CXX_COMPILER}" PARENT_SCOPE)
-        else()
-            message(WARNING "Neither clang nor gcc found")
-        endif()
+        message(WARNING "No suitable host compiler found")
     endif()
 endfunction()
 

From efe4b14e602527d049dac06dd857dd3cdbae3719 Mon Sep 17 00:00:00 2001
From: Junil Kim <jjunil79.kim@lge.com>
Date: Mon, 13 Jan 2025 13:51:15 +0900
Subject: [PATCH 07/10] refactor: Simplify CMake function for detecting host
 compiler

Simplified the CMake function to improve the process of detecting the host compiler.
---
 ggml/src/ggml-vulkan/CMakeLists.txt | 26 ++++++--------------------
 1 file changed, 6 insertions(+), 20 deletions(-)

diff --git a/ggml/src/ggml-vulkan/CMakeLists.txt b/ggml/src/ggml-vulkan/CMakeLists.txt
index 79708b916bbd..82b90a18f009 100644
--- a/ggml/src/ggml-vulkan/CMakeLists.txt
+++ b/ggml/src/ggml-vulkan/CMakeLists.txt
@@ -4,29 +4,15 @@ cmake_policy(SET CMP0114 NEW)
 find_package(Vulkan COMPONENTS glslc REQUIRED)
 
 function(detect_host_compiler)
-    find_program(GNU_C_COMPILER gcc NO_CMAKE_FIND_ROOT_PATH)
-    find_program(GNU_CXX_COMPILER g++ NO_CMAKE_FIND_ROOT_PATH)
-    find_program(CLANG_C_COMPILER clang NO_CMAKE_FIND_ROOT_PATH)
-    find_program(CLANG_CXX_COMPILER clang++ NO_CMAKE_FIND_ROOT_PATH)
-
     if (CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")
-        find_program(MSVC_COMPILER cl NO_CMAKE_FIND_ROOT_PATH)
-        if (MSVC_COMPILER)
-            set(HOST_C_COMPILER "${MSVC_COMPILER}" PARENT_SCOPE)
-            set(HOST_CXX_COMPILER "${MSVC_COMPILER}" PARENT_SCOPE)
-            return()
-        endif()
-    endif()
-
-    if (GNU_C_COMPILER AND GNU_CXX_COMPILER)
-        set(HOST_C_COMPILER "${GNU_C_COMPILER}" PARENT_SCOPE)
-        set(HOST_CXX_COMPILER "${GNU_CXX_COMPILER}" PARENT_SCOPE)
-    elseif (CLANG_C_COMPILER AND CLANG_CXX_COMPILER)
-        set(HOST_C_COMPILER "${CLANG_C_COMPILER}" PARENT_SCOPE)
-        set(HOST_CXX_COMPILER "${CLANG_CXX_COMPILER}" PARENT_SCOPE)
+        find_program(HOST_C_COMPILER NAMES cl gcc clang NO_CMAKE_FIND_ROOT_PATH)
+        find_program(HOST_CXX_COMPILER NAMES cl g++ clang++ NO_CMAKE_FIND_ROOT_PATH)
     else()
-        message(WARNING "No suitable host compiler found")
+        find_program(HOST_C_COMPILER NAMES gcc clang NO_CMAKE_FIND_ROOT_PATH)
+        find_program(HOST_CXX_COMPILER NAMES g++ clang++ NO_CMAKE_FIND_ROOT_PATH)
     endif()
+    set(HOST_C_COMPILER "${HOST_C_COMPILER}" PARENT_SCOPE)
+    set(HOST_CXX_COMPILER "${HOST_CXX_COMPILER}" PARENT_SCOPE)
 endfunction()
 
 if (Vulkan_FOUND)

From 37d0cb6e848ac4c96066f41c3e8ac2e629bd8ba9 Mon Sep 17 00:00:00 2001
From: Junil Kim <jjunil79.kim@lge.com>
Date: Mon, 13 Jan 2025 16:09:11 +0900
Subject: [PATCH 08/10] fix: Remove unnecessary Vulkan library linkage in
 CMakeLists.txt

Since `vulkan-shader-gen.cpp` only requires the `glslc` executable
and not the Vulkan headers or libraries, CMakeLists.txt needs to
be corrected.
(See: ecc93d0558fc3ecb8a5af69d2ece02fae4710ade)
---
 ggml/src/ggml-vulkan/vulkan-shaders/CMakeLists.txt         | 6 ++++--
 ggml/src/ggml-vulkan/vulkan-shaders/vulkan-shaders-gen.cpp | 2 --
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/ggml/src/ggml-vulkan/vulkan-shaders/CMakeLists.txt b/ggml/src/ggml-vulkan/vulkan-shaders/CMakeLists.txt
index bd0c74cb1c77..074031087f45 100644
--- a/ggml/src/ggml-vulkan/vulkan-shaders/CMakeLists.txt
+++ b/ggml/src/ggml-vulkan/vulkan-shaders/CMakeLists.txt
@@ -1,9 +1,11 @@
 find_package (Threads REQUIRED)
-find_package(Vulkan COMPONENTS glslc REQUIRED)
+find_program(GLSLC_EXECUTABLE glslc)
+if(NOT GLSLC_EXECUTABLE)
+    message(FATAL_ERROR "glslc not found.")
+endif()
 
 set(TARGET vulkan-shaders-gen)
 add_executable(${TARGET} vulkan-shaders-gen.cpp)
 install(TARGETS ${TARGET} RUNTIME)
 target_compile_features(${TARGET} PRIVATE cxx_std_17)
 target_link_libraries(vulkan-shaders-gen PUBLIC Threads::Threads)
-target_link_libraries(vulkan-shaders-gen PRIVATE Vulkan::Vulkan)
diff --git a/ggml/src/ggml-vulkan/vulkan-shaders/vulkan-shaders-gen.cpp b/ggml/src/ggml-vulkan/vulkan-shaders/vulkan-shaders-gen.cpp
index 7b5044798d72..2438399174d6 100644
--- a/ggml/src/ggml-vulkan/vulkan-shaders/vulkan-shaders-gen.cpp
+++ b/ggml/src/ggml-vulkan/vulkan-shaders/vulkan-shaders-gen.cpp
@@ -30,8 +30,6 @@
     #include <fcntl.h>
 #endif
 
-#include <vulkan/vulkan_core.h>
-
 #define ASYNCIO_CONCURRENCY 64
 
 std::mutex lock;

From 6fdbf07181087ef33ea0dce496718bb0bca85b69 Mon Sep 17 00:00:00 2001
From: Junil Kim <logyourself@gmail.com>
Date: Wed, 15 Jan 2025 09:14:00 +0900
Subject: [PATCH 09/10] refactor: Rename host_toolchain.cmake.in

- Rename host_toolchain.cmake.in to cmake/host-toolchain.cmake.in
---
 ggml/src/ggml-vulkan/CMakeLists.txt                           | 4 ++--
 .../host-toolchain.cmake.in}                                  | 0
 2 files changed, 2 insertions(+), 2 deletions(-)
 rename ggml/src/ggml-vulkan/{host_toolchain.cmake.in => cmake/host-toolchain.cmake.in} (100%)

diff --git a/ggml/src/ggml-vulkan/CMakeLists.txt b/ggml/src/ggml-vulkan/CMakeLists.txt
index 82b90a18f009..925448de424c 100644
--- a/ggml/src/ggml-vulkan/CMakeLists.txt
+++ b/ggml/src/ggml-vulkan/CMakeLists.txt
@@ -107,8 +107,8 @@ if (Vulkan_FOUND)
             else()
                 message(STATUS "Host compiler: ${HOST_C_COMPILER} ${HOST_CXX_COMPILER}")
             endif()
-            configure_file(${CMAKE_CURRENT_SOURCE_DIR}/host_toolchain.cmake.in ${CMAKE_BINARY_DIR}/host_toolchain.cmake @ONLY)
-            set(HOST_CMAKE_TOOLCHAIN_FILE ${CMAKE_BINARY_DIR}/host_toolchain.cmake)
+            configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/host-toolchain.cmake.in ${CMAKE_BINARY_DIR}/host-toolchain.cmake @ONLY)
+            set(HOST_CMAKE_TOOLCHAIN_FILE ${CMAKE_BINARY_DIR}/host-toolchain.cmake)
         endif()
         message(STATUS "vulkan-shaders-gen toolchain file: ${HOST_CMAKE_TOOLCHAIN_FILE}")
 
diff --git a/ggml/src/ggml-vulkan/host_toolchain.cmake.in b/ggml/src/ggml-vulkan/cmake/host-toolchain.cmake.in
similarity index 100%
rename from ggml/src/ggml-vulkan/host_toolchain.cmake.in
rename to ggml/src/ggml-vulkan/cmake/host-toolchain.cmake.in

From f4d1fbc79f0a02e0c91bb120858f9d6d73d692eb Mon Sep 17 00:00:00 2001
From: Junil Kim <logyourself@gmail.com>
Date: Wed, 15 Jan 2025 19:16:08 +0900
Subject: [PATCH 10/10] refactor: GGML_VULKAN_SHADERS_GEN_TOOLCHAIN

Rename the macro GGML_SHADERS_GEN_TOOLCHAIN to GGML_VULKAN_SHADERS_GEN_TOOLCHAIN
---
 ggml/CMakeLists.txt                 | 2 +-
 ggml/src/ggml-vulkan/CMakeLists.txt | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/ggml/CMakeLists.txt b/ggml/CMakeLists.txt
index 72a3e45ffc0e..185079aa47c4 100644
--- a/ggml/CMakeLists.txt
+++ b/ggml/CMakeLists.txt
@@ -186,7 +186,7 @@ option(GGML_OPENCL_EMBED_KERNELS            "ggml: embed kernels"
 option(GGML_OPENCL_USE_ADRENO_KERNELS       "ggml: use optimized kernels for Adreno"          ON)
 
 # toolchain for vulkan-shaders-gen
-set   (GGML_SHADERS_GEN_TOOLCHAIN "" CACHE FILEPATH "ggml: toolchain file for vulkan-shaders-gen")
+set   (GGML_VULKAN_SHADERS_GEN_TOOLCHAIN "" CACHE FILEPATH "ggml: toolchain file for vulkan-shaders-gen")
 
 # extra artifacts
 option(GGML_BUILD_TESTS    "ggml: build tests"    ${GGML_STANDALONE})
diff --git a/ggml/src/ggml-vulkan/CMakeLists.txt b/ggml/src/ggml-vulkan/CMakeLists.txt
index 925448de424c..d970f7e20b46 100644
--- a/ggml/src/ggml-vulkan/CMakeLists.txt
+++ b/ggml/src/ggml-vulkan/CMakeLists.txt
@@ -98,8 +98,8 @@ if (Vulkan_FOUND)
             endforeach()
         endif()
     else()
-        if (GGML_SHADERS_GEN_TOOLCHAIN)
-            set(HOST_CMAKE_TOOLCHAIN_FILE ${GGML_SHADERS_GEN_TOOLCHAIN})
+        if (GGML_VULKAN_SHADERS_GEN_TOOLCHAIN)
+            set(HOST_CMAKE_TOOLCHAIN_FILE ${GGML_VULKAN_SHADERS_GEN_TOOLCHAIN})
         else()
             detect_host_compiler()
             if (NOT HOST_C_COMPILER OR NOT HOST_CXX_COMPILER)
