Skip to content

Commit c77273e

Browse files
authored
Fixed problems when build JavaKitSampleApp on Windows (#400)
1 parent 791e734 commit c77273e

File tree

5 files changed

+28
-16
lines changed

5 files changed

+28
-16
lines changed

Plugins/JavaCompilerPlugin/JavaCompilerPlugin.swift

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ struct JavaCompilerBuildToolPlugin: BuildToolPlugin {
3030

3131
// Note: Target doesn't have a directoryURL counterpart to directory,
3232
// so we cannot eliminate this deprecation warning.
33-
let sourceDir = target.directory.string
33+
let sourceDir = URL(filePath: target.directory.string)
3434

3535
// The name of the configuration file SwiftJava.config from the target for
3636
// which we are generating Swift wrappers for Java classes.
37-
let configFile = URL(filePath: sourceDir).appending(path: "swift-java.config")
37+
let configFile = sourceDir.appending(path: "swift-java.config")
3838
let config: Configuration?
3939

4040
if let configData = try? Data(contentsOf: configFile) {
@@ -51,28 +51,34 @@ struct JavaCompilerBuildToolPlugin: BuildToolPlugin {
5151
}
5252

5353
let sourceFilePath = sourceFileURL.path
54-
guard sourceFilePath.starts(with: sourceDir) else {
54+
let sourceDirPath = sourceDir.path
55+
guard sourceFilePath.starts(with: sourceDirPath) else {
5556
fatalError("Could not get relative path for source file \(sourceFilePath)")
5657
}
5758

5859
return URL(filePath: context.pluginWorkDirectoryURL.path)
5960
.appending(path: "Java")
60-
.appending(path: String(sourceFilePath.dropFirst(sourceDir.count)))
61+
.appending(path: String(sourceFilePath.dropFirst(sourceDirPath.count)))
6162
.deletingPathExtension()
6263
.appendingPathExtension("class")
6364
}
6465

6566
let javaHome = URL(filePath: findJavaHome())
6667
let javaClassFileURL = context.pluginWorkDirectoryURL
6768
.appending(path: "Java")
69+
#if os(Windows)
70+
let javac = "javac.exe"
71+
#else
72+
let javac = "javac"
73+
#endif
6874
return [
6975
.buildCommand(
7076
displayName: "Compiling \(javaFiles.count) Java files for target \(sourceModule.name) to \(javaClassFileURL)",
7177
executable: javaHome
7278
.appending(path: "bin")
73-
.appending(path: "javac"),
74-
arguments: javaFiles.map { $0.path(percentEncoded: false) } + [
75-
"-d", javaClassFileURL.path(),
79+
.appending(path: javac),
80+
arguments: javaFiles.map { $0.path } + [
81+
"-d", javaClassFileURL.path,
7682
"-parameters", // keep parameter names, which allows us to emit them in generated Swift decls
7783
] + (config?.compilerVersionArgs ?? []),
7884
inputFiles: javaFiles,

Samples/JavaKitSampleApp/Package.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,8 @@ let javaIncludePath = "\(javaHome)/include"
3535
let javaPlatformIncludePath = "\(javaIncludePath)/linux"
3636
#elseif os(macOS)
3737
let javaPlatformIncludePath = "\(javaIncludePath)/darwin"
38-
#else
39-
// TODO: Handle windows as well
40-
#error("Currently only macOS and Linux platforms are supported, this may change in the future.")
38+
#elseif os(Windows)
39+
let javaPlatformIncludePath = "\(javaIncludePath)/win32)"
4140
#endif
4241

4342
let package = Package(

Sources/SwiftJava/JavaKitVM/JavaVirtualMachine.swift

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ typealias JNIEnvPointer = UnsafeMutablePointer<JNIEnv?>
2525
typealias JNIEnvPointer = UnsafeMutableRawPointer
2626
#endif
2727

28+
extension FileManager {
29+
#if os(Windows)
30+
static let pathSeparator = ";"
31+
#else
32+
static let pathSeparator = ":"
33+
#endif
34+
}
35+
2836
public final class JavaVirtualMachine: @unchecked Sendable {
2937
/// The JNI version that we depend on.
3038
static let jniVersion = JNI_VERSION_1_6
@@ -81,8 +89,8 @@ public final class JavaVirtualMachine: @unchecked Sendable {
8189
print("[warning][swift-java][JavaVirtualMachine] Missing classpath element: \(URL(fileURLWithPath: path).absoluteString)") // TODO: stderr
8290
}
8391
}
84-
let colonSeparatedClassPath = classpath.joined(separator: ":")
85-
allVMOptions.append("-Djava.class.path=\(colonSeparatedClassPath)")
92+
let pathSeparatedClassPath = classpath.joined(separator: FileManager.pathSeparator)
93+
allVMOptions.append("-Djava.class.path=\(pathSeparatedClassPath)")
8694
}
8795
allVMOptions.append(contentsOf: vmOptions)
8896

@@ -237,7 +245,7 @@ extension JavaVirtualMachine {
237245
ignoreUnrecognized: Bool = false,
238246
replace: Bool = false
239247
) throws -> JavaVirtualMachine {
240-
precondition(!classpath.contains(where: { $0.contains(":") }), "Classpath element must not contain `:`! Split the path into elements! Was: \(classpath)")
248+
precondition(!classpath.contains(where: { $0.contains(FileManager.pathSeparator) }), "Classpath element must not contain `\(FileManager.pathSeparator)`! Split the path into elements! Was: \(classpath)")
241249

242250
return try sharedJVM.withLock { (sharedJVMPointer: inout JavaVirtualMachine?) in
243251
// If we already have a JavaVirtualMachine instance, return it.

Sources/SwiftJava/JavaKitVM/ThreadLocalStorage.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ package struct ThreadLocalStorage: ~Copyable {
5656
_key = 0
5757
pthread_key_create(&_key, onThreadExit)
5858
#elseif canImport(WinSDK)
59-
key = FlsAlloc(onThreadExit)
59+
_key = FlsAlloc(onThreadExit)
6060
#endif
6161
}
6262

Sources/_Subprocess/Platforms/Subprocess+Windows.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,8 +1213,7 @@ extension UInt8 {
12131213
extension OutputProtocol {
12141214
internal func output(from data: [UInt8]) throws -> OutputType {
12151215
return try data.withUnsafeBytes {
1216-
let span = RawSpan(_unsafeBytes: $0)
1217-
return try self.output(from: span)
1216+
return try self.output(from: $0)
12181217
}
12191218
}
12201219
}

0 commit comments

Comments
 (0)