-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPackage.swift
More file actions
195 lines (169 loc) · 5.12 KB
/
Package.swift
File metadata and controls
195 lines (169 loc) · 5.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// swift-tools-version: 6.0
// The swift-tools-version declares the minimum version of Swift required to build this package.
import Foundation
import PackageDescription
// 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"] {
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
}
if let home = getJavaHomeFromSDKMAN() {
return home
}
if let home = getJavaHomeFromPath() {
return home
}
if ProcessInfo.processInfo.environment["SPI_PROCESSING"] == "1"
&& ProcessInfo.processInfo.environment["SPI_BUILD"] == nil
{
return ""
}
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")
guard FileManager.default.fileExists(atPath: task.executableURL!.path) else {
return nil
}
let pipe = Pipe()
task.standardOutput = pipe
task.standardError = pipe
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 {
return nil
}
} catch {
return nil
}
}
func getJavaHomeFromSDKMAN() -> String? {
let home = FileManager.default.homeDirectoryForCurrentUser
.appendingPathComponent(".sdkman/candidates/java/current")
let javaBin = home.appendingPathComponent("bin/java").path
if FileManager.default.isExecutableFile(atPath: javaBin) {
return home.path
}
return nil
}
func getJavaHomeFromPath() -> String? {
let task = Process()
task.executableURL = URL(fileURLWithPath: "/usr/bin/which")
task.arguments = ["java"]
let pipe = Pipe()
task.standardOutput = pipe
do {
try task.run()
task.waitUntilExit()
guard task.terminationStatus == 0 else { return nil }
let data = pipe.fileHandleForReading.readDataToEndOfFile()
guard
let javaPath = String(data: data, encoding: .utf8)?
.trimmingCharacters(in: .whitespacesAndNewlines),
!javaPath.isEmpty
else { return nil }
let resolved = URL(fileURLWithPath: javaPath).resolvingSymlinksInPath()
return
resolved
.deletingLastPathComponent()
.deletingLastPathComponent()
.path
} catch {
return nil
}
}
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
let package = Package(
name: "swift-java-jni-core",
products: [
.library(
name: "SwiftJavaJNICore",
targets: ["SwiftJavaJNICore"]
)
],
targets: [
.target(
name: "CSwiftJavaJNI",
cSettings: [
.unsafeFlags(["-I\(javaIncludePath)", "-I\(javaPlatformIncludePath)"], .when(platforms: [.macOS, .linux, .windows]))
],
swiftSettings: [
.unsafeFlags(["-I\(javaIncludePath)", "-I\(javaPlatformIncludePath)"], .when(platforms: [.macOS, .linux, .windows]))
],
linkerSettings: [
.linkedLibrary("log", .when(platforms: [.android]))
]
),
.target(
name: "SwiftJavaJNICore",
dependencies: [
"CSwiftJavaJNI"
],
swiftSettings: [
.swiftLanguageMode(.v5),
.unsafeFlags(["-I\(javaIncludePath)", "-I\(javaPlatformIncludePath)"], .when(platforms: [.macOS, .linux, .windows])),
],
linkerSettings: [
.unsafeFlags(
[
"-L\(javaHome)/lib/server",
"-Xlinker", "-rpath",
"-Xlinker", "\(javaHome)/lib/server",
],
.when(platforms: [.linux, .macOS])
),
.unsafeFlags(
[
"-L\(javaHome)/lib"
],
.when(platforms: [.windows])
),
.linkedLibrary(
"jvm",
.when(platforms: [.linux, .macOS, .windows])
),
]
),
.testTarget(
name: "SwiftJavaJNICoreTests",
dependencies: [
"SwiftJavaJNICore"
],
swiftSettings: [
.swiftLanguageMode(.v5),
.unsafeFlags(["-I\(javaIncludePath)", "-I\(javaPlatformIncludePath)"], .when(platforms: [.macOS, .linux, .windows])),
]
),
]
)