Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions Plugins/JavaCompilerPlugin/JavaCompilerPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ struct JavaCompilerBuildToolPlugin: BuildToolPlugin {

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

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

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

let sourceFilePath = sourceFileURL.path
guard sourceFilePath.starts(with: sourceDir) else {
let sourceDirPath = sourceDir.path
guard sourceFilePath.starts(with: sourceDirPath) else {
fatalError("Could not get relative path for source file \(sourceFilePath)")
}

return URL(filePath: context.pluginWorkDirectoryURL.path)
.appending(path: "Java")
.appending(path: String(sourceFilePath.dropFirst(sourceDir.count)))
.appending(path: String(sourceFilePath.dropFirst(sourceDirPath.count)))
.deletingPathExtension()
.appendingPathExtension("class")
}

let javaHome = URL(filePath: findJavaHome())
let javaClassFileURL = context.pluginWorkDirectoryURL
.appending(path: "Java")
#if os(Windows)
let javac = "javac.exe"
#else
let javac = "javac"
#endif
return [
.buildCommand(
displayName: "Compiling \(javaFiles.count) Java files for target \(sourceModule.name) to \(javaClassFileURL)",
executable: javaHome
.appending(path: "bin")
.appending(path: "javac"),
arguments: javaFiles.map { $0.path(percentEncoded: false) } + [
"-d", javaClassFileURL.path(),
.appending(path: javac),
arguments: javaFiles.map { $0.path } + [
"-d", javaClassFileURL.path,
"-parameters", // keep parameter names, which allows us to emit them in generated Swift decls
] + (config?.compilerVersionArgs ?? []),
inputFiles: javaFiles,
Expand Down
5 changes: 2 additions & 3 deletions Samples/JavaKitSampleApp/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@ let javaIncludePath = "\(javaHome)/include"
let javaPlatformIncludePath = "\(javaIncludePath)/linux"
#elseif os(macOS)
let javaPlatformIncludePath = "\(javaIncludePath)/darwin"
#else
// TODO: Handle windows as well
#error("Currently only macOS and Linux platforms are supported, this may change in the future.")
#elseif os(Windows)
let javaPlatformIncludePath = "\(javaIncludePath)/win32)"
#endif

let package = Package(
Expand Down
14 changes: 11 additions & 3 deletions Sources/SwiftJava/JavaKitVM/JavaVirtualMachine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ typealias JNIEnvPointer = UnsafeMutablePointer<JNIEnv?>
typealias JNIEnvPointer = UnsafeMutableRawPointer
#endif

extension FileManager {
#if os(Windows)
static let pathSeparator = ";"
#else
static let pathSeparator = ":"
#endif
}

public final class JavaVirtualMachine: @unchecked Sendable {
/// The JNI version that we depend on.
static let jniVersion = JNI_VERSION_1_6
Expand Down Expand Up @@ -81,8 +89,8 @@ public final class JavaVirtualMachine: @unchecked Sendable {
print("[warning][swift-java][JavaVirtualMachine] Missing classpath element: \(URL(fileURLWithPath: path).absoluteString)") // TODO: stderr
}
}
let colonSeparatedClassPath = classpath.joined(separator: ":")
allVMOptions.append("-Djava.class.path=\(colonSeparatedClassPath)")
let pathSeparatedClassPath = classpath.joined(separator: FileManager.pathSeparator)
allVMOptions.append("-Djava.class.path=\(pathSeparatedClassPath)")
}
allVMOptions.append(contentsOf: vmOptions)

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

return try sharedJVM.withLock { (sharedJVMPointer: inout JavaVirtualMachine?) in
// If we already have a JavaVirtualMachine instance, return it.
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftJava/JavaKitVM/ThreadLocalStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ package struct ThreadLocalStorage: ~Copyable {
_key = 0
pthread_key_create(&_key, onThreadExit)
#elseif canImport(WinSDK)
key = FlsAlloc(onThreadExit)
_key = FlsAlloc(onThreadExit)
#endif
}

Expand Down
3 changes: 1 addition & 2 deletions Sources/_Subprocess/Platforms/Subprocess+Windows.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1213,8 +1213,7 @@ extension UInt8 {
extension OutputProtocol {
internal func output(from data: [UInt8]) throws -> OutputType {
return try data.withUnsafeBytes {
let span = RawSpan(_unsafeBytes: $0)
return try self.output(from: span)
return try self.output(from: $0)
}
}
}
Expand Down