You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit fb2d5cd
Browse filesBrowse the repository at this point in the historyBrowse files
echo"::error::Snippets/SwiftJavaCLIHelp.txt is out of date with the swift-java CLI's --help output (likely a command's flags or abstract text changed). Run scripts/generate-cli-help-snippets.sh and commit the result."
If you'd like `act` to always run with certain flags, these can be be placed in
81
+
If you'd like `act` to always run with certain flags, these can be placed in
82
82
an `.actrc` file either in the current working directory or your home
83
83
directory, for example:
84
84
@@ -88,20 +88,23 @@ directory, for example:
88
88
--action-offline-mode
89
89
```
90
90
91
-
For frequent contributors, we recommend adding the script as a [git pre-push hook](https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks), which you can do via executing the following command in the project root directory:
91
+
For frequent contributors, we recommend running the soundness checks before pushing. You can wire that up as a [git pre-push hook](https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks):
Which makes the script execute, and only allow the `git push` to complete if the check has passed.
101
+
This only allows the `git push` to complete if the checks pass.
102
+
103
+
In the case of formatting issues, `git add` the formatting changes and attempt the push again.
104
+
105
+
### Regenerating generated docs
103
106
104
-
In the case of formatting issues, you can then `git add` the formatting changes, and attempt the push again.
107
+
Some documentation is generated from source and checked in, so CI can catch it drifting out of date. If you changed `Configuration.swift` or a CLI command's flags/help text, run `scripts/generate-docs.sh`and commit the generated changes.
Copy file name to clipboardExpand all lines: README.md
+51-52Lines changed: 51 additions & 52 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,9 +7,9 @@ This project contains tools and libraries that facilitate **Swift & Java Interop
7
7
8
8
## Introduction
9
9
10
-
If you'd like to check out a quick introduction to Swift & Java interoperability, you may be interested in this presentation from WWDC25: [WWDC25: Explore Swift and Java interoperability](https://www.youtube.com/watch?v=QSHO-GUGidA).
10
+
For a quick introduction to Swift & Java interoperability, see the WWDC25 session [Explore Swift and Java interoperability](https://www.youtube.com/watch?v=QSHO-GUGidA).
11
11
12
-
While we work on more quickstarts and documentation, please refer to the Sample projects located in [Samples/](Samples/) that showcase the various ways you can use swift-java in your Swift or Java projects.
12
+
While we work on more quickstarts and documentation, refer to the sample projects in [Samples/](Samples/), which showcase the various ways to use swift-java in Swift or Java projects.
13
13
14
14
## Dependencies
15
15
@@ -42,18 +42,17 @@ sdk install java 25.0.1-amzn
42
42
43
43
## Self-publish supporting Java libraries
44
44
45
-
Swift-java relies on supporting libraries that are under active development and not yet published to Maven Central. To use the project, you'll need to self-publish these libraries locally so your Java project can depend on them.
45
+
swift-java relies on supporting libraries that are under active development and not yet published to Maven Central. To use the project, you'll need to self-publish these libraries locally so your Java project can depend on them.
46
46
47
-
To publish the libraries to your local maven repository (`$HOME/.m2`), you can run:
47
+
To publish the libraries to your local maven repository (`$HOME/.m2`), run this in `swift-java/`:
48
48
49
-
```
50
-
// in swift-java/
49
+
```bash
51
50
./gradlew publishToMavenLocal
52
51
```
53
52
54
-
To consume these libraries in your Java project built using Gradle, you can then include the local repository in the repositories to resolve dependencies from:
53
+
To consume these libraries in your Java project built using Gradle, include the local repository in the repositories to resolve dependencies from:
55
54
56
-
```
55
+
```kotlin
57
56
repositories {
58
57
mavenLocal()
59
58
mavenCentral()
@@ -62,65 +61,66 @@ repositories {
62
61
63
62
We anticipate simplifying this in the future.
64
63
65
-
## SwiftJava macros
64
+
## SwiftJava macros: calling Java from Swift
66
65
67
-
SwiftJava is a Swift library offering macros which simplify writing JNI code "by hand" but also calling Java code from Swift.
66
+
SwiftJava is a Swift library offering macros which simplify writing JNI code "by hand", and calling Java code from Swift.
68
67
69
-
It is possible to generate Swift bindings to Java libraries using SwiftJava by using the `swift-java wrap-java` command.
68
+
You can also generate Swift bindings to Java libraries with the `swift-java wrap-java` command.
70
69
71
70
Required language/runtime versions:
72
-
-**JDK 17+**, any recent JDK installation should be sufficient, as only general reflection and JNI APIs are used by this integratio
71
+
-**JDK 17+**, any recent JDK installation should be sufficient, as only general reflection and JNI APIs are used by this integration
73
72
-**Swift 6.2.x**, because the library uses modern Swift macros
74
73
75
-
**swift-java jextract**
74
+
## swift-java jextract: calling Swift from Java
75
+
76
+
`jextract` is a source generator which **generates Java bindings to existing Swift libraries**.
77
+
Its inputs are Swift sources or packages, and its outputs are the generated Swift and Java code needed to call those functions efficiently from Java.
76
78
77
-
Is a source generator which will **generate Java bindings to existing Swift libraries**.
78
-
Its inputs are Swift sources or packages, and outputs are generated Swift and Java code necessary to call these functions efficiently from Java.
79
+
It has two modes, `ffm` and `jni`.
79
80
80
-
## swift-java jextract --mode=ffm (default)
81
+
###swift-java jextract --mode=ffm (default)
81
82
82
-
This mode provides the most flexibility and performance, and allows to decrease the amount of data being copied between Swift and Java.
83
-
This does require the use of the relatively recent [JEP-454: Foreign Function & Memory API](https://openjdk.org/jeps/454), which is only available since JDK22, and will become part of JDK LTS releases with JDK 25 (depending on your JDK vendor).
83
+
This mode offers the most flexibility and performance, and can reduce the amount of data copied between Swift and Java.
84
+
It requires [JEP-454: Foreign Function & Memory API](https://openjdk.org/jeps/454), which is final since JDK 22.
84
85
85
86
This is the primary way we envision calling Swift code from server-side Java libraries and applications.
86
87
87
88
Required language/runtime versions:
88
89
-**Swift 6.2**, because of dependence on rich swift interface files
89
90
-**JDK 25+**
90
-
- We are validating the implementation using the currently supported non-LTE release, which at present means JDK-25.
91
+
- We validate the implementation against the currently supported non-LTS release, which at present means JDK25.
91
92
92
-
## swift-java jextract --mode=jni
93
+
###swift-java jextract --mode=jni
93
94
94
-
In this mode, the generated sources will use the legacy JNI approach to calling native code.
95
+
In this mode the generated sources use JNI to call native code.
95
96
96
-
This mode is more limited in some performance and flexibility that it can offer, however it is the most compatible, since even very old JVM's as well as even Android systems can be supported by this mode.
97
-
We recommend this mode when FFM is not available, or wide ranging deployment compatibility is your priority. When performance is paramaunt, we recommend the FFM mode instead.
97
+
This mode offers less performance and flexibility than FFM, but it is the most compatible: it works on older JVMs as well as on Android.
98
+
Use it when FFM is not available, or when wide deployment compatibility is your priority. When performance is paramount, prefer FFM.
98
99
99
100
Required language/runtime versions:
100
-
-**Swift 6.2**, because of dependence on rich swift interface files
101
-
-**Java 7+**, including
102
-
101
+
-**Swift 6.2**, because of dependence on rich swift interface files
102
+
-**JDK 17+**; the generated Java sources target the `javaSourceLevel` setting, which supports 17 through 25 (default 22)
103
103
104
104
## Development and Testing
105
105
106
106
This project contains multiple builds, living side by side together.
107
107
108
108
You will need to have:
109
109
- Swift (6.2.x+)
110
-
- Java (25+ for FFM, even though we support lower JDK targets)
110
+
- Java (25+ to build this project, even though the published libraries target lower JDK versions)
111
111
- Gradle (installed by "Gradle wrapper" automatically when you run gradle through `./gradlew`)
112
112
113
113
### Preparing your environment
114
114
115
-
Install **Swift**, the easiest way to do this is to use **Swiftly**: [swift.org/install/](https://www.swift.org/install/).
116
-
This should automatically install a recent Swift, but you can always make sure by running:
115
+
Install **Swift**; the easiest way is [Swiftly](https://www.swift.org/install/).
116
+
This installs a recent Swift, but you can pin the version explicitly:
117
117
118
118
```bash
119
119
swiftly install 6.2 --use
120
120
```
121
121
122
-
Install a recent enough Java distribution. We validate this project using Corretto so you can choose to use that as well,
123
-
however any recent enough Java distribution should work correctly. You can use sdkman to install Java:
122
+
Install a recent enough Java distribution. We validate this project using Corretto, so you may want to use that as well,
123
+
though any recent enough distribution should work. You can use sdkman to install Java:
124
124
125
125
```bash
126
126
# Install sdkman from: https://sdkman.io
@@ -142,52 +142,51 @@ export JAVA_HOME="$(sdk home java current)"
142
142
143
143
### Testing your changes
144
144
145
-
Many tests, including source generation tests, are written in Swift and you can execute them all by running the
146
-
swift package manager test command:
145
+
Many tests, including source generation tests, are written in Swift. Run them all with:
147
146
148
147
```bash
149
148
> swift test
150
149
```
151
150
152
151
When adding tests in `Tests/...` targets, you can run these tests (or filter a specific test using `swift test --filter type-or-method-name`).
153
152
154
-
Some tests are implemented in Java and therefore need to be executed using Gradle.
155
-
Please always use the gradle wrapper (`./gradlew`) to make sure to use the appropriate Gradle version
153
+
Some tests are implemented in Java and need to be executed using Gradle.
154
+
Always use the Gradle wrapper (`./gradlew`) so the correct Gradle version is used:
156
155
157
156
```bash
158
157
> ./gradlew test
159
158
```
160
159
161
-
> Tip: A lot of the **runtime tests** for code relying on `jextract` are **located in sample apps**,
162
-
> so if you need to runtime test any code relying on source generation steps of jextract, consider adding the tests
163
-
> to an appropriate Sample. These tests are also executed in CI (which you can check in the `ci-validate.sh` script
164
-
> contained in every sample repository).
160
+
> Tip: Many of the **runtime tests** for code relying on `jextract` are **located in sample apps**,
161
+
> so if you need to runtime test code that depends on jextract's source generation, consider adding the tests
162
+
> to an appropriate Sample. These tests also run in CI; see the `ci-validate.sh` script in each sample
163
+
> that has one.
165
164
166
165
### Sample apps & tests
167
166
168
167
Sample apps are located in the `Samples/` directory, and they showcase full "roundtrip" usage of the library and/or tools.
169
168
170
-
Samples are build by default by Gradle. Building samples can be skipped by appending the flag `-PskipSamples=true` to a gradle command.
169
+
Samples are built by default by Gradle. Building samples can be skipped by appending the flag `-PskipSamples=true` to a gradle command.
171
170
172
-
#### SwiftJava (Swift -> Java)
171
+
#### SwiftJava (calling Java from Swift)
173
172
174
-
To run a simple app showcasing a Swift process calling into a Java library you can run:
173
+
To run a simple app showcasing a Swift process calling into a Java library:
175
174
176
175
```bash
177
-
cd Samples/SwiftJavaExtractFFMSampleApp
178
-
./ci-validate.sh # which is just `swift build` and a `java -cp ...` invocation of the compiled program
176
+
cd Samples/JavaProbablyPrime
177
+
./ci-validate.sh # which is just a `swift run JavaProbablyPrime 1337`
179
178
```
180
179
181
-
#### jextract (Java -> Swift)
180
+
#### jextract (calling Swift from Java)
182
181
183
-
To run a simple example app showcasing the jextract (Java calling Swift) approach you can:
182
+
To run a simple example app showcasing a Java program calling into Swift:
This will also generate the necessary sources (by invoking jextract, extracting the `Sources/ExampleSwiftLibrary`)
190
-
and generating Java sources in `src/generated/java`.
188
+
This also generates the necessary sources (by invoking jextract on `Sources/MySwiftLibrary`)
189
+
and the Java sources in `src/generated/java`.
191
190
192
191
#### Other sample apps
193
192
@@ -197,7 +196,7 @@ Please refer to the [Samples](Samples) directory for more sample apps which show
197
196
198
197
You can run Swift [ordo-one/package-benchmark](https://github.com/ordo-one/package-benchmark) and OpenJDK [JMH](https://github.com/openjdk/jmh) benchmarks in this project.
199
198
200
-
Swift benchmarks are located under `Benchmarks/` and JMH benchmarks are currently part of the SwiftKit sample project: `Samples/SwiftJavaExtractFFMSampleApp/src/jmh` because they depend on generated sources from the sample.
199
+
Swift benchmarks are located under `Benchmarks/`. JMH benchmarks live in `Samples/SwiftJavaExtractFFMSampleApp/src/jmh`, because they depend on sources generated by that sample.
201
200
202
201
### Swift benchmarks
203
202
@@ -217,7 +216,7 @@ cd Samples/SwiftJavaExtractFFMSampleApp
217
216
./gradlew jmh
218
217
```
219
218
220
-
Please read documentation of both performance testing tools and understand that results must be interpreted and not just taken at face value. Benchmarking is tricky and environment sensitive task, so please be careful when constructing and reading benchmarks and their results. If in doubt, please reach out on the forums.
219
+
Please read the documentation of both performance testing tools, and note that results must be interpreted rather than taken at face value. Benchmarking is tricky and environment sensitive, so be careful when constructing and reading benchmarks and their results. If in doubt, reach out on the forums.
0 commit comments