The previous method for including the DIA SDK caused an absolute
path to be included in LLVMDebugInfoPDB's INTERFACE_LINK_LIBRARIES
when the LLVMExports.cmake file was generated.
This is problematic, as the path was machine-specific,
and therefore may not exist, even if the DIA SDK is available.

diff --git a/llvm/cmake/modules/AddDIA.cmake b/llvm/cmake/modules/AddDIA.cmake
new file mode 100644
index 000000000000..261b68a4c31d
--- /dev/null
+++ b/llvm/cmake/modules/AddDIA.cmake
@@ -0,0 +1,17 @@
+# Allows LLVMDebugInfoPDB to import the DIA SDK as a library target.
+
+if(NOT MSVC_DIA_SDK_DIR)
+    set(MSVC_DIA_SDK_DIR "$ENV{VSINSTALLDIR}DIA SDK")
+endif()
+
+set(MSVC_DIA_SDK_LINK_DIR "${MSVC_DIA_SDK_DIR}\\lib")
+if (CMAKE_SIZEOF_VOID_P EQUAL 8)
+    set(MSVC_DIA_SDK_LINK_DIR "${MSVC_DIA_SDK_LINK_DIR}\\amd64")
+endif()
+
+include_directories("${MSVC_DIA_SDK_DIR}\\include")
+
+add_library(MSVC_diaguids STATIC IMPORTED)
+set_target_properties(MSVC_diaguids PROPERTIES
+    IMPORTED_LOCATION "${MSVC_DIA_SDK_LINK_DIR}\\diaguids.lib"
+)
diff --git a/llvm/cmake/modules/LLVMConfig.cmake.in b/llvm/cmake/modules/LLVMConfig.cmake.in
index 4d8e33711d27..73bb7200f29b 100644
--- a/llvm/cmake/modules/LLVMConfig.cmake.in
+++ b/llvm/cmake/modules/LLVMConfig.cmake.in
@@ -99,6 +99,10 @@ set(LLVM_HAVE_OPT_VIEWER_MODULES @LLVM_HAVE_OPT_VIEWER_MODULES@)
 set(LLVM_CONFIGURATION_TYPES @CMAKE_CONFIGURATION_TYPES@)
 set(LLVM_ENABLE_SHARED_LIBS @BUILD_SHARED_LIBS@)
 
+if(LLVM_ENABLE_DIA_SDK)
+  include("${LLVM_CMAKE_DIR}/AddDIA.cmake")
+endif()
+
 if(NOT TARGET LLVMSupport)
   set(LLVM_EXPORTED_TARGETS "@LLVM_CONFIG_EXPORTS@")
   include("@LLVM_CONFIG_EXPORTS_FILE@")
diff --git a/llvm/lib/DebugInfo/PDB/CMakeLists.txt b/llvm/lib/DebugInfo/PDB/CMakeLists.txt
index 9088bc86f668..fd7523e91bb5 100644
--- a/llvm/lib/DebugInfo/PDB/CMakeLists.txt
+++ b/llvm/lib/DebugInfo/PDB/CMakeLists.txt
@@ -4,12 +4,7 @@ macro(add_pdb_impl_folder group)
 endmacro()
 
 if(LLVM_ENABLE_DIA_SDK)
-  include_directories(${MSVC_DIA_SDK_DIR}/include)
-  set(LIBPDB_LINK_FOLDERS "${MSVC_DIA_SDK_DIR}\\lib")
-  if (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)
+  include(AddDIA)
 
   add_pdb_impl_folder(DIA
     DIA/DIADataStream.cpp
@@ -136,4 +131,6 @@ add_llvm_component_library(LLVMDebugInfoPDB
   ${LIBPDB_ADDITIONAL_HEADER_DIRS}
   )
 
-target_link_libraries(LLVMDebugInfoPDB INTERFACE "${LIBPDB_ADDITIONAL_LIBRARIES}")
+if(LLVM_ENABLE_DIA_SDK)
+  target_link_libraries(LLVMDebugInfoPDB INTERFACE MSVC_diaguids)
+endif()
