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
24 changes: 4 additions & 20 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,13 @@ jobs:
name: Test
uses: swiftlang/github-workflows/.github/workflows/swift_package_test.yml@main
with:
# TODO: need to pre-install Java
enable_windows_checks: false
# TODO: need to pre-install Java
enable_macos_checks: false
enable_windows_checks: true
windows_swift_versions: '["nightly-main"]'
enable_macos_checks: true
enable_ios_checks: false
enable_linux_checks: true
linux_os_versions: '["bookworm", "jammy", "noble", "rhel-ubi9"]'
linux_os_versions: '["amazonlinux2", "bookworm", "noble", "jammy", "rhel-ubi9"]'
linux_exclude_swift_versions: "[{\"swift_version\": \"5.9\"}, {\"swift_version\": \"5.10\"}]"
linux_pre_build_command: |
if command -v apt-get >/dev/null 2>&1 ; then # bookworm, noble, jammy
if command -v sudo &> /dev/null && [ "$EUID" -ne 0 ]; then
sudo apt-get update -y
sudo apt-get install -y default-jdk
else
apt-get update -y
apt-get install -y default-jdk
fi
elif command -v yum >/dev/null 2>&1 ; then # amazonlinux2, rhel-ubi9
# TODO: amazonlinux2 needs more help finding JAVA_HOME
yum update -y
yum install -y java-devel
fi

enable_android_sdk_build: true
enable_android_sdk_checks: true
soundness:
Expand Down
165 changes: 1 addition & 164 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,133 +1,7 @@
// 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: [
Expand All @@ -138,57 +12,20 @@ let package = Package(
],
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]))
]
name: "CSwiftJavaJNI"
),

.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])),
]
),
]
Expand Down
98 changes: 0 additions & 98 deletions Sources/CSwiftJavaJNI/AndroidSupport.cpp

This file was deleted.

Loading
Loading