Skip to content

Commit 4adde8e

Browse files
committed
move samples to Samples/
1 parent b3f1960 commit 4adde8e

File tree

24 files changed

+232
-58
lines changed

24 files changed

+232
-58
lines changed

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ BuildLogic/out/
1414
# Ignore gradle build artifacts
1515
.gradle
1616
**/build/
17+
lib/
1718

1819
# Ignore package resolved
1920
Package.resolved
@@ -28,5 +29,4 @@ Package.resolved
2829
.gradletasknamecache
2930

3031
# Ignore generated sources
31-
JavaSwiftKitDemo/src/generated/*
32-
SwiftKitExample/src/generated/**/*
32+
**/generated/

BuildLogic/src/main/kotlin/build-logic.java-common-conventions.gradle.kts

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15+
import java.util.*
16+
1517
plugins {
1618
java
1719
}
@@ -40,16 +42,39 @@ tasks.withType(JavaCompile::class).forEach {
4042
it.options.compilerArgs.add("-Xlint:preview")
4143
}
4244

45+
46+
fun javaLibraryPaths(): List<String> {
47+
val osName = System.getProperty("os.name")
48+
val osArch = System.getProperty("os.arch")
49+
val isLinux = osName.lowercase(Locale.getDefault()).contains("linux")
50+
51+
return listOf(
52+
if (osName.lowercase(Locale.getDefault()).contains("linux")) {
53+
"""$rootDir/.build/$osArch-unknown-linux-gnu/debug/"""
54+
} else {
55+
if (osArch.equals("aarch64")) {
56+
"""$rootDir/.build/arm64-apple-macosx/debug/"""
57+
} else {
58+
"""$rootDir/.build/$osArch-apple-macosx/debug/"""
59+
}
60+
},
61+
if (isLinux) {
62+
"/usr/lib/swift/linux"
63+
} else {
64+
// assume macOS
65+
"/usr/lib/swift/"
66+
}
67+
)
68+
}
69+
70+
4371
// Configure paths for native (Swift) libraries
4472
tasks.test {
4573
jvmArgs(
4674
"--enable-native-access=ALL-UNNAMED",
4775

4876
// Include the library paths where our dylibs are that we want to load and call
49-
"-Djava.library.path=" + listOf(
50-
"""$rootDir/.build/arm64-apple-macosx/debug/""",
51-
"/usr/lib/swift/"
52-
).joinToString(File.pathSeparator)
77+
"-Djava.library.path=" + javaLibraryPaths().joinToString(File.pathSeparator)
5378
)
5479
}
5580

@@ -60,10 +85,11 @@ tasks.withType<Test> {
6085
}
6186

6287

