Skip to content

Commit 26dcab1

Browse files
committed
[cmake] Refactor DIA SDK detection into FindDIASDK module
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 #86250. Fixes #100372. Fixes #111829. Fixes #152268. Signed-off-by: Ruoyu Zhong <[email protected]>
1 parent 31941d6 commit 26dcab1

File tree

5 files changed

+85
-44
lines changed

5 files changed

+85
-44
lines changed

compiler-rt/cmake/config-ix.cmake

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -728,16 +728,9 @@ if (MSVC)
728728
set(LLVM_WINSYSROOT "" CACHE STRING
729729
"If set, argument to clang-cl's /winsysroot")
730730

731-
if (LLVM_WINSYSROOT)
732-
set(MSVC_DIA_SDK_DIR "${LLVM_WINSYSROOT}/DIA SDK" CACHE PATH
733-
"Path to the DIA SDK")
734-
else()
735-
set(MSVC_DIA_SDK_DIR "$ENV{VSINSTALLDIR}DIA SDK" CACHE PATH
736-
"Path to the DIA SDK")
737-
endif()
738-
739731
# See if the DIA SDK is available and usable.
740-
if (IS_DIRECTORY ${MSVC_DIA_SDK_DIR})
732+
find_package(DIASDK)
733+
if (DIASDK_FOUND)
741734
set(CAN_SYMBOLIZE 1)
742735
else()
743736
set(CAN_SYMBOLIZE 0)

llvm/cmake/config-ix.cmake

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -625,32 +625,11 @@ if( MSVC )
625625
set(LLVM_WINSYSROOT "" CACHE STRING
626626
"If set, argument to clang-cl's /winsysroot")
627627

628-
if (LLVM_WINSYSROOT)
629-
set(MSVC_DIA_SDK_DIR "${LLVM_WINSYSROOT}/DIA SDK" CACHE PATH
630-
"Path to the DIA SDK")
631-
else()
632-
set(MSVC_DIA_SDK_DIR "$ENV{VSINSTALLDIR}DIA SDK" CACHE PATH
633-
"Path to the DIA SDK")
634-
endif()
635-
636-
# See if the DIA SDK is available and usable.
637-
# Due to a bug in MSVC 2013's installation software, it is possible
638-
# for MSVC 2013 to write the DIA SDK into the Visual Studio 2012
639-
# install directory. If this happens, the installation is corrupt
640-
# and there's nothing we can do. It happens with enough frequency
641-
# though that we should handle it. We do so by simply checking that
642-
# the DIA SDK folder exists. Should this happen you will need to
643-
# uninstall VS 2012 and then re-install VS 2013.
644-
if (IS_DIRECTORY "${MSVC_DIA_SDK_DIR}")
645-
set(HAVE_DIA_SDK 1)
646-
else()
647-
set(HAVE_DIA_SDK 0)
648-
endif()
649-
628+
find_package(DIASDK)
650629
option(LLVM_ENABLE_DIA_SDK "Use MSVC DIA SDK for debugging if available."
651-
${HAVE_DIA_SDK})
630+
${DIASDK_FOUND})
652631

