From fb5b06aa6dda195bc585b1a74ae549a8c0fc784a Mon Sep 17 00:00:00 2001 From: Gabriele Mondada Date: Tue, 31 Mar 2026 15:19:54 +0200 Subject: [PATCH] Remove from the doc the use of unsafeFlags --- .../Documentation.docc/SwiftPMPlugin.md | 86 ++----------------- 1 file changed, 6 insertions(+), 80 deletions(-) diff --git a/Sources/SwiftJavaDocumentation/Documentation.docc/SwiftPMPlugin.md b/Sources/SwiftJavaDocumentation/Documentation.docc/SwiftPMPlugin.md index 9750ba92..e72f9cfa 100644 --- a/Sources/SwiftJavaDocumentation/Documentation.docc/SwiftPMPlugin.md +++ b/Sources/SwiftJavaDocumentation/Documentation.docc/SwiftPMPlugin.md @@ -9,18 +9,9 @@ The `SwiftJavaPlugin` automates `swift-java` command line tool invocations durin To install the SwiftPM plugin in your target of choice include the `swift-java` package dependency: ```swift -import Foundation +// swift-tools-version: 6.3 -let javaHome = findJavaHome() - -let javaIncludePath = "\(javaHome)/include" -#if os(Linux) - let javaPlatformIncludePath = "\(javaIncludePath)/linux" -#elseif os(macOS) - let javaPlatformIncludePath = "\(javaIncludePath)/darwin" -#elseif os(Windows) - let javaPlatformIncludePath = "\(javaIncludePath)/win32" -#endif +import PackageDescription let package = Package( name: "MyProject", @@ -44,9 +35,11 @@ let package = Package( // ... ], swiftSettings: [ - .unsafeFlags(["-I\(javaIncludePath)", "-I\(javaPlatformIncludePath)"]) + // Some swift-java generated code is not yet compatible with swift 6 + .swiftLanguageMode(.v5) ], plugins: [ + // Include here the plugins you need .plugin(name: "JavaCompilerPlugin", package: "swift-java"), .plugin(name: "JExtractSwiftPlugin", package: "swift-java"), .plugin(name: "SwiftJavaPlugin", package: "swift-java"), @@ -56,71 +49,4 @@ let package = Package( ) ``` -```swift - -// Note: the JAVA_HOME environment variable must be set to point to where -// Java is installed, e.g., -// Library/Java/JavaVirtualMachines/openjdk-21.jdk/Contents/Home. -func findJavaHome() -> String { - if let home = ProcessInfo.processInfo.environment["JAVA_HOME"] { - print("JAVA_HOME = \(home)") - return home - } - - // This is a workaround for envs (some IDEs) which have trouble with - // picking up env variables during the build process - let path = "\(FileManager.default.homeDirectoryForCurrentUser.path()).java_home" - if let home = try? String(contentsOfFile: path, encoding: .utf8) { - if let lastChar = home.last, lastChar.isNewline { - return String(home.dropLast()) - } - - return home - } - - if let home = getJavaHomeFromLibexecJavaHome(), - !home.isEmpty { - return home - } - - fatalError("Please set the JAVA_HOME environment variable to point to where Java is installed.") -} - -/// On MacOS we can use the java_home tool as a fallback if we can't find JAVA_HOME environment variable. -func getJavaHomeFromLibexecJavaHome() -> String? { - let task = Process() - task.executableURL = URL(fileURLWithPath: "/usr/libexec/java_home") - - // Check if the executable exists before trying to run it - guard FileManager.default.fileExists(atPath: task.executableURL!.path) else { - print("/usr/libexec/java_home does not exist") - return nil - } - - let pipe = Pipe() - task.standardOutput = pipe - task.standardError = pipe // Redirect standard error to the same pipe for simplicity - - do { - try task.run() - task.waitUntilExit() - - let data = pipe.fileHandleForReading.readDataToEndOfFile() - let output = String(data: data, encoding: .utf8)?.trimmingCharacters(in: .whitespacesAndNewlines) - - if task.terminationStatus == 0 { - return output - } else { - print("java_home terminated with status: \(task.terminationStatus)") - // Optionally, log the error output for debugging - if let errorOutput = String(data: pipe.fileHandleForReading.readDataToEndOfFile(), encoding: .utf8) { - print("Error output: \(errorOutput)") - } - return nil - } - } catch { - print("Error running java_home: \(error)") - return nil - } -} -``` +> Note: Depending on the use case, swift-java may require running Gradle or accessing files outside the Swift package. Ensure that your environment allows Gradle to run, and add the `--disable-sandbox` parameter when invoking the `swift build` command to build the package.