63-
val buildSwiftJExtract = tasks.register<Exec>("buildSwiftJExtract") {
64-
description = "Builds Swift targets, including jextract-swift"
88+
// TODO: This is a crude workaround, we'll remove 'make' soon and properly track build dependencies
89+
val buildSwiftJExtract = tasks.register<Exec>("buildMake") {
90+
description = "Triggers 'make' build"
6591

66-
workingDir("..")
92+
workingDir(rootDir)
6793
commandLine("make")
6894
}
6995

Makefile

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,16 @@ BUILD_DIR := .build/$(ARCH_SUBDIR)-apple-macosx
3737
LIB_SUFFIX := dylib
3838
endif
3939

40+
SAMPLES_DIR := "Samples"
41+
4042

4143
all: generate-all
4244

4345
$(BUILD_DIR)/debug/libJavaKit.$(LIB_SUFFIX) $(BUILD_DIR)/debug/Java2Swift:
4446
swift build
4547

46-
./JavaSwiftKitDemo/build/classes/java/main/com/example/swift/HelloSubclass.class: JavaSwiftKitDemo/src/main/java/com/example/swift
47-
./gradlew build
48-
49-
run: $(BUILD_DIR)/debug/libJavaKit.$(LIB_SUFFIX) $(BUILD_DIR)/debug/libExampleSwiftLibrary.$(LIB_SUFFIX) JavaSwiftKitDemo/src/main/java/com/example/swift
50-
java -cp JavaSwiftKitDemo/build/classes/java/main -Djava.library.path=$(BUILD_DIR)/debug/ com.example.swift.HelloSwift
48+
run: $(BUILD_DIR)/debug/libJavaKit.$(LIB_SUFFIX) $(BUILD_DIR)/debug/libExampleSwiftLibrary.$(LIB_SUFFIX)
49+
./gradlew Samples:JavaKitSampleApp:run
5150

5251
Java2Swift: $(BUILD_DIR)/debug/Java2Swift
5352

@@ -71,7 +70,7 @@ generate-all: generate-JavaKit generate-JavaKitReflection generate-JavaKitJar ge
7170
jextract-swift
7271
clean:
7372
rm -rf .build; \
74-
rm -rf SwiftKitExample/src/generated/java/*
73+
rm -rf Samples/SwiftKitExampleApp/src/generated/java/*
7574

7675
format:
7776
swift format --recursive . -i
@@ -106,14 +105,14 @@ jextract-run: jextract-swift generate-JExtract-interface-files
106105
swift run jextract-swift \
107106
--package-name com.example.swift.generated \
108107
--swift-module ExampleSwiftLibrary \
109-
--output-directory SwiftKitExample/src/generated/java \
108+
--output-directory ${SAMPLES_DIR}/SwiftKitSampleApp/src/generated/java \
110109
$(BUILD_DIR)/jextract/ExampleSwiftLibrary/MySwiftLibrary.swiftinterface; \
111110
swift run jextract-swift \
112111
--package-name org.swift.swiftkit.generated \
113112
--swift-module SwiftKitSwift \
114-
--output-directory SwiftKitExample/src/generated/java \
113+
--output-directory ${SAMPLES_DIR}/SwiftKitSampleApp/src/generated/java \
115114
$(BUILD_DIR)/jextract/SwiftKitSwift/SwiftKit.swiftinterface
116115

117116

118117
jextract-run-java: jextract-swift generate-JExtract-interface-files
119-
./gradlew run SwiftKitExample
118+
./gradlew Samples:SwiftKitSampleApp:run

Samples/JavaKitSampleApp/build.gradle

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+
// See CONTRIBUTORS.txt for the list of Swift.org project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
plugins {
16+
id("build-logic.java-application-conventions")
17+
}
18+
19+
group = "org.swift.javakit"
20+
version = "1.0-SNAPSHOT"
21+
22+
repositories {
23+
mavenCentral()
24+
}
25+
26+
java {
27+
toolchain {
28+
languageVersion.set(JavaLanguageVersion.of(22))
29+
}
30+
}
31+
32+
dependencies {
33+
implementation(project(':SwiftKit'))
34+
35+
testImplementation(platform("org.junit:junit-bom:5.10.0"))
36+
testImplementation("org.junit.jupiter:junit-jupiter")
37+
}
38+
39+
tasks.test {
40+
useJUnitPlatform()
41+
}
42+
43+
def javaLibraryPaths() {
44+
def osName = System.getProperty("os.name")
45+
def osArch = System.getProperty("os.arch")
46+
def isLinux = osName.toLowerCase(Locale.getDefault()).contains("linux")
47+
48+
return [
49+
osName.toLowerCase(Locale.getDefault()).contains("linux") ?
50+
"${rootDir}/.build/${osArch}-unknown-linux-gnu/debug/" :
51+
osArch == "aarch64" ?
52+
"${rootDir}/.build/arm64-apple-macosx/debug/" :
53+
"${rootDir}/.build/${osArch}-apple-macosx/debug/",
54+
isLinux ?
55+
"/usr/lib/swift/linux" :
56+
// assume macOS
57+
"/usr/lib/swift/"
58+
]
59+
}
60+
61+
application {
62+
mainClass = "com.example.swift.JavaKitSampleMain"
63+
64+
// In order to silence:
65+
// WARNING: A restricted method in java.lang.foreign.SymbolLookup has been called
66+
// WARNING: java.lang.foreign.SymbolLookup::libraryLookup has been called by org.example.swift.JavaKitExample in an unnamed module
67+
// WARNING: Use --enable-native-access=ALL-UNNAMED to avoid a warning for callers in this module
68+
// WARNING: Restricted methods will be blocked in a future release unless native access is enabled
69+
// FIXME: Find out the proper solution to this
70+
applicationDefaultJvmArgs = [
71+
"--enable-native-access=ALL-UNNAMED",
72+
73+
// Include the library paths where our dylibs are that we want to load and call
74+
"-Djava.library.path=" + javaLibraryPaths().join(":")
75+
]
76+
}

SwiftKitExample/src/main/java/com/example/swift/HelloSwift.java renamed to Samples/JavaKitSampleApp/src/main/java/com/example/swift/HelloSwift.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,6 @@ public HelloSwift() {
2727
this.value = initialValue;
2828
}
2929

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-
3530
public native int sayHello(int x, int y);
3631
public native String throwMessageFromSwift(String message) throws Exception;
3732

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+
// See CONTRIBUTORS.txt for the list of Swift.org project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
package com.example.swift;
16+
17+
/**
18+
* This sample shows off a {@link HelloSwift} type which is partially implemented in Swift.
19+
* For the Swift implementation refer to
20+
*/
21+
public class JavaKitSampleMain {
22+
23+
public static void main(String[] args) {
24+
int result = new HelloSubclass("Swift").sayHello(17, 25);
25+
System.out.println("sayHello(17, 25) = " + result);
26+
}
27+
}

SwiftKitExample/build.gradle renamed to Samples/SwiftKitSampleApp/build.gradle

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ plugins {
1616
id("build-logic.java-application-conventions")
1717
}
1818

19-
group = "org.swift.javakit"
19+
group = "org.swift.swiftkit"
2020
version = "1.0-SNAPSHOT"
2121

2222
repositories {
@@ -61,12 +61,16 @@ configurations {
6161
generatedRuntimeOnly.extendsFrom(mainRuntimeOnly)
6262
}
6363

64+
tasks.named("compileJava").configure {
65+
dependsOn("jextract")
66+
}
67+
6468
tasks.test {
6569
useJUnitPlatform()
6670
}
6771

6872
application {
69-
mainClass = "com.example.HelloJava2Swift"
73+
mainClass = "com.example.swift.HelloJava2Swift"
7074

7175
// In order to silence:
7276
// WARNING: A restricted method in java.lang.foreign.SymbolLookup has been called
@@ -79,7 +83,7 @@ application {
7983

8084
// Include the library paths where our dylibs are that we want to load and call
8185
"-Djava.library.path=" + [
82-
"""$rootDir/.build/arm64-apple-macosx/debug/""",
86+
"$rootDir/.build/arm64-apple-macosx/debug/",
8387
"/usr/lib/swift/"
8488
].join(":"),
8589

@@ -88,13 +92,16 @@ application {
8892
]
8993
}
9094

91-
task printSourceSetInformation() {
92-
doLast{
93-
sourceSets.each { srcSet ->
94-
println "["+srcSet.name+"]"
95-
print "--> Source directories: "+srcSet.allJava.srcDirs+"\n"
96-
print "--> Output directories: "+srcSet.output.classesDirs.files+"\n"
97-
println ""
98-
}
99-
}
95+
task jextract(type: Exec) {
96+
description = "Extracts Java accessor sources using jextract"
97+
outputs.dir(layout.buildDirectory.dir("generated"))
98+
inputs.dir("$rootDir/Sources/ExampleSwiftLibrary")
99+
100+
workingDir = rootDir
101+
commandLine "make"
102+
args "jextract-run"
103+
}
104+
105+
tasks.named("compileGeneratedJava").configure {
106+
dependsOn jextract
100107
}

SwiftKitExample/src/main/java/com/example/CallMe.java renamed to Samples/SwiftKitSampleApp/src/main/java/com/example/swift/CallMe.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15-
package com.example;
16-
17-
import org.swift.swiftkit.SwiftKit;
15+
package com.example.swift;
1816

1917
public class CallMe {
2018
public static String callMeStatic() {

SwiftKitExample/src/main/java/com/example/HelloJava2Swift.java renamed to Samples/SwiftKitSampleApp/src/main/java/com/example/swift/HelloJava2Swift.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15-
package com.example;
15+
package com.example.swift;
1616

1717
// Import swift-extract generated sources
1818
import com.example.swift.generated.ExampleSwiftLibrary;

SwiftKitExample/src/test/java/org/swift/swiftkit/MySwiftClassTest.java renamed to Samples/SwiftKitSampleApp/src/test/java/org/swift/swiftkit/MySwiftClassTest.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,9 @@
1515
package org.swift.swiftkit;
1616

1717
import com.example.swift.generated.MySwiftClass;
18-
import com.example.swift.ManualMySwiftClass;
1918
import org.junit.jupiter.api.BeforeAll;
2019
import org.junit.jupiter.api.Test;
2120

22-
import java.lang.foreign.Arena;
23-
2421
import static org.junit.jupiter.api.Assertions.assertEquals;
2522
import static org.junit.jupiter.api.Assertions.assertNotNull;
2623

Sources/ExampleSwiftLibrary/MySwiftLibrary.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ public func helloWorld() {
2828
}
2929

3030
public func globalTakeInt(i: Int) {
31-
p("int:\(i)")
31+
p("i:\(i)")
32+
}
33+
34+
public func globalTakeIntInt(i: Int, j: Int) {
35+
p("i:\(i), j:\(j)")
3236
}
3337

3438
public class MySwiftClass {

Sources/JExtractSwift/Swift2Java.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public struct SwiftToJava: AsyncParsableCommand {
3636

3737
// TODO: Once we ship this, make this `.warning` by default
3838
@Option(name: .shortAndLong, help: "Configure the level of lots that should be printed")
39-
var logLevel: Logger.Level = .trace
39+
var logLevel: Logger.Level = .notice
4040

4141
@Argument(help: "The Swift interface files to export to Java.")
4242
var swiftInterfaceFiles: [String]

Sources/JExtractSwift/Swift2JavaTranslator.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ extension Swift2JavaTranslator {
110110
}
111111

112112
public func postProcessImportedDecls() async throws {
113-
log.info(
113+
log.debug(
114114
"Post process imported decls...",
115115
metadata: [
116116
"types": "\(importedTypes.count)",
@@ -143,7 +143,7 @@ extension Swift2JavaTranslator {
143143

144144
log.info("Mapping members of: \(tyDecl.swiftTypeName)")
145145
tyDecl.initializers = try await tyDecl.initializers._mapAsync { initDecl in
146-
dylib.log.logLevel = .trace
146+
dylib.log.logLevel = .info
147147

148148
let initDecl = try await dylib.fillInAllocatingInitMangledName(initDecl)
149149
log.info("Mapped initializer '\(initDecl.identifier)' -> '\(initDecl.swiftMangledName)'")

0 commit comments

Comments
 (0)