Skip to content
Open
Show file tree
Hide file tree
Changes from 9 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
64 changes: 64 additions & 0 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,70 @@ jobs:
- name: Swift Test
run: "swift test"

# Test that projects depending on swift-java can build with C++ interoperability enabled.
# This is important because users may enable -cxx-interoperability-mode=default in their
# own projects, and swift-java's public API must be compatible with that mode.
# See: https://github.com/swiftlang/swift-java/issues/391
verify-samples-cxx-interop:
name: Sample ${{ matrix.sample_app }} (C++ Interop) (${{ matrix.os_version }} swift:${{ matrix.swift_version }} jdk:${{matrix.jdk_vendor}})
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
swift_version: ['6.1.3', '6.2', 'nightly']
os_version: ['jammy']
jdk_vendor: ['corretto']
sample_app: [
'JavaDependencySampleApp',
'JavaKitSampleApp',
'JavaProbablyPrime',
'JavaSieve',
'SwiftAndJavaJarSampleLib',
'SwiftJavaExtractFFMSampleApp',
'SwiftJavaExtractJNISampleApp',
]
container:
image: ${{ (contains(matrix.swift_version, 'nightly') && 'swiftlang/swift') || 'swift' }}:${{ matrix.swift_version }}-${{ matrix.os_version }}
env:
CXX_INTEROP: '1'
steps:
- uses: actions/checkout@v4
- name: Prepare CI Environment
uses: ./.github/actions/prepare_env
- name: "Verify sample with C++ Interop: ${{ matrix.sample_app }}"
run: .github/scripts/validate_sample.sh Samples/${{ matrix.sample_app }}

verify-samples-cxx-interop-macos:
name: Sample ${{ matrix.sample_app }} (C++ Interop) (${{ matrix.os_version }} swift:${{ matrix.swift_version }} jdk:${{matrix.jdk_vendor}})
runs-on: [self-hosted, macos, sequoia, ARM64]
strategy:
fail-fast: false
matrix:
swift_version: ['6.2']
os_version: ['macos']
jdk_vendor: ['corretto']
sample_app: [
'JavaDependencySampleApp',
'JavaKitSampleApp',
'JavaProbablyPrime',
'JavaSieve',
'SwiftAndJavaJarSampleLib',
'SwiftJavaExtractFFMSampleApp',
'SwiftJavaExtractJNISampleApp',
]
env:
CXX_INTEROP: '1'
steps:
- uses: actions/checkout@v4
- name: Prepare CI Environment
uses: ./.github/actions/prepare_env
- name: Install Swiftly
run: ./.github/scripts/install_swiftly.sh
env:
SWIFT_VERSION: "${{ matrix.swift_version }}"
- name: "Verify sample with C++ Interop: ${{ matrix.sample_app }}"
run: .github/scripts/validate_sample.sh Samples/${{ matrix.sample_app }}

build-swift-android:
name: Sample SwiftJavaExtractJNISampleApp (Android) (${{ matrix.os_version }} swift:${{ matrix.swift_version }} jdk:${{matrix.jdk_vendor}} android:${{matrix.sdk_triple}})
runs-on: ubuntu-latest
Expand Down
8 changes: 8 additions & 0 deletions Samples/JavaDependencySampleApp/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ let javaIncludePath = "\(javaHome)/include"
#error("Currently only macOS and Linux platforms are supported, this may change in the future.")
#endif

// Support C++ interoperability mode via CXX_INTEROP environment variable.
// This is used to test that swift-java's public API is compatible with projects
// that enable C++ interoperability mode.
// See: https://github.com/swiftlang/swift-java/issues/391
let cxxInteropEnabled = ProcessInfo.processInfo.environment["CXX_INTEROP"] == "1"

