From 28f9db7bfa0186520f35cad9557c0c0b74df8462 Mon Sep 17 00:00:00 2001 From: Egor Zhdan Date: Tue, 26 Aug 2025 12:02:08 +0100 Subject: [PATCH] [WinSDK] Modularize `` This fixes modularization errors that arise when importing a C++ header that contains `#include `, which might hijack this header from the WinSDK module where it belongs. --- Runtimes/Overlay/Windows/clang/CMakeLists.txt | 11 +++++++++-- Runtimes/Resync.cmake | 3 ++- lib/ClangImporter/ClangIncludePaths.cpp | 17 +++++++++++++---- stdlib/cmake/WindowsVFS.yaml.in | 8 +++++++- stdlib/public/Platform/CMakeLists.txt | 3 ++- stdlib/public/Platform/winsdk_shared.modulemap | 16 ++++++++++++++++ .../{winsdk.modulemap => winsdk_um.modulemap} | 2 +- stdlib/public/Windows/WinSDK.swift | 1 + test/stdlib/WinSDK_GUID.swift | 9 +++++++++ 9 files changed, 60 insertions(+), 10 deletions(-) create mode 100644 stdlib/public/Platform/winsdk_shared.modulemap rename stdlib/public/Platform/{winsdk.modulemap => winsdk_um.modulemap} (99%) create mode 100644 test/stdlib/WinSDK_GUID.swift diff --git a/Runtimes/Overlay/Windows/clang/CMakeLists.txt b/Runtimes/Overlay/Windows/clang/CMakeLists.txt index 79958577cf6cc..1d88b8ed024ad 100644 --- a/Runtimes/Overlay/Windows/clang/CMakeLists.txt +++ b/Runtimes/Overlay/Windows/clang/CMakeLists.txt @@ -18,7 +18,13 @@ roots: contents: - name: module.modulemap type: file - external-contents: "@CMAKE_CURRENT_SOURCE_DIR@/winsdk.modulemap" + external-contents: "@CMAKE_CURRENT_SOURCE_DIR@/winsdk_um.modulemap" + - name: "@WindowsSdkDir@/Include/@WindowsSDKVersion@/shared" + type: directory + contents: + - name: module.modulemap + type: file + external-contents: "@CMAKE_CURRENT_SOURCE_DIR@/winsdk_shared.modulemap" - name: "@UniversalCRTSdkDir@/Include/@UCRTVersion@/ucrt" type: directory contents: @@ -47,5 +53,6 @@ install(FILES ucrt.modulemap vcruntime.apinotes vcruntime.modulemap - winsdk.modulemap + winsdk_um.modulemap + winsdk_shared.modulemap DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}) diff --git a/Runtimes/Resync.cmake b/Runtimes/Resync.cmake index 822e8044212c0..9b93b807e85a2 100644 --- a/Runtimes/Resync.cmake +++ b/Runtimes/Resync.cmake @@ -158,7 +158,8 @@ message(STATUS "Windows modulemaps[${StdlibSources}/Platform] -> ${CMAKE_CURRENT copy_files(public/Platform Overlay/Windows/clang FILES ucrt.modulemap - winsdk.modulemap + winsdk_um.modulemap + winsdk_shared.modulemap vcruntime.modulemap vcruntime.apinotes) diff --git a/lib/ClangImporter/ClangIncludePaths.cpp b/lib/ClangImporter/ClangIncludePaths.cpp index 948b99876f02d..4eb9548656465 100644 --- a/lib/ClangImporter/ClangIncludePaths.cpp +++ b/lib/ClangImporter/ClangIncludePaths.cpp @@ -482,14 +482,23 @@ void GetWindowsFileMappings( WindowsSDK.Path, WindowsSDK.MajorVersion, WindowsSDK.IncludeVersion, WindowsSDK.LibraryVersion)) { + assert(WindowsSDK.MajorVersion > 8); llvm::SmallString<261> WinSDKInjection{WindowsSDK.Path}; llvm::sys::path::append(WinSDKInjection, "Include"); - if (WindowsSDK.MajorVersion > 8) - llvm::sys::path::append(WinSDKInjection, WindowsSDK.IncludeVersion, "um"); + llvm::sys::path::append(WinSDKInjection, WindowsSDK.IncludeVersion, "um"); llvm::sys::path::append(WinSDKInjection, "module.modulemap"); - AuxiliaryFile = - GetPlatformAuxiliaryFile("windows", "winsdk.modulemap", SearchPathOpts); + AuxiliaryFile = GetPlatformAuxiliaryFile("windows", "winsdk_um.modulemap", + SearchPathOpts); + if (!AuxiliaryFile.empty()) + fileMapping.redirectedFiles.emplace_back(std::string(WinSDKInjection), + AuxiliaryFile); + + llvm::sys::path::remove_filename(WinSDKInjection); + llvm::sys::path::remove_filename(WinSDKInjection); + llvm::sys::path::append(WinSDKInjection, "shared", "module.modulemap"); + AuxiliaryFile = GetPlatformAuxiliaryFile( + "windows", "winsdk_shared.modulemap", SearchPathOpts); if (!AuxiliaryFile.empty()) fileMapping.redirectedFiles.emplace_back(std::string(WinSDKInjection), AuxiliaryFile); diff --git a/stdlib/cmake/WindowsVFS.yaml.in b/stdlib/cmake/WindowsVFS.yaml.in index 743915a6e5e87..9a728125ebd74 100644 --- a/stdlib/cmake/WindowsVFS.yaml.in +++ b/stdlib/cmake/WindowsVFS.yaml.in @@ -8,7 +8,13 @@ roots: contents: - name: module.modulemap type: file - external-contents: "@PROJECT_SOURCE_DIR@\\public\\Platform\\winsdk.modulemap" + external-contents: "@PROJECT_SOURCE_DIR@\\public\\Platform\\winsdk_um.modulemap" + - name: "@UniversalCRTSdkDir@\\Include\\@UCRTVersion@\\shared" + type: directory + contents: + - name: module.modulemap + type: file + external-contents: "@PROJECT_SOURCE_DIR@\\public\\Platform\\winsdk_shared.modulemap" - name: "@UniversalCRTSdkDir@\\Include\\@UCRTVersion@\\ucrt" type: directory contents: diff --git a/stdlib/public/Platform/CMakeLists.txt b/stdlib/public/Platform/CMakeLists.txt index d8365f42a3d22..083b5578eb42c 100644 --- a/stdlib/public/Platform/CMakeLists.txt +++ b/stdlib/public/Platform/CMakeLists.txt @@ -467,7 +467,8 @@ if(WINDOWS IN_LIST SWIFT_SDKS) ucrt.modulemap vcruntime.apinotes vcruntime.modulemap - winsdk.modulemap + winsdk_um.modulemap + winsdk_shared.modulemap DESTINATION "share" COMPONENT sdk-overlay) endif() diff --git a/stdlib/public/Platform/winsdk_shared.modulemap b/stdlib/public/Platform/winsdk_shared.modulemap new file mode 100644 index 0000000000000..598373d6b32e5 --- /dev/null +++ b/stdlib/public/Platform/winsdk_shared.modulemap @@ -0,0 +1,16 @@ +//===--- WinSDK_Shared.modulemap ------------------------------------------===// +// +// This source file is part of the Swift.org open source project +// +// Copyright (c) 2014 - 2025 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors +// +//===----------------------------------------------------------------------===// + +module _GUID { + header "guiddef.h" + export * +} diff --git a/stdlib/public/Platform/winsdk.modulemap b/stdlib/public/Platform/winsdk_um.modulemap similarity index 99% rename from stdlib/public/Platform/winsdk.modulemap rename to stdlib/public/Platform/winsdk_um.modulemap index 9f085740c2015..a7e12f0a08390 100644 --- a/stdlib/public/Platform/winsdk.modulemap +++ b/stdlib/public/Platform/winsdk_um.modulemap @@ -1,4 +1,4 @@ -//===--- WinSDK.modulemap -------------------------------------------------===// +//===--- WinSDK_UM.modulemap ----------------------------------------------===// // // This source file is part of the Swift.org open source project // diff --git a/stdlib/public/Windows/WinSDK.swift b/stdlib/public/Windows/WinSDK.swift index f6e36846e3d88..f2055d013c6a3 100644 --- a/stdlib/public/Windows/WinSDK.swift +++ b/stdlib/public/Windows/WinSDK.swift @@ -11,6 +11,7 @@ //===----------------------------------------------------------------------===// @_exported import ucrt +@_exported import _GUID @_exported import WinSDK // Clang module // WinBase.h diff --git a/test/stdlib/WinSDK_GUID.swift b/test/stdlib/WinSDK_GUID.swift new file mode 100644 index 0000000000000..631888157003f --- /dev/null +++ b/test/stdlib/WinSDK_GUID.swift @@ -0,0 +1,9 @@ +// RUN: %target-build-swift %s +// REQUIRES: OS=windows-msvc + +// Make sure that importing WinSDK brings in the GUID type, which is declared in +// /shared and not in /um. + +import WinSDK + +public func usesGUID(_ x: GUID) {}