Skip to content

Commit eaf8f7d

Browse files
committed
Make the SourceKit plugins work on Windows
1 parent 52e0351 commit eaf8f7d

File tree

19 files changed

+276
-43
lines changed

19 files changed

+276
-43
lines changed

Documentation/Environment Variables.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ The following environment variables can be used to control some behavior in Sour
1818
- `SOURCEKIT_LSP_KEEP_TEST_SCRATCH_DIR`: Does not delete the temporary files created during test execution. Allows inspection of the test projects after the test finishes.
1919
- `SOURCEKIT_LSP_TEST_MODULE_CACHE`: Specifies where tests should store their shared module cache. Defaults to writing the module cache to a temporary directory. Intended so that CI systems can clean the module cache directory after running.
2020
- `SOURCEKIT_LSP_TEST_TIMEOUT`: Override the timeout duration for tests, in seconds.
21+
- `SOURCEKIT_LSP_TEST_PLUGIN_PATHS`: Load the SourceKit plugins from this path instead of relative to the package's build folder.

Package.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -540,11 +540,13 @@ var targets: [Target] = [
540540
dependencies: [
541541
"Csourcekitd",
542542
"SourceKitDForPlugin",
543+
"SwiftExtensionsForPlugin",
543544
"SwiftSourceKitPluginCommon",
544545
],
545546
swiftSettings: globalSwiftSettings + [
546547
.unsafeFlags([
547548
"-module-alias", "SourceKitD=SourceKitDForPlugin",
549+
"-module-alias", "SwiftExtensions=SwiftExtensionsForPlugin",
548550
])
549551
]
550552
),
@@ -662,11 +664,11 @@ var targets: [Target] = [
662664
if buildOnlyTests {
663665
products = []
664666
targets = targets.compactMap { target in
665-
guard target.isTest || target.name == "SKTestSupport" else {
667+
guard target.isTest || target.name.contains("TestSupport") else {
666668
return nil
667669
}
668670
target.dependencies = target.dependencies.filter { dependency in
669-
if case .byNameItem(name: "SKTestSupport", _) = dependency {
671+
if case .byNameItem(name: let name, _) = dependency, name.contains("TestSupport") {
670672
return true
671673
}
672674
return false
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
add_library(CCompletionScoring INTERFACE)
2+
target_include_directories(CCompletionScoring INTERFACE "include")

Sources/CCompletionScoring/include/CCompletionScoring.h

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,28 @@
1616
#define _GNU_SOURCE
1717
#include <string.h>
1818

19-
static inline void *sourcekitlsp_memmem(const void *haystack, size_t haystacklen, const void *needle, size_t needlelen) {
19+
static inline void *sourcekitlsp_memmem(const void *haystack, size_t haystack_len, const void *needle, size_t needle_len) {
20+
#if defined(_WIN32) && !defined(__CYGWIN__)
21+
// memmem is not available on Windows
22+
if (!haystack || haystack_len == 0) {
23+
return NULL;
24+
}
25+
if (!needle || needle_len == 0) {
26+
return NULL;
27+
}
28+
if (needle_len > haystack_len) {
29+
return NULL;
30+
}
31+
32+
for (size_t offset = 0; offset <= haystack_len - needle_len; ++offset) {
33+
if (memcmp(haystack + offset, needle, needle_len) == 0) {
34+
return (void *)haystack + offset;
35+
}
36+
}
37+
return NULL;
38+
#else
2039
return memmem(haystack, haystacklen, needle, needlelen);
40+
#endif
2141
}
2242

2343
#endif // SOURCEKITLSP_CCOMPLETIONSCORING_H
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module CCompletionScoring {
2+
header "CCompletionScoring.h"
3+
export *
4+
}

Sources/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ add_compile_options("$<$<COMPILE_LANGUAGE:Swift>:SHELL:-swift-version 6>")
44
add_subdirectory(BuildServerProtocol)
55
add_subdirectory(BuildSystemIntegration)
66
add_subdirectory(CAtomics)
7+
add_subdirectory(CCompletionScoring)
8+
add_subdirectory(CompletionScoring)
79
add_subdirectory(Csourcekitd)
810
add_subdirectory(Diagnose)
911
add_subdirectory(InProcessClient)
@@ -18,5 +20,8 @@ add_subdirectory(SourceKitLSP)
1820
add_subdirectory(SourceKitD)
1921
add_subdirectory(sourcekit-lsp)
2022
add_subdirectory(SwiftExtensions)
23+
add_subdirectory(SwiftSourceKitClientPlugin)
24+
add_subdirectory(SwiftSourceKitPlugin)
25+
add_subdirectory(SwiftSourceKitPluginCommon)
2126
add_subdirectory(ToolchainRegistry)
2227
add_subdirectory(TSCExtensions)
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
set(sources
2+
Semantics/SemanticClassification.swift
3+
Semantics/CompletionScore.swift
4+
Semantics/Components
5+
Semantics/Components/TypeCompatibility.swift
6+
Semantics/Components/StructuralProximity.swift
7+
Semantics/Components/Flair.swift
8+
Semantics/Components/CompletionKind.swift
9+
Semantics/Components/Popularity.swift
10+
Semantics/Components/ScopeProximity.swift
11+
Semantics/Components/Availability.swift
12+
Semantics/Components/SynchronicityCompatibility.swift
13+
Semantics/Components/PopularityIndex.swift
14+
Semantics/Components/ModuleProximity.swift
15+
Semantics/Components/PopularityTable.swift
16+
Utilities/UnsafeStackAllocator.swift
17+
Utilities/Serialization
18+
Utilities/Serialization/BinaryCodable.swift
19+
Utilities/Serialization/BinaryEncoder.swift
20+
Utilities/Serialization/BinaryDecoder.swift
21+
Utilities/Serialization/Conformances
22+
Utilities/Serialization/Conformances/Dictionary+BinaryCodable.swift
23+
Utilities/Serialization/Conformances/OptionSet+BinaryCodable.swift
24+
Utilities/Serialization/Conformances/Optional+BinaryCodable.swift
25+
Utilities/Serialization/Conformances/Scalars+BinaryCodable.swift
26+
Utilities/Serialization/Conformances/String+BinaryCodable.swift
27+
Utilities/Serialization/Conformances/Array+BinaryCodable.swift
28+
Utilities/SwiftExtensions.swift
29+
Utilities/SelectTopK.swift
30+
Utilities/UnsafeArray.swift
31+
Text/CandidateBatch.swift
32+
Text/MatchCollator.Match.swift
33+
Text/UTF8Byte.swift
34+
Text/MatchCollator.swift
35+
Text/InfluencingIdentifiers.swift
36+
Text/MatchCollator.Selection.swift
37+
Text/ScoredMatchSelector.swift
38+
Text/RejectionFilter.swift
39+
Text/Pattern.swift)
40+
41+
add_library(CompletionScoring STATIC ${sources})
42+
set_target_properties(CompletionScoring PROPERTIES
43+
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})
44+
target_link_libraries(CompletionScoring PUBLIC
45+
CCompletionScoring)
46+
47+
add_library(CompletionScoringForPlugin STATIC ${sources})
48+
set_target_properties(CompletionScoringForPlugin PROPERTIES
49+
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})
50+
target_link_libraries(CompletionScoringForPlugin PUBLIC
51+
CCompletionScoring)

Sources/Csourcekitd/include/CodeCompletionSwiftInterop.h

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ typedef void *swiftide_api_connection_t;
2424
/// may be more expensive to compute.
2525
typedef void *swiftide_api_completion_item_t;
2626

27-
typedef enum {
27+
enum swiftide_api_completion_kind_t: uint32_t {
2828
SWIFTIDE_COMPLETION_KIND_NONE = 0,
2929
SWIFTIDE_COMPLETION_KIND_IMPORT = 1,
3030
SWIFTIDE_COMPLETION_KIND_UNRESOLVEDMEMBER = 2,
@@ -69,17 +69,17 @@ typedef enum {
6969
SWIFTIDE_COMPLETION_KIND_TYPESIMPLEORCOMPOSITION = 41,
7070
SWIFTIDE_COMPLETION_KIND_TYPEPOSSIBLEFUNCTIONPARAMBEGINNING = 42,
7171
SWIFTIDE_COMPLETION_KIND_TYPEATTRINHERITANCEBEGINNING = 43,
72-
} swiftide_api_completion_kind_t;
72+
};
7373

74-
typedef enum {
74+
enum swiftide_api_completion_item_kind_t: uint32_t {
7575
SWIFTIDE_COMPLETION_ITEM_KIND_DECLARATION = 0,
7676
SWIFTIDE_COMPLETION_ITEM_KIND_KEYWORD = 1,
7777
SWIFTIDE_COMPLETION_ITEM_KIND_PATTERN = 2,
7878
SWIFTIDE_COMPLETION_ITEM_KIND_LITERAL = 3,
7979
SWIFTIDE_COMPLETION_ITEM_KIND_BUILTINOPERATOR = 4,
80-
} swiftide_api_completion_item_kind_t;
80+
};
8181

82-
typedef enum {
82+
enum swiftide_api_completion_item_decl_kind_t: uint32_t {
8383
SWIFTIDE_COMPLETION_ITEM_DECL_KIND_MODULE = 0,
8484
SWIFTIDE_COMPLETION_ITEM_DECL_KIND_CLASS = 1,
8585
SWIFTIDE_COMPLETION_ITEM_DECL_KIND_STRUCT = 2,
@@ -105,18 +105,18 @@ typedef enum {
105105
SWIFTIDE_COMPLETION_ITEM_DECL_KIND_PRECEDENCEGROUP = 22,
106106
SWIFTIDE_COMPLETION_ITEM_DECL_KIND_ACTOR = 23,
107107
SWIFTIDE_COMPLETION_ITEM_DECL_KIND_MACRO = 24,
108-
} swiftide_api_completion_item_decl_kind_t;
108+
};
109109

110-
typedef enum {
110+
enum swiftide_api_completion_type_relation_t: uint32_t {
111111
SWIFTIDE_COMPLETION_TYPE_RELATION_NOTAPPLICABLE = 0,
112112
SWIFTIDE_COMPLETION_TYPE_RELATION_UNKNOWN = 1,
113113
SWIFTIDE_COMPLETION_TYPE_RELATION_UNRELATED = 2,
114114
SWIFTIDE_COMPLETION_TYPE_RELATION_INVALID = 3,
115115
SWIFTIDE_COMPLETION_TYPE_RELATION_CONVERTIBLE = 4,
116116
SWIFTIDE_COMPLETION_TYPE_RELATION_IDENTICAL = 5,
117-
} swiftide_api_completion_type_relation_t;
117+
};
118118

119-
typedef enum {
119+
enum swiftide_api_completion_semantic_context_t: uint32_t {
120120
SWIFTIDE_COMPLETION_SEMANTIC_CONTEXT_NONE = 0,
121121
/* obsoleted */SWIFTIDE_COMPLETION_SEMANTIC_CONTEXT_EXPRESSIONSPECIFIC = 1,
122122
SWIFTIDE_COMPLETION_SEMANTIC_CONTEXT_LOCAL = 2,
@@ -125,19 +125,19 @@ typedef enum {
125125
SWIFTIDE_COMPLETION_SEMANTIC_CONTEXT_OUTSIDENOMINAL = 5,
126126
SWIFTIDE_COMPLETION_SEMANTIC_CONTEXT_CURRENTMODULE = 6,
127127
SWIFTIDE_COMPLETION_SEMANTIC_CONTEXT_OTHERMODULE = 7,
128-
} swiftide_api_completion_semantic_context_t;
128+
};
129129

130-
typedef enum {
130+
enum swiftide_api_completion_flair_t: uint32_t {
131131
SWIFTIDE_COMPLETION_FLAIR_EXPRESSIONSPECIFIC = 1 << 0,
132132
SWIFTIDE_COMPLETION_FLAIR_SUPERCHAIN = 1 << 1,
133133
SWIFTIDE_COMPLETION_FLAIR_ARGUMENTLABELS = 1 << 2,
134134
SWIFTIDE_COMPLETION_FLAIR_COMMONKEYWORDATCURRENTPOSITION = 1 << 3,
135135
SWIFTIDE_COMPLETION_FLAIR_RAREKEYWORDATCURRENTPOSITION = 1 << 4,
136136
SWIFTIDE_COMPLETION_FLAIR_RARETYPEATCURRENTPOSITION = 1 << 5,
137137
SWIFTIDE_COMPLETION_FLAIR_EXPRESSIONATNONSCRIPTORMAINFILESCOPE = 1 << 6,
138-
} swiftide_api_completion_flair_t;
138+
};
139139

140-
typedef enum {
140+
enum swiftide_api_completion_not_recommended_reason_t: uint32_t {
141141
SWIFTIDE_COMPLETION_NOT_RECOMMENDED_NONE = 0,
142142
SWIFTIDE_COMPLETION_NOT_RECOMMENDED_REDUNDANT_IMPORT = 1,
143143
SWIFTIDE_COMPLETION_NOT_RECOMMENDED_DEPRECATED = 2,
@@ -147,15 +147,15 @@ typedef enum {
147147
SWIFTIDE_COMPLETION_NOT_RECOMMENDED_REDUNDANT_IMPORT_INDIRECT = 6,
148148
SWIFTIDE_COMPLETION_NOT_RECOMMENDED_SOFTDEPRECATED = 7,
149149
SWIFTIDE_COMPLETION_NOT_RECOMMENDED_NON_ASYNC_ALTERNATIVE_USED_IN_ASYNC_CONTEXT = 8,
150-
} swiftide_api_completion_not_recommended_reason_t;
150+
};
151151

152-
typedef enum {
152+
enum swiftide_api_completion_diagnostic_severity_t: uint32_t {
153153
SWIFTIDE_COMPLETION_DIAGNOSTIC_SEVERITY_NONE = 0,
154154
SWIFTIDE_COMPLETION_DIAGNOSTIC_SEVERITY_ERROR = 1,
155155
SWIFTIDE_COMPLETION_DIAGNOSTIC_SEVERITY_WARNING = 2,
156156
SWIFTIDE_COMPLETION_DIAGNOSTIC_SEVERITY_REMARK = 3,
157157
SWIFTIDE_COMPLETION_DIAGNOSTIC_SEVERITY_NOTE = 4,
158-
} swiftide_api_completion_diagnostic_severity_t;
158+
};
159159

160160
typedef void *swiftide_api_completion_request_t;
161161

@@ -278,7 +278,7 @@ typedef struct {
278278
uint64_t index
279279
);
280280

281-
swiftide_api_completion_kind_t (*_Nonnull completion_result_get_kind)(
281+
enum swiftide_api_completion_kind_t (*_Nonnull completion_result_get_kind)(
282282
_Null_unspecified swiftide_api_completion_response_t
283283
);
284284

@@ -361,7 +361,7 @@ typedef struct {
361361
void (*_Nonnull completion_item_get_diagnostic)(
362362
_Null_unspecified swiftide_api_completion_response_t,
363363
_Null_unspecified swiftide_api_completion_item_t,
364-
void (^_Null_unspecified handler)(swiftide_api_completion_diagnostic_severity_t, const char *_Null_unspecified)
364+
void (^_Null_unspecified handler)(enum swiftide_api_completion_diagnostic_severity_t, const char *_Null_unspecified)
365365
);
366366

367367
bool (*_Nonnull completion_item_is_system)(

Sources/SKLogging/CMakeLists.txt

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
add_library(SKLogging STATIC
1+
set(sources
22
CustomLogStringConvertible.swift
33
Error+ForLogging.swift
44
Logging.swift
@@ -7,10 +7,25 @@ add_library(SKLogging STATIC
77
OrLog.swift
88
SetGlobalLogFileHandler.swift
99
SplitLogMessage.swift)
10+
11+
add_library(SKLogging STATIC ${sources})
1012
set_target_properties(SKLogging PROPERTIES
1113
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})
1214
target_link_libraries(SKLogging PRIVATE
1315
$<$<NOT:$<PLATFORM_ID:Darwin>>:Foundation>)
1416
target_link_libraries(SKLogging PUBLIC
1517
SwiftExtensions
1618
Crypto)
19+
20+
add_library(SKLoggingForPlugin STATIC ${sources})
21+
set_target_properties(SKLoggingForPlugin PROPERTIES
22+
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})
23+
target_compile_options(SKLoggingForPlugin PRIVATE
24+
$<$<COMPILE_LANGUAGE:Swift>:
25+
-DNO_CRYPTO_DEPENDENCY;
26+
"SHELL:-module-alias SwiftExtensions=SwiftExtensionsForPlugin"
27+
>)
28+
target_link_libraries(SKLoggingForPlugin PRIVATE
29+
$<$<NOT:$<PLATFORM_ID:Darwin>>:Foundation>)
30+
target_link_libraries(SKLoggingForPlugin PUBLIC
31+
SwiftExtensionsForPlugin)

Sources/SKTestSupport/PluginPaths.swift

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,17 @@ private func pluginPaths(relativeTo base: URL) -> PluginPaths? {
8888
// When building using 'swift test'
8989
do {
9090
#if canImport(Darwin)
91-
let dylibExtension = "dylib"
91+
let clientPluginName = "libSwiftSourceKitClientPlugin.dylib"
92+
let servicePluginName = "libSwiftSourceKitPlugin.dylib"
9293
#elseif os(Windows)
93-
let dylibExtension = "dll"
94+
let clientPluginName = "SwiftSourceKitClientPlugin.dll"
95+
let servicePluginName = "SwiftSourceKitPlugin.dll"
9496
#else
95-
let dylibExtension = "so"
97+
let clientPluginName = "libSwiftSourceKitClientPlugin.so"
98+
let servicePluginName = "libSwiftSourceKitPlugin.so"
9699
#endif
97-
let clientPlugin = base.appendingPathComponent("libSwiftSourceKitClientPlugin.\(dylibExtension)")
98-
let servicePlugin = base.appendingPathComponent("libSwiftSourceKitPlugin.\(dylibExtension)")
100+
let clientPlugin = base.appendingPathComponent(clientPluginName)
101+
let servicePlugin = base.appendingPathComponent(servicePluginName)
99102
if fileExists(at: clientPlugin) && fileExists(at: servicePlugin) {
100103
return PluginPaths(clientPlugin: clientPlugin, servicePlugin: servicePlugin)
101104
}
@@ -113,16 +116,19 @@ package var sourceKitPluginPaths: PluginPaths {
113116
"Could not find SourceKit plugin. Ensure that you build the entire SourceKit-LSP package before running tests."
114117
}
115118

116-
var base = xctestBundle
119+
var base =
120+
if let pluginPaths = ProcessInfo.processInfo.environment["SOURCEKIT_LSP_TEST_PLUGIN_PATHS"] {
121+
URL(fileURLWithPath: pluginPaths)
122+
} else {
123+
xctestBundle
124+
}
117125
while base.pathComponents.count > 1 {
118126
if let paths = pluginPaths(relativeTo: base) {
119127
return paths
120128
}
121129
base = base.deletingLastPathComponent()
122130
}
123131

124-
// If we couldn't find the plugins, keep `didLoadPlugin = false`, which will throw an error in each test case's
125-
// `setUp` function. We can't throw an error from the class `setUp` function.
126132
throw PluginLoadingError()
127133
}
128134
}

Sources/SKUtilities/CMakeLists.txt

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
1-
2-
add_library(SKUtilities STATIC
1+
set(sources
32
Debouncer.swift
43
Dictionary+InitWithElementsKeyedBy.swift
54
LineTable.swift
65
)
6+
7+
add_library(SKUtilities STATIC ${sources})
78
set_target_properties(SKUtilities PROPERTIES
89
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})
910
target_link_libraries(SKUtilities PRIVATE
1011
SKLogging
1112
SwiftExtensions
12-
TSCBasic
1313
$<$<NOT:$<PLATFORM_ID:Darwin>>:Foundation>)
14+
15+
add_library(SKUtilitiesForPlugin STATIC ${sources})
16+
set_target_properties(SKUtilitiesForPlugin PROPERTIES
17+
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})
18+
target_compile_options(SKUtilitiesForPlugin PRIVATE
19+
$<$<COMPILE_LANGUAGE:Swift>:
20+
"SHELL:-module-alias SKLogging=SKLoggingForPlugin"
21+
"SHELL:-module-alias SwiftExtensions=SwiftExtensionsForPlugin"
22+
>)
23+
target_link_libraries(SKUtilitiesForPlugin PRIVATE
24+
SKLoggingForPlugin
25+
SwiftExtensionsForPlugin
26+
$<$<NOT:$<PLATFORM_ID:Darwin>>:Foundation>)

0 commit comments

Comments
 (0)