|
1 | 1 | // swift-tools-version: 6.0
|
2 | 2 | import PackageDescription
|
3 | 3 |
|
| 4 | +import Foundation |
| 5 | + |
| 6 | +// Note: the JAVA_HOME environment variable must be set to point to where |
| 7 | +// Java is installed, e.g., |
| 8 | +// Library/Java/JavaVirtualMachines/openjdk-21.jdk/Contents/Home. |
| 9 | +func findJavaHome() -> String { |
| 10 | + if let home = ProcessInfo.processInfo.environment["JAVA_HOME"] { |
| 11 | + print("JAVA_HOME = \(home)") |
| 12 | + return home |
| 13 | + } |
| 14 | + |
| 15 | + // This is a workaround for envs (some IDEs) which have trouble with |
| 16 | + // picking up env variables during the build process |
| 17 | + let path = "\(FileManager.default.homeDirectoryForCurrentUser.path()).java_home" |
| 18 | + if let home = try? String(contentsOfFile: path, encoding: .utf8) { |
| 19 | + if let lastChar = home.last, lastChar.isNewline { |
| 20 | + return String(home.dropLast()) |
| 21 | + } |
| 22 | + |
| 23 | + return home |
| 24 | + } |
| 25 | + |
| 26 | + if let home = getJavaHomeFromLibexecJavaHome(), |
| 27 | + !home.isEmpty { |
| 28 | + return home |
| 29 | + } |
| 30 | + |
| 31 | + |
| 32 | + if ProcessInfo.processInfo.environment["SPI_PROCESSING"] == "1" && ProcessInfo.processInfo.environment["SPI_BUILD"] == nil { |
| 33 | + // Just ignore that we're missing a JAVA_HOME when building in Swift Package Index during general processing where no Java is needed. However, do _not_ suppress the error during SPI's compatibility build stage where Java is required. |
| 34 | + return "" |
| 35 | + } |
| 36 | + fatalError("Please set the JAVA_HOME environment variable to point to where Java is installed.") |
| 37 | +} |
| 38 | + |
| 39 | +/// On MacOS we can use the java_home tool as a fallback if we can't find JAVA_HOME environment variable. |
| 40 | +func getJavaHomeFromLibexecJavaHome() -> String? { |
| 41 | + let task = Process() |
| 42 | + task.executableURL = URL(fileURLWithPath: "/usr/libexec/java_home") |
| 43 | + |
| 44 | + // Check if the executable exists before trying to run it |
| 45 | + guard FileManager.default.fileExists(atPath: task.executableURL!.path) else { |
| 46 | + print("/usr/libexec/java_home does not exist") |
| 47 | + return nil |
| 48 | + } |
| 49 | + |
| 50 | + let pipe = Pipe() |
| 51 | + task.standardOutput = pipe |
| 52 | + task.standardError = pipe // Redirect standard error to the same pipe for simplicity |
| 53 | + |
| 54 | + do { |
| 55 | + try task.run() |
| 56 | + task.waitUntilExit() |
| 57 | + |
| 58 | + let data = pipe.fileHandleForReading.readDataToEndOfFile() |
| 59 | + let output = String(data: data, encoding: .utf8)?.trimmingCharacters(in: .whitespacesAndNewlines) |
| 60 | + |
| 61 | + if task.terminationStatus == 0 { |
| 62 | + return output |
| 63 | + } else { |
| 64 | + print("java_home terminated with status: \(task.terminationStatus)") |
| 65 | + // Optionally, log the error output for debugging |
| 66 | + if let errorOutput = String(data: pipe.fileHandleForReading.readDataToEndOfFile(), encoding: .utf8) { |
| 67 | + print("Error output: \(errorOutput)") |
| 68 | + } |
| 69 | + return nil |
| 70 | + } |
| 71 | + } catch { |
| 72 | + print("Error running java_home: \(error)") |
| 73 | + return nil |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +let javaHome = findJavaHome() |
| 78 | + |
4 | 79 | let package = Package(
|
5 | 80 | name: "swift-jni",
|
6 | 81 | products: [
|
7 | 82 | .library(name: "SwiftJNI", targets: ["SwiftJNI"]),
|
8 | 83 | ],
|
9 | 84 | targets: [
|
10 |
| - .target(name: "SwiftJNI", dependencies: ["CSwiftJavaJNI"]), |
| 85 | + .target(name: "SwiftJNI", |
| 86 | + dependencies: ["CSwiftJavaJNI"], |
| 87 | + linkerSettings: [ |
| 88 | + .unsafeFlags( |
| 89 | + [ |
| 90 | + "-L\(javaHome)/lib/server", |
| 91 | + "-Xlinker", "-rpath", |
| 92 | + "-Xlinker", "\(javaHome)/lib/server", |
| 93 | + ], |
| 94 | + .when(platforms: [.linux, .macOS]) |
| 95 | + ), |
| 96 | + .unsafeFlags( |
| 97 | + [ |
| 98 | + "-L\(javaHome)/lib" |
| 99 | + ], |
| 100 | + .when(platforms: [.windows])), |
| 101 | + .linkedLibrary( |
| 102 | + "jvm", |
| 103 | + .when(platforms: [.linux, .macOS, .windows]) |
| 104 | + ), |
| 105 | + ] |
| 106 | + ), |
11 | 107 | .target(name: "CSwiftJavaJNI")
|
12 | 108 | ]
|
13 | 109 | )
|
0 commit comments