[llvm-branch-commits] [compiler-rt] [llvm] release/22.x: [cmake] Refactor DIA SDK detection into FindDIASDK module (#160354) (PR #195279)

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Fri May 1 08:49:48 PDT 2026


https://github.com/llvmbot created https://github.com/llvm/llvm-project/pull/195279

Backport 3545244333dfc64de756ad13f7e3792d79989898

Requested by: @ZhongRuoyu

>From 831efadca8b9f2063861cc297c9be0779546de97 Mon Sep 17 00:00:00 2001
From: Ruoyu Zhong <zhongruoyu at outlook.com>
Date: Fri, 1 May 2026 23:29:45 +0800
Subject: [PATCH] [cmake] Refactor DIA SDK detection into FindDIASDK module
 (#160354)

This consolidates the DIA SDK detection logic from
`{llvm,compiler-rt}/cmake/config-ix.cmake` into a new centralized,
reusable `FindDIASDK.cmake` module.

In addition to code deduplication, it also helps to avoid hard-coded
references to the DIA SDK location in `LLVMExports.cmake`, hence
allowing a pre-built LLVM distribution for Windows to be used on another
host without requiring the DIA SDK location to be the same.

Fixes https://github.com/llvm/llvm-project/issues/86250.
Fixes https://github.com/llvm/llvm-project/issues/100372.
Fixes https://github.com/llvm/llvm-project/issues/111829.
Fixes https://github.com/llvm/llvm-project/issues/152268.

---------

Signed-off-by: Ruoyu Zhong <zhongruoyu at outlook.com>
(cherry picked from commit 3545244333dfc64de756ad13f7e3792d79989898)
---
 compiler-rt/cmake/config-ix.cmake      | 11 +---
 llvm/cmake/config-ix.cmake             | 27 +--------
 llvm/cmake/modules/FindDIASDK.cmake    | 77 ++++++++++++++++++++++++++
 llvm/cmake/modules/LLVMConfig.cmake.in |  3 +
 llvm/lib/DebugInfo/PDB/CMakeLists.txt  | 14 +----
 5 files changed, 88 insertions(+), 44 deletions(-)
 create mode 100644 llvm/cmake/modules/FindDIASDK.cmake

diff --git a/compiler-rt/cmake/config-ix.cmake b/compiler-rt/cmake/config-ix.cmake
index 1f82ff3cf7531..e91fd08092138 100644
--- a/compiler-rt/cmake/config-ix.cmake
+++ b/compiler-rt/cmake/config-ix.cmake
@@ -734,16 +734,9 @@ if (MSVC)
   set(LLVM_WINSYSROOT "" CACHE STRING
     "If set, argument to clang-cl's /winsysroot")
 
-  if (LLVM_WINSYSROOT)
-    set(MSVC_DIA_SDK_DIR "${LLVM_WINSYSROOT}/DIA SDK" CACHE PATH
-        "Path to the DIA SDK")
-  else()
-    set(MSVC_DIA_SDK_DIR "$ENV{VSINSTALLDIR}DIA SDK" CACHE PATH
-        "Path to the DIA SDK")
-  endif()
-
   # See if the DIA SDK is available and usable.
-  if (IS_DIRECTORY ${MSVC_DIA_SDK_DIR})
+  find_package(DIASDK)
+  if (DIASDK_FOUND)
     set(CAN_SYMBOLIZE 1)
   else()
     set(CAN_SYMBOLIZE 0)
diff --git a/llvm/cmake/config-ix.cmake b/llvm/cmake/config-ix.cmake
index ed2bfa6df68f4..dcef7aefc65ec 100644
--- a/llvm/cmake/config-ix.cmake
+++ b/llvm/cmake/config-ix.cmake
@@ -625,32 +625,11 @@ if( MSVC )
   set(LLVM_WINSYSROOT "" CACHE STRING
     "If set, argument to clang-cl's /winsysroot")
 
-  if (LLVM_WINSYSROOT)
-    set(MSVC_DIA_SDK_DIR "${LLVM_WINSYSROOT}/DIA SDK" CACHE PATH
-        "Path to the DIA SDK")
-  else()
-    set(MSVC_DIA_SDK_DIR "$ENV{VSINSTALLDIR}DIA SDK" CACHE PATH
-        "Path to the DIA SDK")
-  endif()
-
-  # See if the DIA SDK is available and usable.
-  # Due to a bug in MSVC 2013's installation software, it is possible
-  # for MSVC 2013 to write the DIA SDK into the Visual Studio 2012
-  # install directory.  If this happens, the installation is corrupt
-  # and there's nothing we can do.  It happens with enough frequency
-  # though that we should handle it.  We do so by simply checking that
-  # the DIA SDK folder exists.  Should this happen you will need to
-  # uninstall VS 2012 and then re-install VS 2013.
-  if (IS_DIRECTORY "${MSVC_DIA_SDK_DIR}")
-    set(HAVE_DIA_SDK 1)
-  else()
-    set(HAVE_DIA_SDK 0)
-  endif()
-
+  find_package(DIASDK)
   option(LLVM_ENABLE_DIA_SDK "Use MSVC DIA SDK for debugging if available."
-                             ${HAVE_DIA_SDK})
+                             ${DIASDK_FOUND})
 
-  if(LLVM_ENABLE_DIA_SDK AND NOT HAVE_DIA_SDK)
+  if(LLVM_ENABLE_DIA_SDK AND NOT DIASDK_FOUND)
     message(FATAL_ERROR "DIA SDK not found. If you have both VS 2012 and 2013 installed, you may need to uninstall the former and re-install the latter afterwards.")
   endif()
 else()
diff --git a/llvm/cmake/modules/FindDIASDK.cmake b/llvm/cmake/modules/FindDIASDK.cmake
new file mode 100644
index 0000000000000..f3133d29de3b2
--- /dev/null
+++ b/llvm/cmake/modules/FindDIASDK.cmake
@@ -0,0 +1,77 @@
+# Finds the Microsoft DIA SDK and sets DIASDK_FOUND and related variables.
+#
+# This module is intended to be used both internally by LLVM's build system and
+# by consuming projects when loading LLVMConfig.cmake.
+#
+# LLVM_WINSYSROOT may be set for locating the DIA SDK.
+#
+# If successful, the following variables will be defined:
+#   DIASDK_FOUND
+#   DIASDK_INCLUDE_DIR
+#   DIASDK_LIBRARIES
+#
+# Additionally, the following import target will be defined:
+#   DIASDK::Diaguids
+
+if(NOT WIN32)
+  set(DIASDK_FOUND FALSE)
+  return()
+endif()
+
+if(LLVM_WINSYSROOT)
+  set(MSVC_DIA_SDK_DIR "${LLVM_WINSYSROOT}/DIA SDK" CACHE PATH
+      "Path to the DIA SDK")
+elseif($ENV{VSINSTALLDIR})
+  set(MSVC_DIA_SDK_DIR "$ENV{VSINSTALLDIR}DIA SDK" CACHE PATH
+      "Path to the DIA SDK")
+elseif(NOT DEFINED MSVC_DIA_SDK_DIR)
+  message(STATUS "MSVC_DIA_SDK_DIR not set, and could not be inferred. DIA SDK "
+                 "may not be found.")
+endif()
+
+find_path(DIASDK_INCLUDE_DIR
+  NAMES dia2.h
+  PATHS "${MSVC_DIA_SDK_DIR}/include"
+  NO_DEFAULT_PATH
+  NO_CMAKE_FIND_ROOT_PATH
+)
+
+if(IS_DIRECTORY "${MSVC_DIA_SDK_DIR}")
+  set(_DIA_SDK_LIB_DIR "${MSVC_DIA_SDK_DIR}/lib")
+
+  if("$ENV{VSCMD_ARG_TGT_ARCH}" STREQUAL "arm64")
+    set(_DIA_SDK_LIB_DIR "${_DIA_SDK_LIB_DIR}/arm64")
+  elseif("$ENV{VSCMD_ARG_TGT_ARCH}" STREQUAL "arm")
+    set(_DIA_SDK_LIB_DIR "${_DIA_SDK_LIB_DIR}/arm")
+  elseif(CMAKE_SIZEOF_VOID_P EQUAL 8)
+    set(_DIA_SDK_LIB_DIR "${_DIA_SDK_LIB_DIR}/amd64")
+  endif()
+
+  find_library(DIASDK_LIBRARIES
+    NAMES diaguids
+    PATHS "${_DIA_SDK_LIB_DIR}"
+    NO_DEFAULT_PATH
+    NO_CMAKE_FIND_ROOT_PATH
+  )
+endif()
+
+include(FindPackageHandleStandardArgs)
+find_package_handle_standard_args(
+  DIASDK
+  FOUND_VAR
+    DIASDK_FOUND
+  REQUIRED_VARS
+    DIASDK_INCLUDE_DIR
+    DIASDK_LIBRARIES
+)
+mark_as_advanced(DIASDK_INCLUDE_DIR DIASDK_LIBRARIES)
+
+if(DIASDK_FOUND)
+  if(NOT TARGET DIASDK::Diaguids)
+    add_library(DIASDK::Diaguids UNKNOWN IMPORTED)
+    set_target_properties(DIASDK::Diaguids PROPERTIES
+      IMPORTED_LOCATION "${DIASDK_LIBRARIES}"
+      INTERFACE_INCLUDE_DIRECTORIES "${DIASDK_INCLUDE_DIR}"
+    )
+  endif()
+endif()
diff --git a/llvm/cmake/modules/LLVMConfig.cmake.in b/llvm/cmake/modules/LLVMConfig.cmake.in
index 70c807abea98a..cadd3f44b6e56 100644
--- a/llvm/cmake/modules/LLVMConfig.cmake.in
+++ b/llvm/cmake/modules/LLVMConfig.cmake.in
@@ -95,6 +95,9 @@ endif()
 set(LLVM_WITH_Z3 @LLVM_WITH_Z3@)
 
 set(LLVM_ENABLE_DIA_SDK @LLVM_ENABLE_DIA_SDK@)
+if(LLVM_ENABLE_DIA_SDK)
+  find_package(DIASDK)
+endif()
 
 set(LLVM_NATIVE_ARCH @LLVM_NATIVE_ARCH@)
 
diff --git a/llvm/lib/DebugInfo/PDB/CMakeLists.txt b/llvm/lib/DebugInfo/PDB/CMakeLists.txt
index b42fae41992e9..afde28914dacd 100644
--- a/llvm/lib/DebugInfo/PDB/CMakeLists.txt
+++ b/llvm/lib/DebugInfo/PDB/CMakeLists.txt
@@ -4,17 +4,9 @@ macro(add_pdb_impl_folder group)
 endmacro()
 
 if(LLVM_ENABLE_DIA_SDK)
-  include_directories(SYSTEM ${MSVC_DIA_SDK_DIR}/include)
-  set(LIBPDB_LINK_FOLDERS "${MSVC_DIA_SDK_DIR}\\lib")
-
-  if ("$ENV{VSCMD_ARG_TGT_ARCH}" STREQUAL "arm64")
-    set(LIBPDB_LINK_FOLDERS "${LIBPDB_LINK_FOLDERS}\\arm64")
-  elseif ("$ENV{VSCMD_ARG_TGT_ARCH}" STREQUAL "arm")
-    set(LIBPDB_LINK_FOLDERS "${LIBPDB_LINK_FOLDERS}\\arm")
-  elseif (CMAKE_SIZEOF_VOID_P EQUAL 8)
-    set(LIBPDB_LINK_FOLDERS "${LIBPDB_LINK_FOLDERS}\\amd64")
-  endif()
-  file(TO_CMAKE_PATH "${LIBPDB_LINK_FOLDERS}\\diaguids.lib" LIBPDB_ADDITIONAL_LIBRARIES)
+  find_package(DIASDK REQUIRED)
+  include_directories(SYSTEM "${DIASDK_INCLUDE_DIR}")
+  set(LIBPDB_ADDITIONAL_LIBRARIES DIASDK::Diaguids)
 
   add_pdb_impl_folder(DIA
     DIA/DIADataStream.cpp



More information about the llvm-branch-commits mailing list