653-
if(LLVM_ENABLE_DIA_SDK AND NOT HAVE_DIA_SDK)
632+
if(LLVM_ENABLE_DIA_SDK AND NOT DIASDK_FOUND)
654633
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.")
655634
endif()
656635
else()
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Finds the Microsoft DIA SDK and sets DIASDK_FOUND and related variables.
2+
#
3+
# This module is intended to be used both internally by LLVM's build system and
4+
# by consuming projects when loading LLVMConfig.cmake.
5+
#
6+
# LLVM_WINSYSROOT may be set for locating the DIA SDK.
7+
#
8+
# If successful, the following variables will be defined:
9+
# DIASDK_FOUND
10+
# DIASDK_INCLUDE_DIR
11+
# DIASDK_LIBRARIES
12+
#
13+
# Additionally, the following import target will be defined:
14+
# DIASDK::Diaguids
15+
16+
if(NOT WIN32)
17+
set(DIASDK_FOUND FALSE)
18+
return()
19+
endif()
20+
21+
if(LLVM_WINSYSROOT)
22+
set(MSVC_DIA_SDK_DIR "${LLVM_WINSYSROOT}/DIA SDK" CACHE PATH
23+
"Path to the DIA SDK")
24+
else()
25+
set(MSVC_DIA_SDK_DIR "$ENV{VSINSTALLDIR}DIA SDK" CACHE PATH
26+
"Path to the DIA SDK")
27+
endif()
28+
29+
find_path(DIASDK_INCLUDE_DIR
30+
NAMES dia2.h
31+
PATHS "${MSVC_DIA_SDK_DIR}/include"
32+
NO_DEFAULT_PATH
33+
NO_CMAKE_FIND_ROOT_PATH
34+
)
35+
36+
if(IS_DIRECTORY "${MSVC_DIA_SDK_DIR}")
37+
set(_DIA_SDK_LIB_DIR "${MSVC_DIA_SDK_DIR}/lib")
38+
39+
if("$ENV{VSCMD_ARG_TGT_ARCH}" STREQUAL "arm64")
40+
set(_DIA_SDK_LIB_DIR "${_DIA_SDK_LIB_DIR}/arm64")
41+
elseif("$ENV{VSCMD_ARG_TGT_ARCH}" STREQUAL "arm")
42+
set(_DIA_SDK_LIB_DIR "${_DIA_SDK_LIB_DIR}/arm")
43+
elseif(CMAKE_SIZEOF_VOID_P EQUAL 8)
44+
set(_DIA_SDK_LIB_DIR "${_DIA_SDK_LIB_DIR}/amd64")
45+
endif()
46+
47+
find_library(DIASDK_LIBRARIES
48+
NAMES diaguids
49+
PATHS "${_DIA_SDK_LIB_DIR}"
50+
NO_DEFAULT_PATH
51+
NO_CMAKE_FIND_ROOT_PATH
52+
)
53+
endif()
54+
55+
include(FindPackageHandleStandardArgs)
56+
find_package_handle_standard_args(
57+
DIASDK
58+
FOUND_VAR
59+
DIASDK_FOUND
60+
REQUIRED_VARS
61+
DIASDK_INCLUDE_DIR
62+
DIASDK_LIBRARIES
63+
)
64+
mark_as_advanced(DIASDK_INCLUDE_DIR DIASDK_LIBRARIES)
65+
66+
if(DIASDK_FOUND)
67+
if(NOT TARGET DIASDK::Diaguids)
68+
add_library(DIASDK::Diaguids UNKNOWN IMPORTED)
69+
set_target_properties(DIASDK::Diaguids PROPERTIES
70+
IMPORTED_LOCATION "${DIASDK_LIBRARIES}"
71+
INTERFACE_INCLUDE_DIRECTORIES "${DIASDK_INCLUDE_DIR}"
72+
)
73+
endif()
74+
endif()

llvm/cmake/modules/LLVMConfig.cmake.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ endif()
9595
set(LLVM_WITH_Z3 @LLVM_WITH_Z3@)
9696

9797
set(LLVM_ENABLE_DIA_SDK @LLVM_ENABLE_DIA_SDK@)
98+
if(LLVM_ENABLE_DIA_SDK)
99+
find_package(DIASDK)
100+
endif()
98101

99102
set(LLVM_NATIVE_ARCH @LLVM_NATIVE_ARCH@)
100103

llvm/lib/DebugInfo/PDB/CMakeLists.txt

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,9 @@ macro(add_pdb_impl_folder group)
44
endmacro()
55

66
if(LLVM_ENABLE_DIA_SDK)
7-
include_directories(SYSTEM ${MSVC_DIA_SDK_DIR}/include)
8-
set(LIBPDB_LINK_FOLDERS "${MSVC_DIA_SDK_DIR}\\lib")
9-
10-
if ("$ENV{VSCMD_ARG_TGT_ARCH}" STREQUAL "arm64")
11-
set(LIBPDB_LINK_FOLDERS "${LIBPDB_LINK_FOLDERS}\\arm64")
12-
elseif ("$ENV{VSCMD_ARG_TGT_ARCH}" STREQUAL "arm")
13-
set(LIBPDB_LINK_FOLDERS "${LIBPDB_LINK_FOLDERS}\\arm")
14-
elseif (CMAKE_SIZEOF_VOID_P EQUAL 8)
15-
set(LIBPDB_LINK_FOLDERS "${LIBPDB_LINK_FOLDERS}\\amd64")
16-
endif()
17-
file(TO_CMAKE_PATH "${LIBPDB_LINK_FOLDERS}\\diaguids.lib" LIBPDB_ADDITIONAL_LIBRARIES)
7+
find_package(DIASDK REQUIRED)
8+
include_directories(SYSTEM "${DIASDK_INCLUDE_DIR}")
9+
set(LIBPDB_ADDITIONAL_LIBRARIES DIASDK::Diaguids)
1810

1911
add_pdb_impl_folder(DIA
2012
DIA/DIADataStream.cpp

0 commit comments

Comments
 (0)