Skip to content

Commit 0c627a0

Browse files
ktosoDougGregorLukasa
authored andcommitted
Initial commit
Co-authored-by: Doug Gregor <[email protected]> Co-authored-by: Cory Benfield <[email protected]>
1 parent d876c65 commit 0c627a0

File tree

171 files changed

+31752
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

171 files changed

+31752
-1
lines changed

.gitignore

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
.DS_Store
2+
.build
3+
Packages
4+
xcuserdata/
5+
DerivedData/
6+
.swiftpm
7+
.swiftpm/configuration/registries.json
8+
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
9+
.netrc
10+
*.class
11+
12+
# Ignore gradle build artifacts
13+
.gradle
14+
**/build/
15+
16+
# Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored)
17+
!gradle-wrapper.jar
18+
19+
# Avoid ignore Gradle wrappper properties
20+
!gradle-wrapper.properties
21+
22+
# Cache of project
23+
.gradletasknamecache
24+
25+
# Ignore generated sources
26+
JavaSwiftKitDemo/src/main/java/com/example/swift/generated/*

BuildLogic/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
build
2+
.gradle

BuildLogic/build.gradle.kts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2024 Apple Inc. and the Swift.org project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
//
10+
// SPDX-License-Identifier: Apache-2.0
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
repositories {
15+
gradlePluginPortal()
16+
}
17+
18+
plugins {
19+
`kotlin-dsl`
20+
}

BuildLogic/settings.gradle.kts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2024 Apple Inc. and the Swift.org project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
//
10+
// SPDX-License-Identifier: Apache-2.0
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
rootProject.name = "BuildLogic"
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2024 Apple Inc. and the Swift.org project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
//
10+
// SPDX-License-Identifier: Apache-2.0
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
plugins {
15+
id("build-logic.java-common-conventions")
16+
17+
id("application")
18+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2024 Apple Inc. and the Swift.org project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
//
10+
// SPDX-License-Identifier: Apache-2.0
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
plugins {
15+
java
16+
}
17+
18+
java {
19+
toolchain {
20+
languageVersion = JavaLanguageVersion.of(22)
21+
}
22+
}
23+
24+
repositories {
25+
mavenCentral()
26+
}
27+
28+
testing {
29+
suites {
30+
val test by getting(JvmTestSuite::class) {
31+
useJUnitJupiter("5.10.3")
32+
}
33+
}
34+
}
35+
36+
/// Enable access to preview APIs, e.g. java.lang.foreign.* (Panama)
37+
tasks.withType(JavaCompile::class).forEach {
38+
it.options.compilerArgs.add("--enable-preview")
39+
it.options.compilerArgs.add("-Xlint:preview")
40+
}
41+
42+
// Configure paths for native (Swift) libraries
43+
tasks.test {
44+
jvmArgs(
45+
"--enable-native-access=ALL-UNNAMED",
46+
47+
// Include the library paths where our dylibs are that we want to load and call
48+
"-Djava.library.path=" + listOf(
49+
"""$rootDir/.build/arm64-apple-macosx/debug/""",
50+
"/usr/lib/swift/"
51+
).joinToString(File.pathSeparator)
52+
)
53+
}
54+
55+
tasks.withType<Test> {
56+
this.testLogging {
57+
this.showStandardStreams = true
58+
}
59+
}
60+
61+
62+
val buildSwiftJExtract = tasks.register<Exec>("buildSwiftJExtract") {
63+
description = "Builds Swift targets, including jextract-swift"
64+
65+
workingDir("..")
66+
commandLine("make")
67+
}
68+
69+
tasks.build {
70+
dependsOn(buildSwiftJExtract)
71+
}
72+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2024 Apple Inc. and the Swift.org project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
//
10+
// SPDX-License-Identifier: Apache-2.0
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
plugins {
15+
id("build-logic.java-common-conventions")
16+
17+
id("application")
18+
}

JavaSwiftKitDemo/build.gradle.kts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2024 Apple Inc. and the Swift.org project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
//
10+
// SPDX-License-Identifier: Apache-2.0
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
plugins {
15+
id("build-logic.java-application-conventions")
16+
}
17+
18+
group = "org.swift.javakit"
19+
version = "1.0-SNAPSHOT"
20+
21+
repositories {
22+
mavenCentral()
23+
}
24+
25+
java {
26+
toolchain {
27+
languageVersion.set(JavaLanguageVersion.of(22))
28+
}
29+
}
30+
31+
dependencies {
32+
testImplementation(platform("org.junit:junit-bom:5.10.0"))
33+
testImplementation("org.junit.jupiter:junit-jupiter")
34+
}
35+
36+
tasks.test {
37+
useJUnitPlatform()
38+
}
39+
40+
application {
41+
mainClass = "org.example.HelloJava2Swift"
42+
43+
// In order to silence:
44+
// WARNING: A restricted method in java.lang.foreign.SymbolLookup has been called
45+
// WARNING: java.lang.foreign.SymbolLookup::libraryLookup has been called by org.example.swift.JavaKitExample in an unnamed module
46+
// WARNING: Use --enable-native-access=ALL-UNNAMED to avoid a warning for callers in this module
47+
// WARNING: Restricted methods will be blocked in a future release unless native access is enabled
48+
// FIXME: Find out the proper solution to this
49+
applicationDefaultJvmArgs = listOf(
50+
"--enable-native-access=ALL-UNNAMED",
51+
52+
// Include the library paths where our dylibs are that we want to load and call
53+
"-Djava.library.path=" + listOf(
54+
"""$rootDir/.build/arm64-apple-macosx/debug/""",
55+
"/usr/lib/swift/"
56+
).joinToString(":"),
57+
58+
// Enable tracing downcalls (to Swift)
59+
"-Djextract.trace.downcalls=true"
60+
)
61+
}
62+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2024 Apple Inc. and the Swift.org project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
//
10+
// SPDX-License-Identifier: Apache-2.0
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
package com.example.swift;
15+
import com.example.swift.HelloSwift;
16+
17+
public class HelloSubclass extends HelloSwift {
18+
private String greeting;
19+
20+
public HelloSubclass(String greeting) {
21+
this.greeting = greeting;
22+
}
23+
24+
private void greetMe() {
25+
super.greet(greeting);
26+
}
27+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2024 Apple Inc. and the Swift.org project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
//
10+
// SPDX-License-Identifier: Apache-2.0
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
package com.example.swift;
15+
import com.example.swift.HelloSubclass;
16+
17+
public class HelloSwift {
18+
private double value;
19+
private static double initialValue = 3.14159;
20+
private String name = "Java";
21+
22+
static {
23+
System.loadLibrary("JavaKitExample");
24+
}
25+
26+
public HelloSwift() {
27+
this.value = initialValue;
28+
}
29+
30+
public static void main(String[] args) {
31+
int result = new HelloSubclass("Swift").sayHello(17, 25);
32+
System.out.println("sayHello(17, 25) = " + result);
33+
}
34+
35+
public native int sayHello(int x, int y);
36+
public native String throwMessageFromSwift(String message) throws Exception;
37+
38+
// To be called back by the native code
39+
private double sayHelloBack(int i) {
40+
System.out.println("And hello back from " + name + "! You passed me " + i);
41+
return value;
42+
}
43+
44+
public void greet(String name) {
45+
System.out.println("Salutations, " + name);
46+
}
47+
48+
String[] doublesToStrings(double[] doubles) {
49+
int size = doubles.length;
50+
String[] strings = new String[size];
51+
52+
for(int i = 0; i < size; i++) {
53+
strings[i] = "" + doubles[i];
54+
}
55+
56+
return strings;
57+
}
58+
59+
public void throwMessage(String message) throws Exception {
60+
throw new Exception(message);
61+
}
62+
}

0 commit comments

Comments
 (0)