let package = Package(
name: "JavaDependencySampleApp",
platforms: [
Expand Down Expand Up @@ -73,6 +79,7 @@ let package = Package(
swiftSettings: [
.unsafeFlags(["-I\(javaIncludePath)", "-I\(javaPlatformIncludePath)"]),
.swiftLanguageMode(.v5),
.interoperabilityMode(.Cxx, .when(platforms: cxxInteropEnabled ? [.macOS, .linux] : [])),
],
plugins: [
.plugin(name: "SwiftJavaPlugin", package: "swift-java"),
Expand All @@ -92,6 +99,7 @@ let package = Package(
swiftSettings: [
.unsafeFlags(["-I\(javaIncludePath)", "-I\(javaPlatformIncludePath)"]),
.swiftLanguageMode(.v5),
.interoperabilityMode(.Cxx, .when(platforms: cxxInteropEnabled ? [.macOS, .linux] : [])),
],
plugins: [
// .plugin(name: "SwiftJavaBootstrapJavaPlugin", package: "swift-java"),
Expand Down
9 changes: 8 additions & 1 deletion Samples/JavaKitSampleApp/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ let javaIncludePath = "\(javaHome)/include"
let javaPlatformIncludePath = "\(javaIncludePath)/win32)"
#endif

// Support C++ interoperability mode via CXX_INTEROP environment variable.
// This is used to test that swift-java's public API is compatible with projects
// that enable C++ interoperability mode.
// See: https://github.com/swiftlang/swift-java/issues/391
let cxxInteropEnabled = ProcessInfo.processInfo.environment["CXX_INTEROP"] == "1"

let package = Package(
name: "JavaKitSampleApp",
platforms: [
Expand Down Expand Up @@ -70,7 +76,8 @@ let package = Package(
],
swiftSettings: [
.swiftLanguageMode(.v5),
.unsafeFlags(["-I\(javaIncludePath)", "-I\(javaPlatformIncludePath)"])
.unsafeFlags(["-I\(javaIncludePath)", "-I\(javaPlatformIncludePath)"]),
.interoperabilityMode(.Cxx, .when(platforms: cxxInteropEnabled ? [.macOS, .linux] : [])),
],
plugins: [
.plugin(name: "JavaCompilerPlugin", package: "swift-java"),
Expand Down
11 changes: 10 additions & 1 deletion Samples/JavaProbablyPrime/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@
import CompilerPluginSupport
import PackageDescription

import class Foundation.ProcessInfo

// Support C++ interoperability mode via CXX_INTEROP environment variable.
// This is used to test that swift-java's public API is compatible with projects
// that enable C++ interoperability mode.
// See: https://github.com/swiftlang/swift-java/issues/391
let cxxInteropEnabled = ProcessInfo.processInfo.environment["CXX_INTEROP"] == "1"

let package = Package(
name: "JavaProbablyPrime",
platforms: [
Expand Down Expand Up @@ -34,7 +42,8 @@ let package = Package(
.product(name: "ArgumentParser", package: "swift-argument-parser"),
],
swiftSettings: [
.swiftLanguageMode(.v5)
.swiftLanguageMode(.v5),
.interoperabilityMode(.Cxx, .when(platforms: cxxInteropEnabled ? [.macOS, .linux] : [])),
],
plugins: [
.plugin(name: "SwiftJavaPlugin", package: "swift-java"),
Expand Down
12 changes: 10 additions & 2 deletions Samples/JavaSieve/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ let javaIncludePath = "\(javaHome)/include"
#error("Currently only macOS and Linux platforms are supported, this may change in the future.")
#endif

// Support C++ interoperability mode via CXX_INTEROP environment variable.
// This is used to test that swift-java's public API is compatible with projects
// that enable C++ interoperability mode.
// See: https://github.com/swiftlang/swift-java/issues/391
let cxxInteropEnabled = ProcessInfo.processInfo.environment["CXX_INTEROP"] == "1"

let package = Package(
name: "JavaSieve",
platforms: [
Expand All @@ -59,7 +65,8 @@ let package = Package(
],
exclude: ["swift-java.config"],
swiftSettings: [
.unsafeFlags(["-I\(javaIncludePath)", "-I\(javaPlatformIncludePath)"])
.unsafeFlags(["-I\(javaIncludePath)", "-I\(javaPlatformIncludePath)"]),
.interoperabilityMode(.Cxx, .when(platforms: cxxInteropEnabled ? [.macOS, .linux] : [])),
],
plugins: [
.plugin(name: "SwiftJavaPlugin", package: "swift-java"),
Expand All @@ -77,7 +84,8 @@ let package = Package(
],
exclude: ["swift-java.config"],
swiftSettings: [
.unsafeFlags(["-I\(javaIncludePath)", "-I\(javaPlatformIncludePath)"])
.unsafeFlags(["-I\(javaIncludePath)", "-I\(javaPlatformIncludePath)"]),
.interoperabilityMode(.Cxx, .when(platforms: cxxInteropEnabled ? [.macOS, .linux] : [])),
],
plugins: [
.plugin(name: "SwiftJavaPlugin", package: "swift-java"),
Expand Down
9 changes: 8 additions & 1 deletion Samples/SwiftAndJavaJarSampleLib/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ let javaIncludePath = "\(javaHome)/include"
#error("Currently only macOS and Linux platforms are supported, this may change in the future.")
#endif

// Support C++ interoperability mode via CXX_INTEROP environment variable.
// This is used to test that swift-java's public API is compatible with projects
// that enable C++ interoperability mode.
// See: https://github.com/swiftlang/swift-java/issues/391
let cxxInteropEnabled = ProcessInfo.processInfo.environment["CXX_INTEROP"] == "1"

let package = Package(
name: "SwiftAndJavaJarSampleLib",
platforms: [
Expand Down Expand Up @@ -70,7 +76,8 @@ let package = Package(
],
swiftSettings: [
.swiftLanguageMode(.v5),
.unsafeFlags(["-I\(javaIncludePath)", "-I\(javaPlatformIncludePath)"])
.unsafeFlags(["-I\(javaIncludePath)", "-I\(javaPlatformIncludePath)"]),
.interoperabilityMode(.Cxx, .when(platforms: cxxInteropEnabled ? [.macOS, .linux] : [])),
],
plugins: [
.plugin(name: "JExtractSwiftPlugin", package: "swift-java"),
Expand Down
9 changes: 8 additions & 1 deletion Samples/SwiftJavaExtractFFMSampleApp/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ let javaIncludePath = "\(javaHome)/include"
#error("Currently only macOS and Linux platforms are supported, this may change in the future.")
#endif

// Support C++ interoperability mode via CXX_INTEROP environment variable.
// This is used to test that swift-java's public API is compatible with projects
// that enable C++ interoperability mode.
// See: https://github.com/swiftlang/swift-java/issues/391
let cxxInteropEnabled = ProcessInfo.processInfo.environment["CXX_INTEROP"] == "1"

let package = Package(
name: "SwiftJavaExtractFFMSampleApp",
platforms: [
Expand Down Expand Up @@ -72,7 +78,8 @@ let package = Package(
],
swiftSettings: [
.swiftLanguageMode(.v5),
.unsafeFlags(["-I\(javaIncludePath)", "-I\(javaPlatformIncludePath)"])
.unsafeFlags(["-I\(javaIncludePath)", "-I\(javaPlatformIncludePath)"]),
.interoperabilityMode(.Cxx, .when(platforms: cxxInteropEnabled ? [.macOS, .linux] : [])),
],
plugins: [
.plugin(name: "JExtractSwiftPlugin", package: "swift-java"),
Expand Down
7 changes: 7 additions & 0 deletions Samples/SwiftJavaExtractJNISampleApp/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ let javaIncludePath = "\(javaHome)/include"
#error("Currently only macOS and Linux platforms are supported, this may change in the future.")
#endif

// Support C++ interoperability mode via CXX_INTEROP environment variable.
// This is used to test that swift-java's public API is compatible with projects
// that enable C++ interoperability mode.
// See: https://github.com/swiftlang/swift-java/issues/391
let cxxInteropEnabled = ProcessInfo.processInfo.environment["CXX_INTEROP"] == "1"

let package = Package(
name: "JExtractJNISampleApp",
platforms: [
Expand Down Expand Up @@ -70,6 +76,7 @@ let package = Package(
swiftSettings: [
.swiftLanguageMode(.v5),
.unsafeFlags(["-I\(javaIncludePath)", "-I\(javaPlatformIncludePath)"]),
.interoperabilityMode(.Cxx, .when(platforms: cxxInteropEnabled ? [.macOS, .linux] : [])),
],
plugins: [
.plugin(name: "JExtractSwiftPlugin", package: "swift-java")
Expand Down
48 changes: 48 additions & 0 deletions Sources/CSwiftJavaJNI/include/CSwiftJavaJNI.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,52 @@

#include <jni.h>

// Provide C-compatible type aliases for JNI types.
// When Swift modules have C++ interoperability enabled, jni.h is parsed
// in C++ mode where JNIEnv is defined as a struct (JNIEnv_). However,
// SwiftJava modules are compiled without C++ interop, causing JNIEnv
// to be a pointer type. This mismatch causes type errors.
//
// These typedefs provide consistent C-style pointer types that work
// regardless of the C++ interoperability mode.
// See: https://github.com/swiftlang/swift-java/issues/391
#ifdef __cplusplus
// Android NDK uses JNINativeInterface instead of JNINativeInterface_
#ifdef __ANDROID__
typedef const JNINativeInterface *CJNIEnv;
#else
typedef const JNINativeInterface_ *CJNIEnv;
#endif
typedef _jobject *Cjobject;
typedef _jclass *Cjclass;
typedef _jstring *Cjstring;
typedef _jarray *Cjarray;
typedef _jobjectArray *CjobjectArray;
typedef _jbooleanArray *CjbooleanArray;
typedef _jbyteArray *CjbyteArray;
typedef _jcharArray *CjcharArray;
typedef _jshortArray *CjshortArray;
typedef _jintArray *CjintArray;
typedef _jlongArray *CjlongArray;
typedef _jfloatArray *CjfloatArray;
typedef _jdoubleArray *CjdoubleArray;
typedef _jthrowable *Cjthrowable;
#else
typedef JNIEnv CJNIEnv;
typedef jobject Cjobject;
typedef jclass Cjclass;
typedef jstring Cjstring;
typedef jarray Cjarray;
typedef jobjectArray CjobjectArray;
typedef jbooleanArray CjbooleanArray;
typedef jbyteArray CjbyteArray;
typedef jcharArray CjcharArray;
typedef jshortArray CjshortArray;
typedef jintArray CjintArray;
typedef jlongArray CjlongArray;
typedef jfloatArray CjfloatArray;
typedef jdoubleArray CjdoubleArray;
typedef jthrowable Cjthrowable;
#endif

#endif /* CSwiftJavaJNI_h */
Original file line number Diff line number Diff line change
Expand Up @@ -367,8 +367,16 @@ extension JNISwift2JavaGenerator {
func innerBody(in printer: inout CodePrinter) -> String {
let loweredResult = nativeSignature.result.conversion.render(&printer, result)

if !decl.functionSignature.result.type.isVoid {
return "return \(loweredResult)"
// For async functions, loweredResult is empty as they handle return internally
// via CompletableFuture. Only apply unsafeBitCast for non-void, non-async functions.
if !decl.functionSignature.result.type.isVoid && !loweredResult.isEmpty {
let resultType = nativeSignature.result.javaType.jniTypeName
// Use unsafeBitCast for C++ interoperability compatibility.
// SwiftJava uses JNI types (jstring, jobject, etc.) internally,
// but generated thunks use C-compatible types (Cjstring, Cjobject, etc.)
// to ensure compatibility with modules that have C++ interop enabled.
// See: https://github.com/swiftlang/swift-java/issues/391
return "return unsafeBitCast(\(loweredResult), to: \(resultType).self)"
} else {
return loweredResult
}
Expand Down Expand Up @@ -454,7 +462,9 @@ extension JNISwift2JavaGenerator {

let thunkParameters =
[
"environment: UnsafeMutablePointer<JNIEnv?>!",
// Use CJNIEnv for C++ interoperability compatibility.
// See: https://github.com/swiftlang/swift-java/issues/391
"environment: UnsafeMutablePointer<CJNIEnv?>!",
"thisClass: jclass"
] + translatedParameters
let thunkReturnType = resultType != .void ? " -> \(resultType.jniTypeName)" : ""
Expand Down
3 changes: 2 additions & 1 deletion Sources/JExtractSwiftLib/JavaParameter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ struct JavaParameter {
var jniTypeName: String {
switch self {
case .concrete(let type): type.jniTypeName
case .generic: "jobject?"
// Use C-compatible type for C++ interoperability
case .generic: "Cjobject?"
}
}

Expand Down
28 changes: 15 additions & 13 deletions Sources/JavaTypes/JavaType+JNI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

extension JavaType {
/// Map this Java type to the appropriate JNI type name.
/// Uses C-compatible types (Cjobject, Cjstring, etc.) for C++ interoperability.
/// See: https://github.com/swiftlang/swift-java/issues/391
package var jniTypeName: String {
switch self {
case .boolean: "jboolean"
Expand All @@ -25,19 +27,19 @@ extension JavaType {
case .int: "jint"
case .long: "jlong"
case .void: "void"
case .array(.boolean): "jbooleanArray?"
case .array(.byte): "jbyteArray?"
case .array(.char): "jcharArray?"
case .array(.short): "jshortArray?"
case .array(.int): "jintArray?"
case .array(.long): "jlongArray?"
case .array(.float): "jfloatArray?"
case .array(.double): "jdoubleArray?"
case .array: "jobjectArray?"
case .class(package: "java.lang", name: "String", _): "jstring?"
case .class(package: "java.lang", name: "Class", _): "jclass?"
case .class(package: "java.lang", name: "Throwable", _): "jthrowable?"
case .class: "jobject?"
case .array(.boolean): "CjbooleanArray?"
case .array(.byte): "CjbyteArray?"
case .array(.char): "CjcharArray?"
case .array(.short): "CjshortArray?"
case .array(.int): "CjintArray?"
case .array(.long): "CjlongArray?"
case .array(.float): "CjfloatArray?"
case .array(.double): "CjdoubleArray?"
case .array: "CjobjectArray?"
case .class(package: "java.lang", name: "String", _): "Cjstring?"
case .class(package: "java.lang", name: "Class", _): "Cjclass?"
case .class(package: "java.lang", name: "Throwable", _): "Cjthrowable?"
case .class: "Cjobject?"
}
}

Expand Down
4 changes: 3 additions & 1 deletion Sources/SwiftJava/JVM/JavaVirtualMachine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ import Foundation
#endif

public typealias JavaVMPointer = UnsafeMutablePointer<JavaVM?>
// Use CJNIEnv for C++ interoperability compatibility.
// See: https://github.com/swiftlang/swift-java/issues/391
#if canImport(Android)
typealias JNIEnvPointer = UnsafeMutablePointer<JNIEnv?>
typealias JNIEnvPointer = UnsafeMutablePointer<CJNIEnv?>
#else
typealias JNIEnvPointer = UnsafeMutableRawPointer
#endif
Expand Down
Loading
Loading