Skip to content

Commit 9a49bb7

Browse files
authored
Merge pull request #2685 from rintaro/windows-no-swiftwinsdk
[Windows] Avoid linking with swiftWinSDK.dll
2 parents b514509 + d360086 commit 9a49bb7

File tree

9 files changed

+80
-8
lines changed

9 files changed

+80
-8
lines changed

.spi.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ builder:
1313
- SwiftCompilerPluginMessageHandling
1414
- SwiftDiagnostics
1515
- SwiftIDEUtils
16+
- SwiftLibraryPluginProvider
1617
- SwiftOperators
1718
- SwiftParser
1819
- SwiftParserDiagnostics

Package.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ let package = Package(
1818
.library(name: "SwiftCompilerPluginMessageHandling", targets: ["SwiftCompilerPluginMessageHandling"]),
1919
.library(name: "SwiftDiagnostics", targets: ["SwiftDiagnostics"]),
2020
.library(name: "SwiftIDEUtils", targets: ["SwiftIDEUtils"]),
21+
.library(name: "SwiftLibraryPluginProvider", targets: ["SwiftLibraryPluginProvider"]),
2122
.library(name: "SwiftOperators", targets: ["SwiftOperators"]),
2223
.library(name: "SwiftParser", targets: ["SwiftParser"]),
2324
.library(name: "SwiftParserDiagnostics", targets: ["SwiftParserDiagnostics"]),
@@ -143,7 +144,12 @@ let package = Package(
143144

144145
.target(
145146
name: "SwiftLibraryPluginProvider",
146-
dependencies: ["SwiftSyntaxMacros", "SwiftCompilerPluginMessageHandling"],
147+
dependencies: ["SwiftSyntaxMacros", "SwiftCompilerPluginMessageHandling", "_SwiftLibraryPluginProviderCShims"],
148+
exclude: ["CMakeLists.txt"]
149+
),
150+
151+
.target(
152+
name: "_SwiftLibraryPluginProviderCShims",
147153
exclude: ["CMakeLists.txt"]
148154
),
149155

Sources/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
# See http://swift.org/LICENSE.txt for license information
77
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors
88

9+
add_subdirectory(_SwiftLibraryPluginProviderCShims)
910
add_subdirectory(_SwiftSyntaxCShims)
1011
add_subdirectory(SwiftBasicFormat)
1112
add_subdirectory(SwiftSyntax)

Sources/SwiftLibraryPluginProvider/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
add_swift_syntax_library(SwiftLibraryPluginProvider
1010
LibraryPluginProvider.swift
1111
)
12-
12+
target_link_libraries(SwiftLibraryPluginProvider PRIVATE
13+
_SwiftLibraryPluginProviderCShims
14+
)
1315
target_link_swift_syntax_libraries(SwiftLibraryPluginProvider PUBLIC
1416
SwiftSyntaxMacros
1517
SwiftCompilerPluginMessageHandling

Sources/SwiftLibraryPluginProvider/LibraryPluginProvider.swift

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#if swift(>=6.0)
1414
public import SwiftSyntaxMacros
1515
@_spi(PluginMessage) public import SwiftCompilerPluginMessageHandling
16+
private import _SwiftLibraryPluginProviderCShims
1617
// NOTE: Do not use '_SwiftSyntaxCShims' for 'dlopen' and 'LoadLibraryW' (Windows)
1718
// because we don't want other modules depend on 'WinSDK'.
1819
#if canImport(Darwin)
@@ -21,20 +22,17 @@ private import Darwin
2122
private import Glibc
2223
#elseif canImport(Musl)
2324
private import Musl
24-
#elseif canImport(WinSDK)
25-
private import WinSDK
2625
#endif
2726
#else
2827
import SwiftSyntaxMacros
2928
@_spi(PluginMessage) import SwiftCompilerPluginMessageHandling
29+
@_implementationOnly import _SwiftLibraryPluginProviderCShims
3030
#if canImport(Darwin)
3131
@_implementationOnly import Darwin
3232
#elseif canImport(Glibc)
3333
@_implementationOnly import Glibc
3434
#elseif canImport(Musl)
3535
@_implementationOnly import Musl
36-
#elseif canImport(WinSDK)
37-
@_implementationOnly import WinSDK
3836
#endif
3937
#endif
4038

@@ -130,9 +128,9 @@ private func _loadLibrary(_ path: String) throws -> UnsafeMutableRawPointer {
130128
let end = utf16Path.initialize(fromContentsOf: path.utf16)
131129
utf16Path.initializeElement(at: end, to: 0)
132130

133-
guard let dlHandle = LoadLibraryW(utf16Path.baseAddress) else {
131+
guard let dlHandle = swiftlibrarypluginprovider_LoadLibraryW(utf16Path.baseAddress) else {
134132
// FIXME: Format the error code to string.
135-
throw LibraryPluginError(message: "loader error: \(GetLastError())")
133+
throw LibraryPluginError(message: "loader error: \(swiftlibrarypluginprovider_GetLastError())")
136134
}
137135
return UnsafeMutableRawPointer(dlHandle)
138136
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
add_library(_SwiftLibraryPluginProviderCShims STATIC
2+
LoadLibrary.c
3+
)
4+
target_include_directories(_SwiftLibraryPluginProviderCShims PUBLIC "include")
5+
set_property(GLOBAL APPEND PROPERTY SWIFT_EXPORTS _SwiftLibraryPluginProviderCShims)
6+
install(TARGETS _SwiftLibraryPluginProviderCShims EXPORT SwiftSyntaxTargets)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift open source project
4+
//
5+
// Copyright (c) 2024 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See http://swift.org/LICENSE.txt for license information
9+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "LoadLibrary.h"
14+
15+
#ifdef _WIN32
16+
17+
#include <windows.h>
18+
#include <libloaderapi.h>
19+
#include <errhandlingapi.h>
20+
21+
void *swiftlibrarypluginprovider_LoadLibraryW(uint16_t *lpLibFileName) {
22+
return LoadLibraryW(lpLibFileName);
23+
}
24+
25+
unsigned long swiftlibrarypluginprovider_GetLastError() {
26+
return GetLastError();
27+
}
28+
29+
#endif /* _WIN32 */
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift open source project
4+
//
5+
// Copyright (c) 2024 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See http://swift.org/LICENSE.txt for license information
9+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef SWIFT_LIBRARY_PLUGIN_PROVIDER_LOAD_LIBRARY_H
14+
#define SWIFT_LIBRARY_PLUGIN_PROVIDER_LOAD_LIBRARY_H
15+
16+
#ifdef _WIN32
17+
18+
#include <stdint.h>
19+
20+
void *swiftlibrarypluginprovider_LoadLibraryW(uint16_t *lpLibFileName);
21+
unsigned long swiftlibrarypluginprovider_GetLastError();
22+
23+
#endif /* _WIN32 */
24+
25+
#endif /* SWIFT_LIBRARY_PLUGIN_PROVIDER_LOAD_LIBRARY_H */
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module _SwiftLibraryPluginProviderCShims {
2+
header "LoadLibrary.h"
3+
export *
4+
}

0 commit comments

Comments
 (0)